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_paths_startup();
117 session_startup(&argc
, &argv
);
119 /* parse out command line args */
120 parse_args(argc
, argv
);
122 ob_display
= XOpenDisplay(NULL
);
123 if (ob_display
== NULL
)
124 ob_exit_with_error("Failed to open the display.");
125 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
126 ob_exit_with_error("Failed to set display as close-on-exec.");
128 ob_main_loop
= ob_main_loop_new(ob_display
);
130 /* set up signal handler */
131 ob_main_loop_signal_add(ob_main_loop
, SIGUSR1
, signal_handler
, NULL
, NULL
);
132 ob_main_loop_signal_add(ob_main_loop
, SIGUSR2
, signal_handler
, NULL
, NULL
);
133 ob_main_loop_signal_add(ob_main_loop
, SIGTERM
, signal_handler
, NULL
, NULL
);
134 ob_main_loop_signal_add(ob_main_loop
, SIGINT
, signal_handler
, NULL
, NULL
);
135 ob_main_loop_signal_add(ob_main_loop
, SIGHUP
, signal_handler
, NULL
, NULL
);
136 ob_main_loop_signal_add(ob_main_loop
, SIGPIPE
, signal_handler
, NULL
, NULL
);
137 ob_main_loop_signal_add(ob_main_loop
, SIGCHLD
, signal_handler
, NULL
, NULL
);
139 ob_screen
= DefaultScreen(ob_display
);
141 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
142 if (ob_rr_inst
== NULL
)
143 ob_exit_with_error("Failed to initialize the render library.");
145 XSynchronize(ob_display
, xsync
);
147 /* check for locale support */
148 if (!XSupportsLocale())
149 g_warning("X server does not support locale.");
150 if (!XSetLocaleModifiers(""))
151 g_warning("Cannot set locale modifiers for the X server.");
153 /* set our error handler */
154 XSetErrorHandler(xerror_handler
);
156 /* set the DISPLAY environment variable for any lauched children, to the
157 display we're using, so they open in the right place. */
158 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
160 /* create available cursors */
161 cursors
[OB_CURSOR_NONE
] = None
;
162 cursors
[OB_CURSOR_POINTER
] =
163 XCreateFontCursor(ob_display
, XC_left_ptr
);
164 cursors
[OB_CURSOR_BUSY
] =
165 XCreateFontCursor(ob_display
, XC_watch
);
166 cursors
[OB_CURSOR_MOVE
] =
167 XCreateFontCursor(ob_display
, XC_fleur
);
168 cursors
[OB_CURSOR_NORTH
] =
169 XCreateFontCursor(ob_display
, XC_top_side
);
170 cursors
[OB_CURSOR_NORTHEAST
] =
171 XCreateFontCursor(ob_display
, XC_top_right_corner
);
172 cursors
[OB_CURSOR_EAST
] =
173 XCreateFontCursor(ob_display
, XC_right_side
);
174 cursors
[OB_CURSOR_SOUTHEAST
] =
175 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
176 cursors
[OB_CURSOR_SOUTH
] =
177 XCreateFontCursor(ob_display
, XC_bottom_side
);
178 cursors
[OB_CURSOR_SOUTHWEST
] =
179 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
180 cursors
[OB_CURSOR_WEST
] =
181 XCreateFontCursor(ob_display
, XC_left_side
);
182 cursors
[OB_CURSOR_NORTHWEST
] =
183 XCreateFontCursor(ob_display
, XC_top_left_corner
);
185 /* create available keycodes */
186 keys
[OB_KEY_RETURN
] =
187 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
188 keys
[OB_KEY_ESCAPE
] =
189 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
191 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
193 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
195 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
197 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
199 prop_startup(); /* get atoms values for the display */
200 extensions_query_all(); /* find which extensions are present */
202 if (screen_annex()) { /* it will be ours! */
209 /* startup the parsing so everything can register sections
214 /* parse/load user options */
215 if (parse_load_rc(&doc
, &node
))
216 parse_tree(i
, doc
, node
->xmlChildrenNode
);
217 /* we're done with parsing now, kill it */
222 /* load the theme specified in the rc file */
225 if ((theme
= RrThemeNew(ob_rr_inst
, config_theme
))) {
226 RrThemeFree(ob_rr_theme
);
229 if (ob_rr_theme
== NULL
)
230 ob_exit_with_error("Unable to load a theme.");
236 /* update all existing windows for the new theme */
237 for (it
= client_list
; it
; it
= g_list_next(it
)) {
238 ObClient
*c
= it
->data
;
239 frame_adjust_theme(c
->frame
);
242 event_startup(reconfigure
);
243 /* focus_backup is used for stacking, so this needs to come before
244 anything that calls stacking_add */
245 focus_startup(reconfigure
);
246 window_startup(reconfigure
);
247 sn_startup(reconfigure
);
248 screen_startup(reconfigure
);
249 grab_startup(reconfigure
);
250 group_startup(reconfigure
);
251 client_startup(reconfigure
);
252 dock_startup(reconfigure
);
253 moveresize_startup(reconfigure
);
254 keyboard_startup(reconfigure
);
255 mouse_startup(reconfigure
);
256 menu_startup(reconfigure
);
259 /* get all the existing windows */
261 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS
);
265 /* redecorate all existing windows */
266 for (it
= client_list
; it
; it
= g_list_next(it
)) {
267 ObClient
*c
= it
->data
;
268 frame_adjust_area(c
->frame
, TRUE
, TRUE
, FALSE
);
274 state
= OB_STATE_RUNNING
;
275 ob_main_loop_run(ob_main_loop
);
276 state
= OB_STATE_EXITING
;
280 client_unmanage_all();
283 menu_shutdown(reconfigure
);
284 mouse_shutdown(reconfigure
);
285 keyboard_shutdown(reconfigure
);
286 moveresize_shutdown(reconfigure
);
287 dock_shutdown(reconfigure
);
288 client_shutdown(reconfigure
);
289 group_shutdown(reconfigure
);
290 grab_shutdown(reconfigure
);
291 screen_shutdown(reconfigure
);
292 focus_shutdown(reconfigure
);
293 sn_shutdown(reconfigure
);
294 window_shutdown(reconfigure
);
295 event_shutdown(reconfigure
);
297 } while (reconfigure
);
300 XSync(ob_display
, FALSE
);
302 RrThemeFree(ob_rr_theme
);
303 RrInstanceFree(ob_rr_inst
);
307 XCloseDisplay(ob_display
);
309 parse_paths_shutdown();
312 if (restart_path
!= NULL
) {
317 /* run other window manager */
318 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
319 execvp(argvp
[0], argvp
);
322 g_warning("failed to execute '%s': %s", restart_path
,
329 execvp(argv
[0], argv
); /* try how we were run */
330 execlp(argv
[0], g_path_get_basename(argv
[0]),
331 (char *)NULL
); /* last resort */
337 static void signal_handler(gint signal
, gpointer data
)
341 ob_debug("Caught signal %d. Restarting.\n", signal
);
345 ob_debug("Caught signal %d. Reconfiguring.\n", signal
);
350 while (waitpid(-1, NULL
, WNOHANG
) > 0);
353 ob_debug("Caught signal %d. Exiting.\n", signal
);
354 /* TERM and INT return a 0 code */
355 ob_exit(!(signal
== SIGTERM
|| signal
== SIGINT
));
359 static void print_version()
361 g_print("Openbox %s\n", PACKAGE_VERSION
);
362 g_print("Copyright (c) 2006 Mikael Magnusson\n");
363 g_print("Copyright (c) 2003 Ben Jansens\n\n");
364 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
365 g_print("This is free software, and you are welcome to redistribute it\n");
366 g_print("under certain conditions. See the file COPYING for details.\n\n");
369 static void print_help()
371 g_print("Syntax: openbox [options]\n\n");
372 g_print("Options:\n\n");
374 g_print(" --sm-disable Disable connection to session manager\n");
375 g_print(" --sm-client-id ID Specify session management ID\n");
376 g_print(" --sm-save-file FILE Specify file to load a saved session"
379 g_print(" --replace Replace the currently running window "
381 g_print(" --help Display this help and exit\n");
382 g_print(" --version Display the version and exit\n");
383 g_print(" --sync Run in synchronous mode (this is slow and "
385 " debugging X routines)\n");
386 g_print(" --debug Display debugging output\n");
387 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT
);
390 static void parse_args(gint argc
, gchar
**argv
)
394 for (i
= 1; i
< argc
; ++i
) {
395 if (!strcmp(argv
[i
], "--version")) {
398 } else if (!strcmp(argv
[i
], "--help")) {
401 } else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
402 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
403 } else if (!strcmp(argv
[i
], "--replace")) {
404 ob_replace_wm
= TRUE
;
405 } else if (!strcmp(argv
[i
], "--sync")) {
407 } else if (!strcmp(argv
[i
], "--debug")) {
408 ob_debug_show_output(TRUE
);
410 g_printerr("Invalid option: '%s'\n\n", argv
[i
]);
417 void ob_exit_with_error(gchar
*msg
)
424 void ob_restart_other(const gchar
*path
)
426 restart_path
= g_strdup(path
);
436 void ob_reconfigure()
442 void ob_exit(gint code
)
445 ob_main_loop_exit(ob_main_loop
);
448 Cursor
ob_cursor(ObCursor cursor
)
450 g_assert(cursor
< OB_NUM_CURSORS
);
451 return cursors
[cursor
];
454 KeyCode
ob_keycode(ObKey key
)
456 g_assert(key
< OB_NUM_KEYS
);