1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 openbox.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
30 #include "startupnotify.h"
32 #include "moveresize.h"
36 #include "extensions.h"
37 #include "menuframe.h"
43 #include "parser/parse.h"
44 #include "render/render.h"
45 #include "render/theme.h"
59 #ifdef HAVE_SYS_STAT_H
60 # include <sys/stat.h>
61 # include <sys/types.h>
63 #ifdef HAVE_SYS_WAIT_H
64 # include <sys/types.h>
65 # include <sys/wait.h>
72 #include <X11/cursorfont.h>
74 RrInstance
*ob_rr_inst
;
76 ObMainLoop
*ob_main_loop
;
79 gboolean ob_replace_wm
= FALSE
;
82 static gboolean xsync
= FALSE
;
83 static gboolean reconfigure
= FALSE
;
84 static gboolean restart
= FALSE
;
85 static gchar
*restart_path
= NULL
;
86 static Cursor cursors
[OB_NUM_CURSORS
];
87 static KeyCode keys
[OB_NUM_KEYS
];
88 static gint exitcode
= 0;
89 static gboolean reconfigure_and_exit
= FALSE
;
91 static void signal_handler(gint signal
, gpointer data
);
92 static void parse_args(gint argc
, gchar
**argv
);
94 gint
main(gint argc
, gchar
**argv
)
97 ob_debug_show_output(TRUE
);
100 state
= OB_STATE_STARTING
;
102 /* initialize the locale */
103 if (!setlocale(LC_ALL
, ""))
104 g_warning("Couldn't set locale from environment.\n");
105 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
106 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
107 textdomain(PACKAGE_NAME
);
109 g_set_prgname(argv
[0]);
111 if (chdir(g_get_home_dir()) == -1)
112 g_warning("Unable to change to home directory (%s): %s",
113 g_get_home_dir(), g_strerror(errno
));
115 /* parse out command line args */
116 parse_args(argc
, argv
);
118 if (!reconfigure_and_exit
) {
119 parse_paths_startup();
121 session_startup(argc
, argv
);
124 ob_display
= XOpenDisplay(NULL
);
125 if (ob_display
== NULL
)
126 ob_exit_with_error("Failed to open the display.");
127 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
128 ob_exit_with_error("Failed to set display as close-on-exec.");
130 if (reconfigure_and_exit
) {
134 prop_startup(); /* get atoms values for the display */
135 ret
= PROP_GET32(RootWindow(ob_display
, DefaultScreen(ob_display
)),
136 openbox_pid
, cardinal
, &pid
);
137 XCloseDisplay(ob_display
);
139 g_print("Openbox does not appear to be running on this "
142 g_print("Telling the Openbox process # %u to reconfigure.\n", pid
);
143 ret
= (kill(pid
, SIGUSR2
) == 0);
145 g_print("Error: %s.\n", strerror(errno
));
147 exit(ret
? EXIT_SUCCESS
: EXIT_FAILURE
);
150 ob_main_loop
= ob_main_loop_new(ob_display
);
152 /* set up signal handler */
153 ob_main_loop_signal_add(ob_main_loop
, SIGUSR1
, signal_handler
, NULL
, NULL
);
154 ob_main_loop_signal_add(ob_main_loop
, SIGUSR2
, signal_handler
, NULL
, NULL
);
155 ob_main_loop_signal_add(ob_main_loop
, SIGTERM
, signal_handler
, NULL
, NULL
);
156 ob_main_loop_signal_add(ob_main_loop
, SIGINT
, signal_handler
, NULL
, NULL
);
157 ob_main_loop_signal_add(ob_main_loop
, SIGHUP
, signal_handler
, NULL
, NULL
);
158 ob_main_loop_signal_add(ob_main_loop
, SIGPIPE
, signal_handler
, NULL
, NULL
);
159 ob_main_loop_signal_add(ob_main_loop
, SIGCHLD
, signal_handler
, NULL
, NULL
);
161 ob_screen
= DefaultScreen(ob_display
);
163 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
164 if (ob_rr_inst
== NULL
)
165 ob_exit_with_error("Failed to initialize the render library.");
167 XSynchronize(ob_display
, xsync
);
169 /* check for locale support */
170 if (!XSupportsLocale())
171 g_warning("X server does not support locale.");
172 if (!XSetLocaleModifiers(""))
173 g_warning("Cannot set locale modifiers for the X server.");
175 /* set our error handler */
176 XSetErrorHandler(xerror_handler
);
178 /* set the DISPLAY environment variable for any lauched children, to the
179 display we're using, so they open in the right place. */
180 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
182 /* create available cursors */
183 cursors
[OB_CURSOR_NONE
] = None
;
184 cursors
[OB_CURSOR_POINTER
] =
185 XCreateFontCursor(ob_display
, XC_left_ptr
);
186 cursors
[OB_CURSOR_BUSY
] =
187 XCreateFontCursor(ob_display
, XC_watch
);
188 cursors
[OB_CURSOR_MOVE
] =
189 XCreateFontCursor(ob_display
, XC_fleur
);
190 cursors
[OB_CURSOR_NORTH
] =
191 XCreateFontCursor(ob_display
, XC_top_side
);
192 cursors
[OB_CURSOR_NORTHEAST
] =
193 XCreateFontCursor(ob_display
, XC_top_right_corner
);
194 cursors
[OB_CURSOR_EAST
] =
195 XCreateFontCursor(ob_display
, XC_right_side
);
196 cursors
[OB_CURSOR_SOUTHEAST
] =
197 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
198 cursors
[OB_CURSOR_SOUTH
] =
199 XCreateFontCursor(ob_display
, XC_bottom_side
);
200 cursors
[OB_CURSOR_SOUTHWEST
] =
201 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
202 cursors
[OB_CURSOR_WEST
] =
203 XCreateFontCursor(ob_display
, XC_left_side
);
204 cursors
[OB_CURSOR_NORTHWEST
] =
205 XCreateFontCursor(ob_display
, XC_top_left_corner
);
207 /* create available keycodes */
208 keys
[OB_KEY_RETURN
] =
209 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
210 keys
[OB_KEY_ESCAPE
] =
211 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
213 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
215 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
217 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
219 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
221 prop_startup(); /* get atoms values for the display */
222 extensions_query_all(); /* find which extensions are present */
224 if (screen_annex()) { /* it will be ours! */
231 /* startup the parsing so everything can register sections
236 /* parse/load user options */
237 if (parse_load_rc(&doc
, &node
))
238 parse_tree(i
, doc
, node
->xmlChildrenNode
);
239 /* we're done with parsing now, kill it */
244 /* load the theme specified in the rc file */
247 if ((theme
= RrThemeNew(ob_rr_inst
, config_theme
,
248 config_font_activewindow
,
249 config_font_inactivewindow
,
250 config_font_menutitle
,
251 config_font_menuitem
)))
253 RrThemeFree(ob_rr_theme
);
256 if (ob_rr_theme
== NULL
)
257 ob_exit_with_error("Unable to load a theme.");
263 /* update all existing windows for the new theme */
264 for (it
= client_list
; it
; it
= g_list_next(it
)) {
265 ObClient
*c
= it
->data
;
266 frame_adjust_theme(c
->frame
);
269 event_startup(reconfigure
);
270 /* focus_backup is used for stacking, so this needs to come before
271 anything that calls stacking_add */
272 focus_startup(reconfigure
);
273 window_startup(reconfigure
);
274 sn_startup(reconfigure
);
275 screen_startup(reconfigure
);
276 grab_startup(reconfigure
);
277 group_startup(reconfigure
);
278 client_startup(reconfigure
);
279 dock_startup(reconfigure
);
280 moveresize_startup(reconfigure
);
281 keyboard_startup(reconfigure
);
282 mouse_startup(reconfigure
);
283 menu_startup(reconfigure
);
286 /* get all the existing windows */
288 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS
);
292 /* redecorate all existing windows */
293 for (it
= client_list
; it
; it
= g_list_next(it
)) {
294 ObClient
*c
= it
->data
;
295 /* the new config can change the window's decorations */
296 client_setup_decor_and_functions(c
);
297 /* redraw the frames */
298 frame_adjust_area(c
->frame
, TRUE
, TRUE
, FALSE
);
304 state
= OB_STATE_RUNNING
;
305 ob_main_loop_run(ob_main_loop
);
306 state
= OB_STATE_EXITING
;
310 client_unmanage_all();
313 menu_shutdown(reconfigure
);
314 mouse_shutdown(reconfigure
);
315 keyboard_shutdown(reconfigure
);
316 moveresize_shutdown(reconfigure
);
317 dock_shutdown(reconfigure
);
318 client_shutdown(reconfigure
);
319 group_shutdown(reconfigure
);
320 grab_shutdown(reconfigure
);
321 screen_shutdown(reconfigure
);
322 focus_shutdown(reconfigure
);
323 sn_shutdown(reconfigure
);
324 window_shutdown(reconfigure
);
325 event_shutdown(reconfigure
);
327 } while (reconfigure
);
330 XSync(ob_display
, FALSE
);
332 RrThemeFree(ob_rr_theme
);
333 RrInstanceFree(ob_rr_inst
);
337 XCloseDisplay(ob_display
);
339 parse_paths_shutdown();
342 if (restart_path
!= NULL
) {
347 /* run other window manager */
348 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
349 execvp(argvp
[0], argvp
);
352 g_warning("failed to execute '%s': %s", restart_path
,
359 execvp(argv
[0], argv
); /* try how we were run */
360 execlp(argv
[0], g_path_get_basename(argv
[0]),
361 (char *)NULL
); /* last resort */
367 static void signal_handler(gint signal
, gpointer data
)
371 ob_debug("Caught signal %d. Restarting.\n", signal
);
375 ob_debug("Caught signal %d. Reconfiguring.\n", signal
);
380 while (waitpid(-1, NULL
, WNOHANG
) > 0);
383 ob_debug("Caught signal %d. Exiting.\n", signal
);
384 /* TERM and INT return a 0 code */
385 ob_exit(!(signal
== SIGTERM
|| signal
== SIGINT
));
389 static void print_version()
391 g_print("Openbox %s\n", PACKAGE_VERSION
);
392 g_print("Copyright (c) 2006 Mikael Magnusson\n");
393 g_print("Copyright (c) 2003 Ben 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");
399 static void print_help()
401 g_print("Syntax: openbox [options]\n\n");
402 g_print("Options:\n\n");
403 g_print(" --reconfigure Tell the currently running instance of "
405 " reconfigure (and then exit immediately)\n");
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"
412 g_print(" --replace Replace the currently running window "
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 "
418 " debugging X routines)\n");
419 g_print(" --debug Display debugging output\n");
420 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT
);
423 static void parse_args(gint argc
, gchar
**argv
)
427 for (i
= 1; i
< argc
; ++i
) {
428 if (!strcmp(argv
[i
], "--version")) {
431 } else if (!strcmp(argv
[i
], "--help")) {
434 } else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
435 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
436 } else if (!strcmp(argv
[i
], "--replace")) {
437 ob_replace_wm
= TRUE
;
438 } else if (!strcmp(argv
[i
], "--sync")) {
440 } else if (!strcmp(argv
[i
], "--debug")) {
441 ob_debug_show_output(TRUE
);
442 } else if (!strcmp(argv
[i
], "--reconfigure")) {
443 reconfigure_and_exit
= TRUE
;
448 void ob_exit_with_error(gchar
*msg
)
455 void ob_restart_other(const gchar
*path
)
457 restart_path
= g_strdup(path
);
467 void ob_reconfigure()
473 void ob_exit(gint code
)
476 ob_main_loop_exit(ob_main_loop
);
479 Cursor
ob_cursor(ObCursor cursor
)
481 g_assert(cursor
< OB_NUM_CURSORS
);
482 return cursors
[cursor
];
485 KeyCode
ob_keycode(ObKey key
)
487 g_assert(key
< OB_NUM_KEYS
);