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