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 message_and_exit
= FALSE
;
91 static gboolean being_replaced
= FALSE
;
93 static void signal_handler(gint signal
, gpointer data
);
94 static void parse_args(gint argc
, gchar
**argv
);
96 gint
main(gint argc
, gchar
**argv
)
99 ob_debug_show_output(TRUE
);
102 state
= OB_STATE_STARTING
;
104 /* initialize the locale */
105 if (!setlocale(LC_ALL
, ""))
106 g_warning("Couldn't set locale from environment.\n");
107 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
108 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
109 textdomain(PACKAGE_NAME
);
111 g_set_prgname(argv
[0]);
113 if (chdir(g_get_home_dir()) == -1)
114 g_warning("Unable to change to home directory (%s): %s",
115 g_get_home_dir(), g_strerror(errno
));
117 /* parse out command line args */
118 parse_args(argc
, argv
);
120 if (!message_and_exit
) {
121 parse_paths_startup();
123 session_startup(argc
, argv
);
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.");
132 if (message_and_exit
) {
135 /* Send client message telling the OB process to reconfigure */
136 prop_message(RootWindow(ob_display
, ob_screen
), prop_atoms
.ob_control
,
137 *message
, 0, 0, 0, SubstructureNotifyMask
);
138 XCloseDisplay(ob_display
);
142 ob_main_loop
= ob_main_loop_new(ob_display
);
144 /* set up signal handler */
145 ob_main_loop_signal_add(ob_main_loop
, SIGUSR1
, signal_handler
, NULL
, NULL
);
146 ob_main_loop_signal_add(ob_main_loop
, SIGUSR2
, signal_handler
, NULL
, NULL
);
147 ob_main_loop_signal_add(ob_main_loop
, SIGTERM
, signal_handler
, NULL
, NULL
);
148 ob_main_loop_signal_add(ob_main_loop
, SIGINT
, signal_handler
, NULL
, NULL
);
149 ob_main_loop_signal_add(ob_main_loop
, SIGHUP
, signal_handler
, NULL
, NULL
);
150 ob_main_loop_signal_add(ob_main_loop
, SIGPIPE
, signal_handler
, NULL
, NULL
);
151 ob_main_loop_signal_add(ob_main_loop
, SIGCHLD
, signal_handler
, NULL
, NULL
);
153 ob_screen
= DefaultScreen(ob_display
);
155 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
156 if (ob_rr_inst
== NULL
)
157 ob_exit_with_error("Failed to initialize the render library.");
159 XSynchronize(ob_display
, xsync
);
161 /* check for locale support */
162 if (!XSupportsLocale())
163 g_warning("X server does not support locale.");
164 if (!XSetLocaleModifiers(""))
165 g_warning("Cannot set locale modifiers for the X server.");
167 /* set our error handler */
168 XSetErrorHandler(xerror_handler
);
170 /* set the DISPLAY environment variable for any lauched children, to the
171 display we're using, so they open in the right place. */
172 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
174 /* create available cursors */
175 cursors
[OB_CURSOR_NONE
] = None
;
176 cursors
[OB_CURSOR_POINTER
] =
177 XCreateFontCursor(ob_display
, XC_left_ptr
);
178 cursors
[OB_CURSOR_BUSY
] =
179 XCreateFontCursor(ob_display
, XC_watch
);
180 cursors
[OB_CURSOR_MOVE
] =
181 XCreateFontCursor(ob_display
, XC_fleur
);
182 cursors
[OB_CURSOR_NORTH
] =
183 XCreateFontCursor(ob_display
, XC_top_side
);
184 cursors
[OB_CURSOR_NORTHEAST
] =
185 XCreateFontCursor(ob_display
, XC_top_right_corner
);
186 cursors
[OB_CURSOR_EAST
] =
187 XCreateFontCursor(ob_display
, XC_right_side
);
188 cursors
[OB_CURSOR_SOUTHEAST
] =
189 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
190 cursors
[OB_CURSOR_SOUTH
] =
191 XCreateFontCursor(ob_display
, XC_bottom_side
);
192 cursors
[OB_CURSOR_SOUTHWEST
] =
193 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
194 cursors
[OB_CURSOR_WEST
] =
195 XCreateFontCursor(ob_display
, XC_left_side
);
196 cursors
[OB_CURSOR_NORTHWEST
] =
197 XCreateFontCursor(ob_display
, XC_top_left_corner
);
199 /* create available keycodes */
200 keys
[OB_KEY_RETURN
] =
201 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
202 keys
[OB_KEY_ESCAPE
] =
203 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
205 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
207 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
209 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
211 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
213 prop_startup(); /* get atoms values for the display */
214 extensions_query_all(); /* find which extensions are present */
216 if (screen_annex()) { /* it will be ours! */
223 /* startup the parsing so everything can register sections
228 /* parse/load user options */
229 if (parse_load_rc(&doc
, &node
))
230 parse_tree(i
, doc
, node
->xmlChildrenNode
);
231 /* we're done with parsing now, kill it */
236 /* load the theme specified in the rc file */
239 if ((theme
= RrThemeNew(ob_rr_inst
, config_theme
,
240 config_font_activewindow
,
241 config_font_inactivewindow
,
242 config_font_menutitle
,
243 config_font_menuitem
)))
245 RrThemeFree(ob_rr_theme
);
248 if (ob_rr_theme
== NULL
)
249 ob_exit_with_error("Unable to load a theme.");
255 /* update all existing windows for the new theme */
256 for (it
= client_list
; it
; it
= g_list_next(it
)) {
257 ObClient
*c
= it
->data
;
258 frame_adjust_theme(c
->frame
);
261 event_startup(reconfigure
);
262 /* focus_backup is used for stacking, so this needs to come before
263 anything that calls stacking_add */
264 focus_startup(reconfigure
);
265 window_startup(reconfigure
);
266 sn_startup(reconfigure
);
267 screen_startup(reconfigure
);
268 grab_startup(reconfigure
);
269 group_startup(reconfigure
);
270 client_startup(reconfigure
);
271 dock_startup(reconfigure
);
272 moveresize_startup(reconfigure
);
273 keyboard_startup(reconfigure
);
274 mouse_startup(reconfigure
);
275 menu_startup(reconfigure
);
278 /* get all the existing windows */
280 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS
);
284 /* redecorate all existing windows */
285 for (it
= client_list
; it
; it
= g_list_next(it
)) {
286 ObClient
*c
= it
->data
;
287 /* the new config can change the window's decorations */
288 client_setup_decor_and_functions(c
);
289 /* redraw the frames */
290 frame_adjust_area(c
->frame
, TRUE
, TRUE
, FALSE
);
296 state
= OB_STATE_RUNNING
;
297 ob_main_loop_run(ob_main_loop
);
298 state
= OB_STATE_EXITING
;
302 client_unmanage_all();
305 menu_shutdown(reconfigure
);
306 mouse_shutdown(reconfigure
);
307 keyboard_shutdown(reconfigure
);
308 moveresize_shutdown(reconfigure
);
309 dock_shutdown(reconfigure
);
310 client_shutdown(reconfigure
);
311 group_shutdown(reconfigure
);
312 grab_shutdown(reconfigure
);
313 screen_shutdown(reconfigure
);
314 focus_shutdown(reconfigure
);
315 sn_shutdown(reconfigure
);
316 window_shutdown(reconfigure
);
317 event_shutdown(reconfigure
);
319 } while (reconfigure
);
322 XSync(ob_display
, FALSE
);
324 RrThemeFree(ob_rr_theme
);
325 RrInstanceFree(ob_rr_inst
);
327 session_shutdown(being_replaced
);
329 XCloseDisplay(ob_display
);
331 parse_paths_shutdown();
334 if (restart_path
!= NULL
) {
339 /* run other window manager */
340 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
341 execvp(argvp
[0], argvp
);
344 g_warning("failed to execute '%s': %s", restart_path
,
351 execvp(argv
[0], argv
); /* try how we were run */
352 execlp(argv
[0], g_path_get_basename(argv
[0]),
353 (char *)NULL
); /* last resort */
359 static void signal_handler(gint signal
, gpointer data
)
363 ob_debug("Caught signal %d. Restarting.\n", signal
);
367 ob_debug("Caught signal %d. Reconfiguring.\n", signal
);
372 while (waitpid(-1, NULL
, WNOHANG
) > 0);
375 ob_debug("Caught signal %d. Exiting.\n", signal
);
376 /* TERM and INT return a 0 code */
377 ob_exit(!(signal
== SIGTERM
|| signal
== SIGINT
));
381 static void print_version()
383 g_print("Openbox %s\n", PACKAGE_VERSION
);
384 g_print("Copyright (c) 2006 Mikael Magnusson\n");
385 g_print("Copyright (c) 2003 Ben Jansens\n\n");
386 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
387 g_print("This is free software, and you are welcome to redistribute it\n");
388 g_print("under certain conditions. See the file COPYING for details.\n\n");
391 static void print_help()
393 g_print("Syntax: openbox [options]\n\n");
394 g_print("Options:\n\n");
395 g_print(" --reconfigure Tell the currently running instance of "
397 " reconfigure (and then exit immediately)\n");
399 g_print(" --sm-disable Disable connection to session manager\n");
400 g_print(" --sm-client-id ID Specify session management ID\n");
401 g_print(" --sm-save-file FILE Specify file to load a saved session"
404 g_print(" --replace Replace the currently running window "
406 g_print(" --help Display this help and exit\n");
407 g_print(" --version Display the version and exit\n");
408 g_print(" --sync Run in synchronous mode (this is slow and "
410 " debugging X routines)\n");
411 g_print(" --debug Display debugging output\n");
412 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT
);
415 static void parse_args(gint argc
, gchar
**argv
)
419 for (i
= 1; i
< argc
; ++i
) {
420 if (!strcmp(argv
[i
], "--version")) {
423 } else if (!strcmp(argv
[i
], "--help")) {
426 } else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
427 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
428 } else if (!strcmp(argv
[i
], "--replace")) {
429 ob_replace_wm
= TRUE
;
430 } else if (!strcmp(argv
[i
], "--sync")) {
432 } else if (!strcmp(argv
[i
], "--debug")) {
433 ob_debug_show_output(TRUE
);
434 } else if (!strcmp(argv
[i
], "--reconfigure")) {
435 message_and_exit
= TRUE
;
436 message
= &prop_atoms
.ob_reconfigure
;
437 } else if (!strcmp(argv
[i
], "--restart")) {
438 message_and_exit
= TRUE
;
439 message
= &prop_atoms
.ob_restart
;
444 void ob_exit_with_error(gchar
*msg
)
447 session_shutdown(TRUE
);
451 void ob_restart_other(const gchar
*path
)
453 restart_path
= g_strdup(path
);
463 void ob_reconfigure()
469 void ob_exit(gint code
)
472 ob_main_loop_exit(ob_main_loop
);
475 void ob_exit_replace()
478 being_replaced
= TRUE
;
479 ob_main_loop_exit(ob_main_loop
);
482 Cursor
ob_cursor(ObCursor cursor
)
484 g_assert(cursor
< OB_NUM_CURSORS
);
485 return cursors
[cursor
];
488 KeyCode
ob_keycode(ObKey key
)
490 g_assert(key
< OB_NUM_KEYS
);