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)
45 hide_desktop(m->desk);
49 ewmh_update_current_desktop();
51 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
54 void activate_desktop(monitor_t *m, desktop_t *d)
61 hide_desktop(m->desk);
65 put_status(SBSC_MASK_DESKTOP_ACTIVATE, "desktop_activate 0x%08X 0x%08X\n", m->id, d->id);
66 put_status(SBSC_MASK_REPORT);
69 desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
71 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
73 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
77 coordinates_t loc = {m, f, NULL};
78 if (desktop_matches(&loc, &loc, sel)) {
81 f = (dir == CYCLE_PREV ? f->prev : f->next);
83 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
90 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
95 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout 0x%08X 0x%08X %s\n", m->id, d->id, LAYOUT_STR(l));
98 put_status(SBSC_MASK_REPORT);
102 bool transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
104 if (ms == NULL || md == NULL || d == NULL || ms == md) {
108 bool was_active = (d == ms->desk);
110 unlink_desktop(ms, d);
112 if (ms->sticky_count > 0 && was_active && ms->desk != NULL) {
113 sticky_still = false;
114 transfer_sticky_nodes(ms, d, ms->desk, d->root);
118 if (md->desk != NULL) {
122 insert_desktop(md, d);
124 history_transfer_desktop(md, d);
125 adapt_geometry(&ms->rectangle, &md->rectangle, d->root);
128 if (was_active && ms->desk != NULL) {
130 focus_node(ms, ms->desk, ms->desk->focus);
132 activate_node(ms, ms->desk, ms->desk->focus);
138 focus_node(md, d, d->focus);
140 activate_node(md, d, d->focus);
144 ewmh_update_wm_desktops();
145 ewmh_update_desktop_names();
146 ewmh_update_current_desktop();
148 put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer 0x%08X 0x%08X 0x%08X\n", ms->id, d->id, md->id);
149 put_status(SBSC_MASK_REPORT);
154 desktop_t *make_desktop(const char *name, uint32_t id)
156 desktop_t *d = malloc(sizeof(desktop_t));
157 snprintf(d->name, sizeof(d->name), "%s", name == NULL ? DEFAULT_DESK_NAME : name);
158 if (id == XCB_NONE) {
159 d->id = xcb_generate_id(dpy);
161 d->prev = d->next = NULL;
162 d->root = d->focus = NULL;
163 d->layout = LAYOUT_TILED;
164 d->padding = (padding_t) PADDING;
165 d->window_gap = window_gap;
166 d->border_width = border_width;
170 void rename_desktop(monitor_t *m, desktop_t *d, const char *name)
173 put_status(SBSC_MASK_DESKTOP_RENAME, "desktop_rename 0x%08X 0x%08X %s %s\n", m->id, d->id, d->name, name);
175 snprintf(d->name, sizeof(d->name), "%s", name);
177 put_status(SBSC_MASK_REPORT);
178 ewmh_update_desktop_names();
181 void insert_desktop(monitor_t *m, desktop_t *d)
183 if (m->desk == NULL) {
188 m->desk_tail->next = d;
189 d->prev = m->desk_tail;
194 void add_desktop(monitor_t *m, desktop_t *d)
196 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add 0x%08X %s 0x%08X\n", d->id, d->name, m->id);
198 d->border_width = m->border_width;
199 d->window_gap = m->window_gap;
200 insert_desktop(m, d);
201 ewmh_update_number_of_desktops();
202 ewmh_update_desktop_names();
203 ewmh_update_wm_desktops();
204 put_status(SBSC_MASK_REPORT);
207 desktop_t *find_desktop_in(const char *name, monitor_t *m)
213 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
214 if (streq(d->name, name)) {
222 void empty_desktop(monitor_t *m, desktop_t *d)
224 destroy_tree(m, d, d->root);
225 d->root = d->focus = NULL;
228 void unlink_desktop(monitor_t *m, desktop_t *d)
230 desktop_t *prev = d->prev;
231 desktop_t *next = d->next;
241 if (m->desk_head == d) {
245 if (m->desk_tail == d) {
250 m->desk = history_last_desktop(m, d);
251 if (m->desk == NULL) {
252 m->desk = (prev == NULL ? next : prev);
256 d->prev = d->next = NULL;
259 void remove_desktop(monitor_t *m, desktop_t *d)
261 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove 0x%08X 0x%08X\n", m->id, d->id);
263 bool was_focused = (mon != NULL && d == mon->desk);
264 bool was_active = (d == m->desk);
265 history_remove(d, NULL, false);
266 unlink_desktop(m, d);
270 ewmh_update_current_desktop();
271 ewmh_update_number_of_desktops();
272 ewmh_update_desktop_names();
274 if (mon != NULL && m->desk != NULL) {
277 } else if (was_active) {
278 activate_node(m, m->desk, m->desk->focus);
282 put_status(SBSC_MASK_REPORT);
285 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
287 if (ds == NULL || dd == NULL || ds == dd) {
290 transfer_node(ms, ds, ds->root, md, dd, dd->focus);
293 bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
295 if (d1 == NULL || d2 == NULL || d1 == d2 ||
296 (m1->desk == d1 && m1->sticky_count > 0) ||
297 (m2->desk == d2 && m2->sticky_count > 0)) {
301 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);
303 bool d1_focused = (m1->desk == d1);
304 bool d2_focused = (m2->desk == d2);
307 if (m1->desk == d1) {
310 if (m1->desk_head == d1) {
313 if (m1->desk_tail == d1) {
316 if (m2->desk == d2) {
319 if (m2->desk_head == d2) {
322 if (m2->desk_tail == d2) {
326 if (m1->desk == d1) {
328 } else if (m1->desk == d2) {
331 if (m1->desk_head == d1) {
333 } else if (m1->desk_head == d2) {
336 if (m1->desk_tail == d1) {
338 } else if (m1->desk_tail == d2) {
343 desktop_t *p1 = d1->prev;
344 desktop_t *n1 = d1->next;
345 desktop_t *p2 = d2->prev;
346 desktop_t *n2 = d2->next;
348 if (p1 != NULL && p1 != d2) {
351 if (n1 != NULL && n1 != d2) {
354 if (p2 != NULL && p2 != d1) {
357 if (n2 != NULL && n2 != d1) {
361 d1->prev = p2 == d1 ? d2 : p2;
362 d1->next = n2 == d1 ? d2 : n2;
363 d2->prev = p1 == d2 ? d1 : p1;
364 d2->next = n1 == d2 ? d1 : n1;
367 adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
368 adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
369 history_swap_desktops(m1, d1, m2, d2);
374 if (d1_focused && !d2_focused) {
377 } else if (!d1_focused && d2_focused) {
382 if (d1 == mon->desk) {
383 focus_node(m2, d1, d1->focus);
384 } else if (d1 == m2->desk) {
385 activate_node(m2, d1, d1->focus);
388 if (d2 == mon->desk) {
389 focus_node(m1, d2, d2->focus);
390 } else if (d2 == m1->desk) {
391 activate_node(m1, d2, d2->focus);
394 ewmh_update_wm_desktops();
395 ewmh_update_desktop_names();
396 ewmh_update_current_desktop();
398 put_status(SBSC_MASK_REPORT);
403 void show_desktop(desktop_t *d)
411 void hide_desktop(desktop_t *d)
419 bool is_urgent(desktop_t *d)
421 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
422 if (n->client == NULL) {
425 if (n->client->urgent) {