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() {}
16 #include "parser/parse.h"
23 # include <sys/types.h>
27 #include <X11/SM/SMlib.h>
29 static SmcConn sm_conn;
30 static gchar *save_file;
32 static gchar **sm_argv;
33 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
34 Bool shutdown, int interact_style, Bool fast);
35 static void sm_die(SmcConn conn, SmPointer data);
36 static void sm_save_complete(SmcConn conn, SmPointer data);
37 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
39 static void save_commands()
42 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
43 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
46 gboolean has_id = FALSE, has_file = FALSE;
48 for (i = 1; !has_id && !has_file && i < sm_argc - 1; ++i) {
49 if (!has_id && strcmp(sm_argv[i], "--sm-client-id") == 0)
51 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
55 n = (has_file ? sm_argc-2 : sm_argc);
56 n = (has_id ? n-2 : n);
57 prop_cmd.vals = g_new(SmPropValue, n);
58 prop_cmd.num_vals = n;
59 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
60 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
61 strcmp (sm_argv[i], "--sm-save-file") == 0) {
62 ++i, --j; /* skip the next as well, keep j where it is */
64 prop_cmd.vals[j].value = sm_argv[i];
65 prop_cmd.vals[j].length = strlen(sm_argv[i]);
69 n = (has_file ? sm_argc : sm_argc+2);
70 n = (has_id ? n-2 : n);
71 prop_res.vals = g_new(SmPropValue, n);
72 prop_res.num_vals = n;
73 for (i = 0, j = 0; i < sm_argc; ++i, ++j) {
74 if (strcmp (sm_argv[i], "--sm-client-id") == 0 ||
75 strcmp (sm_argv[i], "--sm-save-file") == 0) {
76 ++i, --j; /* skip the next as well, keep j where it is */
78 prop_res.vals[j].value = sm_argv[i];
79 prop_res.vals[j].length = strlen(sm_argv[i]);
83 file_path = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
86 prop_res.vals[j].value = "--sm-save-file";
87 prop_res.vals[j++].length = strlen("--sm-save-file");
88 prop_res.vals[j].value = file_path;
89 prop_res.vals[j++].length = strlen(file_path);
93 SmcSetProperties(sm_conn, 1, props);
96 g_free(prop_res.vals);
97 g_free(prop_cmd.vals);
100 void session_startup(int argc, char **argv)
102 #define SM_ERR_LEN 1024
105 char sm_err[SM_ERR_LEN];
110 cb.save_yourself.callback = sm_save_yourself;
111 cb.save_yourself.client_data = NULL;
113 cb.die.callback = sm_die;
114 cb.die.client_data = NULL;
116 cb.save_complete.callback = sm_save_complete;
117 cb.save_complete.client_data = NULL;
119 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
120 cb.shutdown_cancelled.client_data = NULL;
122 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
123 SmcSaveYourselfProcMask |
125 SmcSaveCompleteProcMask |
126 SmcShutdownCancelledProcMask,
127 &cb, ob_sm_id, &ob_sm_id,
130 g_warning("Failed to connect to session manager: %s", sm_err);
132 SmPropValue val_prog;
134 SmPropValue val_hint;
137 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
138 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
139 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
140 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
141 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
146 val_prog.value = argv[0];
147 val_prog.length = strlen(argv[0]);
149 val_uid.value = g_strdup(g_get_user_name());
150 val_uid.length = strlen(val_uid.value);
152 hint = SmRestartImmediately;
153 val_hint.value = &hint;
156 sprintf(pid, "%ld", (long)getpid());
158 val_pid.length = strlen(pid);
160 /* priority with gnome-session-manager, low to run before other apps */
162 val_pri.value = &pri;
165 prop_prog.vals = &val_prog;
166 prop_uid.vals = &val_uid;
167 prop_hint.vals = &val_hint;
168 prop_pid.vals = &val_pid;
169 prop_pri.vals = &val_pri;
171 props[0] = &prop_prog;
172 props[1] = &prop_uid;
173 props[2] = &prop_hint;
174 props[3] = &prop_pid;
175 props[4] = &prop_pri;
177 SmcSetProperties(sm_conn, 5, props);
179 g_free(val_uid.value);
181 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
185 void session_shutdown()
190 SmPropValue val_hint;
191 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
195 /* when we exit, we want to reset this to a more friendly state */
196 hint = SmRestartIfRunning;
197 val_hint.value = &hint;
200 prop_hint.vals = &val_hint;
202 props[0] = &prop_hint;
204 SmcSetProperties(sm_conn, 1, props);
206 SmcCloseConnection(sm_conn, 0, NULL);
210 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
211 Bool shutdown, int interact_style, Bool fast)
216 gboolean success = TRUE;
218 ob_debug("got SAVE YOURSELF from session manager\n");
220 /* this algo is from metacity */
221 filename = g_strdup_printf("%d-%d-%u.obs",
225 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
229 f = fopen(save_file, "w");
232 g_warning("unable to save the session to %s: %s",
233 save_file, strerror(errno));
235 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
236 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
238 for (it = client_list; it; it = g_list_next(it)) {
241 gint prex, prey, prew, preh;
242 ObClient *c = it->data;
244 if (!client_normal(c))
249 prew = c->area.width;
250 preh = c->area.height;
251 if (PROP_GETA32(c->window, openbox_premax, cardinal,
252 (guint32**)&dimensions, &num)) {
254 prex = dimensions[0];
255 prey = dimensions[1];
256 prew = dimensions[2];
257 preh = dimensions[3];
262 fprintf(f, "\t<window id=\"%s\">\n",
263 g_markup_escape_text("XXX", -1));
264 fprintf(f, "\t\t<name>%s</name>\n",
265 g_markup_escape_text(c->name, -1));
266 fprintf(f, "\t\t<class>%s</class>\n",
267 g_markup_escape_text(c->class, -1));
268 fprintf(f, "\t\t<role>%s</role>\n",
269 g_markup_escape_text(c->role, -1));
270 fprintf(f, "\t\t<desktop>%d</desktop>\n", c->desktop);
271 fprintf(f, "\t\t<x>%d</x>\n", prex);
272 fprintf(f, "\t\t<y>%d</y>\n", prey);
273 fprintf(f, "\t\t<width>%d</width>\n", prew);
274 fprintf(f, "\t\t<height>%d</height>\n", preh);
276 fprintf(f, "\t\t<shaded />\n");
278 fprintf(f, "\t\t<iconic />\n");
280 fprintf(f, "\t\t<skip_pager />\n");
282 fprintf(f, "\t\t<skip_taskbar />\n");
284 fprintf(f, "\t\t<fullscreen />\n");
286 fprintf(f, "\t\t<above />\n");
288 fprintf(f, "\t\t<below />\n");
290 fprintf(f, "\t\t<max_horz />\n");
292 fprintf(f, "\t\t<max_vert />\n");
293 fprintf(f, "\t</window>\n\n");
296 fprintf(f, "</openbox_session>\n");
300 g_warning("error while saving the session to %s: %s",
301 save_file, strerror(errno));
308 SmcSaveYourselfDone(conn, success);
311 static void sm_die(SmcConn conn, SmPointer data)
314 ob_debug("got DIE from session manager\n");
317 static void sm_save_complete(SmcConn conn, SmPointer data)
319 ob_debug("got SAVE COMPLETE from session manager\n");
322 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
324 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
327 void session_load(char *path)
333 if (!parse_load(path, "openbox_session", &doc, &node))
336 if (!parse_attr_string("id", node, &sm_id))
338 ob_sm_id = g_strdup(sm_id);
340 node = parse_find_node("window", node->xmlChildrenNode);
342 gchar *id, *name, *class, *role;
345 gboolean shaded, iconic, skip_pager, skip_taskbar, fullscreen;
346 gboolean above, below, max_horz, max_vert;
348 if (!parse_attr_string("id", node, &id))
349 goto session_load_bail;
350 if (!(n = parse_find_node("name", node->xmlChildrenNode)))
351 goto session_load_bail;
352 name = parse_string(doc, n);
353 if (!(n = parse_find_node("class", node->xmlChildrenNode)))
354 goto session_load_bail;
355 class = parse_string(doc, n);
356 if (!(n = parse_find_node("role", node->xmlChildrenNode)))
357 goto session_load_bail;
358 role = parse_string(doc, n);
359 if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
360 goto session_load_bail;
361 desktop = parse_int(doc, n);
362 if (!(n = parse_find_node("x", node->xmlChildrenNode)))
363 goto session_load_bail;
364 x = parse_int(doc, n);
365 if (!(n = parse_find_node("y", node->xmlChildrenNode)))
366 goto session_load_bail;
367 y = parse_int(doc, n);
368 if (!(n = parse_find_node("width", node->xmlChildrenNode)))
369 goto session_load_bail;
370 w = parse_int(doc, n);
371 if (!(n = parse_find_node("height", node->xmlChildrenNode)))
372 goto session_load_bail;
373 h = parse_int(doc, n);
375 shaded = parse_find_node("shaded", node->xmlChildrenNode) != NULL;
376 iconic = parse_find_node("iconic", node->xmlChildrenNode) != NULL;
377 skip_pager = parse_find_node("skip_pager", node->xmlChildrenNode)
379 skip_taskbar = parse_find_node("skip_taskbar", node->xmlChildrenNode)
381 fullscreen = parse_find_node("fullscreen", node->xmlChildrenNode)
383 above = parse_find_node("above", node->xmlChildrenNode) != NULL;
384 below = parse_find_node("below", node->xmlChildrenNode) != NULL;
385 max_horz = parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
386 max_vert = parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
388 g_message("read session window %s", name);
394 node = parse_find_node("window", node->next);