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