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);
176 unlink_desktop(ms, d);
178 if ((!follow || !d_was_active || !ms_was_focused) && md->desk != NULL) {
184 insert_desktop(md, d);
185 history_remove(d, NULL, false);
189 if (activate_desktop(ms, NULL)) {
190 activate_node(ms, ms->desk, NULL);
192 if (ms_was_focused) {
193 focus_node(md, d, d->focus);
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);
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);
208 ms->sticky_count -= sticky_count(d->root);
209 md->sticky_count += sticky_count(d->root);
211 transfer_sticky_nodes(md, d, md, md->desk, d->root);
216 adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
219 if ((!follow || !d_was_active || !ms_was_focused) && md->desk == d) {
221 focus_node(md, d, d->focus);
223 activate_node(md, d, d->focus);
227 ewmh_update_wm_desktops();
228 ewmh_update_desktop_names();
229 ewmh_update_desktop_viewport();
230 ewmh_update_current_desktop();
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);
238 desktop_t *make_desktop(const char *name, uint32_t id)
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);
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;
255 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
258 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
260 snprintf(d->name, sizeof(d->name), "%s", name);
262 put_status(SBSC_MASK_REPORT);
263 ewmh_update_desktop_names();
266 void insert_desktop(monitor_t *m, desktop_t *d)
268 if (m->desk == NULL) {
273 m->desk_tail->next = d;
274 d->prev = m->desk_tail;
279 void add_desktop(monitor_t *m, desktop_t *d)
281 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X 0x%08X %s\n", m->id, d->id, d->name);
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);
294 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
300 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
309 void unlink_desktop(monitor_t *m, desktop_t *d)
311 desktop_t *prev = d->prev;
312 desktop_t *next = d->next;
322 if (m->desk_head == d) {
326 if (m->desk_tail == d) {
334 d->prev = d->next = NULL;
337 void remove_desktop(monitor_t *m, desktop_t *d)
339 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
341 remove_node(m, d, d->root);
342 unlink_desktop(m, d);
343 history_remove(d, NULL, false);
346 ewmh_update_current_desktop();
347 ewmh_update_number_of_desktops();
348 ewmh_update_desktop_names();
349 ewmh_update_desktop_viewport();
351 if (mon != NULL && m->desk == NULL) {
353 focus_node(m, NULL, NULL);
355 activate_desktop(m, NULL);
356 if (m->desk != NULL) {
357 activate_node(m, m->desk, m->desk->focus);
362 put_status(SBSC_MASK_REPORT);
365 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
367 if (ds == NULL || dd == NULL || ds == dd) {
370 /* TODO: Handle sticky nodes. */
371 transfer_node(ms, ds, ds->root, md, dd, dd->focus, false);
374 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
376 if (d1 == NULL || d2 == NULL || d1 == d2 ||
377 (m1->desk == d1 && m1->sticky_count > 0) ||
378 (m2->desk == d2 && m2->sticky_count > 0)) {
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);
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);
390 if (m1->desk == d1) {
393 if (m1->desk_head == d1) {
396 if (m1->desk_tail == d1) {
399 if (m2->desk == d2) {
402 if (m2->desk_head == d2) {
405 if (m2->desk_tail == d2) {
409 if (m1->desk == d1) {
411 } else if (m1->desk == d2) {
414 if (m1->desk_head == d1) {
416 } else if (m1->desk_head == d2) {
419 if (m1->desk_tail == d1) {
421 } else if (m1->desk_tail == d2) {
426 desktop_t *p1 = d1->prev;
427 desktop_t *n1 = d1->next;
428 desktop_t *p2 = d2->prev;
429 desktop_t *n2 = d2->next;
431 if (p1 != NULL && p1 != d2) {
434 if (n1 != NULL && n1 != d2) {
437 if (p2 != NULL && p2 != d1) {
440 if (n2 != NULL && n2 != d1) {
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;
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);
458 if (d1_was_active && !d2_was_active) {
459 if ((!follow && m1 != m2) || !d1_was_focused) {
463 } else if (!d1_was_active && d2_was_active) {
465 if ((!follow && m1 != m2) || !d2_was_focused) {
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);
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);
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);
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);
496 ewmh_update_wm_desktops();
497 ewmh_update_desktop_names();
498 ewmh_update_desktop_viewport();
499 ewmh_update_current_desktop();
501 put_status(SBSC_MASK_REPORT);
506 void show_desktop(desktop_t *d)
511 show_node(d, d->root);
514 void hide_desktop(desktop_t *d)
519 hide_node(d, d->root);
522 bool is_urgent(desktop_t *d)
524 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
525 if (n->client == NULL) {
528 if (n->client->urgent) {