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