]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
fixed to accomodate the changes to the render/ API.
[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/font.h"
24 #include "render/theme.h"
25
26 #ifdef HAVE_FCNTL_H
27 #  include <fcntl.h>
28 #endif
29 #ifdef HAVE_SIGNAL_H
30 #  include <signal.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif
35 #ifdef HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 #  include <sys/stat.h>
43 #  include <sys/types.h>
44 #endif
45
46 #include <X11/cursorfont.h>
47
48 RrInstance *ob_rr_inst = 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     char *theme;
70     xmlDocPtr doc;
71     xmlNodePtr node;
72
73     ob_state = State_Starting;
74
75     /* initialize the locale */
76     if (!setlocale(LC_ALL, ""))
77         g_warning("Couldn't set locale from environment.\n");
78     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
79     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
80     textdomain(PACKAGE_NAME);
81
82     /* start our event dispatcher and register for signals */
83     dispatch_startup();
84     dispatch_register(Event_Signal, signal_handler, NULL);
85
86     /* set up signal handler */
87     sigemptyset(&sigset);
88     action.sa_handler = dispatch_signal;
89     action.sa_mask = sigset;
90     action.sa_flags = SA_NOCLDSTOP;
91     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
92     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
93     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
94     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
95     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
96     sigaction(SIGINT, &action, (struct sigaction *) NULL);
97     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
98
99     /* create the ~/.openbox dir */
100     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
101     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
102                  S_IROTH | S_IWOTH | S_IXOTH));
103     g_free(path);
104     /* create the ~/.openbox/themes dir */
105     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
106     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
107                  S_IROTH | S_IWOTH | S_IXOTH));
108     g_free(path);
109      
110     /* parse out command line args */
111     parse_args(argc, argv);
112
113     ob_display = XOpenDisplay(NULL);
114     if (ob_display == NULL) {
115         /* print a message and exit */
116         g_critical("Failed to open the display.");
117         exit(1);
118     }
119     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
120         /* print a message and exit */
121         g_critical("Failed to set display as close-on-exec.");
122         exit(1);
123     }
124
125 #ifdef USE_LIBSN
126     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
127 #endif
128
129     ob_screen = DefaultScreen(ob_display);
130     ob_root = RootWindow(ob_display, ob_screen);
131
132     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
133     if (ob_rr_inst == NULL) {
134         g_critical("Failed to initialize the render library.");
135         exit(1);
136     }
137
138     /* XXX fork self onto other screens */
139      
140     XSynchronize(ob_display, ob_sync);
141
142     /* check for locale support */
143     if (!XSupportsLocale())
144         g_warning("X server does not support locale.");
145     if (!XSetLocaleModifiers(""))
146         g_warning("Cannot set locale modifiers for the X server.");
147
148     /* set our error handler */
149     XSetErrorHandler(xerror_handler);
150
151     /* set the DISPLAY environment variable for any lauched children, to the
152        display we're using, so they open in the right place. */
153     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
154
155     ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
156     ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
157     ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
158     ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
159     ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
160     ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
161     ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
162     ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
163     ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
164     ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
165     ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
166
167     prop_startup(); /* get atoms values for the display */
168     extensions_query_all(); /* find which extensions are present */
169
170     /* save stuff that we can use to restore state */
171     startup_save();
172
173     if (screen_annex()) { /* it will be ours! */
174         /* startup the parsing so everything can register sections of the rc */
175         parse_startup();
176
177         /* anything that is going to read data from the rc file needs to be 
178            in this group */
179         timer_startup();
180         font_startup();
181         theme_startup(ob_rr_inst);
182         event_startup();
183         grab_startup();
184         plugin_startup();
185         /* load the plugins specified in the pluginrc */
186         plugin_loadall();
187
188         /* set up the kernel config shit */
189         config_startup();
190         /* parse/load user options */
191         if (parse_load_rc(&doc, &node))
192             parse_tree(doc, node->xmlChildrenNode, NULL);
193         /* we're done with parsing now, kill it */
194         parse_shutdown();
195
196         /* load the theme specified in the rc file */
197         theme = theme_load(config_theme);
198         g_free(theme);
199         if (!theme) return 1;
200
201         window_startup();
202         menu_startup();
203         frame_startup();
204         moveresize_startup();
205         focus_startup();
206         screen_startup();
207         group_startup();
208         client_startup();
209         dock_startup();
210
211         /* call startup for all the plugins */
212         plugin_startall();
213
214         /* get all the existing windows */
215         client_manage_all();
216
217         ob_state = State_Running;
218         while (!ob_shutdown)
219             event_loop();
220         ob_state = State_Exiting;
221
222         dock_remove_all();
223         client_unmanage_all();
224
225         plugin_shutdown(); /* calls all the plugins' shutdown functions */
226         dock_shutdown();
227         client_shutdown();
228         group_shutdown();
229         screen_shutdown();
230         focus_shutdown();
231         moveresize_shutdown();
232         frame_shutdown();
233         menu_shutdown();
234         window_shutdown();
235         grab_shutdown();
236         event_shutdown();
237         theme_shutdown();
238         timer_shutdown();
239         config_shutdown();
240     }
241
242     dispatch_shutdown();
243
244     RrInstanceFree(ob_rr_inst);
245     XCloseDisplay(ob_display);
246
247     if (ob_restart) {
248         if (ob_restart_path != NULL) {
249             int argcp;
250             char **argvp;
251             GError *err = NULL;
252
253             /* run other shit */
254             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
255                 execvp(argvp[0], argvp);
256                 g_strfreev(argvp);
257             } else {
258                 g_warning("failed to execute '%s': %s", ob_restart_path,
259                           err->message);
260             }
261         }
262
263         /* re-run me */
264         execvp(argv[0], argv); /* try how we were run */
265         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
266     }
267      
268     return 0;
269 }
270
271 void signal_handler(const ObEvent *e, void *data)
272 {
273     int s;
274
275     s = e->data.s.signal;
276     switch (s) {
277     case SIGUSR1:
278         g_message("Caught SIGUSR1 signal. Restarting.");
279         ob_shutdown = ob_restart = TRUE;
280         break;
281
282     case SIGHUP:
283     case SIGINT:
284     case SIGTERM:
285     case SIGPIPE:
286         g_message("Caught signal %d. Exiting.", s);
287         ob_shutdown = TRUE;
288         break;
289
290     case SIGFPE:
291     case SIGSEGV:
292         g_message("Caught signal %d. Aborting and dumping core.", s);
293         abort();
294     }
295 }
296
297 void print_version()
298 {
299     g_print("Openbox %s\n\n", PACKAGE_VERSION);
300     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
301     g_print("This is free software, and you are welcome to redistribute it\n");
302     g_print("under certain conditions. See the file COPYING for details.\n\n");
303 }
304
305 void print_help()
306 {
307     print_version();
308     g_print("Syntax: %s [options]\n\n", BINARY);
309     g_print("Options:\n\n");
310     g_print("  -rc PATH     Specify the path to the rc file to use\n");
311     g_print("  -help        Display this help and exit\n");
312     g_print("  -version     Display the version and exit\n");
313     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
314             "               for debugging X routines)\n");
315     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
316 }
317
318 void parse_args(int argc, char **argv)
319 {
320     int i;
321
322     for (i = 1; i < argc; ++i) {
323         if (!strcmp(argv[i], "-version")) {
324             print_version();
325             exit(0);
326         } else if (!strcmp(argv[i], "-help")) {
327             print_help();
328             exit(0);
329         } else if (!strcmp(argv[i], "-sync")) {
330             ob_sync = TRUE;
331         } else if (!strcmp(argv[i], "-rc")) {
332             if (i == argc - 1) /* no args left */
333                 g_printerr("-rc requires an argument\n");
334             else
335                 ob_rc_path = argv[++i];
336         } else {
337             g_printerr("Invalid option: '%s'\n\n", argv[i]);
338             print_help();
339             exit(1);
340         }
341     }
342 }
343
344 gboolean ob_pointer_pos(int *x, int *y)
345 {
346     Window w;
347     int i;
348     guint u;
349
350     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
351 }