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