10 #include "moveresize.h"
12 #include "extensions.h"
20 #include "render/render.h"
21 #include "render/font.h"
22 #include "render/theme.h"
33 #ifdef HAVE_SYS_WAIT_H
34 # include <sys/types.h>
35 # include <sys/wait.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
45 # include <sys/types.h>
48 #include <X11/cursorfont.h>
50 Display *ob_display = NULL;
54 gboolean ob_shutdown = FALSE;
55 gboolean ob_restart = FALSE;
56 char *ob_restart_path = NULL;
57 gboolean ob_remote = TRUE;
58 gboolean ob_sync = FALSE;
60 char *ob_rc_path = NULL;
62 void signal_handler(const ObEvent *e, void *data);
63 void parse_args(int argc, char **argv);
65 int main(int argc, char **argv)
67 struct sigaction action;
72 ob_state = State_Starting;
74 /* initialize the locale */
75 if (!setlocale(LC_ALL, ""))
76 g_warning("Couldn't set locale from environment.\n");
77 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
78 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
79 textdomain(PACKAGE_NAME);
81 /* start our event dispatcher and register for signals */
83 dispatch_register(Event_Signal, signal_handler, NULL);
85 /* set up signal handler */
87 action.sa_handler = dispatch_signal;
88 action.sa_mask = sigset;
89 action.sa_flags = SA_NOCLDSTOP;
90 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
91 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
92 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
93 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
94 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
95 sigaction(SIGINT, &action, (struct sigaction *) NULL);
96 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
97 sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
99 /* anything that died while we were restarting won't give us a SIGCHLD */
100 while (waitpid(-1, NULL, WNOHANG) > 0);
102 /* create the ~/.openbox dir */
103 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
104 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
105 S_IROTH | S_IWOTH | S_IXOTH));
107 /* create the ~/.openbox/themes dir */
108 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
109 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
110 S_IROTH | S_IWOTH | S_IXOTH));
113 /* parse out command line args */
114 parse_args(argc, argv);
116 ob_display = XOpenDisplay(NULL);
117 if (ob_display == NULL) {
118 /* print a message and exit */
119 g_critical("Failed to open the display.");
122 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
123 /* print a message and exit */
124 g_critical("Failed to set display as close-on-exec.");
128 ob_screen = DefaultScreen(ob_display);
129 ob_root = RootWindow(ob_display, ob_screen);
131 /* XXX fork self onto other screens */
133 XSynchronize(ob_display, ob_sync);
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 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
149 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
150 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
151 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
152 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
153 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
155 prop_startup(); /* get atoms values for the display */
156 extensions_query_all(); /* find which extensions are present */
158 if (screen_annex()) { /* it will be ours! */
159 /* startup the parsing so everything can register sections of the rc */
162 /* anything that is going to read data from the rc file needs to be
169 moveresize_startup();
172 /* load the plugins specified in the pluginrc */
175 /* set up the kernel config shit */
177 /* parse/load user options */
179 /* we're done with parsing now, kill it */
182 /* load the theme specified in the rc file */
183 theme = theme_load(config_theme);
185 if (!theme) return 1;
195 /* call startup for all the plugins */
198 /* get all the existing windows */
201 ob_state = State_Running;
204 ob_state = State_Exiting;
206 client_unmanage_all();
208 plugin_shutdown(); /* calls all the plugins' shutdown functions */
226 XCloseDisplay(ob_display);
229 if (ob_restart_path != NULL) {
235 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
236 execvp(argvp[0], argvp);
239 g_warning("failed to execute '%s': %s", ob_restart_path,
245 execvp(argv[0], argv); /* try how we were run */
246 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
252 void signal_handler(const ObEvent *e, void *data)
256 s = e->data.s.signal;
259 g_message("Caught SIGUSR1 signal. Restarting.");
260 ob_shutdown = ob_restart = TRUE;
271 g_message("Caught signal %d. Exiting.", s);
277 g_error("Caught signal %d. Aborting and dumping core.", s);
283 g_print("Openbox %s\n\n", PACKAGE_VERSION);
284 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
285 g_print("This is free software, and you are welcome to redistribute it\n");
286 g_print("under certain conditions. See the file COPYING for details.\n\n");
292 g_print("Syntax: %s [options]\n\n", BINARY);
293 g_print("Options:\n\n");
294 g_print(" -rc PATH Specify the path to the rc file to use\n");
295 g_print(" -help Display this help and exit\n");
296 g_print(" -version Display the version and exit\n");
297 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
298 " for debugging X routines)\n");
299 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
302 void parse_args(int argc, char **argv)
306 for (i = 1; i < argc; ++i) {
307 if (!strcmp(argv[i], "-version")) {
310 } else if (!strcmp(argv[i], "-help")) {
313 } else if (!strcmp(argv[i], "-sync")) {
315 } else if (!strcmp(argv[i], "-rc")) {
316 if (i == argc - 1) /* no args left */
317 g_printerr("-rc requires an argument\n");
319 ob_rc_path = argv[++i];
321 g_printerr("Invalid option: '%s'\n\n", argv[i]);
328 gboolean ob_pointer_pos(int *x, int *y)
334 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);