#ifndef USE_SM
+#include "session.h"
+#include "client.h"
+
+GList *session_saved_state;
+
void session_load(char *path) {}
void session_startup(int argc, char **argv) {}
void session_shutdown() {}
+GList* session_state_find(ObClient *c) { return NULL; }
+gboolean session_state_cmp(ObSessionState *s, ObClient *c) { return FALSE; }
+void session_state_free(ObSessionState *state) {}
#else
#include <X11/SM/SMlib.h>
+GList *session_saved_state;
+
static SmcConn sm_conn;
static gchar *save_file;
static gint sm_argc;
static gchar **sm_argv;
+
+static gboolean session_save();
+
static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
Bool shutdown, int interact_style, Bool fast);
static void sm_die(SmcConn conn, SmPointer data);
}
}
- prop_res.vals[j].value = "--sm-save-file";
- prop_res.vals[j++].length = strlen("--sm-save-file");
- prop_res.vals[j].value = save_file;
- prop_res.vals[j++].length = strlen(save_file);
+ if (save_file) {
+ prop_res.vals[j].value = "--sm-save-file";
+ prop_res.vals[j++].length = strlen("--sm-save-file");
+ prop_res.vals[j].value = save_file;
+ prop_res.vals[j++].length = strlen(save_file);
+ } else {
+ prop_res.vals[j].value = "--sm-client-id";
+ prop_res.vals[j++].length = strlen("--sm-client-id");
+ prop_res.vals[j].value = ob_sm_id;
+ prop_res.vals[j++].length = strlen(ob_sm_id);
+ }
props[0] = &prop_res;
props[1] = &prop_cmd;
- SmcSetProperties(sm_conn, 1, props);
+ SmcSetProperties(sm_conn, 2, props);
g_free(prop_res.vals);
g_free(prop_cmd.vals);
SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
SmProp *props[6];
- gulong hint, pri;
+ gchar hint, pri;
gchar pid[32];
val_prog.value = argv[0];
g_free(val_uid.value);
- ob_debug("Connected to session manager with id %s\n", ob_sm_id);
+ save_commands();
}
}
SmcSetProperties(sm_conn, 1, props);
SmcCloseConnection(sm_conn, 0, NULL);
+
+ while (session_saved_state) {
+ session_state_free(session_saved_state->data);
+ session_saved_state = g_list_delete_link(session_saved_state,
+ session_saved_state);
+ }
}
}
+static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
+{
+ gboolean success;
+
+ ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
+
+ success = session_save();
+ save_commands();
+
+ SmcSaveYourselfDone(conn, success);
+}
+
static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
Bool shutdown, int interact_style, Bool fast)
+{
+ ob_debug("got SAVE YOURSELF from session manager\n");
+
+ if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
+ ob_debug("SAVE YOURSELF PHASE 2 failed\n");
+ SmcSaveYourselfDone(conn, FALSE);
+ }
+}
+
+static void sm_die(SmcConn conn, SmPointer data)
+{
+ ob_exit();
+ ob_debug("got DIE from session manager\n");
+}
+
+static void sm_save_complete(SmcConn conn, SmPointer data)
+{
+ ob_debug("got SAVE COMPLETE from session manager\n");
+}
+
+static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
+{
+ ob_debug("got SHUTDOWN CANCELLED from session manager\n");
+}
+
+static gboolean session_save()
{
gchar *filename;
FILE *f;
GList *it;
gboolean success = TRUE;
- ob_debug("got SAVE YOURSELF from session manager\n");
-
/* this algo is from metacity */
filename = g_strdup_printf("%d-%d-%u.obs",
(int) time(NULL),
g_warning("unable to save the session to %s: %s",
save_file, strerror(errno));
} else {
+ guint stack_pos = 0;
+
fprintf(f, "<?xml version=\"1.0\"?>\n\n");
fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
- for (it = client_list; it; it = g_list_next(it)) {
+ for (it = stacking_list; it; it = g_list_next(it)) {
guint num;
gint32 *dimensions;
gint prex, prey, prew, preh;
- ObClient *c = it->data;
+ ObClient *c;
+ gchar *client_id, *t;
+
+ if (WINDOW_IS_CLIENT(it->data))
+ c = WINDOW_AS_CLIENT(it->data);
+ else
+ continue;
if (!client_normal(c))
continue;
+ if (!(client_id = client_get_sm_client_id(c)))
+ continue;
+
prex = c->area.x;
prey = c->area.y;
prew = c->area.width;
g_free(dimensions);
}
- fprintf(f, "\t<window id=\"%s\">\n",
- g_markup_escape_text("XXX", -1));
- fprintf(f, "\t\t<name>%s</name>\n",
- g_markup_escape_text(c->name, -1));
- fprintf(f, "\t\t<class>%s</class>\n",
- g_markup_escape_text(c->class, -1));
- fprintf(f, "\t\t<role>%s</role>\n",
- g_markup_escape_text(c->role, -1));
- fprintf(f, "\t\t<desktop>%d</desktop>\n", c->desktop);
- fprintf(f, "\t\t<x>%d</x>\n", prex);
- fprintf(f, "\t\t<y>%d</y>\n", prey);
- fprintf(f, "\t\t<width>%d</width>\n", prew);
- fprintf(f, "\t\t<height>%d</height>\n", preh);
+ fprintf(f, "<window id=\"%s\">\n", client_id);
+
+ t = g_markup_escape_text(c->name, -1);
+ fprintf(f, "\t<name>%s</name>\n", t);
+ g_free(t);
+
+ t = g_markup_escape_text(c->class, -1);
+ fprintf(f, "\t<class>%s</class>\n", t);
+ g_free(t);
+
+ t = g_markup_escape_text(c->role, -1);
+ fprintf(f, "\t<role>%s</role>\n", t);
+ g_free(t);
+
+ fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
+ fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
+ fprintf(f, "\t<x>%d</x>\n", prex);
+ fprintf(f, "\t<y>%d</y>\n", prey);
+ fprintf(f, "\t<width>%d</width>\n", prew);
+ fprintf(f, "\t<height>%d</height>\n", preh);
if (c->shaded)
- fprintf(f, "\t\t<shaded />\n");
+ fprintf(f, "\t<shaded />\n");
if (c->iconic)
- fprintf(f, "\t\t<iconic />\n");
+ fprintf(f, "\t<iconic />\n");
if (c->skip_pager)
- fprintf(f, "\t\t<skip_pager />\n");
+ fprintf(f, "\t<skip_pager />\n");
if (c->skip_taskbar)
- fprintf(f, "\t\t<skip_taskbar />\n");
+ fprintf(f, "\t<skip_taskbar />\n");
if (c->fullscreen)
- fprintf(f, "\t\t<fullscreen />\n");
+ fprintf(f, "\t<fullscreen />\n");
if (c->above)
- fprintf(f, "\t\t<above />\n");
+ fprintf(f, "\t<above />\n");
if (c->below)
- fprintf(f, "\t\t<below />\n");
+ fprintf(f, "\t<below />\n");
if (c->max_horz)
- fprintf(f, "\t\t<max_horz />\n");
+ fprintf(f, "\t<max_horz />\n");
if (c->max_vert)
- fprintf(f, "\t\t<max_vert />\n");
- fprintf(f, "\t</window>\n\n");
+ fprintf(f, "\t<max_vert />\n");
+ fprintf(f, "</window>\n\n");
+
+ ++stack_pos;
+
+ g_free(client_id);
}
fprintf(f, "</openbox_session>\n");
fclose(f);
}
- save_commands();
+ return success;
+}
- SmcSaveYourselfDone(conn, success);
+void session_state_free(ObSessionState *state)
+{
+ if (state) {
+ g_free(state->id);
+ g_free(state->name);
+ g_free(state->class);
+ g_free(state->role);
+
+ g_free(state);
+ }
}
-static void sm_die(SmcConn conn, SmPointer data)
+gboolean session_state_cmp(ObSessionState *s, ObClient *c)
{
- ob_exit();
- ob_debug("got DIE from session manager\n");
+ gchar *client_id;
+
+ if (!(client_id = client_get_sm_client_id(c)))
+ return FALSE;
+ if (strcmp(s->id, client_id)) {
+ g_free(client_id);
+ return FALSE;
+ }
+ g_free(client_id);
+ if (strcmp(s->name, c->name))
+ return FALSE;
+ if (strcmp(s->class, c->class))
+ return FALSE;
+ if (strcmp(s->role, c->role))
+ return FALSE;
+ return TRUE;
}
-static void sm_save_complete(SmcConn conn, SmPointer data)
+GList* session_state_find(ObClient *c)
{
- ob_debug("got SAVE COMPLETE from session manager\n");
+ GList *it;
+
+ for (it = session_saved_state; it; it = g_list_next(it)) {
+ ObSessionState *s = it->data;
+ if (!s->matched && session_state_cmp(s, c)) {
+ s->matched = TRUE;
+ break;
+ }
+ }
+ return it;
}
-static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
+static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
{
- ob_debug("got SHUTDOWN CANCELLED from session manager\n");
+ return s1->stacking - s2->stacking;
}
void session_load(char *path)
node = parse_find_node("window", node->xmlChildrenNode);
while (node) {
- gchar *id, *name, *class, *role;
- guint desktop;
- gint x, y, w, h;
- gboolean shaded, iconic, skip_pager, skip_taskbar, fullscreen;
- gboolean above, below, max_horz, max_vert;
+ ObSessionState *state;
- if (!parse_attr_string("id", node, &id))
+ state = g_new0(ObSessionState, 1);
+
+ if (!parse_attr_string("id", node, &state->id))
goto session_load_bail;
if (!(n = parse_find_node("name", node->xmlChildrenNode)))
goto session_load_bail;
- name = parse_string(doc, n);
+ state->name = parse_string(doc, n);
if (!(n = parse_find_node("class", node->xmlChildrenNode)))
goto session_load_bail;
- class = parse_string(doc, n);
+ state->class = parse_string(doc, n);
if (!(n = parse_find_node("role", node->xmlChildrenNode)))
goto session_load_bail;
- role = parse_string(doc, n);
+ state->role = parse_string(doc, n);
+ if (!(n = parse_find_node("stacking", node->xmlChildrenNode)))
+ goto session_load_bail;
+ state->stacking = parse_int(doc, n);
if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
goto session_load_bail;
- desktop = parse_int(doc, n);
+ state->desktop = parse_int(doc, n);
if (!(n = parse_find_node("x", node->xmlChildrenNode)))
goto session_load_bail;
- x = parse_int(doc, n);
+ state->x = parse_int(doc, n);
if (!(n = parse_find_node("y", node->xmlChildrenNode)))
goto session_load_bail;
- y = parse_int(doc, n);
+ state->y = parse_int(doc, n);
if (!(n = parse_find_node("width", node->xmlChildrenNode)))
goto session_load_bail;
- w = parse_int(doc, n);
+ state->w = parse_int(doc, n);
if (!(n = parse_find_node("height", node->xmlChildrenNode)))
goto session_load_bail;
- h = parse_int(doc, n);
-
- shaded = parse_find_node("shaded", node->xmlChildrenNode) != NULL;
- iconic = parse_find_node("iconic", node->xmlChildrenNode) != NULL;
- skip_pager = parse_find_node("skip_pager", node->xmlChildrenNode)
- != NULL;
- skip_taskbar = parse_find_node("skip_taskbar", node->xmlChildrenNode)
- != NULL;
- fullscreen = parse_find_node("fullscreen", node->xmlChildrenNode)
- != NULL;
- above = parse_find_node("above", node->xmlChildrenNode) != NULL;
- below = parse_find_node("below", node->xmlChildrenNode) != NULL;
- max_horz = parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
- max_vert = parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
+ state->h = parse_int(doc, n);
+
+ state->shaded =
+ parse_find_node("shaded", node->xmlChildrenNode) != NULL;
+ state->iconic =
+ parse_find_node("iconic", node->xmlChildrenNode) != NULL;
+ state->skip_pager =
+ parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
+ state->skip_taskbar =
+ parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
+ state->fullscreen =
+ parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
+ state->above =
+ parse_find_node("above", node->xmlChildrenNode) != NULL;
+ state->below =
+ parse_find_node("below", node->xmlChildrenNode) != NULL;
+ state->max_horz =
+ parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
+ state->max_vert =
+ parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
- g_message("read session window %s", name);
-
- /* XXX save this */
+ /* save this */
+ session_saved_state = g_list_prepend(session_saved_state, state);
+ goto session_load_ok;
session_load_bail:
+ session_state_free(state);
+
+ session_load_ok:
node = parse_find_node("window", node->next);
}
- xmlFreeDoc(doc);
+ /* sort them by their stacking order */
+ session_saved_state = g_list_sort(session_saved_state,
+ (GCompareFunc)stack_sort);
- unlink(path);
+ xmlFreeDoc(doc);
}
#endif