1 /* This session code is largely inspired by metacity code. */
8 GList *session_saved_state;
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) {}
24 #include "parser/parse.h"
31 # include <sys/types.h>
35 #include <X11/SM/SMlib.h>
37 GList *session_saved_state;
39 static SmcConn sm_conn;
40 static gchar *save_file;
42 static gchar **sm_argv;
44 static gboolean session_save();
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);
52 static void save_commands()
55 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
56 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
58 gboolean has_id = FALSE, has_file = FALSE;
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)
63 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
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 */
76 prop_cmd.vals[j].value = sm_argv[i];
77 prop_cmd.vals[j].length = strlen(sm_argv[i]);
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 */
90 prop_res.vals[j].value = sm_argv[i];
91 prop_res.vals[j].length = strlen(sm_argv[i]);
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);
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);
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 void session_startup(int argc, char **argv)
117 #define SM_ERR_LEN 1024
120 char sm_err[SM_ERR_LEN];
125 cb.save_yourself.callback = sm_save_yourself;
126 cb.save_yourself.client_data = NULL;
128 cb.die.callback = sm_die;
129 cb.die.client_data = NULL;
131 cb.save_complete.callback = sm_save_complete;
132 cb.save_complete.client_data = NULL;
134 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
135 cb.shutdown_cancelled.client_data = NULL;
137 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
138 SmcSaveYourselfProcMask |
140 SmcSaveCompleteProcMask |
141 SmcShutdownCancelledProcMask,
142 &cb, ob_sm_id, &ob_sm_id,
145 g_warning("Failed to connect to session manager: %s", sm_err);
147 SmPropValue val_prog;
149 SmPropValue val_hint;
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, };
161 val_prog.value = argv[0];
162 val_prog.length = strlen(argv[0]);
164 val_uid.value = g_strdup(g_get_user_name());
165 val_uid.length = strlen(val_uid.value);
167 hint = SmRestartImmediately;
168 val_hint.value = &hint;
171 sprintf(pid, "%ld", (long)getpid());
173 val_pid.length = strlen(pid);
175 /* priority with gnome-session-manager, low to run before other apps */
177 val_pri.value = &pri;
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;
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;
192 SmcSetProperties(sm_conn, 5, props);
194 g_free(val_uid.value);
200 void session_shutdown()
205 SmPropValue val_hint;
206 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
210 /* when we exit, we want to reset this to a more friendly state */
211 hint = SmRestartIfRunning;
212 val_hint.value = &hint;
215 prop_hint.vals = &val_hint;
217 props[0] = &prop_hint;
219 SmcSetProperties(sm_conn, 1, props);
221 SmcCloseConnection(sm_conn, 0, NULL);
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);
231 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
235 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
237 success = session_save();
240 SmcSaveYourselfDone(conn, success);
243 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
244 Bool shutdown, int interact_style, Bool fast)
246 ob_debug("got SAVE YOURSELF from session manager\n");
248 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
249 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
250 SmcSaveYourselfDone(conn, FALSE);
254 static void sm_die(SmcConn conn, SmPointer data)
257 ob_debug("got DIE from session manager\n");
260 static void sm_save_complete(SmcConn conn, SmPointer data)
262 ob_debug("got SAVE COMPLETE from session manager\n");
265 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
267 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
270 static gboolean session_save()
275 gboolean success = TRUE;
277 /* this algo is from metacity */
278 filename = g_strdup_printf("%d-%d-%u.obs",
282 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
286 f = fopen(save_file, "w");
289 g_warning("unable to save the session to %s: %s",
290 save_file, strerror(errno));
294 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
295 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
297 for (it = stacking_list; it; it = g_list_next(it)) {
300 gint prex, prey, prew, preh;
302 gchar *client_id, *t;
304 if (WINDOW_IS_CLIENT(it->data))
305 c = WINDOW_AS_CLIENT(it->data);
309 if (!client_normal(c))
312 if (!(client_id = client_get_sm_client_id(c)))
317 prew = c->area.width;
318 preh = c->area.height;
319 if (PROP_GETA32(c->window, openbox_premax, cardinal,
320 (guint32**)&dimensions, &num)) {
322 prex = dimensions[0];
323 prey = dimensions[1];
324 prew = dimensions[2];
325 preh = dimensions[3];
330 fprintf(f, "<window id=\"%s\">\n", client_id);
332 t = g_markup_escape_text(c->name, -1);
333 fprintf(f, "\t<name>%s</name>\n", t);
336 t = g_markup_escape_text(c->class, -1);
337 fprintf(f, "\t<class>%s</class>\n", t);
340 t = g_markup_escape_text(c->role, -1);
341 fprintf(f, "\t<role>%s</role>\n", t);
344 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
345 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
346 fprintf(f, "\t<x>%d</x>\n", prex);
347 fprintf(f, "\t<y>%d</y>\n", prey);
348 fprintf(f, "\t<width>%d</width>\n", prew);
349 fprintf(f, "\t<height>%d</height>\n", preh);
351 fprintf(f, "\t<shaded />\n");
353 fprintf(f, "\t<iconic />\n");
355 fprintf(f, "\t<skip_pager />\n");
357 fprintf(f, "\t<skip_taskbar />\n");
359 fprintf(f, "\t<fullscreen />\n");
361 fprintf(f, "\t<above />\n");
363 fprintf(f, "\t<below />\n");
365 fprintf(f, "\t<max_horz />\n");
367 fprintf(f, "\t<max_vert />\n");
368 fprintf(f, "</window>\n\n");
375 fprintf(f, "</openbox_session>\n");
379 g_warning("error while saving the session to %s: %s",
380 save_file, strerror(errno));
388 void session_state_free(ObSessionState *state)
393 g_free(state->class);
400 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
404 if (!(client_id = client_get_sm_client_id(c)))
406 if (strcmp(s->id, client_id)) {
411 if (strcmp(s->name, c->name))
413 if (strcmp(s->class, c->class))
415 if (strcmp(s->role, c->role))
420 GList* session_state_find(ObClient *c)
424 for (it = session_saved_state; it; it = g_list_next(it)) {
425 ObSessionState *s = it->data;
426 if (!s->matched && session_state_cmp(s, c)) {
434 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
436 return s1->stacking - s2->stacking;
439 void session_load(char *path)
445 if (!parse_load(path, "openbox_session", &doc, &node))
448 if (!parse_attr_string("id", node, &sm_id))
450 ob_sm_id = g_strdup(sm_id);
452 node = parse_find_node("window", node->xmlChildrenNode);
454 ObSessionState *state;
456 state = g_new0(ObSessionState, 1);
458 if (!parse_attr_string("id", node, &state->id))
459 goto session_load_bail;
460 if (!(n = parse_find_node("name", node->xmlChildrenNode)))
461 goto session_load_bail;
462 state->name = parse_string(doc, n);
463 if (!(n = parse_find_node("class", node->xmlChildrenNode)))
464 goto session_load_bail;
465 state->class = parse_string(doc, n);
466 if (!(n = parse_find_node("role", node->xmlChildrenNode)))
467 goto session_load_bail;
468 state->role = parse_string(doc, n);
469 if (!(n = parse_find_node("stacking", node->xmlChildrenNode)))
470 goto session_load_bail;
471 state->stacking = parse_int(doc, n);
472 if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
473 goto session_load_bail;
474 state->desktop = parse_int(doc, n);
475 if (!(n = parse_find_node("x", node->xmlChildrenNode)))
476 goto session_load_bail;
477 state->x = parse_int(doc, n);
478 if (!(n = parse_find_node("y", node->xmlChildrenNode)))
479 goto session_load_bail;
480 state->y = parse_int(doc, n);
481 if (!(n = parse_find_node("width", node->xmlChildrenNode)))
482 goto session_load_bail;
483 state->w = parse_int(doc, n);
484 if (!(n = parse_find_node("height", node->xmlChildrenNode)))
485 goto session_load_bail;
486 state->h = parse_int(doc, n);
489 parse_find_node("shaded", node->xmlChildrenNode) != NULL;
491 parse_find_node("iconic", node->xmlChildrenNode) != NULL;
493 parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
494 state->skip_taskbar =
495 parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
497 parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
499 parse_find_node("above", node->xmlChildrenNode) != NULL;
501 parse_find_node("below", node->xmlChildrenNode) != NULL;
503 parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
505 parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
508 session_saved_state = g_list_prepend(session_saved_state, state);
509 goto session_load_ok;
512 session_state_free(state);
516 node = parse_find_node("window", node->next);
519 /* sort them by their stacking order */
520 session_saved_state = g_list_sort(session_saved_state,
521 (GCompareFunc)stack_sort);