1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 session.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 /* This session code is largely inspired by metacity code. */
25 GList *session_saved_state = NULL;
26 gint session_desktop = -1;
29 void session_startup(gint argc, gchar **argv) {}
30 void session_shutdown(gboolean permanent) {}
31 GList* session_state_find(struct _ObClient *c) { return NULL; }
41 #include "parser/parse.h"
48 # include <sys/types.h>
52 #include <X11/SM/SMlib.h>
54 #define SM_ERR_LEN 1024
56 static SmcConn sm_conn;
58 static gchar **sm_argv;
60 /* Data saved from the first level save yourself */
62 ObClient *focus_client;
66 static gboolean session_connect();
68 static void session_load_file(const gchar *path);
69 static gboolean session_save_to_file(const ObSMSaveData *savedata);
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();
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);
85 static gboolean session_state_cmp(ObSessionState *s, ObClient *c);
86 static void session_state_free(ObSessionState *state);
88 void session_startup(gint argc, gchar **argv)
92 if (!ob_sm_use) return;
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));
104 if (ob_sm_save_file != NULL) {
105 ob_debug_type(OB_DEBUG_SM, "Loading from session file %s\n",
107 session_load_file(ob_sm_save_file);
111 /* this algo is from metacity */
112 filename = g_strdup_printf("%u-%u-%u.obs",
116 ob_sm_save_file = g_build_filename(dir, filename, NULL);
120 if (session_connect()) {
121 session_setup_program();
122 session_setup_user();
123 session_setup_restart_style(TRUE);
125 session_setup_priority();
126 session_setup_clone_command();
132 void session_shutdown(gboolean permanent)
134 if (!ob_sm_use) return;
137 /* if permanent is true then we will change our session state so that
138 the SM won't run us again */
140 session_setup_restart_style(FALSE);
142 SmcCloseConnection(sm_conn, 0, NULL);
144 while (session_saved_state) {
145 session_state_free(session_saved_state->data);
146 session_saved_state = g_list_delete_link(session_saved_state,
147 session_saved_state);
152 /*! Connect to the session manager and set up our callback functions */
153 static gboolean session_connect()
157 gchar sm_err[SM_ERR_LEN];
159 /* set up our callback functions */
160 cb.save_yourself.callback = sm_save_yourself;
161 cb.save_yourself.client_data = NULL;
162 cb.die.callback = sm_die;
163 cb.die.client_data = NULL;
164 cb.save_complete.callback = sm_save_complete;
165 cb.save_complete.client_data = NULL;
166 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
167 cb.shutdown_cancelled.client_data = NULL;
169 /* connect to the server */
171 ob_debug_type(OB_DEBUG_SM, "Connecting to SM with id: %s\n",
172 oldid ? oldid : "(null)");
173 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
174 SmcSaveYourselfProcMask |
176 SmcSaveCompleteProcMask |
177 SmcShutdownCancelledProcMask,
178 &cb, oldid, &ob_sm_id,
179 SM_ERR_LEN-1, sm_err);
181 ob_debug_type(OB_DEBUG_SM, "Connected to SM with id: %s\n", ob_sm_id);
183 ob_debug("Failed to connect to session manager: %s\n", sm_err);
184 return sm_conn != NULL;
187 static void session_setup_program()
191 .length = strlen(sm_argv[0]) + 1
194 .name = g_strdup(SmProgram),
195 .type = g_strdup(SmARRAY8),
199 SmProp *list = ∝
200 ob_debug_type(OB_DEBUG_SM, "Setting program: %s\n", sm_argv[0]);
201 SmcSetProperties(sm_conn, 1, &list);
206 static void session_setup_user()
208 char *user = g_strdup(g_get_user_name());
212 .length = strlen(user) + 1
215 .name = g_strdup(SmUserID),
216 .type = g_strdup(SmARRAY8),
220 SmProp *list = ∝
221 ob_debug_type(OB_DEBUG_SM, "Setting user: %s\n", user);
222 SmcSetProperties(sm_conn, 1, &list);
228 static void session_setup_restart_style(gboolean restart)
230 gchar restart_hint = restart ? SmRestartImmediately : SmRestartIfRunning;
233 .value = &restart_hint,
237 .name = g_strdup(SmRestartStyleHint),
238 .type = g_strdup(SmCARD8),
242 SmProp *list = ∝
243 ob_debug_type(OB_DEBUG_SM, "Setting restart: %d\n", restart);
244 SmcSetProperties(sm_conn, 1, &list);
249 static void session_setup_pid()
251 gchar *pid = g_strdup_printf("%ld", (glong) getpid());
255 .length = strlen(pid) + 1
258 .name = g_strdup(SmProcessID),
259 .type = g_strdup(SmARRAY8),
263 SmProp *list = ∝
264 ob_debug_type(OB_DEBUG_SM, "Setting pid: %s\n", pid);
265 SmcSetProperties(sm_conn, 1, &list);
271 /*! This is a gnome-session-manager extension */
272 static void session_setup_priority()
274 gchar priority = 20; /* 20 is a lower prioity to run before other apps */
281 .name = g_strdup("_GSM_Priority"),
282 .type = g_strdup(SmCARD8),
286 SmProp *list = ∝
287 ob_debug_type(OB_DEBUG_SM, "Setting priority: %d\n", priority);
288 SmcSetProperties(sm_conn, 1, &list);
293 static void session_setup_clone_command()
297 SmPropValue *vals = g_new(SmPropValue, sm_argc);
299 .name = g_strdup(SmCloneCommand),
300 .type = g_strdup(SmLISTofARRAY8),
304 SmProp *list = ∝
306 ob_debug_type(OB_DEBUG_SM, "Setting clone command: (%d)\n", sm_argc);
307 for (i = 0; i < sm_argc; ++i) {
308 vals[i].value = sm_argv[i];
309 vals[i].length = strlen(sm_argv[i]) + 1;
310 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i].value);
313 SmcSetProperties(sm_conn, 1, &list);
319 static void session_setup_restart_command()
324 if (ob_config_type) num += 2;
326 SmPropValue *vals = g_new(SmPropValue, sm_argc + num);
328 .name = g_strdup(SmRestartCommand),
329 .type = g_strdup(SmLISTofARRAY8),
330 .num_vals = sm_argc + num,
333 SmProp *list = ∝
335 ob_debug_type(OB_DEBUG_SM, "Setting restart command: (%d)\n", sm_argc+4);
336 for (i = 0; i < sm_argc; ++i) {
337 vals[i].value = sm_argv[i];
338 vals[i].length = strlen(sm_argv[i]) + 1;
339 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i].value);
342 vals[i].value = g_strdup("--sm-client-id");
343 vals[i].length = strlen("--sm-client-id") + 1;
344 vals[i+1].value = ob_sm_id;
345 vals[i+1].length = strlen(ob_sm_id) + 1;
346 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i].value);
347 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i+1].value);
349 vals[i+2].value = g_strdup("--sm-save-file");
350 vals[i+2].length = strlen("--sm-save-file") + 1;
351 vals[i+3].value = ob_sm_save_file;
352 vals[i+3].length = strlen(ob_sm_save_file) + 1;
353 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i+2].value);
354 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i+3].value);
356 if (ob_config_type) {
357 vals[i+4].value = g_strdup("--config-namespace");
358 vals[i+4].length = strlen("--config-namespace") + 1;
359 vals[i+5].value = ob_config_type;
360 vals[i+5].length = strlen(ob_config_type) + 1;
361 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i+4].value);
362 ob_debug_type(OB_DEBUG_SM, " %s\n", vals[i+5].value);
365 SmcSetProperties(sm_conn, 1, &list);
368 g_free(vals[i].value);
369 g_free(vals[i+2].value);
371 g_free(vals[i+4].value);
375 static ObSMSaveData *sm_save_get_data()
377 ObSMSaveData *savedata = g_new0(ObSMSaveData, 1);
378 savedata->focus_client = focus_client;
379 savedata->desktop = screen_desktop;
383 static void sm_save_yourself_2(SmcConn conn, SmPointer data)
386 ObSMSaveData *savedata = data;
388 /* save the current state */
389 ob_debug_type(OB_DEBUG_SM, "Session save phase 2 requested\n");
390 ob_debug_type(OB_DEBUG_SM,
391 " Saving session to file '%s'\n", ob_sm_save_file);
392 if (savedata == NULL)
393 savedata = sm_save_get_data();
394 success = session_save_to_file(savedata);
397 /* tell the session manager how to restore this state */
398 if (success) session_setup_restart_command();
400 ob_debug_type(OB_DEBUG_SM, "Saving is done (success = %d)\n", success);
401 SmcSaveYourselfDone(conn, success);
405 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
406 Bool shutdown, gint interact_style, Bool fast)
408 ObSMSaveData *savedata = NULL;
411 ob_debug_type(OB_DEBUG_SM, "Session save requested\n");
413 vendor = SmcVendor(sm_conn);
414 ob_debug_type(OB_DEBUG_SM, "Session manager's vendor: %s\n", vendor);
416 if (!strcmp(vendor, "KDE")) {
417 /* ksmserver guarantees that phase 1 will complete before allowing any
418 clients interaction, so we can save this sanely here before they
419 get messed up from interaction */
420 savedata = sm_save_get_data();
424 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_2, savedata)) {
425 ob_debug_type(OB_DEBUG_SM, "Requst for phase 2 failed\n");
427 SmcSaveYourselfDone(conn, FALSE);
431 static void sm_die(SmcConn conn, SmPointer data)
433 ob_debug_type(OB_DEBUG_SM, "Die requested\n");
437 static void sm_save_complete(SmcConn conn, SmPointer data)
439 ob_debug_type(OB_DEBUG_SM, "Save complete\n");
442 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
444 ob_debug_type(OB_DEBUG_SM, "Shutdown cancelled\n");
447 static gboolean session_save_to_file(const ObSMSaveData *savedata)
451 gboolean success = TRUE;
453 f = fopen(ob_sm_save_file, "w");
456 g_message(_("Unable to save the session to '%s': %s"),
457 ob_sm_save_file, g_strerror(errno));
459 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
460 fprintf(f, "<openbox_session>\n\n");
462 fprintf(f, "<desktop>%d</desktop>\n", savedata->desktop);
464 /* they are ordered top to bottom in stacking order */
465 for (it = stacking_list; it; it = g_list_next(it)) {
466 gint prex, prey, prew, preh;
470 if (WINDOW_IS_CLIENT(it->data))
471 c = WINDOW_AS_CLIENT(it->data);
475 if (!client_normal(c))
478 if (!c->sm_client_id) {
479 ob_debug_type(OB_DEBUG_SM, "Client %s does not have a "
482 if (!c->wm_command) {
483 ob_debug_type(OB_DEBUG_SM, "Client %s does not have an "
484 "oldskool wm_command set either. We won't "
485 "be saving its data\n",
491 ob_debug_type(OB_DEBUG_SM, "Saving state for client %s\n",
496 prew = c->area.width;
497 preh = c->area.height;
499 prex = c->pre_fullscreen_area.x;
500 prey = c->pre_fullscreen_area.x;
501 prew = c->pre_fullscreen_area.width;
502 preh = c->pre_fullscreen_area.height;
505 prex = c->pre_max_area.x;
506 prew = c->pre_max_area.width;
509 prey = c->pre_max_area.y;
510 preh = c->pre_max_area.height;
514 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
516 fprintf(f, "<window command=\"%s\">\n", c->wm_command);
518 t = g_markup_escape_text(c->name, -1);
519 fprintf(f, "\t<name>%s</name>\n", t);
522 t = g_markup_escape_text(c->class, -1);
523 fprintf(f, "\t<class>%s</class>\n", t);
526 t = g_markup_escape_text(c->role, -1);
527 fprintf(f, "\t<role>%s</role>\n", t);
530 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
531 fprintf(f, "\t<x>%d</x>\n", prex);
532 fprintf(f, "\t<y>%d</y>\n", prey);
533 fprintf(f, "\t<width>%d</width>\n", prew);
534 fprintf(f, "\t<height>%d</height>\n", preh);
536 fprintf(f, "\t<shaded />\n");
538 fprintf(f, "\t<iconic />\n");
540 fprintf(f, "\t<skip_pager />\n");
542 fprintf(f, "\t<skip_taskbar />\n");
544 fprintf(f, "\t<fullscreen />\n");
546 fprintf(f, "\t<above />\n");
548 fprintf(f, "\t<below />\n");
550 fprintf(f, "\t<max_horz />\n");
552 fprintf(f, "\t<max_vert />\n");
554 fprintf(f, "\t<undecorated />\n");
555 if (savedata->focus_client == c)
556 fprintf(f, "\t<focused />\n");
557 fprintf(f, "</window>\n\n");
560 fprintf(f, "</openbox_session>\n");
564 g_message(_("Error while saving the session to '%s': %s"),
565 ob_sm_save_file, g_strerror(errno));
573 static void session_state_free(ObSessionState *state)
577 g_free(state->command);
579 g_free(state->class);
586 static gboolean session_state_cmp(ObSessionState *s, ObClient *c)
588 ob_debug_type(OB_DEBUG_SM, "Comparing client against saved state: \n");
589 ob_debug_type(OB_DEBUG_SM, " client id: %s \n", c->sm_client_id);
590 ob_debug_type(OB_DEBUG_SM, " client name: %s \n", c->name);
591 ob_debug_type(OB_DEBUG_SM, " client class: %s \n", c->class);
592 ob_debug_type(OB_DEBUG_SM, " client role: %s \n", c->role);
593 ob_debug_type(OB_DEBUG_SM, " client command: %s \n",
594 c->wm_command ? c->wm_command : "(null)");
595 ob_debug_type(OB_DEBUG_SM, " state id: %s \n", s->id);
596 ob_debug_type(OB_DEBUG_SM, " state name: %s \n", s->name);
597 ob_debug_type(OB_DEBUG_SM, " state class: %s \n", s->class);
598 ob_debug_type(OB_DEBUG_SM, " state role: %s \n", s->role);
599 ob_debug_type(OB_DEBUG_SM, " state command: %s \n",
600 s->command ? s->command : "(null)");
602 if ((c->sm_client_id && s->id && !strcmp(c->sm_client_id, s->id)) ||
603 (c->wm_command && s->command && !strcmp(c->wm_command, s->command)))
605 return (!strcmp(s->name, c->name) &&
606 !strcmp(s->class, c->class) &&
607 !strcmp(s->role, c->role));
612 GList* session_state_find(ObClient *c)
616 for (it = session_saved_state; it; it = g_list_next(it)) {
617 ObSessionState *s = it->data;
618 if (!s->matched && session_state_cmp(s, c)) {
626 static void session_load_file(const gchar *path)
631 if (!parse_load(path, "openbox_session", &doc, &node))
634 if ((n = parse_find_node("desktop", node->children)))
635 session_desktop = parse_int(doc, n);
637 for (node = parse_find_node("window", node->children); node != NULL;
638 node = parse_find_node("window", node->next))
640 ObSessionState *state;
642 state = g_new0(ObSessionState, 1);
644 if (!parse_attr_string("id", node, &state->id))
645 if (!parse_attr_string("command", node, &state->command))
646 goto session_load_bail;
647 if (!(n = parse_find_node("name", node->children)))
648 goto session_load_bail;
649 state->name = parse_string(doc, n);
650 if (!(n = parse_find_node("class", node->children)))
651 goto session_load_bail;
652 state->class = parse_string(doc, n);
653 if (!(n = parse_find_node("role", node->children)))
654 goto session_load_bail;
655 state->role = parse_string(doc, n);
656 if (!(n = parse_find_node("desktop", node->children)))
657 goto session_load_bail;
658 state->desktop = parse_int(doc, n);
659 if (!(n = parse_find_node("x", node->children)))
660 goto session_load_bail;
661 state->x = parse_int(doc, n);
662 if (!(n = parse_find_node("y", node->children)))
663 goto session_load_bail;
664 state->y = parse_int(doc, n);
665 if (!(n = parse_find_node("width", node->children)))
666 goto session_load_bail;
667 state->w = parse_int(doc, n);
668 if (!(n = parse_find_node("height", node->children)))
669 goto session_load_bail;
670 state->h = parse_int(doc, n);
673 parse_find_node("shaded", node->children) != NULL;
675 parse_find_node("iconic", node->children) != NULL;
677 parse_find_node("skip_pager", node->children) != NULL;
678 state->skip_taskbar =
679 parse_find_node("skip_taskbar", node->children) != NULL;
681 parse_find_node("fullscreen", node->children) != NULL;
683 parse_find_node("above", node->children) != NULL;
685 parse_find_node("below", node->children) != NULL;
687 parse_find_node("max_horz", node->children) != NULL;
689 parse_find_node("max_vert", node->children) != NULL;
691 parse_find_node("undecorated", node->children) != NULL;
693 parse_find_node("focused", node->children) != NULL;
695 /* save this. they are in the file in stacking order, so preserve
697 session_saved_state = g_list_append(session_saved_state, state);
701 session_state_free(state);