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