10 #include "moveresize.h"
12 #include "extensions.h"
20 #include "render/render.h"
21 #include "render/font.h"
22 #include "render/theme.h"
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
41 # include <sys/types.h>
44 #include <X11/cursorfont.h>
46 Display *ob_display = NULL;
50 gboolean ob_shutdown = FALSE;
51 gboolean ob_restart = FALSE;
52 char *ob_restart_path = NULL;
53 gboolean ob_remote = TRUE;
54 gboolean ob_sync = FALSE;
56 char *ob_rc_path = NULL;
58 void signal_handler(const ObEvent *e, void *data);
59 void parse_args(int argc, char **argv);
61 int main(int argc, char **argv)
63 struct sigaction action;
68 ob_state = State_Starting;
70 /* initialize the locale */
71 if (!setlocale(LC_ALL, ""))
72 g_warning("Couldn't set locale from environment.\n");
73 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
74 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
75 textdomain(PACKAGE_NAME);
77 /* start our event dispatcher and register for signals */
79 dispatch_register(Event_Signal, signal_handler, NULL);
81 /* set up signal handler */
83 action.sa_handler = dispatch_signal;
84 action.sa_mask = sigset;
85 action.sa_flags = SA_NOCLDSTOP;
86 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
87 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
88 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
89 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
90 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
91 sigaction(SIGINT, &action, (struct sigaction *) NULL);
92 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
94 /* create the ~/.openbox dir */
95 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
96 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
97 S_IROTH | S_IWOTH | S_IXOTH));
99 /* create the ~/.openbox/themes dir */
100 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
101 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
102 S_IROTH | S_IWOTH | S_IXOTH));
105 /* parse out command line args */
106 parse_args(argc, argv);
108 ob_display = XOpenDisplay(NULL);
109 if (ob_display == NULL) {
110 /* print a message and exit */
111 g_critical("Failed to open the display.");
114 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
115 /* print a message and exit */
116 g_critical("Failed to set display as close-on-exec.");
120 ob_screen = DefaultScreen(ob_display);
121 ob_root = RootWindow(ob_display, ob_screen);
123 /* XXX fork self onto other screens */
125 XSynchronize(ob_display, ob_sync);
127 /* check for locale support */
128 if (!XSupportsLocale())
129 g_warning("X server does not support locale.");
130 if (!XSetLocaleModifiers(""))
131 g_warning("Cannot set locale modifiers for the X server.");
133 /* set our error handler */
134 XSetErrorHandler(xerror_handler);
136 /* set the DISPLAY environment variable for any lauched children, to the
137 display we're using, so they open in the right place. */
138 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
140 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
141 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
142 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
143 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
144 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
145 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
146 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
147 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
148 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
149 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
151 prop_startup(); /* get atoms values for the display */
152 extensions_query_all(); /* find which extensions are present */
154 if (screen_annex()) { /* it will be ours! */
155 /* startup the parsing so everything can register sections of the rc */
158 /* anything that is going to read data from the rc file needs to be
165 moveresize_startup();
168 /* load the plugins specified in the pluginrc */
171 /* set up the kernel config shit */
173 /* parse/load user options */
175 /* we're done with parsing now, kill it */
178 /* load the theme specified in the rc file */
179 theme = theme_load(config_theme);
181 if (!theme) return 1;
191 /* call startup for all the plugins */
194 /* get all the existing windows */
197 ob_state = State_Running;
200 ob_state = State_Exiting;
202 client_unmanage_all();
204 plugin_shutdown(); /* calls all the plugins' shutdown functions */
222 XCloseDisplay(ob_display);
225 if (ob_restart_path != NULL) {
231 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
232 execvp(argvp[0], argvp);
235 g_warning("failed to execute '%s': %s", ob_restart_path,
241 execvp(argv[0], argv); /* try how we were run */
242 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
248 void signal_handler(const ObEvent *e, void *data)
252 s = e->data.s.signal;
255 g_message("Caught SIGUSR1 signal. Restarting.");
256 ob_shutdown = ob_restart = TRUE;
263 g_message("Caught signal %d. Exiting.", s);
269 g_error("Caught signal %d. Aborting and dumping core.", s);
275 g_print("Openbox %s\n\n", PACKAGE_VERSION);
276 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
277 g_print("This is free software, and you are welcome to redistribute it\n");
278 g_print("under certain conditions. See the file COPYING for details.\n\n");
284 g_print("Syntax: %s [options]\n\n", BINARY);
285 g_print("Options:\n\n");
286 g_print(" -rc PATH Specify the path to the rc file to use\n");
287 g_print(" -help Display this help and exit\n");
288 g_print(" -version Display the version and exit\n");
289 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
290 " for debugging X routines)\n");
291 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
294 void parse_args(int argc, char **argv)
298 for (i = 1; i < argc; ++i) {
299 if (!strcmp(argv[i], "-version")) {
302 } else if (!strcmp(argv[i], "-help")) {
305 } else if (!strcmp(argv[i], "-sync")) {
307 } else if (!strcmp(argv[i], "-rc")) {
308 if (i == argc - 1) /* no args left */
309 g_printerr("-rc requires an argument\n");
311 ob_rc_path = argv[++i];
313 g_printerr("Invalid option: '%s'\n\n", argv[i]);
320 gboolean ob_pointer_pos(int *x, int *y)
326 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);