]> Dogcows Code - chaz/openbox/blob - openbox/openbox.c
add libSM *client* support, so we should work with gnome-session now!
[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 RrInstance *ob_rr_inst = NULL;
52 RrTheme *ob_rr_theme = NULL;
53 Display *ob_display = NULL;
54 int ob_screen;
55 Window ob_root;
56 State ob_state;
57 gboolean ob_shutdown = FALSE;
58 gboolean ob_restart = FALSE;
59 char *ob_restart_path = NULL;
60 gboolean ob_remote = TRUE;
61 gboolean ob_sync = FALSE;
62 Cursors ob_cursors;
63 char *ob_rc_path = NULL;
64
65 static void signal_handler(const ObEvent *e, void *data);
66 static void parse_args(int argc, char **argv);
67 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
68 Bool shutdown, int interact_style, Bool fast);
69 static void sm_die(SmcConn conn, SmPointer data);
70 static void sm_save_complete(SmcConn conn, SmPointer data);
71 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
72 static void exit_with_error(gchar *msg);
73
74 int main(int argc, char **argv)
75 {
76 struct sigaction action;
77 sigset_t sigset;
78 char *path;
79 xmlDocPtr doc;
80 xmlNodePtr node;
81 SmcCallbacks cb;
82 char sm_err[SM_ERR_LEN];
83 char *sm_id;
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, NULL, &sm_id, SM_ERR_LEN, sm_err);
149 if (ob_sm_conn == NULL)
150 g_warning("Failed to connect to session manager: %s", sm_err);
151 else
152 g_message("Connected to session manager with id %s", sm_id);
153 g_free (sm_id);
154
155 #ifdef USE_LIBSN
156 ob_sn_display = sn_display_new(ob_display, NULL, NULL);
157 #endif
158
159 ob_screen = DefaultScreen(ob_display);
160 ob_root = RootWindow(ob_display, ob_screen);
161
162 ob_rr_inst = RrInstanceNew(ob_display, ob_screen);
163 if (ob_rr_inst == NULL)
164 exit_with_error("Failed to initialize the render library.");
165
166 /* XXX fork self onto other screens */
167
168 XSynchronize(ob_display, ob_sync);
169
170 /* check for locale support */
171 if (!XSupportsLocale())
172 g_warning("X server does not support locale.");
173 if (!XSetLocaleModifiers(""))
174 g_warning("Cannot set locale modifiers for the X server.");
175
176 /* set our error handler */
177 XSetErrorHandler(xerror_handler);
178
179 /* set the DISPLAY environment variable for any lauched children, to the
180 display we're using, so they open in the right place. */
181 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display)));
182
183 ob_cursors.ptr = XCreateFontCursor(ob_display, XC_left_ptr);
184 ob_cursors.busy = XCreateFontCursor(ob_display, XC_watch);
185 ob_cursors.move = XCreateFontCursor(ob_display, XC_fleur);
186 ob_cursors.tl = XCreateFontCursor(ob_display, XC_top_left_corner);
187 ob_cursors.tr = XCreateFontCursor(ob_display, XC_top_right_corner);
188 ob_cursors.bl = XCreateFontCursor(ob_display, XC_bottom_left_corner);
189 ob_cursors.br = XCreateFontCursor(ob_display, XC_bottom_right_corner);
190 ob_cursors.t = XCreateFontCursor(ob_display, XC_top_side);
191 ob_cursors.r = XCreateFontCursor(ob_display, XC_right_side);
192 ob_cursors.b = XCreateFontCursor(ob_display, XC_bottom_side);
193 ob_cursors.l = XCreateFontCursor(ob_display, XC_left_side);
194
195 prop_startup(); /* get atoms values for the display */
196 extensions_query_all(); /* find which extensions are present */
197
198 /* save stuff that we can use to restore state */
199 startup_save();
200
201 if (screen_annex()) { /* it will be ours! */
202 /* startup the parsing so everything can register sections of the rc */
203 parse_startup();
204
205 /* anything that is going to read data from the rc file needs to be
206 in this group */
207 timer_startup();
208 event_startup();
209 grab_startup();
210 plugin_startup();
211 /* load the plugins specified in the pluginrc */
212 plugin_loadall();
213
214 /* set up the kernel config shit */
215 config_startup();
216 /* parse/load user options */
217 if (parse_load_rc(&doc, &node))
218 parse_tree(doc, node->xmlChildrenNode, NULL);
219 /* we're done with parsing now, kill it */
220 parse_shutdown();
221
222 /* load the theme specified in the rc file */
223 ob_rr_theme = RrThemeNew(ob_rr_inst, config_theme);
224 if (ob_rr_theme == NULL)
225 exit_with_error("Unable to load a theme.");
226
227 window_startup();
228 menu_startup();
229 frame_startup();
230 moveresize_startup();
231 focus_startup();
232 screen_startup();
233 group_startup();
234 client_startup();
235 dock_startup();
236
237 /* call startup for all the plugins */
238 plugin_startall();
239
240 /* get all the existing windows */
241 client_manage_all();
242
243 ob_state = State_Running;
244 while (!ob_shutdown)
245 event_loop();
246 ob_state = State_Exiting;
247
248 dock_remove_all();
249 client_unmanage_all();
250
251 plugin_shutdown(); /* calls all the plugins' shutdown functions */
252 dock_shutdown();
253 client_shutdown();
254 group_shutdown();
255 screen_shutdown();
256 focus_shutdown();
257 moveresize_shutdown();
258 frame_shutdown();
259 menu_shutdown();
260 window_shutdown();
261 grab_shutdown();
262 event_shutdown();
263 timer_shutdown();
264 config_shutdown();
265 }
266
267 dispatch_shutdown();
268
269 RrThemeFree(ob_rr_theme);
270 RrInstanceFree(ob_rr_inst);
271
272 if (ob_sm_conn)
273 SmcCloseConnection(ob_sm_conn, 0, NULL);
274 XCloseDisplay(ob_display);
275
276 if (ob_restart) {
277 if (ob_restart_path != NULL) {
278 int argcp;
279 char **argvp;
280 GError *err = NULL;
281
282 /* run other shit */
283 if (g_shell_parse_argv(ob_restart_path, &argcp, &argvp, &err)) {
284 execvp(argvp[0], argvp);
285 g_strfreev(argvp);
286 } else {
287 g_warning("failed to execute '%s': %s", ob_restart_path,
288 err->message);
289 }
290 }
291
292 /* re-run me */
293 execvp(argv[0], argv); /* try how we were run */
294 execlp(BINARY, BINARY, NULL); /* try this as a last resort */
295 }
296
297 return 0;
298 }
299
300 static void signal_handler(const ObEvent *e, void *data)
301 {
302 int s;
303
304 s = e->data.s.signal;
305 switch (s) {
306 case SIGUSR1:
307 g_message("Caught SIGUSR1 signal. Restarting.");
308 ob_shutdown = ob_restart = TRUE;
309 break;
310
311 case SIGHUP:
312 case SIGINT:
313 case SIGTERM:
314 case SIGPIPE:
315 g_message("Caught signal %d. Exiting.", s);
316 ob_shutdown = TRUE;
317 break;
318
319 case SIGFPE:
320 case SIGSEGV:
321 g_message("Caught signal %d. Aborting and dumping core.", s);
322 abort();
323 }
324 }
325
326 static void print_version()
327 {
328 g_print("Openbox %s\n\n", PACKAGE_VERSION);
329 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
330 g_print("This is free software, and you are welcome to redistribute it\n");
331 g_print("under certain conditions. See the file COPYING for details.\n\n");
332 }
333
334 static void print_help()
335 {
336 print_version();
337 g_print("Syntax: %s [options]\n\n", BINARY);
338 g_print("Options:\n\n");
339 g_print(" -rc PATH Specify the path to the rc file to use\n");
340 g_print(" -help Display this help and exit\n");
341 g_print(" -version Display the version and exit\n");
342 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
343 " for debugging X routines)\n");
344 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT);
345 }
346
347 static void parse_args(int argc, char **argv)
348 {
349 int i;
350
351 for (i = 1; i < argc; ++i) {
352 if (!strcmp(argv[i], "-version")) {
353 print_version();
354 exit(0);
355 } else if (!strcmp(argv[i], "-help")) {
356 print_help();
357 exit(0);
358 } else if (!strcmp(argv[i], "-sync")) {
359 ob_sync = TRUE;
360 } else if (!strcmp(argv[i], "-rc")) {
361 if (i == argc - 1) /* no args left */
362 g_printerr("-rc requires an argument\n");
363 else
364 ob_rc_path = argv[++i];
365 } else {
366 g_printerr("Invalid option: '%s'\n\n", argv[i]);
367 print_help();
368 exit(1);
369 }
370 }
371 }
372
373 gboolean ob_pointer_pos(int *x, int *y)
374 {
375 Window w;
376 int i;
377 guint u;
378
379 return !!XQueryPointer(ob_display, ob_root, &w, &w, x, y, &i, &i, &u);
380 }
381
382 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
383 Bool shutdown, int interact_style, Bool fast)
384 {
385 g_message("got SAVE YOURSELF from session manager");
386 SmcSaveYourselfDone(conn, TRUE);
387 }
388
389 static void sm_die(SmcConn conn, SmPointer data)
390 {
391 ob_shutdown = TRUE;
392 g_message("got DIE from session manager");
393 }
394
395 static void sm_save_complete(SmcConn conn, SmPointer data)
396 {
397 g_message("got SAVE COMPLETE from session manager");
398 }
399
400 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
401 {
402 g_message("got SHUTDOWN CANCELLED from session manager");
403 }
404
405 static void exit_with_error(gchar *msg)
406 {
407 g_critical(msg);
408 if (ob_sm_conn)
409 SmcCloseConnection(ob_sm_conn, 1, &msg);
410 exit(EXIT_FAILURE);
411 }
This page took 0.052049 seconds and 5 git commands to generate.