1 /* This session code is largely inspired by metacity code. */
8 GList
*session_saved_state
;
10 void session_load(char *path
) {}
11 void session_startup(int argc
, char **argv
) {}
12 void session_shutdown() {}
13 GList
* session_state_find(ObClient
*c
) { return NULL
; }
14 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
) { return FALSE
; }
15 void session_state_free(ObSessionState
*state
) {}
24 #include "parser/parse.h"
31 # include <sys/types.h>
35 #include <X11/SM/SMlib.h>
37 GList
*session_saved_state
;
39 static SmcConn sm_conn
;
40 static gchar
*save_file
;
42 static gchar
**sm_argv
;
44 static gboolean
session_save();
46 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
47 Bool shutdown
, int interact_style
, Bool fast
);
48 static void sm_die(SmcConn conn
, SmPointer data
);
49 static void sm_save_complete(SmcConn conn
, SmPointer data
);
50 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
52 static void save_commands()
55 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
56 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
58 gboolean has_id
= FALSE
, has_file
= FALSE
;
60 for (i
= 1; !has_id
&& !has_file
&& i
< sm_argc
- 1; ++i
) {
61 if (!has_id
&& strcmp(sm_argv
[i
], "--sm-client-id") == 0)
63 if (!has_file
&& strcmp(sm_argv
[i
], "--sm-save-file") == 0)
67 n
= (has_file
? sm_argc
-2 : sm_argc
);
68 n
= (has_id
? n
-2 : n
);
69 prop_cmd
.vals
= g_new(SmPropValue
, n
);
70 prop_cmd
.num_vals
= n
;
71 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
72 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
73 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
74 ++i
, --j
; /* skip the next as well, keep j where it is */
76 prop_cmd
.vals
[j
].value
= sm_argv
[i
];
77 prop_cmd
.vals
[j
].length
= strlen(sm_argv
[i
]);
81 n
= (has_file
? sm_argc
: sm_argc
+2);
82 n
= (has_id
? n
-2 : n
);
83 prop_res
.vals
= g_new(SmPropValue
, n
);
84 prop_res
.num_vals
= n
;
85 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
86 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
87 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
88 ++i
, --j
; /* skip the next as well, keep j where it is */
90 prop_res
.vals
[j
].value
= sm_argv
[i
];
91 prop_res
.vals
[j
].length
= strlen(sm_argv
[i
]);
96 prop_res
.vals
[j
].value
= "--sm-save-file";
97 prop_res
.vals
[j
++].length
= strlen("--sm-save-file");
98 prop_res
.vals
[j
].value
= save_file
;
99 prop_res
.vals
[j
++].length
= strlen(save_file
);
101 prop_res
.vals
[j
].value
= "--sm-client-id";
102 prop_res
.vals
[j
++].length
= strlen("--sm-client-id");
103 prop_res
.vals
[j
].value
= ob_sm_id
;
104 prop_res
.vals
[j
++].length
= strlen(ob_sm_id
);
107 props
[0] = &prop_res
;
108 props
[1] = &prop_cmd
;
109 SmcSetProperties(sm_conn
, 2, props
);
111 g_free(prop_res
.vals
);
112 g_free(prop_cmd
.vals
);
115 void session_startup(int argc
, char **argv
)
117 #define SM_ERR_LEN 1024
120 char sm_err
[SM_ERR_LEN
];
125 cb
.save_yourself
.callback
= sm_save_yourself
;
126 cb
.save_yourself
.client_data
= NULL
;
128 cb
.die
.callback
= sm_die
;
129 cb
.die
.client_data
= NULL
;
131 cb
.save_complete
.callback
= sm_save_complete
;
132 cb
.save_complete
.client_data
= NULL
;
134 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
135 cb
.shutdown_cancelled
.client_data
= NULL
;
137 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
138 SmcSaveYourselfProcMask
|
140 SmcSaveCompleteProcMask
|
141 SmcShutdownCancelledProcMask
,
142 &cb
, ob_sm_id
, &ob_sm_id
,
145 g_warning("Failed to connect to session manager: %s", sm_err
);
147 SmPropValue val_prog
;
149 SmPropValue val_hint
;
152 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
153 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
154 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
155 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
156 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
161 val_prog
.value
= argv
[0];
162 val_prog
.length
= strlen(argv
[0]);
164 val_uid
.value
= g_strdup(g_get_user_name());
165 val_uid
.length
= strlen(val_uid
.value
);
167 hint
= SmRestartImmediately
;
168 val_hint
.value
= &hint
;
171 sprintf(pid
, "%ld", (long)getpid());
173 val_pid
.length
= strlen(pid
);
175 /* priority with gnome-session-manager, low to run before other apps */
177 val_pri
.value
= &pri
;
180 prop_prog
.vals
= &val_prog
;
181 prop_uid
.vals
= &val_uid
;
182 prop_hint
.vals
= &val_hint
;
183 prop_pid
.vals
= &val_pid
;
184 prop_pri
.vals
= &val_pri
;
186 props
[0] = &prop_prog
;
187 props
[1] = &prop_uid
;
188 props
[2] = &prop_hint
;
189 props
[3] = &prop_pid
;
190 props
[4] = &prop_pri
;
192 SmcSetProperties(sm_conn
, 5, props
);
194 g_free(val_uid
.value
);
198 ob_debug("Connected to session manager with id %s\n", ob_sm_id
);
202 void session_shutdown()
207 SmPropValue val_hint
;
208 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
212 /* when we exit, we want to reset this to a more friendly state */
213 hint
= SmRestartIfRunning
;
214 val_hint
.value
= &hint
;
217 prop_hint
.vals
= &val_hint
;
219 props
[0] = &prop_hint
;
221 SmcSetProperties(sm_conn
, 1, props
);
223 SmcCloseConnection(sm_conn
, 0, NULL
);
225 while (session_saved_state
) {
226 session_state_free(session_saved_state
->data
);
227 session_saved_state
= g_list_delete_link(session_saved_state
,
228 session_saved_state
);
233 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
237 ob_debug("got SAVE YOURSELF PHASE 2 from session manager\n");
239 success
= session_save();
242 SmcSaveYourselfDone(conn
, success
);
245 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
246 Bool shutdown
, int interact_style
, Bool fast
)
248 ob_debug("got SAVE YOURSELF from session manager\n");
250 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
251 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
252 SmcSaveYourselfDone(conn
, FALSE
);
256 static void sm_die(SmcConn conn
, SmPointer data
)
259 ob_debug("got DIE from session manager\n");
262 static void sm_save_complete(SmcConn conn
, SmPointer data
)
264 ob_debug("got SAVE COMPLETE from session manager\n");
267 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
269 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
272 static gboolean
session_save()
277 gboolean success
= TRUE
;
279 /* this algo is from metacity */
280 filename
= g_strdup_printf("%d-%d-%u.obs",
284 save_file
= g_build_filename(g_get_home_dir(), ".openbox", "sessions",
288 f
= fopen(save_file
, "w");
291 g_warning("unable to save the session to %s: %s",
292 save_file
, strerror(errno
));
296 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
297 fprintf(f
, "<openbox_session id=\"%s\">\n\n", ob_sm_id
);
299 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
302 gint prex
, prey
, prew
, preh
;
304 gchar
*client_id
, *t
;
306 if (WINDOW_IS_CLIENT(it
->data
))
307 c
= WINDOW_AS_CLIENT(it
->data
);
311 if (!client_normal(c
))
314 if (!(client_id
= client_get_sm_client_id(c
)))
319 prew
= c
->area
.width
;
320 preh
= c
->area
.height
;
321 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
322 (guint32
**)&dimensions
, &num
)) {
324 prex
= dimensions
[0];
325 prey
= dimensions
[1];
326 prew
= dimensions
[2];
327 preh
= dimensions
[3];
332 fprintf(f
, "<window id=\"%s\">\n", client_id
);
334 t
= g_markup_escape_text(c
->name
, -1);
335 fprintf(f
, "\t<name>%s</name>\n", t
);
338 t
= g_markup_escape_text(c
->class, -1);
339 fprintf(f
, "\t<class>%s</class>\n", t
);
342 t
= g_markup_escape_text(c
->role
, -1);
343 fprintf(f
, "\t<role>%s</role>\n", t
);
346 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
347 fprintf(f
, "\t<stacking>%d</stacking>\n", stack_pos
);
348 fprintf(f
, "\t<x>%d</x>\n", prex
);
349 fprintf(f
, "\t<y>%d</y>\n", prey
);
350 fprintf(f
, "\t<width>%d</width>\n", prew
);
351 fprintf(f
, "\t<height>%d</height>\n", preh
);
353 fprintf(f
, "\t<shaded />\n");
355 fprintf(f
, "\t<iconic />\n");
357 fprintf(f
, "\t<skip_pager />\n");
359 fprintf(f
, "\t<skip_taskbar />\n");
361 fprintf(f
, "\t<fullscreen />\n");
363 fprintf(f
, "\t<above />\n");
365 fprintf(f
, "\t<below />\n");
367 fprintf(f
, "\t<max_horz />\n");
369 fprintf(f
, "\t<max_vert />\n");
370 fprintf(f
, "</window>\n\n");
377 fprintf(f
, "</openbox_session>\n");
381 g_warning("error while saving the session to %s: %s",
382 save_file
, strerror(errno
));
390 void session_state_free(ObSessionState
*state
)
395 g_free(state
->class);
402 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
406 if (!(client_id
= client_get_sm_client_id(c
)))
408 if (strcmp(s
->id
, client_id
)) {
413 if (strcmp(s
->name
, c
->name
))
415 if (strcmp(s
->class, c
->class))
417 if (strcmp(s
->role
, c
->role
))
422 GList
* session_state_find(ObClient
*c
)
426 for (it
= session_saved_state
; it
; it
= g_list_next(it
)) {
427 ObSessionState
*s
= it
->data
;
428 if (!s
->matched
&& session_state_cmp(s
, c
)) {
436 static gint
stack_sort(const ObSessionState
*s1
, const ObSessionState
*s2
)
438 return s1
->stacking
- s2
->stacking
;
441 void session_load(char *path
)
447 if (!parse_load(path
, "openbox_session", &doc
, &node
))
450 if (!parse_attr_string("id", node
, &sm_id
))
452 ob_sm_id
= g_strdup(sm_id
);
454 node
= parse_find_node("window", node
->xmlChildrenNode
);
456 ObSessionState
*state
;
458 state
= g_new0(ObSessionState
, 1);
460 if (!parse_attr_string("id", node
, &state
->id
))
461 goto session_load_bail
;
462 if (!(n
= parse_find_node("name", node
->xmlChildrenNode
)))
463 goto session_load_bail
;
464 state
->name
= parse_string(doc
, n
);
465 if (!(n
= parse_find_node("class", node
->xmlChildrenNode
)))
466 goto session_load_bail
;
467 state
->class = parse_string(doc
, n
);
468 if (!(n
= parse_find_node("role", node
->xmlChildrenNode
)))
469 goto session_load_bail
;
470 state
->role
= parse_string(doc
, n
);
471 if (!(n
= parse_find_node("stacking", node
->xmlChildrenNode
)))
472 goto session_load_bail
;
473 state
->stacking
= parse_int(doc
, n
);
474 if (!(n
= parse_find_node("desktop", node
->xmlChildrenNode
)))
475 goto session_load_bail
;
476 state
->desktop
= parse_int(doc
, n
);
477 if (!(n
= parse_find_node("x", node
->xmlChildrenNode
)))
478 goto session_load_bail
;
479 state
->x
= parse_int(doc
, n
);
480 if (!(n
= parse_find_node("y", node
->xmlChildrenNode
)))
481 goto session_load_bail
;
482 state
->y
= parse_int(doc
, n
);
483 if (!(n
= parse_find_node("width", node
->xmlChildrenNode
)))
484 goto session_load_bail
;
485 state
->w
= parse_int(doc
, n
);
486 if (!(n
= parse_find_node("height", node
->xmlChildrenNode
)))
487 goto session_load_bail
;
488 state
->h
= parse_int(doc
, n
);
491 parse_find_node("shaded", node
->xmlChildrenNode
) != NULL
;
493 parse_find_node("iconic", node
->xmlChildrenNode
) != NULL
;
495 parse_find_node("skip_pager", node
->xmlChildrenNode
) != NULL
;
496 state
->skip_taskbar
=
497 parse_find_node("skip_taskbar", node
->xmlChildrenNode
) != NULL
;
499 parse_find_node("fullscreen", node
->xmlChildrenNode
) != NULL
;
501 parse_find_node("above", node
->xmlChildrenNode
) != NULL
;
503 parse_find_node("below", node
->xmlChildrenNode
) != NULL
;
505 parse_find_node("max_horz", node
->xmlChildrenNode
) != NULL
;
507 parse_find_node("max_vert", node
->xmlChildrenNode
) != NULL
;
510 session_saved_state
= g_list_prepend(session_saved_state
, state
);
511 goto session_load_ok
;
514 session_state_free(state
);
518 node
= parse_find_node("window", node
->next
);
521 /* sort them by their stacking order */
522 session_saved_state
= g_list_sort(session_saved_state
,
523 (GCompareFunc
)stack_sort
);