12 #include "moveresize.h"
14 #include "extensions.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 # include <sys/types.h>
47 #include <X11/SM/SMlib.h>
50 #include <X11/cursorfont.h>
53 gboolean ob_sm_use = TRUE;
55 gchar *ob_sm_id = NULL;
58 RrInstance *ob_rr_inst = NULL;
59 RrTheme *ob_rr_theme = NULL;
60 Display *ob_display = NULL;
64 gboolean ob_shutdown = FALSE;
65 gboolean ob_restart = FALSE;
66 char *ob_restart_path = NULL;
67 gboolean ob_remote = TRUE;
68 gboolean ob_sync = FALSE;
70 char *ob_rc_path = NULL;
72 static void signal_handler(const ObEvent *e, void *data);
73 static void parse_args(int argc, char **argv);
75 static void sm_startup(int argc, char **argv);
76 static void sm_shutdown();
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);
85 static void exit_with_error(gchar *msg);
87 int main(int argc, char **argv)
89 struct sigaction action;
95 ob_state = State_Starting;
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);
104 /* start our event dispatcher and register for signals */
106 dispatch_register(Event_Signal, signal_handler, NULL);
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);
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));
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));
132 g_set_prgname(argv[0]);
134 /* parse out command line args */
135 parse_args(argc, argv);
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.");
143 sm_startup(argc, argv);
146 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
149 ob_screen = DefaultScreen(ob_display);
150 ob_root = RootWindow(ob_display, ob_screen);
152 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
153 if (ob_rr_inst == NULL)
154 exit_with_error("Failed to initialize the render library.");
156 /* XXX fork self onto other screens */
158 XSynchronize(ob_display, ob_sync);
160 /* check for locale support */
161 if (!XSupportsLocale())
162 g_warning("X server does not support locale.");
163 if (!XSetLocaleModifiers(""))
164 g_warning("Cannot set locale modifiers for the X server.");
166 /* set our error handler */
167 XSetErrorHandler(xerror_handler);
169 /* set the DISPLAY environment variable for any lauched children, to the
170 display we're using, so they open in the right place. */
171 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
173 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
174 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
175 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
176 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
177 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
178 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
179 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
180 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
181 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
182 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
183 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
185 prop_startup(); /* get atoms values for the display */
186 extensions_query_all(); /* find which extensions are present */
188 /* save stuff that we can use to restore state */
191 if (screen_annex()) { /* it will be ours! */
192 /* startup the parsing so everything can register sections of the rc */
195 /* anything that is going to read data from the rc file needs to be
202 /* load the plugins specified in the pluginrc */
205 /* set up the kernel config shit */
208 /* parse/load user options */
209 if (parse_load_rc(&doc, &node))
210 parse_tree(doc, node->xmlChildrenNode, NULL);
211 /* we're done with parsing now, kill it */
214 /* load the theme specified in the rc file */
215 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
216 if (ob_rr_theme == NULL)
217 exit_with_error("Unable to load a theme.");
220 moveresize_startup();
227 /* call startup for all the plugins */
230 /* get all the existing windows */
233 ob_state = State_Running;
236 ob_state = State_Exiting;
239 client_unmanage_all();
241 plugin_shutdown(); /* calls all the plugins' shutdown functions */
247 moveresize_shutdown();
259 RrThemeFree(ob_rr_theme);
260 RrInstanceFree(ob_rr_inst);
264 XCloseDisplay(ob_display);
267 if (ob_restart_path != NULL) {
273 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
274 execvp(argvp[0], argvp);
277 g_warning("failed to execute '%s': %s", ob_restart_path,
283 execvp(argv[0], argv); /* try how we were run */
284 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
290 static void sm_startup(int argc, char **argv)
294 #define SM_ERR_LEN 1024
297 char sm_err[SM_ERR_LEN];
299 cb.save_yourself.callback = sm_save_yourself;
300 cb.save_yourself.client_data = NULL;
302 cb.die.callback = sm_die;
303 cb.die.client_data = NULL;
305 cb.save_complete.callback = sm_save_complete;
306 cb.save_complete.client_data = NULL;
308 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
309 cb.shutdown_cancelled.client_data = NULL;
311 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
312 SmcSaveYourselfProcMask |
314 SmcSaveCompleteProcMask |
315 SmcShutdownCancelledProcMask,
316 &cb, ob_sm_id, &ob_sm_id,
318 if (ob_sm_conn == NULL)
319 g_warning("Failed to connect to session manager: %s", sm_err);
321 SmPropValue val_prog;
323 SmPropValue val_hint;
326 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
327 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
328 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
329 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
330 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
331 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
332 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
339 for (i = 1; i < argc - 1; ++i)
340 if (strcmp(argv[i], "-sm-client-id") == 0)
342 has_id = (i < argc - 1);
344 prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
345 prop_cmd.num_vals = (has_id ? argc-2 : argc);
346 for (i = 0, j = 0; i < argc; ++i, ++j) {
347 if (strcmp (argv[i], "-sm-client-id") == 0) {
348 ++i, --j; /* skip the next as well, keep j where it is */
350 prop_cmd.vals[j].value = argv[i];
351 prop_cmd.vals[j].length = strlen(argv[i]);
355 prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
356 prop_res.num_vals = (has_id ? argc : argc+2);
357 for (i = 0, j = 0; i < argc; ++i, ++j) {
358 if (strcmp (argv[i], "-sm-client-id") == 0) {
359 ++i, --j; /* skip the next as well, keep j where it is */
361 prop_res.vals[j].value = argv[i];
362 prop_res.vals[j].length = strlen(argv[i]);
365 prop_res.vals[j].value = "-sm-client-id";
366 prop_res.vals[j++].length = strlen("-sm-client-id");
367 prop_res.vals[j].value = ob_sm_id;
368 prop_res.vals[j++].length = strlen(ob_sm_id);
370 val_prog.value = argv[0];
371 val_prog.length = strlen(argv[0]);
373 val_uid.value = g_get_user_name();
374 val_uid.length = strlen(val_uid.value);
376 hint = SmRestartImmediately;
377 val_hint.value = &hint;
380 sprintf(pid, "%ld", (long)getpid());
382 val_pid.length = strlen(pid);
384 /* priority with gnome-session-manager, low to run before other apps */
386 val_pri.value = &pri;
389 prop_prog.vals = &val_prog;
390 prop_uid.vals = &val_uid;
391 prop_hint.vals = &val_hint;
392 prop_pid.vals = &val_pid;
393 prop_pri.vals = &val_pri;
395 props[0] = &prop_prog;
396 props[1] = &prop_cmd;
397 props[2] = &prop_res;
398 props[3] = &prop_uid;
399 props[4] = &prop_hint;
400 props[5] = &prop_pid;
401 props[6] = &prop_pri;
403 SmcSetProperties(ob_sm_conn, 7, props);
405 g_free(prop_cmd.vals);
406 g_free(prop_res.vals);
408 g_message("Connected to session manager with id %s", ob_sm_id);
414 static void sm_shutdown()
418 SmPropValue val_hint;
419 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
423 /* when we exit, we want to reset this to a more friendly state */
424 hint = SmRestartIfRunning;
425 val_hint.value = &hint;
428 prop_hint.vals = &val_hint;
430 props[0] = &prop_hint;
432 SmcSetProperties(ob_sm_conn, 1, props);
434 SmcCloseConnection(ob_sm_conn, 0, NULL);
439 static void signal_handler(const ObEvent *e, void *data)
443 s = e->data.s.signal;
446 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
447 ob_shutdown = ob_restart = TRUE;
454 fprintf(stderr, "Caught signal %d. Exiting.", s);
460 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
465 static void print_version()
467 g_print("Openbox %s\n\n", PACKAGE_VERSION);
468 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
469 g_print("This is free software, and you are welcome to redistribute it\n");
470 g_print("under certain conditions. See the file COPYING for details.\n\n");
473 static void print_help()
476 g_print("Syntax: %s [options]\n\n", BINARY);
477 g_print("Options:\n\n");
478 g_print(" -rc PATH Specify the path to the rc file to use\n");
480 g_print(" -sm-client-id ID Specify session management ID\n");
481 g_print(" -sm-disable Disable connection to session manager\n");
483 g_print(" -help Display this help and exit\n");
484 g_print(" -version Display the version and exit\n");
485 g_print(" -sync Run in synchronous mode (this is slow and\n"
486 " meant for debugging X routines)\n");
487 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
490 static void parse_args(int argc, char **argv)
494 for (i = 1; i < argc; ++i) {
495 if (!strcmp(argv[i], "-version")) {
498 } else if (!strcmp(argv[i], "-help")) {
501 } else if (!strcmp(argv[i], "-sync")) {
503 } else if (!strcmp(argv[i], "-rc")) {
504 if (i == argc - 1) /* no args left */
505 g_printerr(_("-rc requires an argument\n"));
507 ob_rc_path = argv[++i];
509 } else if (!strcmp(argv[i], "-sm-client-id")) {
510 if (i == argc - 1) /* no args left */
511 g_printerr(_("-sm-client-id requires an argument\n"));
513 ob_sm_id = argv[++i];
514 } else if (!strcmp(argv[i], "-sm-disable")) {
518 g_printerr("Invalid option: '%s'\n\n", argv[i]);
525 gboolean ob_pointer_pos(int *x, int *y)
531 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
535 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
536 Bool shutdown, int interact_style, Bool fast)
538 g_message("got SAVE YOURSELF from session manager");
539 SmcSaveYourselfDone(conn, TRUE);
542 static void sm_die(SmcConn conn, SmPointer data)
545 g_message("got DIE from session manager");
548 static void sm_save_complete(SmcConn conn, SmPointer data)
550 g_message("got SAVE COMPLETE from session manager");
553 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
555 g_message("got SHUTDOWN CANCELLED from session manager");
559 static void exit_with_error(gchar *msg)