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