12 #include "moveresize.h"
14 #include "extensions.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
45 #include <X11/SM/SMlib.h>
46 #include <X11/cursorfont.h>
48 #define SM_ERR_LEN 1024
51 gchar *ob_sm_id = NULL;
52 RrInstance *ob_rr_inst = NULL;
53 RrTheme *ob_rr_theme = NULL;
54 Display *ob_display = NULL;
58 gboolean ob_shutdown = FALSE;
59 gboolean ob_restart = FALSE;
60 char *ob_restart_path = NULL;
61 gboolean ob_remote = TRUE;
62 gboolean ob_sync = FALSE;
64 char *ob_rc_path = NULL;
66 static void signal_handler(const ObEvent *e, void *data);
67 static void parse_args(int argc, char **argv);
68 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
69 Bool shutdown, int interact_style, Bool fast);
70 static void sm_die(SmcConn conn, SmPointer data);
71 static void sm_save_complete(SmcConn conn, SmPointer data);
72 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
73 static void exit_with_error(gchar *msg);
75 int main(int argc, char **argv)
77 struct sigaction action;
83 char sm_err[SM_ERR_LEN];
85 ob_state = State_Starting;
87 /* initialize the locale */
88 if (!setlocale(LC_ALL, ""))
89 g_warning("Couldn't set locale from environment.\n");
90 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
91 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
92 textdomain(PACKAGE_NAME);
94 /* start our event dispatcher and register for signals */
96 dispatch_register(Event_Signal, signal_handler, NULL);
98 /* set up signal handler */
100 action.sa_handler = dispatch_signal;
101 action.sa_mask = sigset;
102 action.sa_flags = SA_NOCLDSTOP;
103 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
104 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
105 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
106 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
107 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
108 sigaction(SIGINT, &action, (struct sigaction *) NULL);
109 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
111 /* create the ~/.openbox dir */
112 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
113 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
114 S_IROTH | S_IWOTH | S_IXOTH));
116 /* create the ~/.openbox/themes dir */
117 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
118 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
119 S_IROTH | S_IWOTH | S_IXOTH));
122 /* parse out command line args */
123 parse_args(argc, argv);
125 ob_display = XOpenDisplay(NULL);
126 if (ob_display == NULL)
127 exit_with_error("Failed to open the display.");
128 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
129 exit_with_error("Failed to set display as close-on-exec.");
131 cb.save_yourself.callback = sm_save_yourself;
132 cb.save_yourself.client_data = NULL;
134 cb.die.callback = sm_die;
135 cb.die.client_data = NULL;
137 cb.save_complete.callback = sm_save_complete;
138 cb.save_complete.client_data = NULL;
140 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
141 cb.shutdown_cancelled.client_data = NULL;
143 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
144 SmcSaveYourselfProcMask |
146 SmcSaveCompleteProcMask |
147 SmcShutdownCancelledProcMask,
148 &cb, ob_sm_id, &ob_sm_id,
150 if (ob_sm_conn == NULL)
151 g_warning("Failed to connect to session manager: %s", sm_err);
155 SmPropValue val_prog;
157 SmProp prop_cmd = { SmCloneCommand, "SmLISTofARRAY8", 1, };
158 SmProp prop_res = { SmRestartCommand, "SmLISTofARRAY8", 1, };
159 SmProp prop_prog = { SmProgram, "SmARRAY8", 1, };
160 SmProp prop_uid = { SmUserID, "SmARRAY8", 1, };
164 val_cmd.value = argv[0];
165 val_cmd.length = strlen(argv[0]);
166 val_res.value = argv[0];
167 val_res.length = strlen(argv[0]); /* XXX -id foo */
168 val_prog.value = argv[0];
169 val_prog.length = strlen(argv[0]);
171 user = g_strdup_printf("%ld", (long)getuid());
172 val_uid.value = user;
173 val_uid.length = strlen(user);
175 prop_cmd.vals = &val_cmd;
176 prop_res.vals = &val_res;
177 prop_prog.vals = &val_prog;
178 prop_uid.vals = &val_uid;
180 props[0] = &prop_cmd;
181 props[1] = &prop_res;
182 props[2] = &prop_prog;
183 props[3] = &prop_uid;
185 SmcSetProperties(ob_sm_conn, 3, props);
189 g_message("Connected to session manager with id %s", ob_sm_id);
194 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
197 ob_screen = DefaultScreen(ob_display);
198 ob_root = RootWindow(ob_display, ob_screen);
200 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
201 if (ob_rr_inst == NULL)
202 exit_with_error("Failed to initialize the render library.");
204 /* XXX fork self onto other screens */
206 XSynchronize(ob_display, ob_sync);
208 /* check for locale support */
209 if (!XSupportsLocale())
210 g_warning("X server does not support locale.");
211 if (!XSetLocaleModifiers(""))
212 g_warning("Cannot set locale modifiers for the X server.");
214 /* set our error handler */
215 XSetErrorHandler(xerror_handler);
217 /* set the DISPLAY environment variable for any lauched children, to the
218 display we're using, so they open in the right place. */
219 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
221 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
222 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
223 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
224 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
225 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
226 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
227 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
228 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
229 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
230 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
231 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
233 prop_startup(); /* get atoms values for the display */
234 extensions_query_all(); /* find which extensions are present */
236 /* save stuff that we can use to restore state */
239 if (screen_annex()) { /* it will be ours! */
240 /* startup the parsing so everything can register sections of the rc */
243 /* anything that is going to read data from the rc file needs to be
249 /* load the plugins specified in the pluginrc */
252 /* set up the kernel config shit */
254 /* parse/load user options */
255 if (parse_load_rc(&doc, &node))
256 parse_tree(doc, node->xmlChildrenNode, NULL);
257 /* we're done with parsing now, kill it */
260 /* load the theme specified in the rc file */
261 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
262 if (ob_rr_theme == NULL)
263 exit_with_error("Unable to load a theme.");
268 moveresize_startup();
275 /* call startup for all the plugins */
278 /* get all the existing windows */
281 ob_state = State_Running;
284 ob_state = State_Exiting;
287 client_unmanage_all();
289 plugin_shutdown(); /* calls all the plugins' shutdown functions */
295 moveresize_shutdown();
307 RrThemeFree(ob_rr_theme);
308 RrInstanceFree(ob_rr_inst);
311 SmcCloseConnection(ob_sm_conn, 0, NULL);
312 XCloseDisplay(ob_display);
315 if (ob_restart_path != NULL) {
321 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
322 execvp(argvp[0], argvp);
325 g_warning("failed to execute '%s': %s", ob_restart_path,
331 execvp(argv[0], argv); /* try how we were run */
332 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
338 static void signal_handler(const ObEvent *e, void *data)
342 s = e->data.s.signal;
345 g_message("Caught SIGUSR1 signal. Restarting.");
346 ob_shutdown = ob_restart = TRUE;
353 g_message("Caught signal %d. Exiting.", s);
359 g_message("Caught signal %d. Aborting and dumping core.", s);
364 static void print_version()
366 g_print("Openbox %s\n\n", PACKAGE_VERSION);
367 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
368 g_print("This is free software, and you are welcome to redistribute it\n");
369 g_print("under certain conditions. See the file COPYING for details.\n\n");
372 static void print_help()
375 g_print("Syntax: %s [options]\n\n", BINARY);
376 g_print("Options:\n\n");
377 g_print(" -rc PATH Specify the path to the rc file to use\n");
378 g_print(" -help Display this help and exit\n");
379 g_print(" -version Display the version and exit\n");
380 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
381 " for debugging X routines)\n");
382 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
385 static void parse_args(int argc, char **argv)
389 for (i = 1; i < argc; ++i) {
390 if (!strcmp(argv[i], "-version")) {
393 } else if (!strcmp(argv[i], "-help")) {
396 } else if (!strcmp(argv[i], "-sync")) {
398 } else if (!strcmp(argv[i], "-rc")) {
399 if (i == argc - 1) /* no args left */
400 g_printerr("-rc requires an argument\n");
402 ob_rc_path = argv[++i];
404 g_printerr("Invalid option: '%s'\n\n", argv[i]);
411 gboolean ob_pointer_pos(int *x, int *y)
417 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
420 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
421 Bool shutdown, int interact_style, Bool fast)
423 g_message("got SAVE YOURSELF from session manager");
424 SmcSaveYourselfDone(conn, TRUE);
427 static void sm_die(SmcConn conn, SmPointer data)
430 g_message("got DIE from session manager");
433 static void sm_save_complete(SmcConn conn, SmPointer data)
435 g_message("got SAVE COMPLETE from session manager");
438 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
440 g_message("got SHUTDOWN CANCELLED from session manager");
443 static void exit_with_error(gchar *msg)
447 SmcCloseConnection(ob_sm_conn, 1, &msg);