]> Dogcows Code - chaz/openbox/blob - openbox/session.c
use the sm_client_id in the client struct
[chaz/openbox] / openbox / session.c
1 /* This session code is largely inspired by metacity code. */
2
3 #ifndef USE_SM
4
5 #include "session.h"
6 #include "client.h"
7
8 GList *session_saved_state;
9
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) {}
15
16 #else
17
18 #include "debug.h"
19 #include "openbox.h"
20 #include "session.h"
21 #include "client.h"
22 #include "prop.h"
23 #include "gettext.h"
24 #include "parser/parse.h"
25
26 #include <time.h>
27 #include <errno.h>
28 #include <stdio.h>
29
30 #ifdef HAVE_UNISTD_H
31 # include <sys/types.h>
32 # include <unistd.h>
33 #endif
34
35 #include <X11/SM/SMlib.h>
36
37 GList *session_saved_state;
38
39 static gboolean sm_disable;
40 static SmcConn sm_conn;
41 static gchar *save_file;
42 static gchar *sm_id;
43 static gint sm_argc;
44 static gchar **sm_argv;
45
46 static void session_load(char *path);
47 static gboolean session_save();
48
49 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
50 Bool shutdown, int interact_style, Bool fast);
51 static void sm_die(SmcConn conn, SmPointer data);
52 static void sm_save_complete(SmcConn conn, SmPointer data);
53 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data);
54
55 static void save_commands()
56 {
57 SmProp *props[2];
58 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
59 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
60 gint i;
61
62 prop_cmd.vals = g_new(SmPropValue, sm_argc);
63 prop_cmd.num_vals = sm_argc;
64 for (i = 0; i < sm_argc; ++i) {
65 prop_cmd.vals[i].value = sm_argv[i];
66 prop_cmd.vals[i].length = strlen(sm_argv[i]);
67 }
68
69 prop_res.vals = g_new(SmPropValue, sm_argc + 2);
70 prop_res.num_vals = sm_argc + 2;
71 for (i = 0; i < sm_argc; ++i) {
72 prop_res.vals[i].value = sm_argv[i];
73 prop_res.vals[i].length = strlen(sm_argv[i]);
74 }
75
76 if (save_file) {
77 prop_res.vals[i].value = "--sm-save-file";
78 prop_res.vals[i++].length = strlen("--sm-save-file");
79 prop_res.vals[i].value = save_file;
80 prop_res.vals[i++].length = strlen(save_file);
81 } else {
82 prop_res.vals[i].value = "--sm-client-id";
83 prop_res.vals[i++].length = strlen("--sm-client-id");
84 prop_res.vals[i].value = sm_id;
85 prop_res.vals[i++].length = strlen(sm_id);
86 }
87
88 props[0] = &prop_res;
89 props[1] = &prop_cmd;
90 SmcSetProperties(sm_conn, 2, props);
91
92 g_free(prop_res.vals);
93 g_free(prop_cmd.vals);
94 }
95
96 static void remove_two_args(int *argc, char ***argv, int index)
97 {
98 int i;
99
100 for (i = index; i < index + 2; ++i)
101 (*argv)[i] = (*argv)[i+2];
102 *argc -= 2;
103 }
104
105 static void parse_args(int *argc, char ***argv)
106 {
107 int i;
108
109 for (i = 1; i < *argc; ++i) {
110 if (!strcmp((*argv)[i], "--sm-client-id")) {
111 if (i == *argc - 1) /* no args left */
112 g_printerr(_("--sm-client-id requires an argument\n"));
113 else {
114 sm_id = g_strdup((*argv)[i+1]);
115 remove_two_args(argc, argv, i);
116 ++i;
117 }
118 } else if (!strcmp((*argv)[i], "--sm-save-file")) {
119 if (i == *argc - 1) /* no args left */
120 g_printerr(_("--sm-save-file requires an argument\n"));
121 else {
122 save_file = g_strdup((*argv)[i+1]);
123 remove_two_args(argc, argv, i);
124 ++i;
125 }
126 } else if (!strcmp((*argv)[i], "--sm-disable")) {
127 sm_disable = TRUE;
128 }
129 }
130 }
131
132 void session_startup(int *argc, char ***argv)
133 {
134 #define SM_ERR_LEN 1024
135
136 SmcCallbacks cb;
137 char sm_err[SM_ERR_LEN];
138
139 parse_args(argc, argv);
140
141 if (sm_disable)
142 return;
143
144 if (save_file)
145 session_load(save_file);
146
147 sm_argc = *argc;
148 sm_argv = *argv;
149
150 cb.save_yourself.callback = sm_save_yourself;
151 cb.save_yourself.client_data = NULL;
152
153 cb.die.callback = sm_die;
154 cb.die.client_data = NULL;
155
156 cb.save_complete.callback = sm_save_complete;
157 cb.save_complete.client_data = NULL;
158
159 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
160 cb.shutdown_cancelled.client_data = NULL;
161
162 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
163 SmcSaveYourselfProcMask |
164 SmcDieProcMask |
165 SmcSaveCompleteProcMask |
166 SmcShutdownCancelledProcMask,
167 &cb, sm_id, &sm_id,
168 SM_ERR_LEN, sm_err);
169 if (sm_conn == NULL)
170 g_warning("Failed to connect to session manager: %s", sm_err);
171 else {
172 SmPropValue val_prog;
173 SmPropValue val_uid;
174 SmPropValue val_hint;
175 SmPropValue val_pri;
176 SmPropValue val_pid;
177 SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
178 SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
179 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
180 SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
181 SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
182 SmProp *props[6];
183 gchar hint, pri;
184 gchar pid[32];
185
186 val_prog.value = sm_argv[0];
187 val_prog.length = strlen(sm_argv[0]);
188
189 val_uid.value = g_strdup(g_get_user_name());
190 val_uid.length = strlen(val_uid.value);
191
192 hint = SmRestartImmediately;
193 val_hint.value = &hint;
194 val_hint.length = 1;
195
196 sprintf(pid, "%ld", (long)getpid());
197 val_pid.value = pid;
198 val_pid.length = strlen(pid);
199
200 /* priority with gnome-session-manager, low to run before other apps */
201 pri = 20;
202 val_pri.value = &pri;
203 val_pri.length = 1;
204
205 prop_prog.vals = &val_prog;
206 prop_uid.vals = &val_uid;
207 prop_hint.vals = &val_hint;
208 prop_pid.vals = &val_pid;
209 prop_pri.vals = &val_pri;
210
211 props[0] = &prop_prog;
212 props[1] = &prop_uid;
213 props[2] = &prop_hint;
214 props[3] = &prop_pid;
215 props[4] = &prop_pri;
216
217 SmcSetProperties(sm_conn, 5, props);
218
219 g_free(val_uid.value);
220
221 save_commands();
222 }
223 }
224
225 void session_shutdown()
226 {
227 g_free(save_file);
228 g_free(sm_id);
229
230 if (sm_conn) {
231 SmPropValue val_hint;
232 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
233 SmProp *props[1];
234 gulong hint;
235
236 /* when we exit, we want to reset this to a more friendly state */
237 hint = SmRestartIfRunning;
238 val_hint.value = &hint;
239 val_hint.length = 1;
240
241 prop_hint.vals = &val_hint;
242
243 props[0] = &prop_hint;
244
245 SmcSetProperties(sm_conn, 1, props);
246
247 SmcCloseConnection(sm_conn, 0, NULL);
248
249 while (session_saved_state) {
250 session_state_free(session_saved_state->data);
251 session_saved_state = g_list_delete_link(session_saved_state,
252 session_saved_state);
253 }
254 }
255 }
256
257 static void sm_save_yourself_phase2(SmcConn conn, SmPointer data)
258 {
259 gboolean success;
260
261 success = session_save();
262 save_commands();
263
264 SmcSaveYourselfDone(conn, success);
265 }
266
267 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
268 Bool shutdown, int interact_style, Bool fast)
269 {
270 if (!SmcRequestSaveYourselfPhase2(conn, sm_save_yourself_phase2, data)) {
271 ob_debug("SAVE YOURSELF PHASE 2 failed\n");
272 SmcSaveYourselfDone(conn, FALSE);
273 }
274 }
275
276 static void sm_die(SmcConn conn, SmPointer data)
277 {
278 ob_exit();
279 }
280
281 static void sm_save_complete(SmcConn conn, SmPointer data)
282 {
283 }
284
285 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
286 {
287 }
288
289 static gboolean session_save()
290 {
291 gchar *filename;
292 FILE *f;
293 GList *it;
294 gboolean success = TRUE;
295
296 /* this algo is from metacity */
297 filename = g_strdup_printf("%d-%d-%u.obs",
298 (int) time(NULL),
299 (int) getpid(),
300 g_random_int());
301 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
302 filename, NULL);
303 g_free(filename);
304
305 f = fopen(save_file, "w");
306 if (!f) {
307 success = FALSE;
308 g_warning("unable to save the session to %s: %s",
309 save_file, strerror(errno));
310 } else {
311 guint stack_pos = 0;
312
313 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
314 fprintf(f, "<openbox_session id=\"%s\">\n\n", sm_id);
315
316 for (it = stacking_list; it; it = g_list_next(it)) {
317 guint num;
318 gint32 *dimensions;
319 gint prex, prey, prew, preh;
320 ObClient *c;
321 gchar *t;
322
323 if (WINDOW_IS_CLIENT(it->data))
324 c = WINDOW_AS_CLIENT(it->data);
325 else
326 continue;
327
328 if (!client_normal(c))
329 continue;
330
331 if (!c->sm_client_id)
332 continue;
333
334 prex = c->area.x;
335 prey = c->area.y;
336 prew = c->area.width;
337 preh = c->area.height;
338 if (PROP_GETA32(c->window, openbox_premax, cardinal,
339 (guint32**)&dimensions, &num)) {
340 if (num == 4) {
341 prex = dimensions[0];
342 prey = dimensions[1];
343 prew = dimensions[2];
344 preh = dimensions[3];
345 }
346 g_free(dimensions);
347 }
348
349 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
350
351 t = g_markup_escape_text(c->name, -1);
352 fprintf(f, "\t<name>%s</name>\n", t);
353 g_free(t);
354
355 t = g_markup_escape_text(c->class, -1);
356 fprintf(f, "\t<class>%s</class>\n", t);
357 g_free(t);
358
359 t = g_markup_escape_text(c->role, -1);
360 fprintf(f, "\t<role>%s</role>\n", t);
361 g_free(t);
362
363 fprintf(f, "\t<desktop>%d</desktop>\n", c->desktop);
364 fprintf(f, "\t<stacking>%d</stacking>\n", stack_pos);
365 fprintf(f, "\t<x>%d</x>\n", prex);
366 fprintf(f, "\t<y>%d</y>\n", prey);
367 fprintf(f, "\t<width>%d</width>\n", prew);
368 fprintf(f, "\t<height>%d</height>\n", preh);
369 if (c->shaded)
370 fprintf(f, "\t<shaded />\n");
371 if (c->iconic)
372 fprintf(f, "\t<iconic />\n");
373 if (c->skip_pager)
374 fprintf(f, "\t<skip_pager />\n");
375 if (c->skip_taskbar)
376 fprintf(f, "\t<skip_taskbar />\n");
377 if (c->fullscreen)
378 fprintf(f, "\t<fullscreen />\n");
379 if (c->above)
380 fprintf(f, "\t<above />\n");
381 if (c->below)
382 fprintf(f, "\t<below />\n");
383 if (c->max_horz)
384 fprintf(f, "\t<max_horz />\n");
385 if (c->max_vert)
386 fprintf(f, "\t<max_vert />\n");
387 fprintf(f, "</window>\n\n");
388
389 ++stack_pos;
390 }
391
392 fprintf(f, "</openbox_session>\n");
393
394 if (fflush(f)) {
395 success = FALSE;
396 g_warning("error while saving the session to %s: %s",
397 save_file, strerror(errno));
398 }
399 fclose(f);
400 }
401
402 return success;
403 }
404
405 void session_state_free(ObSessionState *state)
406 {
407 if (state) {
408 g_free(state->id);
409 g_free(state->name);
410 g_free(state->class);
411 g_free(state->role);
412
413 g_free(state);
414 }
415 }
416
417 gboolean session_state_cmp(ObSessionState *s, ObClient *c)
418 {
419 return (c->sm_client_id &&
420 !strcmp(s->id, c->sm_client_id) &&
421 !strcmp(s->name, c->name) &&
422 !strcmp(s->class, c->class) &&
423 !strcmp(s->role, c->role));
424 }
425
426 GList* session_state_find(ObClient *c)
427 {
428 GList *it;
429
430 for (it = session_saved_state; it; it = g_list_next(it)) {
431 ObSessionState *s = it->data;
432 if (!s->matched && session_state_cmp(s, c)) {
433 s->matched = TRUE;
434 break;
435 }
436 }
437 return it;
438 }
439
440 static gint stack_sort(const ObSessionState *s1, const ObSessionState *s2)
441 {
442 return s1->stacking - s2->stacking;
443 }
444
445 static void session_load(char *path)
446 {
447 xmlDocPtr doc;
448 xmlNodePtr node, n;
449 gchar *id;
450
451 if (!parse_load(path, "openbox_session", &doc, &node))
452 return;
453
454 if (!parse_attr_string("id", node, &id))
455 return;
456 g_free(sm_id);
457 sm_id = id;
458
459 node = parse_find_node("window", node->children);
460 while (node) {
461 ObSessionState *state;
462
463 state = g_new0(ObSessionState, 1);
464
465 if (!parse_attr_string("id", node, &state->id))
466 goto session_load_bail;
467 if (!(n = parse_find_node("name", node->children)))
468 goto session_load_bail;
469 state->name = parse_string(doc, n);
470 if (!(n = parse_find_node("class", node->children)))
471 goto session_load_bail;
472 state->class = parse_string(doc, n);
473 if (!(n = parse_find_node("role", node->children)))
474 goto session_load_bail;
475 state->role = parse_string(doc, n);
476 if (!(n = parse_find_node("stacking", node->children)))
477 goto session_load_bail;
478 state->stacking = parse_int(doc, n);
479 if (!(n = parse_find_node("desktop", node->children)))
480 goto session_load_bail;
481 state->desktop = parse_int(doc, n);
482 if (!(n = parse_find_node("x", node->children)))
483 goto session_load_bail;
484 state->x = parse_int(doc, n);
485 if (!(n = parse_find_node("y", node->children)))
486 goto session_load_bail;
487 state->y = parse_int(doc, n);
488 if (!(n = parse_find_node("width", node->children)))
489 goto session_load_bail;
490 state->w = parse_int(doc, n);
491 if (!(n = parse_find_node("height", node->children)))
492 goto session_load_bail;
493 state->h = parse_int(doc, n);
494
495 state->shaded =
496 parse_find_node("shaded", node->children) != NULL;
497 state->iconic =
498 parse_find_node("iconic", node->children) != NULL;
499 state->skip_pager =
500 parse_find_node("skip_pager", node->children) != NULL;
501 state->skip_taskbar =
502 parse_find_node("skip_taskbar", node->children) != NULL;
503 state->fullscreen =
504 parse_find_node("fullscreen", node->children) != NULL;
505 state->above =
506 parse_find_node("above", node->children) != NULL;
507 state->below =
508 parse_find_node("below", node->children) != NULL;
509 state->max_horz =
510 parse_find_node("max_horz", node->children) != NULL;
511 state->max_vert =
512 parse_find_node("max_vert", node->children) != NULL;
513
514 /* save this */
515 session_saved_state = g_list_prepend(session_saved_state, state);
516 goto session_load_ok;
517
518 session_load_bail:
519 session_state_free(state);
520
521 session_load_ok:
522
523 node = parse_find_node("window", node->next);
524 }
525
526 /* sort them by their stacking order */
527 session_saved_state = g_list_sort(session_saved_state,
528 (GCompareFunc)stack_sort);
529
530 xmlFreeDoc(doc);
531 }
532
533 #endif
This page took 0.058185 seconds and 5 git commands to generate.