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 = TRUE;
55 void signal_handler(const ObEvent *e, void *data);
57 int main(int argc, char **argv)
59 struct sigaction action;
62 ob_state = State_Starting;
64 /* initialize the locale */
65 if (!setlocale(LC_ALL, ""))
66 g_warning("Couldn't set locale from environment.\n");
67 bindtextdomain(PACKAGE, LOCALEDIR);
68 bind_textdomain_codeset(PACKAGE, "UTF-8");
71 /* start our event dispatcher and register for signals */
73 dispatch_register(Event_Signal, signal_handler, NULL);
75 /* set up signal handler */
77 action.sa_handler = dispatch_signal;
78 action.sa_mask = sigset;
79 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
80 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
81 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
82 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
83 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
84 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
85 sigaction(SIGINT, &action, (struct sigaction *) NULL);
86 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
87 sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
89 /* anything that died while we were restarting won't give us a SIGCHLD */
90 while (waitpid(-1, NULL, WNOHANG) > 0);
92 /* XXX parse out command line args */
93 (void)argc;(void)argv;
95 ob_display = XOpenDisplay(NULL);
96 if (ob_display == NULL) {
97 /* print a message and exit */
98 g_critical("Failed to open the display.");
101 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
102 /* print a message and exit */
103 g_critical("Failed to set display as close-on-exec.");
107 ob_screen = DefaultScreen(ob_display);
108 ob_root = RootWindow(ob_display, ob_screen);
110 /* XXX fork self onto other screens */
112 XSynchronize(ob_display, ob_sync);
114 /* check for locale support */
115 if (!XSupportsLocale())
116 g_warning("X server does not support locale.");
117 if (!XSetLocaleModifiers(""))
118 g_warning("Cannot set locale modifiers for the X server.");
120 /* set our error handler */
121 XSetErrorHandler(xerror_handler);
123 /* set the DISPLAY environment variable for any lauched children, to the
124 display we're using, so they open in the right place. */
125 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
127 ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
128 ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
129 ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
131 prop_startup(); /* get atoms values for the display */
132 extensions_query_all(); /* find which extensions are present */
134 if (screen_annex()) { /* it will be ours! */
139 engine_startup(themerc_engine);
147 /* XXX load all plugins!! */
148 plugin_open("focus");
149 plugin_open("keyboard");
150 plugin_open("mouse");
152 /* get all the existing windows */
155 ob_state = State_Running;
158 ob_state = State_Exiting;
160 client_unmanage_all();
162 plugin_shutdown(); /* calls all the plugins' shutdown functions */
176 XCloseDisplay(ob_display);
179 ob_restart_path = "";
180 if (ob_restart_path != NULL) {
186 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
187 execvp(argvp[0], argvp);
190 g_warning("failed to execute '%s': %s", ob_restart_path,
196 execvp(argv[0], argv); /* try how we were run */
197 execlp("ob3", "ob3", NULL); /* try this as a last resort */
203 void signal_handler(const ObEvent *e, void *data)
207 s = e->data.s.signal;
210 g_message("Caught SIGUSR1 signal. Restarting.");
211 ob_shutdown = ob_restart = TRUE;
222 g_message("Caught signal %d. Exiting.", s);
228 g_error("Caught signal %d. Aborting and dumping core.", s);