change the --config-file to --config, which works differently..
[mikachu/openbox.git] / openbox / openbox.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    openbox.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "openbox.h"
22 #include "session.h"
23 #include "dock.h"
24 #include "modkeys.h"
25 #include "event.h"
26 #include "menu.h"
27 #include "client.h"
28 #include "xerror.h"
29 #include "prop.h"
30 #include "screen.h"
31 #include "startupnotify.h"
32 #include "focus.h"
33 #include "moveresize.h"
34 #include "frame.h"
35 #include "keyboard.h"
36 #include "mouse.h"
37 #include "extensions.h"
38 #include "menuframe.h"
39 #include "grab.h"
40 #include "group.h"
41 #include "config.h"
42 #include "mainloop.h"
43 #include "gettext.h"
44 #include "parser/parse.h"
45 #include "render/render.h"
46 #include "render/theme.h"
47
48 #ifdef HAVE_FCNTL_H
49 #  include <fcntl.h>
50 #endif
51 #ifdef HAVE_SIGNAL_H
52 #  include <signal.h>
53 #endif
54 #ifdef HAVE_STDLIB_H
55 #  include <stdlib.h>
56 #endif
57 #ifdef HAVE_LOCALE_H
58 #  include <locale.h>
59 #endif
60 #ifdef HAVE_SYS_STAT_H
61 #  include <sys/stat.h>
62 #  include <sys/types.h>
63 #endif
64 #ifdef HAVE_SYS_WAIT_H
65 #  include <sys/types.h>
66 #  include <sys/wait.h>
67 #endif
68 #ifdef HAVE_UNISTD_H
69 #  include <unistd.h>
70 #endif
71 #include <errno.h>
72
73 #include <X11/cursorfont.h>
74 #if USE_XCURSOR
75 #include <X11/Xcursor/Xcursor.h>
76 #endif
77
78 RrInstance *ob_rr_inst;
79 RrTheme    *ob_rr_theme;
80 ObMainLoop *ob_main_loop;
81 Display    *ob_display;
82 gint        ob_screen;
83 gboolean    ob_replace_wm = FALSE;
84
85 static ObState   state;
86 static gboolean  xsync = FALSE;
87 static gboolean  reconfigure = FALSE;
88 static gboolean  restart = FALSE;
89 static gchar    *restart_path = NULL;
90 static Cursor    cursors[OB_NUM_CURSORS];
91 static KeyCode   keys[OB_NUM_KEYS];
92 static gint      exitcode = 0;
93 static guint     remote_control = 0;
94 static gboolean  being_replaced = FALSE;
95 static gchar    *config_type = NULL;
96
97 static void signal_handler(gint signal, gpointer data);
98 static void parse_args(gint argc, gchar **argv);
99 static Cursor load_cursor(const gchar *name, guint fontval);
100
101 gint main(gint argc, gchar **argv)
102 {
103     state = OB_STATE_STARTING;
104
105     /* initialize the locale */
106     if (!setlocale(LC_ALL, ""))
107         g_message(_("Couldn't set locale from environment."));
108     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
109     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
110     textdomain(PACKAGE_NAME);
111
112     g_set_prgname(argv[0]);
113
114     if (chdir(g_get_home_dir()) == -1)
115         g_message(_("Unable to change to home directory '%s': %s"),
116                   g_get_home_dir(), g_strerror(errno));
117      
118     /* parse out command line args */
119     parse_args(argc, argv);
120
121     if (!remote_control) {
122         parse_paths_startup();
123
124         session_startup(argc, argv);
125     }
126
127     ob_display = XOpenDisplay(NULL);
128     if (ob_display == NULL)
129         ob_exit_with_error("Failed to open the display.");
130     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
131         ob_exit_with_error("Failed to set display as close-on-exec.");
132
133     if (remote_control) {
134         prop_startup();
135
136         /* Send client message telling the OB process to:
137          * remote_control = 1 -> reconfigure 
138          * remote_control = 2 -> restart */
139         PROP_MSG(RootWindow(ob_display, ob_screen),
140                  openbox_control, remote_control, 0, 0, 0);
141         XCloseDisplay(ob_display);
142         exit(EXIT_SUCCESS);
143     }
144
145     ob_main_loop = ob_main_loop_new(ob_display);
146
147     /* set up signal handler */
148     ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
149     ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
150     ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
151     ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
152     ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
153     ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
154     ob_main_loop_signal_add(ob_main_loop, SIGCHLD, signal_handler, NULL, NULL);
155
156     ob_screen = DefaultScreen(ob_display);
157
158     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
159     if (ob_rr_inst == NULL)
160         ob_exit_with_error("Failed to initialize the render library.");
161
162     XSynchronize(ob_display, xsync);
163
164     /* check for locale support */
165     if (!XSupportsLocale())
166         g_message(_("X server does not support locale."));
167     if (!XSetLocaleModifiers(""))
168         g_message(_("Cannot set locale modifiers for the X server."));
169
170     /* set our error handler */
171     XSetErrorHandler(xerror_handler);
172
173     /* set the DISPLAY environment variable for any lauched children, to the
174        display we're using, so they open in the right place. */
175     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
176
177     /* create available cursors */
178     cursors[OB_CURSOR_NONE] = None;
179     cursors[OB_CURSOR_POINTER] = load_cursor("left_ptr", XC_left_ptr);
180     cursors[OB_CURSOR_BUSY] = load_cursor("left_ptr_watch", XC_watch);
181     cursors[OB_CURSOR_MOVE] = load_cursor("fleur", XC_fleur);
182     cursors[OB_CURSOR_NORTH] = load_cursor("top_side", XC_top_side);
183     cursors[OB_CURSOR_NORTHEAST] = load_cursor("top_right_corner",
184                                                XC_top_right_corner);
185     cursors[OB_CURSOR_EAST] = load_cursor("right_side", XC_right_side);
186     cursors[OB_CURSOR_SOUTHEAST] = load_cursor("bottom_right_corner",
187                                                XC_bottom_right_corner);
188     cursors[OB_CURSOR_SOUTH] = load_cursor("bottom_side", XC_bottom_side);
189     cursors[OB_CURSOR_SOUTHWEST] = load_cursor("bottom_left_corner",
190                                                XC_bottom_left_corner);
191     cursors[OB_CURSOR_WEST] = load_cursor("left_side", XC_left_side);
192     cursors[OB_CURSOR_NORTHWEST] = load_cursor("top_left_corner",
193                                                XC_top_left_corner);
194
195     /* create available keycodes */
196     keys[OB_KEY_RETURN] =
197         XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
198     keys[OB_KEY_ESCAPE] =
199         XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
200     keys[OB_KEY_LEFT] =
201         XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
202     keys[OB_KEY_RIGHT] =
203         XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
204     keys[OB_KEY_UP] =
205         XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
206     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     if (screen_annex()) { /* it will be ours! */
213         do {
214             {
215                 ObParseInst *i;
216                 xmlDocPtr doc;
217                 xmlNodePtr node;
218
219                 modkeys_startup(reconfigure);
220
221                 /* startup the parsing so everything can register sections
222                    of the rc */
223                 i = parse_startup();
224
225                 /* start up config which sets up with the parser */
226                 config_startup(i);
227
228                 /* parse/load user options */
229                 if (parse_load_rc(config_type, &doc, &node)) {
230                     parse_tree(i, doc, node->xmlChildrenNode);
231                     parse_close(doc);
232                 } else
233                     g_message(_("Unable to find a valid config file, using some simple defaults"));
234
235                 if (config_type != NULL)
236                     PROP_SETS(RootWindow(ob_display, ob_screen),
237                               openbox_config, config_type);
238
239                 /* we're done with parsing now, kill it */
240                 parse_shutdown(i);
241             }
242
243             /* load the theme specified in the rc file */
244             {
245                 RrTheme *theme;
246                 if ((theme = RrThemeNew(ob_rr_inst, config_theme,
247                                         config_font_activewindow,
248                                         config_font_inactivewindow,
249                                         config_font_menutitle,
250                                         config_font_menuitem,
251                                         config_font_osd)))
252                 {
253                     RrThemeFree(ob_rr_theme);
254                     ob_rr_theme = theme;
255                 }
256                 if (ob_rr_theme == NULL)
257                     ob_exit_with_error("Unable to load a theme.");
258             }
259
260             if (reconfigure) {
261                 GList *it;
262
263                 /* update all existing windows for the new theme */
264                 for (it = client_list; it; it = g_list_next(it)) {
265                     ObClient *c = it->data;
266                     frame_adjust_theme(c->frame);
267                 }
268             }
269             event_startup(reconfigure);
270             /* focus_backup is used for stacking, so this needs to come before
271                anything that calls stacking_add */
272             focus_startup(reconfigure);
273             window_startup(reconfigure);
274             sn_startup(reconfigure);
275             screen_startup(reconfigure);
276             grab_startup(reconfigure);
277             group_startup(reconfigure);
278             client_startup(reconfigure);
279             dock_startup(reconfigure);
280             moveresize_startup(reconfigure);
281             keyboard_startup(reconfigure);
282             mouse_startup(reconfigure);
283             menu_startup(reconfigure);
284             menu_frame_startup(reconfigure);
285
286             if (!reconfigure) {
287                 /* get all the existing windows */
288                 client_manage_all();
289                 focus_fallback(TRUE);
290             } else {
291                 GList *it;
292
293                 /* redecorate all existing windows */
294                 for (it = client_list; it; it = g_list_next(it)) {
295                     ObClient *c = it->data;
296                     /* the new config can change the window's decorations */
297                     client_setup_decor_and_functions(c);
298                     /* redraw the frames */
299                     frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
300                 }
301             }
302
303             reconfigure = FALSE;
304
305             state = OB_STATE_RUNNING;
306             ob_main_loop_run(ob_main_loop);
307             state = OB_STATE_EXITING;
308
309             if (!reconfigure) {
310                 dock_remove_all();
311                 client_unmanage_all();
312             }
313
314             menu_frame_shutdown(reconfigure);
315             menu_shutdown(reconfigure);
316             mouse_shutdown(reconfigure);
317             keyboard_shutdown(reconfigure);
318             moveresize_shutdown(reconfigure);
319             dock_shutdown(reconfigure);
320             client_shutdown(reconfigure);
321             group_shutdown(reconfigure);
322             grab_shutdown(reconfigure);
323             screen_shutdown(reconfigure);
324             focus_shutdown(reconfigure);
325             sn_shutdown(reconfigure);
326             window_shutdown(reconfigure);
327             event_shutdown(reconfigure);
328             config_shutdown();
329             modkeys_shutdown(reconfigure);
330         } while (reconfigure);
331     }
332
333     XSync(ob_display, FALSE);
334
335     RrThemeFree(ob_rr_theme);
336     RrInstanceFree(ob_rr_inst);
337
338     session_shutdown(being_replaced);
339
340     XCloseDisplay(ob_display);
341
342     parse_paths_shutdown();
343
344     if (restart) {
345         if (restart_path != NULL) {
346             gint argcp;
347             gchar **argvp;
348             GError *err = NULL;
349
350             /* run other window manager */
351             if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
352                 execvp(argvp[0], argvp);
353                 g_strfreev(argvp);
354             } else {
355                 g_message(
356                     _("Restart failed to execute new executable '%s': %s"),
357                     restart_path, err->message);
358                 g_error_free(err);
359             }
360         }
361
362         /* re-run me */
363         execvp(argv[0], argv); /* try how we were run */
364         execlp(argv[0], g_path_get_basename(argv[0]),
365                (char *)NULL); /* last resort */
366     }
367      
368     return exitcode;
369 }
370
371 static void signal_handler(gint signal, gpointer data)
372 {
373     switch (signal) {
374     case SIGUSR1:
375         ob_debug("Caught signal %d. Restarting.\n", signal);
376         ob_restart();
377         break;
378     case SIGUSR2:
379         ob_debug("Caught signal %d. Reconfiguring.\n", signal);
380         ob_reconfigure(); 
381         break;
382     case SIGCHLD:
383         /* reap children */
384         while (waitpid(-1, NULL, WNOHANG) > 0);
385         break;
386     default:
387         ob_debug("Caught signal %d. Exiting.\n", signal);
388         /* TERM and INT return a 0 code */
389         ob_exit(!(signal == SIGTERM || signal == SIGINT));
390     }
391 }
392
393 static void print_version()
394 {
395     g_print("Openbox %s\n", PACKAGE_VERSION);
396     g_print(_("Copyright (c)"));
397     g_print(" 2007        Mikael Magnusson\n");
398     g_print(_("Copyright (c)"));
399     g_print(" 2003-2007   Dana Jansens\n\n");
400     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
401     g_print("This is free software, and you are welcome to redistribute it\n");
402     g_print("under certain conditions. See the file COPYING for details.\n\n");
403 }
404
405 static void print_help()
406 {
407     g_print(_("Syntax: openbox [options]\n"));
408     g_print(_("\nOptions:\n\n"));
409     g_print(_("  --config TYPE       Specify the configuration profile to use\n"));
410 #ifdef USE_SM
411     g_print(_("  --sm-disable        Disable connection to session manager\n"));
412     g_print(_("  --sm-client-id ID   Specify session management ID\n"));
413     g_print(_("  --sm-save-file FILE Specify file to load a saved session from\n"));
414 #endif
415     g_print(_("  --replace           Replace the currently running window manager\n"));
416     g_print(_("  --help              Display this help and exit\n"));
417     g_print(_("  --version           Display the version and exit\n"));
418     g_print(_("\nPassing messages to a running Openbox instance:\n\n"));
419     g_print(_("  --reconfigure       Reload Openbox's configuration\n"));
420     g_print(_("\nDebugging options:\n\n"));
421     g_print(_("  --sync              Run in synchronous mode\n"));
422     g_print(_("  --debug             Display debugging output\n"));
423     g_print(_("  --debug-focus       Display debugging output for focus handling\n"));
424     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
425 }
426
427 static void parse_args(gint argc, gchar **argv)
428 {
429     gint i;
430
431     for (i = 1; i < argc; ++i) {
432         if (!strcmp(argv[i], "--version")) {
433             print_version();
434             exit(0);
435         } else if (!strcmp(argv[i], "--help")) {
436             print_help();
437             exit(0);
438         } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
439             g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
440         } else if (!strcmp(argv[i], "--replace")) {
441             ob_replace_wm = TRUE;
442         } else if (!strcmp(argv[i], "--sync")) {
443             xsync = TRUE;
444         } else if (!strcmp(argv[i], "--debug")) {
445             ob_debug_show_output(TRUE);
446             ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
447         } else if (!strcmp(argv[i], "--debug-focus")) {
448             ob_debug_show_output(TRUE);
449             ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
450             ob_debug_enable(OB_DEBUG_FOCUS, TRUE);
451         } else if (!strcmp(argv[i], "--reconfigure")) {
452             remote_control = 1;
453         } else if (!strcmp(argv[i], "--restart")) {
454             remote_control = 2;
455         } else if (!strcmp(argv[i], "--config")) {
456             if (i == argc - 1) /* no args left */
457                 g_printerr(_("--config-file requires an argument\n"));
458             else {
459                 config_type = g_strdup(argv[i+1]);
460                 ++i;
461             }
462         }
463     }
464 }
465
466 static Cursor load_cursor(const gchar *name, guint fontval)
467 {
468     Cursor c = None;
469
470 #if USE_XCURSOR
471     c = XcursorLibraryLoadCursor(ob_display, name);
472 #endif
473     if (c == None)
474         c = XCreateFontCursor(ob_display, fontval);
475     return c;
476 }
477
478 void ob_exit_with_error(const gchar *msg)
479 {
480     g_critical(msg);
481     session_shutdown(TRUE);
482     exit(EXIT_FAILURE);
483 }
484
485 void ob_restart_other(const gchar *path)
486 {
487     restart_path = g_strdup(path);
488     ob_restart();
489 }
490
491 void ob_restart()
492 {
493     restart = TRUE;
494     ob_exit(0);
495 }
496
497 void ob_reconfigure()
498 {
499     reconfigure = TRUE;
500     ob_exit(0);
501 }
502
503 void ob_exit(gint code)
504 {
505     exitcode = code;
506     ob_main_loop_exit(ob_main_loop);
507 }
508
509 void ob_exit_replace()
510 {
511     exitcode = 0;
512     being_replaced = TRUE;
513     ob_main_loop_exit(ob_main_loop);
514 }
515
516 Cursor ob_cursor(ObCursor cursor)
517 {
518     g_assert(cursor < OB_NUM_CURSORS);
519     return cursors[cursor];
520 }
521
522 KeyCode ob_keycode(ObKey key)
523 {
524     g_assert(key < OB_NUM_KEYS);
525     return keys[key];
526 }
527
528 ObState ob_state()
529 {
530     return state;
531 }