]> git.lizzy.rs Git - bspwm.git/blob - desktop.c
Fix source_these: remove commas
[bspwm.git] / desktop.c
1 /* Copyright (c) 2012-2014, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  *
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.
27  */
28
29 #include <stdlib.h>
30 #include "bspwm.h"
31 #include "ewmh.h"
32 #include "history.h"
33 #include "monitor.h"
34 #include "query.h"
35 #include "tree.h"
36 #include "window.h"
37 #include "desktop.h"
38
39 void focus_desktop(monitor_t *m, desktop_t *d)
40 {
41         focus_monitor(m);
42
43         if (d == mon->desk)
44                 return;
45
46         PRINTF("focus desktop %s\n", d->name);
47
48         show_desktop(d);
49         hide_desktop(mon->desk);
50
51         mon->desk = d;
52
53         ewmh_update_current_desktop();
54         put_status();
55 }
56
57 desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
58 {
59         desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
60         if (f == NULL)
61                 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
62
63         while (f != d) {
64                 coordinates_t loc = {m, f, NULL};
65                 if (desktop_matches(&loc, &loc, sel))
66                         return f;
67                 f = (dir == CYCLE_PREV ? f->prev : f->next);
68                 if (f == NULL)
69                         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
70         }
71
72         return NULL;
73 }
74
75 void change_layout(monitor_t *m, desktop_t *d, layout_t l)
76 {
77         d->layout = l;
78         arrange(m, d);
79         if (d == mon->desk)
80                 put_status();
81 }
82
83 void transfer_desktop(monitor_t *ms, monitor_t *md, desktop_t *d)
84 {
85         if (ms == md)
86                 return;
87
88         desktop_t *dd = ms->desk;
89         unlink_desktop(ms, d);
90         insert_desktop(md, d);
91
92         if (d == dd) {
93                 if (ms->desk != NULL)
94                         show_desktop(ms->desk);
95                 if (md->desk != d)
96                         hide_desktop(d);
97         }
98
99         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
100                 translate_client(ms, md, n->client);
101
102         arrange(md, d);
103
104         if (d != dd && md->desk == d)
105                 show_desktop(d);
106
107         history_transfer_desktop(md, d);
108
109         ewmh_update_wm_desktops();
110         ewmh_update_desktop_names();
111         ewmh_update_current_desktop();
112         put_status();
113 }
114
115 desktop_t *make_desktop(const char *name)
116 {
117         desktop_t *d = malloc(sizeof(desktop_t));
118         if (name == NULL)
119                 snprintf(d->name, sizeof(d->name), "%s%d", DEFAULT_DESK_NAME, ++desktop_uid);
120         else
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;
128         d->floating = false;
129         return d;
130 }
131
132 void insert_desktop(monitor_t *m, desktop_t *d)
133 {
134         if (m->desk == NULL) {
135                 m->desk = d;
136                 m->desk_head = d;
137                 m->desk_tail = d;
138         } else {
139                 m->desk_tail->next = d;
140                 d->prev = m->desk_tail;
141                 m->desk_tail = d;
142         }
143 }
144
145 void add_desktop(monitor_t *m, desktop_t *d)
146 {
147         PRINTF("add desktop %s\n", d->name);
148
149         insert_desktop(m, d);
150         num_desktops++;
151         ewmh_update_number_of_desktops();
152         ewmh_update_desktop_names();
153         put_status();
154 }
155
156 void empty_desktop(desktop_t *d)
157 {
158         destroy_tree(d->root);
159         d->root = d->focus = NULL;
160 }
161
162 void unlink_desktop(monitor_t *m, desktop_t *d)
163 {
164         desktop_t *prev = d->prev;
165         desktop_t *next = d->next;
166         desktop_t *last_desk = history_get_desktop(m, d);
167         if (prev != NULL)
168                 prev->next = next;
169         if (next != NULL)
170                 next->prev = prev;
171         if (m->desk_head == d)
172                 m->desk_head = next;
173         if (m->desk_tail == d)
174                 m->desk_tail = prev;
175         if (m->desk == d)
176                 m->desk = (last_desk == NULL ? (prev == NULL ? next : prev) : last_desk);
177         d->prev = d->next = NULL;
178 }
179
180 void remove_desktop(monitor_t *m, desktop_t *d)
181 {
182         PRINTF("remove desktop %s\n", d->name);
183
184         unlink_desktop(m, d);
185         history_remove(d, NULL);
186         empty_desktop(d);
187         free(d);
188         num_desktops--;
189         ewmh_update_number_of_desktops();
190         ewmh_update_desktop_names();
191         put_status();
192 }
193
194 void merge_desktops(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd)
195 {
196         if (ds == NULL || dd == NULL || ds == dd)
197                 return;
198         node_t *n = first_extrema(ds->root);
199         while (n != NULL) {
200                 node_t *next = next_leaf(n, ds->root);
201                 transfer_node(ms, ds, n, md, dd, dd->focus);
202                 n = next;
203         }
204 }
205
206 void swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2)
207 {
208         if (d1 == NULL || d2 == NULL || d1 == d2)
209                 return;
210
211         PRINTF("swap desktops %s %s\n", d1->name, d2->name);
212
213         bool d1_focused = (m1->desk == d1);
214         bool d2_focused = (m2->desk == d2);
215
216         if (m1 != m2) {
217                 if (m1->desk == d1)
218                         m1->desk = d2;
219                 if (m1->desk_head == d1)
220                         m1->desk_head = d2;
221                 if (m1->desk_tail == d1)
222                         m1->desk_tail = d2;
223                 if (m2->desk == d2)
224                         m2->desk = d1;
225                 if (m2->desk_head == d2)
226                         m2->desk_head = d1;
227                 if (m2->desk_tail == d2)
228                         m2->desk_tail = d1;
229         } else {
230                 if (m1->desk_head == d1)
231                         m1->desk_head = d2;
232                 else if (m1->desk_head == d2)
233                         m1->desk_head = d1;
234                 if (m1->desk_tail == d1)
235                         m1->desk_tail = d2;
236                 else if (m1->desk_tail == d2)
237                         m1->desk_tail = d1;
238         }
239
240         desktop_t *p1 = d1->prev;
241         desktop_t *n1 = d1->next;
242         desktop_t *p2 = d2->prev;
243         desktop_t *n2 = d2->next;
244
245         if (p1 != NULL && p1 != d2)
246                 p1->next = d2;
247         if (n1 != NULL && n1 != d2)
248                 n1->prev = d2;
249         if (p2 != NULL && p2 != d1)
250                 p2->next = d1;
251         if (n2 != NULL && n2 != d1)
252                 n2->prev = d1;
253
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;
258
259         if (m1 != m2) {
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);
265                 arrange(m1, d2);
266                 arrange(m2, d1);
267                 if (d1_focused && !d2_focused) {
268                         hide_desktop(d1);
269                         show_desktop(d2);
270                 } else if (!d1_focused && d2_focused) {
271                         show_desktop(d1);
272                         hide_desktop(d2);
273                 }
274         }
275
276         update_input_focus();
277         ewmh_update_wm_desktops();
278         ewmh_update_desktop_names();
279         ewmh_update_current_desktop();
280         put_status();
281 }
282
283 void show_desktop(desktop_t *d)
284 {
285         if (!visible)
286                 return;
287         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
288                 window_show(n->client->window);
289 }
290
291 void hide_desktop(desktop_t *d)
292 {
293         if (!visible)
294                 return;
295         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
296                 window_hide(n->client->window);
297 }
298
299 bool is_urgent(desktop_t *d)
300 {
301         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
302                 if (n->client->urgent)
303                         return true;
304         return false;
305 }