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