declare functions that are needed when built without session support
[dana/openbox.git] / 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 ObSessionState* session_state_find(struct _ObClient *c) { return NULL; }
9 void session_state_free(ObSessionState *state) {}
10
11 #else
12
13 #include "debug.h"
14 #include "openbox.h"
15 #include "session.h"
16 #include "client.h"
17 #include "prop.h"
18 #include "parser/parse.h"
19
20 #include <time.h>
21 #include <errno.h>
22 #include <stdio.h>
23
24 #ifdef HAVE_UNISTD_H
25 #  include <sys/types.h>
26 #  include <unistd.h>
27 #endif
28
29 #include <X11/SM/SMlib.h>
30
31 static SmcConn     sm_conn;
32 static gchar      *save_file;
33 static gint        sm_argc;
34 static gchar     **sm_argv;
35 static GSList     *sm_saved_state;
36
37 static gboolean session_save();
38
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);
44
45 static void save_commands()
46 {
47     SmProp *props[2];
48     SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
49     SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
50     gint i, j, n;
51     gboolean has_id = FALSE, has_file = FALSE;
52
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)
55             has_id = TRUE;
56         if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
57             has_file = TRUE;
58     }
59
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 */
68         } else {
69             prop_cmd.vals[j].value = sm_argv[i];
70             prop_cmd.vals[j].length = strlen(sm_argv[i]);
71         }
72     }
73
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 */
82         } else {
83             prop_res.vals[j].value = sm_argv[i];
84             prop_res.vals[j].length = strlen(sm_argv[i]);
85         }
86     }
87
88     if (save_file) {
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);
93     } else {
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);
98     }
99
100     props[0] = &prop_res;
101     props[1] = &prop_cmd;
102     SmcSetProperties(sm_conn, 2, props);
103
104     g_free(prop_res.vals);
105     g_free(prop_cmd.vals);
106 }
107
108 void session_startup(int argc, char **argv)
109 {
110 #define SM_ERR_LEN 1024
111
112     SmcCallbacks cb;
113     char sm_err[SM_ERR_LEN];
114
115     sm_argc = argc;
116     sm_argv = argv;
117
118     cb.save_yourself.callback = sm_save_yourself;
119     cb.save_yourself.client_data = NULL;
120
121     cb.die.callback = sm_die;
122     cb.die.client_data = NULL;
123
124     cb.save_complete.callback = sm_save_complete;
125     cb.save_complete.client_data = NULL;
126
127     cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
128     cb.shutdown_cancelled.client_data = NULL;
129
130     sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
131                                 SmcSaveYourselfProcMask |
132                                 SmcDieProcMask |
133                                 SmcSaveCompleteProcMask |
134                                 SmcShutdownCancelledProcMask,
135                                 &cb, ob_sm_id, &ob_sm_id,
136                                 SM_ERR_LEN, sm_err);
137     if (sm_conn == NULL)
138         g_warning("Failed to connect to session manager: %s", sm_err);
139     else {
140         SmPropValue val_prog;
141         SmPropValue val_uid;
142         SmPropValue val_hint; 
143         SmPropValue val_pri;
144         SmPropValue val_pid;
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, };
150         SmProp *props[6];
151         gchar hint, pri;
152         gchar pid[32];
153
154         val_prog.value = argv[0];
155         val_prog.length = strlen(argv[0]);
156
157         val_uid.value = g_strdup(g_get_user_name());
158         val_uid.length = strlen(val_uid.value);
159
160         hint = SmRestartImmediately;
161         val_hint.value = &hint;
162         val_hint.length = 1;
163
164         sprintf(pid, "%ld", (long)getpid());
165         val_pid.value = pid;
166         val_pid.length = strlen(pid);
167
168         /* priority with gnome-session-manager, low to run before other apps */
169         pri = 20;
170         val_pri.value = &pri;
171         val_pri.length = 1;
172
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;
178
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;
184
185         SmcSetProperties(sm_conn, 5, props);
186
187         g_free(val_uid.value);
188
189         save_commands();
190
191         ob_debug("Connected to session manager with id %s\n", ob_sm_id);
192     }
193 }
194
195 void session_shutdown()
196 {
197     g_free(save_file);
198
199     if (sm_conn) {
200         SmPropValue val_hint;
201         SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
202         SmProp *props[1];
203         gulong hint;
204
205         /* when we exit, we want to reset this to a more friendly state */
206         hint = SmRestartIfRunning;
207         val_hint.value = &hint;
208         val_hint.length = 1;
209
210         prop_hint.vals = &val_hint;
211
212         props[0] = &prop_hint;
213
214         SmcSetProperties(sm_conn, 1, props);
215
216         SmcCloseConnection(sm_conn, 0, NULL);
217     }
218 }
219
220 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
221 {
222     gboolean success;
223
224     ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
225
226     success = session_save();
227     save_commands();
228
229     SmcSaveYourselfDone(conn, success);
230 }
231
232 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
233                              Bool shutdown, int interact_style, Bool fast)
234 {
235     ob_debug("got SAVE YOURSELF from session manager\n");
236
237     if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
238         ob_debug("SAVE YOURSELF PHASE 2 failed\n");
239         SmcSaveYourselfDone(conn, FALSE);
240     }
241 }
242
243 static void sm_die(SmcConn conn, SmPointer data)
244 {
245     ob_exit();
246     ob_debug("got DIE from session manager\n");
247 }
248
249 static void sm_save_complete(SmcConn conn, SmPointer data)
250 {
251     ob_debug("got SAVE COMPLETE from session manager\n");
252 }
253
254 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
255 {
256     ob_debug("got SHUTDOWN CANCELLED from session manager\n");
257 }
258
259 static gboolean session_save()
260 {
261     gchar *filename;
262     FILE *f;
263     GList *it;
264     gboolean success = TRUE;
265
266     /* this algo is from metacity */
267     filename = g_strdup_printf("%d-%d-%u.obs",
268                                (int) time(NULL),
269                                (int) getpid(),
270                                g_random_int());
271     save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
272                                  filename, NULL);
273     g_free(filename);
274
275     f = fopen(save_file, "w");
276     if (!f) {
277         success = FALSE;
278         g_warning("unable to save the session to %s: %s",
279                   save_file, strerror(errno));
280     } else {
281         fprintf(f, "<?xml version=\"1.0\"?>\n\n");
282         fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
283
284         for (it = client_list; it; it = g_list_next(it)) {
285             guint num;
286             gint32 *dimensions;
287             gint prex, prey, prew, preh;
288             ObClient *c = it->data;
289             gchar *client_id, *t;
290
291             if (!client_normal(c))
292                 continue;
293
294             if (!(client_id = client_get_sm_client_id(c)))
295                 continue;
296
297             prex = c->area.x;
298             prey = c->area.y;
299             prew = c->area.width;
300             preh = c->area.height;
301             if (PROP_GETA32(c->window, openbox_premax, cardinal,
302                             (guint32**)&dimensions, &num)) {
303                 if (num == 4) {
304                     prex = dimensions[0];
305                     prey = dimensions[1];
306                     prew = dimensions[2];
307                     preh = dimensions[3];
308                 }
309                 g_free(dimensions);
310             }
311
312             fprintf(f, "<window id=\"%s\">\n", client_id);
313
314             t = g_markup_escape_text(c->name, -1);
315             fprintf(f, "\t<name>%s</name>\n", t);
316             g_free(t);
317
318             t = g_markup_escape_text(c->class, -1);
319             fprintf(f, "\t<class>%s</class>\n", t);
320             g_free(t);
321
322             t = g_markup_escape_text(c->role, -1);
323             fprintf(f, "\t<role>%s</role>\n", t);
324             g_free(t);
325
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);
331             if (c->shaded)
332                 fprintf(f, "\t<shaded />\n");
333             if (c->iconic)
334                 fprintf(f, "\t<iconic />\n");
335             if (c->skip_pager)
336                 fprintf(f, "\t<skip_pager />\n");
337             if (c->skip_taskbar)
338                 fprintf(f, "\t<skip_taskbar />\n");
339             if (c->fullscreen)
340                 fprintf(f, "\t<fullscreen />\n");
341             if (c->above)
342                 fprintf(f, "\t<above />\n");
343             if (c->below)
344                 fprintf(f, "\t<below />\n");
345             if (c->max_horz)
346                 fprintf(f, "\t<max_horz />\n");
347             if (c->max_vert)
348                 fprintf(f, "\t<max_vert />\n");
349             fprintf(f, "</window>\n\n");
350
351             g_free(client_id);
352         }
353
354         fprintf(f, "</openbox_session>\n");
355
356         if (fflush(f)) {
357             success = FALSE;
358             g_warning("error while saving the session to %s: %s",
359                       save_file, strerror(errno));
360         }
361         fclose(f);
362     }
363
364     return success;
365 }
366
367 void session_state_free(ObSessionState *state)
368 {
369     if (state) {
370         g_free(state->id);
371         g_free(state->name);
372         g_free(state->class);
373         g_free(state->role);
374
375         g_free(state);
376     }
377 }
378
379 static gboolean session_state_cmp(ObSessionState *s, ObClient *c)
380 {
381     gchar *client_id;
382
383     if (!(client_id = client_get_sm_client_id(c)))
384         return FALSE;
385     g_print("\nsaved %s\nnow %s\n", s->id, client_id);
386     if (strcmp(s->id, client_id)) {
387         g_free(client_id);
388         return FALSE;
389     }
390     g_free(client_id);
391     g_print("saved %s\nnow %s\n", s->name, c->name);
392     if (strcmp(s->name, c->name))
393         return FALSE;
394     g_print("saved %s\nnow %s\n", s->class, c->class);
395     if (strcmp(s->class, c->class))
396         return FALSE;
397     g_print("saved %s\nnow %s\n\n", s->role, c->role);
398     if (strcmp(s->role, c->role))
399         return FALSE;
400     return TRUE;
401 }
402
403 ObSessionState* session_state_find(ObClient *c)
404 {
405     GSList *it;
406
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);
411             return s;
412         }
413     return NULL;
414 }
415
416 void session_load(char *path)
417 {
418     xmlDocPtr doc;
419     xmlNodePtr node, n;
420     gchar *sm_id;
421
422     g_message("loading session from %s", path);
423
424     if (!parse_load(path, "openbox_session", &doc, &node))
425         return;
426
427     if (!parse_attr_string("id", node, &sm_id))
428         return;
429     ob_sm_id = g_strdup(sm_id);
430
431     node = parse_find_node("window", node->xmlChildrenNode);
432     while (node) {
433         ObSessionState *state;
434
435         state = g_new0(ObSessionState, 1);
436
437         if (!parse_attr_string("id", node, &state->id))
438             goto session_load_bail;
439         if (!(n = parse_find_node("name", node->xmlChildrenNode)))
440             goto session_load_bail;
441         state->name = parse_string(doc, n);
442         if (!(n = parse_find_node("class", node->xmlChildrenNode)))
443             goto session_load_bail;
444         state->class = parse_string(doc, n);
445         if (!(n = parse_find_node("role", node->xmlChildrenNode)))
446             goto session_load_bail;
447         state->role = parse_string(doc, n);
448         if (!(n = parse_find_node("desktop", node->xmlChildrenNode)))
449             goto session_load_bail;
450         state->desktop = parse_int(doc, n);
451         if (!(n = parse_find_node("x", node->xmlChildrenNode)))
452             goto session_load_bail;
453         state->x = parse_int(doc, n);
454         if (!(n = parse_find_node("y", node->xmlChildrenNode)))
455             goto session_load_bail;
456         state->y = parse_int(doc, n);
457         if (!(n = parse_find_node("width", node->xmlChildrenNode)))
458             goto session_load_bail;
459         state->w = parse_int(doc, n);
460         if (!(n = parse_find_node("height", node->xmlChildrenNode)))
461             goto session_load_bail;
462         state->h = parse_int(doc, n);
463
464         state->shaded =
465             parse_find_node("shaded", node->xmlChildrenNode) != NULL;
466         state->iconic =
467             parse_find_node("iconic", node->xmlChildrenNode) != NULL;
468         state->skip_pager =
469             parse_find_node("skip_pager", node->xmlChildrenNode) != NULL;
470         state->skip_taskbar =
471             parse_find_node("skip_taskbar", node->xmlChildrenNode) != NULL;
472         state->fullscreen =
473             parse_find_node("fullscreen", node->xmlChildrenNode) != NULL;
474         state->above =
475             parse_find_node("above", node->xmlChildrenNode) != NULL;
476         state->below =
477             parse_find_node("below", node->xmlChildrenNode) != NULL;
478         state->max_horz =
479             parse_find_node("max_horz", node->xmlChildrenNode) != NULL;
480         state->max_vert =
481             parse_find_node("max_vert", node->xmlChildrenNode) != NULL;
482         
483         g_message("read session window %s", state->name);
484
485         /* save this */
486         g_message("saved state for %s %s", state->name, state->id);
487         sm_saved_state = g_slist_prepend(sm_saved_state, state);
488         goto session_load_ok;
489
490     session_load_bail:
491         session_state_free(state);
492
493     session_load_ok:
494
495         node = parse_find_node("window", node->next);
496     }
497
498     xmlFreeDoc(doc);
499 }
500
501 #endif