]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
ob_remote has been killed, remove ob_sync from the public header, its only used inter...
[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 int ob_screen;
62 Window ob_root;
63 ObState ob_state;
64 Cursor ob_cursors[OB_NUM_CURSORS];
65 KeyCode ob_keys[OB_NUM_KEYS];
66
67 static gboolean sync;
68 static gboolean shutdown;
69 static gboolean restart;
70 static char *restart_path;
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 ob_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 ob_root = RootWindow(ob_display, ob_screen);
151
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.");
155
156 /* XXX fork self onto other screens */
157
158 XSynchronize(ob_display, sync);
159
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.");
165
166 /* set our error handler */
167 XSetErrorHandler(xerror_handler);
168
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)));
172
173 /* create available cursors */
174 ob_cursors[OB_CURSOR_POINTER] =
175 XCreateFontCursor(ob_display, XC_left_ptr);
176 ob_cursors[OB_CURSOR_BUSY] =
177 XCreateFontCursor(ob_display, XC_watch);
178 ob_cursors[OB_CURSOR_MOVE] =
179 XCreateFontCursor(ob_display, XC_fleur);
180 ob_cursors[OB_CURSOR_NORTH] =
181 XCreateFontCursor(ob_display, XC_top_side);
182 ob_cursors[OB_CURSOR_NORTHEAST] =
183 XCreateFontCursor(ob_display, XC_top_right_corner);
184 ob_cursors[OB_CURSOR_EAST] =
185 XCreateFontCursor(ob_display, XC_right_side);
186 ob_cursors[OB_CURSOR_SOUTHEAST] =
187 XCreateFontCursor(ob_display, XC_bottom_right_corner);
188 ob_cursors[OB_CURSOR_SOUTH] =
189 XCreateFontCursor(ob_display, XC_bottom_side);
190 ob_cursors[OB_CURSOR_SOUTHWEST] =
191 XCreateFontCursor(ob_display, XC_bottom_left_corner);
192 ob_cursors[OB_CURSOR_WEST] =
193 XCreateFontCursor(ob_display, XC_left_side);
194 ob_cursors[OB_CURSOR_NORTHWEST] =
195 XCreateFontCursor(ob_display, XC_top_left_corner);
196
197 /* create available keycodes */
198 ob_keys[OB_KEY_RETURN] =
199 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
200 ob_keys[OB_KEY_ESCAPE] =
201 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
202 ob_keys[OB_KEY_LEFT] =
203 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
204 ob_keys[OB_KEY_RIGHT] =
205 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
206 ob_keys[OB_KEY_UP] =
207 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
208 ob_keys[OB_KEY_DOWN] =
209 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
210
211 prop_startup(); /* get atoms values for the display */
212 extensions_query_all(); /* find which extensions are present */
213
214 /* save stuff that we can use to restore state */
215 startup_save();
216
217 if (screen_annex()) { /* it will be ours! */
218 /* startup the parsing so everything can register sections of the rc */
219 parse_startup();
220
221 /* anything that is going to read data from the rc file needs to be
222 in this group */
223 timer_startup();
224 event_startup();
225 grab_startup();
226 /* focus_backup is used for stacking, so this needs to come before
227 anything that calls stacking_add */
228 focus_startup();
229 window_startup();
230 plugin_startup();
231 /* load the plugins specified in the pluginrc */
232 plugin_loadall();
233
234 /* set up the kernel config shit */
235 config_startup();
236 menu_startup();
237 /* parse/load user options */
238 if (parse_load_rc(&doc, &node))
239 parse_tree(doc, node->xmlChildrenNode, NULL);
240 /* we're done with parsing now, kill it */
241 parse_shutdown();
242
243 /* load the theme specified in the rc file */
244 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
245 if (ob_rr_theme == NULL)
246 exit_with_error("Unable to load a theme.");
247
248 frame_startup();
249 moveresize_startup();
250 screen_startup();
251 group_startup();
252 client_startup();
253 dock_startup();
254
255 /* call startup for all the plugins */
256 plugin_startall();
257
258 /* get all the existing windows */
259 client_manage_all();
260
261 ob_state = OB_STATE_RUNNING;
262 while (!shutdown)
263 event_loop();
264 ob_state = OB_STATE_EXITING;
265
266 dock_remove_all();
267 client_unmanage_all();
268
269 plugin_shutdown(); /* calls all the plugins' shutdown functions */
270 dock_shutdown();
271 client_shutdown();
272 group_shutdown();
273 screen_shutdown();
274 focus_shutdown();
275 moveresize_shutdown();
276 frame_shutdown();
277 menu_shutdown();
278 window_shutdown();
279 grab_shutdown();
280 event_shutdown();
281 timer_shutdown();
282 config_shutdown();
283 }
284
285 dispatch_shutdown();
286
287 RrThemeFree(ob_rr_theme);
288 RrInstanceFree(ob_rr_inst);
289
290 sm_shutdown();
291
292 XCloseDisplay(ob_display);
293
294 if (restart) {
295 if (restart_path != NULL) {
296 int argcp;
297 char **argvp;
298 GError *err = NULL;
299
300 /* run other shit */
301 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
302 execvp(argvp[0], argvp);
303 g_strfreev(argvp);
304 } else {
305 g_warning("failed to execute '%s': %s", restart_path,
306 err->message);
307 }
308 }
309
310 /* re-run me */
311 execvp(argv[0], argv); /* try how we were run */
312 }
313
314 return 0;
315 }
316
317 static void sm_startup(int argc, char **argv)
318 {
319 #ifdef USE_SM
320
321 #define SM_ERR_LEN 1024
322
323 SmcCallbacks cb;
324 char sm_err[SM_ERR_LEN];
325
326 cb.save_yourself.callback = sm_save_yourself;
327 cb.save_yourself.client_data = NULL;
328
329 cb.die.callback = sm_die;
330 cb.die.client_data = NULL;
331
332 cb.save_complete.callback = sm_save_complete;
333 cb.save_complete.client_data = NULL;
334
335 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
336 cb.shutdown_cancelled.client_data = NULL;
337
338 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
339 SmcSaveYourselfProcMask |
340 SmcDieProcMask |
341 SmcSaveCompleteProcMask |
342 SmcShutdownCancelledProcMask,
343 &cb, ob_sm_id, &ob_sm_id,
344 SM_ERR_LEN, sm_err);
345 if (ob_sm_conn == NULL)
346 g_warning("Failed to connect to session manager: %s", sm_err);
347 else {
348 SmPropValue val_prog;
349 SmPropValue val_uid;
350 SmPropValue val_hint;
351 SmPropValue val_pri;
352 SmPropValue val_pid;
353 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
354 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
355 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
356 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
357 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
358 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
359 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
360 SmProp *props[7];
361 gulong hint, pri;
362 gchar pid[32];
363 gint i, j;
364 gboolean has_id;
365
366 for (i = 1; i < argc - 1; ++i)
367 if (strcmp(argv[i], "--sm-client-id") == 0)
368 break;
369 has_id = (i < argc - 1);
370
371 prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
372 prop_cmd.num_vals = (has_id ? argc-2 : argc);
373 for (i = 0, j = 0; i < argc; ++i, ++j) {
374 if (strcmp (argv[i], "--sm-client-id") == 0) {
375 ++i, --j; /* skip the next as well, keep j where it is */
376 } else {
377 prop_cmd.vals[j].value = argv[i];
378 prop_cmd.vals[j].length = strlen(argv[i]);
379 }
380 }
381
382 prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
383 prop_res.num_vals = (has_id ? argc : argc+2);
384 for (i = 0, j = 0; i < argc; ++i, ++j) {
385 if (strcmp (argv[i], "--sm-client-id") == 0) {
386 ++i, --j; /* skip the next as well, keep j where it is */
387 } else {
388 prop_res.vals[j].value = argv[i];
389 prop_res.vals[j].length = strlen(argv[i]);
390 }
391 }
392 prop_res.vals[j].value = "--sm-client-id";
393 prop_res.vals[j++].length = strlen("--sm-client-id");
394 prop_res.vals[j].value = ob_sm_id;
395 prop_res.vals[j++].length = strlen(ob_sm_id);
396
397 val_prog.value = argv[0];
398 val_prog.length = strlen(argv[0]);
399
400 val_uid.value = g_strdup(g_get_user_name());
401 val_uid.length = strlen(val_uid.value);
402
403 hint = SmRestartImmediately;
404 val_hint.value = &hint;
405 val_hint.length = 1;
406
407 sprintf(pid, "%ld", (long)getpid());
408 val_pid.value = pid;
409 val_pid.length = strlen(pid);
410
411 /* priority with gnome-session-manager, low to run before other apps */
412 pri = 20;
413 val_pri.value = &pri;
414 val_pri.length = 1;
415
416 prop_prog.vals = &val_prog;
417 prop_uid.vals = &val_uid;
418 prop_hint.vals = &val_hint;
419 prop_pid.vals = &val_pid;
420 prop_pri.vals = &val_pri;
421
422 props[0] = &prop_prog;
423 props[1] = &prop_cmd;
424 props[2] = &prop_res;
425 props[3] = &prop_uid;
426 props[4] = &prop_hint;
427 props[5] = &prop_pid;
428 props[6] = &prop_pri;
429
430 SmcSetProperties(ob_sm_conn, 7, props);
431
432 g_free(val_uid.value);
433 g_free(prop_cmd.vals);
434 g_free(prop_res.vals);
435
436 g_message("Connected to session manager with id %s", ob_sm_id);
437 }
438 g_free (ob_sm_id);
439 #endif
440 }
441
442 static void sm_shutdown()
443 {
444 #ifdef USE_SM
445 if (ob_sm_conn) {
446 SmPropValue val_hint;
447 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
448 SmProp *props[1];
449 gulong hint;
450
451 /* when we exit, we want to reset this to a more friendly state */
452 hint = SmRestartIfRunning;
453 val_hint.value = &hint;
454 val_hint.length = 1;
455
456 prop_hint.vals = &val_hint;
457
458 props[0] = &prop_hint;
459
460 SmcSetProperties(ob_sm_conn, 1, props);
461
462 SmcCloseConnection(ob_sm_conn, 0, NULL);
463 }
464 #endif
465 }
466
467 static void signal_handler(const ObEvent *e, void *data)
468 {
469 int s;
470
471 s = e->data.s.signal;
472 switch (s) {
473 case SIGUSR1:
474 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
475 ob_restart();
476 break;
477
478 case SIGHUP:
479 case SIGINT:
480 case SIGTERM:
481 case SIGPIPE:
482 fprintf(stderr, "Caught signal %d. Exiting.", s);
483 ob_exit();
484 break;
485
486 case SIGFPE:
487 case SIGSEGV:
488 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
489 abort();
490 }
491 }
492
493 static void print_version()
494 {
495 g_print("Openbox %s\n\n", PACKAGE_VERSION);
496 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
497 g_print("This is free software, and you are welcome to redistribute it\n");
498 g_print("under certain conditions. See the file COPYING for details.\n\n");
499 }
500
501 static void print_help()
502 {
503 print_version();
504 g_print("Syntax: openbox [options]\n\n");
505 g_print("Options:\n\n");
506 #ifdef USE_SM
507 g_print(" --sm-client-id ID Specify session management ID\n");
508 g_print(" --sm-disable Disable connection to session manager\n");
509 #endif
510 g_print(" --help Display this help and exit\n");
511 g_print(" --version Display the version and exit\n");
512 g_print(" --sync Run in synchronous mode (this is slow and\n"
513 " meant for debugging X routines)\n");
514 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
515 }
516
517 static void parse_args(int argc, char **argv)
518 {
519 int i;
520
521 for (i = 1; i < argc; ++i) {
522 if (!strcmp(argv[i], "--version")) {
523 print_version();
524 exit(0);
525 } else if (!strcmp(argv[i], "--help")) {
526 print_help();
527 exit(0);
528 } else if (!strcmp(argv[i], "--sync")) {
529 sync = TRUE;
530 #ifdef USE_SM
531 } else if (!strcmp(argv[i], "--sm-client-id")) {
532 if (i == argc - 1) /* no args left */
533 g_printerr(_("--sm-client-id requires an argument\n"));
534 else
535 ob_sm_id = argv[++i];
536 } else if (!strcmp(argv[i], "--sm-disable")) {
537 ob_sm_use = FALSE;
538 #endif
539 } else {
540 g_printerr("Invalid option: '%s'\n\n", argv[i]);
541 print_help();
542 exit(1);
543 }
544 }
545 }
546
547 gboolean ob_pointer_pos(int *x, int *y)
548 {
549 Window w;
550 int i;
551 guint u;
552
553 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
554 }
555
556 #ifdef USE_SM
557 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
558 Bool shutdown, int interact_style, Bool fast)
559 {
560 g_message("got SAVE YOURSELF from session manager");
561 SmcSaveYourselfDone(conn, TRUE);
562 }
563
564 static void sm_die(SmcConn conn, SmPointer data)
565 {
566 ob_exit();
567 g_message("got DIE from session manager");
568 }
569
570 static void sm_save_complete(SmcConn conn, SmPointer data)
571 {
572 g_message("got SAVE COMPLETE from session manager");
573 }
574
575 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
576 {
577 g_message("got SHUTDOWN CANCELLED from session manager");
578 }
579 #endif
580
581 static void exit_with_error(gchar *msg)
582 {
583 g_critical(msg);
584 sm_shutdown();
585 exit(EXIT_FAILURE);
586 }
587
588 void ob_restart_other(const gchar *path)
589 {
590 restart_path = g_strdup(path);
591 ob_restart();
592 }
593
594 void ob_restart()
595 {
596 restart = TRUE;
597 ob_exit();
598 }
599
600 void ob_exit()
601 {
602 shutdown = TRUE;
603 }
604
605 Cursor ob_cursor(ObCursor cursor)
606 {
607 g_assert(cursor < OB_NUM_CURSORS);
608 return ob_cursors[cursor];
609 }
610
611 KeyCode ob_keycode(ObKey key)
612 {
613 g_assert(key < OB_NUM_KEYS);
614 return ob_keys[key];
615 }
This page took 0.05871 seconds and 5 git commands to generate.