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