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 GList
*session_saved_state
;
36 static SmcConn sm_conn
;
37 static gchar
*save_file
;
39 static gchar
**sm_argv
;
41 static gboolean
session_save();
43 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
44 Bool shutdown
, int interact_style
, Bool fast
);
45 static void sm_die(SmcConn conn
, SmPointer data
);
46 static void sm_save_complete(SmcConn conn
, SmPointer data
);
47 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
49 static void save_commands()
52 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
53 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
55 gboolean has_id
= FALSE
, has_file
= FALSE
;
57 for (i
= 1; !has_id
&& !has_file
&& i
< sm_argc
- 1; ++i
) {
58 if (!has_id
&& strcmp(sm_argv
[i
], "--sm-client-id") == 0)
60 if (!has_file
&& strcmp(sm_argv
[i
], "--sm-save-file") == 0)
64 n
= (has_file
? sm_argc
-2 : sm_argc
);
65 n
= (has_id
? n
-2 : n
);
66 prop_cmd
.vals
= g_new(SmPropValue
, n
);
67 prop_cmd
.num_vals
= n
;
68 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
69 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
70 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
71 ++i
, --j
; /* skip the next as well, keep j where it is */
73 prop_cmd
.vals
[j
].value
= sm_argv
[i
];
74 prop_cmd
.vals
[j
].length
= strlen(sm_argv
[i
]);
78 n
= (has_file
? sm_argc
: sm_argc
+2);
79 n
= (has_id
? n
-2 : n
);
80 prop_res
.vals
= g_new(SmPropValue
, n
);
81 prop_res
.num_vals
= n
;
82 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
83 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
84 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
85 ++i
, --j
; /* skip the next as well, keep j where it is */
87 prop_res
.vals
[j
].value
= sm_argv
[i
];
88 prop_res
.vals
[j
].length
= strlen(sm_argv
[i
]);
93 prop_res
.vals
[j
].value
= "--sm-save-file";
94 prop_res
.vals
[j
++].length
= strlen("--sm-save-file");
95 prop_res
.vals
[j
].value
= save_file
;
96 prop_res
.vals
[j
++].length
= strlen(save_file
);
98 prop_res
.vals
[j
].value
= "--sm-client-id";
99 prop_res
.vals
[j
++].length
= strlen("--sm-client-id");
100 prop_res
.vals
[j
].value
= ob_sm_id
;
101 prop_res
.vals
[j
++].length
= strlen(ob_sm_id
);
104 props
[0] = &prop_res
;
105 props
[1] = &prop_cmd
;
106 SmcSetProperties(sm_conn
, 2, props
);
108 g_free(prop_res
.vals
);
109 g_free(prop_cmd
.vals
);
112 void session_startup(int argc
, char **argv
)
114 #define SM_ERR_LEN 1024
117 char sm_err
[SM_ERR_LEN
];
122 cb
.save_yourself
.callback
= sm_save_yourself
;
123 cb
.save_yourself
.client_data
= NULL
;
125 cb
.die
.callback
= sm_die
;
126 cb
.die
.client_data
= NULL
;
128 cb
.save_complete
.callback
= sm_save_complete
;
129 cb
.save_complete
.client_data
= NULL
;
131 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
132 cb
.shutdown_cancelled
.client_data
= NULL
;
134 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
135 SmcSaveYourselfProcMask
|
137 SmcSaveCompleteProcMask
|
138 SmcShutdownCancelledProcMask
,
139 &cb
, ob_sm_id
, &ob_sm_id
,
142 g_warning("Failed to connect to session manager: %s", sm_err
);
144 SmPropValue val_prog
;
146 SmPropValue val_hint
;
149 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
150 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
151 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
152 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
153 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
158 val_prog
.value
= argv
[0];
159 val_prog
.length
= strlen(argv
[0]);
161 val_uid
.value
= g_strdup(g_get_user_name());
162 val_uid
.length
= strlen(val_uid
.value
);
164 hint
= SmRestartImmediately
;
165 val_hint
.value
= &hint
;
168 sprintf(pid
, "%ld", (long)getpid());
170 val_pid
.length
= strlen(pid
);
172 /* priority with gnome-session-manager, low to run before other apps */
174 val_pri
.value
= &pri
;
177 prop_prog
.vals
= &val_prog
;
178 prop_uid
.vals
= &val_uid
;
179 prop_hint
.vals
= &val_hint
;
180 prop_pid
.vals
= &val_pid
;
181 prop_pri
.vals
= &val_pri
;
183 props
[0] = &prop_prog
;
184 props
[1] = &prop_uid
;
185 props
[2] = &prop_hint
;
186 props
[3] = &prop_pid
;
187 props
[4] = &prop_pri
;
189 SmcSetProperties(sm_conn
, 5, props
);
191 g_free(val_uid
.value
);
195 ob_debug("Connected to session manager with id %s\n", ob_sm_id
);
199 void session_shutdown()
204 SmPropValue val_hint
;
205 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
209 /* when we exit, we want to reset this to a more friendly state */
210 hint
= SmRestartIfRunning
;
211 val_hint
.value
= &hint
;
214 prop_hint
.vals
= &val_hint
;
216 props
[0] = &prop_hint
;
218 SmcSetProperties(sm_conn
, 1, props
);
220 SmcCloseConnection(sm_conn
, 0, NULL
);
222 while (session_saved_state
) {
223 session_state_free(session_saved_state
->data
);
224 session_saved_state
= g_list_delete_link(session_saved_state
,
225 session_saved_state
);
230 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
234 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
236 success
= session_save();
239 SmcSaveYourselfDone(conn
, success
);
242 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
243 Bool shutdown
, int interact_style
, Bool fast
)
245 ob_debug("got SAVE YOURSELF from session manager\n");
247 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
248 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
249 SmcSaveYourselfDone(conn
, FALSE
);
253 static void sm_die(SmcConn conn
, SmPointer data
)
256 ob_debug("got DIE from session manager\n");
259 static void sm_save_complete(SmcConn conn
, SmPointer data
)
261 ob_debug("got SAVE COMPLETE from session manager\n");
264 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
266 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
269 static gboolean
session_save()
274 gboolean success
= TRUE
;
276 /* this algo is from metacity */
277 filename
= g_strdup_printf("%d-%d-%u.obs",
281 save_file
= g_build_filename(g_get_home_dir(), ".openbox", "sessions",
285 f
= fopen(save_file
, "w");
288 g_warning("unable to save the session to %s: %s",
289 save_file
, strerror(errno
));
293 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
294 fprintf(f
, "<openbox_session id=\"%s\">\n\n", ob_sm_id
);
296 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
299 gint prex
, prey
, prew
, preh
;
301 gchar
*client_id
, *t
;
303 if (WINDOW_IS_CLIENT(it
->data
))
304 c
= WINDOW_AS_CLIENT(it
->data
);
308 if (!client_normal(c
))
311 if (!(client_id
= client_get_sm_client_id(c
)))
316 prew
= c
->area
.width
;
317 preh
= c
->area
.height
;
318 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
319 (guint32
**)&dimensions
, &num
)) {
321 prex
= dimensions
[0];
322 prey
= dimensions
[1];
323 prew
= dimensions
[2];
324 preh
= dimensions
[3];
329 fprintf(f
, "<window id=\"%s\">\n", client_id
);
331 t
= g_markup_escape_text(c
->name
, -1);
332 fprintf(f
, "\t<name>%s</name>\n", t
);
335 t
= g_markup_escape_text(c
->class, -1);
336 fprintf(f
, "\t<class>%s</class>\n", t
);
339 t
= g_markup_escape_text(c
->role
, -1);
340 fprintf(f
, "\t<role>%s</role>\n", t
);
343 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
344 fprintf(f
, "\t<stacking>%d</stacking>\n", stack_pos
);
345 fprintf(f
, "\t<x>%d</x>\n", prex
);
346 fprintf(f
, "\t<y>%d</y>\n", prey
);
347 fprintf(f
, "\t<width>%d</width>\n", prew
);
348 fprintf(f
, "\t<height>%d</height>\n", preh
);
350 fprintf(f
, "\t<shaded />\n");
352 fprintf(f
, "\t<iconic />\n");
354 fprintf(f
, "\t<skip_pager />\n");
356 fprintf(f
, "\t<skip_taskbar />\n");
358 fprintf(f
, "\t<fullscreen />\n");
360 fprintf(f
, "\t<above />\n");
362 fprintf(f
, "\t<below />\n");
364 fprintf(f
, "\t<max_horz />\n");
366 fprintf(f
, "\t<max_vert />\n");
367 fprintf(f
, "</window>\n\n");
374 fprintf(f
, "</openbox_session>\n");
378 g_warning("error while saving the session to %s: %s",
379 save_file
, strerror(errno
));
387 void session_state_free(ObSessionState
*state
)
392 g_free(state
->class);
399 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
403 if (!(client_id
= client_get_sm_client_id(c
)))
405 if (strcmp(s
->id
, client_id
)) {
410 if (strcmp(s
->name
, c
->name
))
412 if (strcmp(s
->class, c
->class))
414 if (strcmp(s
->role
, c
->role
))
419 GList
* session_state_find(ObClient
*c
)
423 for (it
= session_saved_state
; it
; it
= g_list_next(it
)) {
424 ObSessionState
*s
= it
->data
;
425 if (!s
->matched
&& session_state_cmp(s
, c
)) {
433 static gint
stack_sort(const ObSessionState
*s1
, const ObSessionState
*s2
)
435 return s1
->stacking
- s2
->stacking
;
438 void session_load(char *path
)
444 if (!parse_load(path
, "openbox_session", &doc
, &node
))
447 if (!parse_attr_string("id", node
, &sm_id
))
449 ob_sm_id
= g_strdup(sm_id
);
451 node
= parse_find_node("window", node
->xmlChildrenNode
);
453 ObSessionState
*state
;
455 state
= g_new0(ObSessionState
, 1);
457 if (!parse_attr_string("id", node
, &state
->id
))
458 goto session_load_bail
;
459 if (!(n
= parse_find_node("name", node
->xmlChildrenNode
)))
460 goto session_load_bail
;
461 state
->name
= parse_string(doc
, n
);
462 if (!(n
= parse_find_node("class", node
->xmlChildrenNode
)))
463 goto session_load_bail
;
464 state
->class = parse_string(doc
, n
);
465 if (!(n
= parse_find_node("role", node
->xmlChildrenNode
)))
466 goto session_load_bail
;
467 state
->role
= parse_string(doc
, n
);
468 if (!(n
= parse_find_node("stacking", node
->xmlChildrenNode
)))
469 goto session_load_bail
;
470 state
->stacking
= parse_int(doc
, n
);
471 if (!(n
= parse_find_node("desktop", node
->xmlChildrenNode
)))
472 goto session_load_bail
;
473 state
->desktop
= parse_int(doc
, n
);
474 if (!(n
= parse_find_node("x", node
->xmlChildrenNode
)))
475 goto session_load_bail
;
476 state
->x
= parse_int(doc
, n
);
477 if (!(n
= parse_find_node("y", node
->xmlChildrenNode
)))
478 goto session_load_bail
;
479 state
->y
= parse_int(doc
, n
);
480 if (!(n
= parse_find_node("width", node
->xmlChildrenNode
)))
481 goto session_load_bail
;
482 state
->w
= parse_int(doc
, n
);
483 if (!(n
= parse_find_node("height", node
->xmlChildrenNode
)))
484 goto session_load_bail
;
485 state
->h
= parse_int(doc
, n
);
488 parse_find_node("shaded", node
->xmlChildrenNode
) != NULL
;
490 parse_find_node("iconic", node
->xmlChildrenNode
) != NULL
;
492 parse_find_node("skip_pager", node
->xmlChildrenNode
) != NULL
;
493 state
->skip_taskbar
=
494 parse_find_node("skip_taskbar", node
->xmlChildrenNode
) != NULL
;
496 parse_find_node("fullscreen", node
->xmlChildrenNode
) != NULL
;
498 parse_find_node("above", node
->xmlChildrenNode
) != NULL
;
500 parse_find_node("below", node
->xmlChildrenNode
) != NULL
;
502 parse_find_node("max_horz", node
->xmlChildrenNode
) != NULL
;
504 parse_find_node("max_vert", node
->xmlChildrenNode
) != NULL
;
507 session_saved_state
= g_list_prepend(session_saved_state
, state
);
508 goto session_load_ok
;
511 session_state_free(state
);
515 node
= parse_find_node("window", node
->next
);
518 /* sort them by their stacking order */
519 session_saved_state
= g_list_sort(session_saved_state
,
520 (GCompareFunc
)stack_sort
);