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];
64 static gint exitcode = 0;
66 static void signal_handler(int signal, gpointer data);
67 static void parse_args(int argc, char **argv);
69 int main(int argc, char **argv)
72 ob_debug_show_output(TRUE);
75 state = OB_STATE_STARTING;
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);
84 g_set_prgname(argv[0]);
86 parse_paths_startup();
88 session_startup(&argc, &argv);
90 /* parse out command line args */
91 parse_args(argc, argv);
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.");
99 ob_main_loop = ob_main_loop_new(ob_display);
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);
109 ob_screen = DefaultScreen(ob_display);
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.");
115 /* XXX fork self onto other screens */
117 XSynchronize(ob_display, xsync);
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.");
125 /* set our error handler */
126 XSetErrorHandler(xerror_handler);
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)));
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);
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"));
163 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
165 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
167 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
169 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
171 prop_startup(); /* get atoms values for the display */
172 extensions_query_all(); /* find which extensions are present */
174 if (screen_annex()) { /* it will be ours! */
181 /* startup the parsing so everything can register sections
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 */
194 /* load the theme specified in the rc file */
197 if ((theme = RrThemeNew(ob_rr_inst, config_theme))) {
198 RrThemeFree(ob_rr_theme);
201 if (ob_rr_theme == NULL)
202 ob_exit_with_error("Unable to load a theme.");
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);
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);
231 /* get all the existing windows */
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);
245 state = OB_STATE_RUNNING;
246 ob_main_loop_run(ob_main_loop);
247 state = OB_STATE_EXITING;
251 client_unmanage_all();
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);
268 } while (reconfigure);
271 RrThemeFree(ob_rr_theme);
272 RrInstanceFree(ob_rr_inst);
276 XCloseDisplay(ob_display);
278 parse_paths_shutdown();
281 if (restart_path != NULL) {
287 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
288 execvp(argvp[0], argvp);
291 g_warning("failed to execute '%s': %s", restart_path,
297 execvp(argv[0], argv); /* try how we were run */
298 execlp(argv[0], g_path_get_basename(argv[0])); /* last resort */
304 static void signal_handler(int signal, gpointer data)
306 if (signal == SIGUSR1) {
307 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
309 } else if (signal == SIGUSR2) {
310 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
313 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
314 /* TERM and INT return a 0 code */
315 ob_exit(!(signal == SIGTERM || signal == SIGINT));
319 static void print_version()
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");
327 static void print_help()
330 g_print("Syntax: openbox [options]\n\n");
331 g_print("Options:\n\n");
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"
338 g_print(" --replace Replace the currently running window "
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);
348 static void parse_args(int argc, char **argv)
352 for (i = 1; i < argc; ++i) {
353 if (!strcmp(argv[i], "--version")) {
356 } else if (!strcmp(argv[i], "--help")) {
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")) {
365 } else if (!strcmp(argv[i], "--debug")) {
366 ob_debug_show_output(TRUE);
368 g_printerr("Invalid option: '%s'\n\n", argv[i]);
375 void ob_exit_with_error(gchar *msg)
382 void ob_restart_other(const gchar *path)
384 restart_path = g_strdup(path);
394 void ob_reconfigure()
400 void ob_exit(gint code)
403 ob_main_loop_exit(ob_main_loop);
406 Cursor ob_cursor(ObCursor cursor)
408 g_assert(cursor < OB_NUM_CURSORS);
409 return cursors[cursor];
412 KeyCode ob_keycode(ObKey key)
414 g_assert(key < OB_NUM_KEYS);