]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
expose exit_with_error
[dana/openbox.git] / openbox / openbox.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "session.h"
4 #include "dock.h"
5 #include "event.h"
6 #include "menu.h"
7 #include "client.h"
8 #include "dispatch.h"
9 #include "xerror.h"
10 #include "prop.h"
11 #include "startup.h"
12 #include "screen.h"
13 #include "focus.h"
14 #include "moveresize.h"
15 #include "frame.h"
16 #include "extensions.h"
17 #include "grab.h"
18 #include "plugin.h"
19 #include "timer.h"
20 #include "group.h"
21 #include "config.h"
22 #include "gettext.h"
23 #include "parser/parse.h"
24 #include "render/render.h"
25 #include "render/theme.h"
26
27 #ifdef HAVE_FCNTL_H
28 #  include <fcntl.h>
29 #endif
30 #ifdef HAVE_SIGNAL_H
31 #define __USE_UNIX98
32 #  include <signal.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #  include <stdlib.h>
36 #endif
37 #ifdef HAVE_LOCALE_H
38 #  include <locale.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 #  include <sys/stat.h>
42 #  include <sys/types.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #  include <unistd.h>
46 #endif
47
48 #include <X11/cursorfont.h>
49
50 RrInstance *ob_rr_inst;
51 RrTheme    *ob_rr_theme;
52 Display    *ob_display;
53 gint        ob_screen;
54 gboolean    ob_sm_use = TRUE;
55 gchar      *ob_sm_id;
56 gboolean    ob_replace_wm;
57
58 static ObState   state;
59 static gboolean  xsync;
60 static gboolean  shutdown;
61 static gboolean  restart;
62 static char     *restart_path;
63 static Cursor    cursors[OB_NUM_CURSORS];
64 static KeyCode   keys[OB_NUM_KEYS];
65 static gchar    *sm_save_file;
66
67 static void signal_handler(const ObEvent *e, void *data);
68 static void parse_args(int argc, char **argv);
69
70 int main(int argc, char **argv)
71 {
72     struct sigaction action;
73     sigset_t sigset;
74     char *path;
75     xmlDocPtr doc;
76     xmlNodePtr node;
77
78 #ifdef DEBUG
79     ob_debug_show_output(TRUE);
80 #endif
81
82     state = OB_STATE_STARTING;
83
84     /* initialize the locale */
85     if (!setlocale(LC_ALL, ""))
86         g_warning("Couldn't set locale from environment.\n");
87     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
88     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
89     textdomain(PACKAGE_NAME);
90
91     /* start our event dispatcher and register for signals */
92     dispatch_startup();
93     dispatch_register(Event_Signal, signal_handler, NULL);
94
95     /* set up signal handler */
96     sigemptyset(&sigset);
97     action.sa_handler = dispatch_signal;
98     action.sa_mask = sigset;
99     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
100     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
101     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
102 /*    sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
103     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
104     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
105     sigaction(SIGINT, &action, (struct sigaction *) NULL);
106     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
107
108     /* create the ~/.openbox dir */
109     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
110     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
111                  S_IROTH | S_IWOTH | S_IXOTH));
112     g_free(path);
113     /* create the ~/.openbox/themes dir */
114     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
115     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
116                  S_IROTH | S_IWOTH | S_IXOTH));
117     g_free(path);
118     /* create the ~/.openbox/sessions dir */
119     path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
120     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
121                  S_IROTH | S_IWOTH | S_IXOTH));
122     g_free(path);
123
124     g_set_prgname(argv[0]);
125      
126     /* parse out command line args */
127     parse_args(argc, argv);
128
129     ob_display = XOpenDisplay(NULL);
130     if (ob_display == NULL)
131         exit_with_error("Failed to open the display.");
132     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
133         ob_exit_with_error("Failed to set display as close-on-exec.");
134
135     if (sm_save_file)
136         session_load(sm_save_file);
137     session_startup(argc, argv);
138
139 #ifdef USE_LIBSN
140     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
141 #endif
142
143     ob_screen = DefaultScreen(ob_display);
144
145     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
146     if (ob_rr_inst == NULL)
147         ob_exit_with_error("Failed to initialize the render library.");
148
149     /* XXX fork self onto other screens */
150      
151     XSynchronize(ob_display, xsync);
152
153     /* check for locale support */
154     if (!XSupportsLocale())
155         g_warning("X server does not support locale.");
156     if (!XSetLocaleModifiers(""))
157         g_warning("Cannot set locale modifiers for the X server.");
158
159     /* set our error handler */
160     XSetErrorHandler(xerror_handler);
161
162     /* set the DISPLAY environment variable for any lauched children, to the
163        display we're using, so they open in the right place. */
164     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
165
166     /* create available cursors */
167     cursors[OB_CURSOR_POINTER] =
168         XCreateFontCursor(ob_display, XC_left_ptr);
169     cursors[OB_CURSOR_BUSY] =
170         XCreateFontCursor(ob_display, XC_watch);
171     cursors[OB_CURSOR_MOVE] =
172         XCreateFontCursor(ob_display, XC_fleur);
173     cursors[OB_CURSOR_NORTH] =
174         XCreateFontCursor(ob_display, XC_top_side);
175     cursors[OB_CURSOR_NORTHEAST] =
176         XCreateFontCursor(ob_display, XC_top_right_corner);
177     cursors[OB_CURSOR_EAST] =
178         XCreateFontCursor(ob_display, XC_right_side);
179     cursors[OB_CURSOR_SOUTHEAST] =
180         XCreateFontCursor(ob_display, XC_bottom_right_corner);
181     cursors[OB_CURSOR_SOUTH] =
182         XCreateFontCursor(ob_display, XC_bottom_side);
183     cursors[OB_CURSOR_SOUTHWEST] =
184         XCreateFontCursor(ob_display, XC_bottom_left_corner);
185     cursors[OB_CURSOR_WEST] =
186         XCreateFontCursor(ob_display, XC_left_side);
187     cursors[OB_CURSOR_NORTHWEST] =
188         XCreateFontCursor(ob_display, XC_top_left_corner);
189
190     /* create available keycodes */
191     keys[OB_KEY_RETURN] =
192         XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
193     keys[OB_KEY_ESCAPE] =
194         XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
195     keys[OB_KEY_LEFT] =
196         XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
197     keys[OB_KEY_RIGHT] =
198         XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
199     keys[OB_KEY_UP] =
200         XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
201     keys[OB_KEY_DOWN] =
202         XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
203
204     prop_startup(); /* get atoms values for the display */
205     extensions_query_all(); /* find which extensions are present */
206
207     /* save stuff that we can use to restore state */
208     startup_save();
209
210     if (screen_annex()) { /* it will be ours! */
211         /* startup the parsing so everything can register sections of the rc */
212         parse_startup();
213
214         /* anything that is going to read data from the rc file needs to be 
215            in this group */
216         timer_startup();
217         event_startup();
218         grab_startup();
219         /* focus_backup is used for stacking, so this needs to come before
220            anything that calls stacking_add */
221         focus_startup();
222         window_startup();
223         plugin_startup();
224         /* load the plugins specified in the pluginrc */
225         plugin_loadall();
226
227         /* set up the kernel config shit */
228         config_startup();
229         menu_startup();
230         /* parse/load user options */
231         if (parse_load_rc(&doc, &node))
232             parse_tree(doc, node->xmlChildrenNode, NULL);
233         /* we're done with parsing now, kill it */
234         parse_shutdown();
235
236         /* load the theme specified in the rc file */
237         ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
238         if (ob_rr_theme == NULL)
239             ob_exit_with_error("Unable to load a theme.");
240
241         moveresize_startup();
242         screen_startup();
243         group_startup();
244         client_startup();
245         dock_startup();
246
247         /* call startup for all the plugins */
248         plugin_startall();
249
250         /* get all the existing windows */
251         client_manage_all();
252
253         state = OB_STATE_RUNNING;
254         while (!shutdown)
255             event_loop();
256         state = OB_STATE_EXITING;
257
258         dock_remove_all();
259         client_unmanage_all();
260
261         plugin_shutdown(); /* calls all the plugins' shutdown functions */
262         dock_shutdown();
263         client_shutdown();
264         group_shutdown();
265         screen_shutdown();
266         focus_shutdown();
267         moveresize_shutdown();
268         menu_shutdown();
269         window_shutdown();
270         grab_shutdown();
271         event_shutdown();
272         timer_shutdown();
273         config_shutdown();
274     }
275
276     dispatch_shutdown();
277
278     RrThemeFree(ob_rr_theme);
279     RrInstanceFree(ob_rr_inst);
280
281     session_shutdown();
282     g_free(ob_sm_id);
283
284     XCloseDisplay(ob_display);
285
286     if (restart) {
287         if (restart_path != NULL) {
288             int argcp;
289             char **argvp;
290             GError *err = NULL;
291
292             /* run other shit */
293             if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
294                 execvp(argvp[0], argvp);
295                 g_strfreev(argvp);
296             } else {
297                 g_warning("failed to execute '%s': %s", restart_path,
298                           err->message);
299             }
300         }
301
302         /* re-run me */
303         execvp(argv[0], argv); /* try how we were run */
304     }
305      
306     return 0;
307 }
308
309 static void signal_handler(const ObEvent *e, void *data)
310 {
311     int s;
312
313     s = e->data.s.signal;
314     switch (s) {
315     case SIGUSR1:
316         fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
317         ob_restart();
318         break;
319
320     case SIGHUP:
321     case SIGINT:
322     case SIGTERM:
323     case SIGPIPE:
324         fprintf(stderr, "Caught signal %d. Exiting.", s);
325         ob_exit();
326         break;
327
328     case SIGFPE:
329     case SIGSEGV:
330         fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
331         abort();
332     }
333 }
334
335 static void print_version()
336 {
337     g_print("Openbox %s\n\n", PACKAGE_VERSION);
338     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
339     g_print("This is free software, and you are welcome to redistribute it\n");
340     g_print("under certain conditions. See the file COPYING for details.\n\n");
341 }
342
343 static void print_help()
344 {
345     print_version();
346     g_print("Syntax: openbox [options]\n\n");
347     g_print("Options:\n\n");
348 #ifdef USE_SM
349     g_print("  --sm-disable        Disable connection to session manager\n");
350     g_print("  --sm-client-id ID   Specify session management ID\n");
351     g_print("  --sm-save-file FILE Specify file to load a saved session\n"
352             "                      from\n");
353 #endif
354     g_print("  --replace           Replace the currently running window "
355             "manager\n");
356     g_print("  --help              Display this help and exit\n");
357     g_print("  --version           Display the version and exit\n");
358     g_print("  --sync              Run in synchronous mode (this is slow and\n"
359             "                      meant for debugging X routines)\n");
360     g_print("  --debug             Display debugging output\n");
361     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
362 }
363
364 static void parse_args(int argc, char **argv)
365 {
366     int i;
367
368     for (i = 1; i < argc; ++i) {
369         if (!strcmp(argv[i], "--version")) {
370             print_version();
371             exit(0);
372         } else if (!strcmp(argv[i], "--help")) {
373             print_help();
374             exit(0);
375         } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
376             g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
377         } else if (!strcmp(argv[i], "--replace")) {
378             ob_replace_wm = TRUE;
379         } else if (!strcmp(argv[i], "--sync")) {
380             xsync = TRUE;
381         } else if (!strcmp(argv[i], "--debug")) {
382             ob_debug_show_output(TRUE);
383 #ifdef USE_SM
384         } else if (!strcmp(argv[i], "--sm-client-id")) {
385             if (i == argc - 1) /* no args left */
386                 g_printerr(_("--sm-client-id requires an argument\n"));
387             else
388                 ob_sm_id = g_strdup(argv[++i]);
389         } else if (!strcmp(argv[i], "--sm-save-file")) {
390             if (i == argc - 1) /* no args left */
391                 g_printerr(_("--sm-save-file requires an argument\n"));
392             else
393                 sm_save_file = argv[++i];
394         } else if (!strcmp(argv[i], "--sm-disable")) {
395             ob_sm_use = FALSE;
396 #endif
397         } else {
398             g_printerr("Invalid option: '%s'\n\n", argv[i]);
399             print_help();
400             exit(1);
401         }
402     }
403 }
404
405 void ob_exit_with_error(gchar *msg)
406 {
407     g_critical(msg);
408     session_shutdown();
409     exit(EXIT_FAILURE);
410 }
411
412 void ob_restart_other(const gchar *path)
413 {
414     restart_path = g_strdup(path);
415     ob_restart();
416 }
417
418 void ob_restart()
419 {
420     restart = TRUE;
421     ob_exit();
422 }
423
424 void ob_exit()
425 {
426     shutdown = TRUE;
427 }
428
429 Cursor ob_cursor(ObCursor cursor)
430 {
431     g_assert(cursor < OB_NUM_CURSORS);
432     return cursors[cursor];
433 }
434
435 KeyCode ob_keycode(ObKey key)
436 {
437     g_assert(key < OB_NUM_KEYS);
438     return keys[key];
439 }
440
441 ObState ob_state()
442 {
443     return state;
444 }