]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/openbox.c
fix a bunch of memleaks from valgrind and stuff
[mikachu/openbox.git] / openbox / openbox.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "session.h"
4 #include "dock.h"
5 #include "event.h"
6 #include "menu.h"
7 #include "client.h"
8 #include "dispatch.h"
9 #include "xerror.h"
10 #include "prop.h"
11 #include "startup.h"
12 #include "screen.h"
13 #include "focus.h"
14 #include "moveresize.h"
15 #include "frame.h"
16 #include "keyboard.h"
17 #include "mouse.h"
18 #include "extensions.h"
19 #include "grab.h"
20 #include "plugin.h"
21 #include "timer.h"
22 #include "group.h"
23 #include "config.h"
24 #include "gettext.h"
25 #include "parser/parse.h"
26 #include "render/render.h"
27 #include "render/theme.h"
28
29 #ifdef HAVE_FCNTL_H
30 #  include <fcntl.h>
31 #endif
32 #ifdef HAVE_SIGNAL_H
33 #define __USE_UNIX98
34 #  include <signal.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 #  include <stdlib.h>
38 #endif
39 #ifdef HAVE_LOCALE_H
40 #  include <locale.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 #  include <sys/stat.h>
44 #  include <sys/types.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #  include <unistd.h>
48 #endif
49
50 #include <X11/cursorfont.h>
51
52 RrInstance *ob_rr_inst;
53 RrTheme    *ob_rr_theme;
54 Display    *ob_display;
55 gint        ob_screen;
56 gboolean    ob_sm_use = TRUE;
57 gchar      *ob_sm_id;
58 gboolean    ob_replace_wm;
59
60 static ObState   state;
61 static gboolean  xsync;
62 static gboolean  shutdown;
63 static gboolean  restart;
64 static char     *restart_path;
65 static Cursor    cursors[OB_NUM_CURSORS];
66 static KeyCode   keys[OB_NUM_KEYS];
67 static gchar    *sm_save_file;
68
69 static void signal_handler(const ObEvent *e, void *data);
70 static void parse_args(int argc, char **argv);
71
72 int main(int argc, char **argv)
73 {
74     struct sigaction action;
75     sigset_t sigset;
76     char *path;
77     xmlDocPtr doc;
78     xmlNodePtr node;
79
80 #ifdef DEBUG
81     ob_debug_show_output(TRUE);
82 #endif
83
84     state = OB_STATE_STARTING;
85
86     /* initialize the locale */
87     if (!setlocale(LC_ALL, ""))
88         g_warning("Couldn't set locale from environment.\n");
89     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
90     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
91     textdomain(PACKAGE_NAME);
92
93     /* start our event dispatcher and register for signals */
94     dispatch_startup();
95     dispatch_register(Event_Signal, signal_handler, NULL);
96
97     /* set up signal handler */
98     sigemptyset(&sigset);
99     action.sa_handler = dispatch_signal;
100     action.sa_mask = sigset;
101     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
102     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
103     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
104 /*    sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
105     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
106     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
107     sigaction(SIGINT, &action, (struct sigaction *) NULL);
108     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
109
110     /* create the ~/.openbox dir */
111     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
112     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
113                  S_IROTH | S_IWOTH | S_IXOTH));
114     g_free(path);
115     /* create the ~/.openbox/themes dir */
116     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
117     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
118                  S_IROTH | S_IWOTH | S_IXOTH));
119     g_free(path);
120     /* create the ~/.openbox/sessions dir */
121     path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
122     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
123                  S_IROTH | S_IWOTH | S_IXOTH));
124     g_free(path);
125
126     g_set_prgname(argv[0]);
127      
128     /* parse out command line args */
129     parse_args(argc, argv);
130
131     ob_display = XOpenDisplay(NULL);
132     if (ob_display == NULL)
133         ob_exit_with_error("Failed to open the display.");
134     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
135         ob_exit_with_error("Failed to set display as close-on-exec.");
136
137     if (sm_save_file)
138         session_load(sm_save_file);
139     session_startup(argc, argv);
140
141 #ifdef USE_LIBSN
142     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
143 #endif
144
145     ob_screen = DefaultScreen(ob_display);
146
147     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
148     if (ob_rr_inst == NULL)
149         ob_exit_with_error("Failed to initialize the render library.");
150
151     /* XXX fork self onto other screens */
152      
153     XSynchronize(ob_display, xsync);
154
155     /* check for locale support */
156     if (!XSupportsLocale())
157         g_warning("X server does not support locale.");
158     if (!XSetLocaleModifiers(""))
159         g_warning("Cannot set locale modifiers for the X server.");
160
161     /* set our error handler */
162     XSetErrorHandler(xerror_handler);
163
164     /* set the DISPLAY environment variable for any lauched children, to the
165        display we're using, so they open in the right place. */
166     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
167
168     /* create available cursors */
169     cursors[OB_CURSOR_POINTER] =
170         XCreateFontCursor(ob_display, XC_left_ptr);
171     cursors[OB_CURSOR_BUSY] =
172         XCreateFontCursor(ob_display, XC_watch);
173     cursors[OB_CURSOR_MOVE] =
174         XCreateFontCursor(ob_display, XC_fleur);
175     cursors[OB_CURSOR_NORTH] =
176         XCreateFontCursor(ob_display, XC_top_side);
177     cursors[OB_CURSOR_NORTHEAST] =
178         XCreateFontCursor(ob_display, XC_top_right_corner);
179     cursors[OB_CURSOR_EAST] =
180         XCreateFontCursor(ob_display, XC_right_side);
181     cursors[OB_CURSOR_SOUTHEAST] =
182         XCreateFontCursor(ob_display, XC_bottom_right_corner);
183     cursors[OB_CURSOR_SOUTH] =
184         XCreateFontCursor(ob_display, XC_bottom_side);
185     cursors[OB_CURSOR_SOUTHWEST] =
186         XCreateFontCursor(ob_display, XC_bottom_left_corner);
187     cursors[OB_CURSOR_WEST] =
188         XCreateFontCursor(ob_display, XC_left_side);
189     cursors[OB_CURSOR_NORTHWEST] =
190         XCreateFontCursor(ob_display, XC_top_left_corner);
191
192     /* create available keycodes */
193     keys[OB_KEY_RETURN] =
194         XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
195     keys[OB_KEY_ESCAPE] =
196         XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
197     keys[OB_KEY_LEFT] =
198         XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
199     keys[OB_KEY_RIGHT] =
200         XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
201     keys[OB_KEY_UP] =
202         XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
203     keys[OB_KEY_DOWN] =
204         XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
205
206     prop_startup(); /* get atoms values for the display */
207     extensions_query_all(); /* find which extensions are present */
208
209     /* save stuff that we can use to restore state */
210     startup_save();
211
212     if (screen_annex()) { /* it will be ours! */
213         ObParseInst *i;
214
215         /* startup the parsing so everything can register sections of the rc */
216         i = parse_startup();
217
218         /* anything that is going to read data from the rc file needs to be 
219            in this group */
220         timer_startup();
221         event_startup();
222         grab_startup();
223         /* focus_backup is used for stacking, so this needs to come before
224            anything that calls stacking_add */
225         focus_startup();
226         window_startup();
227         plugin_startup();
228         /* load the plugins specified in the pluginrc */
229         plugin_loadall(i);
230
231         /* set up the kernel config shit */
232         config_startup(i);
233         menu_startup(i);
234         /* parse/load user options */
235         if (parse_load_rc(&doc, &node))
236             parse_tree(i, doc, node->xmlChildrenNode);
237         /* we're done with parsing now, kill it */
238         xmlFreeDoc(doc);
239         parse_shutdown(i);
240
241         menu_parse();
242
243         /* load the theme specified in the rc file */
244         ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
245         if (ob_rr_theme == NULL)
246             ob_exit_with_error("Unable to load a theme.");
247
248         moveresize_startup();
249         screen_startup();
250         group_startup();
251         client_startup();
252         dock_startup();
253         keyboard_startup();
254         mouse_startup();
255
256         /* call startup for all the plugins */
257         plugin_startall();
258
259         /* get all the existing windows */
260         client_manage_all();
261
262         state = OB_STATE_RUNNING;
263         while (!shutdown)
264             event_loop();
265         state = OB_STATE_EXITING;
266
267         dock_remove_all();
268         client_unmanage_all();
269
270         plugin_shutdown(); /* calls all the plugins' shutdown functions */
271         menu_shutdown();
272         mouse_shutdown();
273         keyboard_shutdown();
274         dock_shutdown();
275         client_shutdown();
276         group_shutdown();
277         screen_shutdown();
278         focus_shutdown();
279         moveresize_shutdown();
280         window_shutdown();
281         grab_shutdown();
282         event_shutdown();
283         timer_shutdown();
284         config_shutdown();
285     }
286
287     dispatch_shutdown();
288
289     RrThemeFree(ob_rr_theme);
290     RrInstanceFree(ob_rr_inst);
291
292     session_shutdown();
293     g_free(ob_sm_id);
294
295 #ifdef USE_LIBSN
296     sn_display_unref(ob_sn_display);
297 #endif
298
299     XCloseDisplay(ob_display);
300
301     if (restart) {
302         if (restart_path != NULL) {
303             int argcp;
304             char **argvp;
305             GError *err = NULL;
306
307             /* run other shit */
308             if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
309                 execvp(argvp[0], argvp);
310                 g_strfreev(argvp);
311             } else {
312                 g_warning("failed to execute '%s': %s", restart_path,
313                           err->message);
314             }
315         }
316
317         /* re-run me */
318         execvp(argv[0], argv); /* try how we were run */
319     }
320      
321     return 0;
322 }
323
324 static void signal_handler(const ObEvent *e, void *data)
325 {
326     int s;
327
328     s = e->data.s.signal;
329     switch (s) {
330     case SIGUSR1:
331         fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
332         ob_restart();
333         break;
334
335     case SIGHUP:
336     case SIGINT:
337     case SIGTERM:
338     case SIGPIPE:
339         fprintf(stderr, "Caught signal %d. Exiting.", s);
340         ob_exit();
341         break;
342
343     case SIGFPE:
344     case SIGSEGV:
345         fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
346         abort();
347     }
348 }
349
350 static void print_version()
351 {
352     g_print("Openbox %s\n\n", PACKAGE_VERSION);
353     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
354     g_print("This is free software, and you are welcome to redistribute it\n");
355     g_print("under certain conditions. See the file COPYING for details.\n\n");
356 }
357
358 static void print_help()
359 {
360     print_version();
361     g_print("Syntax: openbox [options]\n\n");
362     g_print("Options:\n\n");
363 #ifdef USE_SM
364     g_print("  --sm-disable        Disable connection to session manager\n");
365     g_print("  --sm-client-id ID   Specify session management ID\n");
366     g_print("  --sm-save-file FILE Specify file to load a saved session\n"
367             "                      from\n");
368 #endif
369     g_print("  --replace           Replace the currently running window "
370             "manager\n");
371     g_print("  --help              Display this help and exit\n");
372     g_print("  --version           Display the version and exit\n");
373     g_print("  --sync              Run in synchronous mode (this is slow and\n"
374             "                      meant for debugging X routines)\n");
375     g_print("  --debug             Display debugging output\n");
376     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
377 }
378
379 static void parse_args(int argc, char **argv)
380 {
381     int i;
382
383     for (i = 1; i < argc; ++i) {
384         if (!strcmp(argv[i], "--version")) {
385             print_version();
386             exit(0);
387         } else if (!strcmp(argv[i], "--help")) {
388             print_help();
389             exit(0);
390         } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
391             g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
392         } else if (!strcmp(argv[i], "--replace")) {
393             ob_replace_wm = TRUE;
394         } else if (!strcmp(argv[i], "--sync")) {
395             xsync = TRUE;
396         } else if (!strcmp(argv[i], "--debug")) {
397             ob_debug_show_output(TRUE);
398 #ifdef USE_SM
399         } else if (!strcmp(argv[i], "--sm-client-id")) {
400             if (i == argc - 1) /* no args left */
401                 g_printerr(_("--sm-client-id requires an argument\n"));
402             else
403                 ob_sm_id = g_strdup(argv[++i]);
404         } else if (!strcmp(argv[i], "--sm-save-file")) {
405             if (i == argc - 1) /* no args left */
406                 g_printerr(_("--sm-save-file requires an argument\n"));
407             else
408                 sm_save_file = argv[++i];
409         } else if (!strcmp(argv[i], "--sm-disable")) {
410             ob_sm_use = FALSE;
411 #endif
412         } else {
413             g_printerr("Invalid option: '%s'\n\n", argv[i]);
414             print_help();
415             exit(1);
416         }
417     }
418 }
419
420 void ob_exit_with_error(gchar *msg)
421 {
422     g_critical(msg);
423     session_shutdown();
424     exit(EXIT_FAILURE);
425 }
426
427 void ob_restart_other(const gchar *path)
428 {
429     restart_path = g_strdup(path);
430     ob_restart();
431 }
432
433 void ob_restart()
434 {
435     restart = TRUE;
436     ob_exit();
437 }
438
439 void ob_exit()
440 {
441     shutdown = TRUE;
442 }
443
444 Cursor ob_cursor(ObCursor cursor)
445 {
446     g_assert(cursor < OB_NUM_CURSORS);
447     return cursors[cursor];
448 }
449
450 KeyCode ob_keycode(ObKey key)
451 {
452     g_assert(key < OB_NUM_KEYS);
453     return keys[key];
454 }
455
456 ObState ob_state()
457 {
458     return state;
459 }