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