]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
lwindow needs a type
[chaz/openbox] / openbox / openbox.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "session.h"
4 #include "dock.h"
5 #include "event.h"
6 #include "menu.h"
7 #include "client.h"
8 #include "dispatch.h"
9 #include "xerror.h"
10 #include "prop.h"
11 #include "startup.h"
12 #include "screen.h"
13 #include "focus.h"
14 #include "moveresize.h"
15 #include "frame.h"
16 #include "extensions.h"
17 #include "grab.h"
18 #include "plugin.h"
19 #include "timer.h"
20 #include "group.h"
21 #include "config.h"
22 #include "gettext.h"
23 #include "parser/parse.h"
24 #include "render/render.h"
25 #include "render/theme.h"
26
27 #ifdef HAVE_FCNTL_H
28 # include <fcntl.h>
29 #endif
30 #ifdef HAVE_SIGNAL_H
31 #define __USE_UNIX98
32 # include <signal.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #endif
37 #ifdef HAVE_LOCALE_H
38 # include <locale.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47
48 #include <X11/cursorfont.h>
49
50 RrInstance *ob_rr_inst;
51 RrTheme *ob_rr_theme;
52 Display *ob_display;
53 gint ob_screen;
54 gboolean ob_sm_use = TRUE;
55 gchar *ob_sm_id;
56 gboolean ob_replace_wm;
57
58 static ObState state;
59 static gboolean xsync;
60 static gboolean shutdown;
61 static gboolean restart;
62 static char *restart_path;
63 static Cursor cursors[OB_NUM_CURSORS];
64 static KeyCode keys[OB_NUM_KEYS];
65 static gchar *sm_save_file;
66
67 static void signal_handler(const ObEvent *e, void *data);
68 static void parse_args(int argc, char **argv);
69
70 static void exit_with_error(gchar *msg);
71
72 int main(int argc, char **argv)
73 {
74 struct sigaction action;
75 sigset_t sigset;
76 char *path;
77 xmlDocPtr doc;
78 xmlNodePtr node;
79
80 #ifdef DEBUG
81 ob_debug_show_output(TRUE);
82 #endif
83
84 state = OB_STATE_STARTING;
85
86 /* initialize the locale */
87 if (!setlocale(LC_ALL, ""))
88 g_warning("Couldn't set locale from environment.\n");
89 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
90 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
91 textdomain(PACKAGE_NAME);
92
93 /* start our event dispatcher and register for signals */
94 dispatch_startup();
95 dispatch_register(Event_Signal, signal_handler, NULL);
96
97 /* set up signal handler */
98 sigemptyset(&sigset);
99 action.sa_handler = dispatch_signal;
100 action.sa_mask = sigset;
101 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
102 sigaction(SIGUSR1, &action, (struct sigaction *) NULL);
103 sigaction(SIGPIPE, &action, (struct sigaction *) NULL);
104 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
105 sigaction(SIGFPE, &action, (struct sigaction *) NULL);
106 sigaction(SIGTERM, &action, (struct sigaction *) NULL);
107 sigaction(SIGINT, &action, (struct sigaction *) NULL);
108 sigaction(SIGHUP, &action, (struct sigaction *) NULL);
109
110 /* create the ~/.openbox dir */
111 path = g_build_filename(g_get_home_dir(), ".openbox", NULL);
112 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
113 S_IROTH | S_IWOTH | S_IXOTH));
114 g_free(path);
115 /* create the ~/.openbox/themes dir */
116 path = g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL);
117 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
118 S_IROTH | S_IWOTH | S_IXOTH));
119 g_free(path);
120 /* create the ~/.openbox/sessions dir */
121 path = g_build_filename(g_get_home_dir(), ".openbox", "sessions", NULL);
122 mkdir(path, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP |
123 S_IROTH | S_IWOTH | S_IXOTH));
124 g_free(path);
125
126 g_set_prgname(argv[0]);
127
128 /* parse out command line args */
129 parse_args(argc, argv);
130
131 ob_display = XOpenDisplay(NULL);
132 if (ob_display == NULL)
133 exit_with_error("Failed to open the display.");
134 if (fcntl(ConnectionNumber(ob_display), F_SETFD, 1) == -1)
135 exit_with_error("Failed to set display as close-on-exec.");
136
137 session_load(sm_save_file);
138 session_startup(argc, argv);
139
140 #ifdef USE_LIBSN
141 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
142 #endif
143
144 ob_screen = DefaultScreen(ob_display);
145
146 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
147 if (ob_rr_inst == NULL)
148 exit_with_error("Failed to initialize the render library.");
149
150 /* XXX fork self onto other screens */
151
152 XSynchronize(ob_display, xsync);
153
154 /* check for locale support */
155 if (!XSupportsLocale())
156 g_warning("X server does not support locale.");
157 if (!XSetLocaleModifiers(""))
158 g_warning("Cannot set locale modifiers for the X server.");
159
160 /* set our error handler */
161 XSetErrorHandler(xerror_handler);
162
163 /* set the DISPLAY environment variable for any lauched children, to the
164 display we're using, so they open in the right place. */
165 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
166
167 /* create available cursors */
168 cursors[OB_CURSOR_POINTER] =
169 XCreateFontCursor(ob_display, XC_left_ptr);
170 cursors[OB_CURSOR_BUSY] =
171 XCreateFontCursor(ob_display, XC_watch);
172 cursors[OB_CURSOR_MOVE] =
173 XCreateFontCursor(ob_display, XC_fleur);
174 cursors[OB_CURSOR_NORTH] =
175 XCreateFontCursor(ob_display, XC_top_side);
176 cursors[OB_CURSOR_NORTHEAST] =
177 XCreateFontCursor(ob_display, XC_top_right_corner);
178 cursors[OB_CURSOR_EAST] =
179 XCreateFontCursor(ob_display, XC_right_side);
180 cursors[OB_CURSOR_SOUTHEAST] =
181 XCreateFontCursor(ob_display, XC_bottom_right_corner);
182 cursors[OB_CURSOR_SOUTH] =
183 XCreateFontCursor(ob_display, XC_bottom_side);
184 cursors[OB_CURSOR_SOUTHWEST] =
185 XCreateFontCursor(ob_display, XC_bottom_left_corner);
186 cursors[OB_CURSOR_WEST] =
187 XCreateFontCursor(ob_display, XC_left_side);
188 cursors[OB_CURSOR_NORTHWEST] =
189 XCreateFontCursor(ob_display, XC_top_left_corner);
190
191 /* create available keycodes */
192 keys[OB_KEY_RETURN] =
193 XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
194 keys[OB_KEY_ESCAPE] =
195 XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
196 keys[OB_KEY_LEFT] =
197 XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
198 keys[OB_KEY_RIGHT] =
199 XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
200 keys[OB_KEY_UP] =
201 XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
202 keys[OB_KEY_DOWN] =
203 XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
204
205 prop_startup(); /* get atoms values for the display */
206 extensions_query_all(); /* find which extensions are present */
207
208 /* save stuff that we can use to restore state */
209 startup_save();
210
211 if (screen_annex()) { /* it will be ours! */
212 /* startup the parsing so everything can register sections of the rc */
213 parse_startup();
214
215 /* anything that is going to read data from the rc file needs to be
216 in this group */
217 timer_startup();
218 event_startup();
219 grab_startup();
220 /* focus_backup is used for stacking, so this needs to come before
221 anything that calls stacking_add */
222 focus_startup();
223 window_startup();
224 plugin_startup();
225 /* load the plugins specified in the pluginrc */
226 plugin_loadall();
227
228 /* set up the kernel config shit */
229 config_startup();
230 menu_startup();
231 /* parse/load user options */
232 if (parse_load_rc(&doc, &node))
233 parse_tree(doc, node->xmlChildrenNode, NULL);
234 /* we're done with parsing now, kill it */
235 parse_shutdown();
236
237 /* load the theme specified in the rc file */
238 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
239 if (ob_rr_theme == NULL)
240 exit_with_error("Unable to load a theme.");
241
242 moveresize_startup();
243 screen_startup();
244 group_startup();
245 client_startup();
246 dock_startup();
247
248 /* call startup for all the plugins */
249 plugin_startall();
250
251 /* get all the existing windows */
252 client_manage_all();
253
254 state = OB_STATE_RUNNING;
255 while (!shutdown)
256 event_loop();
257 state = OB_STATE_EXITING;
258
259 dock_remove_all();
260 client_unmanage_all();
261
262 plugin_shutdown(); /* calls all the plugins' shutdown functions */
263 dock_shutdown();
264 client_shutdown();
265 group_shutdown();
266 screen_shutdown();
267 focus_shutdown();
268 moveresize_shutdown();
269 menu_shutdown();
270 window_shutdown();
271 grab_shutdown();
272 event_shutdown();
273 timer_shutdown();
274 config_shutdown();
275 }
276
277 dispatch_shutdown();
278
279 RrThemeFree(ob_rr_theme);
280 RrInstanceFree(ob_rr_inst);
281
282 session_shutdown();
283 g_free(ob_sm_id);
284
285 XCloseDisplay(ob_display);
286
287 if (restart) {
288 if (restart_path != NULL) {
289 int argcp;
290 char **argvp;
291 GError *err = NULL;
292
293 /* run other shit */
294 if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
295 execvp(argvp[0], argvp);
296 g_strfreev(argvp);
297 } else {
298 g_warning("failed to execute '%s': %s", restart_path,
299 err->message);
300 }
301 }
302
303 /* re-run me */
304 execvp(argv[0], argv); /* try how we were run */
305 }
306
307 return 0;
308 }
309
310 static void signal_handler(const ObEvent *e, void *data)
311 {
312 int s;
313
314 s = e->data.s.signal;
315 switch (s) {
316 case SIGUSR1:
317 fprintf(stderr, "Caught SIGUSR1 signal. Restarting.");
318 ob_restart();
319 break;
320
321 case SIGHUP:
322 case SIGINT:
323 case SIGTERM:
324 case SIGPIPE:
325 fprintf(stderr, "Caught signal %d. Exiting.", s);
326 ob_exit();
327 break;
328
329 case SIGFPE:
330 case SIGSEGV:
331 fprintf(stderr, "Caught signal %d. Aborting and dumping core.", s);
332 abort();
333 }
334 }
335
336 static void print_version()
337 {
338 g_print("Openbox %s\n\n", PACKAGE_VERSION);
339 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
340 g_print("This is free software, and you are welcome to redistribute it\n");
341 g_print("under certain conditions. See the file COPYING for details.\n\n");
342 }
343
344 static void print_help()
345 {
346 print_version();
347 g_print("Syntax: openbox [options]\n\n");
348 g_print("Options:\n\n");
349 #ifdef USE_SM
350 g_print(" --sm-disable Disable connection to session manager\n");
351 g_print(" --sm-client-id ID Specify session management ID\n");
352 g_print(" --sm-save-file FILE Specify file to load a saved session\n"
353 " from\n");
354 #endif
355 g_print(" --replace Replace the currently running window "
356 "manager\n");
357 g_print(" --help Display this help and exit\n");
358 g_print(" --version Display the version and exit\n");
359 g_print(" --sync Run in synchronous mode (this is slow and\n"
360 " meant for debugging X routines)\n");
361 g_print(" --debug Display debugging output\n");
362 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
363 }
364
365 static void parse_args(int argc, char **argv)
366 {
367 int i;
368
369 for (i = 1; i < argc; ++i) {
370 if (!strcmp(argv[i], "--version")) {
371 print_version();
372 exit(0);
373 } else if (!strcmp(argv[i], "--help")) {
374 print_help();
375 exit(0);
376 } else if (!strcmp(argv[i], "--g-fatal-warnings")) {
377 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
378 } else if (!strcmp(argv[i], "--replace")) {
379 ob_replace_wm = TRUE;
380 } else if (!strcmp(argv[i], "--sync")) {
381 xsync = TRUE;
382 } else if (!strcmp(argv[i], "--debug")) {
383 ob_debug_show_output(TRUE);
384 #ifdef USE_SM
385 } else if (!strcmp(argv[i], "--sm-client-id")) {
386 if (i == argc - 1) /* no args left */
387 g_printerr(_("--sm-client-id requires an argument\n"));
388 else
389 ob_sm_id = g_strdup(argv[++i]);
390 } else if (!strcmp(argv[i], "--sm-save-file")) {
391 if (i == argc - 1) /* no args left */
392 g_printerr(_("--sm-save-file requires an argument\n"));
393 else
394 sm_save_file = argv[++i];
395 } else if (!strcmp(argv[i], "--sm-disable")) {
396 ob_sm_use = FALSE;
397 #endif
398 } else {
399 g_printerr("Invalid option: '%s'\n\n", argv[i]);
400 print_help();
401 exit(1);
402 }
403 }
404 }
405
406 static void exit_with_error(gchar *msg)
407 {
408 g_critical(msg);
409 session_shutdown();
410 exit(EXIT_FAILURE);
411 }
412
413 void ob_restart_other(const gchar *path)
414 {
415 restart_path = g_strdup(path);
416 ob_restart();
417 }
418
419 void ob_restart()
420 {
421 restart = TRUE;
422 ob_exit();
423 }
424
425 void ob_exit()
426 {
427 shutdown = TRUE;
428 }
429
430 Cursor ob_cursor(ObCursor cursor)
431 {
432 g_assert(cursor < OB_NUM_CURSORS);
433 return cursors[cursor];
434 }
435
436 KeyCode ob_keycode(ObKey key)
437 {
438 g_assert(key < OB_NUM_KEYS);
439 return keys[key];
440 }
441
442 ObState ob_state()
443 {
444 return state;
445 }
This page took 0.05213 seconds and 4 git commands to generate.