1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 openbox.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
29 #include "startupnotify.h"
31 #include "moveresize.h"
35 #include "extensions.h"
36 #include "menuframe.h"
42 #include "parser/parse.h"
43 #include "render/render.h"
44 #include "render/theme.h"
58 #ifdef HAVE_SYS_STAT_H
59 # include <sys/stat.h>
60 # include <sys/types.h>
67 #include <X11/cursorfont.h>
69 RrInstance *ob_rr_inst;
71 ObMainLoop *ob_main_loop;
74 gboolean ob_replace_wm;
77 static gboolean xsync;
78 static gboolean reconfigure;
79 static gboolean restart;
80 static gchar *restart_path;
81 static Cursor cursors[OB_NUM_CURSORS];
82 static KeyCode keys[OB_NUM_KEYS];
83 static gint exitcode = 0;
85 static void signal_handler(gint signal, gpointer data);
86 static void parse_args(gint argc, gchar **argv);
88 gint main(gint argc, gchar **argv)
91 ob_debug_show_output(TRUE);
94 state = OB_STATE_STARTING;
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);
103 g_set_prgname(argv[0]);
105 if (chdir(g_get_home_dir()) == -1)
106 g_warning("Unable to change to home directory (%s): %s",
107 g_get_home_dir(), g_strerror(errno));
109 parse_paths_startup();
111 session_startup(&argc, &argv);
113 /* parse out command line args */
114 parse_args(argc, argv);
116 ob_display = XOpenDisplay(NULL);
117 if (ob_display == NULL)
118 ob_exit_with_error("Failed to open the display.");
119 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
120 ob_exit_with_error("Failed to set display as close-on-exec.");
122 ob_main_loop = ob_main_loop_new(ob_display);
124 /* set up signal handler */
125 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
126 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
127 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
128 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
129 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
130 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
132 ob_screen = DefaultScreen(ob_display);
134 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
135 if (ob_rr_inst == NULL)
136 ob_exit_with_error("Failed to initialize the render library.");
138 XSynchronize(ob_display, xsync);
140 /* check for locale support */
141 if (!XSupportsLocale())
142 g_warning("X server does not support locale.");
143 if (!XSetLocaleModifiers(""))
144 g_warning("Cannot set locale modifiers for the X server.");
146 /* set our error handler */
147 XSetErrorHandler(xerror_handler);
149 /* set the DISPLAY environment variable for any lauched children, to the
150 display we're using, so they open in the right place. */
151 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
153 /* create available cursors */
154 cursors[OB_CURSOR_NONE] = None;
155 cursors[OB_CURSOR_POINTER] =
156 XCreateFontCursor(ob_display, XC_left_ptr);
157 cursors[OB_CURSOR_BUSY] =
158 XCreateFontCursor(ob_display, XC_watch);
159 cursors[OB_CURSOR_MOVE] =
160 XCreateFontCursor(ob_display, XC_fleur);
161 cursors[OB_CURSOR_NORTH] =
162 XCreateFontCursor(ob_display, XC_top_side);
163 cursors[OB_CURSOR_NORTHEAST] =
164 XCreateFontCursor(ob_display, XC_top_right_corner);
165 cursors[OB_CURSOR_EAST] =
166 XCreateFontCursor(ob_display, XC_right_side);
167 cursors[OB_CURSOR_SOUTHEAST] =
168 XCreateFontCursor(ob_display, XC_bottom_right_corner);
169 cursors[OB_CURSOR_SOUTH] =
170 XCreateFontCursor(ob_display, XC_bottom_side);
171 cursors[OB_CURSOR_SOUTHWEST] =
172 XCreateFontCursor(ob_display, XC_bottom_left_corner);
173 cursors[OB_CURSOR_WEST] =
174 XCreateFontCursor(ob_display, XC_left_side);
175 cursors[OB_CURSOR_NORTHWEST] =
176 XCreateFontCursor(ob_display, XC_top_left_corner);
178 /* create available keycodes */
179 keys[OB_KEY_RETURN] =
180 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
181 keys[OB_KEY_ESCAPE] =
182 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
184 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
186 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
188 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
190 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
192 prop_startup(); /* get atoms values for the display */
193 extensions_query_all(); /* find which extensions are present */
195 if (screen_annex()) { /* it will be ours! */
202 /* startup the parsing so everything can register sections
207 /* parse/load user options */
208 if (parse_load_rc(&doc, &node))
209 parse_tree(i, doc, node->xmlChildrenNode);
210 /* we're done with parsing now, kill it */
215 /* load the theme specified in the rc file */
218 if ((theme = RrThemeNew(ob_rr_inst, config_theme))) {
219 RrThemeFree(ob_rr_theme);
222 if (ob_rr_theme == NULL)
223 ob_exit_with_error("Unable to load a theme.");
229 /* update all existing windows for the new theme */
230 for (it = client_list; it; it = g_list_next(it)) {
231 ObClient *c = it->data;
232 frame_adjust_theme(c->frame);
235 event_startup(reconfigure);
236 /* focus_backup is used for stacking, so this needs to come before
237 anything that calls stacking_add */
238 focus_startup(reconfigure);
239 window_startup(reconfigure);
240 sn_startup(reconfigure);
241 screen_startup(reconfigure);
242 grab_startup(reconfigure);
243 group_startup(reconfigure);
244 client_startup(reconfigure);
245 dock_startup(reconfigure);
246 moveresize_startup(reconfigure);
247 keyboard_startup(reconfigure);
248 mouse_startup(reconfigure);
249 menu_startup(reconfigure);
252 /* get all the existing windows */
254 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
258 /* redecorate all existing windows */
259 for (it = client_list; it; it = g_list_next(it)) {
260 ObClient *c = it->data;
261 frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
267 state = OB_STATE_RUNNING;
268 ob_main_loop_run(ob_main_loop);
269 state = OB_STATE_EXITING;
273 client_unmanage_all();
276 menu_shutdown(reconfigure);
277 mouse_shutdown(reconfigure);
278 keyboard_shutdown(reconfigure);
279 moveresize_shutdown(reconfigure);
280 dock_shutdown(reconfigure);
281 client_shutdown(reconfigure);
282 group_shutdown(reconfigure);
283 grab_shutdown(reconfigure);
284 screen_shutdown(reconfigure);
285 focus_shutdown(reconfigure);
286 sn_shutdown(reconfigure);
287 window_shutdown(reconfigure);
288 event_shutdown(reconfigure);
290 } while (reconfigure);
293 XSync(ob_display, FALSE);
295 RrThemeFree(ob_rr_theme);
296 RrInstanceFree(ob_rr_inst);
300 XCloseDisplay(ob_display);
302 parse_paths_shutdown();
305 if (restart_path != NULL) {
311 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
312 execvp(argvp[0], argvp);
315 g_warning("failed to execute '%s': %s", restart_path,
322 execvp(argv[0], argv); /* try how we were run */
323 execlp(argv[0], g_path_get_basename(argv[0])); /* last resort */
329 static void signal_handler(gint signal, gpointer data)
331 if (signal == SIGUSR1) {
332 ob_debug("Caught signal %d. Restarting.\n", signal);
334 } else if (signal == SIGUSR2) {
335 ob_debug("Caught signal %d. Reconfiguring.\n", signal);
338 ob_debug("Caught signal %d. Exiting.\n", signal);
339 /* TERM and INT return a 0 code */
340 ob_exit(!(signal == SIGTERM || signal == SIGINT));
344 static void print_version()
346 g_print("Openbox %s\n", PACKAGE_VERSION);
347 g_print("Copyright (c) 2003 Ben Jansens, and others\n\n");
348 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
349 g_print("This is free software, and you are welcome to redistribute it\n");
350 g_print("under certain conditions. See the file COPYING for details.\n\n");
353 static void print_help()
355 g_print("Syntax: openbox [options]\n\n");
356 g_print("Options:\n\n");
358 g_print(" --sm-disable Disable connection to session manager\n");
359 g_print(" --sm-client-id ID Specify session management ID\n");
360 g_print(" --sm-save-file FILE Specify file to load a saved session"
363 g_print(" --replace Replace the currently running window "
365 g_print(" --help Display this help and exit\n");
366 g_print(" --version Display the version and exit\n");
367 g_print(" --sync Run in synchronous mode (this is slow and "
369 " debugging X routines)\n");
370 g_print(" --debug Display debugging output\n");
371 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT);
374 static void parse_args(gint argc, gchar **argv)
378 for (i = 1; i < argc; ++i) {
379 if (!strcmp(argv[i], "--version")) {
382 } else if (!strcmp(argv[i], "--help")) {
385 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
386 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
387 } else if (!strcmp(argv[i], "--replace")) {
388 ob_replace_wm = TRUE;
389 } else if (!strcmp(argv[i], "--sync")) {
391 } else if (!strcmp(argv[i], "--debug")) {
392 ob_debug_show_output(TRUE);
394 g_printerr("Invalid option: '%s'\n\n", argv[i]);
401 void ob_exit_with_error(gchar *msg)
408 void ob_restart_other(const gchar *path)
410 restart_path = g_strdup(path);
420 void ob_reconfigure()
426 void ob_exit(gint code)
429 ob_main_loop_exit(ob_main_loop);
432 Cursor ob_cursor(ObCursor cursor)
434 g_assert(cursor < OB_NUM_CURSORS);
435 return cursors[cursor];
438 KeyCode ob_keycode(ObKey key)
440 g_assert(key < OB_NUM_KEYS);