]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
reorder startup so menu goes before config parsing
[chaz/openbox] / openbox / openbox.c
1 #include "openbox.h"
2 #include "dock.h"
3 #include "event.h"
4 #include "menu.h"
5 #include "client.h"
6 #include "dispatch.h"
7 #include "xerror.h"
8 #include "prop.h"
9 #include "startup.h"
10 #include "screen.h"
11 #include "focus.h"
12 #include "moveresize.h"
13 #include "frame.h"
14 #include "extensions.h"
15 #include "grab.h"
16 #include "plugin.h"
17 #include "timer.h"
18 #include "group.h"
19 #include "config.h"
20 #include "gettext.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
24
25 #ifdef HAVE_FCNTL_H
26 # include <fcntl.h>
27 #endif
28 #ifdef HAVE_SIGNAL_H
29 # include <signal.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #ifdef HAVE_LOCALE_H
35 # include <locale.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
43 #endif
44
45 #ifdef USE_SM
46 #include <X11/SM/SMlib.h>
47 #endif
48
49 #include <X11/cursorfont.h>
50
51 #ifdef USE_SM
52 gboolean ob_sm_use = TRUE;
53 SmcConn ob_sm_conn;
54 gchar *ob_sm_id = NULL;
55 #endif
56
57 RrInstance *ob_rr_inst = NULL;
58 RrTheme *ob_rr_theme = NULL;
59 Display *ob_display = NULL;
60 int ob_screen;
61 Window ob_root;
62 State ob_state;
63 gboolean ob_shutdown = FALSE;
64 gboolean ob_restart = FALSE;
65 char *ob_restart_path = NULL;
66 gboolean ob_remote = TRUE;
67 gboolean ob_sync = FALSE;
68 Cursors ob_cursors;
69 char *ob_rc_path = NULL;
70
71 static void signal_handler(const ObEvent *e, void *data);
72 static void parse_args(int argc, char **argv);
73
74 static void sm_startup(int argc, char **argv);
75 static void sm_shutdown();
76
77 #ifdef USE_SM
78 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
79 Bool shutdown, int interact_style, Bool fast);
80 static void sm_die(SmcConn conn, SmPointer data);
81 static void sm_save_complete(SmcConn conn, SmPointer data);
82 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
83 #endif
84 static void exit_with_error(gchar *msg);
85
86 int main(int argc, char **argv)
87 {
88 struct sigaction action;
89 sigset_t sigset;
90 char *path;
91 xmlDocPtr doc;
92 xmlNodePtr node;
93
94 ob_state = State_Starting;
95
96 /* initialize the locale */
97 if (!setlocale(LC_ALL, ""))
98 g_warning("Couldn't set locale from environment.\n");
99 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
100 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
101 textdomain(PACKAGE_NAME);
102
103 /* start our event dispatcher and register for signals */
104 dispatch_startup();
105 dispatch_register(Event_Signal, signal_handler, NULL);
106
107 /* set up signal handler */
108 sigemptyset(&sigset);
109 action.sa_handler = dispatch_signal;
110 action.sa_mask = sigset;
111 action.sa_flags = SA_NOCLDSTOP;
112 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
113 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
114 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
115 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
116 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
117 sigaction(SIGINT, &action, (struct sigaction *) NULL);
118 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
119
120 /* create the ~/.openbox dir */
121 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
122 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
123 S_IROTH | S_IWOTH | S_IXOTH));
124 g_free(path);
125 /* create the ~/.openbox/themes dir */
126 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
127 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
128 S_IROTH | S_IWOTH | S_IXOTH));
129 g_free(path);
130
131 /* parse out command line args */
132 parse_args(argc, argv);
133
134 ob_display = XOpenDisplay(NULL);
135 if (ob_display == NULL)
136 exit_with_error("Failed to open the display.");
137 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
138 exit_with_error("Failed to set display as close-on-exec.");
139
140 sm_startup(argc, argv);
141
142 #ifdef USE_LIBSN
143 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
144 #endif
145
146 ob_screen = DefaultScreen(ob_display);
147 ob_root = RootWindow(ob_display, ob_screen);
148
149 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
150 if (ob_rr_inst == NULL)
151 exit_with_error("Failed to initialize the render library.");
152
153 /* XXX fork self onto other screens */
154
155 XSynchronize(ob_display, ob_sync);
156
157 /* check for locale support */
158 if (!XSupportsLocale())
159 g_warning("X server does not support locale.");
160 if (!XSetLocaleModifiers(""))
161 g_warning("Cannot set locale modifiers for the X server.");
162
163 /* set our error handler */
164 XSetErrorHandler(xerror_handler);
165
166 /* set the DISPLAY environment variable for any lauched children, to the
167 display we're using, so they open in the right place. */
168 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
169
170 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
171 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
172 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
173 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
174 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
175 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
176 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
177 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
178 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
179 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
180 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
181
182 prop_startup(); /* get atoms values for the display */
183 extensions_query_all(); /* find which extensions are present */
184
185 /* save stuff that we can use to restore state */
186 startup_save();
187
188 if (screen_annex()) { /* it will be ours! */
189 /* startup the parsing so everything can register sections of the rc */
190 parse_startup();
191
192 /* anything that is going to read data from the rc file needs to be
193 in this group */
194 timer_startup();
195 event_startup();
196 grab_startup();
197 window_startup();
198 plugin_startup();
199 /* load the plugins specified in the pluginrc */
200 plugin_loadall();
201
202 /* set up the kernel config shit */
203 config_startup();
204 menu_startup();
205 /* parse/load user options */
206 if (parse_load_rc(&doc, &node))
207 parse_tree(doc, node->xmlChildrenNode, NULL);
208 /* we're done with parsing now, kill it */
209 parse_shutdown();
210
211 /* load the theme specified in the rc file */
212 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
213 if (ob_rr_theme == NULL)
214 exit_with_error("Unable to load a theme.");
215
216 frame_startup();
217 moveresize_startup();
218 focus_startup();
219 screen_startup();
220 group_startup();
221 client_startup();
222 dock_startup();
223
224 /* call startup for all the plugins */
225 plugin_startall();
226
227 /* get all the existing windows */
228 client_manage_all();
229
230 ob_state = State_Running;
231 while (!ob_shutdown)
232 event_loop();
233 ob_state = State_Exiting;
234
235 dock_remove_all();
236 client_unmanage_all();
237
238 plugin_shutdown(); /* calls all the plugins' shutdown functions */
239 dock_shutdown();
240 client_shutdown();
241 group_shutdown();
242 screen_shutdown();
243 focus_shutdown();
244 moveresize_shutdown();
245 frame_shutdown();
246 menu_shutdown();
247 window_shutdown();
248 grab_shutdown();
249 event_shutdown();
250 timer_shutdown();
251 config_shutdown();
252 }
253
254 dispatch_shutdown();
255
256 RrThemeFree(ob_rr_theme);
257 RrInstanceFree(ob_rr_inst);
258
259 sm_shutdown();
260
261 XCloseDisplay(ob_display);
262
263 if (ob_restart) {
264 if (ob_restart_path != NULL) {
265 int argcp;
266 char **argvp;
267 GError *err = NULL;
268
269 /* run other shit */
270 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
271 execvp(argvp[0], argvp);
272 g_strfreev(argvp);
273 } else {
274 g_warning("failed to execute '%s': %s", ob_restart_path,
275 err->message);
276 }
277 }
278
279 /* re-run me */
280 execvp(argv[0], argv); /* try how we were run */
281 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
282 }
283
284 return 0;
285 }
286
287 static void sm_startup(int argc, char **argv)
288 {
289 #ifdef USE_SM
290
291 #define SM_ERR_LEN 1024
292
293 SmcCallbacks cb;
294 char sm_err[SM_ERR_LEN];
295
296 cb.save_yourself.callback = sm_save_yourself;
297 cb.save_yourself.client_data = NULL;
298
299 cb.die.callback = sm_die;
300 cb.die.client_data = NULL;
301
302 cb.save_complete.callback = sm_save_complete;
303 cb.save_complete.client_data = NULL;
304
305 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
306 cb.shutdown_cancelled.client_data = NULL;
307
308 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
309 SmcSaveYourselfProcMask |
310 SmcDieProcMask |
311 SmcSaveCompleteProcMask |
312 SmcShutdownCancelledProcMask,
313 &cb, ob_sm_id, &ob_sm_id,
314 SM_ERR_LEN, sm_err);
315 if (ob_sm_conn == NULL)
316 g_warning("Failed to connect to session manager: %s", sm_err);
317 else {
318 SmPropValue val_prog;
319 SmPropValue val_uid;
320 SmPropValue val_hint;
321 SmPropValue val_pri;
322 SmPropValue val_pid;
323 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
324 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
325 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
326 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
327 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
328 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
329 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
330 SmProp *props[7];
331 gulong hint, pri;
332 gchar pid[32];
333 gint i, j;
334 gboolean has_id;
335
336 for (i = 1; i < argc - 1; ++i)
337 if (strcmp(argv[i], "-sm-client-id") == 0)
338 break;
339 has_id = (i < argc - 1);
340
341 prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
342 prop_cmd.num_vals = (has_id ? argc-2 : argc);
343 for (i = 0, j = 0; i < argc; ++i, ++j) {
344 if (strcmp (argv[i], "-sm-client-id") == 0) {
345 ++i, --j; /* skip the next as well, keep j where it is */
346 } else {
347 prop_cmd.vals[j].value = argv[i];
348 prop_cmd.vals[j].length = strlen(argv[i]);
349 }
350 }
351
352 prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
353 prop_res.num_vals = (has_id ? argc : argc+2);
354 for (i = 0, j = 0; i < argc; ++i, ++j) {
355 if (strcmp (argv[i], "-sm-client-id") == 0) {
356 ++i, --j; /* skip the next as well, keep j where it is */
357 } else {
358 prop_res.vals[j].value = argv[i];
359 prop_res.vals[j].length = strlen(argv[i]);
360 }
361 }
362 prop_res.vals[j].value = "-sm-client-id";
363 prop_res.vals[j++].length = strlen("-sm-client-id");
364 prop_res.vals[j].value = ob_sm_id;
365 prop_res.vals[j++].length = strlen(ob_sm_id);
366
367 val_prog.value = argv[0];
368 val_prog.length = strlen(argv[0]);
369
370 val_uid.value = g_get_user_name();
371 val_uid.length = strlen(val_uid.value);
372
373 hint = SmRestartImmediately;
374 val_hint.value = &hint;
375 val_hint.length = 1;
376
377 sprintf(pid, "%ld", (long)getpid());
378 val_pid.value = pid;
379 val_pid.length = strlen(pid);
380
381 /* priority with gnome-session-manager, low to run before other apps */
382 pri = 20;
383 val_pri.value = &pri;
384 val_pri.length = 1;
385
386 prop_prog.vals = &val_prog;
387 prop_uid.vals = &val_uid;
388 prop_hint.vals = &val_hint;
389 prop_pid.vals = &val_pid;
390 prop_pri.vals = &val_pri;
391
392 props[0] = &prop_prog;
393 props[1] = &prop_cmd;
394 props[2] = &prop_res;
395 props[3] = &prop_uid;
396 props[4] = &prop_hint;
397 props[5] = &prop_pid;
398 props[6] = &prop_pri;
399
400 SmcSetProperties(ob_sm_conn, 7, props);
401
402 g_free(prop_cmd.vals);
403 g_free(prop_res.vals);
404
405 g_message("Connected to session manager with id %s", ob_sm_id);
406 }
407 g_free (ob_sm_id);
408 #endif
409 }
410
411 static void sm_shutdown()
412 {
413 #ifdef USE_SM
414 if (ob_sm_conn) {
415 SmPropValue val_hint;
416 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
417 SmProp *props[1];
418 gulong hint;
419
420 /* when we exit, we want to reset this to a more friendly state */
421 hint = SmRestartIfRunning;
422 val_hint.value = &hint;
423 val_hint.length = 1;
424
425 prop_hint.vals = &val_hint;
426
427 props[0] = &prop_hint;
428
429 SmcSetProperties(ob_sm_conn, 1, props);
430
431 SmcCloseConnection(ob_sm_conn, 0, NULL);
432 }
433 #endif
434 }
435
436 static void signal_handler(const ObEvent *e, void *data)
437 {
438 int s;
439
440 s = e->data.s.signal;
441 switch (s) {
442 case SIGUSR1:
443 g_message("Caught SIGUSR1 signal. Restarting.");
444 ob_shutdown = ob_restart = TRUE;
445 break;
446
447 case SIGHUP:
448 case SIGINT:
449 case SIGTERM:
450 case SIGPIPE:
451 g_message("Caught signal %d. Exiting.", s);
452 ob_shutdown = TRUE;
453 break;
454
455 case SIGFPE:
456 case SIGSEGV:
457 g_message("Caught signal %d. Aborting and dumping core.", s);
458 abort();
459 }
460 }
461
462 static void print_version()
463 {
464 g_print("Openbox %s\n\n", PACKAGE_VERSION);
465 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
466 g_print("This is free software, and you are welcome to redistribute it\n");
467 g_print("under certain conditions. See the file COPYING for details.\n\n");
468 }
469
470 static void print_help()
471 {
472 print_version();
473 g_print("Syntax: %s [options]\n\n", BINARY);
474 g_print("Options:\n\n");
475 g_print(" -rc PATH Specify the path to the rc file to use\n");
476 #ifdef USE_SM
477 g_print(" -sm-client-id ID Specify session management ID\n");
478 g_print(" -sm-disable Disable connection to session manager\n");
479 #endif
480 g_print(" -help Display this help and exit\n");
481 g_print(" -version Display the version and exit\n");
482 g_print(" -sync Run in synchronous mode (this is slow and\n"
483 " meant for debugging X routines)\n");
484 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
485 }
486
487 static void parse_args(int argc, char **argv)
488 {
489 int i;
490
491 for (i = 1; i < argc; ++i) {
492 if (!strcmp(argv[i], "-version")) {
493 print_version();
494 exit(0);
495 } else if (!strcmp(argv[i], "-help")) {
496 print_help();
497 exit(0);
498 } else if (!strcmp(argv[i], "-sync")) {
499 ob_sync = TRUE;
500 } else if (!strcmp(argv[i], "-rc")) {
501 if (i == argc - 1) /* no args left */
502 g_printerr(_("-rc requires an argument\n"));
503 else
504 ob_rc_path = argv[++i];
505 #ifdef USE_SM
506 } else if (!strcmp(argv[i], "-sm-client-id")) {
507 if (i == argc - 1) /* no args left */
508 g_printerr(_("-sm-client-id requires an argument\n"));
509 else
510 ob_sm_id = argv[++i];
511 } else if (!strcmp(argv[i], "-sm-disable")) {
512 ob_sm_use = FALSE;
513 #endif
514 } else {
515 g_printerr("Invalid option: '%s'\n\n", argv[i]);
516 print_help();
517 exit(1);
518 }
519 }
520 }
521
522 gboolean ob_pointer_pos(int *x, int *y)
523 {
524 Window w;
525 int i;
526 guint u;
527
528 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
529 }
530
531 #ifdef USE_SM
532 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
533 Bool shutdown, int interact_style, Bool fast)
534 {
535 g_message("got SAVE YOURSELF from session manager");
536 SmcSaveYourselfDone(conn, TRUE);
537 }
538
539 static void sm_die(SmcConn conn, SmPointer data)
540 {
541 ob_shutdown = TRUE;
542 g_message("got DIE from session manager");
543 }
544
545 static void sm_save_complete(SmcConn conn, SmPointer data)
546 {
547 g_message("got SAVE COMPLETE from session manager");
548 }
549
550 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
551 {
552 g_message("got SHUTDOWN CANCELLED from session manager");
553 }
554 #endif
555
556 static void exit_with_error(gchar *msg)
557 {
558 g_critical(msg);
559 sm_shutdown();
560 exit(EXIT_FAILURE);
561 }
This page took 0.062354 seconds and 5 git commands to generate.