1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 session.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 /* This session code is largely inspired by metacity code. */
26 GList
*session_saved_state
;
28 void session_startup(gint argc
, gchar
**argv
) {}
29 void session_shutdown(gboolean permanent
) {}
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
) {}
42 #include "parser/parse.h"
49 # include <sys/types.h>
53 #include <X11/SM/SMlib.h>
55 GList
*session_saved_state
;
57 static gboolean sm_disable
;
58 static SmcConn sm_conn
;
59 static gchar
*save_file
;
62 static gchar
**sm_argv
;
63 static gchar
*sm_sessions_path
;
65 static void session_load(gchar
*path
);
66 static gboolean
session_save();
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
);
74 static void save_commands()
77 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
78 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
};
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
]);
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
]);
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
);
100 props
[0] = &prop_res
;
101 props
[1] = &prop_cmd
;
102 SmcSetProperties(sm_conn
, 2, props
);
104 g_free(prop_res
.vals
);
105 g_free(prop_cmd
.vals
);
108 static void remove_args(gint
*argc
, gchar
***argv
, gint index
, gint num
)
112 for (i
= index
; i
< index
+ num
; ++i
)
113 (*argv
)[i
] = (*argv
)[i
+num
];
117 static void parse_args(gint
*argc
, gchar
***argv
)
121 for (i
= 1; i
< *argc
; ++i
) {
122 if (!strcmp((*argv
)[i
], "--sm-client-id")) {
123 if (i
== *argc
- 1) /* no args left */
124 g_printerr(_("--sm-client-id requires an argument\n"));
126 sm_id
= g_strdup((*argv
)[i
+1]);
127 remove_args(argc
, argv
, i
, 2);
130 } else if (!strcmp((*argv
)[i
], "--sm-save-file")) {
131 if (i
== *argc
- 1) /* no args left */
132 g_printerr(_("--sm-save-file requires an argument\n"));
134 save_file
= g_strdup((*argv
)[i
+1]);
135 remove_args(argc
, argv
, i
, 2);
138 } else if (!strcmp((*argv
)[i
], "--sm-disable")) {
140 remove_args(argc
, argv
, i
, 1);
145 void session_startup(gint argc
, gchar
**argv
)
147 #define SM_ERR_LEN 1024
150 gchar sm_err
[SM_ERR_LEN
];
154 sm_argv
= g_new(gchar
*, argc
);
155 for (i
= 0; i
< argc
; ++i
)
156 sm_argv
[i
] = argv
[i
];
158 parse_args(&sm_argc
, &sm_argv
);
165 sm_sessions_path
= g_build_filename(parse_xdg_data_home_path(),
166 "openbox", "sessions", NULL
);
167 if (!parse_mkdir_path(sm_sessions_path
, 0700)) {
168 g_message(_("Unable to make directory '%s': %s"),
169 sm_sessions_path
, g_strerror(errno
));
173 session_load(save_file
);
177 /* this algo is from metacity */
178 filename
= g_strdup_printf("%d-%d-%u.obs",
182 save_file
= g_build_filename(sm_sessions_path
, filename
, NULL
);
186 cb
.save_yourself
.callback
= sm_save_yourself
;
187 cb
.save_yourself
.client_data
= NULL
;
189 cb
.die
.callback
= sm_die
;
190 cb
.die
.client_data
= NULL
;
192 cb
.save_complete
.callback
= sm_save_complete
;
193 cb
.save_complete
.client_data
= NULL
;
195 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
196 cb
.shutdown_cancelled
.client_data
= NULL
;
198 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
199 SmcSaveYourselfProcMask
|
201 SmcSaveCompleteProcMask
|
202 SmcShutdownCancelledProcMask
,
206 ob_debug("Failed to connect to session manager: %s\n", sm_err
);
208 SmPropValue val_prog
;
210 SmPropValue val_hint
;
213 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
214 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
215 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
216 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
217 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
222 val_prog
.value
= sm_argv
[0];
223 val_prog
.length
= strlen(sm_argv
[0]);
225 val_uid
.value
= g_strdup(g_get_user_name());
226 val_uid
.length
= strlen(val_uid
.value
);
228 hint
= SmRestartImmediately
;
229 val_hint
.value
= &hint
;
232 g_snprintf(pid
, sizeof(pid
), "%ld", (glong
) getpid());
234 val_pid
.length
= strlen(pid
);
236 /* priority with gnome-session-manager, low to run before other apps */
238 val_pri
.value
= &pri
;
241 prop_prog
.vals
= &val_prog
;
242 prop_uid
.vals
= &val_uid
;
243 prop_hint
.vals
= &val_hint
;
244 prop_pid
.vals
= &val_pid
;
245 prop_pri
.vals
= &val_pri
;
247 props
[0] = &prop_prog
;
248 props
[1] = &prop_uid
;
249 props
[2] = &prop_hint
;
250 props
[3] = &prop_pid
;
251 props
[4] = &prop_pri
;
253 SmcSetProperties(sm_conn
, 5, props
);
255 g_free(val_uid
.value
);
261 void session_shutdown(gboolean permanent
)
266 g_free(sm_sessions_path
);
272 /* if permanent is true then we will change our session state so that
273 the SM won't run us again */
275 SmPropValue val_hint
;
276 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
280 /* when we exit, we want to reset this to a more friendly state */
281 hint
= SmRestartIfRunning
;
282 val_hint
.value
= &hint
;
285 prop_hint
.vals
= &val_hint
;
287 props
[0] = &prop_hint
;
289 SmcSetProperties(sm_conn
, 1, props
);
292 SmcCloseConnection(sm_conn
, 0, NULL
);
294 while (session_saved_state
) {
295 session_state_free(session_saved_state
->data
);
296 session_saved_state
= g_list_delete_link(session_saved_state
,
297 session_saved_state
);
302 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
306 success
= session_save();
309 SmcSaveYourselfDone(conn
, success
);
312 static void sm_save_yourself(SmcConn conn
, SmPointer data
, gint save_type
,
313 Bool shutdown
, gint interact_style
, Bool fast
)
315 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
316 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
317 SmcSaveYourselfDone(conn
, FALSE
);
321 static void sm_die(SmcConn conn
, SmPointer data
)
326 static void sm_save_complete(SmcConn conn
, SmPointer data
)
330 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
334 static gboolean
session_save()
338 gboolean success
= TRUE
;
340 f
= fopen(save_file
, "w");
343 g_message(_("Unable to save the session to '%s': %s"),
344 save_file
, g_strerror(errno
));
348 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
349 fprintf(f
, "<openbox_session id=\"%s\">\n\n", sm_id
);
351 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
352 gint prex
, prey
, prew
, preh
;
356 if (WINDOW_IS_CLIENT(it
->data
))
357 c
= WINDOW_AS_CLIENT(it
->data
);
361 if (!client_normal(c
))
364 if (!c
->sm_client_id
)
369 prew
= c
->area
.width
;
370 preh
= c
->area
.height
;
372 prex
= c
->pre_fullscreen_area
.x
;
373 prey
= c
->pre_fullscreen_area
.x
;
374 prew
= c
->pre_fullscreen_area
.width
;
375 preh
= c
->pre_fullscreen_area
.height
;
378 prex
= c
->pre_max_area
.x
;
379 prew
= c
->pre_max_area
.width
;
382 prey
= c
->pre_max_area
.y
;
383 preh
= c
->pre_max_area
.height
;
386 fprintf(f
, "<window id=\"%s\">\n", c
->sm_client_id
);
388 t
= g_markup_escape_text(c
->name
, -1);
389 fprintf(f
, "\t<name>%s</name>\n", t
);
392 t
= g_markup_escape_text(c
->class, -1);
393 fprintf(f
, "\t<class>%s</class>\n", t
);
396 t
= g_markup_escape_text(c
->role
, -1);
397 fprintf(f
, "\t<role>%s</role>\n", t
);
400 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
401 fprintf(f
, "\t<stacking>%d</stacking>\n", stack_pos
);
402 fprintf(f
, "\t<x>%d</x>\n", prex
);
403 fprintf(f
, "\t<y>%d</y>\n", prey
);
404 fprintf(f
, "\t<width>%d</width>\n", prew
);
405 fprintf(f
, "\t<height>%d</height>\n", preh
);
407 fprintf(f
, "\t<shaded />\n");
409 fprintf(f
, "\t<iconic />\n");
411 fprintf(f
, "\t<skip_pager />\n");
413 fprintf(f
, "\t<skip_taskbar />\n");
415 fprintf(f
, "\t<fullscreen />\n");
417 fprintf(f
, "\t<above />\n");
419 fprintf(f
, "\t<below />\n");
421 fprintf(f
, "\t<max_horz />\n");
423 fprintf(f
, "\t<max_vert />\n");
424 fprintf(f
, "</window>\n\n");
429 fprintf(f
, "</openbox_session>\n");
433 g_message(_("Error while saving the session to '%s': %s"),
434 save_file
, g_strerror(errno
));
442 void session_state_free(ObSessionState
*state
)
447 g_free(state
->class);
454 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
456 return (c
->sm_client_id
&&
457 !strcmp(s
->id
, c
->sm_client_id
) &&
458 !strcmp(s
->name
, c
->name
) &&
459 !strcmp(s
->class, c
->class) &&
460 !strcmp(s
->role
, c
->role
));
463 GList
* session_state_find(ObClient
*c
)
467 for (it
= session_saved_state
; it
; it
= g_list_next(it
)) {
468 ObSessionState
*s
= it
->data
;
469 if (!s
->matched
&& session_state_cmp(s
, c
)) {
477 static gint
stack_sort(const ObSessionState
*s1
, const ObSessionState
*s2
)
479 return s1
->stacking
- s2
->stacking
;
482 static void session_load(gchar
*path
)
488 if (!parse_load(path
, "openbox_session", &doc
, &node
))
491 if (!parse_attr_string("id", node
, &id
))
496 node
= parse_find_node("window", node
->children
);
498 ObSessionState
*state
;
500 state
= g_new0(ObSessionState
, 1);
502 if (!parse_attr_string("id", node
, &state
->id
))
503 goto session_load_bail
;
504 if (!(n
= parse_find_node("name", node
->children
)))
505 goto session_load_bail
;
506 state
->name
= parse_string(doc
, n
);
507 if (!(n
= parse_find_node("class", node
->children
)))
508 goto session_load_bail
;
509 state
->class = parse_string(doc
, n
);
510 if (!(n
= parse_find_node("role", node
->children
)))
511 goto session_load_bail
;
512 state
->role
= parse_string(doc
, n
);
513 if (!(n
= parse_find_node("stacking", node
->children
)))
514 goto session_load_bail
;
515 state
->stacking
= parse_int(doc
, n
);
516 if (!(n
= parse_find_node("desktop", node
->children
)))
517 goto session_load_bail
;
518 state
->desktop
= parse_int(doc
, n
);
519 if (!(n
= parse_find_node("x", node
->children
)))
520 goto session_load_bail
;
521 state
->x
= parse_int(doc
, n
);
522 if (!(n
= parse_find_node("y", node
->children
)))
523 goto session_load_bail
;
524 state
->y
= parse_int(doc
, n
);
525 if (!(n
= parse_find_node("width", node
->children
)))
526 goto session_load_bail
;
527 state
->w
= parse_int(doc
, n
);
528 if (!(n
= parse_find_node("height", node
->children
)))
529 goto session_load_bail
;
530 state
->h
= parse_int(doc
, n
);
533 parse_find_node("shaded", node
->children
) != NULL
;
535 parse_find_node("iconic", node
->children
) != NULL
;
537 parse_find_node("skip_pager", node
->children
) != NULL
;
538 state
->skip_taskbar
=
539 parse_find_node("skip_taskbar", node
->children
) != NULL
;
541 parse_find_node("fullscreen", node
->children
) != NULL
;
543 parse_find_node("above", node
->children
) != NULL
;
545 parse_find_node("below", node
->children
) != NULL
;
547 parse_find_node("max_horz", node
->children
) != NULL
;
549 parse_find_node("max_vert", node
->children
) != NULL
;
552 session_saved_state
= g_list_prepend(session_saved_state
, state
);
553 goto session_load_ok
;
556 session_state_free(state
);
560 node
= parse_find_node("window", node
->next
);
563 /* sort them by their stacking order */
564 session_saved_state
= g_list_sort(session_saved_state
,
565 (GCompareFunc
)stack_sort
);