]> git.lizzy.rs Git - bspwm.git/blob - monitor.c
5162d5a82eeff42a24d7a77401b782e7a6f98be0
[bspwm.git] / monitor.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 <limits.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "bspwm.h"
33 #include "desktop.h"
34 #include "ewmh.h"
35 #include "history.h"
36 #include "query.h"
37 #include "settings.h"
38 #include "tree.h"
39 #include "window.h"
40 #include "monitor.h"
41
42 monitor_t *make_monitor(xcb_rectangle_t rect)
43 {
44         monitor_t *m = malloc(sizeof(monitor_t));
45         snprintf(m->name, sizeof(m->name), "%s%02d", DEFAULT_MON_NAME, ++monitor_uid);
46         m->prev = m->next = NULL;
47         m->desk = m->desk_head = m->desk_tail = NULL;
48         m->rectangle = rect;
49         m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 0;
50         m->wired = true;
51         m->num_sticky = 0;
52         uint32_t mask = XCB_CW_EVENT_MASK;
53         uint32_t values[] = {XCB_EVENT_MASK_ENTER_WINDOW};
54         m->root = xcb_generate_id(dpy);
55         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);
56         window_lower(m->root);
57         if (focus_follows_pointer)
58                 window_show(m->root);
59         return m;
60 }
61
62 monitor_t *find_monitor(char *name)
63 {
64         for (monitor_t *m = mon_head; m != NULL; m = m->next)
65                 if (streq(m->name, name))
66                         return m;
67         return NULL;
68 }
69
70 monitor_t *get_monitor_by_id(xcb_randr_output_t id)
71 {
72         for (monitor_t *m = mon_head; m != NULL; m = m->next)
73                 if (m->id == id)
74                         return m;
75         return NULL;
76 }
77
78 void embrace_client(monitor_t *m, client_t *c)
79 {
80         if ((c->floating_rectangle.x + c->floating_rectangle.width) <= m->rectangle.x)
81                 c->floating_rectangle.x = m->rectangle.x;
82         else if (c->floating_rectangle.x >= (m->rectangle.x + m->rectangle.width))
83                 c->floating_rectangle.x = (m->rectangle.x + m->rectangle.width) - c->floating_rectangle.width;
84         if ((c->floating_rectangle.y + c->floating_rectangle.height) <= m->rectangle.y)
85                 c->floating_rectangle.y = m->rectangle.y;
86         else if (c->floating_rectangle.y >= (m->rectangle.y + m->rectangle.height))
87                 c->floating_rectangle.y = (m->rectangle.y + m->rectangle.height) - c->floating_rectangle.height;
88 }
89
90 void translate_client(monitor_t *ms, monitor_t *md, client_t *c)
91 {
92         if (frozen_pointer->action != ACTION_NONE || ms == md)
93                 return;
94
95         /* Clip the rectangle to fit into the monitor.  Without this, the fitting
96          * algorithm doesn't work as expected. This also conserves the
97          * out-of-bounds regions */
98         int left_adjust = MAX((ms->rectangle.x - c->floating_rectangle.x), 0);
99         int top_adjust = MAX((ms->rectangle.y - c->floating_rectangle.y), 0);
100         int right_adjust = MAX((c->floating_rectangle.x + c->floating_rectangle.width) - (ms->rectangle.x + ms->rectangle.width), 0);
101         int bottom_adjust = MAX((c->floating_rectangle.y + c->floating_rectangle.height) - (ms->rectangle.y + ms->rectangle.height), 0);
102         c->floating_rectangle.x += left_adjust;
103         c->floating_rectangle.y += top_adjust;
104         c->floating_rectangle.width -= (left_adjust + right_adjust);
105         c->floating_rectangle.height -= (top_adjust + bottom_adjust);
106
107         int dx_s = c->floating_rectangle.x - ms->rectangle.x;
108         int dy_s = c->floating_rectangle.y - ms->rectangle.y;
109
110         int nume_x = dx_s * (md->rectangle.width - c->floating_rectangle.width);
111         int nume_y = dy_s * (md->rectangle.height - c->floating_rectangle.height);
112
113         int deno_x = ms->rectangle.width - c->floating_rectangle.width;
114         int deno_y = ms->rectangle.height - c->floating_rectangle.height;
115
116         int dx_d = (deno_x == 0 ? 0 : nume_x / deno_x);
117         int dy_d = (deno_y == 0 ? 0 : nume_y / deno_y);
118
119         /* Translate and undo clipping */
120         c->floating_rectangle.width += left_adjust + right_adjust;
121         c->floating_rectangle.height += top_adjust + bottom_adjust;
122         c->floating_rectangle.x = md->rectangle.x + dx_d - left_adjust;
123         c->floating_rectangle.y = md->rectangle.y + dy_d - top_adjust;
124 }
125
126 void update_root(monitor_t *m)
127 {
128         xcb_rectangle_t rect = m->rectangle;
129         window_move_resize(m->root, rect.x, rect.y, rect.width, rect.height);
130 }
131
132 void focus_monitor(monitor_t *m)
133 {
134         if (mon == m)
135                 return;
136
137         PRINTF("focus monitor %s\n", m->name);
138
139         mon = m;
140
141         if (pointer_follows_monitor)
142                 center_pointer(m);
143
144         ewmh_update_current_desktop();
145         put_status();
146 }
147
148 monitor_t *add_monitor(xcb_rectangle_t rect)
149 {
150         monitor_t *m = make_monitor(rect);
151         if (mon == NULL) {
152                 mon = m;
153                 mon_head = m;
154                 mon_tail = m;
155         } else {
156                 mon_tail->next = m;
157                 m->prev = mon_tail;
158                 mon_tail = m;
159         }
160         num_monitors++;
161         return m;
162 }
163
164 void remove_monitor(monitor_t *m)
165 {
166         PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
167
168         while (m->desk_head != NULL)
169                 remove_desktop(m, m->desk_head);
170         monitor_t *prev = m->prev;
171         monitor_t *next = m->next;
172         monitor_t *last_mon = history_get_monitor(m);
173         if (prev != NULL)
174                 prev->next = next;
175         if (next != NULL)
176                 next->prev = prev;
177         if (mon_head == m)
178                 mon_head = next;
179         if (mon_tail == m)
180                 mon_tail = prev;
181         if (pri_mon == m)
182                 pri_mon = NULL;
183         if (mon == m) {
184                 mon = (last_mon == NULL ? (prev == NULL ? next : prev) : last_mon);
185                 if (mon != NULL && mon->desk != NULL)
186                         update_current();
187         }
188         xcb_destroy_window(dpy, m->root);
189         free(m);
190         num_monitors--;
191         put_status();
192 }
193
194 void merge_monitors(monitor_t *ms, monitor_t *md)
195 {
196         PRINTF("merge %s into %s\n", ms->name, md->name);
197
198         desktop_t *d = ms->desk_head;
199         while (d != NULL) {
200                 desktop_t *next = d->next;
201                 if (d->root != NULL || strstr(d->name, DEFAULT_DESK_NAME) == NULL)
202                         transfer_desktop(ms, md, d);
203                 d = next;
204         }
205 }
206
207 void swap_monitors(monitor_t *m1, monitor_t *m2)
208 {
209         if (m1 == NULL || m2 == NULL || m1 == m2)
210                 return;
211
212         if (mon_head == m1)
213                 mon_head = m2;
214         else if (mon_head == m2)
215                 mon_head = m1;
216         if (mon_tail == m1)
217                 mon_tail = m2;
218         else if (mon_tail == m2)
219                 mon_tail = m1;
220
221         monitor_t *p1 = m1->prev;
222         monitor_t *n1 = m1->next;
223         monitor_t *p2 = m2->prev;
224         monitor_t *n2 = m2->next;
225
226         if (p1 != NULL && p1 != m2)
227                 p1->next = m2;
228         if (n1 != NULL && n1 != m2)
229                 n1->prev = m2;
230         if (p2 != NULL && p2 != m1)
231                 p2->next = m1;
232         if (n2 != NULL && n2 != m1)
233                 n2->prev = m1;
234
235         m1->prev = p2 == m1 ? m2 : p2;
236         m1->next = n2 == m1 ? m2 : n2;
237         m2->prev = p1 == m2 ? m1 : p1;
238         m2->next = n1 == m2 ? m1 : n1;
239
240         ewmh_update_wm_desktops();
241         ewmh_update_desktop_names();
242         ewmh_update_current_desktop();
243         put_status();
244 }
245
246 monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
247 {
248         monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
249         if (f == NULL)
250                 f = (dir == CYCLE_PREV ? mon_tail : mon_head);
251
252         while (f != m) {
253                 coordinates_t loc = {m, m->desk, NULL};
254                 if (desktop_matches(&loc, &loc, sel))
255                         return f;
256                 f = (dir == CYCLE_PREV ? m->prev : m->next);
257                 if (f == NULL)
258                         f = (dir == CYCLE_PREV ? mon_tail : mon_head);
259         }
260
261         return NULL;
262 }
263
264 bool is_inside_monitor(monitor_t *m, xcb_point_t pt)
265 {
266         xcb_rectangle_t r = m->rectangle;
267         return (r.x <= pt.x && pt.x < (r.x + r.width)
268                         && r.y <= pt.y && pt.y < (r.y + r.height));
269 }
270
271 monitor_t *monitor_from_point(xcb_point_t pt)
272 {
273         for (monitor_t *m = mon_head; m != NULL; m = m->next)
274                 if (is_inside_monitor(m, pt))
275                         return m;
276         return NULL;
277 }
278
279 monitor_t *monitor_from_client(client_t *c)
280 {
281         xcb_point_t pt = {c->floating_rectangle.x, c->floating_rectangle.y};
282         monitor_t *nearest = monitor_from_point(pt);
283         if (nearest == NULL) {
284                 int x = (c->floating_rectangle.x + c->floating_rectangle.width) / 2;
285                 int y = (c->floating_rectangle.y + c->floating_rectangle.height) / 2;
286                 int dmin = INT_MAX;
287                 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
288                         xcb_rectangle_t r = m->rectangle;
289                         int d = abs((r.x + r.width / 2) - x) + abs((r.y + r.height / 2) - y);
290                         if (d < dmin) {
291                                 dmin = d;
292                                 nearest = m;
293                         }
294                 }
295         }
296         return nearest;
297 }
298
299 monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
300 {
301         int dmin = INT_MAX;
302         monitor_t *nearest = NULL;
303         xcb_rectangle_t rect = m->rectangle;
304         for (monitor_t *f = mon_head; f != NULL; f = f->next) {
305                 if (f == m)
306                         continue;
307                 coordinates_t loc = {f, f->desk, NULL};
308                 if (!desktop_matches(&loc, &loc, sel))
309                         continue;
310                 xcb_rectangle_t r = f->rectangle;
311                 if ((dir == DIR_LEFT && r.x < rect.x) ||
312                     (dir == DIR_RIGHT && r.x >= (rect.x + rect.width)) ||
313                     (dir == DIR_UP && r.y < rect.y) ||
314                     (dir == DIR_DOWN && r.y >= (rect.y + rect.height))) {
315                         int d = abs((r.x + r.width / 2) - (rect.x + rect.width / 2)) +
316                                 abs((r.y + r.height / 2) - (rect.y + rect.height / 2));
317                         if (d < dmin) {
318                                 dmin = d;
319                                 nearest = f;
320                         }
321                 }
322         }
323         return nearest;
324 }
325
326 bool update_monitors(void)
327 {
328         PUTS("update monitors");
329         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);
330         if (sres == NULL)
331                 return false;
332
333         monitor_t *m, *mm = NULL;
334
335         int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
336         xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
337
338         xcb_randr_get_output_info_cookie_t cookies[len];
339         for (int i = 0; i < len; i++)
340                 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
341
342         for (m = mon_head; m != NULL; m = m->next)
343                 m->wired = false;
344
345         for (int i = 0; i < len; i++) {
346                 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
347                 if (info != NULL) {
348                         if (info->crtc != XCB_NONE) {
349                                 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);
350                                 if (cir != NULL) {
351                                         xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
352                                         mm = get_monitor_by_id(outputs[i]);
353                                         if (mm != NULL) {
354                                                 mm->rectangle = rect;
355                                                 update_root(mm);
356                                                 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
357                                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
358                                                                 translate_client(mm, mm, n->client);
359                                                 arrange(mm, mm->desk);
360                                                 mm->wired = true;
361                                                 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
362                                         } else {
363                                                 mm = add_monitor(rect);
364                                                 char *name = (char *)xcb_randr_get_output_info_name(info);
365                                                 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info) + 1);
366                                                 snprintf(mm->name, name_len, "%s", name);
367                                                 mm->id = outputs[i];
368                                                 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
369                                         }
370                                 }
371                                 free(cir);
372                         } else if (!remove_disabled_monitors && info->connection != XCB_RANDR_CONNECTION_DISCONNECTED) {
373                                 m = get_monitor_by_id(outputs[i]);
374                                 if (m != NULL)
375                                         m->wired = true;
376                         }
377                 }
378                 free(info);
379         }
380
381         /* initially focus the primary monitor and add the first desktop to it */
382         xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
383         if (gpo != NULL) {
384                 pri_mon = get_monitor_by_id(gpo->output);
385                 if (!running && pri_mon != NULL) {
386                         if (mon != pri_mon)
387                                 mon = pri_mon;
388                         add_desktop(pri_mon, make_desktop(NULL));
389                         ewmh_update_current_desktop();
390                 }
391         }
392         free(gpo);
393
394         /* handle overlapping monitors */
395         if (merge_overlapping_monitors) {
396                 m = mon_head;
397                 while (m != NULL) {
398                         monitor_t *next = m->next;
399                         if (m->wired) {
400                                 for (monitor_t *mb = mon_head; mb != NULL; mb = mb->next)
401                                         if (mb != m && mb->wired &&
402                                                         (m->desk == NULL || mb->desk == NULL) &&
403                                                         contains(mb->rectangle, m->rectangle)) {
404                                                 if (mm == m)
405                                                         mm = mb;
406                                                 merge_monitors(m, mb);
407                                                 remove_monitor(m);
408                                                 break;
409                                         }
410                         }
411                         m = next;
412                 }
413         }
414
415         /* merge and remove disconnected monitors */
416         if (remove_unplugged_monitors) {
417                 m = mon_head;
418                 while (m != NULL) {
419                         monitor_t *next = m->next;
420                         if (!m->wired) {
421                                 merge_monitors(m, mm);
422                                 remove_monitor(m);
423                         }
424                         m = next;
425                 }
426         }
427
428         /* add one desktop to each new monitor */
429         for (m = mon_head; m != NULL; m = m->next)
430                 if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
431                         add_desktop(m, make_desktop(NULL));
432
433         if (!running && pri_mon != NULL && mon_head != pri_mon)
434                 swap_monitors(mon_head, pri_mon);
435
436         free(sres);
437         update_motion_recorder();
438         return (num_monitors > 0);
439 }