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