]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
59ba762779005965f845304bb467822eb4e85937
[chaz/openbox] / openbox / openbox.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "session.h"
4 #include "dock.h"
5 #include "event.h"
6 #include "menu.h"
7 #include "client.h"
8 #include "xerror.h"
9 #include "prop.h"
10 #include "startup.h"
11 #include "screen.h"
12 #include "focus.h"
13 #include "moveresize.h"
14 #include "frame.h"
15 #include "keyboard.h"
16 #include "mouse.h"
17 #include "extensions.h"
18 #include "grab.h"
19 #include "plugin.h"
20 #include "timer.h"
21 #include "group.h"
22 #include "config.h"
23 #include "gettext.h"
24 #include "parser/parse.h"
25 #include "render/render.h"
26 #include "render/theme.h"
27
28 #ifdef HAVE_FCNTL_H
29 # include <fcntl.h>
30 #endif
31 #ifdef HAVE_SIGNAL_H
32 #define __USE_UNIX98
33 # include <signal.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38 #ifdef HAVE_LOCALE_H
39 # include <locale.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 # include <sys/types.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49 #include <X11/cursorfont.h>
50
51 RrInstance *ob_rr_inst;
52 RrTheme *ob_rr_theme;
53 Display *ob_display;
54 gint ob_screen;
55 gboolean ob_sm_use = TRUE;
56 gchar *ob_sm_id;
57 gboolean ob_replace_wm;
58
59 static ObState state;
60 static gboolean xsync;
61 static gboolean shutdown;
62 static gboolean restart;
63 static char *restart_path;
64 static Cursor cursors[OB_NUM_CURSORS];
65 static KeyCode keys[OB_NUM_KEYS];
66 static gchar *sm_save_file;
67
68 static void signal_handler(int signal);
69 static void parse_args(int argc, char **argv);
70
71 int main(int argc, char **argv)
72 {
73 struct sigaction action;
74 sigset_t sigset;
75 char *path;
76 xmlDocPtr doc;
77 xmlNodePtr node;
78
79 #ifdef DEBUG
80 ob_debug_show_output(TRUE);
81 #endif
82
83 state = OB_STATE_STARTING;
84
85 /* initialize the locale */
86 if (!setlocale(LC_ALL, ""))
87 g_warning("Couldn't set locale from environment.\n");
88 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
89 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
90 textdomain(PACKAGE_NAME);
91
92 /* set up signal handler */
93 sigemptyset(&sigset);
94 action.sa_handler = signal_handler;
95 action.sa_mask = sigset;
96 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
97 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
98 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
99 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
100 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
101 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
102 sigaction(SIGINT, &action, (struct sigaction *) NULL);
103 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
104
105 /* create the ~/.openbox dir */
106 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
107 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
108 S_IROTH | S_IWOTH | S_IXOTH));
109 g_free(path);
110 /* create the ~/.openbox/themes dir */
111 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
112 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
113 S_IROTH | S_IWOTH | S_IXOTH));
114 g_free(path);
115 /* create the ~/.openbox/sessions dir */
116 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
117 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
118 S_IROTH | S_IWOTH | S_IXOTH));
119 g_free(path);
120
121 g_set_prgname(argv[0]);
122
123 /* parse out command line args */
124 parse_args(argc, argv);
125
126 ob_display = XOpenDisplay(NULL);
127 if (ob_display == NULL)
128 ob_exit_with_error("Failed to open the display.");
129 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
130 ob_exit_with_error("Failed to set display as close-on-exec.");
131
132 if (sm_save_file)
133 session_load(sm_save_file);
134 session_startup(argc, argv);
135
136 #ifdef USE_LIBSN
137 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
138 #endif
139
140 ob_screen = DefaultScreen(ob_display);
141
142 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
143 if (ob_rr_inst == NULL)
144 ob_exit_with_error("Failed to initialize the render library.");
145
146 /* XXX fork self onto other screens */
147
148 XSynchronize(ob_display, xsync);
149
150 /* check for locale support */
151 if (!XSupportsLocale())
152 g_warning("X server does not support locale.");
153 if (!XSetLocaleModifiers(""))
154 g_warning("Cannot set locale modifiers for the X server.");
155
156 /* set our error handler */
157 XSetErrorHandler(xerror_handler);
158
159 /* set the DISPLAY environment variable for any lauched children, to the
160 display we're using, so they open in the right place. */
161 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
162
163 /* create available cursors */
164 cursors[OB_CURSOR_POINTER] =
165 XCreateFontCursor(ob_display, XC_left_ptr);
166 cursors[OB_CURSOR_BUSY] =
167 XCreateFontCursor(ob_display, XC_watch);
168 cursors[OB_CURSOR_MOVE] =
169 XCreateFontCursor(ob_display, XC_fleur);
170 cursors[OB_CURSOR_NORTH] =
171 XCreateFontCursor(ob_display, XC_top_side);
172 cursors[OB_CURSOR_NORTHEAST] =
173 XCreateFontCursor(ob_display, XC_top_right_corner);
174 cursors[OB_CURSOR_EAST] =
175 XCreateFontCursor(ob_display, XC_right_side);
176 cursors[OB_CURSOR_SOUTHEAST] =
177 XCreateFontCursor(ob_display, XC_bottom_right_corner);
178 cursors[OB_CURSOR_SOUTH] =
179 XCreateFontCursor(ob_display, XC_bottom_side);
180 cursors[OB_CURSOR_SOUTHWEST] =
181 XCreateFontCursor(ob_display, XC_bottom_left_corner);
182 cursors[OB_CURSOR_WEST] =
183 XCreateFontCursor(ob_display, XC_left_side);
184 cursors[OB_CURSOR_NORTHWEST] =
185 XCreateFontCursor(ob_display, XC_top_left_corner);
186
187 /* create available keycodes */
188 keys[OB_KEY_RETURN] =
189 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
190 keys[OB_KEY_ESCAPE] =
191 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
192 keys[OB_KEY_LEFT] =
193 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
194 keys[OB_KEY_RIGHT] =
195 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
196 keys[OB_KEY_UP] =
197 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
198 keys[OB_KEY_DOWN] =
199 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
200
201 prop_startup(); /* get atoms values for the display */
202 extensions_query_all(); /* find which extensions are present */
203
204 /* save stuff that we can use to restore state */
205 startup_save();
206
207 if (screen_annex()) { /* it will be ours! */
208 ObParseInst *i;
209
210 /* startup the parsing so everything can register sections of the rc */
211 i = parse_startup();
212
213 /* anything that is going to read data from the rc file needs to be
214 in this group */
215 timer_startup();
216 event_startup();
217 grab_startup();
218 /* focus_backup is used for stacking, so this needs to come before
219 anything that calls stacking_add */
220 focus_startup();
221 window_startup();
222 plugin_startup();
223 /* load the plugins specified in the pluginrc */
224 plugin_loadall(i);
225
226 /* set up the kernel config shit */
227 config_startup(i);
228 menu_startup(i);
229 /* parse/load user options */
230 if (parse_load_rc(&doc, &node))
231 parse_tree(i, doc, node->xmlChildrenNode);
232 /* we're done with parsing now, kill it */
233 xmlFreeDoc(doc);
234 parse_shutdown(i);
235
236 menu_parse();
237
238 /* load the theme specified in the rc file */
239 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
240 if (ob_rr_theme == NULL)
241 ob_exit_with_error("Unable to load a theme.");
242
243 moveresize_startup();
244 screen_startup();
245 group_startup();
246 client_startup();
247 dock_startup();
248 keyboard_startup();
249 mouse_startup();
250
251 /* call startup for all the plugins */
252 plugin_startall();
253
254 /* get all the existing windows */
255 client_manage_all();
256
257 state = OB_STATE_RUNNING;
258 while (!shutdown)
259 event_loop();
260 state = OB_STATE_EXITING;
261
262 dock_remove_all();
263 client_unmanage_all();
264
265 plugin_shutdown(); /* calls all the plugins' shutdown functions */
266 menu_shutdown();
267 mouse_shutdown();
268 keyboard_shutdown();
269 dock_shutdown();
270 client_shutdown();
271 group_shutdown();
272 screen_shutdown();
273 focus_shutdown();
274 moveresize_shutdown();
275 window_shutdown();
276 grab_shutdown();
277 event_shutdown();
278 timer_shutdown();
279 config_shutdown();
280 }
281
282 RrThemeFree(ob_rr_theme);
283 RrInstanceFree(ob_rr_inst);
284
285 session_shutdown();
286 g_free(ob_sm_id);
287
288 #ifdef USE_LIBSN
289 sn_display_unref(ob_sn_display);
290 #endif
291
292 XCloseDisplay(ob_display);
293
294 if (restart) {
295 if (restart_path != NULL) {
296 int argcp;
297 char **argvp;
298 GError *err = NULL;
299
300 /* run other shit */
301 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
302 execvp(argvp[0], argvp);
303 g_strfreev(argvp);
304 } else {
305 g_warning("failed to execute '%s': %s", restart_path,
306 err->message);
307 }
308 }
309
310 /* re-run me */
311 execvp(argv[0], argv); /* try how we were run */
312 }
313
314 return 0;
315 }
316
317 static void signal_handler(int sig)
318 {
319 switch (sig) {
320 case SIGUSR1:
321 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
322 ob_restart();
323 break;
324
325 case SIGHUP:
326 case SIGINT:
327 case SIGTERM:
328 case SIGPIPE:
329 fprintf(stderr, "Caught signal %d. Exiting.", sig);
330 ob_exit();
331 break;
332
333 case SIGFPE:
334 case SIGSEGV:
335 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", sig);
336 abort();
337 }
338 }
339
340 static void print_version()
341 {
342 g_print("Openbox %s\n\n", PACKAGE_VERSION);
343 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
344 g_print("This is free software, and you are welcome to redistribute it\n");
345 g_print("under certain conditions. See the file COPYING for details.\n\n");
346 }
347
348 static void print_help()
349 {
350 print_version();
351 g_print("Syntax: openbox [options]\n\n");
352 g_print("Options:\n\n");
353 #ifdef USE_SM
354 g_print(" --sm-disable Disable connection to session manager\n");
355 g_print(" --sm-client-id ID Specify session management ID\n");
356 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
357 " from\n");
358 #endif
359 g_print(" --replace Replace the currently running window "
360 "manager\n");
361 g_print(" --help Display this help and exit\n");
362 g_print(" --version Display the version and exit\n");
363 g_print(" --sync Run in synchronous mode (this is slow and\n"
364 " meant for debugging X routines)\n");
365 g_print(" --debug Display debugging output\n");
366 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
367 }
368
369 static void parse_args(int argc, char **argv)
370 {
371 int i;
372
373 for (i = 1; i < argc; ++i) {
374 if (!strcmp(argv[i], "--version")) {
375 print_version();
376 exit(0);
377 } else if (!strcmp(argv[i], "--help")) {
378 print_help();
379 exit(0);
380 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
381 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
382 } else if (!strcmp(argv[i], "--replace")) {
383 ob_replace_wm = TRUE;
384 } else if (!strcmp(argv[i], "--sync")) {
385 xsync = TRUE;
386 } else if (!strcmp(argv[i], "--debug")) {
387 ob_debug_show_output(TRUE);
388 #ifdef USE_SM
389 } else if (!strcmp(argv[i], "--sm-client-id")) {
390 if (i == argc - 1) /* no args left */
391 g_printerr(_("--sm-client-id requires an argument\n"));
392 else
393 ob_sm_id = g_strdup(argv[++i]);
394 } else if (!strcmp(argv[i], "--sm-save-file")) {
395 if (i == argc - 1) /* no args left */
396 g_printerr(_("--sm-save-file requires an argument\n"));
397 else
398 sm_save_file = argv[++i];
399 } else if (!strcmp(argv[i], "--sm-disable")) {
400 ob_sm_use = FALSE;
401 #endif
402 } else {
403 g_printerr("Invalid option: '%s'\n\n", argv[i]);
404 print_help();
405 exit(1);
406 }
407 }
408 }
409
410 void ob_exit_with_error(gchar *msg)
411 {
412 g_critical(msg);
413 session_shutdown();
414 exit(EXIT_FAILURE);
415 }
416
417 void ob_restart_other(const gchar *path)
418 {
419 restart_path = g_strdup(path);
420 ob_restart();
421 }
422
423 void ob_restart()
424 {
425 restart = TRUE;
426 ob_exit();
427 }
428
429 void ob_exit()
430 {
431 shutdown = TRUE;
432 }
433
434 Cursor ob_cursor(ObCursor cursor)
435 {
436 g_assert(cursor < OB_NUM_CURSORS);
437 return cursors[cursor];
438 }
439
440 KeyCode ob_keycode(ObKey key)
441 {
442 g_assert(key < OB_NUM_KEYS);
443 return keys[key];
444 }
445
446 ObState ob_state()
447 {
448 return state;
449 }
This page took 0.052603 seconds and 4 git commands to generate.