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"
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 # include <sys/types.h>
49 #include <X11/cursorfont.h>
51 RrInstance *ob_rr_inst;
53 ObMainLoop *ob_main_loop;
56 gboolean ob_replace_wm;
59 static gboolean xsync;
60 static gboolean reconfigure;
61 static gboolean restart;
62 static char *restart_path;
63 static Cursor cursors[OB_NUM_CURSORS];
64 static KeyCode keys[OB_NUM_KEYS];
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)
74 ob_debug_show_output(TRUE);
77 state = OB_STATE_STARTING;
79 /* initialize the locale */
80 if (!setlocale(LC_ALL, ""))
81 g_warning("Couldn't set locale from environment.\n");
82 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
83 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
84 textdomain(PACKAGE_NAME);
86 /* create the ~/.openbox dir */
87 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
88 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
89 S_IROTH | S_IWOTH | S_IXOTH));
91 /* create the ~/.openbox/themes dir */
92 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
93 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
94 S_IROTH | S_IWOTH | S_IXOTH));
96 /* create the ~/.openbox/sessions dir */
97 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
98 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
99 S_IROTH | S_IWOTH | S_IXOTH));
102 g_set_prgname(argv[0]);
104 session_startup(&argc, &argv);
106 /* parse out command line args */
107 parse_args(argc, argv);
109 ob_display = XOpenDisplay(NULL);
110 if (ob_display == NULL)
111 ob_exit_with_error("Failed to open the display.");
112 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
113 ob_exit_with_error("Failed to set display as close-on-exec.");
115 ob_main_loop = ob_main_loop_new(ob_display);
117 /* set up signal handler */
118 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
119 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
120 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
121 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
122 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
123 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
125 ob_screen = DefaultScreen(ob_display);
127 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
128 if (ob_rr_inst == NULL)
129 ob_exit_with_error("Failed to initialize the render library.");
131 /* XXX fork self onto other screens */
133 XSynchronize(ob_display, xsync);
135 /* check for locale support */
136 if (!XSupportsLocale())
137 g_warning("X server does not support locale.");
138 if (!XSetLocaleModifiers(""))
139 g_warning("Cannot set locale modifiers for the X server.");
141 /* set our error handler */
142 XSetErrorHandler(xerror_handler);
144 /* set the DISPLAY environment variable for any lauched children, to the
145 display we're using, so they open in the right place. */
146 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
148 /* create available cursors */
149 cursors[OB_CURSOR_NONE] = None;
150 cursors[OB_CURSOR_POINTER] =
151 XCreateFontCursor(ob_display, XC_left_ptr);
152 cursors[OB_CURSOR_BUSY] =
153 XCreateFontCursor(ob_display, XC_watch);
154 cursors[OB_CURSOR_MOVE] =
155 XCreateFontCursor(ob_display, XC_fleur);
156 cursors[OB_CURSOR_NORTH] =
157 XCreateFontCursor(ob_display, XC_top_side);
158 cursors[OB_CURSOR_NORTHEAST] =
159 XCreateFontCursor(ob_display, XC_top_right_corner);
160 cursors[OB_CURSOR_EAST] =
161 XCreateFontCursor(ob_display, XC_right_side);
162 cursors[OB_CURSOR_SOUTHEAST] =
163 XCreateFontCursor(ob_display, XC_bottom_right_corner);
164 cursors[OB_CURSOR_SOUTH] =
165 XCreateFontCursor(ob_display, XC_bottom_side);
166 cursors[OB_CURSOR_SOUTHWEST] =
167 XCreateFontCursor(ob_display, XC_bottom_left_corner);
168 cursors[OB_CURSOR_WEST] =
169 XCreateFontCursor(ob_display, XC_left_side);
170 cursors[OB_CURSOR_NORTHWEST] =
171 XCreateFontCursor(ob_display, XC_top_left_corner);
173 /* create available keycodes */
174 keys[OB_KEY_RETURN] =
175 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
176 keys[OB_KEY_ESCAPE] =
177 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
179 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
181 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
183 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
185 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
187 prop_startup(); /* get atoms values for the display */
188 extensions_query_all(); /* find which extensions are present */
190 if (screen_annex()) { /* it will be ours! */
197 /* startup the parsing so everything can register sections
202 /* parse/load user options */
203 if (parse_load_rc(&doc, &node))
204 parse_tree(i, doc, node->xmlChildrenNode);
205 /* we're done with parsing now, kill it */
210 /* load the theme specified in the rc file */
213 if ((theme = RrThemeNew(ob_rr_inst, config_theme))) {
214 RrThemeFree(ob_rr_theme);
217 if (ob_rr_theme == NULL)
218 ob_exit_with_error("Unable to load a theme.");
221 event_startup(reconfigure);
222 grab_startup(reconfigure);
223 /* focus_backup is used for stacking, so this needs to come before
224 anything that calls stacking_add */
225 focus_startup(reconfigure);
226 window_startup(reconfigure);
227 sn_startup(reconfigure);
228 screen_startup(reconfigure);
229 group_startup(reconfigure);
230 client_startup(reconfigure);
231 dock_startup(reconfigure);
232 moveresize_startup(reconfigure);
233 keyboard_startup(reconfigure);
234 mouse_startup(reconfigure);
235 menu_startup(reconfigure);
238 /* get all the existing windows */
243 /* redecorate all existing windows */
244 for (it = client_list; it; it = g_list_next(it)) {
245 ObClient *c = it->data;
246 frame_adjust_theme(c->frame);
252 state = OB_STATE_RUNNING;
253 ob_main_loop_run(ob_main_loop);
254 state = OB_STATE_EXITING;
258 client_unmanage_all();
261 menu_shutdown(reconfigure);
262 mouse_shutdown(reconfigure);
263 keyboard_shutdown(reconfigure);
264 moveresize_shutdown(reconfigure);
265 dock_shutdown(reconfigure);
266 client_shutdown(reconfigure);
267 group_shutdown(reconfigure);
268 screen_shutdown(reconfigure);
269 focus_shutdown(reconfigure);
270 sn_shutdown(reconfigure);
271 window_shutdown(reconfigure);
272 grab_shutdown(reconfigure);
273 event_shutdown(reconfigure);
275 } while (reconfigure);
278 RrThemeFree(ob_rr_theme);
279 RrInstanceFree(ob_rr_inst);
283 XCloseDisplay(ob_display);
286 if (restart_path != NULL) {
292 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
293 execvp(argvp[0], argvp);
296 g_warning("failed to execute '%s': %s", restart_path,
302 execvp(argv[0], argv); /* try how we were run */
303 execlp(argv[0], g_path_get_basename(argv[0])); /* last resort */
309 static void signal_handler(int signal, gpointer data)
311 if (signal == SIGUSR1) {
312 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
314 } else if (signal == SIGUSR2) {
315 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
318 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
323 static void print_version()
325 g_print("Openbox %s\n\n", PACKAGE_VERSION);
326 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
327 g_print("This is free software, and you are welcome to redistribute it\n");
328 g_print("under certain conditions. See the file COPYING for details.\n\n");
331 static void print_help()
334 g_print("Syntax: openbox [options]\n\n");
335 g_print("Options:\n\n");
337 g_print(" --sm-disable Disable connection to session manager\n");
338 g_print(" --sm-client-id ID Specify session management ID\n");
339 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
342 g_print(" --replace Replace the currently running window "
344 g_print(" --help Display this help and exit\n");
345 g_print(" --version Display the version and exit\n");
346 g_print(" --sync Run in synchronous mode (this is slow and\n"
347 " meant for debugging X routines)\n");
348 g_print(" --debug Display debugging output\n");
349 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
352 static void parse_args(int argc, char **argv)
356 for (i = 1; i < argc; ++i) {
357 if (!strcmp(argv[i], "--version")) {
360 } else if (!strcmp(argv[i], "--help")) {
363 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
364 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
365 } else if (!strcmp(argv[i], "--replace")) {
366 ob_replace_wm = TRUE;
367 } else if (!strcmp(argv[i], "--sync")) {
369 } else if (!strcmp(argv[i], "--debug")) {
370 ob_debug_show_output(TRUE);
372 g_printerr("Invalid option: '%s'\n\n", argv[i]);
379 void ob_exit_with_error(gchar *msg)
386 void ob_restart_other(const gchar *path)
388 restart_path = g_strdup(path);
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);
420 gchar *ob_expand_tilde(const gchar *f)
427 spl = g_strsplit(f, "~", 0);
428 ret = g_strjoinv(g_get_home_dir(), spl);
433 void ob_reconfigure()