]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
make session management optional
[dana/openbox.git] / openbox / openbox.c
1 #include "openbox.h"
2 #include "dock.h"
3 #include "event.h"
4 #include "menu.h"
5 #include "client.h"
6 #include "dispatch.h"
7 #include "xerror.h"
8 #include "prop.h"
9 #include "startup.h"
10 #include "screen.h"
11 #include "focus.h"
12 #include "moveresize.h"
13 #include "frame.h"
14 #include "extensions.h"
15 #include "grab.h"
16 #include "plugin.h"
17 #include "timer.h"
18 #include "group.h"
19 #include "config.h"
20 #include "gettext.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
24
25 #ifdef HAVE_FCNTL_H
26 #  include <fcntl.h>
27 #endif
28 #ifdef HAVE_SIGNAL_H
29 #  include <signal.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 #endif
34 #ifdef HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #  include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 #  include <sys/stat.h>
42 #  include <sys/types.h>
43 #endif
44
45 #ifdef USE_SM
46 #include <X11/SM/SMlib.h>
47 #endif
48
49 #include <X11/cursorfont.h>
50
51 #ifdef USE_SM
52 SmcConn     ob_sm_conn;
53 gchar      *ob_sm_id = NULL;
54 #endif
55
56 RrInstance *ob_rr_inst = NULL;
57 RrTheme    *ob_rr_theme = NULL;
58 Display    *ob_display = NULL;
59 int         ob_screen;
60 Window      ob_root;
61 State       ob_state;
62 gboolean    ob_shutdown = FALSE;
63 gboolean    ob_restart  = FALSE;
64 char       *ob_restart_path = NULL;
65 gboolean    ob_remote   = TRUE;
66 gboolean    ob_sync     = FALSE;
67 Cursors     ob_cursors;
68 char       *ob_rc_path  = NULL;
69
70 static void signal_handler(const ObEvent *e, void *data);
71 static void parse_args(int argc, char **argv);
72
73 static void sm_startup(int argc, char **argv);
74 static void sm_shutdown();
75
76 #ifdef USE_SM
77 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
78                              Bool shutdown, int interact_style, Bool fast);
79 static void sm_die(SmcConn conn, SmPointer data);
80 static void sm_save_complete(SmcConn conn, SmPointer data);
81 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
82 #endif
83 static void exit_with_error(gchar *msg);
84
85 int main(int argc, char **argv)
86 {
87     struct sigaction action;
88     sigset_t sigset;
89     char *path;
90     xmlDocPtr doc;
91     xmlNodePtr node;
92
93     ob_state = State_Starting;
94
95     /* initialize the locale */
96     if (!setlocale(LC_ALL, ""))
97         g_warning("Couldn't set locale from environment.\n");
98     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
99     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
100     textdomain(PACKAGE_NAME);
101
102     /* start our event dispatcher and register for signals */
103     dispatch_startup();
104     dispatch_register(Event_Signal, signal_handler, NULL);
105
106     /* set up signal handler */
107     sigemptyset(&sigset);
108     action.sa_handler = dispatch_signal;
109     action.sa_mask = sigset;
110     action.sa_flags = SA_NOCLDSTOP;
111     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
112     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
113     sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
114     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
115     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
116     sigaction(SIGINT, &action, (struct sigaction *) NULL);
117     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
118
119     /* create the ~/.openbox dir */
120     path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
121     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
122                  S_IROTH | S_IWOTH | S_IXOTH));
123     g_free(path);
124     /* create the ~/.openbox/themes dir */
125     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
126     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
127                  S_IROTH | S_IWOTH | S_IXOTH));
128     g_free(path);
129      
130     /* parse out command line args */
131     parse_args(argc, argv);
132
133     ob_display = XOpenDisplay(NULL);
134     if (ob_display == NULL)
135         exit_with_error("Failed to open the display.");
136     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
137         exit_with_error("Failed to set display as close-on-exec.");
138
139     sm_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     ob_root = RootWindow(ob_display, ob_screen);
147
148     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
149     if (ob_rr_inst == NULL)
150         exit_with_error("Failed to initialize the render library.");
151
152     /* XXX fork self onto other screens */
153      
154     XSynchronize(ob_display, ob_sync);
155
156     /* check for locale support */
157     if (!XSupportsLocale())
158         g_warning("X server does not support locale.");
159     if (!XSetLocaleModifiers(""))
160         g_warning("Cannot set locale modifiers for the X server.");
161
162     /* set our error handler */
163     XSetErrorHandler(xerror_handler);
164
165     /* set the DISPLAY environment variable for any lauched children, to the
166        display we're using, so they open in the right place. */
167     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
168
169     ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
170     ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
171     ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
172     ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
173     ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
174     ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
175     ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
176     ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
177     ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
178     ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
179     ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
180
181     prop_startup(); /* get atoms values for the display */
182     extensions_query_all(); /* find which extensions are present */
183
184     /* save stuff that we can use to restore state */
185     startup_save();
186
187     if (screen_annex()) { /* it will be ours! */
188         /* startup the parsing so everything can register sections of the rc */
189         parse_startup();
190
191         /* anything that is going to read data from the rc file needs to be 
192            in this group */
193         timer_startup();
194         event_startup();
195         grab_startup();
196         plugin_startup();
197         /* load the plugins specified in the pluginrc */
198         plugin_loadall();
199
200         /* set up the kernel config shit */
201         config_startup();
202         /* parse/load user options */
203         if (parse_load_rc(&doc, &node))
204             parse_tree(doc, node->xmlChildrenNode, NULL);
205         /* we're done with parsing now, kill it */
206         parse_shutdown();
207
208         /* load the theme specified in the rc file */
209         ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
210         if (ob_rr_theme == NULL)
211             exit_with_error("Unable to load a theme.");
212
213         window_startup();
214         menu_startup();
215         frame_startup();
216         moveresize_startup();
217         focus_startup();
218         screen_startup();
219         group_startup();
220         client_startup();
221         dock_startup();
222
223         /* call startup for all the plugins */
224         plugin_startall();
225
226         /* get all the existing windows */
227         client_manage_all();
228
229         ob_state = State_Running;
230         while (!ob_shutdown)
231             event_loop();
232         ob_state = State_Exiting;
233
234         dock_remove_all();
235         client_unmanage_all();
236
237         plugin_shutdown(); /* calls all the plugins' shutdown functions */
238         dock_shutdown();
239         client_shutdown();
240         group_shutdown();
241         screen_shutdown();
242         focus_shutdown();
243         moveresize_shutdown();
244         frame_shutdown();
245         menu_shutdown();
246         window_shutdown();
247         grab_shutdown();
248         event_shutdown();
249         timer_shutdown();
250         config_shutdown();
251     }
252
253     dispatch_shutdown();
254
255     RrThemeFree(ob_rr_theme);
256     RrInstanceFree(ob_rr_inst);
257
258     sm_shutdown();
259
260     XCloseDisplay(ob_display);
261
262     if (ob_restart) {
263         if (ob_restart_path != NULL) {
264             int argcp;
265             char **argvp;
266             GError *err = NULL;
267
268             /* run other shit */
269             if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
270                 execvp(argvp[0], argvp);
271                 g_strfreev(argvp);
272             } else {
273                 g_warning("failed to execute '%s': %s", ob_restart_path,
274                           err->message);
275             }
276         }
277
278         /* re-run me */
279         execvp(argv[0], argv); /* try how we were run */
280         execlp(BINARY, BINARY, NULL); /* try this as a last resort */
281     }
282      
283     return 0;
284 }
285
286 static void sm_startup(int argc, char **argv)
287 {
288 #ifdef USE_SM
289
290 #define SM_ERR_LEN 1024
291
292     SmcCallbacks cb;
293     char sm_err[SM_ERR_LEN];
294
295     cb.save_yourself.callback = sm_save_yourself;
296     cb.save_yourself.client_data = NULL;
297
298     cb.die.callback = sm_die;
299     cb.die.client_data = NULL;
300
301     cb.save_complete.callback = sm_save_complete;
302     cb.save_complete.client_data = NULL;
303
304     cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
305     cb.shutdown_cancelled.client_data = NULL;
306
307     ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
308                                    SmcSaveYourselfProcMask |
309                                    SmcDieProcMask |
310                                    SmcSaveCompleteProcMask |
311                                    SmcShutdownCancelledProcMask,
312                                    &cb, ob_sm_id, &ob_sm_id,
313                                    SM_ERR_LEN, sm_err);
314     if (ob_sm_conn == NULL)
315         g_warning("Failed to connect to session manager: %s", sm_err);
316     else {
317         SmPropValue val_cmd;
318         SmPropValue val_res;
319         SmPropValue val_prog;
320         SmPropValue val_uid;
321         SmProp prop_cmd = { SmCloneCommand, "SmLISTofARRAY8", 1, };
322         SmProp prop_res = { SmRestartCommand, "SmLISTofARRAY8", 1, };
323         SmProp prop_prog = { SmProgram, "SmARRAY8", 1, };
324         SmProp prop_uid = { SmUserID, "SmARRAY8", 1, };
325         SmProp *props[4];
326
327         val_cmd.value = argv[0];
328         val_cmd.length = strlen(argv[0]);
329         val_res.value = argv[0];
330         val_res.length = strlen(argv[0]); /* XXX -id foo */
331         val_prog.value = argv[0];
332         val_prog.length = strlen(argv[0]);
333
334         val_uid.value = g_strdup_printf("%ld", (long)getuid());
335         val_uid.length = strlen(val_uid.value);
336
337         prop_cmd.vals = &val_cmd;
338         prop_res.vals = &val_res;
339         prop_prog.vals = &val_prog;
340         prop_uid.vals = &val_uid;
341
342         props[0] = &prop_cmd;
343         props[1] = &prop_res;
344         props[2] = &prop_prog;
345         props[3] = &prop_uid;
346
347         SmcSetProperties(ob_sm_conn, 3, props);
348
349         g_free(val_uid.value);
350
351         g_message("Connected to session manager with id %s", ob_sm_id);
352     }
353     g_free (ob_sm_id);
354 #endif
355 }
356
357 static void sm_shutdown()
358 {
359 #ifdef USE_SM
360     if (ob_sm_conn)
361         SmcCloseConnection(ob_sm_conn, 0, NULL);
362 #endif
363 }
364
365 static void signal_handler(const ObEvent *e, void *data)
366 {
367     int s;
368
369     s = e->data.s.signal;
370     switch (s) {
371     case SIGUSR1:
372         g_message("Caught SIGUSR1 signal. Restarting.");
373         ob_shutdown = ob_restart = TRUE;
374         break;
375
376     case SIGHUP:
377     case SIGINT:
378     case SIGTERM:
379     case SIGPIPE:
380         g_message("Caught signal %d. Exiting.", s);
381         ob_shutdown = TRUE;
382         break;
383
384     case SIGFPE:
385     case SIGSEGV:
386         g_message("Caught signal %d. Aborting and dumping core.", s);
387         abort();
388     }
389 }
390
391 static void print_version()
392 {
393     g_print("Openbox %s\n\n", PACKAGE_VERSION);
394     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
395     g_print("This is free software, and you are welcome to redistribute it\n");
396     g_print("under certain conditions. See the file COPYING for details.\n\n");
397 }
398
399 static void print_help()
400 {
401     print_version();
402     g_print("Syntax: %s [options]\n\n", BINARY);
403     g_print("Options:\n\n");
404     g_print("  -rc PATH     Specify the path to the rc file to use\n");
405     g_print("  -help        Display this help and exit\n");
406     g_print("  -version     Display the version and exit\n");
407     g_print("  -sync        Run in synchronous mode (this is slow and meant\n"
408             "               for debugging X routines)\n");
409     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
410 }
411
412 static void parse_args(int argc, char **argv)
413 {
414     int i;
415
416     for (i = 1; i < argc; ++i) {
417         if (!strcmp(argv[i], "-version")) {
418             print_version();
419             exit(0);
420         } else if (!strcmp(argv[i], "-help")) {
421             print_help();
422             exit(0);
423         } else if (!strcmp(argv[i], "-sync")) {
424             ob_sync = TRUE;
425         } else if (!strcmp(argv[i], "-rc")) {
426             if (i == argc - 1) /* no args left */
427                 g_printerr("-rc requires an argument\n");
428             else
429                 ob_rc_path = argv[++i];
430         } else {
431             g_printerr("Invalid option: '%s'\n\n", argv[i]);
432             print_help();
433             exit(1);
434         }
435     }
436 }
437
438 gboolean ob_pointer_pos(int *x, int *y)
439 {
440     Window w;
441     int i;
442     guint u;
443
444     return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
445 }
446
447 #ifdef USE_SM
448 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
449                              Bool shutdown, int interact_style, Bool fast)
450 {
451     g_message("got SAVE YOURSELF from session manager");
452     SmcSaveYourselfDone(conn, TRUE);
453 }
454
455 static void sm_die(SmcConn conn, SmPointer data)
456 {
457     ob_shutdown = TRUE;
458     g_message("got DIE from session manager");
459 }
460
461 static void sm_save_complete(SmcConn conn, SmPointer data)
462 {
463     g_message("got SAVE COMPLETE from session manager");
464 }
465
466 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
467 {
468     g_message("got SHUTDOWN CANCELLED from session manager");
469 }
470 #endif
471
472 static void exit_with_error(gchar *msg)
473 {
474     g_critical(msg);
475     sm_shutdown();
476     exit(EXIT_FAILURE);
477 }