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