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