]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
35a530b76cde50f3cc8a78507eda115133efbe9b
[chaz/openbox] / openbox / openbox.c
1 #include "openbox.h"
2 #include "dock.h"
3 #include "event.h"
4 #include "menu.h"
5 #include "client.h"
6 #include "dispatch.h"
7 #include "xerror.h"
8 #include "prop.h"
9 #include "startup.h"
10 #include "screen.h"
11 #include "focus.h"
12 #include "moveresize.h"
13 #include "frame.h"
14 #include "extensions.h"
15 #include "grab.h"
16 #include "plugin.h"
17 #include "timer.h"
18 #include "group.h"
19 #include "config.h"
20 #include "gettext.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
24
25 #ifdef HAVE_FCNTL_H
26 # include <fcntl.h>
27 #endif
28 #ifdef HAVE_SIGNAL_H
29 # include <signal.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #ifdef HAVE_LOCALE_H
35 # include <locale.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
43 #endif
44
45 #include <X11/SM/SMlib.h>
46 #include <X11/cursorfont.h>
47
48 #define SM_ERR_LEN 1024
49
50 SmcConn ob_sm_conn;
51 gchar *ob_sm_id = NULL;
52 RrInstance *ob_rr_inst = NULL;
53 RrTheme *ob_rr_theme = NULL;
54 Display *ob_display = NULL;
55 int ob_screen;
56 Window ob_root;
57 State ob_state;
58 gboolean ob_shutdown = FALSE;
59 gboolean ob_restart = FALSE;
60 char *ob_restart_path = NULL;
61 gboolean ob_remote = TRUE;
62 gboolean ob_sync = FALSE;
63 Cursors ob_cursors;
64 char *ob_rc_path = NULL;
65
66 static void signal_handler(const ObEvent *e, void *data);
67 static void parse_args(int argc, char **argv);
68 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
69 Bool shutdown, int interact_style, Bool fast);
70 static void sm_die(SmcConn conn, SmPointer data);
71 static void sm_save_complete(SmcConn conn, SmPointer data);
72 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
73 static void exit_with_error(gchar *msg);
74
75 int main(int argc, char **argv)
76 {
77 struct sigaction action;
78 sigset_t sigset;
79 char *path;
80 xmlDocPtr doc;
81 xmlNodePtr node;
82 SmcCallbacks cb;
83 char sm_err[SM_ERR_LEN];
84
85 ob_state = State_Starting;
86
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);
93
94 /* start our event dispatcher and register for signals */
95 dispatch_startup();
96 dispatch_register(Event_Signal, signal_handler, NULL);
97
98 /* set up signal handler */
99 sigemptyset(&sigset);
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);
110
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));
115 g_free(path);
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));
120 g_free(path);
121
122 /* parse out command line args */
123 parse_args(argc, argv);
124
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.");
130
131 cb.save_yourself.callback = sm_save_yourself;
132 cb.save_yourself.client_data = NULL;
133
134 cb.die.callback = sm_die;
135 cb.die.client_data = NULL;
136
137 cb.save_complete.callback = sm_save_complete;
138 cb.save_complete.client_data = NULL;
139
140 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
141 cb.shutdown_cancelled.client_data = NULL;
142
143 ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
144 SmcSaveYourselfProcMask |
145 SmcDieProcMask |
146 SmcSaveCompleteProcMask |
147 SmcShutdownCancelledProcMask,
148 &cb, ob_sm_id, &ob_sm_id,
149 SM_ERR_LEN, sm_err);
150 if (ob_sm_conn == NULL)
151 g_warning("Failed to connect to session manager: %s", sm_err);
152 else {
153 SmPropValue val_cmd;
154 SmPropValue val_res;
155 SmPropValue val_prog;
156 SmPropValue val_uid;
157 SmProp prop_cmd = { SmCloneCommand, "SmLISTofARRAY8", 1, };
158 SmProp prop_res = { SmRestartCommand, "SmLISTofARRAY8", 1, };
159 SmProp prop_prog = { SmProgram, "SmARRAY8", 1, };
160 SmProp prop_uid = { SmUserID, "SmARRAY8", 1, };
161 SmProp *props[4];
162 gchar *user;
163
164 val_cmd.value = argv[0];
165 val_cmd.length = strlen(argv[0]);
166 val_res.value = argv[0];
167 val_res.length = strlen(argv[0]); /* XXX -id foo */
168 val_prog.value = argv[0];
169 val_prog.length = strlen(argv[0]);
170
171 user = g_strdup_printf("%ld", (long)getuid());
172 val_uid.value = user;
173 val_uid.length = strlen(user);
174
175 prop_cmd.vals = &val_cmd;
176 prop_res.vals = &val_res;
177 prop_prog.vals = &val_prog;
178 prop_uid.vals = &val_uid;
179
180 props[0] = &prop_cmd;
181 props[1] = &prop_res;
182 props[2] = &prop_prog;
183 props[3] = &prop_uid;
184
185 SmcSetProperties(ob_sm_conn, 3, props);
186
187 g_free(user);
188
189 g_message("Connected to session manager with id %s", ob_sm_id);
190 }
191 g_free (ob_sm_id);
192
193 #ifdef USE_LIBSN
194 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
195 #endif
196
197 ob_screen = DefaultScreen(ob_display);
198 ob_root = RootWindow(ob_display, ob_screen);
199
200 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
201 if (ob_rr_inst == NULL)
202 exit_with_error("Failed to initialize the render library.");
203
204 /* XXX fork self onto other screens */
205
206 XSynchronize(ob_display, ob_sync);
207
208 /* check for locale support */
209 if (!XSupportsLocale())
210 g_warning("X server does not support locale.");
211 if (!XSetLocaleModifiers(""))
212 g_warning("Cannot set locale modifiers for the X server.");
213
214 /* set our error handler */
215 XSetErrorHandler(xerror_handler);
216
217 /* set the DISPLAY environment variable for any lauched children, to the
218 display we're using, so they open in the right place. */
219 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
220
221 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
222 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
223 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
224 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
225 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
226 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
227 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
228 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
229 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
230 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
231 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
232
233 prop_startup(); /* get atoms values for the display */
234 extensions_query_all(); /* find which extensions are present */
235
236 /* save stuff that we can use to restore state */
237 startup_save();
238
239 if (screen_annex()) { /* it will be ours! */
240 /* startup the parsing so everything can register sections of the rc */
241 parse_startup();
242
243 /* anything that is going to read data from the rc file needs to be
244 in this group */
245 timer_startup();
246 event_startup();
247 grab_startup();
248 plugin_startup();
249 /* load the plugins specified in the pluginrc */
250 plugin_loadall();
251
252 /* set up the kernel config shit */
253 config_startup();
254 /* parse/load user options */
255 if (parse_load_rc(&doc, &node))
256 parse_tree(doc, node->xmlChildrenNode, NULL);
257 /* we're done with parsing now, kill it */
258 parse_shutdown();
259
260 /* load the theme specified in the rc file */
261 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
262 if (ob_rr_theme == NULL)
263 exit_with_error("Unable to load a theme.");
264
265 window_startup();
266 menu_startup();
267 frame_startup();
268 moveresize_startup();
269 focus_startup();
270 screen_startup();
271 group_startup();
272 client_startup();
273 dock_startup();
274
275 /* call startup for all the plugins */
276 plugin_startall();
277
278 /* get all the existing windows */
279 client_manage_all();
280
281 ob_state = State_Running;
282 while (!ob_shutdown)
283 event_loop();
284 ob_state = State_Exiting;
285
286 dock_remove_all();
287 client_unmanage_all();
288
289 plugin_shutdown(); /* calls all the plugins' shutdown functions */
290 dock_shutdown();
291 client_shutdown();
292 group_shutdown();
293 screen_shutdown();
294 focus_shutdown();
295 moveresize_shutdown();
296 frame_shutdown();
297 menu_shutdown();
298 window_shutdown();
299 grab_shutdown();
300 event_shutdown();
301 timer_shutdown();
302 config_shutdown();
303 }
304
305 dispatch_shutdown();
306
307 RrThemeFree(ob_rr_theme);
308 RrInstanceFree(ob_rr_inst);
309
310 if (ob_sm_conn)
311 SmcCloseConnection(ob_sm_conn, 0, NULL);
312 XCloseDisplay(ob_display);
313
314 if (ob_restart) {
315 if (ob_restart_path != NULL) {
316 int argcp;
317 char **argvp;
318 GError *err = NULL;
319
320 /* run other shit */
321 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
322 execvp(argvp[0], argvp);
323 g_strfreev(argvp);
324 } else {
325 g_warning("failed to execute '%s': %s", ob_restart_path,
326 err->message);
327 }
328 }
329
330 /* re-run me */
331 execvp(argv[0], argv); /* try how we were run */
332 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
333 }
334
335 return 0;
336 }
337
338 static void signal_handler(const ObEvent *e, void *data)
339 {
340 int s;
341
342 s = e->data.s.signal;
343 switch (s) {
344 case SIGUSR1:
345 g_message("Caught SIGUSR1 signal. Restarting.");
346 ob_shutdown = ob_restart = TRUE;
347 break;
348
349 case SIGHUP:
350 case SIGINT:
351 case SIGTERM:
352 case SIGPIPE:
353 g_message("Caught signal %d. Exiting.", s);
354 ob_shutdown = TRUE;
355 break;
356
357 case SIGFPE:
358 case SIGSEGV:
359 g_message("Caught signal %d. Aborting and dumping core.", s);
360 abort();
361 }
362 }
363
364 static void print_version()
365 {
366 g_print("Openbox %s\n\n", PACKAGE_VERSION);
367 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
368 g_print("This is free software, and you are welcome to redistribute it\n");
369 g_print("under certain conditions. See the file COPYING for details.\n\n");
370 }
371
372 static void print_help()
373 {
374 print_version();
375 g_print("Syntax: %s [options]\n\n", BINARY);
376 g_print("Options:\n\n");
377 g_print(" -rc PATH Specify the path to the rc file to use\n");
378 g_print(" -help Display this help and exit\n");
379 g_print(" -version Display the version and exit\n");
380 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
381 " for debugging X routines)\n");
382 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
383 }
384
385 static void parse_args(int argc, char **argv)
386 {
387 int i;
388
389 for (i = 1; i < argc; ++i) {
390 if (!strcmp(argv[i], "-version")) {
391 print_version();
392 exit(0);
393 } else if (!strcmp(argv[i], "-help")) {
394 print_help();
395 exit(0);
396 } else if (!strcmp(argv[i], "-sync")) {
397 ob_sync = TRUE;
398 } else if (!strcmp(argv[i], "-rc")) {
399 if (i == argc - 1) /* no args left */
400 g_printerr("-rc requires an argument\n");
401 else
402 ob_rc_path = argv[++i];
403 } else {
404 g_printerr("Invalid option: '%s'\n\n", argv[i]);
405 print_help();
406 exit(1);
407 }
408 }
409 }
410
411 gboolean ob_pointer_pos(int *x, int *y)
412 {
413 Window w;
414 int i;
415 guint u;
416
417 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
418 }
419
420 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
421 Bool shutdown, int interact_style, Bool fast)
422 {
423 g_message("got SAVE YOURSELF from session manager");
424 SmcSaveYourselfDone(conn, TRUE);
425 }
426
427 static void sm_die(SmcConn conn, SmPointer data)
428 {
429 ob_shutdown = TRUE;
430 g_message("got DIE from session manager");
431 }
432
433 static void sm_save_complete(SmcConn conn, SmPointer data)
434 {
435 g_message("got SAVE COMPLETE from session manager");
436 }
437
438 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
439 {
440 g_message("got SHUTDOWN CANCELLED from session manager");
441 }
442
443 static void exit_with_error(gchar *msg)
444 {
445 g_critical(msg);
446 if (ob_sm_conn)
447 SmcCloseConnection(ob_sm_conn, 1, &msg);
448 exit(EXIT_FAILURE);
449 }
This page took 0.052906 seconds and 3 git commands to generate.