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