]> icculus.org git repositories - dana/openbox.git/blob - openbox/openbox.c
add disabled buttons, instead of now showing buttons at all when they wont do anythin...
[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 #define __USE_UNIX98
30 #  include <signal.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif
35 #ifdef HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 #  include <sys/stat.h>
43 #  include <sys/types.h>
44 #endif
45
46 #ifdef USE_SM
47 #include <X11/SM/SMlib.h>
48 #endif
49
50 #include <X11/cursorfont.h>
51
52 #ifdef USE_SM
53 gboolean    ob_sm_use = TRUE;
54 SmcConn     ob_sm_conn;
55 gchar      *ob_sm_id;
56 #endif
57
58 RrInstance *ob_rr_inst;
59 RrTheme    *ob_rr_theme;
60 Display    *ob_display;
61 gint        ob_screen;
62 Cursor      ob_cursors[OB_NUM_CURSORS];
63 KeyCode     ob_keys[OB_NUM_KEYS];
64
65 static ObState   state;
66 static gboolean  xsync;
67 static gboolean  shutdown;
68 static gboolean  restart;
69 static char     *restart_path;
70
71 static void signal_handler(const ObEvent *e, void *data);
72 static void parse_args(int argc, char **argv);
73
74 static void sm_startup(int argc, char **argv);
75 static void sm_shutdown();
76
77 #ifdef USE_SM
78 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
79                              Bool shutdown, int interact_style, Bool fast);
80 static void sm_die(SmcConn conn, SmPointer data);
81 static void sm_save_complete(SmcConn conn, SmPointer data);
82 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
83 #endif
84 static void exit_with_error(gchar *msg);
85
86 int main(int argc, char **argv)
87 {
88     struct sigaction action;
89     sigset_t sigset;
90     char *path;
91     xmlDocPtr doc;
92     xmlNodePtr node;
93
94     state = OB_STATE_STARTING;
95
96     /* initialize the locale */
97     if (!setlocale(LC_ALL, ""))
98         g_warning("Couldn't set locale from environment.\n");
99     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
100     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
101     textdomain(PACKAGE_NAME);
102
103     /* start our event dispatcher and register for signals */
104     dispatch_startup();
105     dispatch_register(Event_Signal, signal_handler, NULL);
106
107     /* set up signal handler */
108     sigemptyset(&sigset);
109     action.sa_handler = dispatch_signal;
110     action.sa_mask = sigset;
111     action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
112     sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
113     sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
114 /*    sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
115     sigaction(SIGFPE, &action, (struct sigaction *) NULL);
116     sigaction(SIGTERM, &action, (struct sigaction *) NULL);
117     sigaction(SIGINT, &action, (struct sigaction *) NULL);
118     sigaction(SIGHUP, &action, (struct sigaction *) NULL);
119
120     /* create the ~/.openbox dir */
121     path = g_build_filename(g_get_home_dir(), ".openbox", 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     /* create the ~/.openbox/themes dir */
126     path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
127     mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
128                  S_IROTH | S_IWOTH | S_IXOTH));
129     g_free(path);
130
131     g_set_prgname(argv[0]);
132      
133     /* parse out command line args */
134     parse_args(argc, argv);
135
136     ob_display = XOpenDisplay(NULL);
137     if (ob_display == NULL)
138         exit_with_error("Failed to open the display.");
139     if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
140         exit_with_error("Failed to set display as close-on-exec.");
141
142     sm_startup(argc, argv);
143
144 #ifdef USE_LIBSN
145     ob_sn_display = sn_display_new(ob_display, NULL, NULL);
146 #endif
147
148     ob_screen = DefaultScreen(ob_display);
149
150     ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
151     if (ob_rr_inst == NULL)
152         exit_with_error("Failed to initialize the render library.");
153
154     /* XXX fork self onto other screens */
155      
156     XSynchronize(ob_display, xsync);
157
158     /* check for locale support */
159     if (!XSupportsLocale())
160         g_warning("X server does not support locale.");
161     if (!XSetLocaleModifiers(""))
162         g_warning("Cannot set locale modifiers for the X server.");
163
164     /* set our error handler */
165     XSetErrorHandler(xerror_handler);
166
167     /* set the DISPLAY environment variable for any lauched children, to the
168        display we're using, so they open in the right place. */
169     putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
170
171     /* create available cursors */
172     ob_cursors[OB_CURSOR_POINTER] =
173         XCreateFontCursor(ob_display, XC_left_ptr);
174     ob_cursors[OB_CURSOR_BUSY] =
175         XCreateFontCursor(ob_display, XC_watch);
176     ob_cursors[OB_CURSOR_MOVE] =
177         XCreateFontCursor(ob_display, XC_fleur);
178     ob_cursors[OB_CURSOR_NORTH] =
179         XCreateFontCursor(ob_display, XC_top_side);
180     ob_cursors[OB_CURSOR_NORTHEAST] =
181         XCreateFontCursor(ob_display, XC_top_right_corner);
182     ob_cursors[OB_CURSOR_EAST] =
183         XCreateFontCursor(ob_display, XC_right_side);
184     ob_cursors[OB_CURSOR_SOUTHEAST] =
185         XCreateFontCursor(ob_display, XC_bottom_right_corner);
186     ob_cursors[OB_CURSOR_SOUTH] =
187         XCreateFontCursor(ob_display, XC_bottom_side);
188     ob_cursors[OB_CURSOR_SOUTHWEST] =
189         XCreateFontCursor(ob_display, XC_bottom_left_corner);
190     ob_cursors[OB_CURSOR_WEST] =
191         XCreateFontCursor(ob_display, XC_left_side);
192     ob_cursors[OB_CURSOR_NORTHWEST] =
193         XCreateFontCursor(ob_display, XC_top_left_corner);
194
195     /* create available keycodes */
196     ob_keys[OB_KEY_RETURN] =
197         XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
198     ob_keys[OB_KEY_ESCAPE] =
199         XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
200     ob_keys[OB_KEY_LEFT] =
201         XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
202     ob_keys[OB_KEY_RIGHT] =
203         XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
204     ob_keys[OB_KEY_UP] =
205         XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
206     ob_keys[OB_KEY_DOWN] =
207         XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
208
209     prop_startup(); /* get atoms values for the display */
210     extensions_query_all(); /* find which extensions are present */
211
212     /* save stuff that we can use to restore state */
213     startup_save();
214
215     if (screen_annex()) { /* it will be ours! */
216         /* startup the parsing so everything can register sections of the rc */
217         parse_startup();
218
219         /* anything that is going to read data from the rc file needs to be 
220            in this group */
221         timer_startup();
222         event_startup();
223         grab_startup();
224         /* focus_backup is used for stacking, so this needs to come before
225            anything that calls stacking_add */
226         focus_startup();
227         window_startup();
228         plugin_startup();
229         /* load the plugins specified in the pluginrc */
230         plugin_loadall();
231
232         /* set up the kernel config shit */
233         config_startup();
234         menu_startup();
235         /* parse/load user options */
236         if (parse_load_rc(&doc, &node))
237             parse_tree(doc, node->xmlChildrenNode, NULL);
238         /* we're done with parsing now, kill it */
239         parse_shutdown();
240
241         /* load the theme specified in the rc file */
242         ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
243         if (ob_rr_theme == NULL)
244             exit_with_error("Unable to load a theme.");
245
246         moveresize_startup();
247         screen_startup();
248         group_startup();
249         client_startup();
250         dock_startup();
251
252         /* call startup for all the plugins */
253         plugin_startall();
254
255         /* get all the existing windows */
256         client_manage_all();
257
258         state = OB_STATE_RUNNING;
259         while (!shutdown)
260             event_loop();
261         state = OB_STATE_EXITING;
262
263         dock_remove_all();
264         client_unmanage_all();
265
266         plugin_shutdown(); /* calls all the plugins' shutdown functions */
267         dock_shutdown();
268         client_shutdown();
269         group_shutdown();
270         screen_shutdown();
271         focus_shutdown();
272         moveresize_shutdown();
273         menu_shutdown();
274         window_shutdown();
275         grab_shutdown();
276         event_shutdown();
277         timer_shutdown();
278         config_shutdown();
279     }
280
281     dispatch_shutdown();
282
283     RrThemeFree(ob_rr_theme);
284     RrInstanceFree(ob_rr_inst);
285
286     sm_shutdown();
287
288     XCloseDisplay(ob_display);
289
290     if (restart) {
291         if (restart_path != NULL) {
292             int argcp;
293             char **argvp;
294             GError *err = NULL;
295
296             /* run other shit */
297             if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
298                 execvp(argvp[0], argvp);
299                 g_strfreev(argvp);
300             } else {
301                 g_warning("failed to execute '%s': %s", restart_path,
302                           err->message);
303             }
304         }
305
306         /* re-run me */
307         execvp(argv[0], argv); /* try how we were run */
308     }
309      
310     return 0;
311 }
312
313 static void sm_startup(int argc, char **argv)
314 {
315 #ifdef USE_SM
316
317 #define SM_ERR_LEN 1024
318
319     SmcCallbacks cb;
320     char sm_err[SM_ERR_LEN];
321
322     cb.save_yourself.callback = sm_save_yourself;
323     cb.save_yourself.client_data = NULL;
324
325     cb.die.callback = sm_die;
326     cb.die.client_data = NULL;
327
328     cb.save_complete.callback = sm_save_complete;
329     cb.save_complete.client_data = NULL;
330
331     cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
332     cb.shutdown_cancelled.client_data = NULL;
333
334     ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
335                                    SmcSaveYourselfProcMask |
336                                    SmcDieProcMask |
337                                    SmcSaveCompleteProcMask |
338                                    SmcShutdownCancelledProcMask,
339                                    &cb, ob_sm_id, &ob_sm_id,
340                                    SM_ERR_LEN, sm_err);
341     if (ob_sm_conn == NULL)
342         g_warning("Failed to connect to session manager: %s", sm_err);
343     else {
344         SmPropValue val_prog;
345         SmPropValue val_uid;
346         SmPropValue val_hint; 
347         SmPropValue val_pri;
348         SmPropValue val_pid;
349         SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
350         SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
351         SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
352         SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
353         SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
354         SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
355         SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
356         SmProp *props[7];
357         gulong hint, pri;
358         gchar pid[32];
359         gint i, j;
360         gboolean has_id;
361
362         for (i = 1; i < argc - 1; ++i)
363             if (strcmp(argv[i], "--sm-client-id") == 0)
364                 break;
365         has_id = (i < argc - 1);
366
367         prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
368         prop_cmd.num_vals = (has_id ? argc-2 : argc);
369         for (i = 0, j = 0; i < argc; ++i, ++j) {
370             if (strcmp (argv[i], "--sm-client-id") == 0) {
371                 ++i, --j; /* skip the next as well, keep j where it is */
372             } else {
373                 prop_cmd.vals[j].value = argv[i];
374                 prop_cmd.vals[j].length = strlen(argv[i]);
375             }
376         }
377
378         prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
379         prop_res.num_vals = (has_id ? argc : argc+2);
380         for (i = 0, j = 0; i < argc; ++i, ++j) { 
381             if (strcmp (argv[i], "--sm-client-id") == 0) {
382                 ++i, --j; /* skip the next as well, keep j where it is */
383             } else {
384                 prop_res.vals[j].value = argv[i];
385                 prop_res.vals[j].length = strlen(argv[i]);
386             }
387         }
388         prop_res.vals[j].value = "--sm-client-id";
389         prop_res.vals[j++].length = strlen("--sm-client-id");
390         prop_res.vals[j].value = ob_sm_id;
391         prop_res.vals[j++].length = strlen(ob_sm_id);
392
393         val_prog.value = argv[0];
394         val_prog.length = strlen(argv[0]);
395
396         val_uid.value = g_strdup(g_get_user_name());
397         val_uid.length = strlen(val_uid.value);
398
399         hint = SmRestartImmediately;
400         val_hint.value = &hint;
401         val_hint.length = 1;
402
403         sprintf(pid, "%ld", (long)getpid());
404         val_pid.value = pid;
405         val_pid.length = strlen(pid);
406
407         /* priority with gnome-session-manager, low to run before other apps */
408         pri = 20;
409         val_pri.value = &pri;
410         val_pri.length = 1;
411
412         prop_prog.vals = &val_prog;
413         prop_uid.vals = &val_uid;
414         prop_hint.vals = &val_hint;
415         prop_pid.vals = &val_pid;
416         prop_pri.vals = &val_pri;
417
418         props[0] = &prop_prog;
419         props[1] = &prop_cmd;
420         props[2] = &prop_res;
421         props[3] = &prop_uid;
422         props[4] = &prop_hint;
423         props[5] = &prop_pid;
424         props[6] = &prop_pri;
425
426         SmcSetProperties(ob_sm_conn, 7, props);
427
428         g_free(val_uid.value);
429         g_free(prop_cmd.vals);
430         g_free(prop_res.vals);
431
432         g_message("Connected to session manager with id %s", ob_sm_id);
433     }
434     g_free (ob_sm_id);
435 #endif
436 }
437
438 static void sm_shutdown()
439 {
440 #ifdef USE_SM
441     if (ob_sm_conn) {
442         SmPropValue val_hint;
443         SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
444         SmProp *props[1];
445         gulong hint;
446
447         /* when we exit, we want to reset this to a more friendly state */
448         hint = SmRestartIfRunning;
449         val_hint.value = &hint;
450         val_hint.length = 1;
451
452         prop_hint.vals = &val_hint;
453
454         props[0] = &prop_hint;
455
456         SmcSetProperties(ob_sm_conn, 1, props);
457
458         SmcCloseConnection(ob_sm_conn, 0, NULL);
459     }
460 #endif
461 }
462
463 static void signal_handler(const ObEvent *e, void *data)
464 {
465     int s;
466
467     s = e->data.s.signal;
468     switch (s) {
469     case SIGUSR1:
470         fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
471         ob_restart();
472         break;
473
474     case SIGHUP:
475     case SIGINT:
476     case SIGTERM:
477     case SIGPIPE:
478         fprintf(stderr, "Caught signal %d. Exiting.", s);
479         ob_exit();
480         break;
481
482     case SIGFPE:
483     case SIGSEGV:
484         fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
485         abort();
486     }
487 }
488
489 static void print_version()
490 {
491     g_print("Openbox %s\n\n", PACKAGE_VERSION);
492     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
493     g_print("This is free software, and you are welcome to redistribute it\n");
494     g_print("under certain conditions. See the file COPYING for details.\n\n");
495 }
496
497 static void print_help()
498 {
499     print_version();
500     g_print("Syntax: openbox [options]\n\n");
501     g_print("Options:\n\n");
502 #ifdef USE_SM
503     g_print("  --sm-client-id ID  Specify session management ID\n");
504     g_print("  --sm-disable       Disable connection to session manager\n");
505 #endif
506     g_print("  --help             Display this help and exit\n");
507     g_print("  --version          Display the version and exit\n");
508     g_print("  --sync             Run in synchronous mode (this is slow and\n"
509             "                     meant for debugging X routines)\n");
510     g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
511 }
512
513 static void parse_args(int argc, char **argv)
514 {
515     int i;
516
517     for (i = 1; i < argc; ++i) {
518         if (!strcmp(argv[i], "--version")) {
519             print_version();
520             exit(0);
521         } else if (!strcmp(argv[i], "--help")) {
522             print_help();
523             exit(0);
524         } else if (!strcmp(argv[i], "--sync")) {
525             xsync = TRUE;
526 #ifdef USE_SM
527         } else if (!strcmp(argv[i], "--sm-client-id")) {
528             if (i == argc - 1) /* no args left */
529                 g_printerr(_("--sm-client-id requires an argument\n"));
530             else
531                 ob_sm_id = argv[++i];
532         } else if (!strcmp(argv[i], "--sm-disable")) {
533             ob_sm_use = FALSE;
534 #endif
535         } else {
536             g_printerr("Invalid option: '%s'\n\n", argv[i]);
537             print_help();
538             exit(1);
539         }
540     }
541 }
542
543 #ifdef USE_SM
544 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
545                              Bool shutdown, int interact_style, Bool fast)
546 {
547     g_message("got SAVE YOURSELF from session manager");
548     SmcSaveYourselfDone(conn, TRUE);
549 }
550
551 static void sm_die(SmcConn conn, SmPointer data)
552 {
553     ob_exit();
554     g_message("got DIE from session manager");
555 }
556
557 static void sm_save_complete(SmcConn conn, SmPointer data)
558 {
559     g_message("got SAVE COMPLETE from session manager");
560 }
561
562 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
563 {
564     g_message("got SHUTDOWN CANCELLED from session manager");
565 }
566 #endif
567
568 static void exit_with_error(gchar *msg)
569 {
570     g_critical(msg);
571     sm_shutdown();
572     exit(EXIT_FAILURE);
573 }
574
575 void ob_restart_other(const gchar *path)
576 {
577     restart_path = g_strdup(path);
578     ob_restart();
579 }
580
581 void ob_restart()
582 {
583     restart = TRUE;
584     ob_exit();
585 }
586
587 void ob_exit()
588 {
589     shutdown = TRUE;
590 }
591
592 Cursor ob_cursor(ObCursor cursor)
593 {
594     g_assert(cursor < OB_NUM_CURSORS);
595     return ob_cursors[cursor];
596 }
597
598 KeyCode ob_keycode(ObKey key)
599 {
600     g_assert(key < OB_NUM_KEYS);
601     return ob_keys[key];
602 }
603
604 ObState ob_state()
605 {
606     return state;
607 }