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