]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
more namespacing to Rr*
[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 "render/render.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 RrInstance *ob_rr_inst = NULL;
48 RrTheme    *ob_rr_theme = NULL;
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     xmlDocPtr doc;
70     xmlNodePtr node;
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
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 #ifdef USE_LIBSN
125     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
126 #endif
127
128     ob_screen = DefaultScreen(ob_display);
129     ob_root = RootWindow(ob_display, ob_screen);
130
131     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
132     if (ob_rr_inst == NULL) {
133         g_critical("Failed to initialize the render library.");
134         exit(1);
135     }
136
137     /* XXX fork self onto other screens */
138      
139     XSynchronize(ob_display, ob_sync);
140
141     /* check for locale support */
142     if (!XSupportsLocale())
143         g_warning("X server does not support locale.");
144     if (!XSetLocaleModifiers(""))
145         g_warning("Cannot set locale modifiers for the X server.");
146
147     /* set our error handler */
148     XSetErrorHandler(xerror_handler);
149
150     /* set the DISPLAY environment variable for any lauched children, to the
151        display we're using, so they open in the right place. */
152     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
153
154     ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
155     ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
156     ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
157     ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
158     ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
159     ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
160     ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
161     ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
162     ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
163     ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
164     ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
165
166     prop_startup(); /* get atoms values for the display */
167     extensions_query_all(); /* find which extensions are present */
168
169     /* save stuff that we can use to restore state */
170     startup_save();
171
172     if (screen_annex()) { /* it will be ours! */
173         /* startup the parsing so everything can register sections of the rc */
174         parse_startup();
175
176         /* anything that is going to read data from the rc file needs to be 
177            in this group */
178         timer_startup();
179         event_startup();
180         grab_startup();
181         plugin_startup();
182         /* load the plugins specified in the pluginrc */
183         plugin_loadall();
184
185         /* set up the kernel config shit */
186         config_startup();
187         /* parse/load user options */
188         if (parse_load_rc(&doc, &node))
189             parse_tree(doc, node->xmlChildrenNode, NULL);
190         /* we're done with parsing now, kill it */
191         parse_shutdown();
192
193         /* load the theme specified in the rc file */
194         ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
195         if (ob_rr_theme == NULL) {
196             g_critical("Unable to load a theme.");
197             exit(1);
198         }
199
200         window_startup();
201         menu_startup();
202         frame_startup();
203         moveresize_startup();
204         focus_startup();
205         screen_startup();
206         group_startup();
207         client_startup();
208         dock_startup();
209
210         /* call startup for all the plugins */
211         plugin_startall();
212
213         /* get all the existing windows */
214         client_manage_all();
215
216         ob_state = State_Running;
217         while (!ob_shutdown)
218             event_loop();
219         ob_state = State_Exiting;
220
221         dock_remove_all();
222         client_unmanage_all();
223
224         plugin_shutdown(); /* calls all the plugins' shutdown functions */
225         dock_shutdown();
226         client_shutdown();
227         group_shutdown();
228         screen_shutdown();
229         focus_shutdown();
230         moveresize_shutdown();
231         frame_shutdown();
232         menu_shutdown();
233         window_shutdown();
234         grab_shutdown();
235         event_shutdown();
236         timer_shutdown();
237         config_shutdown();
238     }
239
240     dispatch_shutdown();
241
242     RrThemeFree(ob_rr_theme);
243     RrInstanceFree(ob_rr_inst);
244     XCloseDisplay(ob_display);
245
246     if (ob_restart) {
247         if (ob_restart_path != NULL) {
248             int argcp;
249             char **argvp;
250             GError *err = NULL;
251
252             /* run other shit */
253             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
254                 execvp(argvp[0], argvp);
255                 g_strfreev(argvp);
256             } else {
257                 g_warning("failed to execute '%s': %s", ob_restart_path,
258                           err->message);
259             }
260         }
261
262         /* re-run me */
263         execvp(argv[0], argv); /* try how we were run */
264         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
265     }
266      
267     return 0;
268 }
269
270 void signal_handler(const ObEvent *e, void *data)
271 {
272     int s;
273
274     s = e->data.s.signal;
275     switch (s) {
276     case SIGUSR1:
277         g_message("Caught SIGUSR1 signal. Restarting.");
278         ob_shutdown = ob_restart = TRUE;
279         break;
280
281     case SIGHUP:
282     case SIGINT:
283     case SIGTERM:
284     case SIGPIPE:
285         g_message("Caught signal %d. Exiting.", s);
286         ob_shutdown = TRUE;
287         break;
288
289     case SIGFPE:
290     case SIGSEGV:
291         g_message("Caught signal %d. Aborting and dumping core.", s);
292         abort();
293     }
294 }
295
296 void print_version()
297 {
298     g_print("Openbox %s\n\n", PACKAGE_VERSION);
299     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
300     g_print("This is free software, and you are welcome to redistribute it\n");
301     g_print("under certain conditions. See the file COPYING for details.\n\n");
302 }
303
304 void print_help()
305 {
306     print_version();
307     g_print("Syntax: %s [options]\n\n", BINARY);
308     g_print("Options:\n\n");
309     g_print("  -rc PATH     Specify the path to the rc file to use\n");
310     g_print("  -help        Display this help and exit\n");
311     g_print("  -version     Display the version and exit\n");
312     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
313             "               for debugging X routines)\n");
314     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
315 }
316
317 void parse_args(int argc, char **argv)
318 {
319     int i;
320
321     for (i = 1; i < argc; ++i) {
322         if (!strcmp(argv[i], "-version")) {
323             print_version();
324             exit(0);
325         } else if (!strcmp(argv[i], "-help")) {
326             print_help();
327             exit(0);
328         } else if (!strcmp(argv[i], "-sync")) {
329             ob_sync = TRUE;
330         } else if (!strcmp(argv[i], "-rc")) {
331             if (i == argc - 1) /* no args left */
332                 g_printerr("-rc requires an argument\n");
333             else
334                 ob_rc_path = argv[++i];
335         } else {
336             g_printerr("Invalid option: '%s'\n\n", argv[i]);
337             print_help();
338             exit(1);
339         }
340     }
341 }
342
343 gboolean ob_pointer_pos(int *x, int *y)
344 {
345     Window w;
346     int i;
347     guint u;
348
349     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
350 }