]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
all events are dispatched
[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(const ObEvent *e);
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     /* start our event dispatcher and register for signals */
67     dispatch_startup();
68     dispatch_register(signal_handler, Event_Signal);
69
70     /* set up signal handler */
71     sigemptyset(&sigset);
72     action.sa_handler = dispatch_signal;
73     action.sa_mask = sigset;
74     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
75     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
76     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
77     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
78     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
79     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
80     sigaction(SIGINT, &action, (struct sigaction *) NULL);
81     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
82     sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
83
84     /* anything that died while we were restarting won't give us a SIGCHLD */
85     while (waitpid(-1, NULL, WNOHANG) > 0);
86      
87     /* XXX parse out command line args */
88     (void)argc;(void)argv;
89
90     ob_display = XOpenDisplay(NULL);
91     if (ob_display == NULL) {
92         /* print a message and exit */
93         g_critical("Failed to open the display.");
94         exit(1);
95     }
96     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
97         /* print a message and exit */
98         g_critical("Failed to set display as close-on-exec.");
99         exit(1);
100     }
101           
102     ob_screen = DefaultScreen(ob_display);
103     ob_root = RootWindow(ob_display, ob_screen);
104
105     /* XXX fork self onto other screens */
106      
107     XSynchronize(ob_display, ob_sync);
108
109     /* check for locale support */
110     if (!XSupportsLocale())
111         g_warning("X server does not support locale.");
112     if (!XSetLocaleModifiers(""))
113         g_warning("Cannot set locale modifiers for the X server.");
114
115     /* set our error handler */
116     XSetErrorHandler(xerror_handler);
117
118     /* set the DISPLAY environment variable for any lauched children, to the
119        display we're using, so they open in the right place. */
120     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
121
122     ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
123     ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
124     ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
125
126     prop_startup(); /* get atoms values for the display */
127     extensions_query_all(); /* find which extensions are present */
128      
129     if (screen_annex()) { /* it will be ours! */
130         timer_startup();
131         render_startup();
132         font_startup();
133         themerc_startup();
134         engine_startup(themerc_engine);
135         event_startup();
136         screen_startup();
137         focus_startup();
138         client_startup();
139
140         dispatch_ob(Event_Ob_Startup);
141
142         /* get all the existing windows */
143         client_manage_all();
144
145         ob_state = State_Running;
146         while (!ob_shutdown) {
147             event_loop();
148         }
149         ob_state = State_Exiting;
150
151         client_unmanage_all();
152
153         dispatch_ob(Event_Ob_Shutdown);
154
155         client_shutdown();
156         screen_shutdown();
157         event_shutdown();
158         engine_shutdown();
159         themerc_shutdown();
160         render_shutdown();
161         timer_shutdown();
162     }
163           
164     XCloseDisplay(ob_display);
165
166     dispatch_shutdown();
167
168     /* XXX if (ob_restart) */
169      
170     return 0;
171 }
172
173 void signal_handler(const ObEvent *e)
174 {
175     switch (e->data.signal) {
176     case SIGUSR1:
177         g_message("Caught SIGUSR1 signal. Restarting.");
178         ob_shutdown = ob_restart = TRUE;
179         break;
180
181     case SIGCHLD:
182         wait(NULL);
183         break;
184
185     case SIGHUP:
186     case SIGINT:
187     case SIGTERM:
188     case SIGPIPE:
189         g_message("Caught signal %d. Exiting.", e->data.signal);
190         ob_shutdown = TRUE;
191         break;
192
193     case SIGFPE:
194     case SIGSEGV:
195         g_error("Caught signal %d. Aborting and dumping core.",e->data.signal);
196     }
197 }