13 #include "moveresize.h"
15 #include "extensions.h"
22 #include "parser/parse.h"
23 #include "render/render.h"
24 #include "render/theme.h"
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 # include <sys/types.h>
48 #include <X11/SM/SMlib.h>
51 #include <X11/cursorfont.h>
54 gboolean ob_sm_use = TRUE;
59 RrInstance *ob_rr_inst;
63 gboolean ob_replace_wm;
66 static gboolean xsync;
67 static gboolean shutdown;
68 static gboolean restart;
69 static char *restart_path;
70 static Cursor cursors[OB_NUM_CURSORS];
71 static KeyCode keys[OB_NUM_KEYS];
73 static void signal_handler(const ObEvent *e, void *data);
74 static void parse_args(int argc, char **argv);
76 static void sm_startup(int argc, char **argv);
77 static void sm_shutdown();
80 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
81 Bool shutdown, int interact_style, Bool fast);
82 static void sm_die(SmcConn conn, SmPointer data);
83 static void sm_save_complete(SmcConn conn, SmPointer data);
84 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
86 static void exit_with_error(gchar *msg);
88 int main(int argc, char **argv)
90 struct sigaction action;
97 ob_debug_show_output(TRUE);
100 state = OB_STATE_STARTING;
102 /* initialize the locale */
103 if (!setlocale(LC_ALL, ""))
104 g_warning("Couldn't set locale from environment.\n");
105 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
106 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
107 textdomain(PACKAGE_NAME);
109 /* start our event dispatcher and register for signals */
111 dispatch_register(Event_Signal, signal_handler, NULL);
113 /* set up signal handler */
114 sigemptyset(&sigset);
115 action.sa_handler = dispatch_signal;
116 action.sa_mask = sigset;
117 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
118 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
119 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
120 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
121 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
122 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
123 sigaction(SIGINT, &action, (struct sigaction *) NULL);
124 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
126 /* create the ~/.openbox dir */
127 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
128 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
129 S_IROTH | S_IWOTH | S_IXOTH));
131 /* create the ~/.openbox/themes dir */
132 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
133 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
134 S_IROTH | S_IWOTH | S_IXOTH));
137 g_set_prgname(argv[0]);
139 /* parse out command line args */
140 parse_args(argc, argv);
142 ob_display = XOpenDisplay(NULL);
143 if (ob_display == NULL)
144 exit_with_error("Failed to open the display.");
145 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
146 exit_with_error("Failed to set display as close-on-exec.");
148 sm_startup(argc, argv);
151 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
154 ob_screen = DefaultScreen(ob_display);
156 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
157 if (ob_rr_inst == NULL)
158 exit_with_error("Failed to initialize the render library.");
160 /* XXX fork self onto other screens */
162 XSynchronize(ob_display, xsync);
164 /* check for locale support */
165 if (!XSupportsLocale())
166 g_warning("X server does not support locale.");
167 if (!XSetLocaleModifiers(""))
168 g_warning("Cannot set locale modifiers for the X server.");
170 /* set our error handler */
171 XSetErrorHandler(xerror_handler);
173 /* set the DISPLAY environment variable for any lauched children, to the
174 display we're using, so they open in the right place. */
175 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
177 /* create available cursors */
178 cursors[OB_CURSOR_POINTER] =
179 XCreateFontCursor(ob_display, XC_left_ptr);
180 cursors[OB_CURSOR_BUSY] =
181 XCreateFontCursor(ob_display, XC_watch);
182 cursors[OB_CURSOR_MOVE] =
183 XCreateFontCursor(ob_display, XC_fleur);
184 cursors[OB_CURSOR_NORTH] =
185 XCreateFontCursor(ob_display, XC_top_side);
186 cursors[OB_CURSOR_NORTHEAST] =
187 XCreateFontCursor(ob_display, XC_top_right_corner);
188 cursors[OB_CURSOR_EAST] =
189 XCreateFontCursor(ob_display, XC_right_side);
190 cursors[OB_CURSOR_SOUTHEAST] =
191 XCreateFontCursor(ob_display, XC_bottom_right_corner);
192 cursors[OB_CURSOR_SOUTH] =
193 XCreateFontCursor(ob_display, XC_bottom_side);
194 cursors[OB_CURSOR_SOUTHWEST] =
195 XCreateFontCursor(ob_display, XC_bottom_left_corner);
196 cursors[OB_CURSOR_WEST] =
197 XCreateFontCursor(ob_display, XC_left_side);
198 cursors[OB_CURSOR_NORTHWEST] =
199 XCreateFontCursor(ob_display, XC_top_left_corner);
201 /* create available keycodes */
202 keys[OB_KEY_RETURN] =
203 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
204 keys[OB_KEY_ESCAPE] =
205 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
207 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
209 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
211 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
213 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
215 prop_startup(); /* get atoms values for the display */
216 extensions_query_all(); /* find which extensions are present */
218 /* save stuff that we can use to restore state */
221 if (screen_annex()) { /* it will be ours! */
222 /* startup the parsing so everything can register sections of the rc */
225 /* anything that is going to read data from the rc file needs to be
230 /* focus_backup is used for stacking, so this needs to come before
231 anything that calls stacking_add */
235 /* load the plugins specified in the pluginrc */
238 /* set up the kernel config shit */
241 /* parse/load user options */
242 if (parse_load_rc(&doc, &node))
243 parse_tree(doc, node->xmlChildrenNode, NULL);
244 /* we're done with parsing now, kill it */
247 /* load the theme specified in the rc file */
248 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
249 if (ob_rr_theme == NULL)
250 exit_with_error("Unable to load a theme.");
252 moveresize_startup();
258 /* call startup for all the plugins */
261 /* get all the existing windows */
264 state = OB_STATE_RUNNING;
267 state = OB_STATE_EXITING;
270 client_unmanage_all();
272 plugin_shutdown(); /* calls all the plugins' shutdown functions */
278 moveresize_shutdown();
289 RrThemeFree(ob_rr_theme);
290 RrInstanceFree(ob_rr_inst);
294 XCloseDisplay(ob_display);
297 if (restart_path != NULL) {
303 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
304 execvp(argvp[0], argvp);
307 g_warning("failed to execute '%s': %s", restart_path,
313 execvp(argv[0], argv); /* try how we were run */
319 static void sm_startup(int argc, char **argv)
323 #define SM_ERR_LEN 1024
326 char sm_err[SM_ERR_LEN];
328 cb.save_yourself.callback = sm_save_yourself;
329 cb.save_yourself.client_data = NULL;
331 cb.die.callback = sm_die;
332 cb.die.client_data = NULL;
334 cb.save_complete.callback = sm_save_complete;
335 cb.save_complete.client_data = NULL;
337 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
338 cb.shutdown_cancelled.client_data = NULL;
340 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
341 SmcSaveYourselfProcMask |
343 SmcSaveCompleteProcMask |
344 SmcShutdownCancelledProcMask,
345 &cb, ob_sm_id, &ob_sm_id,
347 if (ob_sm_conn == NULL)
348 g_warning("Failed to connect to session manager: %s", sm_err);
350 SmPropValue val_prog;
352 SmPropValue val_hint;
355 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
356 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
357 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
358 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
359 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
360 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
361 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
368 for (i = 1; i < argc - 1; ++i)
369 if (strcmp(argv[i], "--sm-client-id") == 0)
371 has_id = (i < argc - 1);
373 prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
374 prop_cmd.num_vals = (has_id ? argc-2 : argc);
375 for (i = 0, j = 0; i < argc; ++i, ++j) {
376 if (strcmp (argv[i], "--sm-client-id") == 0) {
377 ++i, --j; /* skip the next as well, keep j where it is */
379 prop_cmd.vals[j].value = argv[i];
380 prop_cmd.vals[j].length = strlen(argv[i]);
384 prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
385 prop_res.num_vals = (has_id ? argc : argc+2);
386 for (i = 0, j = 0; i < argc; ++i, ++j) {
387 if (strcmp (argv[i], "--sm-client-id") == 0) {
388 ++i, --j; /* skip the next as well, keep j where it is */
390 prop_res.vals[j].value = argv[i];
391 prop_res.vals[j].length = strlen(argv[i]);
394 prop_res.vals[j].value = "--sm-client-id";
395 prop_res.vals[j++].length = strlen("--sm-client-id");
396 prop_res.vals[j].value = ob_sm_id;
397 prop_res.vals[j++].length = strlen(ob_sm_id);
399 val_prog.value = argv[0];
400 val_prog.length = strlen(argv[0]);
402 val_uid.value = g_strdup(g_get_user_name());
403 val_uid.length = strlen(val_uid.value);
405 hint = SmRestartImmediately;
406 val_hint.value = &hint;
409 sprintf(pid, "%ld", (long)getpid());
411 val_pid.length = strlen(pid);
413 /* priority with gnome-session-manager, low to run before other apps */
415 val_pri.value = &pri;
418 prop_prog.vals = &val_prog;
419 prop_uid.vals = &val_uid;
420 prop_hint.vals = &val_hint;
421 prop_pid.vals = &val_pid;
422 prop_pri.vals = &val_pri;
424 props[0] = &prop_prog;
425 props[1] = &prop_cmd;
426 props[2] = &prop_res;
427 props[3] = &prop_uid;
428 props[4] = &prop_hint;
429 props[5] = &prop_pid;
430 props[6] = &prop_pri;
432 SmcSetProperties(ob_sm_conn, 7, props);
434 g_free(val_uid.value);
435 g_free(prop_cmd.vals);
436 g_free(prop_res.vals);
438 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
444 static void sm_shutdown()
448 SmPropValue val_hint;
449 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
453 /* when we exit, we want to reset this to a more friendly state */
454 hint = SmRestartIfRunning;
455 val_hint.value = &hint;
458 prop_hint.vals = &val_hint;
460 props[0] = &prop_hint;
462 SmcSetProperties(ob_sm_conn, 1, props);
464 SmcCloseConnection(ob_sm_conn, 0, NULL);
469 static void signal_handler(const ObEvent *e, void *data)
473 s = e->data.s.signal;
476 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
484 fprintf(stderr, "Caught signal %d. Exiting.", s);
490 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
495 static void print_version()
497 g_print("Openbox %s\n\n", PACKAGE_VERSION);
498 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
499 g_print("This is free software, and you are welcome to redistribute it\n");
500 g_print("under certain conditions. See the file COPYING for details.\n\n");
503 static void print_help()
506 g_print("Syntax: openbox [options]\n\n");
507 g_print("Options:\n\n");
509 g_print(" --sm-client-id ID Specify session management ID\n");
510 g_print(" --sm-disable Disable connection to session manager\n");
512 g_print(" --replace Replace the currently running window "
514 g_print(" --help Display this help and exit\n");
515 g_print(" --version Display the version and exit\n");
516 g_print(" --sync Run in synchronous mode (this is slow and\n"
517 " meant for debugging X routines)\n");
518 g_print(" --debug Display debugging output\n");
519 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
522 static void parse_args(int argc, char **argv)
526 for (i = 1; i < argc; ++i) {
527 if (!strcmp(argv[i], "--version")) {
530 } else if (!strcmp(argv[i], "--help")) {
533 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
534 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
535 } else if (!strcmp(argv[i], "--replace")) {
536 ob_replace_wm = TRUE;
537 } else if (!strcmp(argv[i], "--sync")) {
539 } else if (!strcmp(argv[i], "--debug")) {
540 ob_debug_show_output(TRUE);
542 } else if (!strcmp(argv[i], "--sm-client-id")) {
543 if (i == argc - 1) /* no args left */
544 g_printerr(_("--sm-client-id requires an argument\n"));
546 ob_sm_id = argv[++i];
547 } else if (!strcmp(argv[i], "--sm-disable")) {
551 g_printerr("Invalid option: '%s'\n\n", argv[i]);
559 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
560 Bool shutdown, int interact_style, Bool fast)
562 ob_debug("got SAVE YOURSELF from session manager\n");
563 SmcSaveYourselfDone(conn, TRUE);
566 static void sm_die(SmcConn conn, SmPointer data)
569 ob_debug("got DIE from session manager\n");
572 static void sm_save_complete(SmcConn conn, SmPointer data)
574 ob_debug("got SAVE COMPLETE from session manager\n");
577 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
579 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
583 static void exit_with_error(gchar *msg)
590 void ob_restart_other(const gchar *path)
592 restart_path = g_strdup(path);
607 Cursor ob_cursor(ObCursor cursor)
609 g_assert(cursor < OB_NUM_CURSORS);
610 return cursors[cursor];
613 KeyCode ob_keycode(ObKey key)
615 g_assert(key < OB_NUM_KEYS);