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