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) {
64 d = history_last_desktop(m, NULL);
74 if (m->sticky_count > 0) {
75 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
79 hide_desktop(m->desk);
83 put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
84 put_status(SBSC_MASK_REPORT);
89 bool find_closest_desktop(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, desktop_select_t *sel)
91 monitor_t *m = ref->monitor;
92 desktop_t *d = ref->desktop;
93 d = (dir == CYCLE_PREV ? d->prev : d->next);
95 #define HANDLE_BOUNDARIES(m, d) \
97 m = (dir == CYCLE_PREV ? m->prev : m->next); \
99 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
101 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
103 HANDLE_BOUNDARIES(m, d)
105 while (d != ref->desktop) {
106 coordinates_t loc = {m, d, NULL};
107 if (desktop_matches(&loc, ref, sel)) {
111 d = (dir == CYCLE_PREV ? d->prev : d->next);
112 HANDLE_BOUNDARIES(m, d)
114 #undef HANDLE_BOUNDARIES
119 bool set_layout(monitor_t *m, desktop_t *d, layout_t l)
121 if (d->layout == l) {
127 handle_presel_feedbacks(m, d);
131 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(l));
134 put_status(SBSC_MASK_REPORT);
140 void handle_presel_feedbacks(monitor_t *m, desktop_t *d)
145 if (d->layout == LAYOUT_MONOCLE) {
146 hide_presel_feedbacks(m, d, d->root);
148 show_presel_feedbacks(m, d, d->root);
152 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d, bool follow)
154 if (ms == NULL || md == NULL || d == NULL || ms == md) {
158 bool d_was_active = (d == ms->desk);
159 bool ms_was_focused = (ms == mon);
161 unlink_desktop(ms, d);
163 if ((!follow || !d_was_active || !ms_was_focused) && md->desk != NULL) {
167 insert_desktop(md, d);
168 history_remove(d, NULL, false);
172 if (activate_desktop(ms, NULL)) {
173 activate_node(ms, ms->desk, NULL);
175 if (ms_was_focused) {
176 focus_node(md, d, d->focus);
179 if (ms_was_focused) {
180 focus_node(ms, ms->desk, NULL);
181 } else if (activate_desktop(ms, NULL)) {
182 activate_node(ms, ms->desk, NULL);
187 if (ms->sticky_count > 0 && d_was_active) {
188 transfer_sticky_nodes(ms, d, ms->desk, d->root);
191 adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
194 if ((!follow || !d_was_active || !ms_was_focused) && md->desk == d) {
196 focus_node(md, d, d->focus);
198 activate_node(md, d, d->focus);
202 ewmh_update_wm_desktops();
203 ewmh_update_desktop_names();
204 ewmh_update_desktop_viewport();
205 ewmh_update_current_desktop();
207 put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
208 put_status(SBSC_MASK_REPORT);
213 desktop_t *make_desktop(const char *name, uint32_t id)
215 desktop_t *d = calloc(1, sizeof(desktop_t));
216 snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
217 if (id == XCB_NONE) {
218 d->id = xcb_generate_id(dpy);
220 d->prev = d->next = NULL;
221 d->root = d->focus = NULL;
222 d->layout = LAYOUT_TILED;
223 d->padding = (padding_t) PADDING;
224 d->window_gap = window_gap;
225 d->border_width = border_width;
229 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
232 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
234 snprintf(d->name, sizeof(d->name), "%s", name);
236 put_status(SBSC_MASK_REPORT);
237 ewmh_update_desktop_names();
240 void insert_desktop(monitor_t *m, desktop_t *d)
242 if (m->desk == NULL) {
247 m->desk_tail->next = d;
248 d->prev = m->desk_tail;
253 void add_desktop(monitor_t *m, desktop_t *d)
255 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X 0x%08X %s\n", m->id, d->id, d->name);
257 d->border_width = m->border_width;
258 d->window_gap = m->window_gap;
259 insert_desktop(m, d);
260 ewmh_update_number_of_desktops();
261 ewmh_update_desktop_names();
262 ewmh_update_desktop_viewport();
263 ewmh_update_wm_desktops();
264 put_status(SBSC_MASK_REPORT);
267 desktop_t *find_desktop_in(uint32_t id, monitor_t *m)
273 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
282 void unlink_desktop(monitor_t *m, desktop_t *d)
284 desktop_t *prev = d->prev;
285 desktop_t *next = d->next;
295 if (m->desk_head == d) {
299 if (m->desk_tail == d) {
307 d->prev = d->next = NULL;
310 void remove_desktop(monitor_t *m, desktop_t *d)
312 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
314 remove_node(m, d, d->root);
315 unlink_desktop(m, d);
316 history_remove(d, NULL, false);
319 ewmh_update_current_desktop();
320 ewmh_update_number_of_desktops();
321 ewmh_update_desktop_names();
322 ewmh_update_desktop_viewport();
324 if (mon != NULL && m->desk == NULL) {
326 focus_node(m, NULL, NULL);
328 activate_desktop(m, NULL);
329 if (m->desk != NULL) {
330 activate_node(m, m->desk, m->desk->focus);
335 put_status(SBSC_MASK_REPORT);
338 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
340 if (ds == NULL || dd == NULL || ds == dd) {
343 transfer_node(ms, ds, ds->root, md, dd, dd->focus, false);
346 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
348 if (d1 == NULL || d2 == NULL || d1 == d2 ||
349 (m1->desk == d1 && m1->sticky_count > 0) ||
350 (m2->desk == d2 && m2->sticky_count > 0)) {
354 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);
356 bool d1_was_active = (m1->desk == d1);
357 bool d2_was_active = (m2->desk == d2);
358 bool d1_was_focused = (mon->desk == d1);
359 bool d2_was_focused = (mon->desk == d2);
362 if (m1->desk == d1) {
365 if (m1->desk_head == d1) {
368 if (m1->desk_tail == d1) {
371 if (m2->desk == d2) {
374 if (m2->desk_head == d2) {
377 if (m2->desk_tail == d2) {
381 if (m1->desk == d1) {
383 } else if (m1->desk == d2) {
386 if (m1->desk_head == d1) {
388 } else if (m1->desk_head == d2) {
391 if (m1->desk_tail == d1) {
393 } else if (m1->desk_tail == d2) {
398 desktop_t *p1 = d1->prev;
399 desktop_t *n1 = d1->next;
400 desktop_t *p2 = d2->prev;
401 desktop_t *n2 = d2->next;
403 if (p1 != NULL && p1 != d2) {
406 if (n1 != NULL && n1 != d2) {
409 if (p2 != NULL && p2 != d1) {
412 if (n2 != NULL && n2 != d1) {
416 d1->prev = p2 == d1 ? d2 : p2;
417 d1->next = n2 == d1 ? d2 : n2;
418 d2->prev = p1 == d2 ? d1 : p1;
419 d2->next = n1 == d2 ? d1 : n1;
422 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
423 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
424 history_remove(d1, NULL, false);
425 history_remove(d2, NULL, false);
430 if (d1_was_active && !d2_was_active) {
431 if ((!follow && m1 != m2) || !d1_was_focused) {
435 } else if (!d1_was_active && d2_was_active) {
437 if ((!follow && m1 != m2) || !d2_was_focused) {
442 if (follow || m1 == m2) {
443 if (d1_was_focused) {
444 focus_node(m2, d1, d1->focus);
445 } else if (d1_was_active) {
446 activate_node(m2, d1, d1->focus);
449 if (d2_was_focused) {
450 focus_node(m1, d2, d2->focus);
451 } else if (d2_was_active) {
452 activate_node(m1, d2, d2->focus);
455 if (d1_was_focused) {
456 focus_node(m1, d2, d2->focus);
457 } else if (d1_was_active) {
458 activate_node(m1, d2, d2->focus);
461 if (d2_was_focused) {
462 focus_node(m2, d1, d1->focus);
463 } else if (d2_was_active) {
464 activate_node(m2, d1, d1->focus);
468 ewmh_update_wm_desktops();
469 ewmh_update_desktop_names();
470 ewmh_update_desktop_viewport();
471 ewmh_update_current_desktop();
473 put_status(SBSC_MASK_REPORT);
478 void show_desktop(desktop_t *d)
483 show_node(d, d->root);
486 void hide_desktop(desktop_t *d)
491 hide_node(d, d->root);
494 bool is_urgent(desktop_t *d)
496 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
497 if (n->client == NULL) {
500 if (n->client->urgent) {