]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
kill the window mapping.. its not doing anything anyways? whatever..
[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 Popup *reconfig_popup;
199
200 if (reconfigure) {
201 gint w, h;
202
203 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
204 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
205
206 reconfig_popup = popup_new(FALSE);
207 popup_position(reconfig_popup, CenterGravity, w / 2, h / 2);
208 popup_show(reconfig_popup, _("Reloading . . ."), NULL);
209
210 XFlush(ob_display);
211 }
212
213 event_startup(reconfigure);
214 grab_startup(reconfigure);
215 /* focus_backup is used for stacking, so this needs to come before
216 anything that calls stacking_add */
217 focus_startup(reconfigure);
218 window_startup(reconfigure);
219 sn_startup(reconfigure);
220
221 {
222 ObParseInst *i;
223 xmlDocPtr doc;
224 xmlNodePtr node;
225
226 /* startup the parsing so everything can register sections
227 of the rc */
228 i = parse_startup();
229
230 config_startup(i);
231 /* parse/load user options */
232 if (parse_load_rc(&doc, &node))
233 parse_tree(i, doc, node->xmlChildrenNode);
234 /* we're done with parsing now, kill it */
235 xmlFreeDoc(doc);
236 parse_shutdown(i);
237 }
238
239 /* load the theme specified in the rc file */
240 {
241 RrTheme *theme;
242 if ((theme = RrThemeNew(ob_rr_inst, config_theme)))
243 ob_rr_theme = theme;
244 if (ob_rr_theme == NULL)
245 ob_exit_with_error("Unable to load a theme.");
246 }
247
248 moveresize_startup(reconfigure);
249 screen_startup(reconfigure);
250 group_startup(reconfigure);
251 client_startup(reconfigure);
252 dock_startup(reconfigure);
253 keyboard_startup(reconfigure);
254 mouse_startup(reconfigure);
255 menu_startup(reconfigure);
256
257 if (!reconfigure) {
258 /* get all the existing windows */
259 client_manage_all();
260 } else {
261 GList *it;
262
263 /* redecorate all existing windows */
264 for (it = client_list; it; it = g_list_next(it)) {
265 ObClient *c = it->data;
266 frame_adjust_theme(c->frame);
267 }
268
269 XFlush(ob_display);
270
271 popup_free(reconfig_popup);
272 }
273
274 reconfigure = FALSE;
275
276 state = OB_STATE_RUNNING;
277 ob_main_loop_run(ob_main_loop);
278 state = OB_STATE_EXITING;
279
280 if (!reconfigure) {
281 dock_remove_all();
282 client_unmanage_all();
283 }
284
285 menu_shutdown(reconfigure);
286 mouse_shutdown(reconfigure);
287 keyboard_shutdown(reconfigure);
288 dock_shutdown(reconfigure);
289 client_shutdown(reconfigure);
290 group_shutdown(reconfigure);
291 screen_shutdown(reconfigure);
292 focus_shutdown(reconfigure);
293 moveresize_shutdown(reconfigure);
294 sn_shutdown(reconfigure);
295 window_shutdown(reconfigure);
296 grab_shutdown(reconfigure);
297 event_shutdown(reconfigure);
298 config_shutdown();
299 } while (reconfigure);
300 }
301
302 RrThemeFree(ob_rr_theme);
303 RrInstanceFree(ob_rr_inst);
304
305 session_shutdown();
306 g_free(ob_sm_id);
307
308 XCloseDisplay(ob_display);
309
310 if (restart) {
311 if (restart_path != NULL) {
312 int argcp;
313 char **argvp;
314 GError *err = NULL;
315
316 /* run other shit */
317 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
318 execvp(argvp[0], argvp);
319 g_strfreev(argvp);
320 } else {
321 g_warning("failed to execute '%s': %s", restart_path,
322 err->message);
323 }
324 }
325
326 /* re-run me */
327 execvp(argv[0], argv); /* try how we were run */
328 }
329
330 return 0;
331 }
332
333 static void signal_handler(int signal, gpointer data)
334 {
335 if (signal == SIGUSR1) {
336 fprintf(stderr, "Caught signal %d. Restarting.\n", signal);
337 ob_restart();
338 } else if (signal == SIGUSR2) {
339 fprintf(stderr, "Caught signal %d. Reconfiguring.\n", signal);
340 ob_reconfigure();
341 } else {
342 fprintf(stderr, "Caught signal %d. Exiting.\n", signal);
343 ob_exit();
344 }
345 }
346
347 static void print_version()
348 {
349 g_print("Openbox %s\n\n", PACKAGE_VERSION);
350 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
351 g_print("This is free software, and you are welcome to redistribute it\n");
352 g_print("under certain conditions. See the file COPYING for details.\n\n");
353 }
354
355 static void print_help()
356 {
357 print_version();
358 g_print("Syntax: openbox [options]\n\n");
359 g_print("Options:\n\n");
360 #ifdef USE_SM
361 g_print(" --sm-disable Disable connection to session manager\n");
362 g_print(" --sm-client-id ID Specify session management ID\n");
363 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
364 " from\n");
365 #endif
366 g_print(" --replace Replace the currently running window "
367 "manager\n");
368 g_print(" --help Display this help and exit\n");
369 g_print(" --version Display the version and exit\n");
370 g_print(" --sync Run in synchronous mode (this is slow and\n"
371 " meant for debugging X routines)\n");
372 g_print(" --debug Display debugging output\n");
373 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
374 }
375
376 static void parse_args(int argc, char **argv)
377 {
378 int i;
379
380 for (i = 1; i < argc; ++i) {
381 if (!strcmp(argv[i], "--version")) {
382 print_version();
383 exit(0);
384 } else if (!strcmp(argv[i], "--help")) {
385 print_help();
386 exit(0);
387 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
388 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
389 } else if (!strcmp(argv[i], "--replace")) {
390 ob_replace_wm = TRUE;
391 } else if (!strcmp(argv[i], "--sync")) {
392 xsync = TRUE;
393 } else if (!strcmp(argv[i], "--debug")) {
394 ob_debug_show_output(TRUE);
395 #ifdef USE_SM
396 } else if (!strcmp(argv[i], "--sm-client-id")) {
397 if (i == argc - 1) /* no args left */
398 g_printerr(_("--sm-client-id requires an argument\n"));
399 else
400 ob_sm_id = g_strdup(argv[++i]);
401 } else if (!strcmp(argv[i], "--sm-save-file")) {
402 if (i == argc - 1) /* no args left */
403 g_printerr(_("--sm-save-file requires an argument\n"));
404 else
405 sm_save_file = argv[++i];
406 } else if (!strcmp(argv[i], "--sm-disable")) {
407 ob_sm_use = FALSE;
408 #endif
409 } else {
410 g_printerr("Invalid option: '%s'\n\n", argv[i]);
411 print_help();
412 exit(1);
413 }
414 }
415 }
416
417 void ob_exit_with_error(gchar *msg)
418 {
419 g_critical(msg);
420 session_shutdown();
421 exit(EXIT_FAILURE);
422 }
423
424 void ob_restart_other(const gchar *path)
425 {
426 restart_path = g_strdup(path);
427 ob_restart();
428 }
429
430 void ob_restart()
431 {
432 restart = TRUE;
433 ob_exit();
434 }
435
436 void ob_exit()
437 {
438 ob_main_loop_exit(ob_main_loop);
439 }
440
441 Cursor ob_cursor(ObCursor cursor)
442 {
443 g_assert(cursor < OB_NUM_CURSORS);
444 return cursors[cursor];
445 }
446
447 KeyCode ob_keycode(ObKey key)
448 {
449 g_assert(key < OB_NUM_KEYS);
450 return keys[key];
451 }
452
453 ObState ob_state()
454 {
455 return state;
456 }
457
458 gchar *ob_expand_tilde(const gchar *f)
459 {
460 gchar **spl;
461 gchar *ret, *mid;
462
463 if (!f)
464 return NULL;
465 spl = g_strsplit(f, "~", 0);
466 mid = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, NULL);
467 ret = g_strjoinv(mid, spl);
468 g_free(mid);
469 g_strfreev(spl);
470 return ret;
471 }
472
473 void ob_reconfigure()
474 {
475 reconfigure = TRUE;
476 ob_exit();
477 }
This page took 0.055456 seconds and 5 git commands to generate.