]> git.lizzy.rs Git - bspwm.git/blob - restore.c
Rewrite the stacking engine
[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 <ctype.h>
26 #include <string.h>
27 #include "bspwm.h"
28 #include "desktop.h"
29 #include "ewmh.h"
30 #include "history.h"
31 #include "monitor.h"
32 #include "query.h"
33 #include "stack.h"
34 #include "tree.h"
35 #include "settings.h"
36 #include "restore.h"
37
38 void restore_tree(char *file_path)
39 {
40         if (file_path == NULL)
41                 return;
42
43         FILE *snapshot = fopen(file_path, "r");
44         if (snapshot == NULL) {
45                 warn("Restore tree: can't open '%s'.\n", file_path);
46                 return;
47         }
48
49         PUTS("restore tree");
50
51         char line[MAXLEN];
52         char name[MAXLEN];
53         coordinates_t loc;
54         monitor_t *m = NULL;
55         desktop_t *d = NULL;
56         node_t *n = NULL;
57         unsigned int level, last_level = 0;
58
59         while (fgets(line, sizeof(line), snapshot) != NULL) {
60                 unsigned int len = strlen(line);
61                 level = 0;
62
63                 while (level < len && isspace(line[level]))
64                         level++;
65
66                 if (level == 0) {
67                         int x, y, top, right, bottom, left;
68                         unsigned int w, h;
69                         char end = 0;
70                         name[0] = '\0';
71                         sscanf(line + level, "%s %ux%u%i%i %i,%i,%i,%i %c", name, &w, &h, &x, &y,
72                                &top, &right, &bottom, &left, &end);
73                         m = find_monitor(name);
74                         if (m == NULL)
75                                 continue;
76                         m->rectangle = (xcb_rectangle_t) {x, y, w, h};
77                         m->top_padding = top;
78                         m->right_padding = right;
79                         m->bottom_padding = bottom;
80                         m->left_padding = left;
81                         if (end != 0)
82                                 mon = m;
83                 } else if (level == 1) {
84                         if (m == NULL)
85                                 continue;
86                         int wg, top, right, bottom, left;
87                         unsigned int bw;
88                         char floating, layout = 0, end = 0;
89                         name[0] = '\0';
90                         loc.desktop = NULL;
91                         sscanf(line + level, "%s %u %i %i,%i,%i,%i %c %c %c", name,
92                                &bw, &wg, &top, &right, &bottom, &left, &layout, &floating, &end);
93                         locate_desktop(name, &loc);
94                         d = loc.desktop;
95                         if (d == NULL)
96                                 continue;
97                         d->border_width = bw;
98                         d->window_gap = wg;
99                         d->top_padding = top;
100                         d->right_padding = right;
101                         d->bottom_padding = bottom;
102                         d->left_padding = left;
103                         if (layout == 'M')
104                                 d->layout = LAYOUT_MONOCLE;
105                         else if (layout == 'T')
106                                 d->layout = LAYOUT_TILED;
107                         d->floating = (floating == '-' ? false : true);
108                         if (end != 0)
109                                 m->desk = d;
110                 } else {
111                         if (m == NULL || d == NULL)
112                                 continue;
113                         node_t *birth = make_node();
114                         if (level == 2) {
115                                 empty_desktop(d);
116                                 d->root = birth;
117                         } else if (n != NULL) {
118                                 if (level > last_level) {
119                                         n->first_child = birth;
120                                 } else {
121                                         do {
122                                                 n = n->parent;
123                                         } while (n != NULL && n->second_child != NULL);
124                                         if (n == NULL)
125                                                 continue;
126                                         n->second_child = birth;
127                                 }
128                                 birth->parent = n;
129                         }
130                         n = birth;
131                         char br;
132                         if (isupper(line[level])) {
133                                 char st;
134                                 sscanf(line + level, "%c %c %lf", &st, &br, &n->split_ratio);
135                                 if (st == 'H')
136                                         n->split_type = TYPE_HORIZONTAL;
137                                 else if (st == 'V')
138                                         n->split_type = TYPE_VERTICAL;
139                         } else {
140                                 client_t *c = make_client(XCB_NONE, d->border_width);
141                                 num_clients++;
142                                 char floating, pseudo_tiled, fullscreen, urgent, locked, sticky, private, sd, sm, sl, end = 0;
143                                 sscanf(line + level, "%c %s %s %X %u %hux%hu%hi%hi %c %c%c%c%c%c%c%c%c%c %c", &br,
144                                        c->class_name, c->instance_name, &c->window, &c->border_width,
145                                        &c->floating_rectangle.width, &c->floating_rectangle.height,
146                                        &c->floating_rectangle.x, &c->floating_rectangle.y,
147                                        &sd, &floating, &pseudo_tiled, &fullscreen, &urgent,
148                                        &locked, &sticky, &private, &sm, &sl, &end);
149                                 c->floating = (floating == '-' ? false : true);
150                                 c->pseudo_tiled = (pseudo_tiled == '-' ? false : true);
151                                 c->fullscreen = (fullscreen == '-' ? false : true);
152                                 c->urgent = (urgent == '-' ? false : true);
153                                 c->locked = (locked == '-' ? false : true);
154                                 c->sticky = (sticky == '-' ? false : true);
155                                 c->private = (private == '-' ? false : true);
156                                 n->split_mode = (sm == '-' ? MODE_AUTOMATIC : MODE_MANUAL);
157                                 if (sd == 'U') {
158                                         n->split_dir = DIR_UP;
159                                 } else if (sd == 'R') {
160                                         n->split_dir = DIR_RIGHT;
161                                 } else if (sd == 'D') {
162                                         n->split_dir = DIR_DOWN;
163                                 } else if (sd == 'L') {
164                                         n->split_dir = DIR_LEFT;
165                                 }
166                                 if (sl == 'b') {
167                                         c->layer = LAYER_BELOW;
168                                 } else if (sl == 'a') {
169                                         c->layer = LAYER_ABOVE;
170                                 }
171                                 n->client = c;
172                                 if (end != 0)
173                                         d->focus = n;
174                                 if (c->sticky)
175                                         m->num_sticky++;
176                         }
177                         if (br == 'a')
178                                 n->birth_rotation = 90;
179                         else if (br == 'c')
180                                 n->birth_rotation = 270;
181                         else if (br == 'm')
182                                 n->birth_rotation = 0;
183                 }
184                 last_level = level;
185         }
186
187         fclose(snapshot);
188
189         for (monitor_t *m = mon_head; m != NULL; m = m->next)
190                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
191                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
192                                 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
193                                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
194                                 if (n->client->floating) {
195                                         n->vacant = true;
196                                         update_vacant_state(n->parent);
197                                 }
198                                 if (n->client->private)
199                                         update_privacy_level(n, true);
200                         }
201         ewmh_update_current_desktop();
202 }
203
204 void restore_history(char *file_path)
205 {
206         if (file_path == NULL)
207                 return;
208
209         FILE *snapshot = fopen(file_path, "r");
210         if (snapshot == NULL) {
211                 warn("Restore history: can't open '%s'.\n", file_path);
212                 return;
213         }
214
215         PUTS("restore history");
216
217         char line[MAXLEN];
218         char mnm[SMALEN];
219         char dnm[SMALEN];
220         xcb_window_t win;
221
222         while (fgets(line, sizeof(line), snapshot) != NULL) {
223                 if (sscanf(line, "%s %s %X", mnm, dnm, &win) == 3) {
224                         coordinates_t loc;
225                         if (win != XCB_NONE && !locate_window(win, &loc)) {
226                                 warn("Can't locate window 0x%X.\n", win);
227                                 continue;
228                         }
229                         node_t *n = (win == XCB_NONE ? NULL : loc.node);
230                         if (!locate_desktop(dnm, &loc)) {
231                                 warn("Can't locate desktop '%s'.\n", dnm);
232                                 continue;
233                         }
234                         desktop_t *d = loc.desktop;
235                         if (!locate_monitor(mnm, &loc)) {
236                                 warn("Can't locate monitor '%s'.\n", mnm);
237                                 continue;
238                         }
239                         monitor_t *m = loc.monitor;
240                         history_add(m, d, n);
241                 } else {
242                         warn("Can't parse history entry: '%s'\n", line);
243                 }
244         }
245
246         fclose(snapshot);
247 }
248
249 void restore_stack(char *file_path)
250 {
251         if (file_path == NULL)
252                 return;
253
254         FILE *snapshot = fopen(file_path, "r");
255         if (snapshot == NULL) {
256                 warn("Restore stack: can't open '%s'.\n", file_path);
257                 return;
258         }
259
260         PUTS("restore stack");
261
262         char line[MAXLEN];
263         xcb_window_t win;
264
265         while (fgets(line, sizeof(line), snapshot) != NULL) {
266                 if (sscanf(line, "%X", &win) == 1) {
267                         coordinates_t loc;
268                         if (locate_window(win, &loc))
269                                 stack_insert_after(stack_tail, loc.node);
270                         else
271                                 warn("Can't locate window 0x%X.\n", win);
272                 } else {
273                         warn("Can't parse stack entry: '%s'\n", line);
274                 }
275         }
276
277         fclose(snapshot);
278 }