]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
let the client menu open below the top of the window, just limit it at the titlebar
[mikachu/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "dock.h"
3 #include "event.h"
4 #include "menu.h"
5 #include "client.h"
6 #include "dispatch.h"
7 #include "xerror.h"
8 #include "prop.h"
9 #include "startup.h"
10 #include "screen.h"
11 #include "focus.h"
12 #include "moveresize.h"
13 #include "frame.h"
14 #include "extensions.h"
15 #include "parse.h"
16 #include "grab.h"
17 #include "plugin.h"
18 #include "timer.h"
19 #include "group.h"
20 #include "config.h"
21 #include "gettext.h"
22 #include "render/render.h"
23 #include "render/font.h"
24 #include "render/theme.h"
25
26 #ifdef HAVE_FCNTL_H
27 #  include <fcntl.h>
28 #endif
29 #ifdef HAVE_SIGNAL_H
30 #  include <signal.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif
35 #ifdef HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 #  include <sys/stat.h>
43 #  include <sys/types.h>
44 #endif
45
46 #include <X11/cursorfont.h>
47
48 Display *ob_display  = NULL;
49 int      ob_screen;
50 Window   ob_root;
51 State    ob_state;
52 gboolean ob_shutdown = FALSE;
53 gboolean ob_restart  = FALSE;
54 char    *ob_restart_path = NULL;
55 gboolean ob_remote   = TRUE;
56 gboolean ob_sync     = FALSE;
57 Cursors  ob_cursors;
58 char    *ob_rc_path  = NULL;
59
60 void signal_handler(const ObEvent *e, void *data);
61 void parse_args(int argc, char **argv);
62
63 int main(int argc, char **argv)
64 {
65     struct sigaction action;
66     sigset_t sigset;
67     char *path;
68     char *theme;
69
70     ob_state = State_Starting;
71
72     /* initialize the locale */
73     if (!setlocale(LC_ALL, ""))
74         g_warning("Couldn't set locale from environment.\n");
75     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
76     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
77     textdomain(PACKAGE_NAME);
78
79     /* start our event dispatcher and register for signals */
80     dispatch_startup();
81     dispatch_register(Event_Signal, signal_handler, NULL);
82
83     /* set up signal handler */
84     sigemptyset(&sigset);
85     action.sa_handler = dispatch_signal;
86     action.sa_mask = sigset;
87     action.sa_flags = SA_NOCLDSTOP;
88     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
89     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
90     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
91     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
92     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
93     sigaction(SIGINT, &action, (struct sigaction *) NULL);
94     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
95
96     /* create the ~/.openbox dir */
97     path = g_build_filename(g_get_home_dir(), ".openbox", 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     /* create the ~/.openbox/themes dir */
102     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
103     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
104                  S_IROTH | S_IWOTH | S_IXOTH));
105     g_free(path);
106      
107     /* parse out command line args */
108     parse_args(argc, argv);
109
110     ob_display = XOpenDisplay(NULL);
111     if (ob_display == NULL) {
112         /* print a message and exit */
113         g_critical("Failed to open the display.");
114         exit(1);
115     }
116     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
117         /* print a message and exit */
118         g_critical("Failed to set display as close-on-exec.");
119         exit(1);
120     }
121
122 #ifdef USE_LIBSN
123     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
124 #endif
125
126     ob_screen = DefaultScreen(ob_display);
127     ob_root = RootWindow(ob_display, ob_screen);
128
129     /* XXX fork self onto other screens */
130      
131     XSynchronize(ob_display, ob_sync);
132
133     /* check for locale support */
134     if (!XSupportsLocale())
135         g_warning("X server does not support locale.");
136     if (!XSetLocaleModifiers(""))
137         g_warning("Cannot set locale modifiers for the X server.");
138
139     /* set our error handler */
140     XSetErrorHandler(xerror_handler);
141
142     /* set the DISPLAY environment variable for any lauched children, to the
143        display we're using, so they open in the right place. */
144     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
145
146     ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
147     ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
148     ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
149     ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
150     ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
151     ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
152     ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
153     ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
154     ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
155     ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
156     ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
157
158     prop_startup(); /* get atoms values for the display */
159     extensions_query_all(); /* find which extensions are present */
160
161     /* save stuff that we can use to restore state */
162     startup_save();
163
164     if (screen_annex()) { /* it will be ours! */
165         /* startup the parsing so everything can register sections of the rc */
166         parse_startup();
167
168         /* anything that is going to read data from the rc file needs to be 
169            in this group */
170         timer_startup();
171         render_startup();
172         font_startup();
173         theme_startup();
174         event_startup();
175         grab_startup();
176         plugin_startup();
177         /* load the plugins specified in the pluginrc */
178         plugin_loadall();
179
180         /* set up the kernel config shit */
181         config_startup();
182         /* parse/load user options */
183         parse_rc();
184         /* we're done with parsing now, kill it */
185         parse_shutdown();
186
187         /* load the theme specified in the rc file */
188         theme = theme_load(config_theme);
189         g_free(theme);
190         if (!theme) return 1;
191
192         window_startup();
193         menu_startup();
194         frame_startup();
195         moveresize_startup();
196         focus_startup();
197         screen_startup();
198         group_startup();
199         client_startup();
200         dock_startup();
201
202         /* call startup for all the plugins */
203         plugin_startall();
204
205         /* get all the existing windows */
206         client_manage_all();
207
208         ob_state = State_Running;
209         while (!ob_shutdown)
210             event_loop();
211         ob_state = State_Exiting;
212
213         dock_remove_all();
214         client_unmanage_all();
215
216         plugin_shutdown(); /* calls all the plugins' shutdown functions */
217         dock_shutdown();
218         client_shutdown();
219         group_shutdown();
220         screen_shutdown();
221         focus_shutdown();
222         moveresize_shutdown();
223         frame_shutdown();
224         menu_shutdown();
225         window_shutdown();
226         grab_shutdown();
227         event_shutdown();
228         theme_shutdown();
229         render_shutdown();
230         timer_shutdown();
231         config_shutdown();
232     }
233
234     dispatch_shutdown();
235
236     XCloseDisplay(ob_display);
237
238     if (ob_restart) {
239         if (ob_restart_path != NULL) {
240             int argcp;
241             char **argvp;
242             GError *err = NULL;
243
244             /* run other shit */
245             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
246                 execvp(argvp[0], argvp);
247                 g_strfreev(argvp);
248             } else {
249                 g_warning("failed to execute '%s': %s", ob_restart_path,
250                           err->message);
251             }
252         }
253
254         /* re-run me */
255         execvp(argv[0], argv); /* try how we were run */
256         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
257     }
258      
259     return 0;
260 }
261
262 void signal_handler(const ObEvent *e, void *data)
263 {
264     int s;
265
266     s = e->data.s.signal;
267     switch (s) {
268     case SIGUSR1:
269         g_message("Caught SIGUSR1 signal. Restarting.");
270         ob_shutdown = ob_restart = TRUE;
271         break;
272
273     case SIGHUP:
274     case SIGINT:
275     case SIGTERM:
276     case SIGPIPE:
277         g_message("Caught signal %d. Exiting.", s);
278         ob_shutdown = TRUE;
279         break;
280
281     case SIGFPE:
282     case SIGSEGV:
283         g_message("Caught signal %d. Aborting and dumping core.", s);
284         abort();
285     }
286 }
287
288 void print_version()
289 {
290     g_print("Openbox %s\n\n", PACKAGE_VERSION);
291     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
292     g_print("This is free software, and you are welcome to redistribute it\n");
293     g_print("under certain conditions. See the file COPYING for details.\n\n");
294 }
295
296 void print_help()
297 {
298     print_version();
299     g_print("Syntax: %s [options]\n\n", BINARY);
300     g_print("Options:\n\n");
301     g_print("  -rc PATH     Specify the path to the rc file to use\n");
302     g_print("  -help        Display this help and exit\n");
303     g_print("  -version     Display the version and exit\n");
304     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
305             "               for debugging X routines)\n");
306     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
307 }
308
309 void parse_args(int argc, char **argv)
310 {
311     int i;
312
313     for (i = 1; i < argc; ++i) {
314         if (!strcmp(argv[i], "-version")) {
315             print_version();
316             exit(0);
317         } else if (!strcmp(argv[i], "-help")) {
318             print_help();
319             exit(0);
320         } else if (!strcmp(argv[i], "-sync")) {
321             ob_sync = TRUE;
322         } else if (!strcmp(argv[i], "-rc")) {
323             if (i == argc - 1) /* no args left */
324                 g_printerr("-rc requires an argument\n");
325             else
326                 ob_rc_path = argv[++i];
327         } else {
328             g_printerr("Invalid option: '%s'\n\n", argv[i]);
329             print_help();
330             exit(1);
331         }
332     }
333 }
334
335 gboolean ob_pointer_pos(int *x, int *y)
336 {
337     Window w;
338     int i;
339     guint u;
340
341     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
342 }