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.
34 #include "subscribe.h"
37 void focus_desktop(monitor_t *m, desktop_t *d)
44 PRINTF("focus desktop %s\n", d->name);
45 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus %s %s\n", m->name, d->name);
48 hide_desktop(mon->desk);
52 ewmh_update_current_desktop();
53 put_status(SBSC_MASK_REPORT);
56 desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
58 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
60 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
63 coordinates_t loc = {m, f, NULL};
64 if (desktop_matches(&loc, &loc, sel))
66 f = (dir == CYCLE_PREV ? f->prev : f->next);
68 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
74 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
76 put_status(SBSC_MASK_DESKTOP_LAYOUT, "desktop_layout %s %s %s\n", m->name, d->name, l==LAYOUT_TILED?"tiled":"monocle");
80 put_status(SBSC_MASK_REPORT);
83 void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
88 put_status(SBSC_MASK_DESKTOP_TRANSFER, "desktop_transfer %s %s %s\n", ms->name, d->name, md->name);
90 desktop_t *dd = ms->desk;
91 unlink_desktop(ms, d);
92 insert_desktop(md, d);
96 show_desktop(ms->desk);
101 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
102 translate_client(ms, md, n->client);
106 if (d != dd && md->desk == d)
109 history_transfer_desktop(md, d);
111 ewmh_update_wm_desktops();
112 ewmh_update_desktop_names();
113 ewmh_update_current_desktop();
114 put_status(SBSC_MASK_REPORT);
117 desktop_t *make_desktop(const char *name)
119 desktop_t *d = malloc(sizeof(desktop_t));
121 snprintf(d->name, sizeof(d->name), "%s%d", DEFAULT_DESK_NAME, ++desktop_uid);
123 snprintf(d->name, sizeof(d->name), "%s", name);
124 d->prev = d->next = NULL;
125 d->root = d->focus = NULL;
126 initialize_desktop(d);
130 void initialize_desktop(desktop_t *d)
132 d->layout = LAYOUT_TILED;
133 d->top_padding = d->right_padding = d->bottom_padding = d->left_padding = 0;
134 d->window_gap = window_gap;
135 d->border_width = border_width;
139 void insert_desktop(monitor_t *m, desktop_t *d)
141 if (m->desk == NULL) {
146 m->desk_tail->next = d;
147 d->prev = m->desk_tail;
152 void add_desktop(monitor_t *m, desktop_t *d)
154 PRINTF("add desktop %s\n", d->name);
155 put_status(SBSC_MASK_DESKTOP_ADD, "desktop_add %s %s\n", m->name, d->name);
157 insert_desktop(m, d);
159 ewmh_update_number_of_desktops();
160 ewmh_update_desktop_names();
161 put_status(SBSC_MASK_REPORT);
164 void empty_desktop(desktop_t *d)
166 destroy_tree(d->root);
167 d->root = d->focus = NULL;
170 void unlink_desktop(monitor_t *m, desktop_t *d)
172 desktop_t *prev = d->prev;
173 desktop_t *next = d->next;
174 desktop_t *last_desk = history_get_desktop(m, d);
179 if (m->desk_head == d)
181 if (m->desk_tail == d)
184 m->desk = (last_desk == NULL ? (prev == NULL ? next : prev) : last_desk);
185 d->prev = d->next = NULL;
188 void remove_desktop(monitor_t *m, desktop_t *d)
190 PRINTF("remove desktop %s\n", d->name);
191 put_status(SBSC_MASK_DESKTOP_REMOVE, "desktop_remove %s\n", d->name);
193 unlink_desktop(m, d);
194 history_remove(d, NULL);
200 ewmh_update_current_desktop();
201 ewmh_update_number_of_desktops();
202 ewmh_update_desktop_names();
204 put_status(SBSC_MASK_REPORT);
207 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
209 if (ds == NULL || dd == NULL || ds == dd)
211 node_t *n = first_extrema(ds->root);
213 node_t *next = next_leaf(n, ds->root);
214 transfer_node(ms, ds, n, md, dd, dd->focus);
219 void swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
221 if (d1 == NULL || d2 == NULL || d1 == d2)
224 PRINTF("swap desktops %s %s\n", d1->name, d2->name);
225 put_status(SBSC_MASK_DESKTOP_SWAP, "desktop_swap %s %s %s %s\n", m1->name, d1->name, m2->name, d2->name);
227 bool d1_focused = (m1->desk == d1);
228 bool d2_focused = (m2->desk == d2);
233 if (m1->desk_head == d1)
235 if (m1->desk_tail == d1)
239 if (m2->desk_head == d2)
241 if (m2->desk_tail == d2)
244 if (m1->desk_head == d1)
246 else if (m1->desk_head == d2)
248 if (m1->desk_tail == d1)
250 else if (m1->desk_tail == d2)
254 desktop_t *p1 = d1->prev;
255 desktop_t *n1 = d1->next;
256 desktop_t *p2 = d2->prev;
257 desktop_t *n2 = d2->next;
259 if (p1 != NULL && p1 != d2)
261 if (n1 != NULL && n1 != d2)
263 if (p2 != NULL && p2 != d1)
265 if (n2 != NULL && n2 != d1)
268 d1->prev = p2 == d1 ? d2 : p2;
269 d1->next = n2 == d1 ? d2 : n2;
270 d2->prev = p1 == d2 ? d1 : p1;
271 d2->next = n1 == d2 ? d1 : n1;
274 for (node_t *n = first_extrema(d1->root); n != NULL; n = next_leaf(n, d1->root))
275 translate_client(m1, m2, n->client);
276 for (node_t *n = first_extrema(d2->root); n != NULL; n = next_leaf(n, d2->root))
277 translate_client(m2, m1, n->client);
278 history_swap_desktops(m1, d1, m2, d2);
281 if (d1_focused && !d2_focused) {
284 } else if (!d1_focused && d2_focused) {
290 update_input_focus();
291 ewmh_update_wm_desktops();
292 ewmh_update_desktop_names();
293 ewmh_update_current_desktop();
294 put_status(SBSC_MASK_REPORT);
297 void show_desktop(desktop_t *d)
301 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
302 window_show(n->client->window);
305 void hide_desktop(desktop_t *d)
309 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
310 window_hide(n->client->window);
313 bool is_urgent(desktop_t *d)
315 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
316 if (n->client->urgent)