]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
07f1f992190e9fbbedef728b17582c162bc8747f
[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 "startup.h"
11 #include "screen.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 #ifdef USE_LIBSN
131 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
132 #endif
133
134 ob_screen = DefaultScreen(ob_display);
135
136 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
137 if (ob_rr_inst == NULL)
138 ob_exit_with_error("Failed to initialize the render library.");
139
140 /* XXX fork self onto other screens */
141
142 XSynchronize(ob_display, xsync);
143
144 /* check for locale support */
145 if (!XSupportsLocale())
146 g_warning("X server does not support locale.");
147 if (!XSetLocaleModifiers(""))
148 g_warning("Cannot set locale modifiers for the X server.");
149
150 /* set our error handler */
151 XSetErrorHandler(xerror_handler);
152
153 /* set the DISPLAY environment variable for any lauched children, to the
154 display we're using, so they open in the right place. */
155 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
156
157 /* create available cursors */
158 cursors[OB_CURSOR_POINTER] =
159 XCreateFontCursor(ob_display, XC_left_ptr);
160 cursors[OB_CURSOR_BUSY] =
161 XCreateFontCursor(ob_display, XC_watch);
162 cursors[OB_CURSOR_MOVE] =
163 XCreateFontCursor(ob_display, XC_fleur);
164 cursors[OB_CURSOR_NORTH] =
165 XCreateFontCursor(ob_display, XC_top_side);
166 cursors[OB_CURSOR_NORTHEAST] =
167 XCreateFontCursor(ob_display, XC_top_right_corner);
168 cursors[OB_CURSOR_EAST] =
169 XCreateFontCursor(ob_display, XC_right_side);
170 cursors[OB_CURSOR_SOUTHEAST] =
171 XCreateFontCursor(ob_display, XC_bottom_right_corner);
172 cursors[OB_CURSOR_SOUTH] =
173 XCreateFontCursor(ob_display, XC_bottom_side);
174 cursors[OB_CURSOR_SOUTHWEST] =
175 XCreateFontCursor(ob_display, XC_bottom_left_corner);
176 cursors[OB_CURSOR_WEST] =
177 XCreateFontCursor(ob_display, XC_left_side);
178 cursors[OB_CURSOR_NORTHWEST] =
179 XCreateFontCursor(ob_display, XC_top_left_corner);
180
181 /* create available keycodes */
182 keys[OB_KEY_RETURN] =
183 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
184 keys[OB_KEY_ESCAPE] =
185 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
186 keys[OB_KEY_LEFT] =
187 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
188 keys[OB_KEY_RIGHT] =
189 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
190 keys[OB_KEY_UP] =
191 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
192 keys[OB_KEY_DOWN] =
193 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
194
195 prop_startup(); /* get atoms values for the display */
196 extensions_query_all(); /* find which extensions are present */
197
198 /* save stuff that we can use to restore state */
199 startup_save();
200
201 if (screen_annex()) { /* it will be ours! */
202 do {
203 event_startup(reconfigure);
204 grab_startup(reconfigure);
205 /* focus_backup is used for stacking, so this needs to come before
206 anything that calls stacking_add */
207 focus_startup(reconfigure);
208 window_startup(reconfigure);
209
210 {
211 ObParseInst *i;
212 xmlDocPtr doc;
213 xmlNodePtr node;
214
215 /* startup the parsing so everything can register sections
216 of the rc */
217 i = parse_startup();
218
219 config_startup(i);
220 /* parse/load user options */
221 if (parse_load_rc(&doc, &node))
222 parse_tree(i, doc, node->xmlChildrenNode);
223 /* we're done with parsing now, kill it */
224 xmlFreeDoc(doc);
225 parse_shutdown(i);
226 }
227
228 /* load the theme specified in the rc file */
229 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
230 if (ob_rr_theme == NULL)
231 ob_exit_with_error("Unable to load a theme.");
232
233 moveresize_startup(reconfigure);
234 screen_startup(reconfigure);
235 group_startup(reconfigure);
236 client_startup(reconfigure);
237 dock_startup(reconfigure);
238 keyboard_startup(reconfigure);
239 mouse_startup(reconfigure);
240 menu_startup(reconfigure);
241
242 if (!reconfigure) {
243 /* get all the existing windows */
244 client_manage_all();
245 } else {
246 GList *it;
247
248 /* redecorate all existing windows */
249 for (it = client_list; it; it = g_list_next(it)) {
250 ObClient *c = it->data;
251 frame_adjust_theme(c->frame);
252 }
253 }
254
255 reconfigure = FALSE;
256
257 state = OB_STATE_RUNNING;
258 ob_main_loop_run(ob_main_loop);
259 state = OB_STATE_EXITING;
260
261 if (!reconfigure) {
262 dock_remove_all();
263 client_unmanage_all();
264 }
265
266 menu_shutdown(reconfigure);
267 mouse_shutdown(reconfigure);
268 keyboard_shutdown(reconfigure);
269 dock_shutdown(reconfigure);
270 client_shutdown(reconfigure);
271 group_shutdown(reconfigure);
272 screen_shutdown(reconfigure);
273 focus_shutdown(reconfigure);
274 moveresize_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 #ifdef USE_LIBSN
289 sn_display_unref(ob_sn_display);
290 #endif
291
292 XCloseDisplay(ob_display);
293
294 if (restart) {
295 if (restart_path != NULL) {
296 int argcp;
297 char **argvp;
298 GError *err = NULL;
299
300 /* run other shit */
301 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
302 execvp(argvp[0], argvp);
303 g_strfreev(argvp);
304 } else {
305 g_warning("failed to execute '%s': %s", restart_path,
306 err->message);
307 }
308 }
309
310 /* re-run me */
311 execvp(argv[0], argv); /* try how we were run */
312 }
313
314 return 0;
315 }
316
317 static void signal_handler(int signal, gpointer data)
318 {
319 if (signal == SIGUSR1) {
320 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
321 ob_restart();
322 } else if (signal == SIGUSR2) {
323 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
324 ob_reconfigure();
325 } else {
326 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
327 ob_exit();
328 }
329 }
330
331 static void print_version()
332 {
333 g_print("Openbox %s\n\n", PACKAGE_VERSION);
334 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
335 g_print("This is free software, and you are welcome to redistribute it\n");
336 g_print("under certain conditions. See the file COPYING for details.\n\n");
337 }
338
339 static void print_help()
340 {
341 print_version();
342 g_print("Syntax: openbox [options]\n\n");
343 g_print("Options:\n\n");
344 #ifdef USE_SM
345 g_print(" --sm-disable Disable connection to session manager\n");
346 g_print(" --sm-client-id ID Specify session management ID\n");
347 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
348 " from\n");
349 #endif
350 g_print(" --replace Replace the currently running window "
351 "manager\n");
352 g_print(" --help Display this help and exit\n");
353 g_print(" --version Display the version and exit\n");
354 g_print(" --sync Run in synchronous mode (this is slow and\n"
355 " meant for debugging X routines)\n");
356 g_print(" --debug Display debugging output\n");
357 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
358 }
359
360 static void parse_args(int argc, char **argv)
361 {
362 int i;
363
364 for (i = 1; i < argc; ++i) {
365 if (!strcmp(argv[i], "--version")) {
366 print_version();
367 exit(0);
368 } else if (!strcmp(argv[i], "--help")) {
369 print_help();
370 exit(0);
371 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
372 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
373 } else if (!strcmp(argv[i], "--replace")) {
374 ob_replace_wm = TRUE;
375 } else if (!strcmp(argv[i], "--sync")) {
376 xsync = TRUE;
377 } else if (!strcmp(argv[i], "--debug")) {
378 ob_debug_show_output(TRUE);
379 #ifdef USE_SM
380 } else if (!strcmp(argv[i], "--sm-client-id")) {
381 if (i == argc - 1) /* no args left */
382 g_printerr(_("--sm-client-id requires an argument\n"));
383 else
384 ob_sm_id = g_strdup(argv[++i]);
385 } else if (!strcmp(argv[i], "--sm-save-file")) {
386 if (i == argc - 1) /* no args left */
387 g_printerr(_("--sm-save-file requires an argument\n"));
388 else
389 sm_save_file = argv[++i];
390 } else if (!strcmp(argv[i], "--sm-disable")) {
391 ob_sm_use = FALSE;
392 #endif
393 } else {
394 g_printerr("Invalid option: '%s'\n\n", argv[i]);
395 print_help();
396 exit(1);
397 }
398 }
399 }
400
401 void ob_exit_with_error(gchar *msg)
402 {
403 g_critical(msg);
404 session_shutdown();
405 exit(EXIT_FAILURE);
406 }
407
408 void ob_restart_other(const gchar *path)
409 {
410 restart_path = g_strdup(path);
411 ob_restart();
412 }
413
414 void ob_restart()
415 {
416 restart = TRUE;
417 ob_exit();
418 }
419
420 void ob_exit()
421 {
422 ob_main_loop_exit(ob_main_loop);
423 }
424
425 Cursor ob_cursor(ObCursor cursor)
426 {
427 g_assert(cursor < OB_NUM_CURSORS);
428 return cursors[cursor];
429 }
430
431 KeyCode ob_keycode(ObKey key)
432 {
433 g_assert(key < OB_NUM_KEYS);
434 return keys[key];
435 }
436
437 ObState ob_state()
438 {
439 return state;
440 }
441
442 gchar *ob_expand_tilde(const gchar *f)
443 {
444 gchar **spl;
445 gchar *ret, *mid;
446
447 if (!f)
448 return NULL;
449 spl = g_strsplit(f, "~", 0);
450 mid = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, NULL);
451 ret = g_strjoinv(mid, spl);
452 g_free(mid);
453 g_strfreev(spl);
454 return ret;
455 }
456
457 void ob_reconfigure()
458 {
459 reconfigure = TRUE;
460 ob_exit();
461 }
This page took 0.057658 seconds and 3 git commands to generate.