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