]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
move the openbox engine into librender and the kernel. the theme is loaded and stored...
[chaz/openbox] / openbox / openbox.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "client.h"
4 #include "dispatch.h"
5 #include "xerror.h"
6 #include "prop.h"
7 #include "screen.h"
8 #include "focus.h"
9 #include "frame.h"
10 #include "extensions.h"
11 #include "parse.h"
12 #include "grab.h"
13 #include "plugin.h"
14 #include "timer.h"
15 #include "group.h"
16 #include "config.h"
17 #include "gettext.h"
18 #include "render/render.h"
19 #include "render/font.h"
20 #include "render/theme.h"
21
22 #ifdef HAVE_FCNTL_H
23 # include <fcntl.h>
24 #endif
25 #ifdef HAVE_SIGNAL_H
26 # include <signal.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/types.h>
33 # include <sys/wait.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 #include <X11/cursorfont.h>
47
48 Display *ob_display = NULL;
49 int ob_screen;
50 Window ob_root;
51 State ob_state;
52 gboolean ob_shutdown = FALSE;
53 gboolean ob_restart = FALSE;
54 char *ob_restart_path = NULL;
55 gboolean ob_remote = TRUE;
56 gboolean ob_sync = FALSE;
57 Cursors ob_cursors;
58 char *ob_rc_path = NULL;
59
60 void signal_handler(const ObEvent *e, void *data);
61 void parse_args(int argc, char **argv);
62
63 int main(int argc, char **argv)
64 {
65 struct sigaction action;
66 sigset_t sigset;
67 char *path;
68 char *theme;
69
70 ob_state = State_Starting;
71
72 /* initialize the locale */
73 if (!setlocale(LC_ALL, ""))
74 g_warning("Couldn't set locale from environment.\n");
75 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
76 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
77 textdomain(PACKAGE_NAME);
78
79 /* start our event dispatcher and register for signals */
80 dispatch_startup();
81 dispatch_register(Event_Signal, signal_handler, NULL);
82
83 /* set up signal handler */
84 sigemptyset(&sigset);
85 action.sa_handler = dispatch_signal;
86 action.sa_mask = sigset;
87 action.sa_flags = SA_NOCLDSTOP;
88 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
89 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
90 sigaction(SIGSEGV, &action, (struct sigaction *) NULL);
91 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
92 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
93 sigaction(SIGINT, &action, (struct sigaction *) NULL);
94 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
95 sigaction(SIGCHLD, &action, (struct sigaction *) NULL);
96
97 /* anything that died while we were restarting won't give us a SIGCHLD */
98 while (waitpid(-1, NULL, WNOHANG) > 0);
99
100 /* create the ~/.openbox dir */
101 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
102 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
103 S_IROTH | S_IWOTH | S_IXOTH));
104 g_free(path);
105 /* create the ~/.openbox/themes dir */
106 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
107 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
108 S_IROTH | S_IWOTH | S_IXOTH));
109 g_free(path);
110
111 /* parse out command line args */
112 parse_args(argc, argv);
113
114 ob_display = XOpenDisplay(NULL);
115 if (ob_display == NULL) {
116 /* print a message and exit */
117 g_critical("Failed to open the display.");
118 exit(1);
119 }
120 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1) {
121 /* print a message and exit */
122 g_critical("Failed to set display as close-on-exec.");
123 exit(1);
124 }
125
126 ob_screen = DefaultScreen(ob_display);
127 ob_root = RootWindow(ob_display, ob_screen);
128
129 /* XXX fork self onto other screens */
130
131 XSynchronize(ob_display, ob_sync);
132
133 /* check for locale support */
134 if (!XSupportsLocale())
135 g_warning("X server does not support locale.");
136 if (!XSetLocaleModifiers(""))
137 g_warning("Cannot set locale modifiers for the X server.");
138
139 /* set our error handler */
140 XSetErrorHandler(xerror_handler);
141
142 /* set the DISPLAY environment variable for any lauched children, to the
143 display we're using, so they open in the right place. */
144 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
145
146 ob_cursors.left_ptr = XCreateFontCursor(ob_display, XC_left_ptr);
147 ob_cursors.ll_angle = XCreateFontCursor(ob_display, XC_ll_angle);
148 ob_cursors.lr_angle = XCreateFontCursor(ob_display, XC_lr_angle);
149
150 prop_startup(); /* get atoms values for the display */
151 extensions_query_all(); /* find which extensions are present */
152
153 if (screen_annex()) { /* it will be ours! */
154 /* startup the parsing so everything can register sections of the rc */
155 parse_startup();
156
157 /* anything that is going to read data from the rc file needs to be
158 in this group */
159 timer_startup();
160 render_startup();
161 font_startup();
162 theme_startup();
163 event_startup();
164 grab_startup();
165 plugin_startup();
166 /* load the plugins specified in the pluginrc */
167 plugin_loadall();
168
169 /* set up the kernel config shit */
170 config_startup();
171 /* parse/load user options */
172 parse_rc();
173 /* we're done with parsing now, kill it */
174 parse_shutdown();
175
176 /* load the theme specified in the rc file */
177 theme = theme_load("ebox"); /* woot i like this theme :) */
178 g_free(theme);
179 if (!theme) return 1;
180
181 frame_startup();
182 focus_startup();
183 screen_startup();
184 group_startup();
185 client_startup();
186
187 /* call startup for all the plugins */
188 plugin_startall();
189
190 /* get all the existing windows */
191 client_manage_all();
192
193 ob_state = State_Running;
194 while (!ob_shutdown)
195 event_loop();
196 ob_state = State_Exiting;
197
198 client_unmanage_all();
199
200 plugin_shutdown(); /* calls all the plugins' shutdown functions */
201 client_shutdown();
202 group_shutdown();
203 screen_shutdown();
204 focus_shutdown();
205 frame_shutdown();
206 grab_shutdown();
207 event_shutdown();
208 theme_shutdown();
209 render_shutdown();
210 timer_shutdown();
211 config_shutdown();
212 }
213
214 dispatch_shutdown();
215
216 XCloseDisplay(ob_display);
217
218 if (ob_restart) {
219 if (ob_restart_path != NULL) {
220 int argcp;
221 char **argvp;
222 GError *err = NULL;
223
224 /* run other shit */
225 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
226 execvp(argvp[0], argvp);
227 g_strfreev(argvp);
228 } else {
229 g_warning("failed to execute '%s': %s", ob_restart_path,
230 err->message);
231 }
232 }
233
234 /* re-run me */
235 execvp(argv[0], argv); /* try how we were run */
236 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
237 }
238
239 return 0;
240 }
241
242 void signal_handler(const ObEvent *e, void *data)
243 {
244 int s;
245
246 s = e->data.s.signal;
247 switch (s) {
248 case SIGUSR1:
249 g_message("Caught SIGUSR1 signal. Restarting.");
250 ob_shutdown = ob_restart = TRUE;
251 break;
252
253 case SIGCHLD:
254 wait(NULL);
255 break;
256
257 case SIGHUP:
258 case SIGINT:
259 case SIGTERM:
260 case SIGPIPE:
261 g_message("Caught signal %d. Exiting.", s);
262 ob_shutdown = TRUE;
263 break;
264
265 case SIGFPE:
266 case SIGSEGV:
267 g_error("Caught signal %d. Aborting and dumping core.", s);
268 }
269 }
270
271 void print_version()
272 {
273 g_print("Openbox %s\n\n", PACKAGE_VERSION);
274 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
275 g_print("This is free software, and you are welcome to redistribute it\n");
276 g_print("under certain conditions. See the file COPYING for details.\n\n");
277 }
278
279 void print_help()
280 {
281 print_version();
282 g_print("Syntax: %s [options]\n\n", BINARY);
283 g_print("Options:\n\n");
284 g_print(" -rc PATH Specify the path to the rc file to use\n");
285 g_print(" -help Display this help and exit\n");
286 g_print(" -version Display the version and exit\n");
287 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
288 " for debugging X routines)\n");
289 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
290 }
291
292 void parse_args(int argc, char **argv)
293 {
294 int i;
295
296 for (i = 1; i < argc; ++i) {
297 if (!strcmp(argv[i], "-version")) {
298 print_version();
299 exit(0);
300 } else if (!strcmp(argv[i], "-help")) {
301 print_help();
302 exit(0);
303 } else if (!strcmp(argv[i], "-sync")) {
304 ob_sync = TRUE;
305 } else if (!strcmp(argv[i], "-rc")) {
306 if (i == argc - 1) /* no args left */
307 g_printerr("-rc requires an argument\n");
308 else
309 ob_rc_path = argv[++i];
310 } else {
311 g_printerr("Invalid option: '%s'\n\n", argv[i]);
312 print_help();
313 exit(1);
314 }
315 }
316 }
This page took 0.049647 seconds and 4 git commands to generate.