]> git.lizzy.rs Git - bspwm.git/blob - restore.c
Document `control --record-history`
[bspwm.git] / restore.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation and/or
11  * other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20  * 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 "settings.h"
34 #include "stack.h"
35 #include "tree.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 file\n");
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     num_clients = 0;
58     unsigned int level, last_level = 0;
59
60     while (fgets(line, sizeof(line), snapshot) != NULL) {
61         unsigned int len = strlen(line);
62         level = 0;
63
64         while (level < len && isspace(line[level]))
65             level++;
66
67         if (level == 0) {
68             int x, y, left, right, top, bottom;
69             unsigned int w, h;
70             char end = 0;
71             name[0] = '\0';
72             sscanf(line + level, "%s %ux%u%i%i %i,%i,%i,%i %c", name, &w, &h, &x, &y, &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 == 2) {
84             if (m == NULL)
85                 continue;
86             int wg;
87             unsigned int tf, bw;
88             char layout = 0, end = 0;
89             name[0] = '\0';
90             loc.desktop = NULL;
91             sscanf(line + level, "%s %u %i %u %c %c", name, &bw, &wg, &tf, &layout, &end);
92             locate_desktop(name, &loc);
93             d = loc.desktop;
94             if (d == NULL)
95                 continue;
96             d->border_width = bw;
97             d->window_gap = wg;
98             d->tags_field = tf;
99             if (layout == 'M')
100                 d->layout = LAYOUT_MONOCLE;
101             else if (layout == 'T')
102                 d->layout = LAYOUT_TILED;
103             if (end != 0)
104                 m->desk = d;
105
106         } else {
107             if (m == NULL || d == NULL)
108                 continue;
109             node_t *birth = make_node();
110             if (level == 4) {
111                 empty_desktop(d);
112                 d->root = birth;
113             } else if (n != NULL) {
114                 if (level > last_level) {
115                     n->first_child = birth;
116                 } else {
117                     do {
118                         n = n->parent;
119                     } while (n != NULL && n->second_child != NULL);
120                     if (n == NULL)
121                         continue;
122                     n->second_child = birth;
123                 }
124                 birth->parent = n;
125             }
126             n = birth;
127
128             char br;
129             if (isupper(line[level])) {
130                 char st;
131                 sscanf(line + level, "%c %c %lf", &st, &br, &n->split_ratio);
132                 if (st == 'H')
133                     n->split_type = TYPE_HORIZONTAL;
134                 else if (st == 'V')
135                     n->split_type = TYPE_VERTICAL;
136             } else {
137                 client_t *c = make_client(XCB_NONE);
138                 num_clients++;
139                 char floating, transient, fullscreen, urgent, locked, sticky, sd, sm, end = 0;
140                 sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c %c%c%c%c%c%c%c %c", &br, c->class_name, &c->window, &c->tags_field, &c->border_width, &c->floating_rectangle.width, &c->floating_rectangle.height, &c->floating_rectangle.x, &c->floating_rectangle.y, &sd, &floating, &transient, &fullscreen, &urgent, &locked, &sticky, &sm, &end);
141                 c->floating = (floating == '-' ? false : true);
142                 c->transient = (transient == '-' ? false : true);
143                 c->fullscreen = (fullscreen == '-' ? false : true);
144                 c->urgent = (urgent == '-' ? false : true);
145                 c->locked = (locked == '-' ? false : true);
146                 c->sticky = (sticky == '-' ? false : true);
147                 n->split_mode = (sm == '-' ? MODE_AUTOMATIC : MODE_MANUAL);
148                 if (sd == 'U')
149                     n->split_dir = DIR_UP;
150                 else if (sd == 'R')
151                     n->split_dir = DIR_RIGHT;
152                 else if (sd == 'D')
153                     n->split_dir = DIR_DOWN;
154                 else if (sd == 'L')
155                     n->split_dir = DIR_LEFT;
156                 n->client = c;
157                 if (end != 0)
158                     d->focus = n;
159                 if (c->sticky)
160                     m->num_sticky++;
161             }
162             if (br == 'a')
163                 n->birth_rotation = 90;
164             else if (br == 'c')
165                 n->birth_rotation = 270;
166             else if (br == 'm')
167                 n->birth_rotation = 0;
168         }
169         last_level = level;
170     }
171
172     fclose(snapshot);
173
174     for (monitor_t *m = mon_head; m != NULL; m = m->next)
175         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
176             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
177                 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
178                 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
179                 if (n->client->floating || !is_visible(d, n)) {
180                     n->vacant = true;
181                     update_vacant_state(n->parent);
182                 }
183             }
184     ewmh_update_current_desktop();
185 }
186
187 void restore_history(char *file_path)
188 {
189     if (file_path == NULL)
190         return;
191
192     FILE *snapshot = fopen(file_path, "r");
193     if (snapshot == NULL) {
194         warn("Restore history: can't open '%s'.\n", file_path);
195         return;
196     }
197
198     PUTS("restore history");
199
200     char line[MAXLEN];
201     char mnm[SMALEN];
202     char dnm[SMALEN];
203     xcb_window_t win;
204
205     while (fgets(line, sizeof(line), snapshot) != NULL) {
206         if (sscanf(line, "%s %s %X", mnm, dnm, &win) == 3) {
207             coordinates_t loc;
208             if (win != XCB_NONE && !locate_window(win, &loc)) {
209                 warn("Can't locate window 0x%X.\n", win);
210                 continue;
211             }
212             node_t *n = (win == XCB_NONE ? NULL : loc.node);
213             if (!locate_desktop(dnm, &loc)) {
214                 warn("Can't locate desktop '%s'.\n", dnm);
215                 continue;
216             }
217             desktop_t *d = loc.desktop;
218             if (!locate_monitor(mnm, &loc)) {
219                 warn("Can't locate monitor '%s'.\n", mnm);
220                 continue;
221             }
222             monitor_t *m = loc.monitor;
223             history_add(m, d, n);
224         } else {
225             warn("Can't parse history entry: '%s'\n", line);
226         }
227     }
228
229     fclose(snapshot);
230 }
231
232 void restore_stack(char *file_path)
233 {
234     if (file_path == NULL)
235         return;
236
237     FILE *snapshot = fopen(file_path, "r");
238     if (snapshot == NULL) {
239         warn("Restore stack: can't open '%s'.\n", file_path);
240         return;
241     }
242
243     PUTS("restore stack");
244
245     char line[MAXLEN];
246     xcb_window_t win;
247
248     while (fgets(line, sizeof(line), snapshot) != NULL) {
249         if (sscanf(line, "%X", &win) == 1) {
250             coordinates_t loc;
251             if (locate_window(win, &loc))
252                 stack_insert_after(stack_tail, loc.node);
253             else
254                 warn("Can't locate window 0x%X.\n", win);
255         } else {
256             warn("Can't parse stack entry: '%s'\n", line);
257         }
258     }
259
260     fclose(snapshot);
261 }