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