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