1 /* This session code is largely inspired by metacity code. */
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
) {}
21 #include "parser/parse.h"
28 # include <sys/types.h>
32 #include <X11/SM/SMlib.h>
34 static SmcConn sm_conn
;
35 static gchar
*save_file
;
37 static gchar
**sm_argv
;
38 static GSList
*sm_saved_state
;
40 static gboolean
session_save();
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
);
48 static void save_commands()
51 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
52 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
54 gboolean has_id
= FALSE
, has_file
= FALSE
;
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)
59 if (!has_file
&& strcmp(sm_argv
[i
], "--sm-save-file") == 0)
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 */
72 prop_cmd
.vals
[j
].value
= sm_argv
[i
];
73 prop_cmd
.vals
[j
].length
= strlen(sm_argv
[i
]);
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 */
86 prop_res
.vals
[j
].value
= sm_argv
[i
];
87 prop_res
.vals
[j
].length
= strlen(sm_argv
[i
]);
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
);
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
);
103 props
[0] = &prop_res
;
104 props
[1] = &prop_cmd
;
105 SmcSetProperties(sm_conn
, 2, props
);
107 g_free(prop_res
.vals
);
108 g_free(prop_cmd
.vals
);
111 void session_startup(int argc
, char **argv
)
113 #define SM_ERR_LEN 1024
116 char sm_err
[SM_ERR_LEN
];
121 cb
.save_yourself
.callback
= sm_save_yourself
;
122 cb
.save_yourself
.client_data
= NULL
;
124 cb
.die
.callback
= sm_die
;
125 cb
.die
.client_data
= NULL
;
127 cb
.save_complete
.callback
= sm_save_complete
;
128 cb
.save_complete
.client_data
= NULL
;
130 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
131 cb
.shutdown_cancelled
.client_data
= NULL
;
133 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
134 SmcSaveYourselfProcMask
|
136 SmcSaveCompleteProcMask
|
137 SmcShutdownCancelledProcMask
,
138 &cb
, ob_sm_id
, &ob_sm_id
,
141 g_warning("Failed to connect to session manager: %s", sm_err
);
143 SmPropValue val_prog
;
145 SmPropValue val_hint
;
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, };
157 val_prog
.value
= argv
[0];
158 val_prog
.length
= strlen(argv
[0]);
160 val_uid
.value
= g_strdup(g_get_user_name());
161 val_uid
.length
= strlen(val_uid
.value
);
163 hint
= SmRestartImmediately
;
164 val_hint
.value
= &hint
;
167 sprintf(pid
, "%ld", (long)getpid());
169 val_pid
.length
= strlen(pid
);
171 /* priority with gnome-session-manager, low to run before other apps */
173 val_pri
.value
= &pri
;
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
;
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
;
188 SmcSetProperties(sm_conn
, 5, props
);
190 g_free(val_uid
.value
);
194 ob_debug("Connected to session manager with id %s\n", ob_sm_id
);
198 void session_shutdown()
203 SmPropValue val_hint
;
204 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
208 /* when we exit, we want to reset this to a more friendly state */
209 hint
= SmRestartIfRunning
;
210 val_hint
.value
= &hint
;
213 prop_hint
.vals
= &val_hint
;
215 props
[0] = &prop_hint
;
217 SmcSetProperties(sm_conn
, 1, props
);
219 SmcCloseConnection(sm_conn
, 0, NULL
);
223 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
227 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
229 success
= session_save();
232 SmcSaveYourselfDone(conn
, success
);
235 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
236 Bool shutdown
, int interact_style
, Bool fast
)
238 ob_debug("got SAVE YOURSELF from session manager\n");
240 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
241 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
242 SmcSaveYourselfDone(conn
, FALSE
);
246 static void sm_die(SmcConn conn
, SmPointer data
)
249 ob_debug("got DIE from session manager\n");
252 static void sm_save_complete(SmcConn conn
, SmPointer data
)
254 ob_debug("got SAVE COMPLETE from session manager\n");
257 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
259 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
262 static gboolean
session_save()
267 gboolean success
= TRUE
;
269 /* this algo is from metacity */
270 filename
= g_strdup_printf("%d-%d-%u.obs",
274 save_file
= g_build_filename(g_get_home_dir(), ".openbox", "sessions",
278 f
= fopen(save_file
, "w");
281 g_warning("unable to save the session to %s: %s",
282 save_file
, strerror(errno
));
284 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
285 fprintf(f
, "<openbox_session id=\"%s\">\n\n", ob_sm_id
);
287 for (it
= client_list
; it
; it
= g_list_next(it
)) {
290 gint prex
, prey
, prew
, preh
;
291 ObClient
*c
= it
->data
;
292 gchar
*client_id
, *t
;
294 if (!client_normal(c
))
297 if (!(client_id
= client_get_sm_client_id(c
)))
302 prew
= c
->area
.width
;
303 preh
= c
->area
.height
;
304 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
305 (guint32
**)&dimensions
, &num
)) {
307 prex
= dimensions
[0];
308 prey
= dimensions
[1];
309 prew
= dimensions
[2];
310 preh
= dimensions
[3];
315 fprintf(f
, "<window id=\"%s\">\n", client_id
);
317 t
= g_markup_escape_text(c
->name
, -1);
318 fprintf(f
, "\t<name>%s</name>\n", t
);
321 t
= g_markup_escape_text(c
->class, -1);
322 fprintf(f
, "\t<class>%s</class>\n", t
);
325 t
= g_markup_escape_text(c
->role
, -1);
326 fprintf(f
, "\t<role>%s</role>\n", t
);
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
);
335 fprintf(f
, "\t<shaded />\n");
337 fprintf(f
, "\t<iconic />\n");
339 fprintf(f
, "\t<skip_pager />\n");
341 fprintf(f
, "\t<skip_taskbar />\n");
343 fprintf(f
, "\t<fullscreen />\n");
345 fprintf(f
, "\t<above />\n");
347 fprintf(f
, "\t<below />\n");
349 fprintf(f
, "\t<max_horz />\n");
351 fprintf(f
, "\t<max_vert />\n");
352 fprintf(f
, "</window>\n\n");
357 fprintf(f
, "</openbox_session>\n");
361 g_warning("error while saving the session to %s: %s",
362 save_file
, strerror(errno
));
370 void session_state_free(ObSessionState
*state
)
375 g_free(state
->class);
382 static gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
386 if (!(client_id
= client_get_sm_client_id(c
)))
388 g_print("\nsaved %s\nnow %s\n", s
->id
, client_id
);
389 if (strcmp(s
->id
, client_id
)) {
394 g_print("saved %s\nnow %s\n", s
->name
, c
->name
);
395 if (strcmp(s
->name
, c
->name
))
397 g_print("saved %s\nnow %s\n", s
->class, c
->class);
398 if (strcmp(s
->class, c
->class))
400 g_print("saved %s\nnow %s\n\n", s
->role
, c
->role
);
401 if (strcmp(s
->role
, c
->role
))
406 ObSessionState
* session_state_find(ObClient
*c
)
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
);
419 void session_load(char *path
)
425 if (!parse_load(path
, "openbox_session", &doc
, &node
))
428 if (!parse_attr_string("id", node
, &sm_id
))
430 ob_sm_id
= g_strdup(sm_id
);
432 node
= parse_find_node("window", node
->xmlChildrenNode
);
434 ObSessionState
*state
;
436 state
= g_new0(ObSessionState
, 1);
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
);
466 parse_find_node("shaded", node
->xmlChildrenNode
) != NULL
;
468 parse_find_node("iconic", node
->xmlChildrenNode
) != NULL
;
470 parse_find_node("skip_pager", node
->xmlChildrenNode
) != NULL
;
471 state
->skip_taskbar
=
472 parse_find_node("skip_taskbar", node
->xmlChildrenNode
) != NULL
;
474 parse_find_node("fullscreen", node
->xmlChildrenNode
) != NULL
;
476 parse_find_node("above", node
->xmlChildrenNode
) != NULL
;
478 parse_find_node("below", node
->xmlChildrenNode
) != NULL
;
480 parse_find_node("max_horz", node
->xmlChildrenNode
) != NULL
;
482 parse_find_node("max_vert", node
->xmlChildrenNode
) != NULL
;
485 sm_saved_state
= g_slist_prepend(sm_saved_state
, state
);
486 goto session_load_ok
;
489 session_state_free(state
);
493 node
= parse_find_node("window", node
->next
);