]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
move config option loading for the kernel into config.c/h
[dana/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "client.h"
4 #include "dispatch.h"
5 #include "xerror.h"
6 #include "prop.h"
7 #include "screen.h"
8 #include "focus.h"
9 #include "extensions.h"
10 #include "parse.h"
11 #include "grab.h"
12 #include "engine.h"
13 #include "plugin.h"
14 #include "timer.h"
15 #include "group.h"
16 #include "config.h"
17 #include "gettext.h"
18 #include "../render/render.h"
19 #include "../render/font.h"
20
21 #ifdef HAVE_FCNTL_H
22 #  include <fcntl.h>
23 #endif
24 #ifdef HAVE_SIGNAL_H
25 #  include <signal.h>
26 #endif
27 #ifdef HAVE_STDLIB_H
28 #  include <stdlib.h>
29 #endif
30 #ifdef HAVE_SYS_WAIT_H
31 #  include <sys/types.h>
32 #  include <sys/wait.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
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     sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
94
95     /* anything that died while we were restarting won't give us a SIGCHLD */
96     while (waitpid(-1, NULL, WNOHANG) > 0);
97
98     /* create the ~/.openbox dir */
99     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
100     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
101                  S_IROTH | S_IWOTH | S_IXOTH));
102     g_free(path);
103     /* create the ~/.openbox/themes dir */
104     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
105     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
106                  S_IROTH | S_IWOTH | S_IXOTH));
107     g_free(path);
108      
109     /* parse out command line args */
110     parse_args(argc, argv);
111
112     ob_display = XOpenDisplay(NULL);
113     if (ob_display == NULL) {
114         /* print a message and exit */
115         g_critical("Failed to open the display.");
116         exit(1);
117     }
118     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
119         /* print a message and exit */
120         g_critical("Failed to set display as close-on-exec.");
121         exit(1);
122     }
123           
124     ob_screen = DefaultScreen(ob_display);
125     ob_root = RootWindow(ob_display, ob_screen);
126
127     /* XXX fork self onto other screens */
128      
129     XSynchronize(ob_display, ob_sync);
130
131     /* check for locale support */
132     if (!XSupportsLocale())
133         g_warning("X server does not support locale.");
134     if (!XSetLocaleModifiers(""))
135         g_warning("Cannot set locale modifiers for the X server.");
136
137     /* set our error handler */
138     XSetErrorHandler(xerror_handler);
139
140     /* set the DISPLAY environment variable for any lauched children, to the
141        display we're using, so they open in the right place. */
142     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
143
144     ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
145     ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
146     ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
147
148     prop_startup(); /* get atoms values for the display */
149     extensions_query_all(); /* find which extensions are present */
150
151     if (screen_annex()) { /* it will be ours! */
152         /* startup the parsing so everything can register sections of the rc */
153         parse_startup();
154
155         /* anything that is going to read data from the rc file needs to be 
156            in this group */
157         timer_startup();
158         render_startup();
159         font_startup();
160         event_startup();
161         grab_startup();
162         engine_startup();
163         plugin_startup();
164         /* load the plugins specified in the pluginrc */
165         plugin_loadall();
166
167         /* set up the kernel config shit */
168         config_startup();
169         /* parse/load user options */
170         parse_rc();
171         /* we're done with parsing now, kill it */
172         parse_shutdown();
173
174         /* load the engine specified in the rc */
175         engine_load();
176
177         focus_startup();
178         screen_startup();
179         group_startup();
180         client_startup();
181
182         /* call startup for all the plugins */
183         plugin_startall();
184
185         /* get all the existing windows */
186         client_manage_all();
187
188         ob_state = State_Running;
189         while (!ob_shutdown)
190             event_loop();
191         ob_state = State_Exiting;
192
193         client_unmanage_all();
194
195         plugin_shutdown(); /* calls all the plugins' shutdown functions */
196         client_shutdown();
197         group_shutdown();
198         screen_shutdown();
199         focus_shutdown();
200         engine_shutdown();
201         grab_shutdown();
202         event_shutdown();
203         render_shutdown();
204         timer_shutdown();
205         config_shutdown();
206     }
207
208     dispatch_shutdown();
209
210     XCloseDisplay(ob_display);
211
212     if (ob_restart) {
213         if (ob_restart_path != NULL) {
214             int argcp;
215             char **argvp;
216             GError *err = NULL;
217
218             /* run other shit */
219             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
220                 execvp(argvp[0], argvp);
221                 g_strfreev(argvp);
222             } else {
223                 g_warning("failed to execute '%s': %s", ob_restart_path,
224                           err->message);
225             }
226         }
227
228         /* re-run me */
229         execvp(argv[0], argv); /* try how we were run */
230         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
231     }
232      
233     return 0;
234 }
235
236 void signal_handler(const ObEvent *e, void *data)
237 {
238     int s;
239
240     s = e->data.s.signal;
241     switch (s) {
242     case SIGUSR1:
243         g_message("Caught SIGUSR1 signal. Restarting.");
244         ob_shutdown = ob_restart = TRUE;
245         break;
246
247     case SIGCHLD:
248         wait(NULL);
249         break;
250
251     case SIGHUP:
252     case SIGINT:
253     case SIGTERM:
254     case SIGPIPE:
255         g_message("Caught signal %d. Exiting.", s);
256         ob_shutdown = TRUE;
257         break;
258
259     case SIGFPE:
260     case SIGSEGV:
261         g_error("Caught signal %d. Aborting and dumping core.", s);
262     }
263 }
264
265 void print_version()
266 {
267     g_print("Openbox %s\n\n", PACKAGE_VERSION);
268     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
269     g_print("This is free software, and you are welcome to redistribute it\n");
270     g_print("under certain conditions. See the file COPYING for details.\n\n");
271 }
272
273 void print_help()
274 {
275     print_version();
276     g_print("Syntax: %s [options]\n\n", BINARY);
277     g_print("Options:\n\n");
278     g_print("  -rc PATH     Specify the path to the rc file to use\n");
279     g_print("  -help        Display this help and exit\n");
280     g_print("  -version     Display the version and exit\n");
281     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
282             "               for debugging X routines)\n");
283     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
284 }
285
286 void parse_args(int argc, char **argv)
287 {
288     int i;
289
290     for (i = 1; i < argc; ++i) {
291         if (!strcmp(argv[i], "-version")) {
292             print_version();
293             exit(0);
294         } else if (!strcmp(argv[i], "-help")) {
295             print_help();
296             exit(0);
297         } else if (!strcmp(argv[i], "-sync")) {
298             ob_sync = TRUE;
299         } else if (!strcmp(argv[i], "-rc")) {
300             if (i == argc - 1) /* no args left */
301                 g_printerr("-rc requires an argument\n");
302             else
303                 ob_rc_path = argv[++i];
304         } else {
305             g_printerr("Invalid option: '%s'\n\n", argv[i]);
306             print_help();
307             exit(1);
308         }
309     }
310 }