1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 openbox.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
29 #include "startupnotify.h"
31 #include "moveresize.h"
35 #include "extensions.h"
36 #include "menuframe.h"
42 #include "parser/parse.h"
43 #include "render/render.h"
44 #include "render/theme.h"
58 #ifdef HAVE_SYS_STAT_H
59 # include <sys/stat.h>
60 # include <sys/types.h>
66 #include <X11/cursorfont.h>
68 RrInstance
*ob_rr_inst
;
70 ObMainLoop
*ob_main_loop
;
73 gboolean ob_replace_wm
;
76 static gboolean xsync
;
77 static gboolean reconfigure
;
78 static gboolean restart
;
79 static gchar
*restart_path
;
80 static Cursor cursors
[OB_NUM_CURSORS
];
81 static KeyCode keys
[OB_NUM_KEYS
];
82 static gint exitcode
= 0;
84 static void signal_handler(gint signal
, gpointer data
);
85 static void parse_args(gint argc
, gchar
**argv
);
87 gint
main(gint argc
, gchar
**argv
)
90 ob_debug_show_output(TRUE
);
93 state
= OB_STATE_STARTING
;
95 /* initialize the locale */
96 if (!setlocale(LC_ALL
, ""))
97 g_warning("Couldn't set locale from environment.\n");
98 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
99 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
100 textdomain(PACKAGE_NAME
);
102 g_set_prgname(argv
[0]);
104 chdir(g_get_home_dir());
106 parse_paths_startup();
108 session_startup(&argc
, &argv
);
110 /* parse out command line args */
111 parse_args(argc
, argv
);
113 ob_display
= XOpenDisplay(NULL
);
114 if (ob_display
== NULL
)
115 ob_exit_with_error("Failed to open the display.");
116 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
117 ob_exit_with_error("Failed to set display as close-on-exec.");
119 ob_main_loop
= ob_main_loop_new(ob_display
);
121 /* set up signal handler */
122 ob_main_loop_signal_add(ob_main_loop
, SIGUSR1
, signal_handler
, NULL
, NULL
);
123 ob_main_loop_signal_add(ob_main_loop
, SIGUSR2
, signal_handler
, NULL
, NULL
);
124 ob_main_loop_signal_add(ob_main_loop
, SIGTERM
, signal_handler
, NULL
, NULL
);
125 ob_main_loop_signal_add(ob_main_loop
, SIGINT
, signal_handler
, NULL
, NULL
);
126 ob_main_loop_signal_add(ob_main_loop
, SIGHUP
, signal_handler
, NULL
, NULL
);
127 ob_main_loop_signal_add(ob_main_loop
, SIGPIPE
, signal_handler
, NULL
, NULL
);
129 ob_screen
= DefaultScreen(ob_display
);
131 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
132 if (ob_rr_inst
== NULL
)
133 ob_exit_with_error("Failed to initialize the render library.");
135 XSynchronize(ob_display
, xsync
);
137 /* check for locale support */
138 if (!XSupportsLocale())
139 g_warning("X server does not support locale.");
140 if (!XSetLocaleModifiers(""))
141 g_warning("Cannot set locale modifiers for the X server.");
143 /* set our error handler */
144 XSetErrorHandler(xerror_handler
);
146 /* set the DISPLAY environment variable for any lauched children, to the
147 display we're using, so they open in the right place. */
148 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
150 /* create available cursors */
151 cursors
[OB_CURSOR_NONE
] = None
;
152 cursors
[OB_CURSOR_POINTER
] =
153 XCreateFontCursor(ob_display
, XC_left_ptr
);
154 cursors
[OB_CURSOR_BUSY
] =
155 XCreateFontCursor(ob_display
, XC_watch
);
156 cursors
[OB_CURSOR_MOVE
] =
157 XCreateFontCursor(ob_display
, XC_fleur
);
158 cursors
[OB_CURSOR_NORTH
] =
159 XCreateFontCursor(ob_display
, XC_top_side
);
160 cursors
[OB_CURSOR_NORTHEAST
] =
161 XCreateFontCursor(ob_display
, XC_top_right_corner
);
162 cursors
[OB_CURSOR_EAST
] =
163 XCreateFontCursor(ob_display
, XC_right_side
);
164 cursors
[OB_CURSOR_SOUTHEAST
] =
165 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
166 cursors
[OB_CURSOR_SOUTH
] =
167 XCreateFontCursor(ob_display
, XC_bottom_side
);
168 cursors
[OB_CURSOR_SOUTHWEST
] =
169 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
170 cursors
[OB_CURSOR_WEST
] =
171 XCreateFontCursor(ob_display
, XC_left_side
);
172 cursors
[OB_CURSOR_NORTHWEST
] =
173 XCreateFontCursor(ob_display
, XC_top_left_corner
);
175 /* create available keycodes */
176 keys
[OB_KEY_RETURN
] =
177 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
178 keys
[OB_KEY_ESCAPE
] =
179 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
181 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
183 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
185 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
187 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
189 prop_startup(); /* get atoms values for the display */
190 extensions_query_all(); /* find which extensions are present */
192 if (screen_annex()) { /* it will be ours! */
199 /* startup the parsing so everything can register sections
204 /* parse/load user options */
205 if (parse_load_rc(&doc
, &node
))
206 parse_tree(i
, doc
, node
->xmlChildrenNode
);
207 /* we're done with parsing now, kill it */
212 /* load the theme specified in the rc file */
215 if ((theme
= RrThemeNew(ob_rr_inst
, config_theme
))) {
216 RrThemeFree(ob_rr_theme
);
219 if (ob_rr_theme
== NULL
)
220 ob_exit_with_error("Unable to load a theme.");
226 /* update all existing windows for the new theme */
227 for (it
= client_list
; it
; it
= g_list_next(it
)) {
228 ObClient
*c
= it
->data
;
229 frame_adjust_theme(c
->frame
);
232 event_startup(reconfigure
);
233 /* focus_backup is used for stacking, so this needs to come before
234 anything that calls stacking_add */
235 focus_startup(reconfigure
);
236 window_startup(reconfigure
);
237 sn_startup(reconfigure
);
238 screen_startup(reconfigure
);
239 grab_startup(reconfigure
);
240 group_startup(reconfigure
);
241 client_startup(reconfigure
);
242 dock_startup(reconfigure
);
243 moveresize_startup(reconfigure
);
244 keyboard_startup(reconfigure
);
245 mouse_startup(reconfigure
);
246 menu_startup(reconfigure
);
249 /* get all the existing windows */
251 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS
);
255 /* redecorate all existing windows */
256 for (it
= client_list
; it
; it
= g_list_next(it
)) {
257 ObClient
*c
= it
->data
;
258 frame_adjust_area(c
->frame
, TRUE
, TRUE
, FALSE
);
264 state
= OB_STATE_RUNNING
;
265 ob_main_loop_run(ob_main_loop
);
266 state
= OB_STATE_EXITING
;
270 client_unmanage_all();
273 menu_shutdown(reconfigure
);
274 mouse_shutdown(reconfigure
);
275 keyboard_shutdown(reconfigure
);
276 moveresize_shutdown(reconfigure
);
277 dock_shutdown(reconfigure
);
278 client_shutdown(reconfigure
);
279 group_shutdown(reconfigure
);
280 grab_shutdown(reconfigure
);
281 screen_shutdown(reconfigure
);
282 focus_shutdown(reconfigure
);
283 sn_shutdown(reconfigure
);
284 window_shutdown(reconfigure
);
285 event_shutdown(reconfigure
);
287 } while (reconfigure
);
290 XSync(ob_display
, FALSE
);
292 RrThemeFree(ob_rr_theme
);
293 RrInstanceFree(ob_rr_inst
);
297 XCloseDisplay(ob_display
);
299 parse_paths_shutdown();
302 if (restart_path
!= NULL
) {
308 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
309 execvp(argvp
[0], argvp
);
312 g_warning("failed to execute '%s': %s", restart_path
,
319 execvp(argv
[0], argv
); /* try how we were run */
320 execlp(argv
[0], g_path_get_basename(argv
[0])); /* last resort */
326 static void signal_handler(gint signal
, gpointer data
)
328 if (signal
== SIGUSR1
) {
329 ob_debug("Caught signal %d. Restarting.\n", signal
);
331 } else if (signal
== SIGUSR2
) {
332 ob_debug("Caught signal %d. Reconfiguring.\n", signal
);
335 ob_debug("Caught signal %d. Exiting.\n", signal
);
336 /* TERM and INT return a 0 code */
337 ob_exit(!(signal
== SIGTERM
|| signal
== SIGINT
));
341 static void print_version()
343 g_print("Openbox %s\n", PACKAGE_VERSION
);
344 g_print("Copyright (c) 2003 Ben Jansens, and others\n\n");
345 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
346 g_print("This is free software, and you are welcome to redistribute it\n");
347 g_print("under certain conditions. See the file COPYING for details.\n\n");
350 static void print_help()
352 g_print("Syntax: openbox [options]\n\n");
353 g_print("Options:\n\n");
355 g_print(" --sm-disable Disable connection to session manager\n");
356 g_print(" --sm-client-id ID Specify session management ID\n");
357 g_print(" --sm-save-file FILE Specify file to load a saved session"
360 g_print(" --replace Replace the currently running window "
362 g_print(" --help Display this help and exit\n");
363 g_print(" --version Display the version and exit\n");
364 g_print(" --sync Run in synchronous mode (this is slow and "
366 " debugging X routines)\n");
367 g_print(" --debug Display debugging output\n");
368 g_print("\nPlease report bugs at %s\n\n", PACKAGE_BUGREPORT
);
371 static void parse_args(gint argc
, gchar
**argv
)
375 for (i
= 1; i
< argc
; ++i
) {
376 if (!strcmp(argv
[i
], "--version")) {
379 } else if (!strcmp(argv
[i
], "--help")) {
382 } else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
383 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
384 } else if (!strcmp(argv
[i
], "--replace")) {
385 ob_replace_wm
= TRUE
;
386 } else if (!strcmp(argv
[i
], "--sync")) {
388 } else if (!strcmp(argv
[i
], "--debug")) {
389 ob_debug_show_output(TRUE
);
391 g_printerr("Invalid option: '%s'\n\n", argv
[i
]);
398 void ob_exit_with_error(gchar
*msg
)
405 void ob_restart_other(const gchar
*path
)
407 restart_path
= g_strdup(path
);
417 void ob_reconfigure()
423 void ob_exit(gint code
)
426 ob_main_loop_exit(ob_main_loop
);
429 Cursor
ob_cursor(ObCursor cursor
)
431 g_assert(cursor
< OB_NUM_CURSORS
);
432 return cursors
[cursor
];
435 KeyCode
ob_keycode(ObKey key
)
437 g_assert(key
< OB_NUM_KEYS
);