]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
stop using python internally. add an event dispatcher
[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 "engine.h"
12 #include "themerc.h"
13 #include "timer.h"
14 #include "../render/render.h"
15 #include "../render/font.h"
16
17 #ifdef HAVE_FCNTL_H
18 #  include <fcntl.h>
19 #endif
20 #ifdef HAVE_SYS_SELECT_H
21 #  include <sys/select.h>
22 #endif
23 #ifdef HAVE_SIGNAL_H
24 #  include <signal.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 #endif
29 #ifdef HAVE_SYS_WAIT_H
30 #  include <sys/types.h>
31 #  include <sys/wait.h>
32 #endif
33 #ifdef HAVE_LOCALE_H
34 #  include <locale.h>
35 #endif
36
37 #include <X11/cursorfont.h>
38
39 Display *ob_display  = NULL;
40 int      ob_screen;
41 Window   ob_root;
42 State    ob_state;
43 gboolean ob_shutdown = FALSE;
44 gboolean ob_restart  = FALSE;
45 char    *ob_restart_path = NULL;
46 gboolean ob_remote   = FALSE;
47 gboolean ob_sync     = TRUE;
48 Cursors  ob_cursors;
49
50 void signal_handler(int signal);
51
52 int main(int argc, char **argv)
53 {
54     struct sigaction action;
55     sigset_t sigset;
56
57     ob_state = State_Starting;
58
59     /* initialize the locale */
60     if (!setlocale(LC_ALL, ""))
61         g_warning("Couldn't set locale from environment.\n");
62     bindtextdomain(PACKAGE, LOCALEDIR);
63     bind_textdomain_codeset(PACKAGE, "UTF-8");
64     textdomain(PACKAGE);
65
66     /* set up signal handler */
67     sigemptyset(&sigset);
68     action.sa_handler = signal_handler;
69     action.sa_mask = sigset;
70     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
71     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
72     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
73     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
74     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
75     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
76     sigaction(SIGINT, &action, (struct sigaction *) NULL);
77     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
78     sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
79
80     /* anything that died while we were restarting won't give us a SIGCHLD */
81     while (waitpid(-1, NULL, WNOHANG) > 0);
82      
83     /* XXX parse out command line args */
84     (void)argc;(void)argv;
85
86     ob_display = XOpenDisplay(NULL);
87     if (ob_display == NULL) {
88         /* print a message and exit */
89         g_critical("Failed to open the display.");
90         exit(1);
91     }
92     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
93         /* print a message and exit */
94         g_critical("Failed to set display as close-on-exec.");
95         exit(1);
96     }
97           
98     ob_screen = DefaultScreen(ob_display);
99     ob_root = RootWindow(ob_display, ob_screen);
100
101     /* XXX fork self onto other screens */
102      
103     XSynchronize(ob_display, ob_sync);
104
105     /* check for locale support */
106     if (!XSupportsLocale())
107         g_warning("X server does not support locale.");
108     if (!XSetLocaleModifiers(""))
109         g_warning("Cannot set locale modifiers for the X server.");
110
111     /* set our error handler */
112     XSetErrorHandler(xerror_handler);
113
114     /* set the DISPLAY environment variable for any lauched children, to the
115        display we're using, so they open in the right place. */
116     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
117
118     ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
119     ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
120     ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
121
122     prop_startup(); /* get atoms values for the display */
123     extensions_query_all(); /* find which extensions are present */
124      
125     if (screen_annex()) { /* it will be ours! */
126         timer_startup();
127         dispatch_startup();
128         render_startup();
129         font_startup();
130         themerc_startup();
131         engine_startup(themerc_engine);
132         event_startup();
133         screen_startup();
134         focus_startup();
135         client_startup();
136           
137         /*HOOKFIRE(startup, "()");XXX*/
138
139         /* get all the existing windows */
140         client_manage_all();
141
142         ob_state = State_Running;
143         while (!ob_shutdown) {
144             event_loop();
145         }
146         ob_state = State_Exiting;
147
148         client_unmanage_all();
149
150         /*HOOKFIRE(shutdown, "()");XXX*/
151
152         client_shutdown();
153         screen_shutdown();
154         event_shutdown();
155         engine_shutdown();
156         themerc_shutdown();
157         render_shutdown();
158         dispatch_shutdown();
159         timer_shutdown();
160     }
161           
162     XCloseDisplay(ob_display);
163
164     /* XXX if (ob_restart) */
165      
166     return 0;
167 }
168
169 void signal_handler(int signal)
170 {
171     switch (signal) {
172     case SIGUSR1:
173         g_message("Caught SIGUSR1 signal. Restarting.");
174         ob_shutdown = ob_restart = TRUE;
175         break;
176
177     case SIGCHLD:
178         wait(NULL);
179         break;
180
181     case SIGHUP:
182     case SIGINT:
183     case SIGTERM:
184     case SIGPIPE:
185         g_message("Caught signal %d. Exiting.", signal);
186         ob_shutdown = TRUE;
187         break;
188
189     case SIGFPE:
190     case SIGSEGV:
191         g_error("Caught signal %d. Aborting and dumping core.", signal);
192     }
193 }