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