11 #include "startupnotify.h"
13 #include "moveresize.h"
17 #include "extensions.h"
18 #include "menuframe.h"
24 #include "parser/parse.h"
25 #include "render/render.h"
26 #include "render/theme.h"
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
48 #include <X11/cursorfont.h>
50 RrInstance *ob_rr_inst;
52 ObMainLoop *ob_main_loop;
55 gboolean ob_replace_wm;
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];
65 static void signal_handler(int signal, gpointer data);
66 static void parse_args(int argc, char **argv);
68 int main(int argc, char **argv)
71 ob_debug_show_output(TRUE);
74 state = OB_STATE_STARTING;
76 /* initialize the locale */
77 if (!setlocale(LC_ALL, ""))
78 g_warning("Couldn't set locale from environment.\n");
79 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
80 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
81 textdomain(PACKAGE_NAME);
83 g_set_prgname(argv[0]);
85 parse_paths_startup();
87 session_startup(&argc, &argv);
89 /* parse out command line args */
90 parse_args(argc, argv);
92 ob_display = XOpenDisplay(NULL);
93 if (ob_display == NULL)
94 ob_exit_with_error("Failed to open the display.");
95 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
96 ob_exit_with_error("Failed to set display as close-on-exec.");
98 ob_main_loop = ob_main_loop_new(ob_display);
100 /* set up signal handler */
101 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
102 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
103 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
104 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
105 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
106 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
108 ob_screen = DefaultScreen(ob_display);
110 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
111 if (ob_rr_inst == NULL)
112 ob_exit_with_error("Failed to initialize the render library.");
114 /* XXX fork self onto other screens */
116 XSynchronize(ob_display, xsync);
118 /* check for locale support */
119 if (!XSupportsLocale())
120 g_warning("X server does not support locale.");
121 if (!XSetLocaleModifiers(""))
122 g_warning("Cannot set locale modifiers for the X server.");
124 /* set our error handler */
125 XSetErrorHandler(xerror_handler);
127 /* set the DISPLAY environment variable for any lauched children, to the
128 display we're using, so they open in the right place. */
129 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
131 /* create available cursors */
132 cursors[OB_CURSOR_NONE] = None;
133 cursors[OB_CURSOR_POINTER] =
134 XCreateFontCursor(ob_display, XC_left_ptr);
135 cursors[OB_CURSOR_BUSY] =
136 XCreateFontCursor(ob_display, XC_watch);
137 cursors[OB_CURSOR_MOVE] =
138 XCreateFontCursor(ob_display, XC_fleur);
139 cursors[OB_CURSOR_NORTH] =
140 XCreateFontCursor(ob_display, XC_top_side);
141 cursors[OB_CURSOR_NORTHEAST] =
142 XCreateFontCursor(ob_display, XC_top_right_corner);
143 cursors[OB_CURSOR_EAST] =
144 XCreateFontCursor(ob_display, XC_right_side);
145 cursors[OB_CURSOR_SOUTHEAST] =
146 XCreateFontCursor(ob_display, XC_bottom_right_corner);
147 cursors[OB_CURSOR_SOUTH] =
148 XCreateFontCursor(ob_display, XC_bottom_side);
149 cursors[OB_CURSOR_SOUTHWEST] =
150 XCreateFontCursor(ob_display, XC_bottom_left_corner);
151 cursors[OB_CURSOR_WEST] =
152 XCreateFontCursor(ob_display, XC_left_side);
153 cursors[OB_CURSOR_NORTHWEST] =
154 XCreateFontCursor(ob_display, XC_top_left_corner);
156 /* create available keycodes */
157 keys[OB_KEY_RETURN] =
158 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
159 keys[OB_KEY_ESCAPE] =
160 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
162 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
164 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
166 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
168 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
170 prop_startup(); /* get atoms values for the display */
171 extensions_query_all(); /* find which extensions are present */
173 if (screen_annex()) { /* it will be ours! */
180 /* startup the parsing so everything can register sections
185 /* parse/load user options */
186 if (parse_load_rc(&doc, &node))
187 parse_tree(i, doc, node->xmlChildrenNode);
188 /* we're done with parsing now, kill it */
193 /* load the theme specified in the rc file */
196 if ((theme = RrThemeNew(ob_rr_inst, config_theme))) {
197 RrThemeFree(ob_rr_theme);
200 if (ob_rr_theme == NULL)
201 ob_exit_with_error("Unable to load a theme.");
207 /* update all existing windows for the new theme */
208 for (it = client_list; it; it = g_list_next(it)) {
209 ObClient *c = it->data;
210 frame_adjust_theme(c->frame);
213 event_startup(reconfigure);
214 grab_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 group_startup(reconfigure);
222 client_startup(reconfigure);
223 dock_startup(reconfigure);
224 moveresize_startup(reconfigure);
225 keyboard_startup(reconfigure);
226 mouse_startup(reconfigure);
227 menu_startup(reconfigure);
230 /* get all the existing windows */
235 /* redecorate all existing windows */
236 for (it = client_list; it; it = g_list_next(it)) {
237 ObClient *c = it->data;
238 frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
244 state = OB_STATE_RUNNING;
245 ob_main_loop_run(ob_main_loop);
246 state = OB_STATE_EXITING;
250 client_unmanage_all();
253 menu_shutdown(reconfigure);
254 mouse_shutdown(reconfigure);
255 keyboard_shutdown(reconfigure);
256 moveresize_shutdown(reconfigure);
257 dock_shutdown(reconfigure);
258 client_shutdown(reconfigure);
259 group_shutdown(reconfigure);
260 screen_shutdown(reconfigure);
261 focus_shutdown(reconfigure);
262 sn_shutdown(reconfigure);
263 window_shutdown(reconfigure);
264 grab_shutdown(reconfigure);
265 event_shutdown(reconfigure);
267 } while (reconfigure);
270 RrThemeFree(ob_rr_theme);
271 RrInstanceFree(ob_rr_inst);
275 XCloseDisplay(ob_display);
277 parse_paths_shutdown();
280 if (restart_path != NULL) {
286 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
287 execvp(argvp[0], argvp);
290 g_warning("failed to execute '%s': %s", restart_path,
296 execvp(argv[0], argv); /* try how we were run */
297 execlp(argv[0], g_path_get_basename(argv[0])); /* last resort */
303 static void signal_handler(int signal, gpointer data)
305 if (signal == SIGUSR1) {
306 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
308 } else if (signal == SIGUSR2) {
309 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
312 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
317 static void print_version()
319 g_print("Openbox %s\n\n", PACKAGE_VERSION);
320 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
321 g_print("This is free software, and you are welcome to redistribute it\n");
322 g_print("under certain conditions. See the file COPYING for details.\n\n");
325 static void print_help()
328 g_print("Syntax: openbox [options]\n\n");
329 g_print("Options:\n\n");
331 g_print(" --sm-disable Disable connection to session manager\n");
332 g_print(" --sm-client-id ID Specify session management ID\n");
333 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
336 g_print(" --replace Replace the currently running window "
338 g_print(" --help Display this help and exit\n");
339 g_print(" --version Display the version and exit\n");
340 g_print(" --sync Run in synchronous mode (this is slow and\n"
341 " meant for debugging X routines)\n");
342 g_print(" --debug Display debugging output\n");
343 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
346 static void parse_args(int argc, char **argv)
350 for (i = 1; i < argc; ++i) {
351 if (!strcmp(argv[i], "--version")) {
354 } else if (!strcmp(argv[i], "--help")) {
357 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
358 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
359 } else if (!strcmp(argv[i], "--replace")) {
360 ob_replace_wm = TRUE;
361 } else if (!strcmp(argv[i], "--sync")) {
363 } else if (!strcmp(argv[i], "--debug")) {
364 ob_debug_show_output(TRUE);
366 g_printerr("Invalid option: '%s'\n\n", argv[i]);
373 void ob_exit_with_error(gchar *msg)
380 void ob_restart_other(const gchar *path)
382 restart_path = g_strdup(path);
392 void ob_reconfigure()
400 ob_main_loop_exit(ob_main_loop);
403 Cursor ob_cursor(ObCursor cursor)
405 g_assert(cursor < OB_NUM_CURSORS);
406 return cursors[cursor];
409 KeyCode ob_keycode(ObKey key)
411 g_assert(key < OB_NUM_KEYS);