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
);
200 void session_shutdown()
205 SmPropValue val_hint
;
206 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
210 /* when we exit, we want to reset this to a more friendly state */
211 hint
= SmRestartIfRunning
;
212 val_hint
.value
= &hint
;
215 prop_hint
.vals
= &val_hint
;
217 props
[0] = &prop_hint
;
219 SmcSetProperties(sm_conn
, 1, props
);
221 SmcCloseConnection(sm_conn
, 0, NULL
);
223 while (session_saved_state
) {
224 session_state_free(session_saved_state
->data
);
225 session_saved_state
= g_list_delete_link(session_saved_state
,
226 session_saved_state
);
231 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
235 success
= session_save();
238 SmcSaveYourselfDone(conn
, success
);
241 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
242 Bool shutdown
, int interact_style
, Bool fast
)
244 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
245 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
246 SmcSaveYourselfDone(conn
, FALSE
);
250 static void sm_die(SmcConn conn
, SmPointer data
)
255 static void sm_save_complete(SmcConn conn
, SmPointer data
)
259 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
263 static gboolean
session_save()
268 gboolean success
= TRUE
;
270 /* this algo is from metacity */
271 filename
= g_strdup_printf("%d-%d-%u.obs",
275 save_file
= g_build_filename(g_get_home_dir(), ".openbox", "sessions",
279 f
= fopen(save_file
, "w");
282 g_warning("unable to save the session to %s: %s",
283 save_file
, strerror(errno
));
287 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
288 fprintf(f
, "<openbox_session id=\"%s\">\n\n", ob_sm_id
);
290 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
293 gint prex
, prey
, prew
, preh
;
295 gchar
*client_id
, *t
;
297 if (WINDOW_IS_CLIENT(it
->data
))
298 c
= WINDOW_AS_CLIENT(it
->data
);
302 if (!client_normal(c
))
305 if (!(client_id
= client_get_sm_client_id(c
)))
310 prew
= c
->area
.width
;
311 preh
= c
->area
.height
;
312 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
313 (guint32
**)&dimensions
, &num
)) {
315 prex
= dimensions
[0];
316 prey
= dimensions
[1];
317 prew
= dimensions
[2];
318 preh
= dimensions
[3];
323 fprintf(f
, "<window id=\"%s\">\n", client_id
);
325 t
= g_markup_escape_text(c
->name
, -1);
326 fprintf(f
, "\t<name>%s</name>\n", t
);
329 t
= g_markup_escape_text(c
->class, -1);
330 fprintf(f
, "\t<class>%s</class>\n", t
);
333 t
= g_markup_escape_text(c
->role
, -1);
334 fprintf(f
, "\t<role>%s</role>\n", t
);
337 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
338 fprintf(f
, "\t<stacking>%d</stacking>\n", stack_pos
);
339 fprintf(f
, "\t<x>%d</x>\n", prex
);
340 fprintf(f
, "\t<y>%d</y>\n", prey
);
341 fprintf(f
, "\t<width>%d</width>\n", prew
);
342 fprintf(f
, "\t<height>%d</height>\n", preh
);
344 fprintf(f
, "\t<shaded />\n");
346 fprintf(f
, "\t<iconic />\n");
348 fprintf(f
, "\t<skip_pager />\n");
350 fprintf(f
, "\t<skip_taskbar />\n");
352 fprintf(f
, "\t<fullscreen />\n");
354 fprintf(f
, "\t<above />\n");
356 fprintf(f
, "\t<below />\n");
358 fprintf(f
, "\t<max_horz />\n");
360 fprintf(f
, "\t<max_vert />\n");
361 fprintf(f
, "</window>\n\n");
368 fprintf(f
, "</openbox_session>\n");
372 g_warning("error while saving the session to %s: %s",
373 save_file
, strerror(errno
));
381 void session_state_free(ObSessionState
*state
)
386 g_free(state
->class);
393 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
397 if (!(client_id
= client_get_sm_client_id(c
)))
399 if (strcmp(s
->id
, client_id
)) {
404 if (strcmp(s
->name
, c
->name
))
406 if (strcmp(s
->class, c
->class))
408 if (strcmp(s
->role
, c
->role
))
413 GList
* session_state_find(ObClient
*c
)
417 for (it
= session_saved_state
; it
; it
= g_list_next(it
)) {
418 ObSessionState
*s
= it
->data
;
419 if (!s
->matched
&& session_state_cmp(s
, c
)) {
427 static gint
stack_sort(const ObSessionState
*s1
, const ObSessionState
*s2
)
429 return s1
->stacking
- s2
->stacking
;
432 void session_load(char *path
)
438 if (!parse_load(path
, "openbox_session", &doc
, &node
))
441 if (!parse_attr_string("id", node
, &sm_id
))
443 ob_sm_id
= g_strdup(sm_id
);
445 node
= parse_find_node("window", node
->xmlChildrenNode
);
447 ObSessionState
*state
;
449 state
= g_new0(ObSessionState
, 1);
451 if (!parse_attr_string("id", node
, &state
->id
))
452 goto session_load_bail
;
453 if (!(n
= parse_find_node("name", node
->xmlChildrenNode
)))
454 goto session_load_bail
;
455 state
->name
= parse_string(doc
, n
);
456 if (!(n
= parse_find_node("class", node
->xmlChildrenNode
)))
457 goto session_load_bail
;
458 state
->class = parse_string(doc
, n
);
459 if (!(n
= parse_find_node("role", node
->xmlChildrenNode
)))
460 goto session_load_bail
;
461 state
->role
= parse_string(doc
, n
);
462 if (!(n
= parse_find_node("stacking", node
->xmlChildrenNode
)))
463 goto session_load_bail
;
464 state
->stacking
= parse_int(doc
, n
);
465 if (!(n
= parse_find_node("desktop", node
->xmlChildrenNode
)))
466 goto session_load_bail
;
467 state
->desktop
= parse_int(doc
, n
);
468 if (!(n
= parse_find_node("x", node
->xmlChildrenNode
)))
469 goto session_load_bail
;
470 state
->x
= parse_int(doc
, n
);
471 if (!(n
= parse_find_node("y", node
->xmlChildrenNode
)))
472 goto session_load_bail
;
473 state
->y
= parse_int(doc
, n
);
474 if (!(n
= parse_find_node("width", node
->xmlChildrenNode
)))
475 goto session_load_bail
;
476 state
->w
= parse_int(doc
, n
);
477 if (!(n
= parse_find_node("height", node
->xmlChildrenNode
)))
478 goto session_load_bail
;
479 state
->h
= parse_int(doc
, n
);
482 parse_find_node("shaded", node
->xmlChildrenNode
) != NULL
;
484 parse_find_node("iconic", node
->xmlChildrenNode
) != NULL
;
486 parse_find_node("skip_pager", node
->xmlChildrenNode
) != NULL
;
487 state
->skip_taskbar
=
488 parse_find_node("skip_taskbar", node
->xmlChildrenNode
) != NULL
;
490 parse_find_node("fullscreen", node
->xmlChildrenNode
) != NULL
;
492 parse_find_node("above", node
->xmlChildrenNode
) != NULL
;
494 parse_find_node("below", node
->xmlChildrenNode
) != NULL
;
496 parse_find_node("max_horz", node
->xmlChildrenNode
) != NULL
;
498 parse_find_node("max_vert", node
->xmlChildrenNode
) != NULL
;
501 session_saved_state
= g_list_prepend(session_saved_state
, state
);
502 goto session_load_ok
;
505 session_state_free(state
);
509 node
= parse_find_node("window", node
->next
);
512 /* sort them by their stacking order */
513 session_saved_state
= g_list_sort(session_saved_state
,
514 (GCompareFunc
)stack_sort
);