]> git.lizzy.rs Git - bspwm.git/blob - restore.c
Don't add a monitor without desktops
[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                         focusedMonitorName = copy_string(t+1, json);
121                         t++;
122                 } else if (keyeq("primaryMonitorName", t, json)) {
123                         free(primaryMonitorName);
124                         primaryMonitorName = copy_string(t+1, json);
125                         t++;
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));
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                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
176                                 xcb_change_window_attributes(dpy, n->id, XCB_CW_EVENT_MASK, values);
177                         }
178                 }
179         }
180
181         ewmh_update_client_list(false);
182         ewmh_update_client_list(true);
183         ewmh_update_active_window();
184         ewmh_update_number_of_desktops();
185         ewmh_update_current_desktop();
186         ewmh_update_desktop_names();
187
188         free(tokens);
189         free(json);
190
191         return true;
192 }
193
194 #define RESTORE_INT(k, p) \
195         } else if (keyeq(#k, *t, json)) { \
196                 (*t)++; \
197                 sscanf(json + (*t)->start, "%i", p);
198
199 #define RESTORE_UINT(k, p) \
200         } else if (keyeq(#k, *t, json)) { \
201                 (*t)++; \
202                 sscanf(json + (*t)->start, "%u", p);
203
204 #define RESTORE_USINT(k, p) \
205         } else if (keyeq(#k, *t, json)) { \
206                 (*t)++; \
207                 sscanf(json + (*t)->start, "%hu", p);
208
209 #define RESTORE_DOUBLE(k, p) \
210         } else if (keyeq(#k, *t, json)) { \
211                 (*t)++; \
212                 sscanf(json + (*t)->start, "%lf", p);
213
214 #define RESTORE_ANY(k, p, f) \
215         } else if (keyeq(#k, *t, json)) { \
216                 (*t)++; \
217                 char *val = copy_string(*t, json); \
218                 f(val, p); \
219                 free(val);
220
221 #define RESTORE_BOOL(k, p)  RESTORE_ANY(k, p, parse_bool)
222
223 monitor_t *restore_monitor(jsmntok_t **t, char *json)
224 {
225         int num = (*t)->size;
226         (*t)++;
227         monitor_t *m = make_monitor(NULL);
228         char *focusedDesktopName = NULL;
229
230         for (int i = 0; i < num; i++) {
231                 if (keyeq("name", *t, json)) {
232                         (*t)++;
233                         snprintf(m->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
234                 RESTORE_UINT(id, &m->id)
235                 RESTORE_BOOL(wired, &m->wired)
236                 RESTORE_INT(topPadding, &m->top_padding)
237                 RESTORE_INT(rightPadding, &m->right_padding)
238                 RESTORE_INT(bottomPadding, &m->bottom_padding)
239                 RESTORE_INT(leftPadding, &m->left_padding)
240                 RESTORE_UINT(stickyCount, &m->sticky_count)
241                 } else if (keyeq("rectangle", *t, json)) {
242                         (*t)++;
243                         restore_rectangle(&m->rectangle, t, json);
244                         update_root(m, &m->rectangle);
245                         continue;
246                 } else if (keyeq("focusedDesktopName", *t, json)) {
247                         free(focusedDesktopName);
248                         (*t)++;
249                         focusedDesktopName = copy_string(*t, json);
250                 } else if (keyeq("desktops", *t, json)) {
251                         (*t)++;
252                         int s = (*t)->size;
253                         (*t)++;
254                         for (int j = 0; j < s; j++) {
255                                 desktop_t *d = restore_desktop(t, json);
256                                 add_desktop(m, d);
257                         }
258                         continue;
259                 } else {
260                         warn("Restore monitor: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
261                         (*t)++;
262                 }
263                 (*t)++;
264         }
265
266         if (focusedDesktopName != NULL) {
267                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
268                         if (streq(focusedDesktopName, d->name)) {
269                                 m->desk = d;
270                                 break;
271                         }
272                 }
273         }
274
275         free(focusedDesktopName);
276
277         return m;
278 }
279
280 desktop_t *restore_desktop(jsmntok_t **t, char *json)
281 {
282         int s = (*t)->size;
283         (*t)++;
284         desktop_t *d = make_desktop(NULL);
285         xcb_window_t focusedNodeId = XCB_NONE;
286
287         for (int i = 0; i < s; i++) {
288                 if (keyeq("name", *t, json)) {
289                         (*t)++;
290                         snprintf(d->name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
291                 } else if (keyeq("layout", *t, json)) {
292                         (*t)++;
293                         char *val = copy_string(*t, json);
294                         layout_t lyt;
295                         if (parse_layout(val, &lyt)) {
296                                 d->layout = lyt;
297                         }
298                         free(val);
299                 RESTORE_INT(topPadding, &d->top_padding)
300                 RESTORE_INT(rightPadding, &d->right_padding)
301                 RESTORE_INT(bottomPadding, &d->bottom_padding)
302                 RESTORE_INT(leftPadding, &d->left_padding)
303                 RESTORE_INT(windowGap, &d->window_gap)
304                 RESTORE_UINT(borderWidth, &d->border_width)
305                 } else if (keyeq("focusedNodeId", *t, json)) {
306                         (*t)++;
307                         sscanf(json + (*t)->start, "%u", &focusedNodeId);
308                 } else if (keyeq("root", *t, json)) {
309                         (*t)++;
310                         d->root = restore_node(t, json);
311                         continue;
312                 } else {
313                         warn("Restore desktop: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
314                         (*t)++;
315                 }
316                 (*t)++;
317         }
318
319         if (focusedNodeId != XCB_NONE) {
320                 d->focus = find_by_id_in(d->root, focusedNodeId);
321         }
322
323         return d;
324 }
325
326 node_t *restore_node(jsmntok_t **t, char *json)
327 {
328         if ((*t)->type == JSMN_PRIMITIVE) {
329                 (*t)++;
330                 return NULL;
331         } else {
332                 int s = (*t)->size;
333                 (*t)++;
334                 /* hack to prevent a new ID from being generated */
335                 node_t *n = make_node(UINT32_MAX);
336
337                 for (int i = 0; i < s; i++) {
338                         if (keyeq("id", *t, json)) {
339                                 (*t)++;
340                                 sscanf(json + (*t)->start, "%u", &n->id);
341                         RESTORE_ANY(splitType, &n->split_type, parse_split_type)
342                         RESTORE_DOUBLE(splitRatio, &n->split_ratio)
343                         RESTORE_INT(birthRotation, &n->birth_rotation)
344                         RESTORE_BOOL(vacant, &n->vacant)
345                         RESTORE_BOOL(sticky, &n->sticky)
346                         RESTORE_BOOL(private, &n->private)
347                         RESTORE_BOOL(locked, &n->locked)
348                         } else if (keyeq("presel", *t, json)) {
349                                 (*t)++;
350                                 n->presel = restore_presel(t, json);
351                                 continue;
352                         } else if (keyeq("rectangle", *t, json)) {
353                                 (*t)++;
354                                 restore_rectangle(&n->rectangle, t, json);
355                                 continue;
356                         } else if (keyeq("firstChild", *t, json)) {
357                                 (*t)++;
358                                 node_t *fc = restore_node(t, json);
359                                 n->first_child = fc;
360                                 if (fc != NULL) {
361                                         fc->parent = n;
362                                 }
363                                 continue;
364                         } else if (keyeq("secondChild", *t, json)) {
365                                 (*t)++;
366                                 node_t *sc = restore_node(t, json);
367                                 n->second_child = sc;
368                                 if (sc != NULL) {
369                                         sc->parent = n;
370                                 }
371                                 continue;
372                         } else if (keyeq("client", *t, json)) {
373                                 (*t)++;
374                                 n->client = restore_client(t, json);
375                                 continue;
376                         } else {
377                                 warn("Restore node: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
378                                 (*t)++;
379                         }
380                         (*t)++;
381                 }
382
383                 return n;
384         }
385 }
386
387 presel_t *restore_presel(jsmntok_t **t, char *json)
388 {
389         if ((*t)->type == JSMN_PRIMITIVE) {
390                 (*t)++;
391                 return NULL;
392         } else {
393                 int s = (*t)->size;
394                 (*t)++;
395                 presel_t *p = make_presel();
396
397                 for (int i = 0; i < s; i++) {
398                         if (keyeq("splitRatio", *t, json)) {
399                                 (*t)++;
400                                 sscanf(json + (*t)->start, "%lf", &p->split_ratio);
401                         RESTORE_ANY(splitDir, &p->split_dir, parse_direction)
402                         }
403
404                         (*t)++;
405                 }
406
407                 return p;
408         }
409 }
410
411
412 client_t *restore_client(jsmntok_t **t, char *json)
413 {
414         if ((*t)->type == JSMN_PRIMITIVE) {
415                 (*t)++;
416                 return NULL;
417         } else {
418                 int s = (*t)->size;
419                 (*t)++;
420                 client_t *c = make_client();
421
422                 for (int i = 0; i < s; i++) {
423                         if (keyeq("className", *t, json)) {
424                                 (*t)++;
425                                 snprintf(c->class_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
426                         } else if (keyeq("instanceName", *t, json)) {
427                                 (*t)++;
428                                 snprintf(c->instance_name, (*t)->end - (*t)->start + 1, "%s", json + (*t)->start);
429                         RESTORE_ANY(state, &c->state, parse_client_state)
430                         RESTORE_ANY(lastState, &c->last_state, parse_client_state)
431                         RESTORE_ANY(layer, &c->layer, parse_stack_layer)
432                         RESTORE_ANY(lastLayer, &c->last_layer, parse_stack_layer)
433                         RESTORE_UINT(borderWidth, &c->border_width)
434                         RESTORE_BOOL(urgent, &c->urgent)
435                         RESTORE_BOOL(visible, &c->visible)
436                         RESTORE_BOOL(icccmFocus, &c->icccm_focus)
437                         RESTORE_BOOL(icccmInput, &c->icccm_input)
438                         RESTORE_USINT(minWidth, &c->min_width)
439                         RESTORE_USINT(maxWidth, &c->max_width)
440                         RESTORE_USINT(minHeight, &c->min_height)
441                         RESTORE_USINT(maxHeight, &c->max_height)
442                         RESTORE_INT(wmStatesCount, &c->wm_states_count)
443                         } else if (keyeq("wmState", *t, json)) {
444                                 (*t)++;
445                                 restore_wm_state(c->wm_state, t, json);
446                                 continue;
447                         } else if (keyeq("tiledRectangle", *t, json)) {
448                                 (*t)++;
449                                 restore_rectangle(&c->tiled_rectangle, t, json);
450                                 continue;
451                         } else if (keyeq("floatingRectangle", *t, json)) {
452                                 (*t)++;
453                                 restore_rectangle(&c->floating_rectangle, t, json);
454                                 continue;
455                         } else {
456                                 warn("Restore client: unknown key: '%.*s'.\n", (*t)->end - (*t)->start, json + (*t)->start);
457                                 (*t)++;
458                         }
459
460                         (*t)++;
461                 }
462
463                 return c;
464         }
465 }
466
467 void restore_rectangle(xcb_rectangle_t *r, jsmntok_t **t, char *json)
468 {
469         int s = (*t)->size;
470         (*t)++;
471
472         for (int i = 0; i < s; i++) {
473                 if (keyeq("x", *t, json)) {
474                         (*t)++;
475                         sscanf(json + (*t)->start, "%hi", &r->x);
476                 } else if (keyeq("y", *t, json)) {
477                         (*t)++;
478                         sscanf(json + (*t)->start, "%hi", &r->y);
479                 } else if (keyeq("width", *t, json)) {
480                         (*t)++;
481                         sscanf(json + (*t)->start, "%hu", &r->width);
482                 } else if (keyeq("height", *t, json)) {
483                         (*t)++;
484                         sscanf(json + (*t)->start, "%hu", &r->height);
485                 }
486                 (*t)++;
487         }
488 }
489
490 void restore_history(jsmntok_t **t, char *json)
491 {
492         int s = (*t)->size;
493         (*t)++;
494
495         for (int i = 0; i < s; i++) {
496                 coordinates_t loc = {NULL, NULL, NULL};
497                 restore_coordinates(&loc, t, json);
498                 if (loc.monitor != NULL && loc.desktop != NULL) {
499                         history_add(loc.monitor, loc.desktop, loc.node);
500                 }
501         }
502 }
503
504 void restore_coordinates(coordinates_t *loc, jsmntok_t **t, char *json)
505 {
506         int s = (*t)->size;
507         (*t)++;
508
509         for (int i = 0; i < s; i++) {
510                 if (keyeq("monitorName", *t, json)) {
511                         (*t)++;
512                         char *name = copy_string(*t, json);
513                         loc->monitor = find_monitor(name);
514                         free(name);
515                 } else if (keyeq("desktopName", *t, json)) {
516                         (*t)++;
517                         char *name = copy_string(*t, json);
518                         loc->desktop = find_desktop_in(name, loc->monitor);
519                         free(name);
520                 } else if (keyeq("nodeId", *t, json)) {
521                         (*t)++;
522                         uint32_t id;
523                         sscanf(json + (*t)->start, "%u", &id);
524                         loc->node = find_by_id_in(loc->desktop!=NULL?loc->desktop->root:NULL, id);
525                 }
526                 (*t)++;
527         }
528 }
529
530 void restore_stack(jsmntok_t **t, char *json)
531 {
532         int s = (*t)->size;
533         (*t)++;
534
535         for (int i = 0; i < s; i++) {
536                 uint32_t id;
537                 sscanf(json + (*t)->start, "%u", &id);
538                 coordinates_t loc;
539                 if (locate_window(id, &loc)) {
540                         stack_insert_after(stack_tail, loc.node);
541                 }
542                 (*t)++;
543         }
544 }
545
546 void restore_wm_state(xcb_atom_t *w, jsmntok_t **t, char *json)
547 {
548         int s = (*t)->size;
549         (*t)++;
550
551         for (int i = 0; i < s; i++) {
552                 sscanf(json + (*t)->start, "%u", &w[i]);
553                 (*t)++;
554         }
555 }
556
557 #undef RESTORE_INT
558 #undef RESTORE_UINT
559 #undef RESTORE_USINT
560 #undef RESTORE_DOUBLE
561 #undef RESTORE_ANY
562 #undef RESTORE_BOOL
563
564 bool keyeq(char *s, jsmntok_t *key, char *json)
565 {
566         size_t n = key->end - key->start;
567         return (strlen(s) == n && strncmp(s, json + key->start, n) == 0);
568 }
569
570 char *copy_string(jsmntok_t *tok, char *json)
571 {
572         size_t len = tok->end - tok->start + 1;
573         char *res = malloc(len * sizeof(char));
574         if (res == NULL) {
575                 perror("Copy string: malloc");
576                 return NULL;
577         }
578         strncpy(res, json+tok->start, len-1);
579         res[len-1] = '\0';
580         return res;
581 }