]> Dogcows Code - chaz/openbox/blob - openbox/session.c
make ob_exit() take an error code. don't exit with 0 for signals other than TERM...
[chaz/openbox] / openbox / session.c
1 /* This session code is largely inspired by metacity code. */
2
3 #ifndef USE_SM
4
5 #include "session.h"
6 #include "client.h"
7
8 GList *session_saved_state;
9
10 void session_startup(int *argc, char ***argv) {}
11 void session_shutdown() {}
12 GList* session_state_find(ObClient *c) { return NULL; }
13 gboolean session_state_cmp(ObSessionState *s, ObClient *c) { return FALSE; }
14 void session_state_free(ObSessionState *state) {}
15
16 #else
17
18 #include "debug.h"
19 #include "openbox.h"
20 #include "session.h"
21 #include "client.h"
22 #include "prop.h"
23 #include "gettext.h"
24 #include "parser/parse.h"
25
26 #include <time.h>
27 #include <errno.h>
28 #include <stdio.h>
29
30 #ifdef HAVE_UNISTD_H
31 # include <sys/types.h>
32 # include <unistd.h>
33 #endif
34
35 #include <X11/SM/SMlib.h>
36
37 GList *session_saved_state;
38
39 static gboolean sm_disable;
40 static SmcConn sm_conn;
41 static gchar *save_file;
42 static gchar *sm_id;
43 static gint sm_argc;
44 static gchar **sm_argv;
45 static gchar *sm_sessions_path;
46
47 static void session_load(char *path);
48 static gboolean session_save();
49
50 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
51 Bool shutdown, int interact_style, Bool fast);
52 static void sm_die(SmcConn conn, SmPointer data);
53 static void sm_save_complete(SmcConn conn, SmPointer data);
54 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
55
56 static void save_commands()
57 {
58 SmProp *props[2];
59 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
60 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
61 gint i;
62
63 prop_cmd.vals = g_new(SmPropValue, sm_argc);
64 prop_cmd.num_vals = sm_argc;
65 for (i = 0; i < sm_argc; ++i) {
66 prop_cmd.vals[i].value = sm_argv[i];
67 prop_cmd.vals[i].length = strlen(sm_argv[i]);
68 }
69
70 prop_res.vals = g_new(SmPropValue, sm_argc + 2);
71 prop_res.num_vals = sm_argc + 2;
72 for (i = 0; i < sm_argc; ++i) {
73 prop_res.vals[i].value = sm_argv[i];
74 prop_res.vals[i].length = strlen(sm_argv[i]);
75 }
76
77 if (save_file) {
78 prop_res.vals[i].value = "--sm-save-file";
79 prop_res.vals[i++].length = strlen("--sm-save-file");
80 prop_res.vals[i].value = save_file;
81 prop_res.vals[i++].length = strlen(save_file);
82 } else {
83 prop_res.vals[i].value = "--sm-client-id";
84 prop_res.vals[i++].length = strlen("--sm-client-id");
85 prop_res.vals[i].value = sm_id;
86 prop_res.vals[i++].length = strlen(sm_id);
87 }
88
89 props[0] = &prop_res;
90 props[1] = &prop_cmd;
91 SmcSetProperties(sm_conn, 2, props);
92
93 g_free(prop_res.vals);
94 g_free(prop_cmd.vals);
95 }
96
97 static void remove_two_args(int *argc, char ***argv, int index)
98 {
99 int i;
100
101 for (i = index; i < index + 2; ++i)
102 (*argv)[i] = (*argv)[i+2];
103 *argc -= 2;
104 }
105
106 static void parse_args(int *argc, char ***argv)
107 {
108 int i;
109
110 for (i = 1; i < *argc; ++i) {
111 if (!strcmp((*argv)[i], "--sm-client-id")) {
112 if (i == *argc - 1) /* no args left */
113 g_printerr(_("--sm-client-id requires an argument\n"));
114 else {
115 sm_id = g_strdup((*argv)[i+1]);
116 remove_two_args(argc, argv, i);
117 ++i;
118 }
119 } else if (!strcmp((*argv)[i], "--sm-save-file")) {
120 if (i == *argc - 1) /* no args left */
121 g_printerr(_("--sm-save-file requires an argument\n"));
122 else {
123 save_file = g_strdup((*argv)[i+1]);
124 remove_two_args(argc, argv, i);
125 ++i;
126 }
127 } else if (!strcmp((*argv)[i], "--sm-disable")) {
128 sm_disable = TRUE;
129 }
130 }
131 }
132
133 void session_startup(int *argc, char ***argv)
134 {
135 #define SM_ERR_LEN 1024
136
137 SmcCallbacks cb;
138 char sm_err[SM_ERR_LEN];
139
140 parse_args(argc, argv);
141
142 if (sm_disable)
143 return;
144
145 sm_sessions_path = g_build_filename(parse_xdg_data_home_path(),
146 "openbox", "sessions", NULL);
147 parse_mkdir_path(sm_sessions_path, 0700);
148
149 if (save_file)
150 session_load(save_file);
151
152 sm_argc = *argc;
153 sm_argv = *argv;
154
155 cb.save_yourself.callback = sm_save_yourself;
156 cb.save_yourself.client_data = NULL;
157
158 cb.die.callback = sm_die;
159 cb.die.client_data = NULL;
160
161 cb.save_complete.callback = sm_save_complete;
162 cb.save_complete.client_data = NULL;
163
164 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
165 cb.shutdown_cancelled.client_data = NULL;
166
167 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
168 SmcSaveYourselfProcMask |
169 SmcDieProcMask |
170 SmcSaveCompleteProcMask |
171 SmcShutdownCancelledProcMask,
172 &cb, sm_id, &sm_id,
173 SM_ERR_LEN, sm_err);
174 if (sm_conn == NULL)
175 g_warning("Failed to connect to session manager: %s", sm_err);
176 else {
177 SmPropValue val_prog;
178 SmPropValue val_uid;
179 SmPropValue val_hint;
180 SmPropValue val_pri;
181 SmPropValue val_pid;
182 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
183 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
184 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
185 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
186 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
187 SmProp *props[6];
188 gchar hint, pri;
189 gchar pid[32];
190
191 val_prog.value = sm_argv[0];
192 val_prog.length = strlen(sm_argv[0]);
193
194 val_uid.value = g_strdup(g_get_user_name());
195 val_uid.length = strlen(val_uid.value);
196
197 hint = SmRestartImmediately;
198 val_hint.value = &hint;
199 val_hint.length = 1;
200
201 sprintf(pid, "%ld", (long)getpid());
202 val_pid.value = pid;
203 val_pid.length = strlen(pid);
204
205 /* priority with gnome-session-manager, low to run before other apps */
206 pri = 20;
207 val_pri.value = &pri;
208 val_pri.length = 1;
209
210 prop_prog.vals = &val_prog;
211 prop_uid.vals = &val_uid;
212 prop_hint.vals = &val_hint;
213 prop_pid.vals = &val_pid;
214 prop_pri.vals = &val_pri;
215
216 props[0] = &prop_prog;
217 props[1] = &prop_uid;
218 props[2] = &prop_hint;
219 props[3] = &prop_pid;
220 props[4] = &prop_pri;
221
222 SmcSetProperties(sm_conn, 5, props);
223
224 g_free(val_uid.value);
225
226 save_commands();
227 }
228 }
229
230 void session_shutdown()
231 {
232 g_free(sm_sessions_path);
233 g_free(save_file);
234 g_free(sm_id);
235
236 if (sm_conn) {
237 SmPropValue val_hint;
238 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
239 SmProp *props[1];
240 gulong hint;
241
242 /* when we exit, we want to reset this to a more friendly state */
243 hint = SmRestartIfRunning;
244 val_hint.value = &hint;
245 val_hint.length = 1;
246
247 prop_hint.vals = &val_hint;
248
249 props[0] = &prop_hint;
250
251 SmcSetProperties(sm_conn, 1, props);
252
253 SmcCloseConnection(sm_conn, 0, NULL);
254
255 while (session_saved_state) {
256 session_state_free(session_saved_state->data);
257 session_saved_state = g_list_delete_link(session_saved_state,
258 session_saved_state);
259 }
260 }
261 }
262
263 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
264 {
265 gboolean success;
266
267 success = session_save();
268 save_commands();
269
270 SmcSaveYourselfDone(conn, success);
271 }
272
273 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
274 Bool shutdown, int interact_style, Bool fast)
275 {
276 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
277 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
278 SmcSaveYourselfDone(conn, FALSE);
279 }
280 }
281
282 static void sm_die(SmcConn conn, SmPointer data)
283 {
284 ob_exit(0);
285 }
286
287 static void sm_save_complete(SmcConn conn, SmPointer data)
288 {
289 }
290
291 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
292 {
293 }
294
295 static gboolean session_save()
296 {
297 gchar *filename;
298 FILE *f;
299 GList *it;
300 gboolean success = TRUE;
301
302 /* this algo is from metacity */
303 filename = g_strdup_printf("%d-%d-%u.obs",
304 (int) time(NULL),
305 (int) getpid(),
306 g_random_int());
307 save_file = g_build_filename(sm_sessions_path, filename, NULL);
308 g_free(filename);
309
310 f = fopen(save_file, "w");
311 if (!f) {
312 success = FALSE;
313 g_warning("unable to save the session to %s: %s",
314 save_file, strerror(errno));
315 } else {
316 guint stack_pos = 0;
317
318 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
319 fprintf(f, "<openbox_session id=\"%s\">\n\n", sm_id);
320
321 for (it = stacking_list; it; it = g_list_next(it)) {
322 guint num;
323 gint32 *dimensions;
324 gint prex, prey, prew, preh;
325 ObClient *c;
326 gchar *t;
327
328 if (WINDOW_IS_CLIENT(it->data))
329 c = WINDOW_AS_CLIENT(it->data);
330 else
331 continue;
332
333 if (!client_normal(c))
334 continue;
335
336 if (!c->sm_client_id)
337 continue;
338
339 prex = c->area.x;
340 prey = c->area.y;
341 prew = c->area.width;
342 preh = c->area.height;
343 if (PROP_GETA32(c->window, openbox_premax, cardinal,
344 (guint32**)&dimensions, &num)) {
345 if (num == 4) {
346 prex = dimensions[0];
347 prey = dimensions[1];
348 prew = dimensions[2];
349 preh = dimensions[3];
350 }
351 g_free(dimensions);
352 }
353
354 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
355
356 t = g_markup_escape_text(c->name, -1);
357 fprintf(f, "\t<name>%s</name>\n", t);
358 g_free(t);
359
360 t = g_markup_escape_text(c->class, -1);
361 fprintf(f, "\t<class>%s</class>\n", t);
362 g_free(t);
363
364 t = g_markup_escape_text(c->role, -1);
365 fprintf(f, "\t<role>%s</role>\n", t);
366 g_free(t);
367
368 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
369 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
370 fprintf(f, "\t<x>%d</x>\n", prex);
371 fprintf(f, "\t<y>%d</y>\n", prey);
372 fprintf(f, "\t<width>%d</width>\n", prew);
373 fprintf(f, "\t<height>%d</height>\n", preh);
374 if (c->shaded)
375 fprintf(f, "\t<shaded />\n");
376 if (c->iconic)
377 fprintf(f, "\t<iconic />\n");
378 if (c->skip_pager)
379 fprintf(f, "\t<skip_pager />\n");
380 if (c->skip_taskbar)
381 fprintf(f, "\t<skip_taskbar />\n");
382 if (c->fullscreen)
383 fprintf(f, "\t<fullscreen />\n");
384 if (c->above)
385 fprintf(f, "\t<above />\n");
386 if (c->below)
387 fprintf(f, "\t<below />\n");
388 if (c->max_horz)
389 fprintf(f, "\t<max_horz />\n");
390 if (c->max_vert)
391 fprintf(f, "\t<max_vert />\n");
392 fprintf(f, "</window>\n\n");
393
394 ++stack_pos;
395 }
396
397 fprintf(f, "</openbox_session>\n");
398
399 if (fflush(f)) {
400 success = FALSE;
401 g_warning("error while saving the session to %s: %s",
402 save_file, strerror(errno));
403 }
404 fclose(f);
405 }
406
407 return success;
408 }
409
410 void session_state_free(ObSessionState *state)
411 {
412 if (state) {
413 g_free(state->id);
414 g_free(state->name);
415 g_free(state->class);
416 g_free(state->role);
417
418 g_free(state);
419 }
420 }
421
422 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
423 {
424 return (c->sm_client_id &&
425 !strcmp(s->id, c->sm_client_id) &&
426 !strcmp(s->name, c->name) &&
427 !strcmp(s->class, c->class) &&
428 !strcmp(s->role, c->role));
429 }
430
431 GList* session_state_find(ObClient *c)
432 {
433 GList *it;
434
435 for (it = session_saved_state; it; it = g_list_next(it)) {
436 ObSessionState *s = it->data;
437 if (!s->matched && session_state_cmp(s, c)) {
438 s->matched = TRUE;
439 break;
440 }
441 }
442 return it;
443 }
444
445 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
446 {
447 return s1->stacking - s2->stacking;
448 }
449
450 static void session_load(char *path)
451 {
452 xmlDocPtr doc;
453 xmlNodePtr node, n;
454 gchar *id;
455
456 if (!parse_load(path, "openbox_session", &doc, &node))
457 return;
458
459 if (!parse_attr_string("id", node, &id))
460 return;
461 g_free(sm_id);
462 sm_id = id;
463
464 node = parse_find_node("window", node->children);
465 while (node) {
466 ObSessionState *state;
467
468 state = g_new0(ObSessionState, 1);
469
470 if (!parse_attr_string("id", node, &state->id))
471 goto session_load_bail;
472 if (!(n = parse_find_node("name", node->children)))
473 goto session_load_bail;
474 state->name = parse_string(doc, n);
475 if (!(n = parse_find_node("class", node->children)))
476 goto session_load_bail;
477 state->class = parse_string(doc, n);
478 if (!(n = parse_find_node("role", node->children)))
479 goto session_load_bail;
480 state->role = parse_string(doc, n);
481 if (!(n = parse_find_node("stacking", node->children)))
482 goto session_load_bail;
483 state->stacking = parse_int(doc, n);
484 if (!(n = parse_find_node("desktop", node->children)))
485 goto session_load_bail;
486 state->desktop = parse_int(doc, n);
487 if (!(n = parse_find_node("x", node->children)))
488 goto session_load_bail;
489 state->x = parse_int(doc, n);
490 if (!(n = parse_find_node("y", node->children)))
491 goto session_load_bail;
492 state->y = parse_int(doc, n);
493 if (!(n = parse_find_node("width", node->children)))
494 goto session_load_bail;
495 state->w = parse_int(doc, n);
496 if (!(n = parse_find_node("height", node->children)))
497 goto session_load_bail;
498 state->h = parse_int(doc, n);
499
500 state->shaded =
501 parse_find_node("shaded", node->children) != NULL;
502 state->iconic =
503 parse_find_node("iconic", node->children) != NULL;
504 state->skip_pager =
505 parse_find_node("skip_pager", node->children) != NULL;
506 state->skip_taskbar =
507 parse_find_node("skip_taskbar", node->children) != NULL;
508 state->fullscreen =
509 parse_find_node("fullscreen", node->children) != NULL;
510 state->above =
511 parse_find_node("above", node->children) != NULL;
512 state->below =
513 parse_find_node("below", node->children) != NULL;
514 state->max_horz =
515 parse_find_node("max_horz", node->children) != NULL;
516 state->max_vert =
517 parse_find_node("max_vert", node->children) != NULL;
518
519 /* save this */
520 session_saved_state = g_list_prepend(session_saved_state, state);
521 goto session_load_ok;
522
523 session_load_bail:
524 session_state_free(state);
525
526 session_load_ok:
527
528 node = parse_find_node("window", node->next);
529 }
530
531 /* sort them by their stacking order */
532 session_saved_state = g_list_sort(session_saved_state,
533 (GCompareFunc)stack_sort);
534
535 xmlFreeDoc(doc);
536 }
537
538 #endif
This page took 0.06608 seconds and 5 git commands to generate.