1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation and/or
11 * other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20 * 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.
35 void focus_desktop(monitor_t *m, desktop_t *d)
42 PRINTF("focus desktop %s\n", d->name);
45 hide_desktop(mon->desk);
49 ewmh_update_current_desktop();
53 desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
55 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
57 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
60 coordinates_t loc = {m, f, NULL};
61 if (desktop_matches(&loc, &loc, sel))
63 f = (dir == CYCLE_PREV ? f->prev : f->next);
65 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
71 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
79 void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
84 desktop_t *dd = ms->desk;
85 unlink_desktop(ms, d);
86 insert_desktop(md, d);
90 show_desktop(ms->desk);
95 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
96 translate_client(ms, md, n->client);
100 if (d != dd && md->desk == d)
103 history_transfer_desktop(md, d);
105 ewmh_update_wm_desktops();
106 ewmh_update_desktop_names();
107 ewmh_update_current_desktop();
111 desktop_t *make_desktop(const char *name)
113 desktop_t *d = malloc(sizeof(desktop_t));
115 snprintf(d->name, sizeof(d->name), "%s%d", DEFAULT_DESK_NAME, ++desktop_uid);
117 snprintf(d->name, sizeof(d->name), "%s", name);
118 d->layout = LAYOUT_TILED;
119 d->prev = d->next = NULL;
120 d->root = d->focus = NULL;
121 d->top_padding = d->right_padding = d->bottom_padding = d->left_padding = 0;
122 d->window_gap = WINDOW_GAP;
123 d->border_width = BORDER_WIDTH;
128 void insert_desktop(monitor_t *m, desktop_t *d)
130 if (m->desk == NULL) {
135 m->desk_tail->next = d;
136 d->prev = m->desk_tail;
141 void add_desktop(monitor_t *m, desktop_t *d)
143 PRINTF("add desktop %s\n", d->name);
145 insert_desktop(m, d);
147 ewmh_update_number_of_desktops();
148 ewmh_update_desktop_names();
152 void empty_desktop(desktop_t *d)
154 destroy_tree(d->root);
155 d->root = d->focus = NULL;
158 void unlink_desktop(monitor_t *m, desktop_t *d)
160 desktop_t *prev = d->prev;
161 desktop_t *next = d->next;
162 desktop_t *last_desk = history_get_desktop(m, d);
167 if (m->desk_head == d)
169 if (m->desk_tail == d)
172 m->desk = (last_desk == NULL ? (prev == NULL ? next : prev) : last_desk);
173 d->prev = d->next = NULL;
176 void remove_desktop(monitor_t *m, desktop_t *d)
178 PRINTF("remove desktop %s\n", d->name);
180 unlink_desktop(m, d);
181 history_remove(d, NULL);
185 ewmh_update_number_of_desktops();
186 ewmh_update_desktop_names();
190 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
192 if (ds == NULL || dd == NULL || ds == dd)
194 node_t *n = first_extrema(ds->root);
196 node_t *next = next_leaf(n, ds->root);
197 transfer_node(ms, ds, n, md, dd, dd->focus);
202 void swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
204 if (d1 == NULL || d2 == NULL || d1 == d2)
207 PRINTF("swap desktops %s %s\n", d1->name, d2->name);
209 bool d1_focused = (m1->desk == d1);
210 bool d2_focused = (m2->desk == d2);
215 if (m1->desk_head == d1)
217 if (m1->desk_tail == d1)
221 if (m2->desk_head == d2)
223 if (m2->desk_tail == d2)
226 if (m1->desk_head == d1)
228 else if (m1->desk_head == d2)
230 if (m1->desk_tail == d1)
232 else if (m1->desk_tail == d2)
236 desktop_t *p1 = d1->prev;
237 desktop_t *n1 = d1->next;
238 desktop_t *p2 = d2->prev;
239 desktop_t *n2 = d2->next;
241 if (p1 != NULL && p1 != d2)
243 if (n1 != NULL && n1 != d2)
245 if (p2 != NULL && p2 != d1)
247 if (n2 != NULL && n2 != d1)
250 d1->prev = p2 == d1 ? d2 : p2;
251 d1->next = n2 == d1 ? d2 : n2;
252 d2->prev = p1 == d2 ? d1 : p1;
253 d2->next = n1 == d2 ? d1 : n1;
256 for (node_t *n = first_extrema(d1->root); n != NULL; n = next_leaf(n, d1->root))
257 translate_client(m1, m2, n->client);
258 for (node_t *n = first_extrema(d2->root); n != NULL; n = next_leaf(n, d2->root))
259 translate_client(m2, m1, n->client);
260 history_swap_desktops(m1, d1, m2, d2);
263 if (d1_focused && !d2_focused) {
266 } else if (!d1_focused && d2_focused) {
272 update_input_focus();
273 ewmh_update_wm_desktops();
274 ewmh_update_desktop_names();
275 ewmh_update_current_desktop();
279 void show_desktop(desktop_t *d)
283 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
284 window_show(n->client->window);
287 void hide_desktop(desktop_t *d)
291 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
292 window_hide(n->client->window);
295 bool is_urgent(desktop_t *d)
297 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
298 if (n->client->urgent)