]> Dogcows Code - chaz/openbox/blob - openbox/session.c
hover theme elements for window titlebar buttons
[chaz/openbox] / openbox / session.c
1 /* This session code is largely inspired by metacity code. */
2
3 #ifndef USE_SM
4
5 void session_load(char *path) {}
6 void session_startup(int argc, char **argv) {}
7 void session_shutdown() {}
8
9 #else
10
11 #include "debug.h"
12 #include "openbox.h"
13 #include "session.h"
14 #include "client.h"
15 #include "prop.h"
16 #include "parser/parse.h"
17
18 #include <time.h>
19 #include <errno.h>
20 #include <stdio.h>
21
22 #ifdef HAVE_UNISTD_H
23 # include <sys/types.h>
24 # include <unistd.h>
25 #endif
26
27 #include <X11/SM/SMlib.h>
28
29 static SmcConn sm_conn;
30 static gchar *save_file;
31 static gint sm_argc;
32 static gchar **sm_argv;
33
34 static gboolean session_save();
35
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);
41
42 static void save_commands()
43 {
44 SmProp *props[2];
45 SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
46 SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
47 gint i, j, n;
48 gboolean has_id = FALSE, has_file = FALSE;
49
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)
52 has_id = TRUE;
53 if (!has_file && strcmp(sm_argv[i], "--sm-save-file") == 0)
54 has_file = TRUE;
55 }
56
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 */
65 } else {
66 prop_cmd.vals[j].value = sm_argv[i];
67 prop_cmd.vals[j].length = strlen(sm_argv[i]);
68 }
69 }
70
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 */
79 } else {
80 prop_res.vals[j].value = sm_argv[i];
81 prop_res.vals[j].length = strlen(sm_argv[i]);
82 }
83 }
84
85 if (save_file) {
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);
90 } else {
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);
95 }
96
97 props[0] = &prop_res;
98 props[1] = &prop_cmd;
99 SmcSetProperties(sm_conn, 2, props);
100
101 g_free(prop_res.vals);
102 g_free(prop_cmd.vals);
103 }
104
105 void session_startup(int argc, char **argv)
106 {
107 #define SM_ERR_LEN 1024
108
109 SmcCallbacks cb;
110 char sm_err[SM_ERR_LEN];
111
112 sm_argc = argc;
113 sm_argv = argv;
114
115 cb.save_yourself.callback = sm_save_yourself;
116 cb.save_yourself.client_data = NULL;
117
118 cb.die.callback = sm_die;
119 cb.die.client_data = NULL;
120
121 cb.save_complete.callback = sm_save_complete;
122 cb.save_complete.client_data = NULL;
123
124 cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
125 cb.shutdown_cancelled.client_data = NULL;
126
127 sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
128 SmcSaveYourselfProcMask |
129 SmcDieProcMask |
130 SmcSaveCompleteProcMask |
131 SmcShutdownCancelledProcMask,
132 &cb, ob_sm_id, &ob_sm_id,
133 SM_ERR_LEN, sm_err);
134 if (sm_conn == NULL)
135 g_warning("Failed to connect to session manager: %s", sm_err);
136 else {
137 SmPropValue val_prog;
138 SmPropValue val_uid;
139 SmPropValue val_hint;
140 SmPropValue val_pri;
141 SmPropValue val_pid;
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, };
147 SmProp *props[6];
148 gchar hint, pri;
149 gchar pid[32];
150
151 val_prog.value = argv[0];
152 val_prog.length = strlen(argv[0]);
153
154 val_uid.value = g_strdup(g_get_user_name());
155 val_uid.length = strlen(val_uid.value);
156
157 hint = SmRestartImmediately;
158 val_hint.value = &hint;
159 val_hint.length = 1;
160
161 sprintf(pid, "%ld", (long)getpid());
162 val_pid.value = pid;
163 val_pid.length = strlen(pid);
164
165 /* priority with gnome-session-manager, low to run before other apps */
166 pri = 20;
167 val_pri.value = &pri;
168 val_pri.length = 1;
169
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;
175
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;
181
182 SmcSetProperties(sm_conn, 5, props);
183
184 g_free(val_uid.value);
185
186 save_commands();
187
188 ob_debug("Connected to session manager with id %s\n", ob_sm_id);
189 }
190 }
191
192 void session_shutdown()
193 {
194 g_free(save_file);
195
196 if (sm_conn) {
197 SmPropValue val_hint;
198 SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
199 SmProp *props[1];
200 gulong hint;
201
202 /* when we exit, we want to reset this to a more friendly state */
203 hint = SmRestartIfRunning;
204 val_hint.value = &hint;
205 val_hint.length = 1;
206
207 prop_hint.vals = &val_hint;
208
209 props[0] = &prop_hint;
210
211 SmcSetProperties(sm_conn, 1, props);
212
213 SmcCloseConnection(sm_conn, 0, NULL);
214 }
215 }
216
217 static void sm_save_yourself(SmcConn conn, SmPointer data, int save_type,
218 Bool shutdown, int interact_style, Bool fast)
219 {
220 gboolean success;
221
222 ob_debug("got SAVE YOURSELF from session manager\n");
223
224 success = session_save();
225 save_commands();
226
227 SmcSaveYourselfDone(conn, success);
228 }
229
230 static void sm_die(SmcConn conn, SmPointer data)
231 {
232 ob_exit();
233 ob_debug("got DIE from session manager\n");
234 }
235
236 static void sm_save_complete(SmcConn conn, SmPointer data)
237 {
238 ob_debug("got SAVE COMPLETE from session manager\n");
239 }
240
241 static void sm_shutdown_cancelled(SmcConn conn, SmPointer data)
242 {
243 ob_debug("got SHUTDOWN CANCELLED from session manager\n");
244 }
245
246 static gboolean session_save()
247 {
248 gchar *filename;
249 FILE *f;
250 GList *it;
251 gboolean success = TRUE;
252
253 /* this algo is from metacity */
254 filename = g_strdup_printf("%d-%d-%u.obs",
255 (int) time(NULL),
256 (int) getpid(),
257 g_random_int());
258 save_file = g_build_filename(g_get_home_dir(), ".openbox", "sessions",
259 filename, NULL);
260 g_free(filename);
261
262 f = fopen(save_file, "w");
263 if (!f) {
264 success = FALSE;
265 g_warning("unable to save the session to %s: %s",
266 save_file, strerror(errno));
267 } else {
268 fprintf(f, "<?xml version=\"1.0\"?>\n\n");
269 fprintf(f, "<openbox_session id=\"%s\">\n\n", ob_sm_id);
270
271 for (it = client_list; it; it = g_list_next(it)) {
272 guint num;
273 gint32 *dimensions;
274 gint prex, prey, prew, preh;
275 ObClient *c = it->data;
276
277 if (!client_normal(c))
278 continue;
279
280 prex = c->area.x;
281 prey = c->area.y;
282 prew = c->area.width;
283 preh = c->area.height;
284 if (PROP_GETA32(c->window, openbox_premax, cardinal,
285 (guint32**)&dimensions, &num)) {
286 if (num == 4) {
287 prex = dimensions[0];
288 prey = dimensions[1];
289 prew = dimensions[2];
290 preh = dimensions[3];
291 }
292 g_free(dimensions);
293 }
294
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);
308 if (c->shaded)
309 fprintf(f, "\t<shaded />\n");
310 if (c->iconic)
311 fprintf(f, "\t<iconic />\n");
312 if (c->skip_pager)
313 fprintf(f, "\t<skip_pager />\n");
314 if (c->skip_taskbar)
315 fprintf(f, "\t<skip_taskbar />\n");
316 if (c->fullscreen)
317 fprintf(f, "\t<fullscreen />\n");
318 if (c->above)
319 fprintf(f, "\t<above />\n");
320 if (c->below)
321 fprintf(f, "\t<below />\n");
322 if (c->max_horz)
323 fprintf(f, "\t<max_horz />\n");
324 if (c->max_vert)
325 fprintf(f, "\t<max_vert />\n");
326 fprintf(f, "</window>\n\n");
327 }
328
329 fprintf(f, "</openbox_session>\n");
330
331 if (fflush(f)) {
332 success = FALSE;
333 g_warning("error while saving the session to %s: %s",
334 save_file, strerror(errno));
335 }
336 fclose(f);
337 }
338
339 return success;
340 }
341
342 void session_load(char *path)
343 {
344 xmlDocPtr doc;
345 xmlNodePtr node, n;
346 gchar *sm_id;
347
348 if (!parse_load(path, "openbox_session", &doc, &node))
349 return;
350
351 if (!parse_attr_string("id", node, &sm_id))
352 return;
353 ob_sm_id = g_strdup(sm_id);
354
355 node = parse_find_node("window", node->xmlChildrenNode);
356 while (node) {
357 gchar *id, *name, *class, *role;
358 guint desktop;
359 gint x, y, w, h;
360 gboolean shaded, iconic, skip_pager, skip_taskbar, fullscreen;
361 gboolean above, below, max_horz, max_vert;
362
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);
389
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)
393 != NULL;
394 skip_taskbar = parse_find_node("skip_taskbar", node->xmlChildrenNode)
395 != NULL;
396 fullscreen = parse_find_node("fullscreen", node->xmlChildrenNode)
397 != NULL;
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;
402
403 g_message("read session window %s", name);
404
405 /* XXX save this */
406
407 session_load_bail:
408
409 node = parse_find_node("window", node->next);
410 }
411
412 xmlFreeDoc(doc);
413
414 unlink(path);
415 }
416
417 #endif
This page took 0.05042 seconds and 4 git commands to generate.