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