]> git.lizzy.rs Git - bspwm.git/blob - desktop.c
Fix memory leak in remove_node
[bspwm.git] / desktop.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 <stdbool.h>
28 #include "bspwm.h"
29 #include "ewmh.h"
30 #include "history.h"
31 #include "monitor.h"
32 #include "query.h"
33 #include "tree.h"
34 #include "desktop.h"
35 #include "subscribe.h"
36 #include "settings.h"
37
38 void focus_desktop(monitor_t *m, desktop_t *d)
39 {
40         bool changed = (m != mon || m->desk != d);
41
42         focus_monitor(m);
43
44         if (m->desk != d) {
45                 if (focus_follows_pointer) {
46                         listen_enter_notify(d->root, false);
47                 }
48                 show_desktop(d);
49                 hide_desktop(m->desk);
50                 if (focus_follows_pointer) {
51                         listen_enter_notify(d->root, true);
52                 }
53                 m->desk = d;
54         }
55
56         if (changed) {
57                 ewmh_update_current_desktop();
58                 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
59         }
60 }
61
62 bool activate_desktop(monitor_t *m, desktop_t *d)
63 {
64         if (m == mon || d == m->desk) {
65                 return false;
66         }
67
68         if (m->sticky_count > 0) {
69                 sticky_still = false;
70                 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
71                 sticky_still = true;
72         }
73
74         show_desktop(d);
75         hide_desktop(m->desk);
76
77         m->desk = d;
78
79         put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
80         put_status(SBSC_MASK_REPORT);
81
82         return true;
83 }
84
85 bool find_closest_desktop(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, desktop_select_t sel)
86 {
87         monitor_t *m = ref->monitor;
88         desktop_t *d = ref->desktop;
89         desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
90
91 #define HANDLE_BOUNDARIES(f)  \
92         if (f == NULL) { \
93                 m = (dir == CYCLE_PREV ? m->prev : m->next); \
94                 if (m == NULL) { \
95                         m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
96                 } \
97                 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
98         }
99         HANDLE_BOUNDARIES(f)
100
101         while (f != d) {
102                 coordinates_t loc = {m, f, NULL};
103                 if (desktop_matches(&loc, ref, sel)) {
104                         *dst = loc;
105                         return true;
106                 }
107                 f = (dir == CYCLE_PREV ? f->prev : f->next);
108                 HANDLE_BOUNDARIES(f)
109         }
110 #undef HANDLE_BOUNDARIES
111
112         return false;
113 }
114
115 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
116 {
117         d->layout = l;
118         arrange(m, d);
119
120         put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(l));
121
122         if (d == m->desk) {
123                 put_status(SBSC_MASK_REPORT);
124         }
125 }
126
127 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
128 {
129         if (ms == NULL || md == NULL || d == NULL || ms == md) {
130                 return false;
131         }
132
133         bool was_active = (d == ms->desk);
134
135         unlink_desktop(ms, d);
136
137         if (md->desk != NULL) {
138                 hide_desktop(d);
139         }
140
141         insert_desktop(md, d);
142
143         if (was_active) {
144                 if (mon == ms) {
145                         focus_node(ms, NULL, NULL);
146                 } else {
147                         activate_node(ms, NULL, NULL);
148                 }
149         }
150
151         if (ms->sticky_count > 0 && was_active) {
152                 sticky_still = false;
153                 transfer_sticky_nodes(ms, d, ms->desk, d->root);
154                 sticky_still = true;
155         }
156
157         history_transfer_desktop(md, d);
158         adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
159         arrange(md, d);
160
161
162         if (md->desk == d) {
163                 if (mon == md) {
164                         focus_node(md, d, d->focus);
165                 } else {
166                         activate_node(md, d, d->focus);
167                 }
168         }
169
170         ewmh_update_wm_desktops();
171         ewmh_update_desktop_names();
172         ewmh_update_current_desktop();
173
174         put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
175         put_status(SBSC_MASK_REPORT);
176
177         return true;
178 }
179
180 desktop_t *make_desktop(const char *name, uint32_t id)
181 {
182         desktop_t *d = malloc(sizeof(desktop_t));
183         snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
184         if (id == XCB_NONE) {
185                 d->id = xcb_generate_id(dpy);
186         }
187         d->prev = d->next = NULL;
188         d->root = d->focus = NULL;
189         d->layout = LAYOUT_TILED;
190         d->padding = (padding_t) PADDING;
191         d->window_gap = window_gap;
192         d->border_width = border_width;
193         return d;
194 }
195
196 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
197 {
198
199         put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
200
201         snprintf(d->name, sizeof(d->name), "%s", name);
202
203         put_status(SBSC_MASK_REPORT);
204         ewmh_update_desktop_names();
205 }
206
207 void insert_desktop(monitor_t *m, desktop_t *d)
208 {
209         if (m->desk == NULL) {
210                 m->desk = d;
211                 m->desk_head = d;
212                 m->desk_tail = d;
213         } else {
214                 m->desk_tail->next = d;
215                 d->prev = m->desk_tail;
216                 m->desk_tail = d;
217         }
218 }
219
220 void add_desktop(monitor_t *m, desktop_t *d)
221 {
222         put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X %s 0x%08X\n", d->id, d->name, m->id);
223
224         d->border_width = m->border_width;
225         d->window_gap = m->window_gap;
226         insert_desktop(m, d);
227         ewmh_update_number_of_desktops();
228         ewmh_update_desktop_names();
229         ewmh_update_wm_desktops();
230         put_status(SBSC_MASK_REPORT);
231 }
232
233 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
234 {
235         if (m == NULL) {
236                 return NULL;
237         }
238
239         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
240                 if (d->id == id) {
241                         return d;
242                 }
243         }
244
245         return NULL;
246 }
247
248 void unlink_desktop(monitor_t *m, desktop_t *d)
249 {
250         desktop_t *prev = d->prev;
251         desktop_t *next = d->next;
252
253         if (prev != NULL) {
254                 prev->next = next;
255         }
256
257         if (next != NULL) {
258                 next->prev = prev;
259         }
260
261         if (m->desk_head == d) {
262                 m->desk_head = next;
263         }
264
265         if (m->desk_tail == d) {
266                 m->desk_tail = prev;
267         }
268
269         if (m->desk == d) {
270                 m->desk = NULL;
271         }
272
273         d->prev = d->next = NULL;
274 }
275
276 void remove_desktop(monitor_t *m, desktop_t *d)
277 {
278         put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
279
280         history_remove(d, NULL, false);
281         unlink_desktop(m, d);
282         remove_node(m, d, d->root);
283         free(d);
284
285         ewmh_update_current_desktop();
286         ewmh_update_number_of_desktops();
287         ewmh_update_desktop_names();
288
289         if (mon != NULL && m->desk == NULL) {
290                 if (m == mon) {
291                         focus_node(m, NULL, NULL);
292                 } else {
293                         activate_node(m, NULL, NULL);
294                 }
295         }
296
297         put_status(SBSC_MASK_REPORT);
298 }
299
300 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
301 {
302         if (ds == NULL || dd == NULL || ds == dd) {
303                 return;
304         }
305         transfer_node(ms, ds, ds->root, md, dd, dd->focus);
306 }
307
308 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
309 {
310         if (d1 == NULL || d2 == NULL || d1 == d2 ||
311             (m1->desk == d1 && m1->sticky_count > 0) ||
312             (m2->desk == d2 && m2->sticky_count > 0)) {
313                 return false;
314         }
315
316         put_status(SBSC_MASK_DESKTOP_SWAP, "desktop_swap 0x%08X 0x%08X 0x%08X 0x%08X\n", m1->id, d1->id, m2->id, d2->id);
317
318         bool d1_focused = (m1->desk == d1);
319         bool d2_focused = (m2->desk == d2);
320
321         if (m1 != m2) {
322                 if (m1->desk == d1) {
323                         m1->desk = d2;
324                 }
325                 if (m1->desk_head == d1) {
326                         m1->desk_head = d2;
327                 }
328                 if (m1->desk_tail == d1) {
329                         m1->desk_tail = d2;
330                 }
331                 if (m2->desk == d2) {
332                         m2->desk = d1;
333                 }
334                 if (m2->desk_head == d2) {
335                         m2->desk_head = d1;
336                 }
337                 if (m2->desk_tail == d2) {
338                         m2->desk_tail = d1;
339                 }
340         } else {
341                 if (m1->desk == d1) {
342                         m1->desk = d2;
343                 } else if (m1->desk == d2) {
344                         m1->desk = d1;
345                 }
346                 if (m1->desk_head == d1) {
347                         m1->desk_head = d2;
348                 } else if (m1->desk_head == d2) {
349                         m1->desk_head = d1;
350                 }
351                 if (m1->desk_tail == d1) {
352                         m1->desk_tail = d2;
353                 } else if (m1->desk_tail == d2) {
354                         m1->desk_tail = d1;
355                 }
356         }
357
358         desktop_t *p1 = d1->prev;
359         desktop_t *n1 = d1->next;
360         desktop_t *p2 = d2->prev;
361         desktop_t *n2 = d2->next;
362
363         if (p1 != NULL && p1 != d2) {
364                 p1->next = d2;
365         }
366         if (n1 != NULL && n1 != d2) {
367                 n1->prev = d2;
368         }
369         if (p2 != NULL && p2 != d1) {
370                 p2->next = d1;
371         }
372         if (n2 != NULL && n2 != d1) {
373                 n2->prev = d1;
374         }
375
376         d1->prev = p2 == d1 ? d2 : p2;
377         d1->next = n2 == d1 ? d2 : n2;
378         d2->prev = p1 == d2 ? d1 : p1;
379         d2->next = n1 == d2 ? d1 : n1;
380
381         if (m1 != m2) {
382                 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
383                 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
384                 history_swap_desktops(m1, d1, m2, d2);
385                 arrange(m1, d2);
386                 arrange(m2, d1);
387         }
388
389         if (d1_focused && !d2_focused) {
390                 hide_desktop(d1);
391                 show_desktop(d2);
392         } else if (!d1_focused && d2_focused) {
393                 show_desktop(d1);
394                 hide_desktop(d2);
395         }
396
397         if (d1 == mon->desk) {
398                 focus_node(m2, d1, d1->focus);
399         } else if (d1 == m2->desk) {
400                 activate_node(m2, d1, d1->focus);
401         }
402
403         if (d2 == mon->desk) {
404                 focus_node(m1, d2, d2->focus);
405         } else if (d2 == m1->desk) {
406                 activate_node(m1, d2, d2->focus);
407         }
408
409         ewmh_update_wm_desktops();
410         ewmh_update_desktop_names();
411         ewmh_update_current_desktop();
412
413         put_status(SBSC_MASK_REPORT);
414
415         return true;
416 }
417
418 void show_desktop(desktop_t *d)
419 {
420         if (d == NULL) {
421                 return;
422         }
423         show_node(d->root);
424 }
425
426 void hide_desktop(desktop_t *d)
427 {
428         if (d == NULL) {
429                 return;
430         }
431         hide_node(d->root);
432 }
433
434 bool is_urgent(desktop_t *d)
435 {
436         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
437                 if (n->client == NULL) {
438                         continue;
439                 }
440                 if (n->client->urgent) {
441                         return true;
442                 }
443         }
444         return false;
445 }