1 /* This session code is largely inspired by metacity code. */
8 GList *session_saved_state;
10 void session_startup(int *argc, char ***argv) {}
11 void session_shutdown() {}
12 GList* session_state_find(ObClient *c) { return NULL; }
13 gboolean session_state_cmp(ObSessionState *s, ObClient *c) { return FALSE; }
14 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 gboolean sm_disable;
40 static SmcConn sm_conn;
41 static gchar *save_file;
44 static gchar **sm_argv;
46 static void session_load(char *path);
47 static gboolean session_save();
49 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
50 Bool shutdown, int interact_style, Bool fast);
51 static void sm_die(SmcConn conn, SmPointer data);
52 static void sm_save_complete(SmcConn conn, SmPointer data);
53 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
55 static void save_commands()
58 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
59 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
62 prop_cmd.vals = g_new(SmPropValue, sm_argc);
63 prop_cmd.num_vals = sm_argc;
64 for (i = 0; i < sm_argc; ++i) {
65 prop_cmd.vals[i].value = sm_argv[i];
66 prop_cmd.vals[i].length = strlen(sm_argv[i]);
69 prop_res.vals = g_new(SmPropValue, sm_argc + 2);
70 prop_res.num_vals = sm_argc + 2;
71 for (i = 0; i < sm_argc; ++i) {
72 prop_res.vals[i].value = sm_argv[i];
73 prop_res.vals[i].length = strlen(sm_argv[i]);
77 prop_res.vals[i].value = "--sm-save-file";
78 prop_res.vals[i++].length = strlen("--sm-save-file");
79 prop_res.vals[i].value = save_file;
80 prop_res.vals[i++].length = strlen(save_file);
82 prop_res.vals[i].value = "--sm-client-id";
83 prop_res.vals[i++].length = strlen("--sm-client-id");
84 prop_res.vals[i].value = sm_id;
85 prop_res.vals[i++].length = strlen(sm_id);
90 SmcSetProperties(sm_conn, 2, props);
92 g_free(prop_res.vals);
93 g_free(prop_cmd.vals);
96 static void remove_two_args(int *argc, char ***argv, int index)
100 for (i = index; i < index + 2; ++i)
101 (*argv)[i] = (*argv)[i+2];
105 static void parse_args(int *argc, char ***argv)
109 for (i = 1; i < *argc; ++i) {
110 if (!strcmp((*argv)[i], "--sm-client-id")) {
111 if (i == *argc - 1) /* no args left */
112 g_printerr(_("--sm-client-id requires an argument\n"));
114 sm_id = g_strdup((*argv)[i+1]);
115 remove_two_args(argc, argv, i);
118 } else if (!strcmp((*argv)[i], "--sm-save-file")) {
119 if (i == *argc - 1) /* no args left */
120 g_printerr(_("--sm-save-file requires an argument\n"));
122 save_file = g_strdup((*argv)[i+1]);
123 remove_two_args(argc, argv, i);
126 } else if (!strcmp((*argv)[i], "--sm-disable")) {
132 void session_startup(int *argc, char ***argv)
134 #define SM_ERR_LEN 1024
137 char sm_err[SM_ERR_LEN];
139 parse_args(argc, argv);
145 session_load(save_file);
150 cb.save_yourself.callback = sm_save_yourself;
151 cb.save_yourself.client_data = NULL;
153 cb.die.callback = sm_die;
154 cb.die.client_data = NULL;
156 cb.save_complete.callback = sm_save_complete;
157 cb.save_complete.client_data = NULL;
159 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
160 cb.shutdown_cancelled.client_data = NULL;
162 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
163 SmcSaveYourselfProcMask |
165 SmcSaveCompleteProcMask |
166 SmcShutdownCancelledProcMask,
170 g_warning("Failed to connect to session manager: %s", sm_err);
172 SmPropValue val_prog;
174 SmPropValue val_hint;
177 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
178 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
179 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
180 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
181 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
186 val_prog.value = sm_argv[0];
187 val_prog.length = strlen(sm_argv[0]);
189 val_uid.value = g_strdup(g_get_user_name());
190 val_uid.length = strlen(val_uid.value);
192 hint = SmRestartImmediately;
193 val_hint.value = &hint;
196 sprintf(pid, "%ld", (long)getpid());
198 val_pid.length = strlen(pid);
200 /* priority with gnome-session-manager, low to run before other apps */
202 val_pri.value = &pri;
205 prop_prog.vals = &val_prog;
206 prop_uid.vals = &val_uid;
207 prop_hint.vals = &val_hint;
208 prop_pid.vals = &val_pid;
209 prop_pri.vals = &val_pri;
211 props[0] = &prop_prog;
212 props[1] = &prop_uid;
213 props[2] = &prop_hint;
214 props[3] = &prop_pid;
215 props[4] = &prop_pri;
217 SmcSetProperties(sm_conn, 5, props);
219 g_free(val_uid.value);
225 void session_shutdown()
231 SmPropValue val_hint;
232 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
236 /* when we exit, we want to reset this to a more friendly state */
237 hint = SmRestartIfRunning;
238 val_hint.value = &hint;
241 prop_hint.vals = &val_hint;
243 props[0] = &prop_hint;
245 SmcSetProperties(sm_conn, 1, props);
247 SmcCloseConnection(sm_conn, 0, NULL);
249 while (session_saved_state) {
250 session_state_free(session_saved_state->data);
251 session_saved_state = g_list_delete_link(session_saved_state,
252 session_saved_state);
257 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
261 success = session_save();
264 SmcSaveYourselfDone(conn, success);
267 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
268 Bool shutdown, int interact_style, Bool fast)
270 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
271 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
272 SmcSaveYourselfDone(conn, FALSE);
276 static void sm_die(SmcConn conn, SmPointer data)
281 static void sm_save_complete(SmcConn conn, SmPointer data)
285 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
289 static gboolean session_save()
294 gboolean success = TRUE;
296 /* this algo is from metacity */
297 filename = g_strdup_printf("%d-%d-%u.obs",
301 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
305 f = fopen(save_file, "w");
308 g_warning("unable to save the session to %s: %s",
309 save_file, strerror(errno));
313 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
314 fprintf(f, "<openbox_session id=\"%s\">\n\n", sm_id);
316 for (it = stacking_list; it; it = g_list_next(it)) {
319 gint prex, prey, prew, preh;
323 if (WINDOW_IS_CLIENT(it->data))
324 c = WINDOW_AS_CLIENT(it->data);
328 if (!client_normal(c))
331 if (!c->sm_client_id)
336 prew = c->area.width;
337 preh = c->area.height;
338 if (PROP_GETA32(c->window, openbox_premax, cardinal,
339 (guint32**)&dimensions, &num)) {
341 prex = dimensions[0];
342 prey = dimensions[1];
343 prew = dimensions[2];
344 preh = dimensions[3];
349 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
351 t = g_markup_escape_text(c->name, -1);
352 fprintf(f, "\t<name>%s</name>\n", t);
355 t = g_markup_escape_text(c->class, -1);
356 fprintf(f, "\t<class>%s</class>\n", t);
359 t = g_markup_escape_text(c->role, -1);
360 fprintf(f, "\t<role>%s</role>\n", t);
363 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
364 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
365 fprintf(f, "\t<x>%d</x>\n", prex);
366 fprintf(f, "\t<y>%d</y>\n", prey);
367 fprintf(f, "\t<width>%d</width>\n", prew);
368 fprintf(f, "\t<height>%d</height>\n", preh);
370 fprintf(f, "\t<shaded />\n");
372 fprintf(f, "\t<iconic />\n");
374 fprintf(f, "\t<skip_pager />\n");
376 fprintf(f, "\t<skip_taskbar />\n");
378 fprintf(f, "\t<fullscreen />\n");
380 fprintf(f, "\t<above />\n");
382 fprintf(f, "\t<below />\n");
384 fprintf(f, "\t<max_horz />\n");
386 fprintf(f, "\t<max_vert />\n");
387 fprintf(f, "</window>\n\n");
392 fprintf(f, "</openbox_session>\n");
396 g_warning("error while saving the session to %s: %s",
397 save_file, strerror(errno));
405 void session_state_free(ObSessionState *state)
410 g_free(state->class);
417 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
419 return (c->sm_client_id &&
420 !strcmp(s->id, c->sm_client_id) &&
421 !strcmp(s->name, c->name) &&
422 !strcmp(s->class, c->class) &&
423 !strcmp(s->role, c->role));
426 GList* session_state_find(ObClient *c)
430 for (it = session_saved_state; it; it = g_list_next(it)) {
431 ObSessionState *s = it->data;
432 if (!s->matched && session_state_cmp(s, c)) {
440 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
442 return s1->stacking - s2->stacking;
445 static void session_load(char *path)
451 if (!parse_load(path, "openbox_session", &doc, &node))
454 if (!parse_attr_string("id", node, &id))
459 node = parse_find_node("window", node->children);
461 ObSessionState *state;
463 state = g_new0(ObSessionState, 1);
465 if (!parse_attr_string("id", node, &state->id))
466 goto session_load_bail;
467 if (!(n = parse_find_node("name", node->children)))
468 goto session_load_bail;
469 state->name = parse_string(doc, n);
470 if (!(n = parse_find_node("class", node->children)))
471 goto session_load_bail;
472 state->class = parse_string(doc, n);
473 if (!(n = parse_find_node("role", node->children)))
474 goto session_load_bail;
475 state->role = parse_string(doc, n);
476 if (!(n = parse_find_node("stacking", node->children)))
477 goto session_load_bail;
478 state->stacking = parse_int(doc, n);
479 if (!(n = parse_find_node("desktop", node->children)))
480 goto session_load_bail;
481 state->desktop = parse_int(doc, n);
482 if (!(n = parse_find_node("x", node->children)))
483 goto session_load_bail;
484 state->x = parse_int(doc, n);
485 if (!(n = parse_find_node("y", node->children)))
486 goto session_load_bail;
487 state->y = parse_int(doc, n);
488 if (!(n = parse_find_node("width", node->children)))
489 goto session_load_bail;
490 state->w = parse_int(doc, n);
491 if (!(n = parse_find_node("height", node->children)))
492 goto session_load_bail;
493 state->h = parse_int(doc, n);
496 parse_find_node("shaded", node->children) != NULL;
498 parse_find_node("iconic", node->children) != NULL;
500 parse_find_node("skip_pager", node->children) != NULL;
501 state->skip_taskbar =
502 parse_find_node("skip_taskbar", node->children) != NULL;
504 parse_find_node("fullscreen", node->children) != NULL;
506 parse_find_node("above", node->children) != NULL;
508 parse_find_node("below", node->children) != NULL;
510 parse_find_node("max_horz", node->children) != NULL;
512 parse_find_node("max_vert", node->children) != NULL;
515 session_saved_state = g_list_prepend(session_saved_state, state);
516 goto session_load_ok;
519 session_state_free(state);
523 node = parse_find_node("window", node->next);
526 /* sort them by their stacking order */
527 session_saved_state = g_list_sort(session_saved_state,
528 (GCompareFunc)stack_sort);