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