]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
all my changes while i was offline.
[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 "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         grab_startup();
170         plugin_startup();
171         /* load the plugins specified in the pluginrc */
172         plugin_loadall();
173
174         /* set up the kernel config shit */
175         config_startup();
176         /* parse/load user options */
177         parse_rc();
178         /* we're done with parsing now, kill it */
179         parse_shutdown();
180
181         /* load the theme specified in the rc file */
182         theme = theme_load(config_theme);
183         g_free(theme);
184         if (!theme) return 1;
185
186         menu_startup();
187         frame_startup();
188         stacking_startup();
189         moveresize_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         moveresize_shutdown();
214         stacking_shutdown();
215         frame_shutdown();
216         menu_shutdown();
217         grab_shutdown();
218         event_shutdown();
219         theme_shutdown();
220         render_shutdown();
221         timer_shutdown();
222         config_shutdown();
223     }
224
225     dispatch_shutdown();
226
227     XCloseDisplay(ob_display);
228
229     if (ob_restart) {
230         if (ob_restart_path != NULL) {
231             int argcp;
232             char **argvp;
233             GError *err = NULL;
234
235             /* run other shit */
236             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
237                 execvp(argvp[0], argvp);
238                 g_strfreev(argvp);
239             } else {
240                 g_warning("failed to execute '%s': %s", ob_restart_path,
241                           err->message);
242             }
243         }
244
245         /* re-run me */
246         execvp(argv[0], argv); /* try how we were run */
247         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
248     }
249      
250     return 0;
251 }
252
253 void signal_handler(const ObEvent *e, void *data)
254 {
255     int s;
256
257     s = e->data.s.signal;
258     switch (s) {
259     case SIGUSR1:
260         g_message("Caught SIGUSR1 signal. Restarting.");
261         ob_shutdown = ob_restart = TRUE;
262         break;
263
264     case SIGHUP:
265     case SIGINT:
266     case SIGTERM:
267     case SIGPIPE:
268         g_message("Caught signal %d. Exiting.", s);
269         ob_shutdown = TRUE;
270         break;
271
272     case SIGFPE:
273     case SIGSEGV:
274         g_error("Caught signal %d. Aborting and dumping core.", s);
275     }
276 }
277
278 void print_version()
279 {
280     g_print("Openbox %s\n\n", PACKAGE_VERSION);
281     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
282     g_print("This is free software, and you are welcome to redistribute it\n");
283     g_print("under certain conditions. See the file COPYING for details.\n\n");
284 }
285
286 void print_help()
287 {
288     print_version();
289     g_print("Syntax: %s [options]\n\n", BINARY);
290     g_print("Options:\n\n");
291     g_print("  -rc PATH     Specify the path to the rc file to use\n");
292     g_print("  -help        Display this help and exit\n");
293     g_print("  -version     Display the version and exit\n");
294     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
295             "               for debugging X routines)\n");
296     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
297 }
298
299 void parse_args(int argc, char **argv)
300 {
301     int i;
302
303     for (i = 1; i < argc; ++i) {
304         if (!strcmp(argv[i], "-version")) {
305             print_version();
306             exit(0);
307         } else if (!strcmp(argv[i], "-help")) {
308             print_help();
309             exit(0);
310         } else if (!strcmp(argv[i], "-sync")) {
311             ob_sync = TRUE;
312         } else if (!strcmp(argv[i], "-rc")) {
313             if (i == argc - 1) /* no args left */
314                 g_printerr("-rc requires an argument\n");
315             else
316                 ob_rc_path = argv[++i];
317         } else {
318             g_printerr("Invalid option: '%s'\n\n", argv[i]);
319             print_help();
320             exit(1);
321         }
322     }
323 }
324
325 gboolean ob_pointer_pos(int *x, int *y)
326 {
327     Window w;
328     int i;
329     guint u;
330
331     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
332 }