]> icculus.org git repositories - dana/openbox.git/blob - c/openbox.c
merge the C branch into HEAD
[dana/openbox.git] / c / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "client.h"
4 #include "xerror.h"
5 #include "prop.h"
6 #include "screen.h"
7 #include "focus.h"
8 #include "extensions.h"
9 #include "python.h"
10 #include "hooks.h"
11 #include "eventdata.h"
12 #include "gettext.h"
13 #include "clientwrap.h"
14 #include "screenwrap.h"
15 #include "kbind.h"
16 #include "mbind.h"
17 #include "frame.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 gboolean ob_remote   = FALSE;
48 gboolean ob_sync     = TRUE;
49 Cursors  ob_cursors;
50
51 void signal_handler(int signal);
52
53 int main(int argc, char **argv)
54 {
55     struct sigaction action;
56     sigset_t sigset;
57
58     ob_state = State_Starting;
59
60     /* initialize the locale */
61     if (!setlocale(LC_ALL, ""))
62         g_warning("Couldn't set locale from environment.\n");
63     bindtextdomain(PACKAGE, LOCALEDIR);
64     bind_textdomain_codeset(PACKAGE, "UTF-8");
65     textdomain(PACKAGE);
66
67     /* set up signal handler */
68     sigemptyset(&sigset);
69     action.sa_handler = signal_handler;
70     action.sa_mask = sigset;
71     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
72     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
73     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
74     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
75     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
76     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
77     sigaction(SIGINT, &action, (struct sigaction *) NULL);
78     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
79     sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
80
81     /* anything that died while we were restarting won't give us a SIGCHLD */
82     while (waitpid(-1, NULL, WNOHANG) > 0);
83      
84     /* XXX parse out command line args */
85     (void)argc;(void)argv;
86
87     /* critical warnings will exit the program */
88     g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
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     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
95         /* print a message and exit */
96         g_critical("Failed to set display as close-on-exec.");
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_init(); /* 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         frame_startup();
127         python_startup();
128         obexport_startup();
129         hooks_startup();
130         screenwrap_startup();
131         clientwrap_startup();
132         eventdata_startup();
133         event_startup();
134         screen_startup();
135         focus_startup();
136         client_startup();
137         kbind_startup();
138         mbind_startup();
139           
140         /* load the user's settings */
141         if (!python_import("rc"))
142             g_warning("ERROR LOADING RC FILE");
143
144         LOGICALHOOK(Startup, g_quark_try_string("none"), NULL);
145
146         /* get all the existing windows */
147         client_manage_all();
148
149         ob_state = State_Running;
150         while (!ob_shutdown) {
151             event_loop();
152         }
153         ob_state = State_Exiting;
154
155         client_unmanage_all();
156
157         LOGICALHOOK(Shutdown, g_quark_try_string("none"), NULL);
158
159         mbind_shutdown();
160         kbind_shutdown();
161         client_shutdown();
162         screen_shutdown();
163         event_shutdown();
164         eventdata_shutdown();
165         clientwrap_shutdown();
166         screenwrap_shutdown();
167         hooks_shutdown();
168         obexport_shutdown();
169         python_shutdown();
170     }
171           
172     XCloseDisplay(ob_display);
173
174     /* XXX if (ob_restart) */
175      
176     return 0;
177 }
178
179 void signal_handler(int signal)
180 {
181     switch (signal) {
182     case SIGUSR1:
183         g_message("Caught SIGUSR1 signal. Restarting.");
184         ob_shutdown = ob_restart = TRUE;
185         break;
186
187     case SIGCHLD:
188         wait(NULL);
189         break;
190
191     case SIGHUP:
192     case SIGINT:
193     case SIGTERM:
194     case SIGPIPE:
195         g_message("Caught signal %d. Exiting.", signal);
196         ob_shutdown = TRUE;
197         break;
198
199     case SIGFPE:
200     case SIGSEGV:
201         g_error("Caught signal %d. Aborting and dumping core.", signal);
202     }
203 }