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;
54 gboolean ob_sm_use = TRUE;
56 gboolean ob_replace_wm;
59 static gboolean xsync;
60 static gboolean shutdown;
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);
68 static void parse_args(int argc, char **argv);
70 int main(int argc, char **argv)
72 struct sigaction action;
79 ob_debug_show_output(TRUE);
82 state = OB_STATE_STARTING;
84 /* initialize the locale */
85 if (!setlocale(LC_ALL, ""))
86 g_warning("Couldn't set locale from environment.\n");
87 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
88 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
89 textdomain(PACKAGE_NAME);
91 /* set up signal handler */
93 action.sa_handler = signal_handler;
94 action.sa_mask = sigset;
95 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
96 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
97 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
98 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
99 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
100 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
101 sigaction(SIGINT, &action, (struct sigaction *) NULL);
102 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
104 /* create the ~/.openbox dir */
105 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
106 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
107 S_IROTH | S_IWOTH | S_IXOTH));
109 /* create the ~/.openbox/themes dir */
110 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
111 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
112 S_IROTH | S_IWOTH | S_IXOTH));
114 /* create the ~/.openbox/sessions dir */
115 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
116 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
117 S_IROTH | S_IWOTH | S_IXOTH));
120 g_set_prgname(argv[0]);
122 /* parse out command line args */
123 parse_args(argc, argv);
125 ob_display = XOpenDisplay(NULL);
126 if (ob_display == NULL)
127 ob_exit_with_error("Failed to open the display.");
128 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
129 ob_exit_with_error("Failed to set display as close-on-exec.");
132 session_load(sm_save_file);
133 session_startup(argc, argv);
136 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
139 ob_screen = DefaultScreen(ob_display);
141 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
142 if (ob_rr_inst == NULL)
143 ob_exit_with_error("Failed to initialize the render library.");
145 /* XXX fork self onto other screens */
147 XSynchronize(ob_display, xsync);
149 /* check for locale support */
150 if (!XSupportsLocale())
151 g_warning("X server does not support locale.");
152 if (!XSetLocaleModifiers(""))
153 g_warning("Cannot set locale modifiers for the X server.");
155 /* set our error handler */
156 XSetErrorHandler(xerror_handler);
158 /* set the DISPLAY environment variable for any lauched children, to the
159 display we're using, so they open in the right place. */
160 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
162 /* create available cursors */
163 cursors[OB_CURSOR_POINTER] =
164 XCreateFontCursor(ob_display, XC_left_ptr);
165 cursors[OB_CURSOR_BUSY] =
166 XCreateFontCursor(ob_display, XC_watch);
167 cursors[OB_CURSOR_MOVE] =
168 XCreateFontCursor(ob_display, XC_fleur);
169 cursors[OB_CURSOR_NORTH] =
170 XCreateFontCursor(ob_display, XC_top_side);
171 cursors[OB_CURSOR_NORTHEAST] =
172 XCreateFontCursor(ob_display, XC_top_right_corner);
173 cursors[OB_CURSOR_EAST] =
174 XCreateFontCursor(ob_display, XC_right_side);
175 cursors[OB_CURSOR_SOUTHEAST] =
176 XCreateFontCursor(ob_display, XC_bottom_right_corner);
177 cursors[OB_CURSOR_SOUTH] =
178 XCreateFontCursor(ob_display, XC_bottom_side);
179 cursors[OB_CURSOR_SOUTHWEST] =
180 XCreateFontCursor(ob_display, XC_bottom_left_corner);
181 cursors[OB_CURSOR_WEST] =
182 XCreateFontCursor(ob_display, XC_left_side);
183 cursors[OB_CURSOR_NORTHWEST] =
184 XCreateFontCursor(ob_display, XC_top_left_corner);
186 /* create available keycodes */
187 keys[OB_KEY_RETURN] =
188 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
189 keys[OB_KEY_ESCAPE] =
190 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
192 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
194 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
196 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
198 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
200 prop_startup(); /* get atoms values for the display */
201 extensions_query_all(); /* find which extensions are present */
203 /* save stuff that we can use to restore state */
206 if (screen_annex()) { /* it will be ours! */
209 /* startup the parsing so everything can register sections of the rc */
212 /* anything that is going to read data from the rc file needs to be
217 /* focus_backup is used for stacking, so this needs to come before
218 anything that calls stacking_add */
222 /* set up the kernel config shit */
224 /* parse/load user options */
225 if (parse_load_rc(&doc, &node))
226 parse_tree(i, doc, node->xmlChildrenNode);
227 /* we're done with parsing now, kill it */
231 /* load the theme specified in the rc file */
232 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
233 if (ob_rr_theme == NULL)
234 ob_exit_with_error("Unable to load a theme.");
236 moveresize_startup();
245 /* get all the existing windows */
248 state = OB_STATE_RUNNING;
251 state = OB_STATE_EXITING;
254 client_unmanage_all();
264 moveresize_shutdown();
272 RrThemeFree(ob_rr_theme);
273 RrInstanceFree(ob_rr_inst);
279 sn_display_unref(ob_sn_display);
282 XCloseDisplay(ob_display);
285 if (restart_path != NULL) {
291 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
292 execvp(argvp[0], argvp);
295 g_warning("failed to execute '%s': %s", restart_path,
301 execvp(argv[0], argv); /* try how we were run */
307 static void signal_handler(int sig)
311 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
319 fprintf(stderr, "Caught signal %d. Exiting.", sig);
325 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", sig);
330 static void print_version()
332 g_print("Openbox %s\n\n", PACKAGE_VERSION);
333 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
334 g_print("This is free software, and you are welcome to redistribute it\n");
335 g_print("under certain conditions. See the file COPYING for details.\n\n");
338 static void print_help()
341 g_print("Syntax: openbox [options]\n\n");
342 g_print("Options:\n\n");
344 g_print(" --sm-disable Disable connection to session manager\n");
345 g_print(" --sm-client-id ID Specify session management ID\n");
346 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
349 g_print(" --replace Replace the currently running window "
351 g_print(" --help Display this help and exit\n");
352 g_print(" --version Display the version and exit\n");
353 g_print(" --sync Run in synchronous mode (this is slow and\n"
354 " meant for debugging X routines)\n");
355 g_print(" --debug Display debugging output\n");
356 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
359 static void parse_args(int argc, char **argv)
363 for (i = 1; i < argc; ++i) {
364 if (!strcmp(argv[i], "--version")) {
367 } else if (!strcmp(argv[i], "--help")) {
370 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
371 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
372 } else if (!strcmp(argv[i], "--replace")) {
373 ob_replace_wm = TRUE;
374 } else if (!strcmp(argv[i], "--sync")) {
376 } else if (!strcmp(argv[i], "--debug")) {
377 ob_debug_show_output(TRUE);
379 } else if (!strcmp(argv[i], "--sm-client-id")) {
380 if (i == argc - 1) /* no args left */
381 g_printerr(_("--sm-client-id requires an argument\n"));
383 ob_sm_id = g_strdup(argv[++i]);
384 } else if (!strcmp(argv[i], "--sm-save-file")) {
385 if (i == argc - 1) /* no args left */
386 g_printerr(_("--sm-save-file requires an argument\n"));
388 sm_save_file = argv[++i];
389 } else if (!strcmp(argv[i], "--sm-disable")) {
393 g_printerr("Invalid option: '%s'\n\n", argv[i]);
400 void ob_exit_with_error(gchar *msg)
407 void ob_restart_other(const gchar *path)
409 restart_path = g_strdup(path);
424 Cursor ob_cursor(ObCursor cursor)
426 g_assert(cursor < OB_NUM_CURSORS);
427 return cursors[cursor];
430 KeyCode ob_keycode(ObKey key)
432 g_assert(key < OB_NUM_KEYS);