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