11 #include "startupnotify.h"
13 #include "moveresize.h"
17 #include "extensions.h"
18 #include "menuframe.h"
25 #include "parser/parse.h"
26 #include "render/render.h"
27 #include "render/theme.h"
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 # include <sys/types.h>
50 #include <X11/cursorfont.h>
52 RrInstance *ob_rr_inst;
54 ObMainLoop *ob_main_loop;
57 gboolean ob_sm_use = TRUE;
59 gboolean ob_replace_wm;
62 static gboolean xsync;
63 static gboolean reconfigure;
64 static gboolean restart;
65 static char *restart_path;
66 static Cursor cursors[OB_NUM_CURSORS];
67 static KeyCode keys[OB_NUM_KEYS];
68 static gchar *sm_save_file;
70 static void signal_handler(int signal, gpointer data);
71 static void parse_args(int argc, char **argv);
73 int main(int argc, char **argv)
78 ob_debug_show_output(TRUE);
81 state = OB_STATE_STARTING;
83 /* initialize the locale */
84 if (!setlocale(LC_ALL, ""))
85 g_warning("Couldn't set locale from environment.\n");
86 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
87 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
88 textdomain(PACKAGE_NAME);
90 /* create the ~/.openbox dir */
91 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
92 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
93 S_IROTH | S_IWOTH | S_IXOTH));
95 /* create the ~/.openbox/themes dir */
96 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
97 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
98 S_IROTH | S_IWOTH | S_IXOTH));
100 /* create the ~/.openbox/sessions dir */
101 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
102 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
103 S_IROTH | S_IWOTH | S_IXOTH));
106 g_set_prgname(argv[0]);
108 /* parse out command line args */
109 parse_args(argc, argv);
111 ob_display = XOpenDisplay(NULL);
112 if (ob_display == NULL)
113 ob_exit_with_error("Failed to open the display.");
114 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
115 ob_exit_with_error("Failed to set display as close-on-exec.");
117 ob_main_loop = ob_main_loop_new(ob_display);
119 /* set up signal handler */
120 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
121 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
122 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
123 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
124 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
125 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
128 session_load(sm_save_file);
129 session_startup(argc, argv);
131 ob_screen = DefaultScreen(ob_display);
133 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
134 if (ob_rr_inst == NULL)
135 ob_exit_with_error("Failed to initialize the render library.");
137 /* XXX fork self onto other screens */
139 XSynchronize(ob_display, xsync);
141 /* check for locale support */
142 if (!XSupportsLocale())
143 g_warning("X server does not support locale.");
144 if (!XSetLocaleModifiers(""))
145 g_warning("Cannot set locale modifiers for the X server.");
147 /* set our error handler */
148 XSetErrorHandler(xerror_handler);
150 /* set the DISPLAY environment variable for any lauched children, to the
151 display we're using, so they open in the right place. */
152 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
154 /* create available cursors */
155 cursors[OB_CURSOR_NONE] = None;
156 cursors[OB_CURSOR_POINTER] =
157 XCreateFontCursor(ob_display, XC_left_ptr);
158 cursors[OB_CURSOR_BUSY] =
159 XCreateFontCursor(ob_display, XC_watch);
160 cursors[OB_CURSOR_MOVE] =
161 XCreateFontCursor(ob_display, XC_fleur);
162 cursors[OB_CURSOR_NORTH] =
163 XCreateFontCursor(ob_display, XC_top_side);
164 cursors[OB_CURSOR_NORTHEAST] =
165 XCreateFontCursor(ob_display, XC_top_right_corner);
166 cursors[OB_CURSOR_EAST] =
167 XCreateFontCursor(ob_display, XC_right_side);
168 cursors[OB_CURSOR_SOUTHEAST] =
169 XCreateFontCursor(ob_display, XC_bottom_right_corner);
170 cursors[OB_CURSOR_SOUTH] =
171 XCreateFontCursor(ob_display, XC_bottom_side);
172 cursors[OB_CURSOR_SOUTHWEST] =
173 XCreateFontCursor(ob_display, XC_bottom_left_corner);
174 cursors[OB_CURSOR_WEST] =
175 XCreateFontCursor(ob_display, XC_left_side);
176 cursors[OB_CURSOR_NORTHWEST] =
177 XCreateFontCursor(ob_display, XC_top_left_corner);
179 /* create available keycodes */
180 keys[OB_KEY_RETURN] =
181 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
182 keys[OB_KEY_ESCAPE] =
183 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
185 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
187 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
189 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
191 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
193 prop_startup(); /* get atoms values for the display */
194 extensions_query_all(); /* find which extensions are present */
196 if (screen_annex()) { /* it will be ours! */
198 Window reconfig_window;
199 Popup *reconfig_popup;
204 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
205 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
207 reconfig_popup = popup_new(FALSE);
208 popup_position(reconfig_popup, CenterGravity, w / 2, h / 2);
209 popup_show(reconfig_popup, _("Reloading . . ."), NULL);
211 reconfig_window = XCreateWindow
212 (ob_display, RootWindow(ob_display, ob_screen),
214 0, CopyFromParent, InputOnly, CopyFromParent, 0, NULL);
215 XMapWindow(ob_display, reconfig_window);
220 event_startup(reconfigure);
221 grab_startup(reconfigure);
222 /* focus_backup is used for stacking, so this needs to come before
223 anything that calls stacking_add */
224 focus_startup(reconfigure);
225 window_startup(reconfigure);
226 sn_startup(reconfigure);
233 /* startup the parsing so everything can register sections
238 /* parse/load user options */
239 if (parse_load_rc(&doc, &node))
240 parse_tree(i, doc, node->xmlChildrenNode);
241 /* we're done with parsing now, kill it */
246 /* load the theme specified in the rc file */
249 if ((theme = RrThemeNew(ob_rr_inst, config_theme)))
251 if (ob_rr_theme == NULL)
252 ob_exit_with_error("Unable to load a theme.");
255 moveresize_startup(reconfigure);
256 screen_startup(reconfigure);
257 group_startup(reconfigure);
258 client_startup(reconfigure);
259 dock_startup(reconfigure);
260 keyboard_startup(reconfigure);
261 mouse_startup(reconfigure);
262 menu_startup(reconfigure);
265 /* get all the existing windows */
270 /* redecorate all existing windows */
271 for (it = client_list; it; it = g_list_next(it)) {
272 ObClient *c = it->data;
273 frame_adjust_theme(c->frame);
278 /* destroy the hiding window */
279 XDestroyWindow(ob_display, reconfig_window);
280 popup_free(reconfig_popup);
285 state = OB_STATE_RUNNING;
286 ob_main_loop_run(ob_main_loop);
287 state = OB_STATE_EXITING;
291 client_unmanage_all();
294 menu_shutdown(reconfigure);
295 mouse_shutdown(reconfigure);
296 keyboard_shutdown(reconfigure);
297 dock_shutdown(reconfigure);
298 client_shutdown(reconfigure);
299 group_shutdown(reconfigure);
300 screen_shutdown(reconfigure);
301 focus_shutdown(reconfigure);
302 moveresize_shutdown(reconfigure);
303 sn_shutdown(reconfigure);
304 window_shutdown(reconfigure);
305 grab_shutdown(reconfigure);
306 event_shutdown(reconfigure);
308 } while (reconfigure);
311 RrThemeFree(ob_rr_theme);
312 RrInstanceFree(ob_rr_inst);
317 XCloseDisplay(ob_display);
320 if (restart_path != NULL) {
326 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
327 execvp(argvp[0], argvp);
330 g_warning("failed to execute '%s': %s", restart_path,
336 execvp(argv[0], argv); /* try how we were run */
342 static void signal_handler(int signal, gpointer data)
344 if (signal == SIGUSR1) {
345 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
347 } else if (signal == SIGUSR2) {
348 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
351 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
356 static void print_version()
358 g_print("Openbox %s\n\n", PACKAGE_VERSION);
359 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
360 g_print("This is free software, and you are welcome to redistribute it\n");
361 g_print("under certain conditions. See the file COPYING for details.\n\n");
364 static void print_help()
367 g_print("Syntax: openbox [options]\n\n");
368 g_print("Options:\n\n");
370 g_print(" --sm-disable Disable connection to session manager\n");
371 g_print(" --sm-client-id ID Specify session management ID\n");
372 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
375 g_print(" --replace Replace the currently running window "
377 g_print(" --help Display this help and exit\n");
378 g_print(" --version Display the version and exit\n");
379 g_print(" --sync Run in synchronous mode (this is slow and\n"
380 " meant for debugging X routines)\n");
381 g_print(" --debug Display debugging output\n");
382 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
385 static void parse_args(int argc, char **argv)
389 for (i = 1; i < argc; ++i) {
390 if (!strcmp(argv[i], "--version")) {
393 } else if (!strcmp(argv[i], "--help")) {
396 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
397 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
398 } else if (!strcmp(argv[i], "--replace")) {
399 ob_replace_wm = TRUE;
400 } else if (!strcmp(argv[i], "--sync")) {
402 } else if (!strcmp(argv[i], "--debug")) {
403 ob_debug_show_output(TRUE);
405 } else if (!strcmp(argv[i], "--sm-client-id")) {
406 if (i == argc - 1) /* no args left */
407 g_printerr(_("--sm-client-id requires an argument\n"));
409 ob_sm_id = g_strdup(argv[++i]);
410 } else if (!strcmp(argv[i], "--sm-save-file")) {
411 if (i == argc - 1) /* no args left */
412 g_printerr(_("--sm-save-file requires an argument\n"));
414 sm_save_file = argv[++i];
415 } else if (!strcmp(argv[i], "--sm-disable")) {
419 g_printerr("Invalid option: '%s'\n\n", argv[i]);
426 void ob_exit_with_error(gchar *msg)
433 void ob_restart_other(const gchar *path)
435 restart_path = g_strdup(path);
447 ob_main_loop_exit(ob_main_loop);
450 Cursor ob_cursor(ObCursor cursor)
452 g_assert(cursor < OB_NUM_CURSORS);
453 return cursors[cursor];
456 KeyCode ob_keycode(ObKey key)
458 g_assert(key < OB_NUM_KEYS);
467 gchar *ob_expand_tilde(const gchar *f)
474 spl = g_strsplit(f, "~", 0);
475 mid = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, NULL);
476 ret = g_strjoinv(mid, spl);
482 void ob_reconfigure()