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