1 /* This session code is largely inspired by metacity code. */
5 void session_load(char *path) {}
6 void session_startup(int argc, char **argv) {}
7 void session_shutdown() {}
8 ObSessionState* session_state_find(struct _ObClient *c) { return NULL; }
9 void session_state_free(ObSessionState *state) {}
18 #include "parser/parse.h"
25 # include <sys/types.h>
29 #include <X11/SM/SMlib.h>
31 static SmcConn sm_conn;
32 static gchar *save_file;
34 static gchar **sm_argv;
35 static GSList *sm_saved_state;
37 static gboolean session_save();
39 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
40 Bool shutdown, int interact_style, Bool fast);
41 static void sm_die(SmcConn conn, SmPointer data);
42 static void sm_save_complete(SmcConn conn, SmPointer data);
43 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
45 static void save_commands()
48 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
49 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
51 gboolean has_id = FALSE, has_file = FALSE;
53 for (i = 1; !has_id && !has_file && i < sm_argc - 1; ++i) {
54 if (!has_id && strcmp(sm_argv[i], "--sm-client-id") == 0)
56 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
60 n = (has_file ? sm_argc-2 : sm_argc);
61 n = (has_id ? n-2 : n);
62 prop_cmd.vals = g_new(SmPropValue, n);
63 prop_cmd.num_vals = n;
64 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
65 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
66 strcmp (sm_argv[i], "--sm-save-file") == 0) {
67 ++i, --j; /* skip the next as well, keep j where it is */
69 prop_cmd.vals[j].value = sm_argv[i];
70 prop_cmd.vals[j].length = strlen(sm_argv[i]);
74 n = (has_file ? sm_argc : sm_argc+2);
75 n = (has_id ? n-2 : n);
76 prop_res.vals = g_new(SmPropValue, n);
77 prop_res.num_vals = n;
78 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
79 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
80 strcmp (sm_argv[i], "--sm-save-file") == 0) {
81 ++i, --j; /* skip the next as well, keep j where it is */
83 prop_res.vals[j].value = sm_argv[i];
84 prop_res.vals[j].length = strlen(sm_argv[i]);
89 prop_res.vals[j].value = "--sm-save-file";
90 prop_res.vals[j++].length = strlen("--sm-save-file");
91 prop_res.vals[j].value = save_file;
92 prop_res.vals[j++].length = strlen(save_file);
94 prop_res.vals[j].value = "--sm-client-id";
95 prop_res.vals[j++].length = strlen("--sm-client-id");
96 prop_res.vals[j].value = ob_sm_id;
97 prop_res.vals[j++].length = strlen(ob_sm_id);
100 props[0] = &prop_res;
101 props[1] = &prop_cmd;
102 SmcSetProperties(sm_conn, 2, props);
104 g_free(prop_res.vals);
105 g_free(prop_cmd.vals);
108 void session_startup(int argc, char **argv)
110 #define SM_ERR_LEN 1024
113 char sm_err[SM_ERR_LEN];
118 cb.save_yourself.callback = sm_save_yourself;
119 cb.save_yourself.client_data = NULL;
121 cb.die.callback = sm_die;
122 cb.die.client_data = NULL;
124 cb.save_complete.callback = sm_save_complete;
125 cb.save_complete.client_data = NULL;
127 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
128 cb.shutdown_cancelled.client_data = NULL;
130 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
131 SmcSaveYourselfProcMask |
133 SmcSaveCompleteProcMask |
134 SmcShutdownCancelledProcMask,
135 &cb, ob_sm_id, &ob_sm_id,
138 g_warning("Failed to connect to session manager: %s", sm_err);
140 SmPropValue val_prog;
142 SmPropValue val_hint;
145 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
146 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
147 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
148 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
149 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
154 val_prog.value = argv[0];
155 val_prog.length = strlen(argv[0]);
157 val_uid.value = g_strdup(g_get_user_name());
158 val_uid.length = strlen(val_uid.value);
160 hint = SmRestartImmediately;
161 val_hint.value = &hint;
164 sprintf(pid, "%ld", (long)getpid());
166 val_pid.length = strlen(pid);
168 /* priority with gnome-session-manager, low to run before other apps */
170 val_pri.value = &pri;
173 prop_prog.vals = &val_prog;
174 prop_uid.vals = &val_uid;
175 prop_hint.vals = &val_hint;
176 prop_pid.vals = &val_pid;
177 prop_pri.vals = &val_pri;
179 props[0] = &prop_prog;
180 props[1] = &prop_uid;
181 props[2] = &prop_hint;
182 props[3] = &prop_pid;
183 props[4] = &prop_pri;
185 SmcSetProperties(sm_conn, 5, props);
187 g_free(val_uid.value);
191 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
195 void session_shutdown()
200 SmPropValue val_hint;
201 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
205 /* when we exit, we want to reset this to a more friendly state */
206 hint = SmRestartIfRunning;
207 val_hint.value = &hint;
210 prop_hint.vals = &val_hint;
212 props[0] = &prop_hint;
214 SmcSetProperties(sm_conn, 1, props);
216 SmcCloseConnection(sm_conn, 0, NULL);
220 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
224 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
226 success = session_save();
229 SmcSaveYourselfDone(conn, success);
232 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
233 Bool shutdown, int interact_style, Bool fast)
235 ob_debug("got SAVE YOURSELF from session manager\n");
237 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
238 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
239 SmcSaveYourselfDone(conn, FALSE);
243 static void sm_die(SmcConn conn, SmPointer data)
246 ob_debug("got DIE from session manager\n");
249 static void sm_save_complete(SmcConn conn, SmPointer data)
251 ob_debug("got SAVE COMPLETE from session manager\n");
254 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
256 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
259 static gboolean session_save()
264 gboolean success = TRUE;
266 /* this algo is from metacity */
267 filename = g_strdup_printf("%d-%d-%u.obs",
271 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
275 f = fopen(save_file, "w");
278 g_warning("unable to save the session to %s: %s",
279 save_file, strerror(errno));
281 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
282 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
284 for (it = client_list; it; it = g_list_next(it)) {
287 gint prex, prey, prew, preh;
288 ObClient *c = it->data;
289 gchar *client_id, *t;
291 if (!client_normal(c))
294 if (!(client_id = client_get_sm_client_id(c)))
299 prew = c->area.width;
300 preh = c->area.height;
301 if (PROP_GETA32(c->window, openbox_premax, cardinal,
302 (guint32**)&dimensions, &num)) {
304 prex = dimensions[0];
305 prey = dimensions[1];
306 prew = dimensions[2];
307 preh = dimensions[3];
312 fprintf(f, "<window id=\"%s\">\n", client_id);
314 t = g_markup_escape_text(c->name, -1);
315 fprintf(f, "\t<name>%s</name>\n", t);
318 t = g_markup_escape_text(c->class, -1);
319 fprintf(f, "\t<class>%s</class>\n", t);
322 t = g_markup_escape_text(c->role, -1);
323 fprintf(f, "\t<role>%s</role>\n", t);
326 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
327 fprintf(f, "\t<x>%d</x>\n", prex);
328 fprintf(f, "\t<y>%d</y>\n", prey);
329 fprintf(f, "\t<width>%d</width>\n", prew);
330 fprintf(f, "\t<height>%d</height>\n", preh);
332 fprintf(f, "\t<shaded />\n");
334 fprintf(f, "\t<iconic />\n");
336 fprintf(f, "\t<skip_pager />\n");
338 fprintf(f, "\t<skip_taskbar />\n");
340 fprintf(f, "\t<fullscreen />\n");
342 fprintf(f, "\t<above />\n");
344 fprintf(f, "\t<below />\n");
346 fprintf(f, "\t<max_horz />\n");
348 fprintf(f, "\t<max_vert />\n");
349 fprintf(f, "</window>\n\n");
354 fprintf(f, "</openbox_session>\n");
358 g_warning("error while saving the session to %s: %s",
359 save_file, strerror(errno));
367 void session_state_free(ObSessionState *state)
372 g_free(state->class);
379 static gboolean session_state_cmp(ObSessionState *s, ObClient *c)
383 if (!(client_id = client_get_sm_client_id(c)))
385 g_print("\nsaved %s\nnow %s\n", s->id, client_id);
386 if (strcmp(s->id, client_id)) {
391 g_print("saved %s\nnow %s\n", s->name, c->name);
392 if (strcmp(s->name, c->name))
394 g_print("saved %s\nnow %s\n", s->class, c->class);
395 if (strcmp(s->class, c->class))
397 g_print("saved %s\nnow %s\n\n", s->role, c->role);
398 if (strcmp(s->role, c->role))
403 ObSessionState* session_state_find(ObClient *c)
407 for (it = sm_saved_state; it; it = g_slist_next(it))
408 if (session_state_cmp(it->data, c)) {
409 ObSessionState *s = it->data;
410 sm_saved_state = g_slist_remove(sm_saved_state, s);
416 void session_load(char *path)
422 if (!parse_load(path, "openbox_session", &doc, &node))
425 if (!parse_attr_string("id", node, &sm_id))
427 ob_sm_id = g_strdup(sm_id);
429 node = parse_find_node("window", node->xmlChildrenNode);
431 ObSessionState *state;
433 state = g_new0(ObSessionState, 1);
435 if (!parse_attr_string("id", node, &state->id))
436 goto session_load_bail;
437 if (!(n = parse_find_node("name", node->xmlChildrenNode)))
438 goto session_load_bail;
439 state->name = parse_string(doc, n);
440 if (!(n = parse_find_node("class", node->xmlChildrenNode)))
441 goto session_load_bail;
442 state->class = parse_string(doc, n);
443 if (!(n = parse_find_node("role", node->xmlChildrenNode)))
444 goto session_load_bail;
445 state->role = parse_string(doc, n);
446 if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
447 goto session_load_bail;
448 state->desktop = parse_int(doc, n);
449 if (!(n = parse_find_node("x", node->xmlChildrenNode)))
450 goto session_load_bail;
451 state->x = parse_int(doc, n);
452 if (!(n = parse_find_node("y", node->xmlChildrenNode)))
453 goto session_load_bail;
454 state->y = parse_int(doc, n);
455 if (!(n = parse_find_node("width", node->xmlChildrenNode)))
456 goto session_load_bail;
457 state->w = parse_int(doc, n);
458 if (!(n = parse_find_node("height", node->xmlChildrenNode)))
459 goto session_load_bail;
460 state->h = parse_int(doc, n);
463 parse_find_node("shaded", node->xmlChildrenNode) != NULL;
465 parse_find_node("iconic", node->xmlChildrenNode) != NULL;
467 parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
468 state->skip_taskbar =
469 parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
471 parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
473 parse_find_node("above", node->xmlChildrenNode) != NULL;
475 parse_find_node("below", node->xmlChildrenNode) != NULL;
477 parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
479 parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
482 sm_saved_state = g_slist_prepend(sm_saved_state, state);
483 goto session_load_ok;
486 session_state_free(state);
490 node = parse_find_node("window", node->next);