]> Dogcows Code - chaz/openbox/blob - openbox/session.c
dont use the _OPENBOX_PREMAX window property anymore, save max and fullscreen pre...
[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(int *argc, char ***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(char *path);
66 static gboolean session_save();
67
68 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
69 Bool shutdown, int 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 if (save_file) {
96 prop_res.vals[i].value = "--sm-save-file";
97 prop_res.vals[i++].length = strlen("--sm-save-file");
98 prop_res.vals[i].value = save_file;
99 prop_res.vals[i++].length = strlen(save_file);
100 } else {
101 prop_res.vals[i].value = "--sm-client-id";
102 prop_res.vals[i++].length = strlen("--sm-client-id");
103 prop_res.vals[i].value = sm_id;
104 prop_res.vals[i++].length = strlen(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 static void remove_two_args(int *argc, char ***argv, int index)
116 {
117 int i;
118
119 for (i = index; i < index + 2; ++i)
120 (*argv)[i] = (*argv)[i+2];
121 *argc -= 2;
122 }
123
124 static void parse_args(int *argc, char ***argv)
125 {
126 int i;
127
128 for (i = 1; i < *argc; ++i) {
129 if (!strcmp((*argv)[i], "--sm-client-id")) {
130 if (i == *argc - 1) /* no args left */
131 g_printerr(_("--sm-client-id requires an argument\n"));
132 else {
133 sm_id = g_strdup((*argv)[i+1]);
134 remove_two_args(argc, argv, i);
135 ++i;
136 }
137 } else if (!strcmp((*argv)[i], "--sm-save-file")) {
138 if (i == *argc - 1) /* no args left */
139 g_printerr(_("--sm-save-file requires an argument\n"));
140 else {
141 save_file = g_strdup((*argv)[i+1]);
142 remove_two_args(argc, argv, i);
143 ++i;
144 }
145 } else if (!strcmp((*argv)[i], "--sm-disable")) {
146 sm_disable = TRUE;
147 }
148 }
149 }
150
151 void session_startup(int *argc, char ***argv)
152 {
153 #define SM_ERR_LEN 1024
154
155 SmcCallbacks cb;
156 char sm_err[SM_ERR_LEN];
157
158 parse_args(argc, argv);
159
160 if (sm_disable)
161 return;
162
163 sm_sessions_path = g_build_filename(parse_xdg_data_home_path(),
164 "openbox", "sessions", NULL);
165 parse_mkdir_path(sm_sessions_path, 0700);
166
167 if (save_file)
168 session_load(save_file);
169
170 sm_argc = *argc;
171 sm_argv = *argv;
172
173 cb.save_yourself.callback = sm_save_yourself;
174 cb.save_yourself.client_data = NULL;
175
176 cb.die.callback = sm_die;
177 cb.die.client_data = NULL;
178
179 cb.save_complete.callback = sm_save_complete;
180 cb.save_complete.client_data = NULL;
181
182 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
183 cb.shutdown_cancelled.client_data = NULL;
184
185 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
186 SmcSaveYourselfProcMask |
187 SmcDieProcMask |
188 SmcSaveCompleteProcMask |
189 SmcShutdownCancelledProcMask,
190 &cb, sm_id, &sm_id,
191 SM_ERR_LEN, sm_err);
192 if (sm_conn == NULL)
193 ob_debug("Failed to connect to session manager: %s\n", sm_err);
194 else {
195 SmPropValue val_prog;
196 SmPropValue val_uid;
197 SmPropValue val_hint;
198 SmPropValue val_pri;
199 SmPropValue val_pid;
200 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
201 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
202 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
203 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
204 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
205 SmProp *props[6];
206 gchar hint, pri;
207 gchar pid[32];
208
209 val_prog.value = sm_argv[0];
210 val_prog.length = strlen(sm_argv[0]);
211
212 val_uid.value = g_strdup(g_get_user_name());
213 val_uid.length = strlen(val_uid.value);
214
215 hint = SmRestartImmediately;
216 val_hint.value = &hint;
217 val_hint.length = 1;
218
219 sprintf(pid, "%ld", (long)getpid());
220 val_pid.value = pid;
221 val_pid.length = strlen(pid);
222
223 /* priority with gnome-session-manager, low to run before other apps */
224 pri = 20;
225 val_pri.value = &pri;
226 val_pri.length = 1;
227
228 prop_prog.vals = &val_prog;
229 prop_uid.vals = &val_uid;
230 prop_hint.vals = &val_hint;
231 prop_pid.vals = &val_pid;
232 prop_pri.vals = &val_pri;
233
234 props[0] = &prop_prog;
235 props[1] = &prop_uid;
236 props[2] = &prop_hint;
237 props[3] = &prop_pid;
238 props[4] = &prop_pri;
239
240 SmcSetProperties(sm_conn, 5, props);
241
242 g_free(val_uid.value);
243
244 save_commands();
245 }
246 }
247
248 void session_shutdown()
249 {
250 g_free(sm_sessions_path);
251 g_free(save_file);
252 g_free(sm_id);
253
254 if (sm_conn) {
255 SmPropValue val_hint;
256 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
257 SmProp *props[1];
258 gulong hint;
259
260 /* when we exit, we want to reset this to a more friendly state */
261 hint = SmRestartIfRunning;
262 val_hint.value = &hint;
263 val_hint.length = 1;
264
265 prop_hint.vals = &val_hint;
266
267 props[0] = &prop_hint;
268
269 SmcSetProperties(sm_conn, 1, props);
270
271 SmcCloseConnection(sm_conn, 0, NULL);
272
273 while (session_saved_state) {
274 session_state_free(session_saved_state->data);
275 session_saved_state = g_list_delete_link(session_saved_state,
276 session_saved_state);
277 }
278 }
279 }
280
281 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
282 {
283 gboolean success;
284
285 success = session_save();
286 save_commands();
287
288 SmcSaveYourselfDone(conn, success);
289 }
290
291 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
292 Bool shutdown, int interact_style, Bool fast)
293 {
294 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
295 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
296 SmcSaveYourselfDone(conn, FALSE);
297 }
298 }
299
300 static void sm_die(SmcConn conn, SmPointer data)
301 {
302 ob_exit(0);
303 }
304
305 static void sm_save_complete(SmcConn conn, SmPointer data)
306 {
307 }
308
309 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
310 {
311 }
312
313 static gboolean session_save()
314 {
315 gchar *filename;
316 FILE *f;
317 GList *it;
318 gboolean success = TRUE;
319
320 /* this algo is from metacity */
321 filename = g_strdup_printf("%d-%d-%u.obs",
322 (int) time(NULL),
323 (int) getpid(),
324 g_random_int());
325 save_file = g_build_filename(sm_sessions_path, filename, NULL);
326 g_free(filename);
327
328 f = fopen(save_file, "w");
329 if (!f) {
330 success = FALSE;
331 g_warning("unable to save the session to %s: %s",
332 save_file, strerror(errno));
333 } else {
334 guint stack_pos = 0;
335
336 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
337 fprintf(f, "<openbox_session id=\"%s\">\n\n", sm_id);
338
339 for (it = stacking_list; it; it = g_list_next(it)) {
340 gint prex, prey, prew, preh;
341 ObClient *c;
342 gchar *t;
343
344 if (WINDOW_IS_CLIENT(it->data))
345 c = WINDOW_AS_CLIENT(it->data);
346 else
347 continue;
348
349 if (!client_normal(c))
350 continue;
351
352 if (!c->sm_client_id)
353 continue;
354
355 prex = c->area.x;
356 prey = c->area.y;
357 prew = c->area.width;
358 preh = c->area.height;
359 if (c->fullscreen) {
360 prex = c->pre_fullscreen_area.x;
361 prey = c->pre_fullscreen_area.x;
362 prew = c->pre_fullscreen_area.width;
363 preh = c->pre_fullscreen_area.height;
364 }
365 if (c->max_horz) {
366 prex = c->pre_max_area.x;
367 prew = c->pre_max_area.width;
368 }
369 if (c->max_vert) {
370 prey = c->pre_max_area.y;
371 preh = c->pre_max_area.height;
372 }
373
374 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
375
376 t = g_markup_escape_text(c->name, -1);
377 fprintf(f, "\t<name>%s</name>\n", t);
378 g_free(t);
379
380 t = g_markup_escape_text(c->class, -1);
381 fprintf(f, "\t<class>%s</class>\n", t);
382 g_free(t);
383
384 t = g_markup_escape_text(c->role, -1);
385 fprintf(f, "\t<role>%s</role>\n", t);
386 g_free(t);
387
388 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
389 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
390 fprintf(f, "\t<x>%d</x>\n", prex);
391 fprintf(f, "\t<y>%d</y>\n", prey);
392 fprintf(f, "\t<width>%d</width>\n", prew);
393 fprintf(f, "\t<height>%d</height>\n", preh);
394 if (c->shaded)
395 fprintf(f, "\t<shaded />\n");
396 if (c->iconic)
397 fprintf(f, "\t<iconic />\n");
398 if (c->skip_pager)
399 fprintf(f, "\t<skip_pager />\n");
400 if (c->skip_taskbar)
401 fprintf(f, "\t<skip_taskbar />\n");
402 if (c->fullscreen)
403 fprintf(f, "\t<fullscreen />\n");
404 if (c->above)
405 fprintf(f, "\t<above />\n");
406 if (c->below)
407 fprintf(f, "\t<below />\n");
408 if (c->max_horz)
409 fprintf(f, "\t<max_horz />\n");
410 if (c->max_vert)
411 fprintf(f, "\t<max_vert />\n");
412 fprintf(f, "</window>\n\n");
413
414 ++stack_pos;
415 }
416
417 fprintf(f, "</openbox_session>\n");
418
419 if (fflush(f)) {
420 success = FALSE;
421 g_warning("error while saving the session to %s: %s",
422 save_file, strerror(errno));
423 }
424 fclose(f);
425 }
426
427 return success;
428 }
429
430 void session_state_free(ObSessionState *state)
431 {
432 if (state) {
433 g_free(state->id);
434 g_free(state->name);
435 g_free(state->class);
436 g_free(state->role);
437
438 g_free(state);
439 }
440 }
441
442 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
443 {
444 return (c->sm_client_id &&
445 !strcmp(s->id, c->sm_client_id) &&
446 !strcmp(s->name, c->name) &&
447 !strcmp(s->class, c->class) &&
448 !strcmp(s->role, c->role));
449 }
450
451 GList* session_state_find(ObClient *c)
452 {
453 GList *it;
454
455 for (it = session_saved_state; it; it = g_list_next(it)) {
456 ObSessionState *s = it->data;
457 if (!s->matched && session_state_cmp(s, c)) {
458 s->matched = TRUE;
459 break;
460 }
461 }
462 return it;
463 }
464
465 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
466 {
467 return s1->stacking - s2->stacking;
468 }
469
470 static void session_load(char *path)
471 {
472 xmlDocPtr doc;
473 xmlNodePtr node, n;
474 gchar *id;
475
476 if (!parse_load(path, "openbox_session", &doc, &node))
477 return;
478
479 if (!parse_attr_string("id", node, &id))
480 return;
481 g_free(sm_id);
482 sm_id = id;
483
484 node = parse_find_node("window", node->children);
485 while (node) {
486 ObSessionState *state;
487
488 state = g_new0(ObSessionState, 1);
489
490 if (!parse_attr_string("id", node, &state->id))
491 goto session_load_bail;
492 if (!(n = parse_find_node("name", node->children)))
493 goto session_load_bail;
494 state->name = parse_string(doc, n);
495 if (!(n = parse_find_node("class", node->children)))
496 goto session_load_bail;
497 state->class = parse_string(doc, n);
498 if (!(n = parse_find_node("role", node->children)))
499 goto session_load_bail;
500 state->role = parse_string(doc, n);
501 if (!(n = parse_find_node("stacking", node->children)))
502 goto session_load_bail;
503 state->stacking = parse_int(doc, n);
504 if (!(n = parse_find_node("desktop", node->children)))
505 goto session_load_bail;
506 state->desktop = parse_int(doc, n);
507 if (!(n = parse_find_node("x", node->children)))
508 goto session_load_bail;
509 state->x = parse_int(doc, n);
510 if (!(n = parse_find_node("y", node->children)))
511 goto session_load_bail;
512 state->y = parse_int(doc, n);
513 if (!(n = parse_find_node("width", node->children)))
514 goto session_load_bail;
515 state->w = parse_int(doc, n);
516 if (!(n = parse_find_node("height", node->children)))
517 goto session_load_bail;
518 state->h = parse_int(doc, n);
519
520 state->shaded =
521 parse_find_node("shaded", node->children) != NULL;
522 state->iconic =
523 parse_find_node("iconic", node->children) != NULL;
524 state->skip_pager =
525 parse_find_node("skip_pager", node->children) != NULL;
526 state->skip_taskbar =
527 parse_find_node("skip_taskbar", node->children) != NULL;
528 state->fullscreen =
529 parse_find_node("fullscreen", node->children) != NULL;
530 state->above =
531 parse_find_node("above", node->children) != NULL;
532 state->below =
533 parse_find_node("below", node->children) != NULL;
534 state->max_horz =
535 parse_find_node("max_horz", node->children) != NULL;
536 state->max_vert =
537 parse_find_node("max_vert", node->children) != NULL;
538
539 /* save this */
540 session_saved_state = g_list_prepend(session_saved_state, state);
541 goto session_load_ok;
542
543 session_load_bail:
544 session_state_free(state);
545
546 session_load_ok:
547
548 node = parse_find_node("window", node->next);
549 }
550
551 /* sort them by their stacking order */
552 session_saved_state = g_list_sort(session_saved_state,
553 (GCompareFunc)stack_sort);
554
555 xmlFreeDoc(doc);
556 }
557
558 #endif
This page took 0.061198 seconds and 5 git commands to generate.