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