]> git.lizzy.rs Git - bspwm.git/blob - restore.c
Fix typo
[bspwm.git] / restore.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "bspwm.h"
30 #include "desktop.h"
31 #include "ewmh.h"
32 #include "history.h"
33 #include "monitor.h"
34 #include "query.h"
35 #include "stack.h"
36 #include "tree.h"
37 #include "settings.h"
38 #include "restore.h"
39 #include "window.h"
40 #include "parse.h"
41
42 bool restore_tree(const char *file_path)
43 {
44         size_t jslen;
45         char *json = read_string(file_path, &jslen);
46
47         if (json == NULL) {
48                 return false;
49         }
50
51         int nbtok = 256;
52         jsmn_parser parser;
53         jsmntok_t *tokens = malloc(nbtok * sizeof(jsmntok_t));
54
55         if (tokens == NULL) {
56                 perror("Restore tree: malloc");
57                 free(json);
58                 return false;
59         }
60
61         jsmn_init(&parser);
62         int ret;
63
64         while ((ret = jsmn_parse(&parser, json, jslen, tokens, nbtok)) == JSMN_ERROR_NOMEM) {
65                 nbtok *= 2;
66                 jsmntok_t *rtokens = realloc(tokens, nbtok * sizeof(jsmntok_t));
67                 if (rtokens == NULL) {
68                         perror("Restore tree: realloc");
69                         free(tokens);
70                         free(json);
71                         return false;
72                 } else {
73                         tokens = rtokens;
74                 }
75         }
76
77         if (ret < 0) {
78                 warn("Restore tree: jsmn_parse: ");
79                 switch (ret) {
80                         case JSMN_ERROR_NOMEM:
81                                 warn("not enough memory.\n");
82                                 break;
83                         case JSMN_ERROR_INVAL:
84                                 warn("found invalid character inside JSON string.\n");
85                                 break;
86                         case JSMN_ERROR_PART:
87                                 warn("not a full JSON packet.\n");
88                                 break;
89                         default:
90                                 warn("unknown error.\n");
91                                 break;
92                 }
93
94                 free(tokens);
95                 free(json);
96
97                 return false;
98         }
99
100         int num = tokens[0].size;
101
102         if (num < 1) {
103                 free(tokens);
104                 free(json);
105
106                 return false;
107         }
108
109         mon = NULL;
110         while (mon_head != NULL) {
111                 remove_monitor(mon_head);
112         }
113
114         jsmntok_t *t = tokens + 1;
115         char *focusedMonitorName = NULL, *primaryMonitorName = NULL;
116
117         for (int i = 0; i < num; i++) {
118                 if (keyeq("focusedMonitorName", t, json)) {
119                         free(focusedMonitorName);
120                         t++;
121                         focusedMonitorName = copy_string(json + t->start, t->end - t->start);
122                 } else if (keyeq("primaryMonitorName", t, json)) {
123                         free(primaryMonitorName);
124                         t++;
125                         primaryMonitorName = copy_string(json + t->start, t->end - t->start);
126                 } else if (keyeq("clientsCount", t, json)) {
127                         t++;
128                         sscanf(json + t->start, "%u", &clients_count);
129                 } else if (keyeq("monitors", t, json)) {
130                         t++;
131                         int s = t->size;
132                         t++;
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));
137                                 }
138                                 add_monitor(m);
139                         }
140                         continue;
141                 } else if (keyeq("focusHistory", t, json)) {
142                         t++;
143                         restore_history(&t, json);
144                         continue;
145                 } else if (keyeq("stackingList", t, json)) {
146                         t++;
147                         restore_stack(&t, json);
148                         continue;
149                 }
150                 t++;
151         }
152
153         if (focusedMonitorName != NULL) {
154                 coordinates_t loc;
155                 if (locate_monitor(focusedMonitorName, &loc)) {
156                         mon = loc.monitor;
157                 }
158         }
159
160         if (primaryMonitorName != NULL) {
161                 coordinates_t loc;
162                 if (locate_monitor(primaryMonitorName, &loc)) {
163                         pri_mon = loc.monitor;
164                 }
165         }
166
167         free(focusedMonitorName);
168         free(primaryMonitorName);
169
170         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
171                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
172                         refresh_presel_feebacks_in(d->root, d, m);
173                         restack_presel_feedback(d);
174                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
175                                 if (n->client == NULL) {
176                                         continue;
177                                 }
178                                 initialize_client(n);
179                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
180                                 xcb_change_window_attributes(dpy, n->id, XCB_CW_EVENT_MASK, values);
181                         }
182                 }
183         }
184
185         ewmh_update_client_list(false);
186         ewmh_update_client_list(true);
187         ewmh_update_active_window();
188         ewmh_update_number_of_desktops();
189         ewmh_update_current_desktop();
190         ewmh_update_desktop_names();
191
192         free(tokens);
193         free(json);
194
195         return true;
196 }
197
198 #define RESTORE_INT(k, p) \
199         } else if (keyeq(#k, *t, json)) { \
200                 (*t)++; \
201                 sscanf(json + (*t)->start, "%i", p);
202
203 #define RESTORE_UINT(k, p) \
204         } else if (keyeq(#k, *t, json)) { \
205                 (*t)++; \
206                 sscanf(json + (*t)->start, "%u", p);
207
208 #define RESTORE_USINT(k, p) \
209         } else if (keyeq(#k, *t, json)) { \
210                 (*t)++; \
211                 sscanf(json + (*t)->start, "%hu", p);
212
213 #define RESTORE_DOUBLE(k, p) \
214         } else if (keyeq(#k, *t, json)) { \
215                 (*t)++; \
216                 sscanf(json + (*t)->start, "%lf", p);
217
218 #define RESTORE_ANY(k, p, f) \
219         } else if (keyeq(#k, *t, json)) { \
220                 (*t)++; \
221                 char *val = copy_string(json + (*t)->start, (*t)->end - (*t)->start); \
222                 f(val, p); \
223                 free(val);
224
225 #define RESTORE_BOOL(k, p)  RESTORE_ANY(k, p, parse_bool)
226
227 monitor_t *restore_monitor(jsmntok_t **t, char *json)
228 {
229         int num = (*t)->size;
230         (*t)++;
231         monitor_t *m = make_monitor(NULL, UINT32_MAX);
232         char *focusedDesktopName = NULL;
233
234         for (int i = 0; i < num; i++) {
235                 if (keyeq("name", *t, json)) {
236                         (*t)++;
237                         snprintf(m->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
238                 RESTORE_UINT(id, &m->id)
239                 RESTORE_UINT(randrId, &m->randr_id)
240                 RESTORE_BOOL(wired, &m->wired)
241                 RESTORE_UINT(stickyCount, &m->sticky_count)
242                 RESTORE_INT(windowGap, &m->window_gap)
243                 RESTORE_UINT(borderWidth, &m->border_width)
244                 } else if (keyeq("padding", *t, json)) {
245                         (*t)++;
246                         restore_padding(&m->padding, t, json);
247                         continue;
248                 } else if (keyeq("rectangle", *t, json)) {
249                         (*t)++;
250                         restore_rectangle(&m->rectangle, t, json);
251                         update_root(m, &m->rectangle);
252                         continue;
253                 } else if (keyeq("focusedDesktopName", *t, json)) {
254                         free(focusedDesktopName);
255                         (*t)++;
256                         focusedDesktopName = copy_string(json + (*t)->start, (*t)->end - (*t)->start);
257                 } else if (keyeq("desktops", *t, json)) {
258                         (*t)++;
259                         int s = (*t)->size;
260                         (*t)++;
261                         for (int j = 0; j < s; j++) {
262                                 desktop_t *d = restore_desktop(t, json);
263                                 add_desktop(m, d);
264                         }
265                         continue;
266                 } else {
267                         warn("Restore monitor: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
268                         (*t)++;
269                 }
270                 (*t)++;
271         }
272
273         if (focusedDesktopName != NULL) {
274                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
275                         if (streq(focusedDesktopName, d->name)) {
276                                 m->desk = d;
277                                 break;
278                         }
279                 }
280         }
281
282         free(focusedDesktopName);
283
284         return m;
285 }
286
287 desktop_t *restore_desktop(jsmntok_t **t, char *json)
288 {
289         int s = (*t)->size;
290         (*t)++;
291         desktop_t *d = make_desktop(NULL, UINT32_MAX);
292         xcb_window_t focusedNodeId = XCB_NONE;
293
294         for (int i = 0; i < s; i++) {
295                 if (keyeq("name", *t, json)) {
296                         (*t)++;
297                         snprintf(d->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
298                 RESTORE_UINT(id, &d->id)
299                 RESTORE_ANY(layout, &d->layout, parse_layout)
300                 RESTORE_INT(windowGap, &d->window_gap)
301                 RESTORE_UINT(borderWidth, &d->border_width)
302                 } else if (keyeq("focusedNodeId", *t, json)) {
303                         (*t)++;
304                         sscanf(json + (*t)->start, "%u", &focusedNodeId);
305                 } else if (keyeq("padding", *t, json)) {
306                         (*t)++;
307                         restore_padding(&d->padding, t, json);
308                         continue;
309                 } else if (keyeq("root", *t, json)) {
310                         (*t)++;
311                         d->root = restore_node(t, json);
312                         continue;
313                 } else {
314                         warn("Restore desktop: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
315                         (*t)++;
316                 }
317                 (*t)++;
318         }
319
320         if (focusedNodeId != XCB_NONE) {
321                 d->focus = find_by_id_in(d->root, focusedNodeId);
322         }
323
324         return d;
325 }
326
327 node_t *restore_node(jsmntok_t **t, char *json)
328 {
329         if ((*t)->type == JSMN_PRIMITIVE) {
330                 (*t)++;
331                 return NULL;
332         } else {
333                 int s = (*t)->size;
334                 (*t)++;
335                 /* hack to prevent a new ID from being generated */
336                 node_t *n = make_node(UINT32_MAX);
337
338                 for (int i = 0; i < s; i++) {
339                         if (keyeq("id", *t, json)) {
340                                 (*t)++;
341                                 sscanf(json + (*t)->start, "%u", &n->id);
342                         RESTORE_ANY(splitType, &n->split_type, parse_split_type)
343                         RESTORE_DOUBLE(splitRatio, &n->split_ratio)
344                         RESTORE_INT(birthRotation, &n->birth_rotation)
345                         RESTORE_BOOL(vacant, &n->vacant)
346                         RESTORE_BOOL(sticky, &n->sticky)
347                         RESTORE_BOOL(private, &n->private)
348                         RESTORE_BOOL(locked, &n->locked)
349                         } else if (keyeq("presel", *t, json)) {
350                                 (*t)++;
351                                 n->presel = restore_presel(t, json);
352                                 continue;
353                         } else if (keyeq("rectangle", *t, json)) {
354                                 (*t)++;
355                                 restore_rectangle(&n->rectangle, t, json);
356                                 continue;
357                         } else if (keyeq("firstChild", *t, json)) {
358                                 (*t)++;
359                                 node_t *fc = restore_node(t, json);
360                                 n->first_child = fc;
361                                 if (fc != NULL) {
362                                         fc->parent = n;
363                                 }
364                                 continue;
365                         } else if (keyeq("secondChild", *t, json)) {
366                                 (*t)++;
367                                 node_t *sc = restore_node(t, json);
368                                 n->second_child = sc;
369                                 if (sc != NULL) {
370                                         sc->parent = n;
371                                 }
372                                 continue;
373                         } else if (keyeq("client", *t, json)) {
374                                 (*t)++;
375                                 n->client = restore_client(t, json);
376                                 continue;
377                         } else {
378                                 warn("Restore node: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
379                                 (*t)++;
380                         }
381                         (*t)++;
382                 }
383
384                 return n;
385         }
386 }
387
388 presel_t *restore_presel(jsmntok_t **t, char *json)
389 {
390         if ((*t)->type == JSMN_PRIMITIVE) {
391                 (*t)++;
392                 return NULL;
393         } else {
394                 int s = (*t)->size;
395                 (*t)++;
396                 presel_t *p = make_presel();
397
398                 for (int i = 0; i < s; i++) {
399                         if (keyeq("splitRatio", *t, json)) {
400                                 (*t)++;
401                                 sscanf(json + (*t)->start, "%lf", &p->split_ratio);
402                         RESTORE_ANY(splitDir, &p->split_dir, parse_direction)
403                         }
404
405                         (*t)++;
406                 }
407
408                 return p;
409         }
410 }
411
412
413 client_t *restore_client(jsmntok_t **t, char *json)
414 {
415         if ((*t)->type == JSMN_PRIMITIVE) {
416                 (*t)++;
417                 return NULL;
418         } else {
419                 int s = (*t)->size;
420                 (*t)++;
421                 client_t *c = make_client();
422
423                 for (int i = 0; i < s; i++) {
424                         if (keyeq("className", *t, json)) {
425                                 (*t)++;
426                                 snprintf(c->class_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
427                         } else if (keyeq("instanceName", *t, json)) {
428                                 (*t)++;
429                                 snprintf(c->instance_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
430                         RESTORE_ANY(state, &c->state, parse_client_state)
431                         RESTORE_ANY(lastState, &c->last_state, parse_client_state)
432                         RESTORE_ANY(layer, &c->layer, parse_stack_layer)
433                         RESTORE_ANY(lastLayer, &c->last_layer, parse_stack_layer)
434                         RESTORE_UINT(borderWidth, &c->border_width)
435                         RESTORE_BOOL(urgent, &c->urgent)
436                         RESTORE_BOOL(visible, &c->visible)
437                         } else if (keyeq("tiledRectangle", *t, json)) {
438                                 (*t)++;
439                                 restore_rectangle(&c->tiled_rectangle, t, json);
440                                 continue;
441                         } else if (keyeq("floatingRectangle", *t, json)) {
442                                 (*t)++;
443                                 restore_rectangle(&c->floating_rectangle, t, json);
444                                 continue;
445                         } else {
446                                 warn("Restore client: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
447                                 (*t)++;
448                         }
449
450                         (*t)++;
451                 }
452
453                 return c;
454         }
455 }
456
457 void restore_rectangle(xcb_rectangle_t *r, jsmntok_t **t, char *json)
458 {
459         int s = (*t)->size;
460         (*t)++;
461
462         for (int i = 0; i < s; i++) {
463                 if (keyeq("x", *t, json)) {
464                         (*t)++;
465                         sscanf(json + (*t)->start, "%hi", &r->x);
466                 } else if (keyeq("y", *t, json)) {
467                         (*t)++;
468                         sscanf(json + (*t)->start, "%hi", &r->y);
469                 } else if (keyeq("width", *t, json)) {
470                         (*t)++;
471                         sscanf(json + (*t)->start, "%hu", &r->width);
472                 } else if (keyeq("height", *t, json)) {
473                         (*t)++;
474                         sscanf(json + (*t)->start, "%hu", &r->height);
475                 }
476                 (*t)++;
477         }
478 }
479
480 void restore_padding(padding_t *p, jsmntok_t **t, char *json)
481 {
482         int s = (*t)->size;
483         (*t)++;
484
485         for (int i = 0; i < s; i++) {
486                 if (keyeq("top", *t, json)) {
487                         (*t)++;
488                         sscanf(json + (*t)->start, "%i", &p->top);
489                 } else if (keyeq("right", *t, json)) {
490                         (*t)++;
491                         sscanf(json + (*t)->start, "%i", &p->right);
492                 } else if (keyeq("bottom", *t, json)) {
493                         (*t)++;
494                         sscanf(json + (*t)->start, "%i", &p->bottom);
495                 } else if (keyeq("left", *t, json)) {
496                         (*t)++;
497                         sscanf(json + (*t)->start, "%i", &p->left);
498                 }
499                 (*t)++;
500         }
501 }
502
503 void restore_history(jsmntok_t **t, char *json)
504 {
505         int s = (*t)->size;
506         (*t)++;
507
508         for (int i = 0; i < s; i++) {
509                 coordinates_t loc = {NULL, NULL, NULL};
510                 restore_coordinates(&loc, t, json);
511                 if (loc.monitor != NULL && loc.desktop != NULL) {
512                         history_add(loc.monitor, loc.desktop, loc.node);
513                 }
514         }
515 }
516
517 void restore_coordinates(coordinates_t *loc, jsmntok_t **t, char *json)
518 {
519         int s = (*t)->size;
520         (*t)++;
521
522         for (int i = 0; i < s; i++) {
523                 if (keyeq("monitorName", *t, json)) {
524                         (*t)++;
525                         char *name = copy_string(json + (*t)->start, (*t)->end - (*t)->start);
526                         loc->monitor = find_monitor(name);
527                         free(name);
528                 } else if (keyeq("desktopName", *t, json)) {
529                         (*t)++;
530                         char *name = copy_string(json + (*t)->start, (*t)->end - (*t)->start);
531                         loc->desktop = find_desktop_in(name, loc->monitor);
532                         free(name);
533                 } else if (keyeq("nodeId", *t, json)) {
534                         (*t)++;
535                         uint32_t id;
536                         sscanf(json + (*t)->start, "%u", &id);
537                         loc->node = find_by_id_in(loc->desktop!=NULL?loc->desktop->root:NULL, id);
538                 }
539                 (*t)++;
540         }
541 }
542
543 void restore_stack(jsmntok_t **t, char *json)
544 {
545         int s = (*t)->size;
546         (*t)++;
547
548         for (int i = 0; i < s; i++) {
549                 uint32_t id;
550                 sscanf(json + (*t)->start, "%u", &id);
551                 coordinates_t loc;
552                 if (locate_window(id, &loc)) {
553                         stack_insert_after(stack_tail, loc.node);
554                 }
555                 (*t)++;
556         }
557 }
558
559 #undef RESTORE_INT
560 #undef RESTORE_UINT
561 #undef RESTORE_USINT
562 #undef RESTORE_DOUBLE
563 #undef RESTORE_ANY
564 #undef RESTORE_BOOL
565
566 bool keyeq(char *s, jsmntok_t *key, char *json)
567 {
568         size_t n = key->end - key->start;
569         return (strlen(s) == n && strncmp(s, json + key->start, n) == 0);
570 }