]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
2a8f422fc0ac202a0dae4fe186bbe3e6e3735fa5
[chaz/openbox] / openbox / openbox.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 openbox.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "openbox.h"
22 #include "session.h"
23 #include "dock.h"
24 #include "event.h"
25 #include "menu.h"
26 #include "client.h"
27 #include "xerror.h"
28 #include "prop.h"
29 #include "screen.h"
30 #include "startupnotify.h"
31 #include "focus.h"
32 #include "moveresize.h"
33 #include "frame.h"
34 #include "keyboard.h"
35 #include "mouse.h"
36 #include "extensions.h"
37 #include "menuframe.h"
38 #include "grab.h"
39 #include "group.h"
40 #include "config.h"
41 #include "mainloop.h"
42 #include "gettext.h"
43 #include "parser/parse.h"
44 #include "render/render.h"
45 #include "render/theme.h"
46
47 #ifdef HAVE_FCNTL_H
48 # include <fcntl.h>
49 #endif
50 #ifdef HAVE_SIGNAL_H
51 # include <signal.h>
52 #endif
53 #ifdef HAVE_STDLIB_H
54 # include <stdlib.h>
55 #endif
56 #ifdef HAVE_LOCALE_H
57 # include <locale.h>
58 #endif
59 #ifdef HAVE_SYS_STAT_H
60 # include <sys/stat.h>
61 # include <sys/types.h>
62 #endif
63 #ifdef HAVE_SYS_WAIT_H
64 # include <sys/types.h>
65 # include <sys/wait.h>
66 #endif
67 #ifdef HAVE_UNISTD_H
68 # include <unistd.h>
69 #endif
70 #include <errno.h>
71
72 #include <X11/cursorfont.h>
73
74 RrInstance *ob_rr_inst;
75 RrTheme *ob_rr_theme;
76 ObMainLoop *ob_main_loop;
77 Display *ob_display;
78 gint ob_screen;
79 gboolean ob_replace_wm = FALSE;
80
81 static ObState state;
82 static gboolean xsync = FALSE;
83 static gboolean reconfigure = FALSE;
84 static gboolean restart = FALSE;
85 static gchar *restart_path = NULL;
86 static Cursor cursors[OB_NUM_CURSORS];
87 static KeyCode keys[OB_NUM_KEYS];
88 static gint exitcode = 0;
89 static gboolean message_and_exit = FALSE;
90 static guint message = 0;
91 static gboolean being_replaced = FALSE;
92
93 static void signal_handler(gint signal, gpointer data);
94 static void parse_args(gint argc, gchar **argv);
95
96 gint main(gint argc, gchar **argv)
97 {
98 #ifdef DEBUG
99 ob_debug_show_output(TRUE);
100 #endif
101
102 state = OB_STATE_STARTING;
103
104 /* initialize the locale */
105 if (!setlocale(LC_ALL, ""))
106 g_warning("Couldn't set locale from environment.\n");
107 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
108 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
109 textdomain(PACKAGE_NAME);
110
111 g_set_prgname(argv[0]);
112
113 if (chdir(g_get_home_dir()) == -1)
114 g_warning("Unable to change to home directory (%s): %s",
115 g_get_home_dir(), g_strerror(errno));
116
117 /* parse out command line args */
118 parse_args(argc, argv);
119
120 if (!message_and_exit) {
121 parse_paths_startup();
122
123 session_startup(argc, argv);
124 }
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 (message_and_exit) {
133 prop_startup();
134
135 /* Send client message telling the OB process to:
136 * message = 1 -> reconfigure
137 * message = 2 -> restart */
138 PROP_MSG(RootWindow(ob_display, ob_screen),
139 ob_control, message, 0, 0, 0);
140 XCloseDisplay(ob_display);
141 exit(0);
142 }
143
144 ob_main_loop = ob_main_loop_new(ob_display);
145
146 /* set up signal handler */
147 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
148 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
149 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
150 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
151 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
152 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
153 ob_main_loop_signal_add(ob_main_loop, SIGCHLD, signal_handler, NULL, NULL);
154
155 ob_screen = DefaultScreen(ob_display);
156
157 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
158 if (ob_rr_inst == NULL)
159 ob_exit_with_error("Failed to initialize the render library.");
160
161 XSynchronize(ob_display, xsync);
162
163 /* check for locale support */
164 if (!XSupportsLocale())
165 g_warning("X server does not support locale.");
166 if (!XSetLocaleModifiers(""))
167 g_warning("Cannot set locale modifiers for the X server.");
168
169 /* set our error handler */
170 XSetErrorHandler(xerror_handler);
171
172 /* set the DISPLAY environment variable for any lauched children, to the
173 display we're using, so they open in the right place. */
174 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
175
176 /* create available cursors */
177 cursors[OB_CURSOR_NONE] = None;
178 cursors[OB_CURSOR_POINTER] =
179 XCreateFontCursor(ob_display, XC_left_ptr);
180 cursors[OB_CURSOR_BUSY] =
181 XCreateFontCursor(ob_display, XC_watch);
182 cursors[OB_CURSOR_MOVE] =
183 XCreateFontCursor(ob_display, XC_fleur);
184 cursors[OB_CURSOR_NORTH] =
185 XCreateFontCursor(ob_display, XC_top_side);
186 cursors[OB_CURSOR_NORTHEAST] =
187 XCreateFontCursor(ob_display, XC_top_right_corner);
188 cursors[OB_CURSOR_EAST] =
189 XCreateFontCursor(ob_display, XC_right_side);
190 cursors[OB_CURSOR_SOUTHEAST] =
191 XCreateFontCursor(ob_display, XC_bottom_right_corner);
192 cursors[OB_CURSOR_SOUTH] =
193 XCreateFontCursor(ob_display, XC_bottom_side);
194 cursors[OB_CURSOR_SOUTHWEST] =
195 XCreateFontCursor(ob_display, XC_bottom_left_corner);
196 cursors[OB_CURSOR_WEST] =
197 XCreateFontCursor(ob_display, XC_left_side);
198 cursors[OB_CURSOR_NORTHWEST] =
199 XCreateFontCursor(ob_display, XC_top_left_corner);
200
201 /* create available keycodes */
202 keys[OB_KEY_RETURN] =
203 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
204 keys[OB_KEY_ESCAPE] =
205 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
206 keys[OB_KEY_LEFT] =
207 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
208 keys[OB_KEY_RIGHT] =
209 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
210 keys[OB_KEY_UP] =
211 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
212 keys[OB_KEY_DOWN] =
213 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
214
215 prop_startup(); /* get atoms values for the display */
216 extensions_query_all(); /* find which extensions are present */
217
218 if (screen_annex()) { /* it will be ours! */
219 do {
220 {
221 ObParseInst *i;
222 xmlDocPtr doc;
223 xmlNodePtr node;
224
225 /* startup the parsing so everything can register sections
226 of the rc */
227 i = parse_startup();
228
229 config_startup(i);
230 /* parse/load user options */
231 if (parse_load_rc(&doc, &node))
232 parse_tree(i, doc, node->xmlChildrenNode);
233 /* we're done with parsing now, kill it */
234 parse_close(doc);
235 parse_shutdown(i);
236 }
237
238 /* load the theme specified in the rc file */
239 {
240 RrTheme *theme;
241 if ((theme = RrThemeNew(ob_rr_inst, config_theme,
242 config_font_activewindow,
243 config_font_inactivewindow,
244 config_font_menutitle,
245 config_font_menuitem)))
246 {
247 RrThemeFree(ob_rr_theme);
248 ob_rr_theme = theme;
249 }
250 if (ob_rr_theme == NULL)
251 ob_exit_with_error("Unable to load a theme.");
252 }
253
254 if (reconfigure) {
255 GList *it;
256
257 /* update all existing windows for the new theme */
258 for (it = client_list; it; it = g_list_next(it)) {
259 ObClient *c = it->data;
260 frame_adjust_theme(c->frame);
261 }
262 }
263 event_startup(reconfigure);
264 /* focus_backup is used for stacking, so this needs to come before
265 anything that calls stacking_add */
266 focus_startup(reconfigure);
267 window_startup(reconfigure);
268 sn_startup(reconfigure);
269 screen_startup(reconfigure);
270 grab_startup(reconfigure);
271 group_startup(reconfigure);
272 client_startup(reconfigure);
273 dock_startup(reconfigure);
274 moveresize_startup(reconfigure);
275 keyboard_startup(reconfigure);
276 mouse_startup(reconfigure);
277 menu_startup(reconfigure);
278
279 if (!reconfigure) {
280 /* get all the existing windows */
281 client_manage_all();
282 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
283 } else {
284 GList *it;
285
286 /* redecorate all existing windows */
287 for (it = client_list; it; it = g_list_next(it)) {
288 ObClient *c = it->data;
289 /* the new config can change the window's decorations */
290 client_setup_decor_and_functions(c);
291 /* redraw the frames */
292 frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
293 }
294 }
295
296 reconfigure = FALSE;
297
298 state = OB_STATE_RUNNING;
299 ob_main_loop_run(ob_main_loop);
300 state = OB_STATE_EXITING;
301
302 if (!reconfigure) {
303 dock_remove_all();
304 client_unmanage_all();
305 }
306
307 menu_shutdown(reconfigure);
308 mouse_shutdown(reconfigure);
309 keyboard_shutdown(reconfigure);
310 moveresize_shutdown(reconfigure);
311 dock_shutdown(reconfigure);
312 client_shutdown(reconfigure);
313 group_shutdown(reconfigure);
314 grab_shutdown(reconfigure);
315 screen_shutdown(reconfigure);
316 focus_shutdown(reconfigure);
317 sn_shutdown(reconfigure);
318 window_shutdown(reconfigure);
319 event_shutdown(reconfigure);
320 config_shutdown();
321 } while (reconfigure);
322 }
323
324 XSync(ob_display, FALSE);
325
326 RrThemeFree(ob_rr_theme);
327 RrInstanceFree(ob_rr_inst);
328
329 session_shutdown(being_replaced);
330
331 XCloseDisplay(ob_display);
332
333 parse_paths_shutdown();
334
335 if (restart) {
336 if (restart_path != NULL) {
337 gint argcp;
338 gchar **argvp;
339 GError *err = NULL;
340
341 /* run other window manager */
342 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
343 execvp(argvp[0], argvp);
344 g_strfreev(argvp);
345 } else {
346 g_warning("failed to execute '%s': %s", restart_path,
347 err->message);
348 g_error_free(err);
349 }
350 }
351
352 /* re-run me */
353 execvp(argv[0], argv); /* try how we were run */
354 execlp(argv[0], g_path_get_basename(argv[0]),
355 (char *)NULL); /* last resort */
356 }
357
358 return exitcode;
359 }
360
361 static void signal_handler(gint signal, gpointer data)
362 {
363 switch (signal) {
364 case SIGUSR1:
365 ob_debug("Caught signal %d. Restarting.\n", signal);
366 ob_restart();
367 break;
368 case SIGUSR2:
369 ob_debug("Caught signal %d. Reconfiguring.\n", signal);
370 ob_reconfigure();
371 break;
372 case SIGCHLD:
373 /* reap children */
374 while (waitpid(-1, NULL, WNOHANG) > 0);
375 break;
376 default:
377 ob_debug("Caught signal %d. Exiting.\n", signal);
378 /* TERM and INT return a 0 code */
379 ob_exit(!(signal == SIGTERM || signal == SIGINT));
380 }
381 }
382
383 static void print_version()
384 {
385 g_print("Openbox %s\n", PACKAGE_VERSION);
386 g_print("Copyright (c) 2006 Mikael Magnusson\n");
387 g_print("Copyright (c) 2003 Ben Jansens\n\n");
388 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
389 g_print("This is free software, and you are welcome to redistribute it\n");
390 g_print("under certain conditions. See the file COPYING for details.\n\n");
391 }
392
393 static void print_help()
394 {
395 g_print("Syntax: openbox [options]\n\n");
396 g_print("Options:\n\n");
397 g_print(" --reconfigure Tell the currently running instance of "
398 "Openbox to\n"
399 " reconfigure (and then exit immediately)\n");
400 #ifdef USE_SM
401 g_print(" --sm-disable Disable connection to session manager\n");
402 g_print(" --sm-client-id ID Specify session management ID\n");
403 g_print(" --sm-save-file FILE Specify file to load a saved session"
404 "from\n");
405 #endif
406 g_print(" --replace Replace the currently running window "
407 "manager\n");
408 g_print(" --help Display this help and exit\n");
409 g_print(" --version Display the version and exit\n");
410 g_print(" --sync Run in synchronous mode (this is slow and "
411 "meant for\n"
412 " debugging X routines)\n");
413 g_print(" --debug Display debugging output\n");
414 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT);
415 }
416
417 static void parse_args(gint argc, gchar **argv)
418 {
419 gint i;
420
421 for (i = 1; i < argc; ++i) {
422 if (!strcmp(argv[i], "--version")) {
423 print_version();
424 exit(0);
425 } else if (!strcmp(argv[i], "--help")) {
426 print_help();
427 exit(0);
428 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
429 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
430 } else if (!strcmp(argv[i], "--replace")) {
431 ob_replace_wm = TRUE;
432 } else if (!strcmp(argv[i], "--sync")) {
433 xsync = TRUE;
434 } else if (!strcmp(argv[i], "--debug")) {
435 ob_debug_show_output(TRUE);
436 } else if (!strcmp(argv[i], "--reconfigure")) {
437 message_and_exit = TRUE;
438 message = 1;
439 } else if (!strcmp(argv[i], "--restart")) {
440 message_and_exit = TRUE;
441 message = 2;
442 }
443 }
444 }
445
446 void ob_exit_with_error(gchar *msg)
447 {
448 g_critical(msg);
449 session_shutdown(TRUE);
450 exit(EXIT_FAILURE);
451 }
452
453 void ob_restart_other(const gchar *path)
454 {
455 restart_path = g_strdup(path);
456 ob_restart();
457 }
458
459 void ob_restart()
460 {
461 restart = TRUE;
462 ob_exit(0);
463 }
464
465 void ob_reconfigure()
466 {
467 reconfigure = TRUE;
468 ob_exit(0);
469 }
470
471 void ob_exit(gint code)
472 {
473 exitcode = code;
474 ob_main_loop_exit(ob_main_loop);
475 }
476
477 void ob_exit_replace()
478 {
479 exitcode = 0;
480 being_replaced = TRUE;
481 ob_main_loop_exit(ob_main_loop);
482 }
483
484 Cursor ob_cursor(ObCursor cursor)
485 {
486 g_assert(cursor < OB_NUM_CURSORS);
487 return cursors[cursor];
488 }
489
490 KeyCode ob_keycode(ObKey key)
491 {
492 g_assert(key < OB_NUM_KEYS);
493 return keys[key];
494 }
495
496 ObState ob_state()
497 {
498 return state;
499 }
This page took 0.06208 seconds and 4 git commands to generate.