1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
36 #include "subscribe.h"
39 bool activate_desktop(monitor_t *m, desktop_t *d)
41 if (d != NULL && m == mon) {
48 d = history_last_desktop(m, NULL);
55 if (d == NULL || d == m->desk) {
59 if (m->sticky_count > 0 && m->desk != NULL) {
60 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
64 hide_desktop(m->desk);
68 history_add(m, d, NULL, false);
70 put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
71 put_status(SBSC_MASK_REPORT);
76 bool find_closest_desktop(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, desktop_select_t *sel)
78 monitor_t *m = ref->monitor;
79 desktop_t *d = ref->desktop;
80 d = (dir == CYCLE_PREV ? d->prev : d->next);
82 #define HANDLE_BOUNDARIES(m, d) \
84 m = (dir == CYCLE_PREV ? m->prev : m->next); \
86 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
88 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
90 HANDLE_BOUNDARIES(m, d)
92 while (d != ref->desktop) {
93 coordinates_t loc = {m, d, NULL};
94 if (desktop_matches(&loc, ref, sel)) {
98 d = (dir == CYCLE_PREV ? d->prev : d->next);
99 HANDLE_BOUNDARIES(m, d)
101 #undef HANDLE_BOUNDARIES
106 bool find_any_desktop(coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel)
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)) {
120 bool set_layout(monitor_t *m, desktop_t *d, layout_t l, bool user)
122 if ((user && d->user_layout == l) || (!user && d->layout == l)) {
126 layout_t old_layout = d->layout;
134 if (user && (!single_monocle || tiled_count(d->root, true) > 1)) {
138 if (d->layout != old_layout) {
139 handle_presel_feedbacks(m, d);
145 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(d->layout));
148 put_status(SBSC_MASK_REPORT);
155 void handle_presel_feedbacks(monitor_t *m, desktop_t *d)
160 if (d->layout == LAYOUT_MONOCLE) {
161 hide_presel_feedbacks(m, d, d->root);
163 show_presel_feedbacks(m, d, d->root);
167 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d, bool follow)
169 if (ms == NULL || md == NULL || d == NULL || ms == md) {
173 bool d_was_active = (d == ms->desk);
174 bool ms_was_focused = (ms == mon);
175 unsigned int sc = (ms->sticky_count > 0 && d_was_active) ? sticky_count(d->root) : 0;
177 unlink_desktop(ms, d);
178 ms->sticky_count -= sc;
180 if ((!follow || !d_was_active || !ms_was_focused) && md->desk != NULL) {
186 insert_desktop(md, d);
187 md->sticky_count += sc;
188 history_remove(d, NULL, false);
192 if (activate_desktop(ms, NULL)) {
193 activate_node(ms, ms->desk, NULL);
195 if (ms_was_focused) {
196 focus_node(md, d, d->focus);
199 if (ms_was_focused) {
200 focus_node(ms, ms->desk, NULL);
201 } else if (activate_desktop(ms, NULL)) {
202 activate_node(ms, ms->desk, NULL);
208 if (ms->desk != NULL) {
209 transfer_sticky_nodes(md, d, ms, ms->desk, d->root);
210 } else if (d != md->desk) {
211 transfer_sticky_nodes(md, d, md, md->desk, d->root);
215 adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
218 if ((!follow || !d_was_active || !ms_was_focused) && md->desk == d) {
220 focus_node(md, d, d->focus);
222 activate_node(md, d, d->focus);
226 ewmh_update_wm_desktops();
227 ewmh_update_desktop_names();
228 ewmh_update_desktop_viewport();
229 ewmh_update_current_desktop();
231 put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
232 put_status(SBSC_MASK_REPORT);
237 desktop_t *make_desktop(const char *name, uint32_t id)
239 desktop_t *d = calloc(1, sizeof(desktop_t));
240 snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
241 if (id == XCB_NONE) {
242 d->id = xcb_generate_id(dpy);
244 d->prev = d->next = NULL;
245 d->root = d->focus = NULL;
246 d->user_layout = LAYOUT_TILED;
247 d->layout = single_monocle ? LAYOUT_MONOCLE : LAYOUT_TILED;
248 d->padding = (padding_t) PADDING;
249 d->window_gap = window_gap;
250 d->border_width = border_width;
254 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
257 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
259 snprintf(d->name, sizeof(d->name), "%s", name);
261 put_status(SBSC_MASK_REPORT);
262 ewmh_update_desktop_names();
265 void insert_desktop(monitor_t *m, desktop_t *d)
267 if (m->desk == NULL) {
272 m->desk_tail->next = d;
273 d->prev = m->desk_tail;
278 void add_desktop(monitor_t *m, desktop_t *d)
280 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X 0x%08X %s\n", m->id, d->id, d->name);
282 d->border_width = m->border_width;
283 d->window_gap = m->window_gap;
284 insert_desktop(m, d);
285 ewmh_update_current_desktop();
286 ewmh_update_number_of_desktops();
287 ewmh_update_desktop_names();
288 ewmh_update_desktop_viewport();
289 ewmh_update_wm_desktops();
290 put_status(SBSC_MASK_REPORT);
293 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
299 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
308 void unlink_desktop(monitor_t *m, desktop_t *d)
310 desktop_t *prev = d->prev;
311 desktop_t *next = d->next;
321 if (m->desk_head == d) {
325 if (m->desk_tail == d) {
333 d->prev = d->next = NULL;
336 void remove_desktop(monitor_t *m, desktop_t *d)
338 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
340 remove_node(m, d, d->root);
341 unlink_desktop(m, d);
342 history_remove(d, NULL, false);
345 ewmh_update_current_desktop();
346 ewmh_update_number_of_desktops();
347 ewmh_update_desktop_names();
348 ewmh_update_desktop_viewport();
350 if (mon != NULL && m->desk == NULL) {
352 focus_node(m, NULL, NULL);
354 activate_desktop(m, NULL);
355 if (m->desk != NULL) {
356 activate_node(m, m->desk, m->desk->focus);
361 put_status(SBSC_MASK_REPORT);
364 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
366 if (ds == NULL || dd == NULL || ds == dd) {
369 /* TODO: Handle sticky nodes. */
370 transfer_node(ms, ds, ds->root, md, dd, dd->focus, false);
373 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
375 if (d1 == NULL || d2 == NULL || d1 == d2) {
379 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);
381 bool d1_was_active = (m1->desk == d1);
382 bool d2_was_active = (m2->desk == d2);
383 bool d1_was_focused = (mon->desk == d1);
384 bool d2_was_focused = (mon->desk == d2);
385 desktop_t *d1_stickies = NULL;
386 desktop_t *d2_stickies = NULL;
388 if (m1->sticky_count > 0 && d1 == m1->desk && sticky_count(d1->root) > 0) {
389 d1_stickies = make_desktop(NULL, XCB_NONE);
390 insert_desktop(m1, d1_stickies);
391 transfer_sticky_nodes(m1, d1, m1, d1_stickies, d1->root);
394 if (m2->sticky_count > 0 && d2 == m2->desk && sticky_count(d2->root) > 0) {
395 d2_stickies = make_desktop(NULL, XCB_NONE);
396 insert_desktop(m2, d2_stickies);
397 transfer_sticky_nodes(m2, d2, m2, d2_stickies, d2->root);
401 if (m1->desk == d1) {
404 if (m1->desk_head == d1) {
407 if (m1->desk_tail == d1) {
410 if (m2->desk == d2) {
413 if (m2->desk_head == d2) {
416 if (m2->desk_tail == d2) {
420 if (m1->desk == d1) {
422 } else if (m1->desk == d2) {
425 if (m1->desk_head == d1) {
427 } else if (m1->desk_head == d2) {
430 if (m1->desk_tail == d1) {
432 } else if (m1->desk_tail == d2) {
437 desktop_t *p1 = d1->prev;
438 desktop_t *n1 = d1->next;
439 desktop_t *p2 = d2->prev;
440 desktop_t *n2 = d2->next;
442 if (p1 != NULL && p1 != d2) {
445 if (n1 != NULL && n1 != d2) {
448 if (p2 != NULL && p2 != d1) {
451 if (n2 != NULL && n2 != d1) {
455 d1->prev = p2 == d1 ? d2 : p2;
456 d1->next = n2 == d1 ? d2 : n2;
457 d2->prev = p1 == d2 ? d1 : p1;
458 d2->next = n1 == d2 ? d1 : n1;
461 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
462 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
463 history_remove(d1, NULL, false);
464 history_remove(d2, NULL, false);
469 if (d1_stickies != NULL) {
470 transfer_sticky_nodes(m1, d1_stickies, m1, d2, d1_stickies->root);
471 unlink_desktop(m1, d1_stickies);
475 if (d2_stickies != NULL) {
476 transfer_sticky_nodes(m2, d2_stickies, m2, d1, d2_stickies->root);
477 unlink_desktop(m2, d2_stickies);
481 if (d1_was_active && !d2_was_active) {
482 if ((!follow && m1 != m2) || !d1_was_focused) {
486 } else if (!d1_was_active && d2_was_active) {
488 if ((!follow && m1 != m2) || !d2_was_focused) {
493 if (follow || m1 == m2) {
494 if (d1_was_focused) {
495 focus_node(m2, d1, d1->focus);
496 } else if (d1_was_active) {
497 activate_node(m2, d1, d1->focus);
500 if (d2_was_focused) {
501 focus_node(m1, d2, d2->focus);
502 } else if (d2_was_active) {
503 activate_node(m1, d2, d2->focus);
506 if (d1_was_focused) {
507 focus_node(m1, d2, d2->focus);
508 } else if (d1_was_active) {
509 activate_node(m1, d2, d2->focus);
512 if (d2_was_focused) {
513 focus_node(m2, d1, d1->focus);
514 } else if (d2_was_active) {
515 activate_node(m2, d1, d1->focus);
519 ewmh_update_wm_desktops();
520 ewmh_update_desktop_names();
521 ewmh_update_desktop_viewport();
522 ewmh_update_current_desktop();
524 put_status(SBSC_MASK_REPORT);
529 void show_desktop(desktop_t *d)
534 show_node(d, d->root);
537 void hide_desktop(desktop_t *d)
542 hide_node(d, d->root);
545 bool is_urgent(desktop_t *d)
547 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
548 if (n->client == NULL) {
551 if (n->client->urgent) {