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