1 /* This session code is largely inspired by metacity code. */
8 GList
*session_saved_state
;
10 void session_startup(int *argc
, char ***argv
) {}
11 void session_shutdown() {}
12 GList
* session_state_find(ObClient
*c
) { return NULL
; }
13 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
) { return FALSE
; }
14 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 gboolean sm_disable
;
40 static SmcConn sm_conn
;
41 static gchar
*save_file
;
44 static gchar
**sm_argv
;
45 static gchar
*sm_sessions_path
;
47 static void session_load(char *path
);
48 static gboolean
session_save();
50 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
51 Bool shutdown
, int interact_style
, Bool fast
);
52 static void sm_die(SmcConn conn
, SmPointer data
);
53 static void sm_save_complete(SmcConn conn
, SmPointer data
);
54 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
56 static void save_commands()
59 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
60 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
63 prop_cmd
.vals
= g_new(SmPropValue
, sm_argc
);
64 prop_cmd
.num_vals
= sm_argc
;
65 for (i
= 0; i
< sm_argc
; ++i
) {
66 prop_cmd
.vals
[i
].value
= sm_argv
[i
];
67 prop_cmd
.vals
[i
].length
= strlen(sm_argv
[i
]);
70 prop_res
.vals
= g_new(SmPropValue
, sm_argc
+ 2);
71 prop_res
.num_vals
= sm_argc
+ 2;
72 for (i
= 0; i
< sm_argc
; ++i
) {
73 prop_res
.vals
[i
].value
= sm_argv
[i
];
74 prop_res
.vals
[i
].length
= strlen(sm_argv
[i
]);
78 prop_res
.vals
[i
].value
= "--sm-save-file";
79 prop_res
.vals
[i
++].length
= strlen("--sm-save-file");
80 prop_res
.vals
[i
].value
= save_file
;
81 prop_res
.vals
[i
++].length
= strlen(save_file
);
83 prop_res
.vals
[i
].value
= "--sm-client-id";
84 prop_res
.vals
[i
++].length
= strlen("--sm-client-id");
85 prop_res
.vals
[i
].value
= sm_id
;
86 prop_res
.vals
[i
++].length
= strlen(sm_id
);
91 SmcSetProperties(sm_conn
, 2, props
);
93 g_free(prop_res
.vals
);
94 g_free(prop_cmd
.vals
);
97 static void remove_two_args(int *argc
, char ***argv
, int index
)
101 for (i
= index
; i
< index
+ 2; ++i
)
102 (*argv
)[i
] = (*argv
)[i
+2];
106 static void parse_args(int *argc
, char ***argv
)
110 for (i
= 1; i
< *argc
; ++i
) {
111 if (!strcmp((*argv
)[i
], "--sm-client-id")) {
112 if (i
== *argc
- 1) /* no args left */
113 g_printerr(_("--sm-client-id requires an argument\n"));
115 sm_id
= g_strdup((*argv
)[i
+1]);
116 remove_two_args(argc
, argv
, i
);
119 } else if (!strcmp((*argv
)[i
], "--sm-save-file")) {
120 if (i
== *argc
- 1) /* no args left */
121 g_printerr(_("--sm-save-file requires an argument\n"));
123 save_file
= g_strdup((*argv
)[i
+1]);
124 remove_two_args(argc
, argv
, i
);
127 } else if (!strcmp((*argv
)[i
], "--sm-disable")) {
133 void session_startup(int *argc
, char ***argv
)
135 #define SM_ERR_LEN 1024
138 char sm_err
[SM_ERR_LEN
];
140 parse_args(argc
, argv
);
145 sm_sessions_path
= g_build_filename(parse_xdg_data_home_path(),
146 "openbox", "sessions", NULL
);
147 parse_mkdir_path(sm_sessions_path
, 0700);
150 session_load(save_file
);
155 cb
.save_yourself
.callback
= sm_save_yourself
;
156 cb
.save_yourself
.client_data
= NULL
;
158 cb
.die
.callback
= sm_die
;
159 cb
.die
.client_data
= NULL
;
161 cb
.save_complete
.callback
= sm_save_complete
;
162 cb
.save_complete
.client_data
= NULL
;
164 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
165 cb
.shutdown_cancelled
.client_data
= NULL
;
167 sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
168 SmcSaveYourselfProcMask
|
170 SmcSaveCompleteProcMask
|
171 SmcShutdownCancelledProcMask
,
175 g_warning("Failed to connect to session manager: %s", sm_err
);
177 SmPropValue val_prog
;
179 SmPropValue val_hint
;
182 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
183 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
184 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
185 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
186 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
191 val_prog
.value
= sm_argv
[0];
192 val_prog
.length
= strlen(sm_argv
[0]);
194 val_uid
.value
= g_strdup(g_get_user_name());
195 val_uid
.length
= strlen(val_uid
.value
);
197 hint
= SmRestartImmediately
;
198 val_hint
.value
= &hint
;
201 sprintf(pid
, "%ld", (long)getpid());
203 val_pid
.length
= strlen(pid
);
205 /* priority with gnome-session-manager, low to run before other apps */
207 val_pri
.value
= &pri
;
210 prop_prog
.vals
= &val_prog
;
211 prop_uid
.vals
= &val_uid
;
212 prop_hint
.vals
= &val_hint
;
213 prop_pid
.vals
= &val_pid
;
214 prop_pri
.vals
= &val_pri
;
216 props
[0] = &prop_prog
;
217 props
[1] = &prop_uid
;
218 props
[2] = &prop_hint
;
219 props
[3] = &prop_pid
;
220 props
[4] = &prop_pri
;
222 SmcSetProperties(sm_conn
, 5, props
);
224 g_free(val_uid
.value
);
230 void session_shutdown()
232 g_free(sm_sessions_path
);
237 SmPropValue val_hint
;
238 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
242 /* when we exit, we want to reset this to a more friendly state */
243 hint
= SmRestartIfRunning
;
244 val_hint
.value
= &hint
;
247 prop_hint
.vals
= &val_hint
;
249 props
[0] = &prop_hint
;
251 SmcSetProperties(sm_conn
, 1, props
);
253 SmcCloseConnection(sm_conn
, 0, NULL
);
255 while (session_saved_state
) {
256 session_state_free(session_saved_state
->data
);
257 session_saved_state
= g_list_delete_link(session_saved_state
,
258 session_saved_state
);
263 static void sm_save_yourself_phase2(SmcConn conn
, SmPointer data
)
267 success
= session_save();
270 SmcSaveYourselfDone(conn
, success
);
273 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
274 Bool shutdown
, int interact_style
, Bool fast
)
276 if (!SmcRequestSaveYourselfPhase2(conn
, sm_save_yourself_phase2
, data
)) {
277 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
278 SmcSaveYourselfDone(conn
, FALSE
);
282 static void sm_die(SmcConn conn
, SmPointer data
)
287 static void sm_save_complete(SmcConn conn
, SmPointer data
)
291 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
295 static gboolean
session_save()
300 gboolean success
= TRUE
;
302 /* this algo is from metacity */
303 filename
= g_strdup_printf("%d-%d-%u.obs",
307 save_file
= g_build_filename(sm_sessions_path
, filename
, NULL
);
310 f
= fopen(save_file
, "w");
313 g_warning("unable to save the session to %s: %s",
314 save_file
, strerror(errno
));
318 fprintf(f
, "<?xml version=\"1.0\"?>\n\n");
319 fprintf(f
, "<openbox_session id=\"%s\">\n\n", sm_id
);
321 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
324 gint prex
, prey
, prew
, preh
;
328 if (WINDOW_IS_CLIENT(it
->data
))
329 c
= WINDOW_AS_CLIENT(it
->data
);
333 if (!client_normal(c
))
336 if (!c
->sm_client_id
)
341 prew
= c
->area
.width
;
342 preh
= c
->area
.height
;
343 if (PROP_GETA32(c
->window
, openbox_premax
, cardinal
,
344 (guint32
**)&dimensions
, &num
)) {
346 prex
= dimensions
[0];
347 prey
= dimensions
[1];
348 prew
= dimensions
[2];
349 preh
= dimensions
[3];
354 fprintf(f
, "<window id=\"%s\">\n", c
->sm_client_id
);
356 t
= g_markup_escape_text(c
->name
, -1);
357 fprintf(f
, "\t<name>%s</name>\n", t
);
360 t
= g_markup_escape_text(c
->class, -1);
361 fprintf(f
, "\t<class>%s</class>\n", t
);
364 t
= g_markup_escape_text(c
->role
, -1);
365 fprintf(f
, "\t<role>%s</role>\n", t
);
368 fprintf(f
, "\t<desktop>%d</desktop>\n", c
->desktop
);
369 fprintf(f
, "\t<stacking>%d</stacking>\n", stack_pos
);
370 fprintf(f
, "\t<x>%d</x>\n", prex
);
371 fprintf(f
, "\t<y>%d</y>\n", prey
);
372 fprintf(f
, "\t<width>%d</width>\n", prew
);
373 fprintf(f
, "\t<height>%d</height>\n", preh
);
375 fprintf(f
, "\t<shaded />\n");
377 fprintf(f
, "\t<iconic />\n");
379 fprintf(f
, "\t<skip_pager />\n");
381 fprintf(f
, "\t<skip_taskbar />\n");
383 fprintf(f
, "\t<fullscreen />\n");
385 fprintf(f
, "\t<above />\n");
387 fprintf(f
, "\t<below />\n");
389 fprintf(f
, "\t<max_horz />\n");
391 fprintf(f
, "\t<max_vert />\n");
392 fprintf(f
, "</window>\n\n");
397 fprintf(f
, "</openbox_session>\n");
401 g_warning("error while saving the session to %s: %s",
402 save_file
, strerror(errno
));
410 void session_state_free(ObSessionState
*state
)
415 g_free(state
->class);
422 gboolean
session_state_cmp(ObSessionState
*s
, ObClient
*c
)
424 return (c
->sm_client_id
&&
425 !strcmp(s
->id
, c
->sm_client_id
) &&
426 !strcmp(s
->name
, c
->name
) &&
427 !strcmp(s
->class, c
->class) &&
428 !strcmp(s
->role
, c
->role
));
431 GList
* session_state_find(ObClient
*c
)
435 for (it
= session_saved_state
; it
; it
= g_list_next(it
)) {
436 ObSessionState
*s
= it
->data
;
437 if (!s
->matched
&& session_state_cmp(s
, c
)) {
445 static gint
stack_sort(const ObSessionState
*s1
, const ObSessionState
*s2
)
447 return s1
->stacking
- s2
->stacking
;
450 static void session_load(char *path
)
456 if (!parse_load(path
, "openbox_session", &doc
, &node
))
459 if (!parse_attr_string("id", node
, &id
))
464 node
= parse_find_node("window", node
->children
);
466 ObSessionState
*state
;
468 state
= g_new0(ObSessionState
, 1);
470 if (!parse_attr_string("id", node
, &state
->id
))
471 goto session_load_bail
;
472 if (!(n
= parse_find_node("name", node
->children
)))
473 goto session_load_bail
;
474 state
->name
= parse_string(doc
, n
);
475 if (!(n
= parse_find_node("class", node
->children
)))
476 goto session_load_bail
;
477 state
->class = parse_string(doc
, n
);
478 if (!(n
= parse_find_node("role", node
->children
)))
479 goto session_load_bail
;
480 state
->role
= parse_string(doc
, n
);
481 if (!(n
= parse_find_node("stacking", node
->children
)))
482 goto session_load_bail
;
483 state
->stacking
= parse_int(doc
, n
);
484 if (!(n
= parse_find_node("desktop", node
->children
)))
485 goto session_load_bail
;
486 state
->desktop
= parse_int(doc
, n
);
487 if (!(n
= parse_find_node("x", node
->children
)))
488 goto session_load_bail
;
489 state
->x
= parse_int(doc
, n
);
490 if (!(n
= parse_find_node("y", node
->children
)))
491 goto session_load_bail
;
492 state
->y
= parse_int(doc
, n
);
493 if (!(n
= parse_find_node("width", node
->children
)))
494 goto session_load_bail
;
495 state
->w
= parse_int(doc
, n
);
496 if (!(n
= parse_find_node("height", node
->children
)))
497 goto session_load_bail
;
498 state
->h
= parse_int(doc
, n
);
501 parse_find_node("shaded", node
->children
) != NULL
;
503 parse_find_node("iconic", node
->children
) != NULL
;
505 parse_find_node("skip_pager", node
->children
) != NULL
;
506 state
->skip_taskbar
=
507 parse_find_node("skip_taskbar", node
->children
) != NULL
;
509 parse_find_node("fullscreen", node
->children
) != NULL
;
511 parse_find_node("above", node
->children
) != NULL
;
513 parse_find_node("below", node
->children
) != NULL
;
515 parse_find_node("max_horz", node
->children
) != NULL
;
517 parse_find_node("max_vert", node
->children
) != NULL
;
520 session_saved_state
= g_list_prepend(session_saved_state
, state
);
521 goto session_load_ok
;
524 session_state_free(state
);
528 node
= parse_find_node("window", node
->next
);
531 /* sort them by their stacking order */
532 session_saved_state
= g_list_sort(session_saved_state
,
533 (GCompareFunc
)stack_sort
);