13 monitor_t *make_monitor(xcb_rectangle_t rect)
15 monitor_t *m = malloc(sizeof(monitor_t));
16 snprintf(m->name, sizeof(m->name), "%s%02d", DEFAULT_MON_NAME, ++monitor_uid);
17 m->prev = m->next = NULL;
18 m->desk = m->last_desk = m->desk_head = m->desk_tail = NULL;
20 m->top_padding = m->right_padding = m->bottom_padding = m->left_padding = 0;
22 uint32_t mask = XCB_CW_EVENT_MASK;
23 uint32_t values[] = {XCB_EVENT_MASK_ENTER_WINDOW};
24 m->root = xcb_generate_id(dpy);
25 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);
26 window_lower(m->root);
27 if (focus_follows_pointer)
32 monitor_t *find_monitor(char *name)
34 for (monitor_t *m = mon_head; m != NULL; m = m->next)
35 if (streq(m->name, name))
40 monitor_t *get_monitor_by_id(xcb_randr_output_t id)
42 for (monitor_t *m = mon_head; m != NULL; m = m->next)
48 void fit_monitor(monitor_t *m, client_t *c)
50 xcb_rectangle_t crect = c->floating_rectangle;
51 xcb_rectangle_t mrect = m->rectangle;
52 while (crect.x < mrect.x)
53 crect.x += mrect.width;
54 while (crect.x > (mrect.x + mrect.width - 1))
55 crect.x -= mrect.width;
56 while (crect.y < mrect.y)
57 crect.y += mrect.height;
58 while (crect.y > (mrect.y + mrect.height - 1))
59 crect.y -= mrect.height;
60 c->floating_rectangle = crect;
63 void update_root(monitor_t *m)
65 xcb_rectangle_t rect = m->rectangle;
66 window_move_resize(m->root, rect.x, rect.y, rect.width, rect.height);
69 void select_monitor(monitor_t *m)
74 PRINTF("select monitor %s\n", m->name);
79 if (pointer_follows_monitor)
82 ewmh_update_current_desktop();
86 monitor_t *add_monitor(xcb_rectangle_t rect)
88 monitor_t *m = make_monitor(rect);
102 void remove_monitor(monitor_t *m)
104 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
106 while (m->desk_head != NULL)
107 remove_desktop(m, m->desk_head);
108 monitor_t *prev = m->prev;
109 monitor_t *next = m->next;
123 monitor_t *mm = (last_mon == NULL ? (prev == NULL ? next : prev) : last_mon);
125 focus_node(mm, mm->desk, mm->desk->focus);
131 xcb_destroy_window(dpy, m->root);
137 void merge_monitors(monitor_t *ms, monitor_t *md)
139 PRINTF("merge %s into %s\n", ms->name, md->name);
141 desktop_t *d = ms->desk_head;
143 desktop_t *next = d->next;
144 if (d->root != NULL || strstr(d->name, DEFAULT_DESK_NAME) == NULL)
145 transfer_desktop(ms, md, d);
150 void swap_monitors(monitor_t *m1, monitor_t *m2)
152 if (m1 == NULL || m2 == NULL || m1 == m2)
157 else if (mon_head == m2)
161 else if (mon_tail == m2)
164 monitor_t *p1 = m1->prev;
165 monitor_t *n1 = m1->next;
166 monitor_t *p2 = m2->prev;
167 monitor_t *n2 = m2->next;
169 if (p1 != NULL && p1 != m2)
171 if (n1 != NULL && n1 != m2)
173 if (p2 != NULL && p2 != m1)
175 if (n2 != NULL && n2 != m1)
178 m1->prev = p2 == m1 ? m2 : p2;
179 m1->next = n2 == m1 ? m2 : n2;
180 m2->prev = p1 == m2 ? m1 : p1;
181 m2->next = n1 == m2 ? m1 : n1;
183 ewmh_update_wm_desktops();
184 ewmh_update_desktop_names();
185 ewmh_update_current_desktop();
189 monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
191 monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
193 f = (dir == CYCLE_PREV ? mon_tail : mon_head);
196 if (desktop_matches(f->desk, sel))
198 f = (dir == CYCLE_PREV ? m->prev : m->next);
200 f = (dir == CYCLE_PREV ? mon_tail : mon_head);
206 monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
209 monitor_t *nearest = NULL;
210 xcb_rectangle_t rect = m->rectangle;
211 for (monitor_t *f = mon_head; f != NULL; f = f->next) {
214 if (!desktop_matches(f->desk, sel))
216 xcb_rectangle_t r = f->rectangle;
217 if ((dir == DIR_LEFT && r.x < rect.x) ||
218 (dir == DIR_RIGHT && r.x >= (rect.x + rect.width)) ||
219 (dir == DIR_UP && r.y < rect.y) ||
220 (dir == DIR_DOWN && r.y >= (rect.y + rect.height))) {
221 int d = abs((r.x + r.width / 2) - (rect.x + rect.width / 2)) +
222 abs((r.y + r.height / 2) - (rect.y + rect.height / 2));
232 bool import_monitors(void)
234 PUTS("import monitors");
235 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);
239 monitor_t *m, *mm = NULL;
241 int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
242 xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
244 xcb_randr_get_output_info_cookie_t cookies[len];
245 for (int i = 0; i < len; i++)
246 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
248 for (m = mon_head; m != NULL; m = m->next)
251 for (int i = 0; i < len; i++) {
252 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
253 if (info != NULL && info->crtc != XCB_NONE) {
255 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);
257 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
258 mm = get_monitor_by_id(outputs[i]);
260 mm->rectangle = rect;
262 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
263 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
264 fit_monitor(mm, n->client);
265 arrange(mm, mm->desk);
267 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
269 mm = add_monitor(rect);
270 char *name = (char *)xcb_randr_get_output_info_name(info);
271 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info) + 1);
272 snprintf(mm->name, name_len, "%s", name);
274 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
282 /* initially focus the primary monitor and add the first desktop to it */
283 xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
285 pri_mon = get_monitor_by_id(gpo->output);
286 if (!running && pri_mon != NULL) {
289 add_desktop(pri_mon, make_desktop(NULL));
290 ewmh_update_current_desktop();
295 /* handle overlapping monitors */
298 monitor_t *next = m->next;
300 for (monitor_t *mb = mon_head; mb != NULL; mb = mb->next)
301 if (mb != m && mb->wired && (m->desk == NULL || mb->desk == NULL)
302 && contains(mb->rectangle, m->rectangle)) {
305 merge_monitors(m, mb);
313 /* merge and remove disconnected monitors */
316 monitor_t *next = m->next;
318 merge_monitors(m, mm);
324 /* add one desktop to each new monitor */
325 for (m = mon_head; m != NULL; m = m->next)
326 if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
327 add_desktop(m, make_desktop(NULL));
330 update_motion_recorder();
331 return (num_monitors > 0);