1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 bool restore_tree(const char *file_path)
47 char *json = read_string(file_path, &jslen);
55 jsmntok_t *tokens = malloc(nbtok * sizeof(jsmntok_t));
58 perror("Restore tree: malloc");
66 while ((ret = jsmn_parse(&parser, json, jslen, tokens, nbtok)) == JSMN_ERROR_NOMEM) {
68 jsmntok_t *rtokens = realloc(tokens, nbtok * sizeof(jsmntok_t));
69 if (rtokens == NULL) {
70 perror("Restore tree: realloc");
80 warn("Restore tree: jsmn_parse: ");
82 case JSMN_ERROR_NOMEM:
83 warn("not enough memory.\n");
85 case JSMN_ERROR_INVAL:
86 warn("found invalid character inside JSON string.\n");
89 warn("not a full JSON packet.\n");
92 warn("unknown error.\n");
102 while (mon_head != NULL) {
103 remove_monitor(mon_head);
106 int num = tokens[0].size;
107 jsmntok_t *t = tokens + 1;
108 char *focusedMonitorName = NULL;
110 for (int i = 0; i < num; i++) {
111 if (keyeq("focusedMonitorName", t, json)) {
112 focusedMonitorName = copy_string(t+1, json);
114 } else if (keyeq("numClients", t, json)) {
116 sscanf(json + t->start, "%u", &num_clients);
117 } else if (keyeq("monitors", t, json)) {
121 for (int j = 0; j < s; j++) {
122 monitor_t *m = restore_monitor(&t, json);
130 if (focusedMonitorName != NULL) {
132 if (locate_monitor(focusedMonitorName, &loc)) {
137 free(focusedMonitorName);
139 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
140 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
141 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
142 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
143 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
148 ewmh_update_client_list();
149 ewmh_update_number_of_desktops();
150 ewmh_update_current_desktop();
151 ewmh_update_desktop_names();
159 #define RESTORE_INT(o, k, p) \
160 } else if (keyeq(#k, *t, json)) { \
162 sscanf(json + (*t)->start, "%i", &o->p);
164 #define RESTORE_UINT(o, k, p) \
165 } else if (keyeq(#k, *t, json)) { \
167 sscanf(json + (*t)->start, "%u", &o->p);
169 #define RESTORE_USINT(o, k, p) \
170 } else if (keyeq(#k, *t, json)) { \
172 sscanf(json + (*t)->start, "%hu", &o->p);
174 #define RESTORE_DOUBLE(o, k, p) \
175 } else if (keyeq(#k, *t, json)) { \
177 sscanf(json + (*t)->start, "%lf", &o->p);
179 #define RESTORE_ANY(o, k, p, f) \
180 } else if (keyeq(#k, *t, json)) { \
182 char *val = copy_string(*t, json); \
186 #define RESTORE_BOOL(o, k, p) RESTORE_ANY(o, k, p, parse_bool)
188 monitor_t *restore_monitor(jsmntok_t **t, char *json)
190 int num = (*t)->size;
192 monitor_t *m = make_monitor(NULL);
193 char *focusedDesktopName = NULL;
195 for (int i = 0; i < num; i++) {
196 if (keyeq("name", *t, json)) {
198 snprintf(m->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
199 RESTORE_UINT(m, id, id)
200 RESTORE_BOOL(m, wired, wired)
201 RESTORE_INT(m, topPadding, top_padding)
202 RESTORE_INT(m, rightPadding, right_padding)
203 RESTORE_INT(m, bottomPadding, bottom_padding)
204 RESTORE_INT(m, leftPadding, left_padding)
205 RESTORE_INT(m, numSticky, num_sticky)
206 } else if (keyeq("rectangle", *t, json)) {
208 restore_rectangle(&m->rectangle, t, json);
209 update_root(m, &m->rectangle);
211 } else if (keyeq("focusedDesktopName", *t, json)) {
213 focusedDesktopName = copy_string(*t, json);
214 } else if (keyeq("desktops", *t, json)) {
218 for (int j = 0; j < s; j++) {
219 desktop_t *d = restore_desktop(t, json);
224 warn("Restore monitor: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
230 if (focusedDesktopName != NULL) {
231 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
232 if (streq(focusedDesktopName, d->name)) {
239 free(focusedDesktopName);
244 desktop_t *restore_desktop(jsmntok_t **t, char *json)
248 desktop_t *d = make_desktop(NULL);
249 xcb_window_t focusedWindow = XCB_NONE;
251 for (int i = 0; i < s; i++) {
252 if (keyeq("name", *t, json)) {
254 snprintf(d->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
255 } else if (keyeq("layout", *t, json)) {
257 char *val = copy_string(*t, json);
259 if (parse_layout(val, &lyt)) {
263 RESTORE_INT(d, topPadding, top_padding)
264 RESTORE_INT(d, rightPadding, right_padding)
265 RESTORE_INT(d, bottomPadding, bottom_padding)
266 RESTORE_INT(d, leftPadding, left_padding)
267 RESTORE_INT(d, windowGap, window_gap)
268 RESTORE_UINT(d, borderWidth, border_width)
269 } else if (keyeq("focusedWindow", *t, json)) {
271 sscanf(json + (*t)->start, "%u", &focusedWindow);
272 } else if (keyeq("root", *t, json)) {
274 d->root = restore_node(t, json);
277 warn("Restore desktop: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
283 if (focusedWindow != XCB_NONE) {
284 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
285 if (f->client->window == focusedWindow) {
295 node_t *restore_node(jsmntok_t **t, char *json)
297 if ((*t)->type == JSMN_PRIMITIVE) {
303 node_t *n = make_node();
305 for (int i = 0; i < s; i++) {
306 if (keyeq("splitType", *t, json)) {
308 char *val = copy_string(*t, json);
309 parse_split_type(val, &n->split_type);
311 RESTORE_DOUBLE(n, splitRatio, split_ratio)
312 RESTORE_ANY(n, splitMode, split_mode, parse_split_mode)
313 RESTORE_ANY(n, splitDir, split_dir, parse_direction)
314 RESTORE_INT(n, birthRotation, birth_rotation)
315 RESTORE_INT(n, privacyLevel, privacy_level)
316 RESTORE_ANY(n, vacant, vacant, parse_bool)
317 } else if (keyeq("rectangle", *t, json)) {
319 restore_rectangle(&n->rectangle, t, json);
321 } else if (keyeq("firstChild", *t, json)) {
323 node_t *fc = restore_node(t, json);
329 } else if (keyeq("secondChild", *t, json)) {
331 node_t *sc = restore_node(t, json);
332 n->second_child = sc;
337 } else if (keyeq("client", *t, json)) {
339 n->client = restore_client(t, json);
349 client_t *restore_client(jsmntok_t **t, char *json)
351 if ((*t)->type == JSMN_PRIMITIVE) {
357 client_t *c = make_client(XCB_NONE, 0);
359 for (int i = 0; i < s; i++) {
360 if (keyeq("window", *t, json)) {
362 sscanf(json + (*t)->start, "%u", &c->window);
363 } else if (keyeq("className", *t, json)) {
365 snprintf(c->class_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
366 } else if (keyeq("instanceName", *t, json)) {
368 snprintf(c->instance_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
369 RESTORE_ANY(c, state, state, parse_client_state)
370 RESTORE_ANY(c, lastState, last_state, parse_client_state)
371 RESTORE_ANY(c, layer, layer, parse_stack_layer)
372 RESTORE_ANY(c, lastLayer, last_layer, parse_stack_layer)
373 RESTORE_UINT(c, borderWidth, border_width)
374 RESTORE_BOOL(c, locked, locked)
375 RESTORE_BOOL(c, sticky, sticky)
376 RESTORE_BOOL(c, urgent, urgent)
377 RESTORE_BOOL(c, private, private)
378 RESTORE_BOOL(c, icccmFocus, icccm_focus)
379 RESTORE_BOOL(c, icccmInput, icccm_input)
380 RESTORE_USINT(c, minWidth, min_width)
381 RESTORE_USINT(c, maxWidth, max_width)
382 RESTORE_USINT(c, minHeight, min_height)
383 RESTORE_USINT(c, maxHeight, max_height)
384 RESTORE_INT(c, numStates, num_states)
385 } else if (keyeq("wmState", *t, json)) {
387 restore_wm_state(c->wm_state, t, json);
389 } else if (keyeq("tiledRectangle", *t, json)) {
391 restore_rectangle(&c->tiled_rectangle, t, json);
393 } else if (keyeq("floatingRectangle", *t, json)) {
395 restore_rectangle(&c->floating_rectangle, t, json);
406 void restore_rectangle(xcb_rectangle_t *r, jsmntok_t **t, char *json)
411 for (int i = 0; i < s; i++) {
412 if (keyeq("x", *t, json)) {
414 sscanf(json + (*t)->start, "%hi", &r->x);
415 } else if (keyeq("y", *t, json)) {
417 sscanf(json + (*t)->start, "%hi", &r->y);
418 } else if (keyeq("width", *t, json)) {
420 sscanf(json + (*t)->start, "%hu", &r->width);
421 } else if (keyeq("height", *t, json)) {
423 sscanf(json + (*t)->start, "%hu", &r->height);
429 void restore_wm_state(xcb_atom_t *w, jsmntok_t **t, char *json)
434 for (int i = 0; i < s; i++) {
435 sscanf(json + (*t)->start, "%u", &w[i]);
443 #undef RESTORE_DOUBLE
447 bool keyeq(char *s, jsmntok_t *key, char *json)
449 return (strncmp(s, json + key->start, key->end - key->start) == 0);
452 char *copy_string(jsmntok_t *tok, char *json)
454 size_t len = tok->end - tok->start + 1;
455 char *res = malloc(len * sizeof(char));
457 perror("Copy string: malloc");
460 strncpy(res, json+tok->start, len-1);
465 bool restore_history(const char *file_path)
467 if (file_path == NULL) {
471 FILE *snapshot = fopen(file_path, "r");
472 if (snapshot == NULL) {
473 perror("Restore history: fopen");
484 while (fgets(line, sizeof(line), snapshot) != NULL) {
485 if (sscanf(line, "%s %s %X", mnm, dnm, &win) == 3) {
487 if (win != XCB_NONE && !locate_window(win, &loc)) {
488 warn("Can't locate window 0x%X.\n", win);
491 node_t *n = (win == XCB_NONE ? NULL : loc.node);
492 if (!locate_desktop(dnm, &loc)) {
493 warn("Can't locate desktop '%s'.\n", dnm);
496 desktop_t *d = loc.desktop;
497 if (!locate_monitor(mnm, &loc)) {
498 warn("Can't locate monitor '%s'.\n", mnm);
501 monitor_t *m = loc.monitor;
502 history_add(m, d, n);
504 warn("Can't parse history entry: '%s'\n", line);
512 bool restore_stack(const char *file_path)
514 if (file_path == NULL) {
518 FILE *snapshot = fopen(file_path, "r");
519 if (snapshot == NULL) {
520 perror("Restore stack: fopen");
527 while (stack_head != NULL) {
528 remove_stack(stack_head);
531 while (fgets(line, sizeof(line), snapshot) != NULL) {
532 if (sscanf(line, "%X", &win) == 1) {
534 if (locate_window(win, &loc)) {
535 stack_insert_after(stack_tail, loc.node);
537 warn("Can't locate window 0x%X.\n", win);
540 warn("Can't parse stack entry: '%s'\n", line);