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