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 RrInstance *ob_rr_inst = NULL;
52 RrTheme *ob_rr_theme = NULL;
53 Display *ob_display = NULL;
57 gboolean ob_shutdown = FALSE;
58 gboolean ob_restart = FALSE;
59 char *ob_restart_path = NULL;
60 gboolean ob_remote = TRUE;
61 gboolean ob_sync = FALSE;
63 char *ob_rc_path = NULL;
65 static void signal_handler(const ObEvent *e, void *data);
66 static void parse_args(int argc, char **argv);
67 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
68 Bool shutdown, int interact_style, Bool fast);
69 static void sm_die(SmcConn conn, SmPointer data);
70 static void sm_save_complete(SmcConn conn, SmPointer data);
71 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
72 static void exit_with_error(gchar *msg);
74 int main(int argc, char **argv)
76 struct sigaction action;
82 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, NULL, &sm_id, SM_ERR_LEN, sm_err);
149 if (ob_sm_conn == NULL)
150 g_warning("Failed to connect to session manager: %s", sm_err);
152 g_message("Connected to session manager with id %s", sm_id);
156 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
159 ob_screen = DefaultScreen(ob_display);
160 ob_root = RootWindow(ob_display, ob_screen);
162 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
163 if (ob_rr_inst == NULL)
164 exit_with_error("Failed to initialize the render library.");
166 /* XXX fork self onto other screens */
168 XSynchronize(ob_display, ob_sync);
170 /* check for locale support */
171 if (!XSupportsLocale())
172 g_warning("X server does not support locale.");
173 if (!XSetLocaleModifiers(""))
174 g_warning("Cannot set locale modifiers for the X server.");
176 /* set our error handler */
177 XSetErrorHandler(xerror_handler);
179 /* set the DISPLAY environment variable for any lauched children, to the
180 display we're using, so they open in the right place. */
181 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
183 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
184 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
185 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
186 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
187 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
188 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
189 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
190 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
191 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
192 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
193 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
195 prop_startup(); /* get atoms values for the display */
196 extensions_query_all(); /* find which extensions are present */
198 /* save stuff that we can use to restore state */
201 if (screen_annex()) { /* it will be ours! */
202 /* startup the parsing so everything can register sections of the rc */
205 /* anything that is going to read data from the rc file needs to be
211 /* load the plugins specified in the pluginrc */
214 /* set up the kernel config shit */
216 /* parse/load user options */
217 if (parse_load_rc(&doc, &node))
218 parse_tree(doc, node->xmlChildrenNode, NULL);
219 /* we're done with parsing now, kill it */
222 /* load the theme specified in the rc file */
223 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
224 if (ob_rr_theme == NULL)
225 exit_with_error("Unable to load a theme.");
230 moveresize_startup();
237 /* call startup for all the plugins */
240 /* get all the existing windows */
243 ob_state = State_Running;
246 ob_state = State_Exiting;
249 client_unmanage_all();
251 plugin_shutdown(); /* calls all the plugins' shutdown functions */
257 moveresize_shutdown();
269 RrThemeFree(ob_rr_theme);
270 RrInstanceFree(ob_rr_inst);
273 SmcCloseConnection(ob_sm_conn, 0, NULL);
274 XCloseDisplay(ob_display);
277 if (ob_restart_path != NULL) {
283 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
284 execvp(argvp[0], argvp);
287 g_warning("failed to execute '%s': %s", ob_restart_path,
293 execvp(argv[0], argv); /* try how we were run */
294 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
300 static void signal_handler(const ObEvent *e, void *data)
304 s = e->data.s.signal;
307 g_message("Caught SIGUSR1 signal. Restarting.");
308 ob_shutdown = ob_restart = TRUE;
315 g_message("Caught signal %d. Exiting.", s);
321 g_message("Caught signal %d. Aborting and dumping core.", s);
326 static void print_version()
328 g_print("Openbox %s\n\n", PACKAGE_VERSION);
329 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
330 g_print("This is free software, and you are welcome to redistribute it\n");
331 g_print("under certain conditions. See the file COPYING for details.\n\n");
334 static void print_help()
337 g_print("Syntax: %s [options]\n\n", BINARY);
338 g_print("Options:\n\n");
339 g_print(" -rc PATH Specify the path to the rc file to use\n");
340 g_print(" -help Display this help and exit\n");
341 g_print(" -version Display the version and exit\n");
342 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
343 " for debugging X routines)\n");
344 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
347 static void parse_args(int argc, char **argv)
351 for (i = 1; i < argc; ++i) {
352 if (!strcmp(argv[i], "-version")) {
355 } else if (!strcmp(argv[i], "-help")) {
358 } else if (!strcmp(argv[i], "-sync")) {
360 } else if (!strcmp(argv[i], "-rc")) {
361 if (i == argc - 1) /* no args left */
362 g_printerr("-rc requires an argument\n");
364 ob_rc_path = argv[++i];
366 g_printerr("Invalid option: '%s'\n\n", argv[i]);
373 gboolean ob_pointer_pos(int *x, int *y)
379 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
382 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
383 Bool shutdown, int interact_style, Bool fast)
385 g_message("got SAVE YOURSELF from session manager");
386 SmcSaveYourselfDone(conn, TRUE);
389 static void sm_die(SmcConn conn, SmPointer data)
392 g_message("got DIE from session manager");
395 static void sm_save_complete(SmcConn conn, SmPointer data)
397 g_message("got SAVE COMPLETE from session manager");
400 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
402 g_message("got SHUTDOWN CANCELLED from session manager");
405 static void exit_with_error(gchar *msg)
409 SmcCloseConnection(ob_sm_conn, 1, &msg);