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