14 #include "moveresize.h"
18 #include "extensions.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;
56 gboolean ob_sm_use = TRUE;
58 gboolean ob_replace_wm;
61 static gboolean xsync;
62 static gboolean shutdown;
63 static gboolean restart;
64 static char *restart_path;
65 static Cursor cursors[OB_NUM_CURSORS];
66 static KeyCode keys[OB_NUM_KEYS];
67 static gchar *sm_save_file;
69 static void signal_handler(const ObEvent *e, void *data);
70 static void parse_args(int argc, char **argv);
72 int main(int argc, char **argv)
74 struct sigaction action;
81 ob_debug_show_output(TRUE);
84 state = OB_STATE_STARTING;
86 /* initialize the locale */
87 if (!setlocale(LC_ALL, ""))
88 g_warning("Couldn't set locale from environment.\n");
89 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
90 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
91 textdomain(PACKAGE_NAME);
93 /* start our event dispatcher and register for signals */
95 dispatch_register(Event_Signal, signal_handler, NULL);
97 /* set up signal handler */
99 action.sa_handler = dispatch_signal;
100 action.sa_mask = sigset;
101 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
102 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
103 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
104 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
105 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
106 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
107 sigaction(SIGINT, &action, (struct sigaction *) NULL);
108 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
110 /* create the ~/.openbox dir */
111 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
112 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
113 S_IROTH | S_IWOTH | S_IXOTH));
115 /* create the ~/.openbox/themes dir */
116 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
117 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
118 S_IROTH | S_IWOTH | S_IXOTH));
120 /* create the ~/.openbox/sessions dir */
121 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
122 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
123 S_IROTH | S_IWOTH | S_IXOTH));
126 g_set_prgname(argv[0]);
128 /* parse out command line args */
129 parse_args(argc, argv);
131 ob_display = XOpenDisplay(NULL);
132 if (ob_display == NULL)
133 ob_exit_with_error("Failed to open the display.");
134 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
135 ob_exit_with_error("Failed to set display as close-on-exec.");
138 session_load(sm_save_file);
139 session_startup(argc, argv);
142 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
145 ob_screen = DefaultScreen(ob_display);
147 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
148 if (ob_rr_inst == NULL)
149 ob_exit_with_error("Failed to initialize the render library.");
151 /* XXX fork self onto other screens */
153 XSynchronize(ob_display, xsync);
155 /* check for locale support */
156 if (!XSupportsLocale())
157 g_warning("X server does not support locale.");
158 if (!XSetLocaleModifiers(""))
159 g_warning("Cannot set locale modifiers for the X server.");
161 /* set our error handler */
162 XSetErrorHandler(xerror_handler);
164 /* set the DISPLAY environment variable for any lauched children, to the
165 display we're using, so they open in the right place. */
166 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
168 /* create available cursors */
169 cursors[OB_CURSOR_POINTER] =
170 XCreateFontCursor(ob_display, XC_left_ptr);
171 cursors[OB_CURSOR_BUSY] =
172 XCreateFontCursor(ob_display, XC_watch);
173 cursors[OB_CURSOR_MOVE] =
174 XCreateFontCursor(ob_display, XC_fleur);
175 cursors[OB_CURSOR_NORTH] =
176 XCreateFontCursor(ob_display, XC_top_side);
177 cursors[OB_CURSOR_NORTHEAST] =
178 XCreateFontCursor(ob_display, XC_top_right_corner);
179 cursors[OB_CURSOR_EAST] =
180 XCreateFontCursor(ob_display, XC_right_side);
181 cursors[OB_CURSOR_SOUTHEAST] =
182 XCreateFontCursor(ob_display, XC_bottom_right_corner);
183 cursors[OB_CURSOR_SOUTH] =
184 XCreateFontCursor(ob_display, XC_bottom_side);
185 cursors[OB_CURSOR_SOUTHWEST] =
186 XCreateFontCursor(ob_display, XC_bottom_left_corner);
187 cursors[OB_CURSOR_WEST] =
188 XCreateFontCursor(ob_display, XC_left_side);
189 cursors[OB_CURSOR_NORTHWEST] =
190 XCreateFontCursor(ob_display, XC_top_left_corner);
192 /* create available keycodes */
193 keys[OB_KEY_RETURN] =
194 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
195 keys[OB_KEY_ESCAPE] =
196 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
198 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
200 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
202 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
204 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
206 prop_startup(); /* get atoms values for the display */
207 extensions_query_all(); /* find which extensions are present */
209 /* save stuff that we can use to restore state */
212 if (screen_annex()) { /* it will be ours! */
213 /* startup the parsing so everything can register sections of the rc */
216 /* anything that is going to read data from the rc file needs to be
221 /* focus_backup is used for stacking, so this needs to come before
222 anything that calls stacking_add */
226 /* load the plugins specified in the pluginrc */
229 /* set up the kernel config shit */
232 /* parse/load user options */
233 if (parse_load_rc(&doc, &node))
234 parse_tree(doc, node->xmlChildrenNode, NULL);
235 /* we're done with parsing now, kill it */
238 /* load the theme specified in the rc file */
239 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
240 if (ob_rr_theme == NULL)
241 ob_exit_with_error("Unable to load a theme.");
243 moveresize_startup();
251 /* call startup for all the plugins */
254 /* get all the existing windows */
257 state = OB_STATE_RUNNING;
260 state = OB_STATE_EXITING;
263 client_unmanage_all();
265 plugin_shutdown(); /* calls all the plugins' shutdown functions */
273 moveresize_shutdown();
284 RrThemeFree(ob_rr_theme);
285 RrInstanceFree(ob_rr_inst);
290 XCloseDisplay(ob_display);
293 if (restart_path != NULL) {
299 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
300 execvp(argvp[0], argvp);
303 g_warning("failed to execute '%s': %s", restart_path,
309 execvp(argv[0], argv); /* try how we were run */
315 static void signal_handler(const ObEvent *e, void *data)
319 s = e->data.s.signal;
322 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
330 fprintf(stderr, "Caught signal %d. Exiting.", s);
336 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
341 static void print_version()
343 g_print("Openbox %s\n\n", PACKAGE_VERSION);
344 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
345 g_print("This is free software, and you are welcome to redistribute it\n");
346 g_print("under certain conditions. See the file COPYING for details.\n\n");
349 static void print_help()
352 g_print("Syntax: openbox [options]\n\n");
353 g_print("Options:\n\n");
355 g_print(" --sm-disable Disable connection to session manager\n");
356 g_print(" --sm-client-id ID Specify session management ID\n");
357 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
360 g_print(" --replace Replace the currently running window "
362 g_print(" --help Display this help and exit\n");
363 g_print(" --version Display the version and exit\n");
364 g_print(" --sync Run in synchronous mode (this is slow and\n"
365 " meant for debugging X routines)\n");
366 g_print(" --debug Display debugging output\n");
367 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
370 static void parse_args(int argc, char **argv)
374 for (i = 1; i < argc; ++i) {
375 if (!strcmp(argv[i], "--version")) {
378 } else if (!strcmp(argv[i], "--help")) {
381 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
382 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
383 } else if (!strcmp(argv[i], "--replace")) {
384 ob_replace_wm = TRUE;
385 } else if (!strcmp(argv[i], "--sync")) {
387 } else if (!strcmp(argv[i], "--debug")) {
388 ob_debug_show_output(TRUE);
390 } else if (!strcmp(argv[i], "--sm-client-id")) {
391 if (i == argc - 1) /* no args left */
392 g_printerr(_("--sm-client-id requires an argument\n"));
394 ob_sm_id = g_strdup(argv[++i]);
395 } else if (!strcmp(argv[i], "--sm-save-file")) {
396 if (i == argc - 1) /* no args left */
397 g_printerr(_("--sm-save-file requires an argument\n"));
399 sm_save_file = argv[++i];
400 } else if (!strcmp(argv[i], "--sm-disable")) {
404 g_printerr("Invalid option: '%s'\n\n", argv[i]);
411 void ob_exit_with_error(gchar *msg)
418 void ob_restart_other(const gchar *path)
420 restart_path = g_strdup(path);
435 Cursor ob_cursor(ObCursor cursor)
437 g_assert(cursor < OB_NUM_CURSORS);
438 return cursors[cursor];
441 KeyCode ob_keycode(ObKey key)
443 g_assert(key < OB_NUM_KEYS);