]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
raise menus above clients.
[chaz/openbox] / openbox / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "menu.h"
4 #include "client.h"
5 #include "dispatch.h"
6 #include "xerror.h"
7 #include "prop.h"
8 #include "screen.h"
9 #include "focus.h"
10 #include "frame.h"
11 #include "extensions.h"
12 #include "parse.h"
13 #include "grab.h"
14 #include "plugin.h"
15 #include "timer.h"
16 #include "group.h"
17 #include "config.h"
18 #include "gettext.h"
19 #include "render/render.h"
20 #include "render/font.h"
21 #include "render/theme.h"
22
23 #ifdef HAVE_FCNTL_H
24 # include <fcntl.h>
25 #endif
26 #ifdef HAVE_SIGNAL_H
27 # include <signal.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32 #ifdef HAVE_SYS_WAIT_H
33 # include <sys/types.h>
34 # include <sys/wait.h>
35 #endif
36 #ifdef HAVE_LOCALE_H
37 # include <locale.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 # include <sys/types.h>
45 #endif
46
47 #include <X11/cursorfont.h>
48
49 Display *ob_display = NULL;
50 int ob_screen;
51 Window ob_root;
52 State ob_state;
53 gboolean ob_shutdown = FALSE;
54 gboolean ob_restart = FALSE;
55 char *ob_restart_path = NULL;
56 gboolean ob_remote = TRUE;
57 gboolean ob_sync = FALSE;
58 Cursors ob_cursors;
59 char *ob_rc_path = NULL;
60
61 void signal_handler(const ObEvent *e, void *data);
62 void parse_args(int argc, char **argv);
63
64 int main(int argc, char **argv)
65 {
66 struct sigaction action;
67 sigset_t sigset;
68 char *path;
69 char *theme;
70
71 ob_state = State_Starting;
72
73 /* initialize the locale */
74 if (!setlocale(LC_ALL, ""))
75 g_warning("Couldn't set locale from environment.\n");
76 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
77 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
78 textdomain(PACKAGE_NAME);
79
80 /* start our event dispatcher and register for signals */
81 dispatch_startup();
82 dispatch_register(Event_Signal, signal_handler, NULL);
83
84 /* set up signal handler */
85 sigemptyset(&sigset);
86 action.sa_handler = dispatch_signal;
87 action.sa_mask = sigset;
88 action.sa_flags = SA_NOCLDSTOP;
89 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
90 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
91 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
92 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
93 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
94 sigaction(SIGINT, &action, (struct sigaction *) NULL);
95 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
96 sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
97
98 /* anything that died while we were restarting won't give us a SIGCHLD */
99 while (waitpid(-1, NULL, WNOHANG) > 0);
100
101 /* create the ~/.openbox dir */
102 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
103 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
104 S_IROTH | S_IWOTH | S_IXOTH));
105 g_free(path);
106 /* create the ~/.openbox/themes dir */
107 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
108 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
109 S_IROTH | S_IWOTH | S_IXOTH));
110 g_free(path);
111
112 /* parse out command line args */
113 parse_args(argc, argv);
114
115 ob_display = XOpenDisplay(NULL);
116 if (ob_display == NULL) {
117 /* print a message and exit */
118 g_critical("Failed to open the display.");
119 exit(1);
120 }
121 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
122 /* print a message and exit */
123 g_critical("Failed to set display as close-on-exec.");
124 exit(1);
125 }
126
127 ob_screen = DefaultScreen(ob_display);
128 ob_root = RootWindow(ob_display, ob_screen);
129
130 /* XXX fork self onto other screens */
131
132 XSynchronize(ob_display, ob_sync);
133
134 /* check for locale support */
135 if (!XSupportsLocale())
136 g_warning("X server does not support locale.");
137 if (!XSetLocaleModifiers(""))
138 g_warning("Cannot set locale modifiers for the X server.");
139
140 /* set our error handler */
141 XSetErrorHandler(xerror_handler);
142
143 /* set the DISPLAY environment variable for any lauched children, to the
144 display we're using, so they open in the right place. */
145 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
146
147 ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
148 ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
149 ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
150
151 prop_startup(); /* get atoms values for the display */
152 extensions_query_all(); /* find which extensions are present */
153
154 if (screen_annex()) { /* it will be ours! */
155 /* startup the parsing so everything can register sections of the rc */
156 parse_startup();
157
158 /* anything that is going to read data from the rc file needs to be
159 in this group */
160 timer_startup();
161 render_startup();
162 font_startup();
163 theme_startup();
164 event_startup();
165 grab_startup();
166 plugin_startup();
167 /* load the plugins specified in the pluginrc */
168 plugin_loadall();
169
170 /* set up the kernel config shit */
171 config_startup();
172 /* parse/load user options */
173 parse_rc();
174 /* we're done with parsing now, kill it */
175 parse_shutdown();
176
177 /* load the theme specified in the rc file */
178 theme = theme_load(config_theme);
179 g_free(theme);
180 if (!theme) return 1;
181
182 menu_startup();
183 frame_startup();
184 stacking_startup();
185 focus_startup();
186 screen_startup();
187 group_startup();
188 client_startup();
189
190 /* call startup for all the plugins */
191 plugin_startall();
192
193 /* get all the existing windows */
194 client_manage_all();
195
196 ob_state = State_Running;
197 while (!ob_shutdown)
198 event_loop();
199 ob_state = State_Exiting;
200
201 client_unmanage_all();
202
203 plugin_shutdown(); /* calls all the plugins' shutdown functions */
204 client_shutdown();
205 group_shutdown();
206 screen_shutdown();
207 focus_shutdown();
208 stacking_shutdown();
209 frame_shutdown();
210 menu_shutdown();
211 grab_shutdown();
212 event_shutdown();
213 theme_shutdown();
214 render_shutdown();
215 timer_shutdown();
216 config_shutdown();
217 }
218
219 dispatch_shutdown();
220
221 XCloseDisplay(ob_display);
222
223 if (ob_restart) {
224 if (ob_restart_path != NULL) {
225 int argcp;
226 char **argvp;
227 GError *err = NULL;
228
229 /* run other shit */
230 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
231 execvp(argvp[0], argvp);
232 g_strfreev(argvp);
233 } else {
234 g_warning("failed to execute '%s': %s", ob_restart_path,
235 err->message);
236 }
237 }
238
239 /* re-run me */
240 execvp(argv[0], argv); /* try how we were run */
241 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
242 }
243
244 return 0;
245 }
246
247 void signal_handler(const ObEvent *e, void *data)
248 {
249 int s;
250
251 s = e->data.s.signal;
252 switch (s) {
253 case SIGUSR1:
254 g_message("Caught SIGUSR1 signal. Restarting.");
255 ob_shutdown = ob_restart = TRUE;
256 break;
257
258 case SIGCHLD:
259 wait(NULL);
260 break;
261
262 case SIGHUP:
263 case SIGINT:
264 case SIGTERM:
265 case SIGPIPE:
266 g_message("Caught signal %d. Exiting.", s);
267 ob_shutdown = TRUE;
268 break;
269
270 case SIGFPE:
271 case SIGSEGV:
272 g_error("Caught signal %d. Aborting and dumping core.", s);
273 }
274 }
275
276 void print_version()
277 {
278 g_print("Openbox %s\n\n", PACKAGE_VERSION);
279 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
280 g_print("This is free software, and you are welcome to redistribute it\n");
281 g_print("under certain conditions. See the file COPYING for details.\n\n");
282 }
283
284 void print_help()
285 {
286 print_version();
287 g_print("Syntax: %s [options]\n\n", BINARY);
288 g_print("Options:\n\n");
289 g_print(" -rc PATH Specify the path to the rc file to use\n");
290 g_print(" -help Display this help and exit\n");
291 g_print(" -version Display the version and exit\n");
292 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
293 " for debugging X routines)\n");
294 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
295 }
296
297 void parse_args(int argc, char **argv)
298 {
299 int i;
300
301 for (i = 1; i < argc; ++i) {
302 if (!strcmp(argv[i], "-version")) {
303 print_version();
304 exit(0);
305 } else if (!strcmp(argv[i], "-help")) {
306 print_help();
307 exit(0);
308 } else if (!strcmp(argv[i], "-sync")) {
309 ob_sync = TRUE;
310 } else if (!strcmp(argv[i], "-rc")) {
311 if (i == argc - 1) /* no args left */
312 g_printerr("-rc requires an argument\n");
313 else
314 ob_rc_path = argv[++i];
315 } else {
316 g_printerr("Invalid option: '%s'\n\n", argv[i]);
317 print_help();
318 exit(1);
319 }
320 }
321 }
This page took 0.048809 seconds and 4 git commands to generate.