9 #include "extensions.h"
16 #include "../render/render.h"
17 #include "../render/font.h"
22 #ifdef HAVE_SYS_SELECT_H
23 # include <sys/select.h>
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/types.h>
33 # include <sys/wait.h>
42 #include <X11/cursorfont.h>
44 Display *ob_display = NULL;
48 gboolean ob_shutdown = FALSE;
49 gboolean ob_restart = FALSE;
50 char *ob_restart_path = NULL;
51 gboolean ob_remote = FALSE;
52 gboolean ob_sync = FALSE;
54 char *ob_rc_path = NULL;
56 void signal_handler(const ObEvent *e, void *data);
57 void parse_args(int argc, char **argv);
59 int main(int argc, char **argv)
61 struct sigaction action;
64 ob_state = State_Starting;
66 /* initialize the locale */
67 if (!setlocale(LC_ALL, ""))
68 g_warning("Couldn't set locale from environment.\n");
69 bindtextdomain(PACKAGE, LOCALEDIR);
70 bind_textdomain_codeset(PACKAGE, "UTF-8");
73 /* start our event dispatcher and register for signals */
75 dispatch_register(Event_Signal, signal_handler, NULL);
77 /* set up signal handler */
79 action.sa_handler = dispatch_signal;
80 action.sa_mask = sigset;
81 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
82 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
83 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
84 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
85 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
86 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
87 sigaction(SIGINT, &action, (struct sigaction *) NULL);
88 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
89 sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
91 /* anything that died while we were restarting won't give us a SIGCHLD */
92 while (waitpid(-1, NULL, WNOHANG) > 0);
94 /* parse out command line args */
95 parse_args(argc, argv);
97 ob_display = XOpenDisplay(NULL);
98 if (ob_display == NULL) {
99 /* print a message and exit */
100 g_critical("Failed to open the display.");
103 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
104 /* print a message and exit */
105 g_critical("Failed to set display as close-on-exec.");
109 ob_screen = DefaultScreen(ob_display);
110 ob_root = RootWindow(ob_display, ob_screen);
112 /* XXX fork self onto other screens */
114 XSynchronize(ob_display, ob_sync);
116 /* check for locale support */
117 if (!XSupportsLocale())
118 g_warning("X server does not support locale.");
119 if (!XSetLocaleModifiers(""))
120 g_warning("Cannot set locale modifiers for the X server.");
122 /* set our error handler */
123 XSetErrorHandler(xerror_handler);
125 /* set the DISPLAY environment variable for any lauched children, to the
126 display we're using, so they open in the right place. */
127 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
129 ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
130 ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
131 ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
133 prop_startup(); /* get atoms values for the display */
134 extensions_query_all(); /* find which extensions are present */
136 if (screen_annex()) { /* it will be ours! */
141 engine_startup(themerc_engine);
149 /* XXX load all plugins!! */
150 plugin_open("focus");
151 plugin_open("keyboard");
152 plugin_open("mouse");
153 plugin_open("placement");
154 plugin_open("resistance");
156 /* get all the existing windows */
159 ob_state = State_Running;
162 ob_state = State_Exiting;
164 client_unmanage_all();
166 plugin_shutdown(); /* calls all the plugins' shutdown functions */
180 XCloseDisplay(ob_display);
183 if (ob_restart_path != NULL) {
189 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
190 execvp(argvp[0], argvp);
193 g_warning("failed to execute '%s': %s", ob_restart_path,
199 execvp(argv[0], argv); /* try how we were run */
200 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
206 void signal_handler(const ObEvent *e, void *data)
210 s = e->data.s.signal;
213 g_message("Caught SIGUSR1 signal. Restarting.");
214 ob_shutdown = ob_restart = TRUE;
225 g_message("Caught signal %d. Exiting.", s);
231 g_error("Caught signal %d. Aborting and dumping core.", s);
237 g_print("Openbox %s\n\n", VERSION);
238 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
239 g_print("This is free software, and you are welcome to redistribute it\n");
240 g_print("under certain conditions. See the file COPYING for details.\n\n");
246 g_print("Syntax: %s [options]\n\n", BINARY);
247 g_print("Options:\n\n");
248 g_print(" -rc PATH Specify the path to the rc file to use\n");
249 g_print(" -help Display this help and exit\n");
250 g_print(" -version Display the version and exit\n");
251 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
252 " for debugging X routines)\n");
253 g_print("\nPlease report bugs at %s\n", BUGURL);
256 void parse_args(int argc, char **argv)
260 for (i = 1; i < argc; ++i) {
261 if (!strcmp(argv[i], "-version")) {
264 } else if (!strcmp(argv[i], "-help")) {
267 } else if (!strcmp(argv[i], "-sync")) {
269 } else if (!strcmp(argv[i], "-rc")) {
270 if (i == argc - 1) /* no args left */
271 g_printerr("-rc requires an argument\n");
273 ob_rc_path = argv[++i];
275 g_printerr("Invalid option: '%s'\n\n", argv[i]);