1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 openbox.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
31 #include "startupnotify.h"
33 #include "moveresize.h"
37 #include "extensions.h"
38 #include "menuframe.h"
44 #include "parser/parse.h"
45 #include "render/render.h"
46 #include "render/theme.h"
60 #ifdef HAVE_SYS_STAT_H
61 # include <sys/stat.h>
62 # include <sys/types.h>
64 #ifdef HAVE_SYS_WAIT_H
65 # include <sys/types.h>
66 # include <sys/wait.h>
73 #include <X11/cursorfont.h>
75 #include <X11/Xcursor/Xcursor.h>
78 RrInstance *ob_rr_inst;
80 ObMainLoop *ob_main_loop;
83 gboolean ob_replace_wm = FALSE;
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_file = NULL;
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);
101 gint main(gint argc, gchar **argv)
103 state = OB_STATE_STARTING;
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);
112 g_set_prgname(argv[0]);
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));
118 /* parse out command line args */
119 parse_args(argc, argv);
121 if (!remote_control) {
122 parse_paths_startup();
124 session_startup(argc, argv);
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.");
133 if (remote_control) {
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);
145 ob_main_loop = ob_main_loop_new(ob_display);
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);
156 ob_screen = DefaultScreen(ob_display);
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.");
162 XSynchronize(ob_display, xsync);
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."));
170 /* set our error handler */
171 XSetErrorHandler(xerror_handler);
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)));
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",
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"));
201 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
203 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
205 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
207 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
209 prop_startup(); /* get atoms values for the display */
210 extensions_query_all(); /* find which extensions are present */
212 if (screen_annex()) { /* it will be ours! */
219 modkeys_startup(reconfigure);
221 /* startup the parsing so everything can register sections
226 /* parse/load user options */
227 if (parse_load_rc(config_file, &doc, &node, &config_file)) {
228 PROP_SETS(RootWindow(ob_display, ob_screen),
229 openbox_rc, config_file);
230 parse_tree(i, doc, node->xmlChildrenNode);
232 g_message(_("Unable to find a valid config file, using some simple defaults"));
233 PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_rc);
235 /* we're done with parsing now, kill it */
240 /* load the theme specified in the rc file */
243 if ((theme = RrThemeNew(ob_rr_inst, config_theme,
244 config_font_activewindow,
245 config_font_inactivewindow,
246 config_font_menutitle,
247 config_font_menuitem,
250 RrThemeFree(ob_rr_theme);
253 if (ob_rr_theme == NULL)
254 ob_exit_with_error("Unable to load a theme.");
260 /* update all existing windows for the new theme */
261 for (it = client_list; it; it = g_list_next(it)) {
262 ObClient *c = it->data;
263 frame_adjust_theme(c->frame);
266 event_startup(reconfigure);
267 /* focus_backup is used for stacking, so this needs to come before
268 anything that calls stacking_add */
269 focus_startup(reconfigure);
270 window_startup(reconfigure);
271 sn_startup(reconfigure);
272 screen_startup(reconfigure);
273 grab_startup(reconfigure);
274 group_startup(reconfigure);
275 client_startup(reconfigure);
276 dock_startup(reconfigure);
277 moveresize_startup(reconfigure);
278 keyboard_startup(reconfigure);
279 mouse_startup(reconfigure);
280 menu_startup(reconfigure);
281 menu_frame_startup(reconfigure);
284 /* get all the existing windows */
286 focus_fallback(TRUE);
290 /* redecorate all existing windows */
291 for (it = client_list; it; it = g_list_next(it)) {
292 ObClient *c = it->data;
293 /* the new config can change the window's decorations */
294 client_setup_decor_and_functions(c);
295 /* redraw the frames */
296 frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
302 state = OB_STATE_RUNNING;
303 ob_main_loop_run(ob_main_loop);
304 state = OB_STATE_EXITING;
308 client_unmanage_all();
311 menu_frame_shutdown(reconfigure);
312 menu_shutdown(reconfigure);
313 mouse_shutdown(reconfigure);
314 keyboard_shutdown(reconfigure);
315 moveresize_shutdown(reconfigure);
316 dock_shutdown(reconfigure);
317 client_shutdown(reconfigure);
318 group_shutdown(reconfigure);
319 grab_shutdown(reconfigure);
320 screen_shutdown(reconfigure);
321 focus_shutdown(reconfigure);
322 sn_shutdown(reconfigure);
323 window_shutdown(reconfigure);
324 event_shutdown(reconfigure);
326 modkeys_shutdown(reconfigure);
327 } while (reconfigure);
330 XSync(ob_display, FALSE);
332 g_free(config_file); /* this is set by parse_load_rc */
333 RrThemeFree(ob_rr_theme);
334 RrInstanceFree(ob_rr_inst);
336 session_shutdown(being_replaced);
338 XCloseDisplay(ob_display);
340 parse_paths_shutdown();
343 if (restart_path != NULL) {
348 /* run other window manager */
349 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
350 execvp(argvp[0], argvp);
354 _("Restart failed to execute new executable '%s': %s"),
355 restart_path, err->message);
361 execvp(argv[0], argv); /* try how we were run */
362 execlp(argv[0], g_path_get_basename(argv[0]),
363 (char *)NULL); /* last resort */
369 static void signal_handler(gint signal, gpointer data)
373 ob_debug("Caught signal %d. Restarting.\n", signal);
377 ob_debug("Caught signal %d. Reconfiguring.\n", signal);
382 while (waitpid(-1, NULL, WNOHANG) > 0);
385 ob_debug("Caught signal %d. Exiting.\n", signal);
386 /* TERM and INT return a 0 code */
387 ob_exit(!(signal == SIGTERM || signal == SIGINT));
391 static void print_version()
393 g_print("Openbox %s\n", PACKAGE_VERSION);
394 g_print(_("Copyright (c)"));
395 g_print(" 2007 Mikael Magnusson\n");
396 g_print(_("Copyright (c)"));
397 g_print(" 2003-2007 Dana Jansens\n\n");
398 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
399 g_print("This is free software, and you are welcome to redistribute it\n");
400 g_print("under certain conditions. See the file COPYING for details.\n\n");
403 static void print_help()
405 g_print(_("Syntax: openbox [options]\n"));
406 g_print(_("\nOptions:\n\n"));
407 g_print(_(" --config-file FILE Specify the file to load for the config file\n"));
409 g_print(_(" --sm-disable Disable connection to session manager\n"));
410 g_print(_(" --sm-client-id ID Specify session management ID\n"));
411 g_print(_(" --sm-save-file FILE Specify file to load a saved session from\n"));
413 g_print(_(" --replace Replace the currently running window manager\n"));
414 g_print(_(" --help Display this help and exit\n"));
415 g_print(_(" --version Display the version and exit\n"));
416 g_print(_("\nPassing messages to a running Openbox instance:\n\n"));
417 g_print(_(" --reconfigure Reload Openbox's configuration\n"));
418 g_print(_("\nDebugging options:\n\n"));
419 g_print(_(" --sync Run in synchronous mode\n"));
420 g_print(_(" --debug Display debugging output\n"));
421 g_print(_(" --debug-focus Display debugging output for focus handling\n"));
422 g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
425 static void parse_args(gint argc, gchar **argv)
429 for (i = 1; i < argc; ++i) {
430 if (!strcmp(argv[i], "--version")) {
433 } else if (!strcmp(argv[i], "--help")) {
436 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
437 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
438 } else if (!strcmp(argv[i], "--replace")) {
439 ob_replace_wm = TRUE;
440 } else if (!strcmp(argv[i], "--sync")) {
442 } else if (!strcmp(argv[i], "--debug")) {
443 ob_debug_show_output(TRUE);
444 ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
445 } else if (!strcmp(argv[i], "--debug-focus")) {
446 ob_debug_show_output(TRUE);
447 ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
448 ob_debug_enable(OB_DEBUG_FOCUS, TRUE);
449 } else if (!strcmp(argv[i], "--reconfigure")) {
451 } else if (!strcmp(argv[i], "--restart")) {
453 } else if (!strcmp(argv[i], "--config-file")) {
454 if (i == argc - 1) /* no args left */
455 g_printerr(_("--config-file requires an argument\n"));
457 config_file = g_strdup(argv[i+1]);
464 static Cursor load_cursor(const gchar *name, guint fontval)
469 c = XcursorLibraryLoadCursor(ob_display, name);
472 c = XCreateFontCursor(ob_display, fontval);
476 void ob_exit_with_error(const gchar *msg)
479 session_shutdown(TRUE);
483 void ob_restart_other(const gchar *path)
485 restart_path = g_strdup(path);
495 void ob_reconfigure()
501 void ob_exit(gint code)
504 ob_main_loop_exit(ob_main_loop);
507 void ob_exit_replace()
510 being_replaced = TRUE;
511 ob_main_loop_exit(ob_main_loop);
514 Cursor ob_cursor(ObCursor cursor)
516 g_assert(cursor < OB_NUM_CURSORS);
517 return cursors[cursor];
520 KeyCode ob_keycode(ObKey key)
522 g_assert(key < OB_NUM_KEYS);