]> Dogcows Code - chaz/openbox/blob - openbox/session.c
save and load the session save file, though its data is not used yet!
[chaz/openbox] / openbox / session.c
1 /* This session code is largely inspired by metacity code. */
2
3 #ifndef USE_SM
4
5 void session_load(char *path) {}
6 void session_startup(int argc, char **argv) {}
7 void session_shutdown() {}
8
9 #else
10
11 #include "debug.h"
12 #include "openbox.h"
13 #include "session.h"
14 #include "client.h"
15 #include "prop.h"
16 #include "parser/parse.h"
17
18 #include <time.h>
19 #include <errno.h>
20 #include <stdio.h>
21
22 #ifdef HAVE_UNISTD_H
23 # include <sys/types.h>
24 # include <unistd.h>
25 #endif
26
27 #include <X11/SM/SMlib.h>
28
29 static SmcConn sm_conn;
30 static gchar *save_file;
31 static gint sm_argc;
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);
38
39 static void save_commands()
40 {
41 SmProp *props[2];
42 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
43 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
44 gchar *file_path;
45 gint i, j, n;
46 gboolean has_id = FALSE, has_file = FALSE;
47
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)
50 has_id = TRUE;
51 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
52 has_file = TRUE;
53 }
54
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 */
63 } else {
64 prop_cmd.vals[j].value = sm_argv[i];
65 prop_cmd.vals[j].length = strlen(sm_argv[i]);
66 }
67 }
68
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 */
77 } else {
78 prop_res.vals[j].value = sm_argv[i];
79 prop_res.vals[j].length = strlen(sm_argv[i]);
80 }
81 }
82
83 file_path = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
84 ob_sm_id, NULL);
85
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);
90
91 props[0] = &prop_res;
92 props[1] = &prop_cmd;
93 SmcSetProperties(sm_conn, 1, props);
94
95 g_free(file_path);
96 g_free(prop_res.vals);
97 g_free(prop_cmd.vals);
98 }
99
100 void session_startup(int argc, char **argv)
101 {
102 #define SM_ERR_LEN 1024
103
104 SmcCallbacks cb;
105 char sm_err[SM_ERR_LEN];
106
107 sm_argc = argc;
108 sm_argv = argv;
109
110 cb.save_yourself.callback = sm_save_yourself;
111 cb.save_yourself.client_data = NULL;
112
113 cb.die.callback = sm_die;
114 cb.die.client_data = NULL;
115
116 cb.save_complete.callback = sm_save_complete;
117 cb.save_complete.client_data = NULL;
118
119 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
120 cb.shutdown_cancelled.client_data = NULL;
121
122 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
123 SmcSaveYourselfProcMask |
124 SmcDieProcMask |
125 SmcSaveCompleteProcMask |
126 SmcShutdownCancelledProcMask,
127 &cb, ob_sm_id, &ob_sm_id,
128 SM_ERR_LEN, sm_err);
129 if (sm_conn == NULL)
130 g_warning("Failed to connect to session manager: %s", sm_err);
131 else {
132 SmPropValue val_prog;
133 SmPropValue val_uid;
134 SmPropValue val_hint;
135 SmPropValue val_pri;
136 SmPropValue val_pid;
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, };
142 SmProp *props[6];
143 gulong hint, pri;
144 gchar pid[32];
145
146 val_prog.value = argv[0];
147 val_prog.length = strlen(argv[0]);
148
149 val_uid.value = g_strdup(g_get_user_name());
150 val_uid.length = strlen(val_uid.value);
151
152 hint = SmRestartImmediately;
153 val_hint.value = &hint;
154 val_hint.length = 1;
155
156 sprintf(pid, "%ld", (long)getpid());
157 val_pid.value = pid;
158 val_pid.length = strlen(pid);
159
160 /* priority with gnome-session-manager, low to run before other apps */
161 pri = 20;
162 val_pri.value = &pri;
163 val_pri.length = 1;
164
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;
170
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;
176
177 SmcSetProperties(sm_conn, 5, props);
178
179 g_free(val_uid.value);
180
181 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
182 }
183 }
184
185 void session_shutdown()
186 {
187 g_free(save_file);
188
189 if (sm_conn) {
190 SmPropValue val_hint;
191 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
192 SmProp *props[1];
193 gulong hint;
194
195 /* when we exit, we want to reset this to a more friendly state */
196 hint = SmRestartIfRunning;
197 val_hint.value = &hint;
198 val_hint.length = 1;
199
200 prop_hint.vals = &val_hint;
201
202 props[0] = &prop_hint;
203
204 SmcSetProperties(sm_conn, 1, props);
205
206 SmcCloseConnection(sm_conn, 0, NULL);
207 }
208 }
209
210 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
211 Bool shutdown, int interact_style, Bool fast)
212 {
213 gchar *filename;
214 FILE *f;
215 GList *it;
216 gboolean success = TRUE;
217
218 ob_debug("got SAVE YOURSELF from session manager\n");
219
220 /* this algo is from metacity */
221 filename = g_strdup_printf("%d-%d-%u.obs",
222 (int) time(NULL),
223 (int) getpid(),
224 g_random_int());
225 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
226 filename, NULL);
227 g_free(filename);
228
229 f = fopen(save_file, "w");
230 if (!f) {
231 success = FALSE;
232 g_warning("unable to save the session to %s: %s",
233 save_file, strerror(errno));
234 } else {
235 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
236 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
237
238 for (it = client_list; it; it = g_list_next(it)) {
239 guint num;
240 gint32 *dimensions;
241 gint prex, prey, prew, preh;
242 ObClient *c = it->data;
243
244 if (!client_normal(c))
245 continue;
246
247 prex = c->area.x;
248 prey = c->area.y;
249 prew = c->area.width;
250 preh = c->area.height;
251 if (PROP_GETA32(c->window, openbox_premax, cardinal,
252 (guint32**)&dimensions, &num)) {
253 if (num == 4) {
254 prex = dimensions[0];
255 prey = dimensions[1];
256 prew = dimensions[2];
257 preh = dimensions[3];
258 }
259 g_free(dimensions);
260 }
261
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);
275 if (c->shaded)
276 fprintf(f, "\t\t<shaded />\n");
277 if (c->iconic)
278 fprintf(f, "\t\t<iconic />\n");
279 if (c->skip_pager)
280 fprintf(f, "\t\t<skip_pager />\n");
281 if (c->skip_taskbar)
282 fprintf(f, "\t\t<skip_taskbar />\n");
283 if (c->fullscreen)
284 fprintf(f, "\t\t<fullscreen />\n");
285 if (c->above)
286 fprintf(f, "\t\t<above />\n");
287 if (c->below)
288 fprintf(f, "\t\t<below />\n");
289 if (c->max_horz)
290 fprintf(f, "\t\t<max_horz />\n");
291 if (c->max_vert)
292 fprintf(f, "\t\t<max_vert />\n");
293 fprintf(f, "\t</window>\n\n");
294 }
295
296 fprintf(f, "</openbox_session>\n");
297
298 if (fflush(f)) {
299 success = FALSE;
300 g_warning("error while saving the session to %s: %s",
301 save_file, strerror(errno));
302 }
303 fclose(f);
304 }
305
306 save_commands();
307
308 SmcSaveYourselfDone(conn, success);
309 }
310
311 static void sm_die(SmcConn conn, SmPointer data)
312 {
313 ob_exit();
314 ob_debug("got DIE from session manager\n");
315 }
316
317 static void sm_save_complete(SmcConn conn, SmPointer data)
318 {
319 ob_debug("got SAVE COMPLETE from session manager\n");
320 }
321
322 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
323 {
324 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
325 }
326
327 void session_load(char *path)
328 {
329 xmlDocPtr doc;
330 xmlNodePtr node, n;
331 gchar *sm_id;
332
333 if (!parse_load(path, "openbox_session", &doc, &node))
334 return;
335
336 if (!parse_attr_string("id", node, &sm_id))
337 return;
338 ob_sm_id = g_strdup(sm_id);
339
340 node = parse_find_node("window", node->xmlChildrenNode);
341 while (node) {
342 gchar *id, *name, *class, *role;
343 guint desktop;
344 gint x, y, w, h;
345 gboolean shaded, iconic, skip_pager, skip_taskbar, fullscreen;
346 gboolean above, below, max_horz, max_vert;
347
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);
374
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)
378 != NULL;
379 skip_taskbar = parse_find_node("skip_taskbar", node->xmlChildrenNode)
380 != NULL;
381 fullscreen = parse_find_node("fullscreen", node->xmlChildrenNode)
382 != NULL;
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;
387
388 g_message("read session window %s", name);
389
390 /* XXX save this */
391
392 session_load_bail:
393
394 node = parse_find_node("window", node->next);
395 }
396
397 xmlFreeDoc(doc);
398
399 unlink(path);
400 }
401
402 #endif
This page took 0.053837 seconds and 4 git commands to generate.