]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
let the configuration file changes affect windows' decroations and functionality...
[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 reconfigure_and_exit = FALSE;
90
91 static void signal_handler(gint signal, gpointer data);
92 static void parse_args(gint argc, gchar **argv);
93
94 gint main(gint argc, gchar **argv)
95 {
96 #ifdef DEBUG
97 ob_debug_show_output(TRUE);
98 #endif
99
100 state = OB_STATE_STARTING;
101
102 /* initialize the locale */
103 if (!setlocale(LC_ALL, ""))
104 g_warning("Couldn't set locale from environment.\n");
105 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
106 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
107 textdomain(PACKAGE_NAME);
108
109 g_set_prgname(argv[0]);
110
111 if (chdir(g_get_home_dir()) == -1)
112 g_warning("Unable to change to home directory (%s): %s",
113 g_get_home_dir(), g_strerror(errno));
114
115 /* parse out command line args */
116 parse_args(argc, argv);
117
118 if (!reconfigure_and_exit) {
119 parse_paths_startup();
120
121 session_startup(argc, argv);
122 }
123
124 ob_display = XOpenDisplay(NULL);
125 if (ob_display == NULL)
126 ob_exit_with_error("Failed to open the display.");
127 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
128 ob_exit_with_error("Failed to set display as close-on-exec.");
129
130 if (reconfigure_and_exit) {
131 guint32 pid;
132 gboolean ret;
133
134 prop_startup(); /* get atoms values for the display */
135 ret = PROP_GET32(RootWindow(ob_display, DefaultScreen(ob_display)),
136 openbox_pid, cardinal, &pid);
137 XCloseDisplay(ob_display);
138 if (!ret) {
139 g_print("Openbox does not appear to be running on this "
140 "display.\n");
141 } else {
142 g_print("Telling the Openbox process # %u to reconfigure.\n", pid);
143 ret = (kill(pid, SIGUSR2) == 0);
144 if (!ret)
145 g_print("Error: %s.\n", strerror(errno));
146 }
147 exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
148 }
149
150 ob_main_loop = ob_main_loop_new(ob_display);
151
152 /* set up signal handler */
153 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
154 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
155 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
156 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
157 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
158 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
159 ob_main_loop_signal_add(ob_main_loop, SIGCHLD, signal_handler, NULL, NULL);
160
161 ob_screen = DefaultScreen(ob_display);
162
163 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
164 if (ob_rr_inst == NULL)
165 ob_exit_with_error("Failed to initialize the render library.");
166
167 XSynchronize(ob_display, xsync);
168
169 /* check for locale support */
170 if (!XSupportsLocale())
171 g_warning("X server does not support locale.");
172 if (!XSetLocaleModifiers(""))
173 g_warning("Cannot set locale modifiers for the X server.");
174
175 /* set our error handler */
176 XSetErrorHandler(xerror_handler);
177
178 /* set the DISPLAY environment variable for any lauched children, to the
179 display we're using, so they open in the right place. */
180 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
181
182 /* create available cursors */
183 cursors[OB_CURSOR_NONE] = None;
184 cursors[OB_CURSOR_POINTER] =
185 XCreateFontCursor(ob_display, XC_left_ptr);
186 cursors[OB_CURSOR_BUSY] =
187 XCreateFontCursor(ob_display, XC_watch);
188 cursors[OB_CURSOR_MOVE] =
189 XCreateFontCursor(ob_display, XC_fleur);
190 cursors[OB_CURSOR_NORTH] =
191 XCreateFontCursor(ob_display, XC_top_side);
192 cursors[OB_CURSOR_NORTHEAST] =
193 XCreateFontCursor(ob_display, XC_top_right_corner);
194 cursors[OB_CURSOR_EAST] =
195 XCreateFontCursor(ob_display, XC_right_side);
196 cursors[OB_CURSOR_SOUTHEAST] =
197 XCreateFontCursor(ob_display, XC_bottom_right_corner);
198 cursors[OB_CURSOR_SOUTH] =
199 XCreateFontCursor(ob_display, XC_bottom_side);
200 cursors[OB_CURSOR_SOUTHWEST] =
201 XCreateFontCursor(ob_display, XC_bottom_left_corner);
202 cursors[OB_CURSOR_WEST] =
203 XCreateFontCursor(ob_display, XC_left_side);
204 cursors[OB_CURSOR_NORTHWEST] =
205 XCreateFontCursor(ob_display, XC_top_left_corner);
206
207 /* create available keycodes */
208 keys[OB_KEY_RETURN] =
209 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
210 keys[OB_KEY_ESCAPE] =
211 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
212 keys[OB_KEY_LEFT] =
213 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
214 keys[OB_KEY_RIGHT] =
215 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
216 keys[OB_KEY_UP] =
217 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
218 keys[OB_KEY_DOWN] =
219 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
220
221 prop_startup(); /* get atoms values for the display */
222 extensions_query_all(); /* find which extensions are present */
223
224 if (screen_annex()) { /* it will be ours! */
225 do {
226 {
227 ObParseInst *i;
228 xmlDocPtr doc;
229 xmlNodePtr node;
230
231 /* startup the parsing so everything can register sections
232 of the rc */
233 i = parse_startup();
234
235 config_startup(i);
236 /* parse/load user options */
237 if (parse_load_rc(&doc, &node))
238 parse_tree(i, doc, node->xmlChildrenNode);
239 /* we're done with parsing now, kill it */
240 parse_close(doc);
241 parse_shutdown(i);
242 }
243
244 /* load the theme specified in the rc file */
245 {
246 RrTheme *theme;
247 if ((theme = RrThemeNew(ob_rr_inst, config_theme))) {
248 RrThemeFree(ob_rr_theme);
249 ob_rr_theme = theme;
250 }
251 if (ob_rr_theme == NULL)
252 ob_exit_with_error("Unable to load a theme.");
253 }
254
255 if (reconfigure) {
256 GList *it;
257
258 /* update all existing windows for the new theme */
259 for (it = client_list; it; it = g_list_next(it)) {
260 ObClient *c = it->data;
261 frame_adjust_theme(c->frame);
262 }
263 }
264 event_startup(reconfigure);
265 /* focus_backup is used for stacking, so this needs to come before
266 anything that calls stacking_add */
267 focus_startup(reconfigure);
268 window_startup(reconfigure);
269 sn_startup(reconfigure);
270 screen_startup(reconfigure);
271 grab_startup(reconfigure);
272 group_startup(reconfigure);
273 client_startup(reconfigure);
274 dock_startup(reconfigure);
275 moveresize_startup(reconfigure);
276 keyboard_startup(reconfigure);
277 mouse_startup(reconfigure);
278 menu_startup(reconfigure);
279
280 if (!reconfigure) {
281 /* get all the existing windows */
282 client_manage_all();
283 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
284 } else {
285 GList *it;
286
287 /* redecorate all existing windows */
288 for (it = client_list; it; it = g_list_next(it)) {
289 ObClient *c = it->data;
290 /* the new config can change the window's decorations */
291 client_setup_decor_and_functions(c);
292 /* redraw the frames */
293 frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
294 }
295 }
296
297 reconfigure = FALSE;
298
299 state = OB_STATE_RUNNING;
300 ob_main_loop_run(ob_main_loop);
301 state = OB_STATE_EXITING;
302
303 if (!reconfigure) {
304 dock_remove_all();
305 client_unmanage_all();
306 }
307
308 menu_shutdown(reconfigure);
309 mouse_shutdown(reconfigure);
310 keyboard_shutdown(reconfigure);
311 moveresize_shutdown(reconfigure);
312 dock_shutdown(reconfigure);
313 client_shutdown(reconfigure);
314 group_shutdown(reconfigure);
315 grab_shutdown(reconfigure);
316 screen_shutdown(reconfigure);
317 focus_shutdown(reconfigure);
318 sn_shutdown(reconfigure);
319 window_shutdown(reconfigure);
320 event_shutdown(reconfigure);
321 config_shutdown();
322 } while (reconfigure);
323 }
324
325 XSync(ob_display, FALSE);
326
327 RrThemeFree(ob_rr_theme);
328 RrInstanceFree(ob_rr_inst);
329
330 session_shutdown();
331
332 XCloseDisplay(ob_display);
333
334 parse_paths_shutdown();
335
336 if (restart) {
337 if (restart_path != NULL) {
338 gint argcp;
339 gchar **argvp;
340 GError *err = NULL;
341
342 /* run other window manager */
343 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
344 execvp(argvp[0], argvp);
345 g_strfreev(argvp);
346 } else {
347 g_warning("failed to execute '%s': %s", restart_path,
348 err->message);
349 g_error_free(err);
350 }
351 }
352
353 /* re-run me */
354 execvp(argv[0], argv); /* try how we were run */
355 execlp(argv[0], g_path_get_basename(argv[0]),
356 (char *)NULL); /* last resort */
357 }
358
359 return exitcode;
360 }
361
362 static void signal_handler(gint signal, gpointer data)
363 {
364 switch (signal) {
365 case SIGUSR1:
366 ob_debug("Caught signal %d. Restarting.\n", signal);
367 ob_restart();
368 break;
369 case SIGUSR2:
370 ob_debug("Caught signal %d. Reconfiguring.\n", signal);
371 ob_reconfigure();
372 break;
373 case SIGCHLD:
374 /* reap children */
375 while (waitpid(-1, NULL, WNOHANG) > 0);
376 break;
377 default:
378 ob_debug("Caught signal %d. Exiting.\n", signal);
379 /* TERM and INT return a 0 code */
380 ob_exit(!(signal == SIGTERM || signal == SIGINT));
381 }
382 }
383
384 static void print_version()
385 {
386 g_print("Openbox %s\n", PACKAGE_VERSION);
387 g_print("Copyright (c) 2006 Mikael Magnusson\n");
388 g_print("Copyright (c) 2003 Ben Jansens\n\n");
389 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
390 g_print("This is free software, and you are welcome to redistribute it\n");
391 g_print("under certain conditions. See the file COPYING for details.\n\n");
392 }
393
394 static void print_help()
395 {
396 g_print("Syntax: openbox [options]\n\n");
397 g_print("Options:\n\n");
398 g_print(" --reconfigure Tell the currently running instance of "
399 "Openbox to\n"
400 " reconfigure (and then exit immediately)\n");
401 #ifdef USE_SM
402 g_print(" --sm-disable Disable connection to session manager\n");
403 g_print(" --sm-client-id ID Specify session management ID\n");
404 g_print(" --sm-save-file FILE Specify file to load a saved session"
405 "from\n");
406 #endif
407 g_print(" --replace Replace the currently running window "
408 "manager\n");
409 g_print(" --help Display this help and exit\n");
410 g_print(" --version Display the version and exit\n");
411 g_print(" --sync Run in synchronous mode (this is slow and "
412 "meant for\n"
413 " debugging X routines)\n");
414 g_print(" --debug Display debugging output\n");
415 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT);
416 }
417
418 static void parse_args(gint argc, gchar **argv)
419 {
420 gint i;
421
422 for (i = 1; i < argc; ++i) {
423 if (!strcmp(argv[i], "--version")) {
424 print_version();
425 exit(0);
426 } else if (!strcmp(argv[i], "--help")) {
427 print_help();
428 exit(0);
429 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
430 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
431 } else if (!strcmp(argv[i], "--replace")) {
432 ob_replace_wm = TRUE;
433 } else if (!strcmp(argv[i], "--sync")) {
434 xsync = TRUE;
435 } else if (!strcmp(argv[i], "--debug")) {
436 ob_debug_show_output(TRUE);
437 } else if (!strcmp(argv[i], "--reconfigure")) {
438 reconfigure_and_exit = TRUE;
439 }
440 }
441 }
442
443 void ob_exit_with_error(gchar *msg)
444 {
445 g_critical(msg);
446 session_shutdown();
447 exit(EXIT_FAILURE);
448 }
449
450 void ob_restart_other(const gchar *path)
451 {
452 restart_path = g_strdup(path);
453 ob_restart();
454 }
455
456 void ob_restart()
457 {
458 restart = TRUE;
459 ob_exit(0);
460 }
461
462 void ob_reconfigure()
463 {
464 reconfigure = TRUE;
465 ob_exit(0);
466 }
467
468 void ob_exit(gint code)
469 {
470 exitcode = code;
471 ob_main_loop_exit(ob_main_loop);
472 }
473
474 Cursor ob_cursor(ObCursor cursor)
475 {
476 g_assert(cursor < OB_NUM_CURSORS);
477 return cursors[cursor];
478 }
479
480 KeyCode ob_keycode(ObKey key)
481 {
482 g_assert(key < OB_NUM_KEYS);
483 return keys[key];
484 }
485
486 ObState ob_state()
487 {
488 return state;
489 }
This page took 0.065243 seconds and 5 git commands to generate.