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 void focus_desktop(monitor_t *m, desktop_t *d)
41 bool changed = (m != mon || m->desk != d);
47 hide_desktop(m->desk);
52 ewmh_update_current_desktop();
53 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
57 bool activate_desktop(monitor_t *m, desktop_t *d)
59 if (d != NULL && m == mon) {
66 d = history_last_desktop(m, NULL);
73 if (d == NULL || d == m->desk) {
77 if (m->sticky_count > 0 && m->desk != NULL) {
78 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
82 hide_desktop(m->desk);
86 history_add(m, d, NULL, false);
88 put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
89 put_status(SBSC_MASK_REPORT);
94 bool find_closest_desktop(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, desktop_select_t *sel)
96 monitor_t *m = ref->monitor;
97 desktop_t *d = ref->desktop;
98 d = (dir == CYCLE_PREV ? d->prev : d->next);
100 #define HANDLE_BOUNDARIES(m, d) \
102 m = (dir == CYCLE_PREV ? m->prev : m->next); \
104 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
106 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
108 HANDLE_BOUNDARIES(m, d)
110 while (d != ref->desktop) {
111 coordinates_t loc = {m, d, NULL};
112 if (desktop_matches(&loc, ref, sel)) {
116 d = (dir == CYCLE_PREV ? d->prev : d->next);
117 HANDLE_BOUNDARIES(m, d)
119 #undef HANDLE_BOUNDARIES
124 bool find_any_desktop(coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel)
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)) {
138 bool set_layout(monitor_t *m, desktop_t *d, layout_t l, bool user)
140 if ((user && d->user_layout == l) || (!user && d->layout == l)) {
144 layout_t old_layout = d->layout;
152 if (user && (!single_monocle || tiled_count(d->root, true) > 1)) {
156 if (d->layout != old_layout) {
157 handle_presel_feedbacks(m, d);
163 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(d->layout));
166 put_status(SBSC_MASK_REPORT);
175 void handle_presel_feedbacks(monitor_t *m, desktop_t *d)
180 if (d->layout == LAYOUT_MONOCLE) {
181 hide_presel_feedbacks(m, d, d->root);
183 show_presel_feedbacks(m, d, d->root);
187 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d, bool follow)
189 if (ms == NULL || md == NULL || d == NULL || ms == md) {
193 bool d_was_active = (d == ms->desk);
194 bool ms_was_focused = (ms == mon);
196 unlink_desktop(ms, d);
198 if ((!follow || !d_was_active || !ms_was_focused) && md->desk != NULL) {
204 insert_desktop(md, d);
205 history_remove(d, NULL, false);
209 if (activate_desktop(ms, NULL)) {
210 activate_node(ms, ms->desk, NULL);
212 if (ms_was_focused) {
213 focus_node(md, d, d->focus);
216 if (ms_was_focused) {
217 focus_node(ms, ms->desk, NULL);
218 } else if (activate_desktop(ms, NULL)) {
219 activate_node(ms, ms->desk, NULL);
224 if (ms->sticky_count > 0 && d_was_active) {
225 if (ms->desk != NULL) {
226 transfer_sticky_nodes(md, d, ms, ms->desk, d->root);
228 ms->sticky_count -= sticky_count(d->root);
229 md->sticky_count += sticky_count(d->root);
231 transfer_sticky_nodes(md, d, md, md->desk, d->root);
236 adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
239 if ((!follow || !d_was_active || !ms_was_focused) && md->desk == d) {
241 focus_node(md, d, d->focus);
243 activate_node(md, d, d->focus);
247 ewmh_update_wm_desktops();
248 ewmh_update_desktop_names();
249 ewmh_update_desktop_viewport();
250 ewmh_update_current_desktop();
252 put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
253 put_status(SBSC_MASK_REPORT);
258 desktop_t *make_desktop(const char *name, uint32_t id)
260 desktop_t *d = calloc(1, sizeof(desktop_t));
261 snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
262 if (id == XCB_NONE) {
263 d->id = xcb_generate_id(dpy);
265 d->prev = d->next = NULL;
266 d->root = d->focus = NULL;
267 d->user_layout = LAYOUT_TILED;
268 d->layout = single_monocle ? LAYOUT_MONOCLE : LAYOUT_TILED;
269 d->padding = (padding_t) PADDING;
270 d->window_gap = window_gap;
271 d->border_width = border_width;
275 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
278 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
280 snprintf(d->name, sizeof(d->name), "%s", name);
282 put_status(SBSC_MASK_REPORT);
283 ewmh_update_desktop_names();
286 void insert_desktop(monitor_t *m, desktop_t *d)
288 if (m->desk == NULL) {
293 m->desk_tail->next = d;
294 d->prev = m->desk_tail;
299 void add_desktop(monitor_t *m, desktop_t *d)
301 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X 0x%08X %s\n", m->id, d->id, d->name);
303 d->border_width = m->border_width;
304 d->window_gap = m->window_gap;
305 insert_desktop(m, d);
306 ewmh_update_number_of_desktops();
307 ewmh_update_desktop_names();
308 ewmh_update_desktop_viewport();
309 ewmh_update_wm_desktops();
310 put_status(SBSC_MASK_REPORT);
313 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
319 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
328 void unlink_desktop(monitor_t *m, desktop_t *d)
330 desktop_t *prev = d->prev;
331 desktop_t *next = d->next;
341 if (m->desk_head == d) {
345 if (m->desk_tail == d) {
353 d->prev = d->next = NULL;
356 void remove_desktop(monitor_t *m, desktop_t *d)
358 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
360 remove_node(m, d, d->root);
361 unlink_desktop(m, d);
362 history_remove(d, NULL, false);
365 ewmh_update_current_desktop();
366 ewmh_update_number_of_desktops();
367 ewmh_update_desktop_names();
368 ewmh_update_desktop_viewport();
370 if (mon != NULL && m->desk == NULL) {
372 focus_node(m, NULL, NULL);
374 activate_desktop(m, NULL);
375 if (m->desk != NULL) {
376 activate_node(m, m->desk, m->desk->focus);
381 put_status(SBSC_MASK_REPORT);
384 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
386 if (ds == NULL || dd == NULL || ds == dd) {
389 /* TODO: Handle sticky nodes. */
390 transfer_node(ms, ds, ds->root, md, dd, dd->focus, false);
393 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
395 if (d1 == NULL || d2 == NULL || d1 == d2 ||
396 (m1->desk == d1 && m1->sticky_count > 0) ||
397 (m2->desk == d2 && m2->sticky_count > 0)) {
401 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);
403 bool d1_was_active = (m1->desk == d1);
404 bool d2_was_active = (m2->desk == d2);
405 bool d1_was_focused = (mon->desk == d1);
406 bool d2_was_focused = (mon->desk == d2);
409 if (m1->desk == d1) {
412 if (m1->desk_head == d1) {
415 if (m1->desk_tail == d1) {
418 if (m2->desk == d2) {
421 if (m2->desk_head == d2) {
424 if (m2->desk_tail == d2) {
428 if (m1->desk == d1) {
430 } else if (m1->desk == d2) {
433 if (m1->desk_head == d1) {
435 } else if (m1->desk_head == d2) {
438 if (m1->desk_tail == d1) {
440 } else if (m1->desk_tail == d2) {
445 desktop_t *p1 = d1->prev;
446 desktop_t *n1 = d1->next;
447 desktop_t *p2 = d2->prev;
448 desktop_t *n2 = d2->next;
450 if (p1 != NULL && p1 != d2) {
453 if (n1 != NULL && n1 != d2) {
456 if (p2 != NULL && p2 != d1) {
459 if (n2 != NULL && n2 != d1) {
463 d1->prev = p2 == d1 ? d2 : p2;
464 d1->next = n2 == d1 ? d2 : n2;
465 d2->prev = p1 == d2 ? d1 : p1;
466 d2->next = n1 == d2 ? d1 : n1;
469 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
470 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
471 history_remove(d1, NULL, false);
472 history_remove(d2, NULL, false);
477 if (d1_was_active && !d2_was_active) {
478 if ((!follow && m1 != m2) || !d1_was_focused) {
482 } else if (!d1_was_active && d2_was_active) {
484 if ((!follow && m1 != m2) || !d2_was_focused) {
489 if (follow || m1 == m2) {
490 if (d1_was_focused) {
491 focus_node(m2, d1, d1->focus);
492 } else if (d1_was_active) {
493 activate_node(m2, d1, d1->focus);
496 if (d2_was_focused) {
497 focus_node(m1, d2, d2->focus);
498 } else if (d2_was_active) {
499 activate_node(m1, d2, d2->focus);
502 if (d1_was_focused) {
503 focus_node(m1, d2, d2->focus);
504 } else if (d1_was_active) {
505 activate_node(m1, d2, d2->focus);
508 if (d2_was_focused) {
509 focus_node(m2, d1, d1->focus);
510 } else if (d2_was_active) {
511 activate_node(m2, d1, d1->focus);
515 ewmh_update_wm_desktops();
516 ewmh_update_desktop_names();
517 ewmh_update_desktop_viewport();
518 ewmh_update_current_desktop();
520 put_status(SBSC_MASK_REPORT);
525 void show_desktop(desktop_t *d)
530 show_node(d, d->root);
533 void hide_desktop(desktop_t *d)
538 hide_node(d, d->root);
541 bool is_urgent(desktop_t *d)
543 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
544 if (n->client == NULL) {
547 if (n->client->urgent) {