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