1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 session.c for the Openbox window manager
4 Copyright (c) 2003 Ben 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. */
26 GList *session_saved_state;
28 void session_startup(gint *argc, gchar ***argv) {}
29 void session_shutdown() {}
30 GList* session_state_find(ObClient *c) { return NULL; }
31 gboolean session_state_cmp(ObSessionState *s, ObClient *c) { return FALSE; }
32 void session_state_free(ObSessionState *state) {}
42 #include "parser/parse.h"
49 # include <sys/types.h>
53 #include <X11/SM/SMlib.h>
55 GList *session_saved_state;
57 static gboolean sm_disable;
58 static SmcConn sm_conn;
59 static gchar *save_file;
62 static gchar **sm_argv;
63 static gchar *sm_sessions_path;
65 static void session_load(gchar *path);
66 static gboolean session_save();
68 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
69 Bool shutdown, gint interact_style, Bool fast);
70 static void sm_die(SmcConn conn, SmPointer data);
71 static void sm_save_complete(SmcConn conn, SmPointer data);
72 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
74 static void save_commands()
77 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
78 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
81 prop_cmd.vals = g_new(SmPropValue, sm_argc);
82 prop_cmd.num_vals = sm_argc;
83 for (i = 0; i < sm_argc; ++i) {
84 prop_cmd.vals[i].value = sm_argv[i];
85 prop_cmd.vals[i].length = strlen(sm_argv[i]);
88 prop_res.vals = g_new(SmPropValue, sm_argc + 2);
89 prop_res.num_vals = sm_argc + 2;
90 for (i = 0; i < sm_argc; ++i) {
91 prop_res.vals[i].value = sm_argv[i];
92 prop_res.vals[i].length = strlen(sm_argv[i]);
96 prop_res.vals[i].value = "--sm-save-file";
97 prop_res.vals[i++].length = strlen("--sm-save-file");
98 prop_res.vals[i].value = save_file;
99 prop_res.vals[i++].length = strlen(save_file);
101 prop_res.vals[i].value = "--sm-client-id";
102 prop_res.vals[i++].length = strlen("--sm-client-id");
103 prop_res.vals[i].value = sm_id;
104 prop_res.vals[i++].length = strlen(sm_id);
107 props[0] = &prop_res;
108 props[1] = &prop_cmd;
109 SmcSetProperties(sm_conn, 2, props);
111 g_free(prop_res.vals);
112 g_free(prop_cmd.vals);
115 static void remove_args(gint *argc, gchar ***argv, gint index, gint num)
119 for (i = index; i < index + num; ++i)
120 (*argv)[i] = (*argv)[i+num];
124 static void parse_args(gint *argc, gchar ***argv)
128 for (i = 1; i < *argc; ++i) {
129 if (!strcmp((*argv)[i], "--sm-client-id")) {
130 if (i == *argc - 1) /* no args left */
131 g_printerr(_("--sm-client-id requires an argument\n"));
133 sm_id = g_strdup((*argv)[i+1]);
134 remove_args(argc, argv, i, 2);
137 } else if (!strcmp((*argv)[i], "--sm-save-file")) {
138 if (i == *argc - 1) /* no args left */
139 g_printerr(_("--sm-save-file requires an argument\n"));
141 save_file = g_strdup((*argv)[i+1]);
142 remove_args(argc, argv, i, 2);
145 } else if (!strcmp((*argv)[i], "--sm-disable")) {
147 remove_args(argc, argv, i, 1);
152 void session_startup(gint *argc, gchar ***argv)
154 #define SM_ERR_LEN 1024
157 gchar sm_err[SM_ERR_LEN];
159 parse_args(argc, argv);
164 sm_sessions_path = g_build_filename(parse_xdg_data_home_path(),
165 "openbox", "sessions", NULL);
166 parse_mkdir_path(sm_sessions_path, 0700);
169 session_load(save_file);
174 cb.save_yourself.callback = sm_save_yourself;
175 cb.save_yourself.client_data = NULL;
177 cb.die.callback = sm_die;
178 cb.die.client_data = NULL;
180 cb.save_complete.callback = sm_save_complete;
181 cb.save_complete.client_data = NULL;
183 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
184 cb.shutdown_cancelled.client_data = NULL;
186 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
187 SmcSaveYourselfProcMask |
189 SmcSaveCompleteProcMask |
190 SmcShutdownCancelledProcMask,
194 ob_debug("Failed to connect to session manager: %s\n", sm_err);
196 SmPropValue val_prog;
198 SmPropValue val_hint;
201 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
202 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
203 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
204 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
205 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
210 val_prog.value = sm_argv[0];
211 val_prog.length = strlen(sm_argv[0]);
213 val_uid.value = g_strdup(g_get_user_name());
214 val_uid.length = strlen(val_uid.value);
216 hint = SmRestartImmediately;
217 val_hint.value = &hint;
220 sprintf(pid, "%ld", (glong)getpid());
222 val_pid.length = strlen(pid);
224 /* priority with gnome-session-manager, low to run before other apps */
226 val_pri.value = &pri;
229 prop_prog.vals = &val_prog;
230 prop_uid.vals = &val_uid;
231 prop_hint.vals = &val_hint;
232 prop_pid.vals = &val_pid;
233 prop_pri.vals = &val_pri;
235 props[0] = &prop_prog;
236 props[1] = &prop_uid;
237 props[2] = &prop_hint;
238 props[3] = &prop_pid;
239 props[4] = &prop_pri;
241 SmcSetProperties(sm_conn, 5, props);
243 g_free(val_uid.value);
249 void session_shutdown()
251 g_free(sm_sessions_path);
256 SmPropValue val_hint;
257 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
261 /* when we exit, we want to reset this to a more friendly state */
262 hint = SmRestartIfRunning;
263 val_hint.value = &hint;
266 prop_hint.vals = &val_hint;
268 props[0] = &prop_hint;
270 SmcSetProperties(sm_conn, 1, props);
272 SmcCloseConnection(sm_conn, 0, NULL);
274 while (session_saved_state) {
275 session_state_free(session_saved_state->data);
276 session_saved_state = g_list_delete_link(session_saved_state,
277 session_saved_state);
282 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
286 success = session_save();
289 SmcSaveYourselfDone(conn, success);
292 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
293 Bool shutdown, gint interact_style, Bool fast)
295 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
296 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
297 SmcSaveYourselfDone(conn, FALSE);
301 static void sm_die(SmcConn conn, SmPointer data)
306 static void sm_save_complete(SmcConn conn, SmPointer data)
310 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
314 static gboolean session_save()
319 gboolean success = TRUE;
321 /* this algo is from metacity */
322 filename = g_strdup_printf("%d-%d-%u.obs",
326 save_file = g_build_filename(sm_sessions_path, filename, NULL);
329 f = fopen(save_file, "w");
332 g_warning("unable to save the session to %s: %s",
333 save_file, strerror(errno));
337 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
338 fprintf(f, "<openbox_session id=\"%s\">\n\n", sm_id);
340 for (it = stacking_list; it; it = g_list_next(it)) {
341 gint prex, prey, prew, preh;
345 if (WINDOW_IS_CLIENT(it->data))
346 c = WINDOW_AS_CLIENT(it->data);
350 if (!client_normal(c))
353 if (!c->sm_client_id)
358 prew = c->area.width;
359 preh = c->area.height;
361 prex = c->pre_fullscreen_area.x;
362 prey = c->pre_fullscreen_area.x;
363 prew = c->pre_fullscreen_area.width;
364 preh = c->pre_fullscreen_area.height;
367 prex = c->pre_max_area.x;
368 prew = c->pre_max_area.width;
371 prey = c->pre_max_area.y;
372 preh = c->pre_max_area.height;
375 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
377 t = g_markup_escape_text(c->name, -1);
378 fprintf(f, "\t<name>%s</name>\n", t);
381 t = g_markup_escape_text(c->class, -1);
382 fprintf(f, "\t<class>%s</class>\n", t);
385 t = g_markup_escape_text(c->role, -1);
386 fprintf(f, "\t<role>%s</role>\n", t);
389 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
390 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
391 fprintf(f, "\t<x>%d</x>\n", prex);
392 fprintf(f, "\t<y>%d</y>\n", prey);
393 fprintf(f, "\t<width>%d</width>\n", prew);
394 fprintf(f, "\t<height>%d</height>\n", preh);
396 fprintf(f, "\t<shaded />\n");
398 fprintf(f, "\t<iconic />\n");
400 fprintf(f, "\t<skip_pager />\n");
402 fprintf(f, "\t<skip_taskbar />\n");
404 fprintf(f, "\t<fullscreen />\n");
406 fprintf(f, "\t<above />\n");
408 fprintf(f, "\t<below />\n");
410 fprintf(f, "\t<max_horz />\n");
412 fprintf(f, "\t<max_vert />\n");
413 fprintf(f, "</window>\n\n");
418 fprintf(f, "</openbox_session>\n");
422 g_warning("error while saving the session to %s: %s",
423 save_file, strerror(errno));
431 void session_state_free(ObSessionState *state)
436 g_free(state->class);
443 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
445 return (c->sm_client_id &&
446 !strcmp(s->id, c->sm_client_id) &&
447 !strcmp(s->name, c->name) &&
448 !strcmp(s->class, c->class) &&
449 !strcmp(s->role, c->role));
452 GList* session_state_find(ObClient *c)
456 for (it = session_saved_state; it; it = g_list_next(it)) {
457 ObSessionState *s = it->data;
458 if (!s->matched && session_state_cmp(s, c)) {
466 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
468 return s1->stacking - s2->stacking;
471 static void session_load(gchar *path)
477 if (!parse_load(path, "openbox_session", &doc, &node))
480 if (!parse_attr_string("id", node, &id))
485 node = parse_find_node("window", node->children);
487 ObSessionState *state;
489 state = g_new0(ObSessionState, 1);
491 if (!parse_attr_string("id", node, &state->id))
492 goto session_load_bail;
493 if (!(n = parse_find_node("name", node->children)))
494 goto session_load_bail;
495 state->name = parse_string(doc, n);
496 if (!(n = parse_find_node("class", node->children)))
497 goto session_load_bail;
498 state->class = parse_string(doc, n);
499 if (!(n = parse_find_node("role", node->children)))
500 goto session_load_bail;
501 state->role = parse_string(doc, n);
502 if (!(n = parse_find_node("stacking", node->children)))
503 goto session_load_bail;
504 state->stacking = parse_int(doc, n);
505 if (!(n = parse_find_node("desktop", node->children)))
506 goto session_load_bail;
507 state->desktop = parse_int(doc, n);
508 if (!(n = parse_find_node("x", node->children)))
509 goto session_load_bail;
510 state->x = parse_int(doc, n);
511 if (!(n = parse_find_node("y", node->children)))
512 goto session_load_bail;
513 state->y = parse_int(doc, n);
514 if (!(n = parse_find_node("width", node->children)))
515 goto session_load_bail;
516 state->w = parse_int(doc, n);
517 if (!(n = parse_find_node("height", node->children)))
518 goto session_load_bail;
519 state->h = parse_int(doc, n);
522 parse_find_node("shaded", node->children) != NULL;
524 parse_find_node("iconic", node->children) != NULL;
526 parse_find_node("skip_pager", node->children) != NULL;
527 state->skip_taskbar =
528 parse_find_node("skip_taskbar", node->children) != NULL;
530 parse_find_node("fullscreen", node->children) != NULL;
532 parse_find_node("above", node->children) != NULL;
534 parse_find_node("below", node->children) != NULL;
536 parse_find_node("max_horz", node->children) != NULL;
538 parse_find_node("max_vert", node->children) != NULL;
541 session_saved_state = g_list_prepend(session_saved_state, state);
542 goto session_load_ok;
545 session_state_free(state);
549 node = parse_find_node("window", node->next);
552 /* sort them by their stacking order */
553 session_saved_state = g_list_sort(session_saved_state,
554 (GCompareFunc)stack_sort);