]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
display a popup and perform a smooth transition when reconfiguring
[chaz/openbox] / openbox / openbox.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "session.h"
4 #include "dock.h"
5 #include "event.h"
6 #include "menu.h"
7 #include "client.h"
8 #include "xerror.h"
9 #include "prop.h"
10 #include "screen.h"
11 #include "startupnotify.h"
12 #include "focus.h"
13 #include "moveresize.h"
14 #include "frame.h"
15 #include "keyboard.h"
16 #include "mouse.h"
17 #include "extensions.h"
18 #include "menuframe.h"
19 #include "grab.h"
20 #include "popup.h"
21 #include "group.h"
22 #include "config.h"
23 #include "mainloop.h"
24 #include "gettext.h"
25 #include "parser/parse.h"
26 #include "render/render.h"
27 #include "render/theme.h"
28
29 #ifdef HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif
32 #ifdef HAVE_SIGNAL_H
33 #define __USE_UNIX98
34 # include <signal.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 # include <stdlib.h>
38 #endif
39 #ifdef HAVE_LOCALE_H
40 # include <locale.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 # include <sys/types.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49
50 #include <X11/cursorfont.h>
51
52 RrInstance *ob_rr_inst;
53 RrTheme *ob_rr_theme;
54 ObMainLoop *ob_main_loop;
55 Display *ob_display;
56 gint ob_screen;
57 gboolean ob_sm_use = TRUE;
58 gchar *ob_sm_id;
59 gboolean ob_replace_wm;
60
61 static ObState state;
62 static gboolean xsync;
63 static gboolean reconfigure;
64 static gboolean restart;
65 static char *restart_path;
66 static Cursor cursors[OB_NUM_CURSORS];
67 static KeyCode keys[OB_NUM_KEYS];
68 static gchar *sm_save_file;
69
70 static void signal_handler(int signal, gpointer data);
71 static void parse_args(int argc, char **argv);
72
73 int main(int argc, char **argv)
74 {
75 char *path;
76
77 #ifdef DEBUG
78 ob_debug_show_output(TRUE);
79 #endif
80
81 state = OB_STATE_STARTING;
82
83 /* initialize the locale */
84 if (!setlocale(LC_ALL, ""))
85 g_warning("Couldn't set locale from environment.\n");
86 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
87 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
88 textdomain(PACKAGE_NAME);
89
90 /* create the ~/.openbox dir */
91 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
92 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
93 S_IROTH | S_IWOTH | S_IXOTH));
94 g_free(path);
95 /* create the ~/.openbox/themes dir */
96 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
97 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
98 S_IROTH | S_IWOTH | S_IXOTH));
99 g_free(path);
100 /* create the ~/.openbox/sessions dir */
101 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
102 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
103 S_IROTH | S_IWOTH | S_IXOTH));
104 g_free(path);
105
106 g_set_prgname(argv[0]);
107
108 /* parse out command line args */
109 parse_args(argc, argv);
110
111 ob_display = XOpenDisplay(NULL);
112 if (ob_display == NULL)
113 ob_exit_with_error("Failed to open the display.");
114 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
115 ob_exit_with_error("Failed to set display as close-on-exec.");
116
117 ob_main_loop = ob_main_loop_new(ob_display);
118
119 /* set up signal handler */
120 ob_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL, NULL);
121 ob_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL, NULL);
122 ob_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL, NULL);
123 ob_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler, NULL, NULL);
124 ob_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler, NULL, NULL);
125 ob_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL, NULL);
126
127 if (sm_save_file)
128 session_load(sm_save_file);
129 session_startup(argc, argv);
130
131 ob_screen = DefaultScreen(ob_display);
132
133 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
134 if (ob_rr_inst == NULL)
135 ob_exit_with_error("Failed to initialize the render library.");
136
137 /* XXX fork self onto other screens */
138
139 XSynchronize(ob_display, xsync);
140
141 /* check for locale support */
142 if (!XSupportsLocale())
143 g_warning("X server does not support locale.");
144 if (!XSetLocaleModifiers(""))
145 g_warning("Cannot set locale modifiers for the X server.");
146
147 /* set our error handler */
148 XSetErrorHandler(xerror_handler);
149
150 /* set the DISPLAY environment variable for any lauched children, to the
151 display we're using, so they open in the right place. */
152 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
153
154 /* create available cursors */
155 cursors[OB_CURSOR_NONE] = None;
156 cursors[OB_CURSOR_POINTER] =
157 XCreateFontCursor(ob_display, XC_left_ptr);
158 cursors[OB_CURSOR_BUSY] =
159 XCreateFontCursor(ob_display, XC_watch);
160 cursors[OB_CURSOR_MOVE] =
161 XCreateFontCursor(ob_display, XC_fleur);
162 cursors[OB_CURSOR_NORTH] =
163 XCreateFontCursor(ob_display, XC_top_side);
164 cursors[OB_CURSOR_NORTHEAST] =
165 XCreateFontCursor(ob_display, XC_top_right_corner);
166 cursors[OB_CURSOR_EAST] =
167 XCreateFontCursor(ob_display, XC_right_side);
168 cursors[OB_CURSOR_SOUTHEAST] =
169 XCreateFontCursor(ob_display, XC_bottom_right_corner);
170 cursors[OB_CURSOR_SOUTH] =
171 XCreateFontCursor(ob_display, XC_bottom_side);
172 cursors[OB_CURSOR_SOUTHWEST] =
173 XCreateFontCursor(ob_display, XC_bottom_left_corner);
174 cursors[OB_CURSOR_WEST] =
175 XCreateFontCursor(ob_display, XC_left_side);
176 cursors[OB_CURSOR_NORTHWEST] =
177 XCreateFontCursor(ob_display, XC_top_left_corner);
178
179 /* create available keycodes */
180 keys[OB_KEY_RETURN] =
181 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
182 keys[OB_KEY_ESCAPE] =
183 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
184 keys[OB_KEY_LEFT] =
185 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
186 keys[OB_KEY_RIGHT] =
187 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
188 keys[OB_KEY_UP] =
189 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
190 keys[OB_KEY_DOWN] =
191 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
192
193 prop_startup(); /* get atoms values for the display */
194 extensions_query_all(); /* find which extensions are present */
195
196 if (screen_annex()) { /* it will be ours! */
197 do {
198 Window reconfig_window;
199 Popup *reconfig_popup;
200
201 if (reconfigure) {
202 gint w, h;
203
204 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
205 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
206
207 reconfig_popup = popup_new(FALSE);
208 popup_position(reconfig_popup, CenterGravity, w / 2, h / 2);
209 popup_show(reconfig_popup, _("Reloading . . ."), NULL);
210
211 reconfig_window = XCreateWindow
212 (ob_display, RootWindow(ob_display, ob_screen),
213 0, 0, w, h,
214 0, CopyFromParent, InputOnly, CopyFromParent, 0, NULL);
215 XMapWindow(ob_display, reconfig_window);
216
217 XFlush(ob_display);
218 }
219
220 event_startup(reconfigure);
221 grab_startup(reconfigure);
222 /* focus_backup is used for stacking, so this needs to come before
223 anything that calls stacking_add */
224 focus_startup(reconfigure);
225 window_startup(reconfigure);
226 sn_startup(reconfigure);
227
228 {
229 ObParseInst *i;
230 xmlDocPtr doc;
231 xmlNodePtr node;
232
233 /* startup the parsing so everything can register sections
234 of the rc */
235 i = parse_startup();
236
237 config_startup(i);
238 /* parse/load user options */
239 if (parse_load_rc(&doc, &node))
240 parse_tree(i, doc, node->xmlChildrenNode);
241 /* we're done with parsing now, kill it */
242 xmlFreeDoc(doc);
243 parse_shutdown(i);
244 }
245
246 /* load the theme specified in the rc file */
247 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
248 if (ob_rr_theme == NULL)
249 ob_exit_with_error("Unable to load a theme.");
250
251 moveresize_startup(reconfigure);
252 screen_startup(reconfigure);
253 group_startup(reconfigure);
254 client_startup(reconfigure);
255 dock_startup(reconfigure);
256 keyboard_startup(reconfigure);
257 mouse_startup(reconfigure);
258 menu_startup(reconfigure);
259
260 if (!reconfigure) {
261 /* get all the existing windows */
262 client_manage_all();
263 } else {
264 GList *it;
265
266 /* redecorate all existing windows */
267 for (it = client_list; it; it = g_list_next(it)) {
268 ObClient *c = it->data;
269 frame_adjust_theme(c->frame);
270 }
271
272 XFlush(ob_display);
273
274 /* destroy the hiding window */
275 XDestroyWindow(ob_display, reconfig_window);
276 popup_free(reconfig_popup);
277 }
278
279 reconfigure = FALSE;
280
281 state = OB_STATE_RUNNING;
282 ob_main_loop_run(ob_main_loop);
283 state = OB_STATE_EXITING;
284
285 if (!reconfigure) {
286 dock_remove_all();
287 client_unmanage_all();
288 }
289
290 menu_shutdown(reconfigure);
291 mouse_shutdown(reconfigure);
292 keyboard_shutdown(reconfigure);
293 dock_shutdown(reconfigure);
294 client_shutdown(reconfigure);
295 group_shutdown(reconfigure);
296 screen_shutdown(reconfigure);
297 focus_shutdown(reconfigure);
298 moveresize_shutdown(reconfigure);
299 sn_shutdown(reconfigure);
300 window_shutdown(reconfigure);
301 grab_shutdown(reconfigure);
302 event_shutdown(reconfigure);
303 config_shutdown();
304 } while (reconfigure);
305 }
306
307 RrThemeFree(ob_rr_theme);
308 RrInstanceFree(ob_rr_inst);
309
310 session_shutdown();
311 g_free(ob_sm_id);
312
313 XCloseDisplay(ob_display);
314
315 if (restart) {
316 if (restart_path != NULL) {
317 int argcp;
318 char **argvp;
319 GError *err = NULL;
320
321 /* run other shit */
322 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
323 execvp(argvp[0], argvp);
324 g_strfreev(argvp);
325 } else {
326 g_warning("failed to execute '%s': %s", restart_path,
327 err->message);
328 }
329 }
330
331 /* re-run me */
332 execvp(argv[0], argv); /* try how we were run */
333 }
334
335 return 0;
336 }
337
338 static void signal_handler(int signal, gpointer data)
339 {
340 if (signal == SIGUSR1) {
341 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
342 ob_restart();
343 } else if (signal == SIGUSR2) {
344 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
345 ob_reconfigure();
346 } else {
347 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
348 ob_exit();
349 }
350 }
351
352 static void print_version()
353 {
354 g_print("Openbox %s\n\n", PACKAGE_VERSION);
355 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
356 g_print("This is free software, and you are welcome to redistribute it\n");
357 g_print("under certain conditions. See the file COPYING for details.\n\n");
358 }
359
360 static void print_help()
361 {
362 print_version();
363 g_print("Syntax: openbox [options]\n\n");
364 g_print("Options:\n\n");
365 #ifdef USE_SM
366 g_print(" --sm-disable Disable connection to session manager\n");
367 g_print(" --sm-client-id ID Specify session management ID\n");
368 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
369 " from\n");
370 #endif
371 g_print(" --replace Replace the currently running window "
372 "manager\n");
373 g_print(" --help Display this help and exit\n");
374 g_print(" --version Display the version and exit\n");
375 g_print(" --sync Run in synchronous mode (this is slow and\n"
376 " meant for debugging X routines)\n");
377 g_print(" --debug Display debugging output\n");
378 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
379 }
380
381 static void parse_args(int argc, char **argv)
382 {
383 int i;
384
385 for (i = 1; i < argc; ++i) {
386 if (!strcmp(argv[i], "--version")) {
387 print_version();
388 exit(0);
389 } else if (!strcmp(argv[i], "--help")) {
390 print_help();
391 exit(0);
392 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
393 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
394 } else if (!strcmp(argv[i], "--replace")) {
395 ob_replace_wm = TRUE;
396 } else if (!strcmp(argv[i], "--sync")) {
397 xsync = TRUE;
398 } else if (!strcmp(argv[i], "--debug")) {
399 ob_debug_show_output(TRUE);
400 #ifdef USE_SM
401 } else if (!strcmp(argv[i], "--sm-client-id")) {
402 if (i == argc - 1) /* no args left */
403 g_printerr(_("--sm-client-id requires an argument\n"));
404 else
405 ob_sm_id = g_strdup(argv[++i]);
406 } else if (!strcmp(argv[i], "--sm-save-file")) {
407 if (i == argc - 1) /* no args left */
408 g_printerr(_("--sm-save-file requires an argument\n"));
409 else
410 sm_save_file = argv[++i];
411 } else if (!strcmp(argv[i], "--sm-disable")) {
412 ob_sm_use = FALSE;
413 #endif
414 } else {
415 g_printerr("Invalid option: '%s'\n\n", argv[i]);
416 print_help();
417 exit(1);
418 }
419 }
420 }
421
422 void ob_exit_with_error(gchar *msg)
423 {
424 g_critical(msg);
425 session_shutdown();
426 exit(EXIT_FAILURE);
427 }
428
429 void ob_restart_other(const gchar *path)
430 {
431 restart_path = g_strdup(path);
432 ob_restart();
433 }
434
435 void ob_restart()
436 {
437 restart = TRUE;
438 ob_exit();
439 }
440
441 void ob_exit()
442 {
443 ob_main_loop_exit(ob_main_loop);
444 }
445
446 Cursor ob_cursor(ObCursor cursor)
447 {
448 g_assert(cursor < OB_NUM_CURSORS);
449 return cursors[cursor];
450 }
451
452 KeyCode ob_keycode(ObKey key)
453 {
454 g_assert(key < OB_NUM_KEYS);
455 return keys[key];
456 }
457
458 ObState ob_state()
459 {
460 return state;
461 }
462
463 gchar *ob_expand_tilde(const gchar *f)
464 {
465 gchar **spl;
466 gchar *ret, *mid;
467
468 if (!f)
469 return NULL;
470 spl = g_strsplit(f, "~", 0);
471 mid = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, NULL);
472 ret = g_strjoinv(mid, spl);
473 g_free(mid);
474 g_strfreev(spl);
475 return ret;
476 }
477
478 void ob_reconfigure()
479 {
480 reconfigure = TRUE;
481 ob_exit();
482 }
This page took 0.05544 seconds and 5 git commands to generate.