]> git.lizzy.rs Git - bspwm.git/blob - monitor.c
Simplify fit_monitor
[bspwm.git] / monitor.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "bspwm.h"
29 #include "desktop.h"
30 #include "ewmh.h"
31 #include "history.h"
32 #include "query.h"
33 #include "settings.h"
34 #include "tree.h"
35 #include "window.h"
36 #include "monitor.h"
37
38 monitor_t *make_monitor(xcb_rectangle_t rect)
39 {
40     monitor_t *m = malloc(sizeof(monitor_t));
41     snprintf(m->name, sizeof(m->name), "%s%02d", DEFAULT_MON_NAME, ++monitor_uid);
42     m->prev = m->next = NULL;
43     m->desk = m->desk_head = m->desk_tail = NULL;
44     m->rectangle = rect;
45     m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 0;
46     m->wired = true;
47     m->num_sticky = 0;
48     uint32_t mask = XCB_CW_EVENT_MASK;
49     uint32_t values[] = {XCB_EVENT_MASK_ENTER_WINDOW};
50     m->root = xcb_generate_id(dpy);
51     xcb_create_window(dpy, XCB_COPY_FROM_PARENT, m->root, root, rect.x, rect.y, rect.width, rect.height, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, mask, values);
52     window_lower(m->root);
53     if (focus_follows_pointer)
54         window_show(m->root);
55     return m;
56 }
57
58 monitor_t *find_monitor(char *name)
59 {
60     for (monitor_t *m = mon_head; m != NULL; m = m->next)
61         if (streq(m->name, name))
62             return m;
63     return NULL;
64 }
65
66 monitor_t *get_monitor_by_id(xcb_randr_output_t id)
67 {
68     for (monitor_t *m = mon_head; m != NULL; m = m->next)
69         if (m->id == id)
70             return m;
71     return NULL;
72 }
73
74 void fit_monitor(monitor_t *m, client_t *c)
75 {
76     xcb_rectangle_t a = m->rectangle;
77     xcb_rectangle_t *b = &c->floating_rectangle;
78     if (b->x <= a.x || (b->x + b->width) >= (a.x + a.width)) {
79         if (b->width >= a.width)
80             b->x = 0;
81         else
82             b->x = a.x + (a.width - b->width) / 2;
83     }
84     if (b->y <= a.y || (b->y + b->height) >= (a.y + a.height)) {
85         if (b->height >= a.height)
86             b->y = 0;
87         else
88             b->y = a.y + (a.height - b->height) / 2;
89     }
90 }
91
92 void update_root(monitor_t *m)
93 {
94     xcb_rectangle_t rect = m->rectangle;
95     window_move_resize(m->root, rect.x, rect.y, rect.width, rect.height);
96 }
97
98 void focus_monitor(monitor_t *m)
99 {
100     if (mon == m)
101         return;
102
103     PRINTF("focus monitor %s\n", m->name);
104
105     mon = m;
106
107     if (pointer_follows_monitor)
108         center_pointer(m);
109
110     ewmh_update_current_desktop();
111     put_status();
112 }
113
114 monitor_t *add_monitor(xcb_rectangle_t rect)
115 {
116     monitor_t *m = make_monitor(rect);
117     if (mon == NULL) {
118         mon = m;
119         mon_head = m;
120         mon_tail = m;
121     } else {
122         mon_tail->next = m;
123         m->prev = mon_tail;
124         mon_tail = m;
125     }
126     num_monitors++;
127     return m;
128 }
129
130 void remove_monitor(monitor_t *m)
131 {
132     PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
133
134     while (m->desk_head != NULL)
135         remove_desktop(m, m->desk_head);
136     monitor_t *prev = m->prev;
137     monitor_t *next = m->next;
138     monitor_t *last_mon = history_get_monitor(m);
139     if (prev != NULL)
140         prev->next = next;
141     if (next != NULL)
142         next->prev = prev;
143     if (mon_head == m)
144         mon_head = next;
145     if (mon_tail == m)
146         mon_tail = prev;
147     if (pri_mon == m)
148         pri_mon = NULL;
149     if (mon == m) {
150         mon = (last_mon == NULL ? (prev == NULL ? next : prev) : last_mon);
151         if (mon != NULL && mon->desk != NULL)
152             update_current();
153     }
154     xcb_destroy_window(dpy, m->root);
155     free(m);
156     num_monitors--;
157     put_status();
158 }
159
160 void merge_monitors(monitor_t *ms, monitor_t *md)
161 {
162     PRINTF("merge %s into %s\n", ms->name, md->name);
163
164     desktop_t *d = ms->desk_head;
165     while (d != NULL) {
166         desktop_t *next = d->next;
167         if (d->root != NULL || strstr(d->name, DEFAULT_DESK_NAME) == NULL)
168             transfer_desktop(ms, md, d);
169         d = next;
170     }
171 }
172
173 void swap_monitors(monitor_t *m1, monitor_t *m2)
174 {
175     if (m1 == NULL || m2 == NULL || m1 == m2)
176         return;
177
178     if (mon_head == m1)
179         mon_head = m2;
180     else if (mon_head == m2)
181         mon_head = m1;
182     if (mon_tail == m1)
183         mon_tail = m2;
184     else if (mon_tail == m2)
185         mon_tail = m1;
186
187     monitor_t *p1 = m1->prev;
188     monitor_t *n1 = m1->next;
189     monitor_t *p2 = m2->prev;
190     monitor_t *n2 = m2->next;
191
192     if (p1 != NULL && p1 != m2)
193         p1->next = m2;
194     if (n1 != NULL && n1 != m2)
195         n1->prev = m2;
196     if (p2 != NULL && p2 != m1)
197         p2->next = m1;
198     if (n2 != NULL && n2 != m1)
199         n2->prev = m1;
200
201     m1->prev = p2 == m1 ? m2 : p2;
202     m1->next = n2 == m1 ? m2 : n2;
203     m2->prev = p1 == m2 ? m1 : p1;
204     m2->next = n1 == m2 ? m1 : n1;
205
206     ewmh_update_wm_desktops();
207     ewmh_update_desktop_names();
208     ewmh_update_current_desktop();
209     put_status();
210 }
211
212 monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
213 {
214     monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
215     if (f == NULL)
216         f = (dir == CYCLE_PREV ? mon_tail : mon_head);
217
218     while (f != m) {
219         coordinates_t loc = {m, m->desk, NULL};
220         if (desktop_matches(&loc, &loc, sel))
221             return f;
222         f = (dir == CYCLE_PREV ? m->prev : m->next);
223         if (f == NULL)
224             f = (dir == CYCLE_PREV ? mon_tail : mon_head);
225     }
226
227     return NULL;
228 }
229
230 monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
231 {
232     int dmin = INT_MAX;
233     monitor_t *nearest = NULL;
234     xcb_rectangle_t rect = m->rectangle;
235     for (monitor_t *f = mon_head; f != NULL; f = f->next) {
236         if (f == m)
237             continue;
238         coordinates_t loc = {f, f->desk, NULL};
239         if (!desktop_matches(&loc, &loc, sel))
240             continue;
241         xcb_rectangle_t r = f->rectangle;
242         if ((dir == DIR_LEFT && r.x < rect.x) ||
243                 (dir == DIR_RIGHT && r.x >= (rect.x + rect.width)) ||
244                 (dir == DIR_UP && r.y < rect.y) ||
245                 (dir == DIR_DOWN && r.y >= (rect.y + rect.height))) {
246             int d = abs((r.x + r.width / 2) - (rect.x + rect.width / 2)) +
247                 abs((r.y + r.height / 2) - (rect.y + rect.height / 2));
248             if (d < dmin) {
249                 dmin = d;
250                 nearest = f;
251             }
252         }
253     }
254     return nearest;
255 }
256
257 bool import_monitors(void)
258 {
259     PUTS("import monitors");
260     xcb_randr_get_screen_resources_current_reply_t *sres = xcb_randr_get_screen_resources_current_reply(dpy, xcb_randr_get_screen_resources_current(dpy, root), NULL);
261     if (sres == NULL)
262         return false;
263
264     monitor_t *m, *mm = NULL;
265
266     int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
267     xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
268
269     xcb_randr_get_output_info_cookie_t cookies[len];
270     for (int i = 0; i < len; i++)
271         cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
272
273     for (m = mon_head; m != NULL; m = m->next)
274         m->wired = false;
275
276     for (int i = 0; i < len; i++) {
277         xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
278         if (info != NULL) {
279             if (info->crtc != XCB_NONE) {
280                 xcb_randr_get_crtc_info_reply_t *cir = xcb_randr_get_crtc_info_reply(dpy, xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME), NULL);
281                 if (cir != NULL) {
282                     xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
283                     mm = get_monitor_by_id(outputs[i]);
284                     if (mm != NULL) {
285                         mm->rectangle = rect;
286                         update_root(mm);
287                         for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
288                             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
289                                 fit_monitor(mm, n->client);
290                         arrange(mm, mm->desk);
291                         mm->wired = true;
292                         PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
293                     } else {
294                         mm = add_monitor(rect);
295                         char *name = (char *)xcb_randr_get_output_info_name(info);
296                         size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info) + 1);
297                         snprintf(mm->name, name_len, "%s", name);
298                         mm->id = outputs[i];
299                         PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
300                     }
301                 }
302                 free(cir);
303             } else if (info->connection != XCB_RANDR_CONNECTION_DISCONNECTED) {
304                 m = get_monitor_by_id(outputs[i]);
305                 if (m != NULL)
306                     m->wired = true;
307             }
308         }
309         free(info);
310     }
311
312     /* initially focus the primary monitor and add the first desktop to it */
313     xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
314     if (gpo != NULL) {
315         pri_mon = get_monitor_by_id(gpo->output);
316         if (!running && pri_mon != NULL) {
317             if (mon != pri_mon)
318                 mon = pri_mon;
319             add_desktop(pri_mon, make_desktop(NULL));
320             ewmh_update_current_desktop();
321         }
322     }
323     free(gpo);
324
325     /* handle overlapping monitors */
326     m = mon_head;
327     while (m != NULL) {
328         monitor_t *next = m->next;
329         if (m->wired) {
330             for (monitor_t *mb = mon_head; mb != NULL; mb = mb->next)
331                 if (mb != m && mb->wired && (m->desk == NULL || mb->desk == NULL)
332                         && contains(mb->rectangle, m->rectangle)) {
333                     if (mm == m)
334                         mm = mb;
335                     merge_monitors(m, mb);
336                     remove_monitor(m);
337                     break;
338                 }
339         }
340         m = next;
341     }
342
343     /* merge and remove disconnected monitors */
344     m = mon_head;
345     while (m != NULL) {
346         monitor_t *next = m->next;
347         if (!m->wired) {
348             merge_monitors(m, mm);
349             remove_monitor(m);
350         }
351         m = next;
352     }
353
354     /* add one desktop to each new monitor */
355     for (m = mon_head; m != NULL; m = m->next)
356         if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
357             add_desktop(m, make_desktop(NULL));
358
359     free(sres);
360     update_motion_recorder();
361     return (num_monitors > 0);
362 }