1 /* Copyright (c) 2012-2014, 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.
24 * The views and conclusions contained in the software and documentation are those
25 * of the authors and should not be interpreted as representing official policies,
26 * either expressed or implied, of the FreeBSD Project.
39 void focus_desktop(monitor_t *m, desktop_t *d)
46 PRINTF("focus desktop %s\n", d->name);
49 hide_desktop(mon->desk);
53 ewmh_update_current_desktop();
57 desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
59 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
61 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
64 coordinates_t loc = {m, f, NULL};
65 if (desktop_matches(&loc, &loc, sel))
67 f = (dir == CYCLE_PREV ? f->prev : f->next);
69 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
75 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
83 void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
88 desktop_t *dd = ms->desk;
89 unlink_desktop(ms, d);
90 insert_desktop(md, d);
94 show_desktop(ms->desk);
99 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
100 translate_client(ms, md, n->client);
104 if (d != dd && md->desk == d)
107 history_transfer_desktop(md, d);
109 ewmh_update_wm_desktops();
110 ewmh_update_desktop_names();
111 ewmh_update_current_desktop();
115 desktop_t *make_desktop(const char *name)
117 desktop_t *d = malloc(sizeof(desktop_t));
119 snprintf(d->name, sizeof(d->name), "%s%d", DEFAULT_DESK_NAME, ++desktop_uid);
121 snprintf(d->name, sizeof(d->name), "%s", name);
122 d->layout = LAYOUT_TILED;
123 d->prev = d->next = NULL;
124 d->root = d->focus = NULL;
125 d->top_padding = d->right_padding = d->bottom_padding = d->left_padding = 0;
126 d->window_gap = WINDOW_GAP;
127 d->border_width = BORDER_WIDTH;
132 void insert_desktop(monitor_t *m, desktop_t *d)
134 if (m->desk == NULL) {
139 m->desk_tail->next = d;
140 d->prev = m->desk_tail;
145 void add_desktop(monitor_t *m, desktop_t *d)
147 PRINTF("add desktop %s\n", d->name);
149 insert_desktop(m, d);
151 ewmh_update_number_of_desktops();
152 ewmh_update_desktop_names();
156 void empty_desktop(desktop_t *d)
158 destroy_tree(d->root);
159 d->root = d->focus = NULL;
162 void unlink_desktop(monitor_t *m, desktop_t *d)
164 desktop_t *prev = d->prev;
165 desktop_t *next = d->next;
166 desktop_t *last_desk = history_get_desktop(m, d);
171 if (m->desk_head == d)
173 if (m->desk_tail == d)
176 m->desk = (last_desk == NULL ? (prev == NULL ? next : prev) : last_desk);
177 d->prev = d->next = NULL;
180 void remove_desktop(monitor_t *m, desktop_t *d)
182 PRINTF("remove desktop %s\n", d->name);
184 unlink_desktop(m, d);
185 history_remove(d, NULL);
189 ewmh_update_number_of_desktops();
190 ewmh_update_desktop_names();
194 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
196 if (ds == NULL || dd == NULL || ds == dd)
198 node_t *n = first_extrema(ds->root);
200 node_t *next = next_leaf(n, ds->root);
201 transfer_node(ms, ds, n, md, dd, dd->focus);
206 void swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
208 if (d1 == NULL || d2 == NULL || d1 == d2)
211 PRINTF("swap desktops %s %s\n", d1->name, d2->name);
213 bool d1_focused = (m1->desk == d1);
214 bool d2_focused = (m2->desk == d2);
219 if (m1->desk_head == d1)
221 if (m1->desk_tail == d1)
225 if (m2->desk_head == d2)
227 if (m2->desk_tail == d2)
230 if (m1->desk_head == d1)
232 else if (m1->desk_head == d2)
234 if (m1->desk_tail == d1)
236 else if (m1->desk_tail == d2)
240 desktop_t *p1 = d1->prev;
241 desktop_t *n1 = d1->next;
242 desktop_t *p2 = d2->prev;
243 desktop_t *n2 = d2->next;
245 if (p1 != NULL && p1 != d2)
247 if (n1 != NULL && n1 != d2)
249 if (p2 != NULL && p2 != d1)
251 if (n2 != NULL && n2 != d1)
254 d1->prev = p2 == d1 ? d2 : p2;
255 d1->next = n2 == d1 ? d2 : n2;
256 d2->prev = p1 == d2 ? d1 : p1;
257 d2->next = n1 == d2 ? d1 : n1;
260 for (node_t *n = first_extrema(d1->root); n != NULL; n = next_leaf(n, d1->root))
261 translate_client(m1, m2, n->client);
262 for (node_t *n = first_extrema(d2->root); n != NULL; n = next_leaf(n, d2->root))
263 translate_client(m2, m1, n->client);
264 history_swap_desktops(m1, d1, m2, d2);
267 if (d1_focused && !d2_focused) {
270 } else if (!d1_focused && d2_focused) {
276 update_input_focus();
277 ewmh_update_wm_desktops();
278 ewmh_update_desktop_names();
279 ewmh_update_current_desktop();
283 void show_desktop(desktop_t *d)
287 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
288 window_show(n->client->window);
291 void hide_desktop(desktop_t *d)
295 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
296 window_hide(n->client->window);
299 bool is_urgent(desktop_t *d)
301 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
302 if (n->client->urgent)