]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
only show -sm-client-id in the help when session management is build in
[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 SmcConn ob_sm_conn;
53 gchar *ob_sm_id = NULL;
54 #endif
55
56 RrInstance *ob_rr_inst = NULL;
57 RrTheme *ob_rr_theme = NULL;
58 Display *ob_display = NULL;
59 int ob_screen;
60 Window ob_root;
61 State ob_state;
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;
67 Cursors ob_cursors;
68 char *ob_rc_path = NULL;
69
70 static void signal_handler(const ObEvent *e, void *data);
71 static void parse_args(int argc, char **argv);
72
73 static void sm_startup(int argc, char **argv);
74 static void sm_shutdown();
75
76 #ifdef USE_SM
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);
82 #endif
83 static void exit_with_error(gchar *msg);
84
85 int main(int argc, char **argv)
86 {
87 struct sigaction action;
88 sigset_t sigset;
89 char *path;
90 xmlDocPtr doc;
91 xmlNodePtr node;
92
93 ob_state = State_Starting;
94
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);
101
102 /* start our event dispatcher and register for signals */
103 dispatch_startup();
104 dispatch_register(Event_Signal, signal_handler, NULL);
105
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);
118
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));
123 g_free(path);
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));
128 g_free(path);
129
130 /* parse out command line args */
131 parse_args(argc, argv);
132
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.");
138
139 sm_startup(argc, argv);
140
141 #ifdef USE_LIBSN
142 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
143 #endif
144
145 ob_screen = DefaultScreen(ob_display);
146 ob_root = RootWindow(ob_display, ob_screen);
147
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.");
151
152 /* XXX fork self onto other screens */
153
154 XSynchronize(ob_display, ob_sync);
155
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.");
161
162 /* set our error handler */
163 XSetErrorHandler(xerror_handler);
164
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)));
168
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);
180
181 prop_startup(); /* get atoms values for the display */
182 extensions_query_all(); /* find which extensions are present */
183
184 /* save stuff that we can use to restore state */
185 startup_save();
186
187 if (screen_annex()) { /* it will be ours! */
188 /* startup the parsing so everything can register sections of the rc */
189 parse_startup();
190
191 /* anything that is going to read data from the rc file needs to be
192 in this group */
193 timer_startup();
194 event_startup();
195 grab_startup();
196 plugin_startup();
197 /* load the plugins specified in the pluginrc */
198 plugin_loadall();
199
200 /* set up the kernel config shit */
201 config_startup();
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 */
206 parse_shutdown();
207
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.");
212
213 window_startup();
214 menu_startup();
215 frame_startup();
216 moveresize_startup();
217 focus_startup();
218 screen_startup();
219 group_startup();
220 client_startup();
221 dock_startup();
222
223 /* call startup for all the plugins */
224 plugin_startall();
225
226 /* get all the existing windows */
227 client_manage_all();
228
229 ob_state = State_Running;
230 while (!ob_shutdown)
231 event_loop();
232 ob_state = State_Exiting;
233
234 dock_remove_all();
235 client_unmanage_all();
236
237 plugin_shutdown(); /* calls all the plugins' shutdown functions */
238 dock_shutdown();
239 client_shutdown();
240 group_shutdown();
241 screen_shutdown();
242 focus_shutdown();
243 moveresize_shutdown();
244 frame_shutdown();
245 menu_shutdown();
246 window_shutdown();
247 grab_shutdown();
248 event_shutdown();
249 timer_shutdown();
250 config_shutdown();
251 }
252
253 dispatch_shutdown();
254
255 RrThemeFree(ob_rr_theme);
256 RrInstanceFree(ob_rr_inst);
257
258 sm_shutdown();
259
260 XCloseDisplay(ob_display);
261
262 if (ob_restart) {
263 if (ob_restart_path != NULL) {
264 int argcp;
265 char **argvp;
266 GError *err = NULL;
267
268 /* run other shit */
269 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
270 execvp(argvp[0], argvp);
271 g_strfreev(argvp);
272 } else {
273 g_warning("failed to execute '%s': %s", ob_restart_path,
274 err->message);
275 }
276 }
277
278 /* re-run me */
279 execvp(argv[0], argv); /* try how we were run */
280 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
281 }
282
283 return 0;
284 }
285
286 static void sm_startup(int argc, char **argv)
287 {
288 #ifdef USE_SM
289
290 #define SM_ERR_LEN 1024
291
292 SmcCallbacks cb;
293 char sm_err[SM_ERR_LEN];
294
295 cb.save_yourself.callback = sm_save_yourself;
296 cb.save_yourself.client_data = NULL;
297
298 cb.die.callback = sm_die;
299 cb.die.client_data = NULL;
300
301 cb.save_complete.callback = sm_save_complete;
302 cb.save_complete.client_data = NULL;
303
304 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
305 cb.shutdown_cancelled.client_data = NULL;
306
307 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
308 SmcSaveYourselfProcMask |
309 SmcDieProcMask |
310 SmcSaveCompleteProcMask |
311 SmcShutdownCancelledProcMask,
312 &cb, ob_sm_id, &ob_sm_id,
313 SM_ERR_LEN, sm_err);
314 if (ob_sm_conn == NULL)
315 g_warning("Failed to connect to session manager: %s", sm_err);
316 else {
317 SmPropValue val_prog;
318 SmPropValue val_uid;
319 SmPropValue val_hint;
320 SmPropValue val_pri;
321 SmPropValue val_pid;
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, };
329 SmProp *props[7];
330 gulong hint, pri;
331 gchar pid[32];
332 gint i;
333 gboolean has_id;
334
335 for (i = 1; i < argc - 1; ++i)
336 if (strcmp(argv[i], "-sm-client-id") == 0)
337 break;
338 has_id = (i < argc - 1);
339
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 */
345 } else {
346 prop_cmd.vals[i].value = argv[i];
347 prop_cmd.vals[i].length = strlen(argv[i]);
348 }
349 }
350
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]);
356 }
357 if (!has_id) {
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);
362 }
363
364 val_prog.value = argv[0];
365 val_prog.length = strlen(argv[0]);
366
367 val_uid.value = g_get_user_name();
368 val_uid.length = strlen(val_uid.value);
369
370 hint = SmRestartImmediately;
371 val_hint.value = &hint;
372 val_hint.length = 1;
373
374 sprintf(pid, "%ld", (long)getpid());
375 val_pid.value = pid;
376 val_pid.length = strlen(pid);
377
378 /* priority with gnome-session-manager, low to run before other apps */
379 pri = 20;
380 val_pri.value = &pri;
381 val_pri.length = 1;
382
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;
388
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;
396
397 SmcSetProperties(ob_sm_conn, 7, props);
398
399 g_free(prop_cmd.vals);
400 g_free(prop_res.vals);
401
402 g_message("Connected to session manager with id %s", ob_sm_id);
403 }
404 g_free (ob_sm_id);
405 #endif
406 }
407
408 static void sm_shutdown()
409 {
410 #ifdef USE_SM
411 if (ob_sm_conn) {
412 SmPropValue val_hint;
413 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
414 SmProp *props[1];
415 gulong hint;
416
417 /* when we exit, we want to reset this to a more friendly state */
418 hint = SmRestartIfRunning;
419 val_hint.value = &hint;
420 val_hint.length = 1;
421
422 prop_hint.vals = &val_hint;
423
424 props[0] = &prop_hint;
425
426 SmcSetProperties(ob_sm_conn, 1, props);
427
428 SmcCloseConnection(ob_sm_conn, 0, NULL);
429 }
430 #endif
431 }
432
433 static void signal_handler(const ObEvent *e, void *data)
434 {
435 int s;
436
437 s = e->data.s.signal;
438 switch (s) {
439 case SIGUSR1:
440 g_message("Caught SIGUSR1 signal. Restarting.");
441 ob_shutdown = ob_restart = TRUE;
442 break;
443
444 case SIGHUP:
445 case SIGINT:
446 case SIGTERM:
447 case SIGPIPE:
448 g_message("Caught signal %d. Exiting.", s);
449 ob_shutdown = TRUE;
450 break;
451
452 case SIGFPE:
453 case SIGSEGV:
454 g_message("Caught signal %d. Aborting and dumping core.", s);
455 abort();
456 }
457 }
458
459 static void print_version()
460 {
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");
465 }
466
467 static void print_help()
468 {
469 print_version();
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 #ifdef USE_SM
474 g_print(" -sm-client-id ID Specify the session manager ID\n");
475 #endif
476 g_print(" -help Display this help and exit\n");
477 g_print(" -version Display the version and exit\n");
478 g_print(" -sync Run in synchronous mode (this is slow and\n"
479 " meant for debugging X routines)\n");
480 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
481 }
482
483 static void parse_args(int argc, char **argv)
484 {
485 int i;
486
487 for (i = 1; i < argc; ++i) {
488 if (!strcmp(argv[i], "-version")) {
489 print_version();
490 exit(0);
491 } else if (!strcmp(argv[i], "-help")) {
492 print_help();
493 exit(0);
494 } else if (!strcmp(argv[i], "-sync")) {
495 ob_sync = TRUE;
496 } else if (!strcmp(argv[i], "-rc")) {
497 if (i == argc - 1) /* no args left */
498 g_printerr(_("-rc requires an argument\n"));
499 else
500 ob_rc_path = argv[++i];
501 #ifdef USE_SM
502 } else if (!strcmp(argv[i], "-sm-client-id")) {
503 if (i == argc - 1) /* no args left */
504 g_printerr(_("-sm-client-id requires an argument\n"));
505 else
506 ob_sm_id = argv[++i];
507 #endif
508 } else {
509 g_printerr("Invalid option: '%s'\n\n", argv[i]);
510 print_help();
511 exit(1);
512 }
513 }
514 }
515
516 gboolean ob_pointer_pos(int *x, int *y)
517 {
518 Window w;
519 int i;
520 guint u;
521
522 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
523 }
524
525 #ifdef USE_SM
526 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
527 Bool shutdown, int interact_style, Bool fast)
528 {
529 g_message("got SAVE YOURSELF from session manager");
530 SmcSaveYourselfDone(conn, TRUE);
531 }
532
533 static void sm_die(SmcConn conn, SmPointer data)
534 {
535 ob_shutdown = TRUE;
536 g_message("got DIE from session manager");
537 }
538
539 static void sm_save_complete(SmcConn conn, SmPointer data)
540 {
541 g_message("got SAVE COMPLETE from session manager");
542 }
543
544 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
545 {
546 g_message("got SHUTDOWN CANCELLED from session manager");
547 }
548 #endif
549
550 static void exit_with_error(gchar *msg)
551 {
552 g_critical(msg);
553 sm_shutdown();
554 exit(EXIT_FAILURE);
555 }
This page took 0.0603 seconds and 5 git commands to generate.