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