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