]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
add the binary and urls to the Makefile and -D's
[mikachu/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "client.h"
4 #include "dispatch.h"
5 #include "xerror.h"
6 #include "prop.h"
7 #include "screen.h"
8 #include "focus.h"
9 #include "extensions.h"
10 #include "gettext.h"
11 #include "grab.h"
12 #include "engine.h"
13 #include "themerc.h"
14 #include "plugin.h"
15 #include "timer.h"
16 #include "../render/render.h"
17 #include "../render/font.h"
18
19 #ifdef HAVE_FCNTL_H
20 #  include <fcntl.h>
21 #endif
22 #ifdef HAVE_SYS_SELECT_H
23 #  include <sys/select.h>
24 #endif
25 #ifdef HAVE_SIGNAL_H
26 #  include <signal.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 #  include <stdlib.h>
30 #endif
31 #ifdef HAVE_SYS_WAIT_H
32 #  include <sys/types.h>
33 #  include <sys/wait.h>
34 #endif
35 #ifdef HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>
40 #endif
41
42 #include <X11/cursorfont.h>
43
44 Display *ob_display  = NULL;
45 int      ob_screen;
46 Window   ob_root;
47 State    ob_state;
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;
53 Cursors  ob_cursors;
54
55 void signal_handler(const ObEvent *e, void *data);
56
57 int main(int argc, char **argv)
58 {
59     struct sigaction action;
60     sigset_t sigset;
61
62     ob_state = State_Starting;
63
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");
69     textdomain(PACKAGE);
70
71     /* start our event dispatcher and register for signals */
72     dispatch_startup();
73     dispatch_register(Event_Signal, signal_handler, NULL);
74
75     /* set up signal handler */
76     sigemptyset(&sigset);
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);
88
89     /* anything that died while we were restarting won't give us a SIGCHLD */
90     while (waitpid(-1, NULL, WNOHANG) > 0);
91      
92     /* XXX parse out command line args */
93     (void)argc;(void)argv;
94
95     ob_display = XOpenDisplay(NULL);
96     if (ob_display == NULL) {
97         /* print a message and exit */
98         g_critical("Failed to open the display.");
99         exit(1);
100     }
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.");
104         exit(1);
105     }
106           
107     ob_screen = DefaultScreen(ob_display);
108     ob_root = RootWindow(ob_display, ob_screen);
109
110     /* XXX fork self onto other screens */
111      
112     XSynchronize(ob_display, ob_sync);
113
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.");
119
120     /* set our error handler */
121     XSetErrorHandler(xerror_handler);
122
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)));
126
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);
130
131     prop_startup(); /* get atoms values for the display */
132     extensions_query_all(); /* find which extensions are present */
133      
134     if (screen_annex()) { /* it will be ours! */
135         timer_startup();
136         render_startup();
137         font_startup();
138         themerc_startup();
139         engine_startup(themerc_engine);
140         event_startup();
141         screen_startup();
142         focus_startup();
143         client_startup();
144         grab_startup();
145         plugin_startup();
146
147         /* XXX load all plugins!! */
148         plugin_open("focus");
149         plugin_open("keyboard");
150         plugin_open("mouse");
151
152         /* get all the existing windows */
153         client_manage_all();
154
155         ob_state = State_Running;
156         while (!ob_shutdown)
157             event_loop();
158         ob_state = State_Exiting;
159
160         client_unmanage_all();
161
162         plugin_shutdown(); /* calls all the plugins' shutdown functions */
163         grab_shutdown();
164         client_shutdown();
165         focus_shutdown();
166         screen_shutdown();
167         event_shutdown();
168         engine_shutdown();
169         themerc_shutdown();
170         render_shutdown();
171         timer_shutdown();
172     }
173
174     dispatch_shutdown();
175
176     XCloseDisplay(ob_display);
177
178     if (ob_restart) {
179         ob_restart_path = "";
180         if (ob_restart_path != NULL) {
181             int argcp;
182             char **argvp;
183             GError *err = NULL;
184
185             /* run other shit */
186             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
187                 execvp(argvp[0], argvp);
188                 g_strfreev(argvp);
189             } else {
190                 g_warning("failed to execute '%s': %s", ob_restart_path,
191                           err->message);
192             }
193         }
194
195         /* re-run me */
196         execvp(argv[0], argv); /* try how we were run */
197         execlp("ob3", "ob3", NULL); /* try this as a last resort */
198     }
199      
200     return 0;
201 }
202
203 void signal_handler(const ObEvent *e, void *data)
204 {
205     int s;
206
207     s = e->data.s.signal;
208     switch (s) {
209     case SIGUSR1:
210         g_message("Caught SIGUSR1 signal. Restarting.");
211         ob_shutdown = ob_restart = TRUE;
212         break;
213
214     case SIGCHLD:
215         wait(NULL);
216         break;
217
218     case SIGHUP:
219     case SIGINT:
220     case SIGTERM:
221     case SIGPIPE:
222         g_message("Caught signal %d. Exiting.", s);
223         ob_shutdown = TRUE;
224         break;
225
226     case SIGFPE:
227     case SIGSEGV:
228         g_error("Caught signal %d. Aborting and dumping core.", s);
229     }
230 }