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