]> Dogcows Code - chaz/openbox/blob - openbox/session.c
make the client destructor a GDestroyNotify
[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 }
199
200 void session_shutdown()
201 {
202 g_free(save_file);
203
204 if (sm_conn) {
205 SmPropValue val_hint;
206 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
207 SmProp *props[1];
208 gulong hint;
209
210 /* when we exit, we want to reset this to a more friendly state */
211 hint = SmRestartIfRunning;
212 val_hint.value = &hint;
213 val_hint.length = 1;
214
215 prop_hint.vals = &val_hint;
216
217 props[0] = &prop_hint;
218
219 SmcSetProperties(sm_conn, 1, props);
220
221 SmcCloseConnection(sm_conn, 0, NULL);
222
223 while (session_saved_state) {
224 session_state_free(session_saved_state->data);
225 session_saved_state = g_list_delete_link(session_saved_state,
226 session_saved_state);
227 }
228 }
229 }
230
231 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
232 {
233 gboolean success;
234
235 success = session_save();
236 save_commands();
237
238 SmcSaveYourselfDone(conn, success);
239 }
240
241 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
242 Bool shutdown, int interact_style, Bool fast)
243 {
244 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
245 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
246 SmcSaveYourselfDone(conn, FALSE);
247 }
248 }
249
250 static void sm_die(SmcConn conn, SmPointer data)
251 {
252 ob_exit();
253 }
254
255 static void sm_save_complete(SmcConn conn, SmPointer data)
256 {
257 }
258
259 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
260 {
261 }
262
263 static gboolean session_save()
264 {
265 gchar *filename;
266 FILE *f;
267 GList *it;
268 gboolean success = TRUE;
269
270 /* this algo is from metacity */
271 filename = g_strdup_printf("%d-%d-%u.obs",
272 (int) time(NULL),
273 (int) getpid(),
274 g_random_int());
275 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
276 filename, NULL);
277 g_free(filename);
278
279 f = fopen(save_file, "w");
280 if (!f) {
281 success = FALSE;
282 g_warning("unable to save the session to %s: %s",
283 save_file, strerror(errno));
284 } else {
285 guint stack_pos = 0;
286
287 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
288 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
289
290 for (it = stacking_list; it; it = g_list_next(it)) {
291 guint num;
292 gint32 *dimensions;
293 gint prex, prey, prew, preh;
294 ObClient *c;
295 gchar *client_id, *t;
296
297 if (WINDOW_IS_CLIENT(it->data))
298 c = WINDOW_AS_CLIENT(it->data);
299 else
300 continue;
301
302 if (!client_normal(c))
303 continue;
304
305 if (!(client_id = client_get_sm_client_id(c)))
306 continue;
307
308 prex = c->area.x;
309 prey = c->area.y;
310 prew = c->area.width;
311 preh = c->area.height;
312 if (PROP_GETA32(c->window, openbox_premax, cardinal,
313 (guint32**)&dimensions, &num)) {
314 if (num == 4) {
315 prex = dimensions[0];
316 prey = dimensions[1];
317 prew = dimensions[2];
318 preh = dimensions[3];
319 }
320 g_free(dimensions);
321 }
322
323 fprintf(f, "<window id=\"%s\">\n", client_id);
324
325 t = g_markup_escape_text(c->name, -1);
326 fprintf(f, "\t<name>%s</name>\n", t);
327 g_free(t);
328
329 t = g_markup_escape_text(c->class, -1);
330 fprintf(f, "\t<class>%s</class>\n", t);
331 g_free(t);
332
333 t = g_markup_escape_text(c->role, -1);
334 fprintf(f, "\t<role>%s</role>\n", t);
335 g_free(t);
336
337 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
338 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
339 fprintf(f, "\t<x>%d</x>\n", prex);
340 fprintf(f, "\t<y>%d</y>\n", prey);
341 fprintf(f, "\t<width>%d</width>\n", prew);
342 fprintf(f, "\t<height>%d</height>\n", preh);
343 if (c->shaded)
344 fprintf(f, "\t<shaded />\n");
345 if (c->iconic)
346 fprintf(f, "\t<iconic />\n");
347 if (c->skip_pager)
348 fprintf(f, "\t<skip_pager />\n");
349 if (c->skip_taskbar)
350 fprintf(f, "\t<skip_taskbar />\n");
351 if (c->fullscreen)
352 fprintf(f, "\t<fullscreen />\n");
353 if (c->above)
354 fprintf(f, "\t<above />\n");
355 if (c->below)
356 fprintf(f, "\t<below />\n");
357 if (c->max_horz)
358 fprintf(f, "\t<max_horz />\n");
359 if (c->max_vert)
360 fprintf(f, "\t<max_vert />\n");
361 fprintf(f, "</window>\n\n");
362
363 ++stack_pos;
364
365 g_free(client_id);
366 }
367
368 fprintf(f, "</openbox_session>\n");
369
370 if (fflush(f)) {
371 success = FALSE;
372 g_warning("error while saving the session to %s: %s",
373 save_file, strerror(errno));
374 }
375 fclose(f);
376 }
377
378 return success;
379 }
380
381 void session_state_free(ObSessionState *state)
382 {
383 if (state) {
384 g_free(state->id);
385 g_free(state->name);
386 g_free(state->class);
387 g_free(state->role);
388
389 g_free(state);
390 }
391 }
392
393 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
394 {
395 gchar *client_id;
396
397 if (!(client_id = client_get_sm_client_id(c)))
398 return FALSE;
399 if (strcmp(s->id, client_id)) {
400 g_free(client_id);
401 return FALSE;
402 }
403 g_free(client_id);
404 if (strcmp(s->name, c->name))
405 return FALSE;
406 if (strcmp(s->class, c->class))
407 return FALSE;
408 if (strcmp(s->role, c->role))
409 return FALSE;
410 return TRUE;
411 }
412
413 GList* session_state_find(ObClient *c)
414 {
415 GList *it;
416
417 for (it = session_saved_state; it; it = g_list_next(it)) {
418 ObSessionState *s = it->data;
419 if (!s->matched && session_state_cmp(s, c)) {
420 s->matched = TRUE;
421 break;
422 }
423 }
424 return it;
425 }
426
427 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
428 {
429 return s1->stacking - s2->stacking;
430 }
431
432 void session_load(char *path)
433 {
434 xmlDocPtr doc;
435 xmlNodePtr node, n;
436 gchar *sm_id;
437
438 if (!parse_load(path, "openbox_session", &doc, &node))
439 return;
440
441 if (!parse_attr_string("id", node, &sm_id))
442 return;
443 ob_sm_id = g_strdup(sm_id);
444
445 node = parse_find_node("window", node->xmlChildrenNode);
446 while (node) {
447 ObSessionState *state;
448
449 state = g_new0(ObSessionState, 1);
450
451 if (!parse_attr_string("id", node, &state->id))
452 goto session_load_bail;
453 if (!(n = parse_find_node("name", node->xmlChildrenNode)))
454 goto session_load_bail;
455 state->name = parse_string(doc, n);
456 if (!(n = parse_find_node("class", node->xmlChildrenNode)))
457 goto session_load_bail;
458 state->class = parse_string(doc, n);
459 if (!(n = parse_find_node("role", node->xmlChildrenNode)))
460 goto session_load_bail;
461 state->role = parse_string(doc, n);
462 if (!(n = parse_find_node("stacking", node->xmlChildrenNode)))
463 goto session_load_bail;
464 state->stacking = parse_int(doc, n);
465 if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
466 goto session_load_bail;
467 state->desktop = parse_int(doc, n);
468 if (!(n = parse_find_node("x", node->xmlChildrenNode)))
469 goto session_load_bail;
470 state->x = parse_int(doc, n);
471 if (!(n = parse_find_node("y", node->xmlChildrenNode)))
472 goto session_load_bail;
473 state->y = parse_int(doc, n);
474 if (!(n = parse_find_node("width", node->xmlChildrenNode)))
475 goto session_load_bail;
476 state->w = parse_int(doc, n);
477 if (!(n = parse_find_node("height", node->xmlChildrenNode)))
478 goto session_load_bail;
479 state->h = parse_int(doc, n);
480
481 state->shaded =
482 parse_find_node("shaded", node->xmlChildrenNode) != NULL;
483 state->iconic =
484 parse_find_node("iconic", node->xmlChildrenNode) != NULL;
485 state->skip_pager =
486 parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
487 state->skip_taskbar =
488 parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
489 state->fullscreen =
490 parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
491 state->above =
492 parse_find_node("above", node->xmlChildrenNode) != NULL;
493 state->below =
494 parse_find_node("below", node->xmlChildrenNode) != NULL;
495 state->max_horz =
496 parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
497 state->max_vert =
498 parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
499
500 /* save this */
501 session_saved_state = g_list_prepend(session_saved_state, state);
502 goto session_load_ok;
503
504 session_load_bail:
505 session_state_free(state);
506
507 session_load_ok:
508
509 node = parse_find_node("window", node->next);
510 }
511
512 /* sort them by their stacking order */
513 session_saved_state = g_list_sort(session_saved_state,
514 (GCompareFunc)stack_sort);
515
516 xmlFreeDoc(doc);
517 }
518
519 #endif
This page took 0.058883 seconds and 4 git commands to generate.