1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
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.
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.
38 monitor_t *make_monitor(xcb_rectangle_t rect)
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;
45 m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 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)
58 monitor_t *find_monitor(char *name)
60 for (monitor_t *m = mon_head; m != NULL; m = m->next)
61 if (streq(m->name, name))
66 monitor_t *get_monitor_by_id(xcb_randr_output_t id)
68 for (monitor_t *m = mon_head; m != NULL; m = m->next)
74 void fit_monitor(monitor_t *m, client_t *c)
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)
82 b->x = a.x + (a.width - b->width) / 2;
84 if (b->y <= a.y || (b->y + b->height) >= (a.y + a.height)) {
85 if (b->height >= a.height)
88 b->y = a.y + (a.height - b->height) / 2;
92 void update_root(monitor_t *m)
94 xcb_rectangle_t rect = m->rectangle;
95 window_move_resize(m->root, rect.x, rect.y, rect.width, rect.height);
98 void focus_monitor(monitor_t *m)
103 PRINTF("focus monitor %s\n", m->name);
107 if (pointer_follows_monitor)
110 ewmh_update_current_desktop();
114 monitor_t *add_monitor(xcb_rectangle_t rect)
116 monitor_t *m = make_monitor(rect);
130 void remove_monitor(monitor_t *m)
132 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
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);
150 mon = (last_mon == NULL ? (prev == NULL ? next : prev) : last_mon);
151 if (mon != NULL && mon->desk != NULL)
154 xcb_destroy_window(dpy, m->root);
160 void merge_monitors(monitor_t *ms, monitor_t *md)
162 PRINTF("merge %s into %s\n", ms->name, md->name);
164 desktop_t *d = ms->desk_head;
166 desktop_t *next = d->next;
167 if (d->root != NULL || strstr(d->name, DEFAULT_DESK_NAME) == NULL)
168 transfer_desktop(ms, md, d);
173 void swap_monitors(monitor_t *m1, monitor_t *m2)
175 if (m1 == NULL || m2 == NULL || m1 == m2)
180 else if (mon_head == m2)
184 else if (mon_tail == m2)
187 monitor_t *p1 = m1->prev;
188 monitor_t *n1 = m1->next;
189 monitor_t *p2 = m2->prev;
190 monitor_t *n2 = m2->next;
192 if (p1 != NULL && p1 != m2)
194 if (n1 != NULL && n1 != m2)
196 if (p2 != NULL && p2 != m1)
198 if (n2 != NULL && n2 != m1)
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;
206 ewmh_update_wm_desktops();
207 ewmh_update_desktop_names();
208 ewmh_update_current_desktop();
212 monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
214 monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
216 f = (dir == CYCLE_PREV ? mon_tail : mon_head);
219 coordinates_t loc = {m, m->desk, NULL};
220 if (desktop_matches(&loc, &loc, sel))
222 f = (dir == CYCLE_PREV ? m->prev : m->next);
224 f = (dir == CYCLE_PREV ? mon_tail : mon_head);
230 monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
233 monitor_t *nearest = NULL;
234 xcb_rectangle_t rect = m->rectangle;
235 for (monitor_t *f = mon_head; f != NULL; f = f->next) {
238 coordinates_t loc = {f, f->desk, NULL};
239 if (!desktop_matches(&loc, &loc, sel))
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));
257 bool import_monitors(void)
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);
264 monitor_t *m, *mm = NULL;
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);
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);
273 for (m = mon_head; m != NULL; m = m->next)
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);
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);
282 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
283 mm = get_monitor_by_id(outputs[i]);
285 mm->rectangle = rect;
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);
292 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
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);
299 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
303 } else if (info->connection != XCB_RANDR_CONNECTION_DISCONNECTED) {
304 m = get_monitor_by_id(outputs[i]);
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);
315 pri_mon = get_monitor_by_id(gpo->output);
316 if (!running && pri_mon != NULL) {
319 add_desktop(pri_mon, make_desktop(NULL));
320 ewmh_update_current_desktop();
325 /* handle overlapping monitors */
328 monitor_t *next = m->next;
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)) {
335 merge_monitors(m, mb);
343 /* merge and remove disconnected monitors */
346 monitor_t *next = m->next;
348 merge_monitors(m, mm);
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));
360 update_motion_recorder();
361 return (num_monitors > 0);