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