]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
fix brain damage in lineto
[dana/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "dock.h"
3 #include "event.h"
4 #include "menu.h"
5 #include "client.h"
6 #include "dispatch.h"
7 #include "xerror.h"
8 #include "prop.h"
9 #include "startup.h"
10 #include "screen.h"
11 #include "focus.h"
12 #include "moveresize.h"
13 #include "frame.h"
14 #include "extensions.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 "parser/parse.h"
22 #include "render2/render.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 struct RrInstance *ob_render_inst = NULL;
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     xmlDocPtr doc;
69     xmlNodePtr node;
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
97     /* create the ~/.openbox dir */
98     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
99     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
100                  S_IROTH | S_IWOTH | S_IXOTH));
101     g_free(path);
102     /* create the ~/.openbox/themes dir */
103     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", 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      
108     /* parse out command line args */
109     parse_args(argc, argv);
110
111     ob_display = XOpenDisplay(NULL);
112     if (ob_display == NULL) {
113         /* print a message and exit */
114         g_critical("Failed to open the display.");
115         exit(1);
116     }
117     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
118         /* print a message and exit */
119         g_critical("Failed to set display as close-on-exec.");
120         exit(1);
121     }
122
123 #ifdef USE_LIBSN
124     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
125 #endif
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.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
148     ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
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     /* save stuff that we can use to restore state */
163     startup_save();
164
165     if (screen_annex()) { /* it will be ours! */
166         ob_render_inst = RrInstanceNew(ob_display, ob_screen);
167
168         /* startup the parsing so everything can register sections of the rc */
169         parse_startup();
170
171         /* anything that is going to read data from the rc file needs to be 
172            in this group */
173         timer_startup();
174         event_startup();
175         grab_startup();
176         plugin_startup();
177         /* load the plugins specified in the pluginrc */
178         plugin_loadall();
179
180         /* set up the kernel config shit */
181         config_startup();
182         /* parse/load user options */
183         if (parse_load_rc(&doc, &node))
184             parse_tree(doc, node->xmlChildrenNode, NULL);
185         /* we're done with parsing now, kill it */
186         parse_shutdown();
187
188         /* load the theme specified in the rc file */
189 /*
190         theme = theme_load(config_theme);
191         g_free(theme);
192         if (!theme) return 1;
193 */
194
195         window_startup();
196         menu_startup();
197         frame_startup();
198         moveresize_startup();
199         focus_startup();
200         screen_startup();
201         group_startup();
202         client_startup();
203         dock_startup();
204
205         /* call startup for all the plugins */
206         plugin_startall();
207
208         /* get all the existing windows */
209         client_manage_all();
210
211         ob_state = State_Running;
212         while (!ob_shutdown)
213             event_loop();
214         ob_state = State_Exiting;
215
216         dock_remove_all();
217         client_unmanage_all();
218
219         plugin_shutdown(); /* calls all the plugins' shutdown functions */
220         dock_shutdown();
221         client_shutdown();
222         group_shutdown();
223         screen_shutdown();
224         focus_shutdown();
225         moveresize_shutdown();
226         frame_shutdown();
227         menu_shutdown();
228         window_shutdown();
229         grab_shutdown();
230         event_shutdown();
231         timer_shutdown();
232         config_shutdown();
233
234         RrInstanceFree(ob_render_inst);
235     }
236
237     dispatch_shutdown();
238
239     XCloseDisplay(ob_display);
240
241     if (ob_restart) {
242         if (ob_restart_path != NULL) {
243             int argcp;
244             char **argvp;
245             GError *err = NULL;
246
247             /* run other shit */
248             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
249                 execvp(argvp[0], argvp);
250                 g_strfreev(argvp);
251             } else {
252                 g_warning("failed to execute '%s': %s", ob_restart_path,
253                           err->message);
254             }
255         }
256
257         /* re-run me */
258         execvp(argv[0], argv); /* try how we were run */
259         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
260     }
261      
262     return 0;
263 }
264
265 void signal_handler(const ObEvent *e, void *data)
266 {
267     int s;
268
269     s = e->data.s.signal;
270     switch (s) {
271     case SIGUSR1:
272         g_message("Caught SIGUSR1 signal. Restarting.");
273         ob_shutdown = ob_restart = TRUE;
274         break;
275
276     case SIGHUP:
277     case SIGINT:
278     case SIGTERM:
279     case SIGPIPE:
280         g_message("Caught signal %d. Exiting.", s);
281         ob_shutdown = TRUE;
282         break;
283
284     case SIGFPE:
285     case SIGSEGV:
286         g_message("Caught signal %d. Aborting and dumping core.", s);
287         abort();
288     }
289 }
290
291 void print_version()
292 {
293     g_print("Openbox %s\n\n", PACKAGE_VERSION);
294     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
295     g_print("This is free software, and you are welcome to redistribute it\n");
296     g_print("under certain conditions. See the file COPYING for details.\n\n");
297 }
298
299 void print_help()
300 {
301     print_version();
302     g_print("Syntax: %s [options]\n\n", BINARY);
303     g_print("Options:\n\n");
304     g_print("  -rc PATH     Specify the path to the rc file to use\n");
305     g_print("  -help        Display this help and exit\n");
306     g_print("  -version     Display the version and exit\n");
307     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
308             "               for debugging X routines)\n");
309     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
310 }
311
312 void parse_args(int argc, char **argv)
313 {
314     int i;
315
316     for (i = 1; i < argc; ++i) {
317         if (!strcmp(argv[i], "-version")) {
318             print_version();
319             exit(0);
320         } else if (!strcmp(argv[i], "-help")) {
321             print_help();
322             exit(0);
323         } else if (!strcmp(argv[i], "-sync")) {
324             ob_sync = TRUE;
325         } else if (!strcmp(argv[i], "-rc")) {
326             if (i == argc - 1) /* no args left */
327                 g_printerr("-rc requires an argument\n");
328             else
329                 ob_rc_path = argv[++i];
330         } else {
331             g_printerr("Invalid option: '%s'\n\n", argv[i]);
332             print_help();
333             exit(1);
334         }
335     }
336 }
337
338 gboolean ob_pointer_pos(int *x, int *y)
339 {
340     Window w;
341     int i;
342     guint u;
343
344     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
345 }