1 /* This session code is largely inspired by metacity code. */
5 void session_load(char *path
) {}
6 void session_startup(int argc
, char **argv
) {}
7 void session_shutdown() {}
16 #include "parser/parse.h"
23 # include <sys/types.h>
27 #include <X11/SM/SMlib.h>
29 static SmcConn sm_conn
;
30 static gchar
*save_file
;
32 static gchar
**sm_argv
;
34 static gboolean
session_save();
36 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
37 Bool shutdown
, int interact_style
, Bool fast
);
38 static void sm_die(SmcConn conn
, SmPointer data
);
39 static void sm_save_complete(SmcConn conn
, SmPointer data
);
40 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
42 static void save_commands()
45 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
46 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
48 gboolean has_id
= FALSE
, has_file
= FALSE
;
50 for (i
= 1; !has_id
&& !has_file
&& i
< sm_argc
- 1; ++i
) {
51 if (!has_id
&& strcmp(sm_argv
[i
], "--sm-client-id") == 0)
53 if (!has_file
&& strcmp(sm_argv
[i
], "--sm-save-file") == 0)
57 n
= (has_file
? sm_argc
-2 : sm_argc
);
58 n
= (has_id
? n
-2 : n
);
59 prop_cmd
.vals
= g_new(SmPropValue
, n
);
60 prop_cmd
.num_vals
= n
;
61 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
62 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
63 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
64 ++i
, --j
; /* skip the next as well, keep j where it is */
66 prop_cmd
.vals
[j
].value
= sm_argv
[i
];
67 prop_cmd
.vals
[j
].length
= strlen(sm_argv
[i
]);
71 n
= (has_file
? sm_argc
: sm_argc
+2);
72 n
= (has_id
? n
-2 : n
);
73 prop_res
.vals
= g_new(SmPropValue
, n
);
74 prop_res
.num_vals
= n
;
75 for (i
= 0, j
= 0; i
< sm_argc
; ++i
, ++j
) {
76 if (strcmp (sm_argv
[i
], "--sm-client-id") == 0 ||
77 strcmp (sm_argv
[i
], "--sm-save-file") == 0) {
78 ++i
, --j
; /* skip the next as well, keep j where it is */
80 prop_res
.vals
[j
].value
= sm_argv
[i
];
81 prop_res
.vals
[j
].length
= strlen(sm_argv
[i
]);
86 prop_res
.vals
[j
].value
= "--sm-save-file";
87 prop_res
.vals
[j
++].length
= strlen("--sm-save-file");
88 prop_res
.vals
[j
].value
= save_file
;
89 prop_res
.vals
[j
++].length
= strlen(save_file
);
91 prop_res
.vals
[j
].value
= "--sm-client-id";
92 prop_res
.vals
[j
++].length
= strlen("--sm-client-id");
93 prop_res
.vals
[j
].value
= ob_sm_id
;
94 prop_res
.vals
[j
++].length
= strlen(ob_sm_id
);
99 SmcSetProperties(sm_conn
, 1, props
);
101 g_free(prop_res
.vals
);
102 g_free(prop_cmd
.vals
);
105 void session_startup(int argc
, char **argv
)
107 #define SM_ERR_LEN 1024
110 char sm_err
[SM_ERR_LEN
];
115 cb
.save_yourself
.callback
= sm_save_yourself
;
116 cb
.save_yourself
.client_data
= NULL
;
118 cb
.die
.callback
= sm_die
;
119 cb
.die
.client_data
= NULL
;
121 cb
.save_complete
.callback
= sm_save_complete
;
122 cb
.save_complete
.client_data
= NULL
;
124 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
125 cb
.shutdown_cancelled
.client_data
= NULL
;
127 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
128 SmcSaveYourselfProcMask
|
130 SmcSaveCompleteProcMask
|
131 SmcShutdownCancelledProcMask
,
132 &cb
, ob_sm_id
, &ob_sm_id
,
135 g_warning("Failed to connect to session manager: %s", sm_err
);
137 SmPropValue val_prog
;
139 SmPropValue val_hint
;
142 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
143 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
144 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
145 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
146 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
151 val_prog
.value
= argv
[0];
152 val_prog
.length
= strlen(argv
[0]);
154 val_uid
.value
= g_strdup(g_get_user_name());
155 val_uid
.length
= strlen(val_uid
.value
);
157 hint
= SmRestartImmediately
;
158 val_hint
.value
= &hint
;
161 sprintf(pid
, "%ld", (long)getpid());
163 val_pid
.length
= strlen(pid
);
165 /* priority with gnome-session-manager, low to run before other apps */
167 val_pri
.value
= &pri
;
170 prop_prog
.vals
= &val_prog
;
171 prop_uid
.vals
= &val_uid
;
172 prop_hint
.vals
= &val_hint
;
173 prop_pid
.vals
= &val_pid
;
174 prop_pri
.vals
= &val_pri
;
176 props
[0] = &prop_prog
;
177 props
[1] = &prop_uid
;
178 props
[2] = &prop_hint
;
179 props
[3] = &prop_pid
;
180 props
[4] = &prop_pri
;
182 SmcSetProperties(sm_conn
, 5, props
);
184 g_free(val_uid
.value
);
188 ob_debug("Connected to session manager with id %s\n", ob_sm_id
);
192 void session_shutdown()
197 SmPropValue val_hint
;
198 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
202 /* when we exit, we want to reset this to a more friendly state */
203 hint
= SmRestartIfRunning
;
204 val_hint
.value
= &hint
;
207 prop_hint
.vals
= &val_hint
;
209 props
[0] = &prop_hint
;
211 SmcSetProperties(sm_conn
, 1, props
);
213 SmcCloseConnection(sm_conn
, 0, NULL
);
217 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
218 Bool shutdown
, int interact_style
, Bool fast
)
222 ob_debug("got SAVE YOURSELF from session manager\n");
224 success
= session_save();
227 SmcSaveYourselfDone(conn
, success
);
230 static void sm_die(SmcConn conn
, SmPointer data
)
233 ob_debug("got DIE from session manager\n");
236 static void sm_save_complete(SmcConn conn
, SmPointer data
)
238 ob_debug("got SAVE COMPLETE from session manager\n");
241 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
243 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
246 static gboolean
session_save()
251 gboolean success
= TRUE
;
253 /* this algo is from metacity */
254 filename
= g_strdup_printf("%d-%d-%u.obs",
258 save_file
= g_build_filename(g_get_home_dir(), ".openbox", "sessions",
262 f
= fopen(save_file
, "w");
265 g_warning("unable to save the session to %s: %s",
266 save_file
, strerror(errno
));
268 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
269 fprintf(f
, "<openbox_session id=\"%s\">\n\n", ob_sm_id
);
271 for (it
= client_list
; it
; it
= g_list_next(it
)) {
274 gint prex
, prey
, prew
, preh
;
275 ObClient
*c
= it
->data
;
277 if (!client_normal(c
))
282 prew
= c
->area
.width
;
283 preh
= c
->area
.height
;
284 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
285 (guint32
**)&dimensions
, &num
)) {
287 prex
= dimensions
[0];
288 prey
= dimensions
[1];
289 prew
= dimensions
[2];
290 preh
= dimensions
[3];
295 fprintf(f
, "<window id=\"%s\">\n",
296 g_markup_escape_text("XXX", -1));
297 fprintf(f
, "\t<name>%s</name>\n",
298 g_markup_escape_text(c
->name
, -1));
299 fprintf(f
, "\t<class>%s</class>\n",
300 g_markup_escape_text(c
->class, -1));
301 fprintf(f
, "\t<role>%s</role>\n",
302 g_markup_escape_text(c
->role
, -1));
303 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
304 fprintf(f
, "\t<x>%d</x>\n", prex
);
305 fprintf(f
, "\t<y>%d</y>\n", prey
);
306 fprintf(f
, "\t<width>%d</width>\n", prew
);
307 fprintf(f
, "\t<height>%d</height>\n", preh
);
309 fprintf(f
, "\t<shaded />\n");
311 fprintf(f
, "\t<iconic />\n");
313 fprintf(f
, "\t<skip_pager />\n");
315 fprintf(f
, "\t<skip_taskbar />\n");
317 fprintf(f
, "\t<fullscreen />\n");
319 fprintf(f
, "\t<above />\n");
321 fprintf(f
, "\t<below />\n");
323 fprintf(f
, "\t<max_horz />\n");
325 fprintf(f
, "\t<max_vert />\n");
326 fprintf(f
, "</window>\n\n");
329 fprintf(f
, "</openbox_session>\n");
333 g_warning("error while saving the session to %s: %s",
334 save_file
, strerror(errno
));
342 void session_load(char *path
)
348 if (!parse_load(path
, "openbox_session", &doc
, &node
))
351 if (!parse_attr_string("id", node
, &sm_id
))
353 ob_sm_id
= g_strdup(sm_id
);
355 node
= parse_find_node("window", node
->xmlChildrenNode
);
357 gchar
*id
, *name
, *class, *role
;
360 gboolean shaded
, iconic
, skip_pager
, skip_taskbar
, fullscreen
;
361 gboolean above
, below
, max_horz
, max_vert
;
363 if (!parse_attr_string("id", node
, &id
))
364 goto session_load_bail
;
365 if (!(n
= parse_find_node("name", node
->xmlChildrenNode
)))
366 goto session_load_bail
;
367 name
= parse_string(doc
, n
);
368 if (!(n
= parse_find_node("class", node
->xmlChildrenNode
)))
369 goto session_load_bail
;
370 class = parse_string(doc
, n
);
371 if (!(n
= parse_find_node("role", node
->xmlChildrenNode
)))
372 goto session_load_bail
;
373 role
= parse_string(doc
, n
);
374 if (!(n
= parse_find_node("desktop", node
->xmlChildrenNode
)))
375 goto session_load_bail
;
376 desktop
= parse_int(doc
, n
);
377 if (!(n
= parse_find_node("x", node
->xmlChildrenNode
)))
378 goto session_load_bail
;
379 x
= parse_int(doc
, n
);
380 if (!(n
= parse_find_node("y", node
->xmlChildrenNode
)))
381 goto session_load_bail
;
382 y
= parse_int(doc
, n
);
383 if (!(n
= parse_find_node("width", node
->xmlChildrenNode
)))
384 goto session_load_bail
;
385 w
= parse_int(doc
, n
);
386 if (!(n
= parse_find_node("height", node
->xmlChildrenNode
)))
387 goto session_load_bail
;
388 h
= parse_int(doc
, n
);
390 shaded
= parse_find_node("shaded", node
->xmlChildrenNode
) != NULL
;
391 iconic
= parse_find_node("iconic", node
->xmlChildrenNode
) != NULL
;
392 skip_pager
= parse_find_node("skip_pager", node
->xmlChildrenNode
)
394 skip_taskbar
= parse_find_node("skip_taskbar", node
->xmlChildrenNode
)
396 fullscreen
= parse_find_node("fullscreen", node
->xmlChildrenNode
)
398 above
= parse_find_node("above", node
->xmlChildrenNode
) != NULL
;
399 below
= parse_find_node("below", node
->xmlChildrenNode
) != NULL
;
400 max_horz
= parse_find_node("max_horz", node
->xmlChildrenNode
) != NULL
;
401 max_vert
= parse_find_node("max_vert", node
->xmlChildrenNode
) != NULL
;
403 g_message("read session window %s", name
);
409 node
= parse_find_node("window", node
->next
);