13 #include "moveresize.h"
17 #include "extensions.h"
23 #include "parser/parse.h"
24 #include "render/render.h"
25 #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_sm_use = TRUE;
57 gboolean ob_replace_wm;
60 static gboolean xsync;
61 static gboolean restart;
62 static char *restart_path;
63 static Cursor cursors[OB_NUM_CURSORS];
64 static KeyCode keys[OB_NUM_KEYS];
65 static gchar *sm_save_file;
67 static void signal_handler(int signal, gpointer data);
68 static void parse_args(int argc, char **argv);
70 int main(int argc, char **argv)
77 ob_debug_show_output(TRUE);
80 state = OB_STATE_STARTING;
82 /* initialize the locale */
83 if (!setlocale(LC_ALL, ""))
84 g_warning("Couldn't set locale from environment.\n");
85 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
86 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
87 textdomain(PACKAGE_NAME);
89 /* create the ~/.openbox dir */
90 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
91 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
92 S_IROTH | S_IWOTH | S_IXOTH));
94 /* create the ~/.openbox/themes dir */
95 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
96 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
97 S_IROTH | S_IWOTH | S_IXOTH));
99 /* create the ~/.openbox/sessions dir */
100 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
101 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
102 S_IROTH | S_IWOTH | S_IXOTH));
105 g_set_prgname(argv[0]);
107 /* parse out command line args */
108 parse_args(argc, argv);
110 ob_display = XOpenDisplay(NULL);
111 if (ob_display == NULL)
112 ob_exit_with_error("Failed to open the display.");
113 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
114 ob_exit_with_error("Failed to set display as close-on-exec.");
116 ob_main_loop = ob_main_loop_new(ob_display);
118 /* set up signal handler */
119 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, 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);
126 session_load(sm_save_file);
127 session_startup(argc, argv);
130 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
133 ob_screen = DefaultScreen(ob_display);
135 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
136 if (ob_rr_inst == NULL)
137 ob_exit_with_error("Failed to initialize the render library.");
139 /* XXX fork self onto other screens */
141 XSynchronize(ob_display, xsync);
143 /* check for locale support */
144 if (!XSupportsLocale())
145 g_warning("X server does not support locale.");
146 if (!XSetLocaleModifiers(""))
147 g_warning("Cannot set locale modifiers for the X server.");
149 /* set our error handler */
150 XSetErrorHandler(xerror_handler);
152 /* set the DISPLAY environment variable for any lauched children, to the
153 display we're using, so they open in the right place. */
154 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
156 /* create available cursors */
157 cursors[OB_CURSOR_POINTER] =
158 XCreateFontCursor(ob_display, XC_left_ptr);
159 cursors[OB_CURSOR_BUSY] =
160 XCreateFontCursor(ob_display, XC_watch);
161 cursors[OB_CURSOR_MOVE] =
162 XCreateFontCursor(ob_display, XC_fleur);
163 cursors[OB_CURSOR_NORTH] =
164 XCreateFontCursor(ob_display, XC_top_side);
165 cursors[OB_CURSOR_NORTHEAST] =
166 XCreateFontCursor(ob_display, XC_top_right_corner);
167 cursors[OB_CURSOR_EAST] =
168 XCreateFontCursor(ob_display, XC_right_side);
169 cursors[OB_CURSOR_SOUTHEAST] =
170 XCreateFontCursor(ob_display, XC_bottom_right_corner);
171 cursors[OB_CURSOR_SOUTH] =
172 XCreateFontCursor(ob_display, XC_bottom_side);
173 cursors[OB_CURSOR_SOUTHWEST] =
174 XCreateFontCursor(ob_display, XC_bottom_left_corner);
175 cursors[OB_CURSOR_WEST] =
176 XCreateFontCursor(ob_display, XC_left_side);
177 cursors[OB_CURSOR_NORTHWEST] =
178 XCreateFontCursor(ob_display, XC_top_left_corner);
180 /* create available keycodes */
181 keys[OB_KEY_RETURN] =
182 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
183 keys[OB_KEY_ESCAPE] =
184 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
186 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
188 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
190 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
192 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
194 prop_startup(); /* get atoms values for the display */
195 extensions_query_all(); /* find which extensions are present */
197 /* save stuff that we can use to restore state */
200 if (screen_annex()) { /* it will be ours! */
203 /* startup the parsing so everything can register sections of the rc */
208 /* focus_backup is used for stacking, so this needs to come before
209 anything that calls stacking_add */
213 /* set up the kernel config shit */
215 /* parse/load user options */
216 if (parse_load_rc(&doc, &node))
217 parse_tree(i, doc, node->xmlChildrenNode);
218 /* we're done with parsing now, kill it */
222 /* load the theme specified in the rc file */
223 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
224 if (ob_rr_theme == NULL)
225 ob_exit_with_error("Unable to load a theme.");
227 moveresize_startup();
236 /* get all the existing windows */
239 state = OB_STATE_RUNNING;
240 ob_main_loop_run(ob_main_loop);
241 state = OB_STATE_EXITING;
244 client_unmanage_all();
254 moveresize_shutdown();
261 RrThemeFree(ob_rr_theme);
262 RrInstanceFree(ob_rr_inst);
268 sn_display_unref(ob_sn_display);
271 XCloseDisplay(ob_display);
274 if (restart_path != NULL) {
280 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
281 execvp(argvp[0], argvp);
284 g_warning("failed to execute '%s': %s", restart_path,
290 execvp(argv[0], argv); /* try how we were run */
296 static void signal_handler(int signal, gpointer data)
298 if (signal == SIGUSR1) {
299 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
302 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
307 static void print_version()
309 g_print("Openbox %s\n\n", PACKAGE_VERSION);
310 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
311 g_print("This is free software, and you are welcome to redistribute it\n");
312 g_print("under certain conditions. See the file COPYING for details.\n\n");
315 static void print_help()
318 g_print("Syntax: openbox [options]\n\n");
319 g_print("Options:\n\n");
321 g_print(" --sm-disable Disable connection to session manager\n");
322 g_print(" --sm-client-id ID Specify session management ID\n");
323 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
326 g_print(" --replace Replace the currently running window "
328 g_print(" --help Display this help and exit\n");
329 g_print(" --version Display the version and exit\n");
330 g_print(" --sync Run in synchronous mode (this is slow and\n"
331 " meant for debugging X routines)\n");
332 g_print(" --debug Display debugging output\n");
333 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
336 static void parse_args(int argc, char **argv)
340 for (i = 1; i < argc; ++i) {
341 if (!strcmp(argv[i], "--version")) {
344 } else if (!strcmp(argv[i], "--help")) {
347 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
348 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
349 } else if (!strcmp(argv[i], "--replace")) {
350 ob_replace_wm = TRUE;
351 } else if (!strcmp(argv[i], "--sync")) {
353 } else if (!strcmp(argv[i], "--debug")) {
354 ob_debug_show_output(TRUE);
356 } else if (!strcmp(argv[i], "--sm-client-id")) {
357 if (i == argc - 1) /* no args left */
358 g_printerr(_("--sm-client-id requires an argument\n"));
360 ob_sm_id = g_strdup(argv[++i]);
361 } else if (!strcmp(argv[i], "--sm-save-file")) {
362 if (i == argc - 1) /* no args left */
363 g_printerr(_("--sm-save-file requires an argument\n"));
365 sm_save_file = argv[++i];
366 } else if (!strcmp(argv[i], "--sm-disable")) {
370 g_printerr("Invalid option: '%s'\n\n", argv[i]);
377 void ob_exit_with_error(gchar *msg)
384 void ob_restart_other(const gchar *path)
386 restart_path = g_strdup(path);
398 ob_main_loop_exit(ob_main_loop);
401 Cursor ob_cursor(ObCursor cursor)
403 g_assert(cursor < OB_NUM_CURSORS);
404 return cursors[cursor];
407 KeyCode ob_keycode(ObKey key)
409 g_assert(key < OB_NUM_KEYS);