]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
support for transients of groups
[dana/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "client.h"
4 #include "dispatch.h"
5 #include "xerror.h"
6 #include "prop.h"
7 #include "screen.h"
8 #include "focus.h"
9 #include "extensions.h"
10 #include "parse.h"
11 #include "grab.h"
12 #include "engine.h"
13 #include "plugin.h"
14 #include "timer.h"
15 #include "group.h"
16 #include "gettext.h"
17 #include "../render/render.h"
18 #include "../render/font.h"
19
20 #ifdef HAVE_FCNTL_H
21 #  include <fcntl.h>
22 #endif
23 #ifdef HAVE_SIGNAL_H
24 #  include <signal.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 #endif
29 #ifdef HAVE_SYS_WAIT_H
30 #  include <sys/types.h>
31 #  include <sys/wait.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 State    ob_state;
50 gboolean ob_shutdown = FALSE;
51 gboolean ob_restart  = FALSE;
52 char    *ob_restart_path = NULL;
53 gboolean ob_remote   = TRUE;
54 gboolean ob_sync     = FALSE;
55 Cursors  ob_cursors;
56 char    *ob_rc_path  = NULL;
57
58 void signal_handler(const ObEvent *e, void *data);
59 void parse_args(int argc, char **argv);
60
61 int main(int argc, char **argv)
62 {
63     struct sigaction action;
64     sigset_t sigset;
65     char *path;
66
67     ob_state = State_Starting;
68
69     /* initialize the locale */
70     if (!setlocale(LC_ALL, ""))
71         g_warning("Couldn't set locale from environment.\n");
72     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
73     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
74     textdomain(PACKAGE_NAME);
75
76     /* start our event dispatcher and register for signals */
77     dispatch_startup();
78     dispatch_register(Event_Signal, signal_handler, NULL);
79
80     /* set up signal handler */
81     sigemptyset(&sigset);
82     action.sa_handler = dispatch_signal;
83     action.sa_mask = sigset;
84     action.sa_flags = SA_NOCLDSTOP;
85     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
86     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
87     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
88     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
89     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
90     sigaction(SIGINT, &action, (struct sigaction *) NULL);
91     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
92     sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
93
94     /* anything that died while we were restarting won't give us a SIGCHLD */
95     while (waitpid(-1, NULL, WNOHANG) > 0);
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     ob_screen = DefaultScreen(ob_display);
124     ob_root = RootWindow(ob_display, ob_screen);
125
126     /* XXX fork self onto other screens */
127      
128     XSynchronize(ob_display, ob_sync);
129
130     /* check for locale support */
131     if (!XSupportsLocale())
132         g_warning("X server does not support locale.");
133     if (!XSetLocaleModifiers(""))
134         g_warning("Cannot set locale modifiers for the X server.");
135
136     /* set our error handler */
137     XSetErrorHandler(xerror_handler);
138
139     /* set the DISPLAY environment variable for any lauched children, to the
140        display we're using, so they open in the right place. */
141     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
142
143     ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
144     ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
145     ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
146
147     prop_startup(); /* get atoms values for the display */
148     extensions_query_all(); /* find which extensions are present */
149
150     if (screen_annex()) { /* it will be ours! */
151         /* startup the parsing so everything can register sections of the rc */
152         parse_startup();
153
154         /* anything that is going to read data from the rc file needs to be 
155            in this group */
156         timer_startup();
157         render_startup();
158         font_startup();
159         event_startup();
160         grab_startup();
161         engine_startup();
162         focus_startup();
163         plugin_startup();
164         /* load the plugins specified in the pluginrc */
165         plugin_loadall();
166
167         /* parse/load user options */
168         parse_rc();
169         /* we're done with parsing now, kill it */
170         parse_shutdown();
171
172         /* load the engine specified in the rc */
173         engine_load();
174
175         screen_startup();
176         group_startup();
177         client_startup();
178
179         /* call startup for all the plugins */
180         plugin_startall();
181
182         /* get all the existing windows */
183         client_manage_all();
184
185         ob_state = State_Running;
186         while (!ob_shutdown)
187             event_loop();
188         ob_state = State_Exiting;
189
190         client_unmanage_all();
191
192         plugin_shutdown(); /* calls all the plugins' shutdown functions */
193         client_shutdown();
194         group_shutdown();
195         screen_shutdown();
196         focus_shutdown();
197         engine_shutdown();
198         grab_shutdown();
199         event_shutdown();
200         render_shutdown();
201         timer_shutdown();
202     }
203
204     dispatch_shutdown();
205
206     XCloseDisplay(ob_display);
207
208     if (ob_restart) {
209         if (ob_restart_path != NULL) {
210             int argcp;
211             char **argvp;
212             GError *err = NULL;
213
214             /* run other shit */
215             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
216                 execvp(argvp[0], argvp);
217                 g_strfreev(argvp);
218             } else {
219                 g_warning("failed to execute '%s': %s", ob_restart_path,
220                           err->message);
221             }
222         }
223
224         /* re-run me */
225         execvp(argv[0], argv); /* try how we were run */
226         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
227     }
228      
229     return 0;
230 }
231
232 void signal_handler(const ObEvent *e, void *data)
233 {
234     int s;
235
236     s = e->data.s.signal;
237     switch (s) {
238     case SIGUSR1:
239         g_message("Caught SIGUSR1 signal. Restarting.");
240         ob_shutdown = ob_restart = TRUE;
241         break;
242
243     case SIGCHLD:
244         wait(NULL);
245         break;
246
247     case SIGHUP:
248     case SIGINT:
249     case SIGTERM:
250     case SIGPIPE:
251         g_message("Caught signal %d. Exiting.", s);
252         ob_shutdown = TRUE;
253         break;
254
255     case SIGFPE:
256     case SIGSEGV:
257         g_error("Caught signal %d. Aborting and dumping core.", s);
258     }
259 }
260
261 void print_version()
262 {
263     g_print("Openbox %s\n\n", PACKAGE_VERSION);
264     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
265     g_print("This is free software, and you are welcome to redistribute it\n");
266     g_print("under certain conditions. See the file COPYING for details.\n\n");
267 }
268
269 void print_help()
270 {
271     print_version();
272     g_print("Syntax: %s [options]\n\n", BINARY);
273     g_print("Options:\n\n");
274     g_print("  -rc PATH     Specify the path to the rc file to use\n");
275     g_print("  -help        Display this help and exit\n");
276     g_print("  -version     Display the version and exit\n");
277     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
278             "               for debugging X routines)\n");
279     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
280 }
281
282 void parse_args(int argc, char **argv)
283 {
284     int i;
285
286     for (i = 1; i < argc; ++i) {
287         if (!strcmp(argv[i], "-version")) {
288             print_version();
289             exit(0);
290         } else if (!strcmp(argv[i], "-help")) {
291             print_help();
292             exit(0);
293         } else if (!strcmp(argv[i], "-sync")) {
294             ob_sync = TRUE;
295         } else if (!strcmp(argv[i], "-rc")) {
296             if (i == argc - 1) /* no args left */
297                 g_printerr("-rc requires an argument\n");
298             else
299                 ob_rc_path = argv[++i];
300         } else {
301             g_printerr("Invalid option: '%s'\n\n", argv[i]);
302             print_help();
303             exit(1);
304         }
305     }
306 }