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