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 ObSessionState* session_state_find(ObClient *c) { return NULL; }
14 gboolean session_state_cmp(ObSessionState *s, struct _ObClient *c)
16 void session_state_free(ObSessionState *state) {}
25 #include "parser/parse.h"
32 # include <sys/types.h>
36 #include <X11/SM/SMlib.h>
38 GList *session_saved_state;
40 static SmcConn sm_conn;
41 static gchar *save_file;
43 static gchar **sm_argv;
45 static gboolean session_save();
47 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
48 Bool shutdown, int interact_style, Bool fast);
49 static void sm_die(SmcConn conn, SmPointer data);
50 static void sm_save_complete(SmcConn conn, SmPointer data);
51 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
53 static void save_commands()
56 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
57 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
59 gboolean has_id = FALSE, has_file = FALSE;
61 for (i = 1; !has_id && !has_file && i < sm_argc - 1; ++i) {
62 if (!has_id && strcmp(sm_argv[i], "--sm-client-id") == 0)
64 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
68 n = (has_file ? sm_argc-2 : sm_argc);
69 n = (has_id ? n-2 : n);
70 prop_cmd.vals = g_new(SmPropValue, n);
71 prop_cmd.num_vals = n;
72 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
73 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
74 strcmp (sm_argv[i], "--sm-save-file") == 0) {
75 ++i, --j; /* skip the next as well, keep j where it is */
77 prop_cmd.vals[j].value = sm_argv[i];
78 prop_cmd.vals[j].length = strlen(sm_argv[i]);
82 n = (has_file ? sm_argc : sm_argc+2);
83 n = (has_id ? n-2 : n);
84 prop_res.vals = g_new(SmPropValue, n);
85 prop_res.num_vals = n;
86 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
87 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
88 strcmp (sm_argv[i], "--sm-save-file") == 0) {
89 ++i, --j; /* skip the next as well, keep j where it is */
91 prop_res.vals[j].value = sm_argv[i];
92 prop_res.vals[j].length = strlen(sm_argv[i]);
97 prop_res.vals[j].value = "--sm-save-file";
98 prop_res.vals[j++].length = strlen("--sm-save-file");
99 prop_res.vals[j].value = save_file;
100 prop_res.vals[j++].length = strlen(save_file);
102 prop_res.vals[j].value = "--sm-client-id";
103 prop_res.vals[j++].length = strlen("--sm-client-id");
104 prop_res.vals[j].value = ob_sm_id;
105 prop_res.vals[j++].length = strlen(ob_sm_id);
108 props[0] = &prop_res;
109 props[1] = &prop_cmd;
110 SmcSetProperties(sm_conn, 2, props);
112 g_free(prop_res.vals);
113 g_free(prop_cmd.vals);
116 void session_startup(int argc, char **argv)
118 #define SM_ERR_LEN 1024
121 char sm_err[SM_ERR_LEN];
126 cb.save_yourself.callback = sm_save_yourself;
127 cb.save_yourself.client_data = NULL;
129 cb.die.callback = sm_die;
130 cb.die.client_data = NULL;
132 cb.save_complete.callback = sm_save_complete;
133 cb.save_complete.client_data = NULL;
135 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
136 cb.shutdown_cancelled.client_data = NULL;
138 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
139 SmcSaveYourselfProcMask |
141 SmcSaveCompleteProcMask |
142 SmcShutdownCancelledProcMask,
143 &cb, ob_sm_id, &ob_sm_id,
146 g_warning("Failed to connect to session manager: %s", sm_err);
148 SmPropValue val_prog;
150 SmPropValue val_hint;
153 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
154 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
155 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
156 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
157 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
162 val_prog.value = argv[0];
163 val_prog.length = strlen(argv[0]);
165 val_uid.value = g_strdup(g_get_user_name());
166 val_uid.length = strlen(val_uid.value);
168 hint = SmRestartImmediately;
169 val_hint.value = &hint;
172 sprintf(pid, "%ld", (long)getpid());
174 val_pid.length = strlen(pid);
176 /* priority with gnome-session-manager, low to run before other apps */
178 val_pri.value = &pri;
181 prop_prog.vals = &val_prog;
182 prop_uid.vals = &val_uid;
183 prop_hint.vals = &val_hint;
184 prop_pid.vals = &val_pid;
185 prop_pri.vals = &val_pri;
187 props[0] = &prop_prog;
188 props[1] = &prop_uid;
189 props[2] = &prop_hint;
190 props[3] = &prop_pid;
191 props[4] = &prop_pri;
193 SmcSetProperties(sm_conn, 5, props);
195 g_free(val_uid.value);
199 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
203 void session_shutdown()
208 SmPropValue val_hint;
209 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
213 /* when we exit, we want to reset this to a more friendly state */
214 hint = SmRestartIfRunning;
215 val_hint.value = &hint;
218 prop_hint.vals = &val_hint;
220 props[0] = &prop_hint;
222 SmcSetProperties(sm_conn, 1, props);
224 SmcCloseConnection(sm_conn, 0, NULL);
226 while (session_saved_state) {
227 session_state_free(session_saved_state->data);
228 session_saved_state = g_list_delete_link(session_saved_state,
229 session_saved_state);
234 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
238 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
240 success = session_save();
243 SmcSaveYourselfDone(conn, success);
246 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
247 Bool shutdown, int interact_style, Bool fast)
249 ob_debug("got SAVE YOURSELF from session manager\n");
251 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
252 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
253 SmcSaveYourselfDone(conn, FALSE);
257 static void sm_die(SmcConn conn, SmPointer data)
260 ob_debug("got DIE from session manager\n");
263 static void sm_save_complete(SmcConn conn, SmPointer data)
265 ob_debug("got SAVE COMPLETE from session manager\n");
268 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
270 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
273 static gboolean session_save()
278 gboolean success = TRUE;
280 /* this algo is from metacity */
281 filename = g_strdup_printf("%d-%d-%u.obs",
285 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
289 f = fopen(save_file, "w");
292 g_warning("unable to save the session to %s: %s",
293 save_file, strerror(errno));
297 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
298 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
300 for (it = stacking_list; it; it = g_list_next(it)) {
303 gint prex, prey, prew, preh;
305 gchar *client_id, *t;
307 if (WINDOW_IS_CLIENT(it->data))
308 c = WINDOW_AS_CLIENT(it->data);
312 if (!client_normal(c))
315 if (!(client_id = client_get_sm_client_id(c)))
320 prew = c->area.width;
321 preh = c->area.height;
322 if (PROP_GETA32(c->window, openbox_premax, cardinal,
323 (guint32**)&dimensions, &num)) {
325 prex = dimensions[0];
326 prey = dimensions[1];
327 prew = dimensions[2];
328 preh = dimensions[3];
333 fprintf(f, "<window id=\"%s\">\n", client_id);
335 t = g_markup_escape_text(c->name, -1);
336 fprintf(f, "\t<name>%s</name>\n", t);
339 t = g_markup_escape_text(c->class, -1);
340 fprintf(f, "\t<class>%s</class>\n", t);
343 t = g_markup_escape_text(c->role, -1);
344 fprintf(f, "\t<role>%s</role>\n", t);
347 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
348 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
349 fprintf(f, "\t<x>%d</x>\n", prex);
350 fprintf(f, "\t<y>%d</y>\n", prey);
351 fprintf(f, "\t<width>%d</width>\n", prew);
352 fprintf(f, "\t<height>%d</height>\n", preh);
354 fprintf(f, "\t<shaded />\n");
356 fprintf(f, "\t<iconic />\n");
358 fprintf(f, "\t<skip_pager />\n");
360 fprintf(f, "\t<skip_taskbar />\n");
362 fprintf(f, "\t<fullscreen />\n");
364 fprintf(f, "\t<above />\n");
366 fprintf(f, "\t<below />\n");
368 fprintf(f, "\t<max_horz />\n");
370 fprintf(f, "\t<max_vert />\n");
371 fprintf(f, "</window>\n\n");
378 fprintf(f, "</openbox_session>\n");
382 g_warning("error while saving the session to %s: %s",
383 save_file, strerror(errno));
391 void session_state_free(ObSessionState *state)
396 g_free(state->class);
403 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
407 if (!(client_id = client_get_sm_client_id(c)))
409 if (strcmp(s->id, client_id)) {
414 if (strcmp(s->name, c->name))
416 if (strcmp(s->class, c->class))
418 if (strcmp(s->role, c->role))
423 GList* session_state_find(ObClient *c)
427 for (it = session_saved_state; it; it = g_list_next(it)) {
428 ObSessionState *s = it->data;
429 if (!s->matched && session_state_cmp(s, c)) {
437 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
439 return s1->stacking - s2->stacking;
442 void session_load(char *path)
448 if (!parse_load(path, "openbox_session", &doc, &node))
451 if (!parse_attr_string("id", node, &sm_id))
453 ob_sm_id = g_strdup(sm_id);
455 node = parse_find_node("window", node->xmlChildrenNode);
457 ObSessionState *state;
459 state = g_new0(ObSessionState, 1);
461 if (!parse_attr_string("id", node, &state->id))
462 goto session_load_bail;
463 if (!(n = parse_find_node("name", node->xmlChildrenNode)))
464 goto session_load_bail;
465 state->name = parse_string(doc, n);
466 if (!(n = parse_find_node("class", node->xmlChildrenNode)))
467 goto session_load_bail;
468 state->class = parse_string(doc, n);
469 if (!(n = parse_find_node("role", node->xmlChildrenNode)))
470 goto session_load_bail;
471 state->role = parse_string(doc, n);
472 if (!(n = parse_find_node("stacking", node->xmlChildrenNode)))
473 goto session_load_bail;
474 state->stacking = parse_int(doc, n);
475 if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
476 goto session_load_bail;
477 state->desktop = parse_int(doc, n);
478 if (!(n = parse_find_node("x", node->xmlChildrenNode)))
479 goto session_load_bail;
480 state->x = parse_int(doc, n);
481 if (!(n = parse_find_node("y", node->xmlChildrenNode)))
482 goto session_load_bail;
483 state->y = parse_int(doc, n);
484 if (!(n = parse_find_node("width", node->xmlChildrenNode)))
485 goto session_load_bail;
486 state->w = parse_int(doc, n);
487 if (!(n = parse_find_node("height", node->xmlChildrenNode)))
488 goto session_load_bail;
489 state->h = parse_int(doc, n);
492 parse_find_node("shaded", node->xmlChildrenNode) != NULL;
494 parse_find_node("iconic", node->xmlChildrenNode) != NULL;
496 parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
497 state->skip_taskbar =
498 parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
500 parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
502 parse_find_node("above", node->xmlChildrenNode) != NULL;
504 parse_find_node("below", node->xmlChildrenNode) != NULL;
506 parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
508 parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
511 session_saved_state = g_list_prepend(session_saved_state, state);
512 goto session_load_ok;
515 session_state_free(state);
519 node = parse_find_node("window", node->next);
522 /* sort them by their stacking order */
523 session_saved_state = g_list_sort(session_saved_state,
524 (GCompareFunc)stack_sort);