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