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