12 #include "moveresize.h"
14 #include "extensions.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
46 #include <X11/SM/SMlib.h>
49 #include <X11/cursorfont.h>
53 gchar *ob_sm_id = NULL;
56 RrInstance *ob_rr_inst = NULL;
57 RrTheme *ob_rr_theme = NULL;
58 Display *ob_display = NULL;
62 gboolean ob_shutdown = FALSE;
63 gboolean ob_restart = FALSE;
64 char *ob_restart_path = NULL;
65 gboolean ob_remote = TRUE;
66 gboolean ob_sync = FALSE;
68 char *ob_rc_path = NULL;
70 static void signal_handler(const ObEvent *e, void *data);
71 static void parse_args(int argc, char **argv);
73 static void sm_startup(int argc, char **argv);
74 static void sm_shutdown();
77 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
78 Bool shutdown, int interact_style, Bool fast);
79 static void sm_die(SmcConn conn, SmPointer data);
80 static void sm_save_complete(SmcConn conn, SmPointer data);
81 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
83 static void exit_with_error(gchar *msg);
85 int main(int argc, char **argv)
87 struct sigaction action;
93 ob_state = State_Starting;
95 /* initialize the locale */
96 if (!setlocale(LC_ALL, ""))
97 g_warning("Couldn't set locale from environment.\n");
98 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
99 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
100 textdomain(PACKAGE_NAME);
102 /* start our event dispatcher and register for signals */
104 dispatch_register(Event_Signal, signal_handler, NULL);
106 /* set up signal handler */
107 sigemptyset(&sigset);
108 action.sa_handler = dispatch_signal;
109 action.sa_mask = sigset;
110 action.sa_flags = SA_NOCLDSTOP;
111 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
112 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
113 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
114 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
115 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
116 sigaction(SIGINT, &action, (struct sigaction *) NULL);
117 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
119 /* create the ~/.openbox dir */
120 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
121 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
122 S_IROTH | S_IWOTH | S_IXOTH));
124 /* create the ~/.openbox/themes dir */
125 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
126 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
127 S_IROTH | S_IWOTH | S_IXOTH));
130 /* parse out command line args */
131 parse_args(argc, argv);
133 ob_display = XOpenDisplay(NULL);
134 if (ob_display == NULL)
135 exit_with_error("Failed to open the display.");
136 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
137 exit_with_error("Failed to set display as close-on-exec.");
139 sm_startup(argc, argv);
142 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
145 ob_screen = DefaultScreen(ob_display);
146 ob_root = RootWindow(ob_display, ob_screen);
148 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
149 if (ob_rr_inst == NULL)
150 exit_with_error("Failed to initialize the render library.");
152 /* XXX fork self onto other screens */
154 XSynchronize(ob_display, ob_sync);
156 /* check for locale support */
157 if (!XSupportsLocale())
158 g_warning("X server does not support locale.");
159 if (!XSetLocaleModifiers(""))
160 g_warning("Cannot set locale modifiers for the X server.");
162 /* set our error handler */
163 XSetErrorHandler(xerror_handler);
165 /* set the DISPLAY environment variable for any lauched children, to the
166 display we're using, so they open in the right place. */
167 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
169 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
170 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
171 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
172 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
173 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
174 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
175 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
176 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
177 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
178 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
179 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
181 prop_startup(); /* get atoms values for the display */
182 extensions_query_all(); /* find which extensions are present */
184 /* save stuff that we can use to restore state */
187 if (screen_annex()) { /* it will be ours! */
188 /* startup the parsing so everything can register sections of the rc */
191 /* anything that is going to read data from the rc file needs to be
197 /* load the plugins specified in the pluginrc */
200 /* set up the kernel config shit */
202 /* parse/load user options */
203 if (parse_load_rc(&doc, &node))
204 parse_tree(doc, node->xmlChildrenNode, NULL);
205 /* we're done with parsing now, kill it */
208 /* load the theme specified in the rc file */
209 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
210 if (ob_rr_theme == NULL)
211 exit_with_error("Unable to load a theme.");
216 moveresize_startup();
223 /* call startup for all the plugins */
226 /* get all the existing windows */
229 ob_state = State_Running;
232 ob_state = State_Exiting;
235 client_unmanage_all();
237 plugin_shutdown(); /* calls all the plugins' shutdown functions */
243 moveresize_shutdown();
255 RrThemeFree(ob_rr_theme);
256 RrInstanceFree(ob_rr_inst);
260 XCloseDisplay(ob_display);
263 if (ob_restart_path != NULL) {
269 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
270 execvp(argvp[0], argvp);
273 g_warning("failed to execute '%s': %s", ob_restart_path,
279 execvp(argv[0], argv); /* try how we were run */
280 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
286 static void sm_startup(int argc, char **argv)
290 #define SM_ERR_LEN 1024
293 char sm_err[SM_ERR_LEN];
295 cb.save_yourself.callback = sm_save_yourself;
296 cb.save_yourself.client_data = NULL;
298 cb.die.callback = sm_die;
299 cb.die.client_data = NULL;
301 cb.save_complete.callback = sm_save_complete;
302 cb.save_complete.client_data = NULL;
304 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
305 cb.shutdown_cancelled.client_data = NULL;
307 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
308 SmcSaveYourselfProcMask |
310 SmcSaveCompleteProcMask |
311 SmcShutdownCancelledProcMask,
312 &cb, ob_sm_id, &ob_sm_id,
314 if (ob_sm_conn == NULL)
315 g_warning("Failed to connect to session manager: %s", sm_err);
317 SmPropValue val_prog;
319 SmPropValue val_hint;
322 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
323 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
324 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
325 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
326 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
327 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
328 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
335 for (i = 1; i < argc - 1; ++i)
336 if (strcmp(argv[i], "-sm-client-id") == 0)
338 has_id = (i < argc - 1);
340 prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
341 prop_cmd.num_vals = (has_id ? argc-2 : argc);
342 for (i = 0; i < argc; ++i) {
343 if (strcmp (argv[i], "-sm-client-id") == 0) {
344 ++i; /* skip the next as well */
346 prop_cmd.vals[i].value = argv[i];
347 prop_cmd.vals[i].length = strlen(argv[i]);
351 prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
352 prop_res.num_vals = (has_id ? argc : argc+2);
353 for (i = 0; i < argc; ++i) {
354 prop_res.vals[i].value = argv[i];
355 prop_res.vals[i].length = strlen(argv[i]);
358 prop_res.vals[i].value = "-sm-client-id";
359 prop_res.vals[i++].length = strlen("-sm-client-id");
360 prop_res.vals[i].value = ob_sm_id;
361 prop_res.vals[i++].length = strlen(ob_sm_id);
364 val_prog.value = argv[0];
365 val_prog.length = strlen(argv[0]);
367 val_uid.value = g_get_user_name();
368 val_uid.length = strlen(val_uid.value);
370 hint = SmRestartImmediately;
371 val_hint.value = &hint;
374 sprintf(pid, "%ld", (long)getpid());
376 val_pid.length = strlen(pid);
378 /* priority with gnome-session-manager, low to run before other apps */
380 val_pri.value = &pri;
383 prop_prog.vals = &val_prog;
384 prop_uid.vals = &val_uid;
385 prop_hint.vals = &val_hint;
386 prop_pid.vals = &val_pid;
387 prop_pri.vals = &val_pri;
389 props[0] = &prop_prog;
390 props[1] = &prop_cmd;
391 props[2] = &prop_res;
392 props[3] = &prop_uid;
393 props[4] = &prop_hint;
394 props[5] = &prop_pid;
395 props[6] = &prop_pri;
397 SmcSetProperties(ob_sm_conn, 7, props);
399 g_free(prop_cmd.vals);
400 g_free(prop_res.vals);
402 g_message("Connected to session manager with id %s", ob_sm_id);
408 static void sm_shutdown()
412 SmPropValue val_hint;
413 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
417 /* when we exit, we want to reset this to a more friendly state */
418 hint = SmRestartIfRunning;
419 val_hint.value = &hint;
422 prop_hint.vals = &val_hint;
424 props[0] = &prop_hint;
426 SmcSetProperties(ob_sm_conn, 1, props);
428 SmcCloseConnection(ob_sm_conn, 0, NULL);
433 static void signal_handler(const ObEvent *e, void *data)
437 s = e->data.s.signal;
440 g_message("Caught SIGUSR1 signal. Restarting.");
441 ob_shutdown = ob_restart = TRUE;
448 g_message("Caught signal %d. Exiting.", s);
454 g_message("Caught signal %d. Aborting and dumping core.", s);
459 static void print_version()
461 g_print("Openbox %s\n\n", PACKAGE_VERSION);
462 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
463 g_print("This is free software, and you are welcome to redistribute it\n");
464 g_print("under certain conditions. See the file COPYING for details.\n\n");
467 static void print_help()
470 g_print("Syntax: %s [options]\n\n", BINARY);
471 g_print("Options:\n\n");
472 g_print(" -rc PATH Specify the path to the rc file to use\n");
473 g_print(" -sm-client-id ID Specify the session manager ID\n");
474 g_print(" -help Display this help and exit\n");
475 g_print(" -version Display the version and exit\n");
476 g_print(" -sync Run in synchronous mode (this is slow and\n"
477 " meant for debugging X routines)\n");
478 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
481 static void parse_args(int argc, char **argv)
485 for (i = 1; i < argc; ++i) {
486 if (!strcmp(argv[i], "-version")) {
489 } else if (!strcmp(argv[i], "-help")) {
492 } else if (!strcmp(argv[i], "-sync")) {
494 } else if (!strcmp(argv[i], "-rc")) {
495 if (i == argc - 1) /* no args left */
496 g_printerr(_("-rc requires an argument\n"));
498 ob_rc_path = argv[++i];
500 } else if (!strcmp(argv[i], "-sm-client-id")) {
501 if (i == argc - 1) /* no args left */
502 g_printerr(_("-sm-client-id requires an argument\n"));
504 ob_sm_id = argv[++i];
507 g_printerr("Invalid option: '%s'\n\n", argv[i]);
514 gboolean ob_pointer_pos(int *x, int *y)
520 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
524 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
525 Bool shutdown, int interact_style, Bool fast)
527 g_message("got SAVE YOURSELF from session manager");
528 SmcSaveYourselfDone(conn, TRUE);
531 static void sm_die(SmcConn conn, SmPointer data)
534 g_message("got DIE from session manager");
537 static void sm_save_complete(SmcConn conn, SmPointer data)
539 g_message("got SAVE COMPLETE from session manager");
542 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
544 g_message("got SHUTDOWN CANCELLED from session manager");
548 static void exit_with_error(gchar *msg)