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