]> git.lizzy.rs Git - bspwm.git/blob - src/desktop.c
Handle sticky nodes in `swap_desktops`
[bspwm.git] / src / 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 bool activate_desktop(monitor_t *m, desktop_t *d)
40 {
41         if (d != NULL && m == mon) {
42                 return false;
43         }
44
45         if (d == NULL) {
46                 d = m->desk;
47                 if (d == NULL) {
48                         d = history_last_desktop(m, NULL);
49                 }
50                 if (d == NULL) {
51                         d = m->desk_head;
52                 }
53         }
54
55         if (d == NULL || d == m->desk) {
56                 return false;
57         }
58
59         if (m->sticky_count > 0 && m->desk != NULL) {
60                 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
61         }
62
63         show_desktop(d);
64         hide_desktop(m->desk);
65
66         m->desk = d;
67
68         history_add(m, d, NULL, false);
69
70         put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
71         put_status(SBSC_MASK_REPORT);
72
73         return true;
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         d = (dir == CYCLE_PREV ? d->prev : d->next);
81
82 #define HANDLE_BOUNDARIES(m, d)  \
83         if (d == 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                 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
89         }
90         HANDLE_BOUNDARIES(m, d)
91
92         while (d != ref->desktop) {
93                 coordinates_t loc = {m, d, NULL};
94                 if (desktop_matches(&loc, ref, sel)) {
95                         *dst = loc;
96                         return true;
97                 }
98                 d = (dir == CYCLE_PREV ? d->prev : d->next);
99                 HANDLE_BOUNDARIES(m, d)
100         }
101 #undef HANDLE_BOUNDARIES
102
103         return false;
104 }
105
106 bool find_any_desktop(coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel)
107 {
108         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
109                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
110                         coordinates_t loc = {m, d, NULL};
111                         if (desktop_matches(&loc, ref, sel)) {
112                                 *dst = loc;
113                                 return true;
114                         }
115                 }
116         }
117         return false;
118 }
119
120 bool set_layout(monitor_t *m, desktop_t *d, layout_t l, bool user)
121 {
122         if ((user && d->user_layout == l) || (!user && d->layout == l)) {
123                 return false;
124         }
125
126         layout_t old_layout = d->layout;
127
128         if (user) {
129                 d->user_layout = l;
130         } else {
131                 d->layout = l;
132         }
133
134         if (user && (!single_monocle || tiled_count(d->root, true) > 1)) {
135                 d->layout = l;
136         }
137
138         if (d->layout != old_layout) {
139                 handle_presel_feedbacks(m, d);
140
141                 if (user) {
142                         arrange(m, d);
143                 }
144
145                 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(d->layout));
146
147                 if (d == m->desk) {
148                         put_status(SBSC_MASK_REPORT);
149                 }
150         }
151
152         return true;
153 }
154
155 void handle_presel_feedbacks(monitor_t *m, desktop_t *d)
156 {
157         if (m->desk != d) {
158                 return;
159         }
160         if (d->layout == LAYOUT_MONOCLE) {
161                 hide_presel_feedbacks(m, d, d->root);
162         } else {
163                 show_presel_feedbacks(m, d, d->root);
164         }
165 }
166
167 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d, bool follow)
168 {
169         if (ms == NULL || md == NULL || d == NULL || ms == md) {
170                 return false;
171         }
172
173         bool d_was_active = (d == ms->desk);
174         bool ms_was_focused = (ms == mon);
175         unsigned int sc = (ms->sticky_count > 0 && d_was_active) ? sticky_count(d->root) : 0;
176
177         unlink_desktop(ms, d);
178         ms->sticky_count -= sc;
179
180         if ((!follow || !d_was_active || !ms_was_focused) && md->desk != NULL) {
181                 hide_sticky = false;
182                 hide_desktop(d);
183                 hide_sticky = true;
184         }
185
186         insert_desktop(md, d);
187         md->sticky_count += sc;
188         history_remove(d, NULL, false);
189
190         if (d_was_active) {
191                 if (follow) {
192                         if (activate_desktop(ms, NULL)) {
193                                 activate_node(ms, ms->desk, NULL);
194                         }
195                         if (ms_was_focused) {
196                                 focus_node(md, d, d->focus);
197                         }
198                 } else {
199                         if (ms_was_focused) {
200                                 focus_node(ms, ms->desk, NULL);
201                         } else if (activate_desktop(ms, NULL)) {
202                                 activate_node(ms, ms->desk, NULL);
203                         }
204                 }
205         }
206
207         if (sc > 0) {
208                 if (ms->desk != NULL) {
209                         transfer_sticky_nodes(md, d, ms, ms->desk, d->root);
210                 } else if (d != md->desk) {
211                         transfer_sticky_nodes(md, d, md, md->desk, d->root);
212                 }
213         }
214
215         adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
216         arrange(md, d);
217
218         if ((!follow || !d_was_active || !ms_was_focused) && md->desk == d) {
219                 if (md == mon) {
220                         focus_node(md, d, d->focus);
221                 } else {
222                         activate_node(md, d, d->focus);
223                 }
224         }
225
226         ewmh_update_wm_desktops();
227         ewmh_update_desktop_names();
228         ewmh_update_desktop_viewport();
229         ewmh_update_current_desktop();
230
231         put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
232         put_status(SBSC_MASK_REPORT);
233
234         return true;
235 }
236
237 desktop_t *make_desktop(const char *name, uint32_t id)
238 {
239         desktop_t *d = calloc(1, sizeof(desktop_t));
240         snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
241         if (id == XCB_NONE) {
242                 d->id = xcb_generate_id(dpy);
243         }
244         d->prev = d->next = NULL;
245         d->root = d->focus = NULL;
246         d->user_layout = LAYOUT_TILED;
247         d->layout = single_monocle ? LAYOUT_MONOCLE : LAYOUT_TILED;
248         d->padding = (padding_t) PADDING;
249         d->window_gap = window_gap;
250         d->border_width = border_width;
251         return d;
252 }
253
254 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
255 {
256
257         put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
258
259         snprintf(d->name, sizeof(d->name), "%s", name);
260
261         put_status(SBSC_MASK_REPORT);
262         ewmh_update_desktop_names();
263 }
264
265 void insert_desktop(monitor_t *m, desktop_t *d)
266 {
267         if (m->desk == NULL) {
268                 m->desk = d;
269                 m->desk_head = d;
270                 m->desk_tail = d;
271         } else {
272                 m->desk_tail->next = d;
273                 d->prev = m->desk_tail;
274                 m->desk_tail = d;
275         }
276 }
277
278 void add_desktop(monitor_t *m, desktop_t *d)
279 {
280         put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X 0x%08X %s\n", m->id, d->id, d->name);
281
282         d->border_width = m->border_width;
283         d->window_gap = m->window_gap;
284         insert_desktop(m, d);
285         ewmh_update_current_desktop();
286         ewmh_update_number_of_desktops();
287         ewmh_update_desktop_names();
288         ewmh_update_desktop_viewport();
289         ewmh_update_wm_desktops();
290         put_status(SBSC_MASK_REPORT);
291 }
292
293 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
294 {
295         if (m == NULL) {
296                 return NULL;
297         }
298
299         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
300                 if (d->id == id) {
301                         return d;
302                 }
303         }
304
305         return NULL;
306 }
307
308 void unlink_desktop(monitor_t *m, desktop_t *d)
309 {
310         desktop_t *prev = d->prev;
311         desktop_t *next = d->next;
312
313         if (prev != NULL) {
314                 prev->next = next;
315         }
316
317         if (next != NULL) {
318                 next->prev = prev;
319         }
320
321         if (m->desk_head == d) {
322                 m->desk_head = next;
323         }
324
325         if (m->desk_tail == d) {
326                 m->desk_tail = prev;
327         }
328
329         if (m->desk == d) {
330                 m->desk = NULL;
331         }
332
333         d->prev = d->next = NULL;
334 }
335
336 void remove_desktop(monitor_t *m, desktop_t *d)
337 {
338         put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
339
340         remove_node(m, d, d->root);
341         unlink_desktop(m, d);
342         history_remove(d, NULL, false);
343         free(d);
344
345         ewmh_update_current_desktop();
346         ewmh_update_number_of_desktops();
347         ewmh_update_desktop_names();
348         ewmh_update_desktop_viewport();
349
350         if (mon != NULL && m->desk == NULL) {
351                 if (m == mon) {
352                         focus_node(m, NULL, NULL);
353                 } else {
354                         activate_desktop(m, NULL);
355                         if (m->desk != NULL) {
356                                 activate_node(m, m->desk, m->desk->focus);
357                         }
358                 }
359         }
360
361         put_status(SBSC_MASK_REPORT);
362 }
363
364 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
365 {
366         if (ds == NULL || dd == NULL || ds == dd) {
367                 return;
368         }
369         /* TODO: Handle sticky nodes. */
370         transfer_node(ms, ds, ds->root, md, dd, dd->focus, false);
371 }
372
373 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
374 {
375         if (d1 == NULL || d2 == NULL || d1 == d2) {
376                 return false;
377         }
378
379         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);
380
381         bool d1_was_active = (m1->desk == d1);
382         bool d2_was_active = (m2->desk == d2);
383         bool d1_was_focused = (mon->desk == d1);
384         bool d2_was_focused = (mon->desk == d2);
385         desktop_t *d1_stickies = NULL;
386         desktop_t *d2_stickies = NULL;
387
388         if (m1->sticky_count > 0 && d1 == m1->desk && sticky_count(d1->root) > 0) {
389                 d1_stickies = make_desktop(NULL, XCB_NONE);
390                 insert_desktop(m1, d1_stickies);
391                 transfer_sticky_nodes(m1, d1, m1, d1_stickies, d1->root);
392         }
393
394         if (m2->sticky_count > 0 && d2 == m2->desk && sticky_count(d2->root) > 0) {
395                 d2_stickies = make_desktop(NULL, XCB_NONE);
396                 insert_desktop(m2, d2_stickies);
397                 transfer_sticky_nodes(m2, d2, m2, d2_stickies, d2->root);
398         }
399
400         if (m1 != m2) {
401                 if (m1->desk == d1) {
402                         m1->desk = d2;
403                 }
404                 if (m1->desk_head == d1) {
405                         m1->desk_head = d2;
406                 }
407                 if (m1->desk_tail == d1) {
408                         m1->desk_tail = d2;
409                 }
410                 if (m2->desk == d2) {
411                         m2->desk = d1;
412                 }
413                 if (m2->desk_head == d2) {
414                         m2->desk_head = d1;
415                 }
416                 if (m2->desk_tail == d2) {
417                         m2->desk_tail = d1;
418                 }
419         } else {
420                 if (m1->desk == d1) {
421                         m1->desk = d2;
422                 } else if (m1->desk == d2) {
423                         m1->desk = d1;
424                 }
425                 if (m1->desk_head == d1) {
426                         m1->desk_head = d2;
427                 } else if (m1->desk_head == d2) {
428                         m1->desk_head = d1;
429                 }
430                 if (m1->desk_tail == d1) {
431                         m1->desk_tail = d2;
432                 } else if (m1->desk_tail == d2) {
433                         m1->desk_tail = d1;
434                 }
435         }
436
437         desktop_t *p1 = d1->prev;
438         desktop_t *n1 = d1->next;
439         desktop_t *p2 = d2->prev;
440         desktop_t *n2 = d2->next;
441
442         if (p1 != NULL && p1 != d2) {
443                 p1->next = d2;
444         }
445         if (n1 != NULL && n1 != d2) {
446                 n1->prev = d2;
447         }
448         if (p2 != NULL && p2 != d1) {
449                 p2->next = d1;
450         }
451         if (n2 != NULL && n2 != d1) {
452                 n2->prev = d1;
453         }
454
455         d1->prev = p2 == d1 ? d2 : p2;
456         d1->next = n2 == d1 ? d2 : n2;
457         d2->prev = p1 == d2 ? d1 : p1;
458         d2->next = n1 == d2 ? d1 : n1;
459
460         if (m1 != m2) {
461                 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
462                 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
463                 history_remove(d1, NULL, false);
464                 history_remove(d2, NULL, false);
465                 arrange(m1, d2);
466                 arrange(m2, d1);
467         }
468
469         if (d1_stickies != NULL) {
470                 transfer_sticky_nodes(m1, d1_stickies, m1, d2, d1_stickies->root);
471                 unlink_desktop(m1, d1_stickies);
472                 free(d1_stickies);
473         }
474
475         if (d2_stickies != NULL) {
476                 transfer_sticky_nodes(m2, d2_stickies, m2, d1, d2_stickies->root);
477                 unlink_desktop(m2, d2_stickies);
478                 free(d2_stickies);
479         }
480
481         if (d1_was_active && !d2_was_active) {
482                 if ((!follow && m1 != m2) || !d1_was_focused) {
483                         hide_desktop(d1);
484                 }
485                 show_desktop(d2);
486         } else if (!d1_was_active && d2_was_active) {
487                 show_desktop(d1);
488                 if ((!follow && m1 != m2) || !d2_was_focused) {
489                         hide_desktop(d2);
490                 }
491         }
492
493         if (follow || m1 == m2) {
494                 if (d1_was_focused) {
495                         focus_node(m2, d1, d1->focus);
496                 } else if (d1_was_active) {
497                         activate_node(m2, d1, d1->focus);
498                 }
499
500                 if (d2_was_focused) {
501                         focus_node(m1, d2, d2->focus);
502                 } else if (d2_was_active) {
503                         activate_node(m1, d2, d2->focus);
504                 }
505         } else {
506                 if (d1_was_focused) {
507                         focus_node(m1, d2, d2->focus);
508                 } else if (d1_was_active) {
509                         activate_node(m1, d2, d2->focus);
510                 }
511
512                 if (d2_was_focused) {
513                         focus_node(m2, d1, d1->focus);
514                 } else if (d2_was_active) {
515                         activate_node(m2, d1, d1->focus);
516                 }
517         }
518
519         ewmh_update_wm_desktops();
520         ewmh_update_desktop_names();
521         ewmh_update_desktop_viewport();
522         ewmh_update_current_desktop();
523
524         put_status(SBSC_MASK_REPORT);
525
526         return true;
527 }
528
529 void show_desktop(desktop_t *d)
530 {
531         if (d == NULL) {
532                 return;
533         }
534         show_node(d, d->root);
535 }
536
537 void hide_desktop(desktop_t *d)
538 {
539         if (d == NULL) {
540                 return;
541         }
542         hide_node(d, d->root);
543 }
544
545 bool is_urgent(desktop_t *d)
546 {
547         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
548                 if (n->client == NULL) {
549                         continue;
550                 }
551                 if (n->client->urgent) {
552                         return true;
553                 }
554         }
555         return false;
556 }