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