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