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