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