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