some changes to ConfigureRequest, based on what I found in FVWM.
[mikachu/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) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana 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 "modkeys.h"
25 #include "event.h"
26 #include "menu.h"
27 #include "client.h"
28 #include "xerror.h"
29 #include "prop.h"
30 #include "screen.h"
31 #include "startupnotify.h"
32 #include "focus.h"
33 #include "moveresize.h"
34 #include "frame.h"
35 #include "keyboard.h"
36 #include "mouse.h"
37 #include "extensions.h"
38 #include "menuframe.h"
39 #include "grab.h"
40 #include "group.h"
41 #include "config.h"
42 #include "mainloop.h"
43 #include "gettext.h"
44 #include "parser/parse.h"
45 #include "render/render.h"
46 #include "render/theme.h"
47
48 #ifdef HAVE_FCNTL_H
49 #  include <fcntl.h>
50 #endif
51 #ifdef HAVE_SIGNAL_H
52 #  include <signal.h>
53 #endif
54 #ifdef HAVE_STDLIB_H
55 #  include <stdlib.h>
56 #endif
57 #ifdef HAVE_LOCALE_H
58 #  include <locale.h>
59 #endif
60 #ifdef HAVE_SYS_STAT_H
61 #  include <sys/stat.h>
62 #  include <sys/types.h>
63 #endif
64 #ifdef HAVE_SYS_WAIT_H
65 #  include <sys/types.h>
66 #  include <sys/wait.h>
67 #endif
68 #ifdef HAVE_UNISTD_H
69 #  include <unistd.h>
70 #endif
71 #include <errno.h>
72
73 #include <X11/cursorfont.h>
74 #if USE_XCURSOR
75 #include <X11/Xcursor/Xcursor.h>
76 #endif
77
78 RrInstance *ob_rr_inst;
79 RrTheme    *ob_rr_theme;
80 ObMainLoop *ob_main_loop;
81 Display    *ob_display;
82 gint        ob_screen;
83 gboolean    ob_replace_wm = FALSE;
84
85 static ObState   state;
86 static gboolean  xsync = FALSE;
87 static gboolean  reconfigure = FALSE;
88 static gboolean  restart = FALSE;
89 static gchar    *restart_path = NULL;
90 static Cursor    cursors[OB_NUM_CURSORS];
91 static KeyCode   keys[OB_NUM_KEYS];
92 static gint      exitcode = 0;
93 static guint     remote_control = 0;
94 static gboolean  being_replaced = FALSE;
95 static gchar    *config_file = NULL;
96
97 static void signal_handler(gint signal, gpointer data);
98 static void parse_args(gint argc, gchar **argv);
99 static Cursor load_cursor(const gchar *name, guint fontval);
100
101 gint main(gint argc, gchar **argv)
102 {
103     state = OB_STATE_STARTING;
104
105     /* initialize the locale */
106     if (!setlocale(LC_ALL, ""))
107         g_message(_("Couldn't set locale from environment."));
108     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
109     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
110     textdomain(PACKAGE_NAME);
111
112     g_set_prgname(argv[0]);
113
114     if (chdir(g_get_home_dir()) == -1)
115         g_message(_("Unable to change to home directory '%s': %s"),
116                   g_get_home_dir(), g_strerror(errno));
117      
118     /* parse out command line args */
119     parse_args(argc, argv);
120
121     if (!remote_control) {
122         parse_paths_startup();
123
124         session_startup(argc, argv);
125     }
126
127     ob_display = XOpenDisplay(NULL);
128     if (ob_display == NULL)
129         ob_exit_with_error("Failed to open the display.");
130     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
131         ob_exit_with_error("Failed to set display as close-on-exec.");
132
133     if (remote_control) {
134         prop_startup();
135
136         /* Send client message telling the OB process to:
137          * remote_control = 1 -> reconfigure 
138          * remote_control = 2 -> restart */
139         PROP_MSG(RootWindow(ob_display, ob_screen),
140                  openbox_control, remote_control, 0, 0, 0);
141         XCloseDisplay(ob_display);
142         exit(EXIT_SUCCESS);
143     }
144
145     ob_main_loop = ob_main_loop_new(ob_display);
146
147     /* set up signal handler */
148     ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
149     ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
150     ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
151     ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
152     ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
153     ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
154     ob_main_loop_signal_add(ob_main_loop, SIGCHLD, signal_handler, NULL, NULL);
155
156     ob_screen = DefaultScreen(ob_display);
157
158     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
159     if (ob_rr_inst == NULL)
160         ob_exit_with_error("Failed to initialize the render library.");
161
162     XSynchronize(ob_display, xsync);
163
164     /* check for locale support */
165     if (!XSupportsLocale())
166         g_message(_("X server does not support locale."));
167     if (!XSetLocaleModifiers(""))
168         g_message(_("Cannot set locale modifiers for the X server."));
169
170     /* set our error handler */
171     XSetErrorHandler(xerror_handler);
172
173     /* set the DISPLAY environment variable for any lauched children, to the
174        display we're using, so they open in the right place. */
175     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
176
177     /* create available cursors */
178     cursors[OB_CURSOR_NONE] = None;
179     cursors[OB_CURSOR_POINTER] = load_cursor("left_ptr", XC_left_ptr);
180     cursors[OB_CURSOR_BUSY] = load_cursor("left_ptr_watch", XC_watch);
181     cursors[OB_CURSOR_MOVE] = load_cursor("fleur", XC_fleur);
182     cursors[OB_CURSOR_NORTH] = load_cursor("top_side", XC_top_side);
183     cursors[OB_CURSOR_NORTHEAST] = load_cursor("top_right_corner",
184                                                XC_top_right_corner);
185     cursors[OB_CURSOR_EAST] = load_cursor("right_side", XC_right_side);
186     cursors[OB_CURSOR_SOUTHEAST] = load_cursor("bottom_right_corner",
187                                                XC_bottom_right_corner);
188     cursors[OB_CURSOR_SOUTH] = load_cursor("bottom_side", XC_bottom_side);
189     cursors[OB_CURSOR_SOUTHWEST] = load_cursor("bottom_left_corner",
190                                                XC_bottom_left_corner);
191     cursors[OB_CURSOR_WEST] = load_cursor("left_side", XC_left_side);
192     cursors[OB_CURSOR_NORTHWEST] = load_cursor("top_left_corner",
193                                                XC_top_left_corner);
194
195     /* create available keycodes */
196     keys[OB_KEY_RETURN] =
197         XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
198     keys[OB_KEY_ESCAPE] =
199         XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
200     keys[OB_KEY_LEFT] =
201         XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
202     keys[OB_KEY_RIGHT] =
203         XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
204     keys[OB_KEY_UP] =
205         XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
206     keys[OB_KEY_DOWN] =
207         XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
208
209     prop_startup(); /* get atoms values for the display */
210     extensions_query_all(); /* find which extensions are present */
211
212     if (screen_annex()) { /* it will be ours! */
213         do {
214             {
215                 ObParseInst *i;
216                 xmlDocPtr doc;
217                 xmlNodePtr node;
218
219                 modkeys_startup(reconfigure);
220
221                 /* startup the parsing so everything can register sections
222                    of the rc */
223                 i = parse_startup();
224
225                 config_startup(i);
226                 /* parse/load user options */
227                 if (parse_load_rc(config_file, &doc, &node, &config_file)) {
228                     PROP_SETS(RootWindow(ob_display, ob_screen),
229                               openbox_rc, config_file);
230                     parse_tree(i, doc, node->xmlChildrenNode);
231                 } else {
232                     g_message(_("Unable to find a valid config file, using some simple defaults"));
233                     PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_rc);
234                 }
235                 /* we're done with parsing now, kill it */
236                 parse_close(doc);
237                 parse_shutdown(i);
238             }
239
240             /* load the theme specified in the rc file */
241             {
242                 RrTheme *theme;
243                 if ((theme = RrThemeNew(ob_rr_inst, config_theme,
244                                         config_font_activewindow,
245                                         config_font_inactivewindow,
246                                         config_font_menutitle,
247                                         config_font_menuitem,
248                                         config_font_osd)))
249                 {
250                     RrThemeFree(ob_rr_theme);
251                     ob_rr_theme = theme;
252                 }
253                 if (ob_rr_theme == NULL)
254                     ob_exit_with_error("Unable to load a theme.");
255             }
256
257             if (reconfigure) {
258                 GList *it;
259
260                 /* update all existing windows for the new theme */
261                 for (it = client_list; it; it = g_list_next(it)) {
262                     ObClient *c = it->data;
263                     frame_adjust_theme(c->frame);
264                 }
265             }
266             event_startup(reconfigure);
267             /* focus_backup is used for stacking, so this needs to come before
268                anything that calls stacking_add */
269             focus_startup(reconfigure);
270             window_startup(reconfigure);
271             sn_startup(reconfigure);
272             screen_startup(reconfigure);
273             grab_startup(reconfigure);
274             group_startup(reconfigure);
275             client_startup(reconfigure);
276             dock_startup(reconfigure);
277             moveresize_startup(reconfigure);
278             keyboard_startup(reconfigure);
279             mouse_startup(reconfigure);
280             menu_startup(reconfigure);
281             menu_frame_startup(reconfigure);
282
283             if (!reconfigure) {
284                 /* get all the existing windows */
285                 client_manage_all();
286                 focus_fallback(TRUE);
287             } else {
288                 GList *it;
289
290                 /* redecorate all existing windows */
291                 for (it = client_list; it; it = g_list_next(it)) {
292                     ObClient *c = it->data;
293                     /* the new config can change the window's decorations */
294                     client_setup_decor_and_functions(c);
295                     /* redraw the frames */
296                     frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
297                 }
298             }
299
300             reconfigure = FALSE;
301
302             state = OB_STATE_RUNNING;
303             ob_main_loop_run(ob_main_loop);
304             state = OB_STATE_EXITING;
305
306             if (!reconfigure) {
307                 dock_remove_all();
308                 client_unmanage_all();
309             }
310
311             menu_frame_shutdown(reconfigure);
312             menu_shutdown(reconfigure);
313             mouse_shutdown(reconfigure);
314             keyboard_shutdown(reconfigure);
315             moveresize_shutdown(reconfigure);
316             dock_shutdown(reconfigure);
317             client_shutdown(reconfigure);
318             group_shutdown(reconfigure);
319             grab_shutdown(reconfigure);
320             screen_shutdown(reconfigure);
321             focus_shutdown(reconfigure);
322             sn_shutdown(reconfigure);
323             window_shutdown(reconfigure);
324             event_shutdown(reconfigure);
325             config_shutdown();
326             modkeys_shutdown(reconfigure);
327         } while (reconfigure);
328     }
329
330     XSync(ob_display, FALSE);
331
332     g_free(config_file); /* this is set by parse_load_rc */
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_message(
354                     _("Restart failed to execute new executable '%s': %s"),
355                     restart_path, err->message);
356                 g_error_free(err);
357             }
358         }
359
360         /* re-run me */
361         execvp(argv[0], argv); /* try how we were run */
362         execlp(argv[0], g_path_get_basename(argv[0]),
363                (char *)NULL); /* last resort */
364     }
365      
366     return exitcode;
367 }
368
369 static void signal_handler(gint signal, gpointer data)
370 {
371     switch (signal) {
372     case SIGUSR1:
373         ob_debug("Caught signal %d. Restarting.\n", signal);
374         ob_restart();
375         break;
376     case SIGUSR2:
377         ob_debug("Caught signal %d. Reconfiguring.\n", signal);
378         ob_reconfigure(); 
379         break;
380     case SIGCHLD:
381         /* reap children */
382         while (waitpid(-1, NULL, WNOHANG) > 0);
383         break;
384     default:
385         ob_debug("Caught signal %d. Exiting.\n", signal);
386         /* TERM and INT return a 0 code */
387         ob_exit(!(signal == SIGTERM || signal == SIGINT));
388     }
389 }
390
391 static void print_version()
392 {
393     g_print("Openbox %s\n", PACKAGE_VERSION);
394     g_print(_("Copyright (c)"));
395     g_print(" 2007        Mikael Magnusson\n");
396     g_print(_("Copyright (c)"));
397     g_print(" 2003-2007   Dana Jansens\n\n");
398     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
399     g_print("This is free software, and you are welcome to redistribute it\n");
400     g_print("under certain conditions. See the file COPYING for details.\n\n");
401 }
402
403 static void print_help()
404 {
405     g_print(_("Syntax: openbox [options]\n"));
406     g_print(_("\nOptions:\n\n"));
407     g_print(_("  --config-file FILE  Specify the file to load for the config file\n"));
408 #ifdef USE_SM
409     g_print(_("  --sm-disable        Disable connection to session manager\n"));
410     g_print(_("  --sm-client-id ID   Specify session management ID\n"));
411     g_print(_("  --sm-save-file FILE Specify file to load a saved session from\n"));
412 #endif
413     g_print(_("  --replace           Replace the currently running window manager\n"));
414     g_print(_("  --help              Display this help and exit\n"));
415     g_print(_("  --version           Display the version and exit\n"));
416     g_print(_("\nPassing messages to a running Openbox instance:\n\n"));
417     g_print(_("  --reconfigure       Reload Openbox's configuration\n"));
418     g_print(_("\nDebugging options:\n\n"));
419     g_print(_("  --sync              Run in synchronous mode\n"));
420     g_print(_("  --debug             Display debugging output\n"));
421     g_print(_("  --debug-focus       Display debugging output for focus handling\n"));
422     g_print(_("\nPlease report bugs at %s\n\n"), PACKAGE_BUGREPORT);
423 }
424
425 static void parse_args(gint argc, gchar **argv)
426 {
427     gint i;
428
429     for (i = 1; i < argc; ++i) {
430         if (!strcmp(argv[i], "--version")) {
431             print_version();
432             exit(0);
433         } else if (!strcmp(argv[i], "--help")) {
434             print_help();
435             exit(0);
436         } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
437             g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
438         } else if (!strcmp(argv[i], "--replace")) {
439             ob_replace_wm = TRUE;
440         } else if (!strcmp(argv[i], "--sync")) {
441             xsync = TRUE;
442         } else if (!strcmp(argv[i], "--debug")) {
443             ob_debug_show_output(TRUE);
444             ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
445         } else if (!strcmp(argv[i], "--debug-focus")) {
446             ob_debug_show_output(TRUE);
447             ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
448             ob_debug_enable(OB_DEBUG_FOCUS, TRUE);
449         } else if (!strcmp(argv[i], "--reconfigure")) {
450             remote_control = 1;
451         } else if (!strcmp(argv[i], "--restart")) {
452             remote_control = 2;
453         } else if (!strcmp(argv[i], "--config-file")) {
454             if (i == argc - 1) /* no args left */
455                 g_printerr(_("--config-file requires an argument\n"));
456             else {
457                 config_file = g_strdup(argv[i+1]);
458                 ++i;
459             }
460         }
461     }
462 }
463
464 static Cursor load_cursor(const gchar *name, guint fontval)
465 {
466     Cursor c = None;
467
468 #if USE_XCURSOR
469     c = XcursorLibraryLoadCursor(ob_display, name);
470 #endif
471     if (c == None)
472         c = XCreateFontCursor(ob_display, fontval);
473     return c;
474 }
475
476 void ob_exit_with_error(const gchar *msg)
477 {
478     g_critical(msg);
479     session_shutdown(TRUE);
480     exit(EXIT_FAILURE);
481 }
482
483 void ob_restart_other(const gchar *path)
484 {
485     restart_path = g_strdup(path);
486     ob_restart();
487 }
488
489 void ob_restart()
490 {
491     restart = TRUE;
492     ob_exit(0);
493 }
494
495 void ob_reconfigure()
496 {
497     reconfigure = TRUE;
498     ob_exit(0);
499 }
500
501 void ob_exit(gint code)
502 {
503     exitcode = code;
504     ob_main_loop_exit(ob_main_loop);
505 }
506
507 void ob_exit_replace()
508 {
509     exitcode = 0;
510     being_replaced = TRUE;
511     ob_main_loop_exit(ob_main_loop);
512 }
513
514 Cursor ob_cursor(ObCursor cursor)
515 {
516     g_assert(cursor < OB_NUM_CURSORS);
517     return cursors[cursor];
518 }
519
520 KeyCode ob_keycode(ObKey key)
521 {
522     g_assert(key < OB_NUM_KEYS);
523     return keys[key];
524 }
525
526 ObState ob_state()
527 {
528     return state;
529 }