]> Dogcows Code - chaz/openbox/blob - openbox/session.c
mem leak
[chaz/openbox] / openbox / session.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 session.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 /* This session code is largely inspired by metacity code. */
20
21 #include "session.h"
22
23 struct _ObClient;
24
25 GList *session_saved_state = NULL;
26 gint session_desktop = -1;
27
28 #ifndef USE_SM
29 void session_startup(gint argc, gchar **argv) {}
30 void session_shutdown(gboolean permanent) {}
31 GList* session_state_find(struct _ObClient *c) { return NULL; }
32 #else
33
34 #include "debug.h"
35 #include "openbox.h"
36 #include "client.h"
37 #include "prop.h"
38 #include "focus.h"
39 #include "screen.h"
40 #include "gettext.h"
41 #include "parser/parse.h"
42
43 #include <time.h>
44 #include <errno.h>
45 #include <stdio.h>
46
47 #ifdef HAVE_UNISTD_H
48 # include <sys/types.h>
49 # include <unistd.h>
50 #endif
51
52 #include <X11/SM/SMlib.h>
53
54 #define SM_ERR_LEN 1024
55
56 static SmcConn sm_conn;
57 static gint sm_argc;
58 static gchar **sm_argv;
59
60 /* Data saved from the first level save yourself */
61 typedef struct {
62 ObClient *focus_client;
63 gint desktop;
64 } ObSMSaveData;
65
66 static gboolean session_connect();
67
68 static void session_load_file(const gchar *path);
69 static gboolean session_save_to_file(const ObSMSaveData *savedata);
70
71 static void session_setup_program();
72 static void session_setup_user();
73 static void session_setup_restart_style(gboolean restart);
74 static void session_setup_pid();
75 static void session_setup_priority();
76 static void session_setup_clone_command();
77 static void session_setup_restart_command();
78
79 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
80 Bool shutdown, gint interact_style, Bool fast);
81 static void sm_die(SmcConn conn, SmPointer data);
82 static void sm_save_complete(SmcConn conn, SmPointer data);
83 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
84
85 static gboolean session_state_cmp(ObSessionState *s, ObClient *c);
86 static void session_state_free(ObSessionState *state);
87
88 void session_startup(gint argc, gchar **argv)
89 {
90 gchar *dir;
91
92 if (!ob_sm_use) return;
93
94 sm_argc = argc;
95 sm_argv = argv;
96
97 dir = g_build_filename(parse_xdg_data_home_path(),
98 "openbox", "sessions", NULL);
99 if (!parse_mkdir_path(dir, 0700)) {
100 g_message(_("Unable to make directory '%s': %s"),
101 dir, g_strerror(errno));
102 }
103
104 if (ob_sm_save_file != NULL) {
105 ob_debug_type(OB_DEBUG_SM, "Loading from session file %s\n",
106 ob_sm_save_file);
107 session_load_file(ob_sm_save_file);
108 } else {
109 gchar *filename;
110
111 /* this algo is from metacity */
112 filename = g_strdup_printf("%u-%u-%u.obs",
113 (guint)time(NULL),
114 (guint)getpid(),
115 g_random_int());
116 ob_sm_save_file = g_build_filename(dir, filename, NULL);
117 g_free(filename);
118 }
119
120 if (session_connect()) {
121 session_setup_program();
122 session_setup_user();
123 session_setup_restart_style(TRUE);
124 session_setup_pid();
125 session_setup_priority();
126 session_setup_clone_command();
127 }
128
129 g_free(dir);
130 }
131
132 void session_shutdown(gboolean permanent)
133 {
134 if (!ob_sm_use) return;
135
136 if (sm_conn) {
137 /* if permanent is true then we will change our session state so that
138 the SM won't run us again */
139 session_setup_restart_style(!permanent);
140
141 SmcCloseConnection(sm_conn, 0, NULL);
142
143 while (session_saved_state) {
144 session_state_free(session_saved_state->data);
145 session_saved_state = g_list_delete_link(session_saved_state,
146 session_saved_state);
147 }
148 }
149 }
150
151 /*! Connect to the session manager and set up our callback functions */
152 static gboolean session_connect()
153 {
154 SmcCallbacks cb;
155 gchar *oldid;
156 gchar sm_err[SM_ERR_LEN];
157
158 /* set up our callback functions */
159 cb.save_yourself.callback = sm_save_yourself;
160 cb.save_yourself.client_data = NULL;
161 cb.die.callback = sm_die;
162 cb.die.client_data = NULL;
163 cb.save_complete.callback = sm_save_complete;
164 cb.save_complete.client_data = NULL;
165 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
166 cb.shutdown_cancelled.client_data = NULL;
167
168 /* connect to the server */
169 oldid = ob_sm_id;
170 ob_debug_type(OB_DEBUG_SM, "Connecting to SM with id: %s\n",
171 oldid ? oldid : "(null)");
172 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
173 SmcSaveYourselfProcMask |
174 SmcDieProcMask |
175 SmcSaveCompleteProcMask |
176 SmcShutdownCancelledProcMask,
177 &cb, oldid, &ob_sm_id,
178 SM_ERR_LEN-1, sm_err);
179 g_free(oldid);
180 if (sm_conn == NULL)
181 ob_debug("Failed to connect to session manager: %s\n", sm_err);
182 return sm_conn != NULL;
183 }
184
185 static void session_setup_program()
186 {
187 SmPropValue vals = {
188 .value = sm_argv[0],
189 .length = strlen(sm_argv[0]) + 1
190 };
191 SmProp prop = {
192 .name = g_strdup(SmProgram),
193 .type = g_strdup(SmARRAY8),
194 .num_vals = 1,
195 .vals = &vals
196 };
197 SmProp *list = &prop;
198 SmcSetProperties(sm_conn, 1, &list);
199 g_free(prop.name);
200 g_free(prop.type);
201 }
202
203 static void session_setup_user()
204 {
205 char *user = g_strdup(g_get_user_name());
206
207 SmPropValue vals = {
208 .value = user,
209 .length = strlen(user) + 1
210 };
211 SmProp prop = {
212 .name = g_strdup(SmUserID),
213 .type = g_strdup(SmARRAY8),
214 .num_vals = 1,
215 .vals = &vals
216 };
217 SmProp *list = &prop;
218 SmcSetProperties(sm_conn, 1, &list);
219 g_free(prop.name);
220 g_free(prop.type);
221 g_free(user);
222 }
223
224 static void session_setup_restart_style(gboolean restart)
225 {
226 char restart_hint = restart ? SmRestartImmediately : SmRestartIfRunning;
227
228 SmPropValue vals = {
229 .value = &restart_hint,
230 .length = 1
231 };
232 SmProp prop = {
233 .name = g_strdup(SmRestartStyleHint),
234 .type = g_strdup(SmCARD8),
235 .num_vals = 1,
236 .vals = &vals
237 };
238 SmProp *list = &prop;
239 SmcSetProperties(sm_conn, 1, &list);
240 g_free(prop.name);
241 g_free(prop.type);
242 }
243
244 static void session_setup_pid()
245 {
246 gchar *pid = g_strdup_printf("%ld", (glong) getpid());
247
248 SmPropValue vals = {
249 .value = pid,
250 .length = strlen(pid) + 1
251 };
252 SmProp prop = {
253 .name = g_strdup(SmProcessID),
254 .type = g_strdup(SmARRAY8),
255 .num_vals = 1,
256 .vals = &vals
257 };
258 SmProp *list = &prop;
259 SmcSetProperties(sm_conn, 1, &list);
260 g_free(prop.name);
261 g_free(prop.type);
262 g_free(pid);
263 }
264
265 /*! This is a gnome-session-manager extension */
266 static void session_setup_priority()
267 {
268 gchar priority = 20; /* 20 is a lower prioity to run before other apps */
269
270 SmPropValue vals = {
271 .value = &priority,
272 .length = 1
273 };
274 SmProp prop = {
275 .name = g_strdup("_GSM_Priority"),
276 .type = g_strdup(SmCARD8),
277 .num_vals = 1,
278 .vals = &vals
279 };
280 SmProp *list = &prop;
281 SmcSetProperties(sm_conn, 1, &list);
282 g_free(prop.name);
283 g_free(prop.type);
284 }
285
286 static void session_setup_clone_command()
287 {
288 gint i;
289
290 SmPropValue *vals = g_new(SmPropValue, sm_argc);
291 SmProp prop = {
292 .name = g_strdup(SmCloneCommand),
293 .type = g_strdup(SmLISTofARRAY8),
294 .num_vals = sm_argc,
295 .vals = vals
296 };
297
298 for (i = 0; i < sm_argc; ++i) {
299 vals[i].value = sm_argv[i];
300 vals[i].length = strlen(sm_argv[i]) + 1;
301 }
302
303 SmProp *list = &prop;
304 SmcSetProperties(sm_conn, 1, &list);
305 g_free(prop.name);
306 g_free(prop.type);
307 g_free(vals);
308 }
309
310 static void session_setup_restart_command()
311 {
312 gint i;
313
314 SmPropValue *vals = g_new(SmPropValue, sm_argc + 4);
315 SmProp prop = {
316 .name = g_strdup(SmRestartCommand),
317 .type = g_strdup(SmLISTofARRAY8),
318 .num_vals = sm_argc + 4,
319 .vals = vals
320 };
321
322 for (i = 0; i < sm_argc; ++i) {
323 vals[i].value = sm_argv[i];
324 vals[i].length = strlen(sm_argv[i]) + 1;
325 }
326
327 vals[i].value = g_strdup("--sm-save-file");
328 vals[i].length = strlen("--sm-save-file") + 1;
329 vals[i+1].value = ob_sm_save_file;
330 vals[i+1].length = strlen(ob_sm_save_file) + 1;
331
332 vals[i+2].value = g_strdup("--sm-client-id");
333 vals[i+2].length = strlen("--sm-client-id") + 1;
334 vals[i+3].value = ob_sm_id;
335 vals[i+3].length = strlen(ob_sm_id) + 1;
336
337 SmProp *list = &prop;
338 SmcSetProperties(sm_conn, 1, &list);
339 g_free(prop.name);
340 g_free(prop.type);
341 g_free(vals[i].value);
342 g_free(vals[i+2].value);
343 g_free(vals);
344 }
345
346 static ObSMSaveData *sm_save_get_data()
347 {
348 ObSMSaveData *savedata = g_new0(ObSMSaveData, 1);
349 savedata->focus_client = focus_client;
350 savedata->desktop = screen_desktop;
351 return savedata;
352 }
353
354 static void sm_save_yourself_2(SmcConn conn, SmPointer data)
355 {
356 gboolean success;
357 ObSMSaveData *savedata = data;
358
359 /* save the current state */
360 ob_debug_type(OB_DEBUG_SM, "Session save phase 2 requested\n");
361 ob_debug_type(OB_DEBUG_SM,
362 " Saving session to file '%s'\n", ob_sm_save_file);
363 if (savedata == NULL)
364 savedata = sm_save_get_data();
365 success = session_save_to_file(savedata);
366 g_free(savedata);
367
368 /* tell the session manager how to restore this state */
369 if (success) session_setup_restart_command();
370
371 ob_debug_type(OB_DEBUG_SM, "Saving is done (success = %d)\n", success);
372 SmcSaveYourselfDone(conn, success);
373 }
374
375
376 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
377 Bool shutdown, gint interact_style, Bool fast)
378 {
379 ObSMSaveData *savedata = NULL;
380 gchar *vendor;
381
382 ob_debug_type(OB_DEBUG_SM, "Session save requested\n");
383
384 vendor = SmcVendor(sm_conn);
385 ob_debug_type(OB_DEBUG_SM, "Session manager's vendor: %s\n", vendor);
386
387 if (!strcmp(vendor, "KDE")) {
388 /* ksmserver guarantees that phase 1 will complete before allowing any
389 clients interaction, so we can save this sanely here before they
390 get messed up from interaction */
391 savedata = sm_save_get_data();
392 }
393 free(vendor);
394
395 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_2, savedata)) {
396 ob_debug_type(OB_DEBUG_SM, "Requst for phase 2 failed\n");
397 g_free(savedata);
398 SmcSaveYourselfDone(conn, FALSE);
399 }
400 }
401
402 static void sm_die(SmcConn conn, SmPointer data)
403 {
404 ob_debug_type(OB_DEBUG_SM, "Die requested\n");
405 ob_exit(0);
406 }
407
408 static void sm_save_complete(SmcConn conn, SmPointer data)
409 {
410 ob_debug_type(OB_DEBUG_SM, "Save complete\n");
411 }
412
413 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
414 {
415 ob_debug_type(OB_DEBUG_SM, "Shutdown cancelled\n");
416 }
417
418 static gboolean session_save_to_file(const ObSMSaveData *savedata)
419 {
420 FILE *f;
421 GList *it;
422 gboolean success = TRUE;
423
424 f = fopen(ob_sm_save_file, "w");
425 if (!f) {
426 success = FALSE;
427 g_message(_("Unable to save the session to '%s': %s"),
428 ob_sm_save_file, g_strerror(errno));
429 } else {
430 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
431 fprintf(f, "<openbox_session>\n\n");
432
433 fprintf(f, "<desktop>%d</desktop>\n", savedata->desktop);
434
435 /* they are ordered top to bottom in stacking order */
436 for (it = stacking_list; it; it = g_list_next(it)) {
437 gint prex, prey, prew, preh;
438 ObClient *c;
439 gchar *t;
440
441 if (WINDOW_IS_CLIENT(it->data))
442 c = WINDOW_AS_CLIENT(it->data);
443 else
444 continue;
445
446 if (!client_normal(c))
447 continue;
448
449 if (!c->sm_client_id) {
450 ob_debug_type(OB_DEBUG_SM, "Client %s does not have a "
451 "session id set\n",
452 c->title);
453 if (!c->wm_command) {
454 ob_debug_type(OB_DEBUG_SM, "Client %s does not have an "
455 "oldskool wm_command set either. We won't "
456 "be saving its data\n",
457 c->title);
458 continue;
459 }
460 }
461
462 ob_debug_type(OB_DEBUG_SM, "Saving state for client %s\n",
463 c->title);
464
465 prex = c->area.x;
466 prey = c->area.y;
467 prew = c->area.width;
468 preh = c->area.height;
469 if (c->fullscreen) {
470 prex = c->pre_fullscreen_area.x;
471 prey = c->pre_fullscreen_area.x;
472 prew = c->pre_fullscreen_area.width;
473 preh = c->pre_fullscreen_area.height;
474 }
475 if (c->max_horz) {
476 prex = c->pre_max_area.x;
477 prew = c->pre_max_area.width;
478 }
479 if (c->max_vert) {
480 prey = c->pre_max_area.y;
481 preh = c->pre_max_area.height;
482 }
483
484 if (c->sm_client_id)
485 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
486 else
487 fprintf(f, "<window command=\"%s\">\n", c->wm_command);
488
489 t = g_markup_escape_text(c->name, -1);
490 fprintf(f, "\t<name>%s</name>\n", t);
491 g_free(t);
492
493 t = g_markup_escape_text(c->class, -1);
494 fprintf(f, "\t<class>%s</class>\n", t);
495 g_free(t);
496
497 t = g_markup_escape_text(c->role, -1);
498 fprintf(f, "\t<role>%s</role>\n", t);
499 g_free(t);
500
501 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
502 fprintf(f, "\t<x>%d</x>\n", prex);
503 fprintf(f, "\t<y>%d</y>\n", prey);
504 fprintf(f, "\t<width>%d</width>\n", prew);
505 fprintf(f, "\t<height>%d</height>\n", preh);
506 if (c->shaded)
507 fprintf(f, "\t<shaded />\n");
508 if (c->iconic)
509 fprintf(f, "\t<iconic />\n");
510 if (c->skip_pager)
511 fprintf(f, "\t<skip_pager />\n");
512 if (c->skip_taskbar)
513 fprintf(f, "\t<skip_taskbar />\n");
514 if (c->fullscreen)
515 fprintf(f, "\t<fullscreen />\n");
516 if (c->above)
517 fprintf(f, "\t<above />\n");
518 if (c->below)
519 fprintf(f, "\t<below />\n");
520 if (c->max_horz)
521 fprintf(f, "\t<max_horz />\n");
522 if (c->max_vert)
523 fprintf(f, "\t<max_vert />\n");
524 if (c->undecorated)
525 fprintf(f, "\t<undecorated />\n");
526 if (savedata->focus_client == c)
527 fprintf(f, "\t<focused />\n");
528 fprintf(f, "</window>\n\n");
529 }
530
531 fprintf(f, "</openbox_session>\n");
532
533 if (fflush(f)) {
534 success = FALSE;
535 g_message(_("Error while saving the session to '%s': %s"),
536 ob_sm_save_file, g_strerror(errno));
537 }
538 fclose(f);
539 }
540
541 return success;
542 }
543
544 static void session_state_free(ObSessionState *state)
545 {
546 if (state) {
547 g_free(state->id);
548 g_free(state->command);
549 g_free(state->name);
550 g_free(state->class);
551 g_free(state->role);
552
553 g_free(state);
554 }
555 }
556
557 static gboolean session_state_cmp(ObSessionState *s, ObClient *c)
558 {
559 ob_debug_type(OB_DEBUG_SM, "Comparing client against saved state: \n");
560 ob_debug_type(OB_DEBUG_SM, " client id: %s \n", c->sm_client_id);
561 ob_debug_type(OB_DEBUG_SM, " client name: %s \n", c->name);
562 ob_debug_type(OB_DEBUG_SM, " client class: %s \n", c->class);
563 ob_debug_type(OB_DEBUG_SM, " client role: %s \n", c->role);
564 ob_debug_type(OB_DEBUG_SM, " client command: %s \n",
565 c->wm_command ? c->wm_command : "(null)");
566 ob_debug_type(OB_DEBUG_SM, " state id: %s \n", s->id);
567 ob_debug_type(OB_DEBUG_SM, " state name: %s \n", s->name);
568 ob_debug_type(OB_DEBUG_SM, " state class: %s \n", s->class);
569 ob_debug_type(OB_DEBUG_SM, " state role: %s \n", s->role);
570 ob_debug_type(OB_DEBUG_SM, " state command: %s \n",
571 s->command ? s->command : "(null)");
572
573 if ((c->sm_client_id && s->id && !strcmp(c->sm_client_id, s->id)) ||
574 (c->wm_command && s->command && !strcmp(c->wm_command, s->command)))
575 {
576 return (!strcmp(s->name, c->name) &&
577 !strcmp(s->class, c->class) &&
578 !strcmp(s->role, c->role));
579 }
580 return FALSE;
581 }
582
583 GList* session_state_find(ObClient *c)
584 {
585 GList *it;
586
587 for (it = session_saved_state; it; it = g_list_next(it)) {
588 ObSessionState *s = it->data;
589 if (!s->matched && session_state_cmp(s, c)) {
590 s->matched = TRUE;
591 break;
592 }
593 }
594 return it;
595 }
596
597 static void session_load_file(const gchar *path)
598 {
599 xmlDocPtr doc;
600 xmlNodePtr node, n;
601
602 if (!parse_load(path, "openbox_session", &doc, &node))
603 return;
604
605 if ((n = parse_find_node("desktop", node->children)))
606 session_desktop = parse_int(doc, n);
607
608 for (node = parse_find_node("window", node->children); node != NULL;
609 node = parse_find_node("window", node->next))
610 {
611 ObSessionState *state;
612
613 state = g_new0(ObSessionState, 1);
614
615 if (!parse_attr_string("id", node, &state->id))
616 if (!parse_attr_string("command", node, &state->command))
617 goto session_load_bail;
618 if (!(n = parse_find_node("name", node->children)))
619 goto session_load_bail;
620 state->name = parse_string(doc, n);
621 if (!(n = parse_find_node("class", node->children)))
622 goto session_load_bail;
623 state->class = parse_string(doc, n);
624 if (!(n = parse_find_node("role", node->children)))
625 goto session_load_bail;
626 state->role = parse_string(doc, n);
627 if (!(n = parse_find_node("desktop", node->children)))
628 goto session_load_bail;
629 state->desktop = parse_int(doc, n);
630 if (!(n = parse_find_node("x", node->children)))
631 goto session_load_bail;
632 state->x = parse_int(doc, n);
633 if (!(n = parse_find_node("y", node->children)))
634 goto session_load_bail;
635 state->y = parse_int(doc, n);
636 if (!(n = parse_find_node("width", node->children)))
637 goto session_load_bail;
638 state->w = parse_int(doc, n);
639 if (!(n = parse_find_node("height", node->children)))
640 goto session_load_bail;
641 state->h = parse_int(doc, n);
642
643 state->shaded =
644 parse_find_node("shaded", node->children) != NULL;
645 state->iconic =
646 parse_find_node("iconic", node->children) != NULL;
647 state->skip_pager =
648 parse_find_node("skip_pager", node->children) != NULL;
649 state->skip_taskbar =
650 parse_find_node("skip_taskbar", node->children) != NULL;
651 state->fullscreen =
652 parse_find_node("fullscreen", node->children) != NULL;
653 state->above =
654 parse_find_node("above", node->children) != NULL;
655 state->below =
656 parse_find_node("below", node->children) != NULL;
657 state->max_horz =
658 parse_find_node("max_horz", node->children) != NULL;
659 state->max_vert =
660 parse_find_node("max_vert", node->children) != NULL;
661 state->undecorated =
662 parse_find_node("undecorated", node->children) != NULL;
663 state->focused =
664 parse_find_node("focused", node->children) != NULL;
665
666 /* save this. they are in the file in stacking order, so preserve
667 that order here */
668 session_saved_state = g_list_append(session_saved_state, state);
669 continue;
670
671 session_load_bail:
672 session_state_free(state);
673 }
674
675 xmlFreeDoc(doc);
676 }
677
678 #endif
This page took 0.069877 seconds and 5 git commands to generate.