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.
43 bool restore_tree(const char *file_path)
46 char *json = read_string(file_path, &jslen);
54 jsmntok_t *tokens = malloc(nbtok * sizeof(jsmntok_t));
57 perror("Restore tree: malloc");
65 while ((ret = jsmn_parse(&parser, json, jslen, tokens, nbtok)) == JSMN_ERROR_NOMEM) {
67 jsmntok_t *rtokens = realloc(tokens, nbtok * sizeof(jsmntok_t));
68 if (rtokens == NULL) {
69 perror("Restore tree: realloc");
79 warn("Restore tree: jsmn_parse: ");
81 case JSMN_ERROR_NOMEM:
82 warn("not enough memory.\n");
84 case JSMN_ERROR_INVAL:
85 warn("found invalid character inside JSON string.\n");
88 warn("not a full JSON packet.\n");
91 warn("unknown error.\n");
101 int num = tokens[0].size;
111 while (mon_head != NULL) {
112 remove_monitor(mon_head);
115 jsmntok_t *t = tokens + 1;
116 uint32_t focused_monitor_id = 0, primary_monitor_id = 0;
117 jsmntok_t *focus_history_token = NULL, *stacking_list_token = NULL;
119 for (int i = 0; i < num; i++) {
120 if (keyeq("focusedMonitorId", t, json)) {
122 sscanf(json + t->start, "%u", &focused_monitor_id);
123 } else if (keyeq("primaryMonitorId", t, json)) {
125 sscanf(json + t->start, "%u", &primary_monitor_id);
126 } else if (keyeq("clientsCount", t, json)) {
128 sscanf(json + t->start, "%u", &clients_count);
129 } else if (keyeq("monitors", t, json)) {
133 for (int j = 0; j < s; j++) {
134 monitor_t *m = restore_monitor(&t, json);
135 if (m->desk == NULL) {
136 add_desktop(m, make_desktop(NULL, XCB_NONE));
141 } else if (keyeq("focusHistory", t, json)) {
144 focus_history_token = t;
146 restore_history(&t, json);
148 } else if (keyeq("stackingList", t, json)) {
151 stacking_list_token = t;
153 restore_stack(&t, json);
159 if (focused_monitor_id != 0) {
161 if (monitor_from_id(focused_monitor_id, &loc)) {
166 if (primary_monitor_id != 0) {
168 if (monitor_from_id(primary_monitor_id, &loc)) {
169 pri_mon = loc.monitor;
173 if (focus_history_token != NULL) {
174 restore_history(&focus_history_token, json);
177 if (stacking_list_token != NULL) {
178 restore_stack(&stacking_list_token, json);
181 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
182 m->id = xcb_generate_id(dpy);
183 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
184 d->id = xcb_generate_id(dpy);
185 regenerate_ids_in(d->root);
186 refresh_presel_feedbacks(m, d, d->root);
187 restack_presel_feedbacks(d);
189 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
190 if (n->client == NULL) {
193 initialize_client(n);
194 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
195 xcb_change_window_attributes(dpy, n->id, XCB_CW_EVENT_MASK, values);
196 window_grab_buttons(n->id);
201 ewmh_update_number_of_desktops();
202 ewmh_update_desktop_names();
203 ewmh_update_desktop_viewport();
204 ewmh_update_current_desktop();
205 ewmh_update_client_list(false);
206 ewmh_update_client_list(true);
207 ewmh_update_active_window();
215 #define RESTORE_INT(k, p) \
216 } else if (keyeq(#k, *t, json)) { \
218 sscanf(json + (*t)->start, "%i", p);
220 #define RESTORE_UINT(k, p) \
221 } else if (keyeq(#k, *t, json)) { \
223 sscanf(json + (*t)->start, "%u", p);
225 #define RESTORE_USINT(k, p) \
226 } else if (keyeq(#k, *t, json)) { \
228 sscanf(json + (*t)->start, "%hu", p);
230 #define RESTORE_DOUBLE(k, p) \
231 } else if (keyeq(#k, *t, json)) { \
233 sscanf(json + (*t)->start, "%lf", p);
235 #define RESTORE_ANY(k, p, f) \
236 } else if (keyeq(#k, *t, json)) { \
238 char *val = copy_string(json + (*t)->start, (*t)->end - (*t)->start); \
242 #define RESTORE_BOOL(k, p) RESTORE_ANY(k, p, parse_bool)
244 monitor_t *restore_monitor(jsmntok_t **t, char *json)
246 int num = (*t)->size;
248 monitor_t *m = make_monitor(NULL, NULL, UINT32_MAX);
249 uint32_t focused_desktop_id = 0;
251 for (int i = 0; i < num; i++) {
252 if (keyeq("name", *t, json)) {
254 snprintf(m->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
255 RESTORE_UINT(id, &m->id)
256 RESTORE_UINT(randrId, &m->randr_id)
257 RESTORE_BOOL(wired, &m->wired)
258 RESTORE_UINT(stickyCount, &m->sticky_count)
259 RESTORE_INT(windowGap, &m->window_gap)
260 RESTORE_UINT(borderWidth, &m->border_width)
261 RESTORE_UINT(focusedDesktopId, &focused_desktop_id)
262 } else if (keyeq("padding", *t, json)) {
264 restore_padding(&m->padding, t, json);
266 } else if (keyeq("rectangle", *t, json)) {
268 restore_rectangle(&m->rectangle, t, json);
269 update_root(m, &m->rectangle);
271 } else if (keyeq("desktops", *t, json)) {
275 for (int j = 0; j < s; j++) {
276 desktop_t *d = restore_desktop(t, json);
281 warn("Restore monitor: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
287 if (focused_desktop_id != 0) {
288 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
289 if (d->id == focused_desktop_id) {
299 desktop_t *restore_desktop(jsmntok_t **t, char *json)
303 desktop_t *d = make_desktop(NULL, UINT32_MAX);
304 xcb_window_t focusedNodeId = XCB_NONE;
306 for (int i = 0; i < s; i++) {
307 if (keyeq("name", *t, json)) {
309 snprintf(d->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
310 RESTORE_UINT(id, &d->id)
311 RESTORE_ANY(layout, &d->layout, parse_layout)
312 RESTORE_INT(windowGap, &d->window_gap)
313 RESTORE_UINT(borderWidth, &d->border_width)
314 } else if (keyeq("focusedNodeId", *t, json)) {
316 sscanf(json + (*t)->start, "%u", &focusedNodeId);
317 } else if (keyeq("padding", *t, json)) {
319 restore_padding(&d->padding, t, json);
321 } else if (keyeq("root", *t, json)) {
323 d->root = restore_node(t, json);
326 warn("Restore desktop: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
332 if (focusedNodeId != XCB_NONE) {
333 d->focus = find_by_id_in(d->root, focusedNodeId);
339 node_t *restore_node(jsmntok_t **t, char *json)
341 if ((*t)->type == JSMN_PRIMITIVE) {
347 /* hack to prevent a new ID from being generated */
348 node_t *n = make_node(UINT32_MAX);
350 for (int i = 0; i < s; i++) {
351 if (keyeq("id", *t, json)) {
353 sscanf(json + (*t)->start, "%u", &n->id);
354 RESTORE_ANY(splitType, &n->split_type, parse_split_type)
355 RESTORE_DOUBLE(splitRatio, &n->split_ratio)
356 RESTORE_BOOL(vacant, &n->vacant)
357 RESTORE_BOOL(hidden, &n->hidden)
358 RESTORE_BOOL(sticky, &n->sticky)
359 RESTORE_BOOL(private, &n->private)
360 RESTORE_BOOL(locked, &n->locked)
361 RESTORE_BOOL(marked, &n->marked)
362 } else if (keyeq("presel", *t, json)) {
364 n->presel = restore_presel(t, json);
366 } else if (keyeq("rectangle", *t, json)) {
368 restore_rectangle(&n->rectangle, t, json);
370 } else if (keyeq("constraints", *t, json)) {
372 restore_constraints(&n->constraints, t, json);
374 } else if (keyeq("firstChild", *t, json)) {
376 node_t *fc = restore_node(t, json);
382 } else if (keyeq("secondChild", *t, json)) {
384 node_t *sc = restore_node(t, json);
385 n->second_child = sc;
390 } else if (keyeq("client", *t, json)) {
392 n->client = restore_client(t, json);
395 warn("Restore node: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
405 presel_t *restore_presel(jsmntok_t **t, char *json)
407 if ((*t)->type == JSMN_PRIMITIVE) {
413 presel_t *p = make_presel();
415 for (int i = 0; i < s; i++) {
416 if (keyeq("splitRatio", *t, json)) {
418 sscanf(json + (*t)->start, "%lf", &p->split_ratio);
419 RESTORE_ANY(splitDir, &p->split_dir, parse_direction)
430 client_t *restore_client(jsmntok_t **t, char *json)
432 if ((*t)->type == JSMN_PRIMITIVE) {
438 client_t *c = make_client();
440 for (int i = 0; i < s; i++) {
441 if (keyeq("className", *t, json)) {
443 snprintf(c->class_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
444 } else if (keyeq("instanceName", *t, json)) {
446 snprintf(c->instance_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
447 RESTORE_ANY(state, &c->state, parse_client_state)
448 RESTORE_ANY(lastState, &c->last_state, parse_client_state)
449 RESTORE_ANY(layer, &c->layer, parse_stack_layer)
450 RESTORE_ANY(lastLayer, &c->last_layer, parse_stack_layer)
451 RESTORE_UINT(borderWidth, &c->border_width)
452 RESTORE_BOOL(urgent, &c->urgent)
453 RESTORE_BOOL(shown, &c->shown)
454 } else if (keyeq("tiledRectangle", *t, json)) {
456 restore_rectangle(&c->tiled_rectangle, t, json);
458 } else if (keyeq("floatingRectangle", *t, json)) {
460 restore_rectangle(&c->floating_rectangle, t, json);
463 warn("Restore client: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
474 void restore_rectangle(xcb_rectangle_t *r, jsmntok_t **t, char *json)
479 for (int i = 0; i < s; i++) {
480 if (keyeq("x", *t, json)) {
482 sscanf(json + (*t)->start, "%hi", &r->x);
483 } else if (keyeq("y", *t, json)) {
485 sscanf(json + (*t)->start, "%hi", &r->y);
486 } else if (keyeq("width", *t, json)) {
488 sscanf(json + (*t)->start, "%hu", &r->width);
489 } else if (keyeq("height", *t, json)) {
491 sscanf(json + (*t)->start, "%hu", &r->height);
497 void restore_constraints(constraints_t *c, jsmntok_t **t, char *json)
502 for (int i = 0; i < s; i++) {
503 if (keyeq("min_width", *t, json)) {
505 sscanf(json + (*t)->start, "%hu", &c->min_width);
506 } else if (keyeq("min_height", *t, json)) {
508 sscanf(json + (*t)->start, "%hu", &c->min_height);
514 void restore_padding(padding_t *p, jsmntok_t **t, char *json)
519 for (int i = 0; i < s; i++) {
520 if (keyeq("top", *t, json)) {
522 sscanf(json + (*t)->start, "%i", &p->top);
523 } else if (keyeq("right", *t, json)) {
525 sscanf(json + (*t)->start, "%i", &p->right);
526 } else if (keyeq("bottom", *t, json)) {
528 sscanf(json + (*t)->start, "%i", &p->bottom);
529 } else if (keyeq("left", *t, json)) {
531 sscanf(json + (*t)->start, "%i", &p->left);
537 void restore_history(jsmntok_t **t, char *json)
542 for (int i = 0; i < s; i++) {
543 coordinates_t loc = {NULL, NULL, NULL};
544 restore_coordinates(&loc, t, json);
545 if (loc.monitor != NULL && loc.desktop != NULL) {
546 history_add(loc.monitor, loc.desktop, loc.node, true);
551 void restore_coordinates(coordinates_t *loc, jsmntok_t **t, char *json)
557 for (int i = 0; i < s; i++) {
558 if (keyeq("monitorId", *t, json)) {
560 sscanf(json + (*t)->start, "%u", &id);
561 loc->monitor = find_monitor(id);
562 } else if (keyeq("desktopId", *t, json)) {
564 sscanf(json + (*t)->start, "%u", &id);
565 loc->desktop = find_desktop_in(id, loc->monitor);
566 } else if (keyeq("nodeId", *t, json)) {
568 sscanf(json + (*t)->start, "%u", &id);
569 loc->node = find_by_id_in(loc->desktop != NULL ? loc->desktop->root : NULL, id);
575 void restore_stack(jsmntok_t **t, char *json)
580 for (int i = 0; i < s; i++) {
582 sscanf(json + (*t)->start, "%u", &id);
584 if (locate_window(id, &loc)) {
585 stack_insert_after(stack_tail, loc.node);
594 #undef RESTORE_DOUBLE
598 bool keyeq(char *s, jsmntok_t *key, char *json)
600 size_t n = key->end - key->start;
601 return (strlen(s) == n && strncmp(s, json + key->start, n) == 0);