6 #include <xcb/xcb_event.h>
7 #include <xcb/xcb_icccm.h>
16 void center(xcb_rectangle_t a, xcb_rectangle_t *b)
18 if (b->width < a.width)
19 b->x = a.x + (a.width - b->width) / 2;
20 if (b->height < a.height)
21 b->y = a.y + (a.height - b->height) / 2;
24 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
26 return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width)
27 && a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
30 bool might_cover(desktop_t *d, node_t *n)
32 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root))
33 if (f != n && is_floating(f->client) && contains(n->client->floating_rectangle, f->client->floating_rectangle))
38 bool locate_window(xcb_window_t win, window_location_t *loc)
40 for (monitor_t *m = mon_head; m != NULL; m = m->next)
41 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
42 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
43 if (n->client->window == win) {
52 bool locate_desktop(char *name, desktop_location_t *loc)
54 for (monitor_t *m = mon_head; m != NULL; m = m->next)
55 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
56 if (strcmp(d->name, name) == 0) {
64 bool is_inside(monitor_t *m, xcb_point_t pt)
66 xcb_rectangle_t r = m->rectangle;
67 return (r.x <= pt.x && pt.x < (r.x + r.width)
68 && r.y <= pt.y && pt.y < (r.y + r.height));
71 xcb_rectangle_t get_rectangle(client_t *c)
74 return c->tiled_rectangle;
76 return c->floating_rectangle;
79 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
81 xcb_rectangle_t rect = get_rectangle(c);
84 pt->x = rect.x + rect.width;
85 pt->y = rect.y + (rect.height / 2);
88 pt->x = rect.x + (rect.width / 2);
89 pt->y = rect.y + rect.height;
93 pt->y = rect.y + (rect.height / 2);
96 pt->x = rect.x + (rect.width / 2);
102 monitor_t *monitor_from_point(xcb_point_t pt)
104 for (monitor_t *m = mon_head; m != NULL; m = m->next)
105 if (is_inside(m, pt))
110 monitor_t *underlying_monitor(client_t *c)
112 xcb_point_t pt = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
113 return monitor_from_point(pt);
116 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
118 window_location_t loc;
119 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
120 uint8_t override_redirect = 0;
123 override_redirect = wa->override_redirect;
127 if (override_redirect || locate_window(win, &loc))
130 bool floating = false, follow = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
132 handle_rules(win, &m, &d, &floating, &follow, &transient, &fullscreen, &takes_focus, &manage);
140 client_t *c = make_client(win);
141 update_floating_rectangle(c);
143 xcb_icccm_get_wm_class_reply_t reply;
144 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
145 strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
146 xcb_icccm_get_wm_class_reply_wipe(&reply);
152 node_t *n = make_node();
155 insert_node(m, d, n, d->focus);
157 disable_shadow(c->window);
160 toggle_floating(d, n);
162 if (d->focus != NULL && d->focus->client->fullscreen)
163 toggle_fullscreen(d, d->focus);
166 toggle_fullscreen(d, n);
169 window_lower(c->window);
171 c->transient = transient;
173 bool give_focus = takes_focus && (d == mon->desk || follow);
177 else if (takes_focus)
180 xcb_rectangle_t *frect = &n->client->floating_rectangle;
181 if (frect->x == 0 && frect->y == 0)
182 center(m->rectangle, frect);
184 fit_monitor(m, n->client);
188 if (d == m->desk && visible)
189 window_show(c->window);
191 /* the same function is already called in `focus_node` but has no effects on unmapped windows */
193 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
195 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
196 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
199 ewmh_set_wm_desktop(n, d);
200 ewmh_update_client_list();
203 void adopt_orphans(void)
205 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
209 PUTS("adopt orphans");
211 int len = xcb_query_tree_children_length(qtr);
212 xcb_window_t *wins = xcb_query_tree_children(qtr);
213 for (int i = 0; i < len; i++) {
215 xcb_window_t win = wins[i];
217 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
218 desktop_location_t loc;
219 if (ewmh_locate_desktop(idx, &loc))
220 manage_window(loc.monitor, loc.desktop, win);
222 manage_window(mon, mon->desk, win);
228 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
230 if (n == NULL || border_width < 1 || n->client->border_width < 1)
233 xcb_window_t win = n->client->window;
234 uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
236 if (n->split_mode == MODE_AUTOMATIC) {
237 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
239 xcb_rectangle_t actual_rectangle = get_rectangle(n->client);
241 uint16_t width = actual_rectangle.width;
242 uint16_t height = actual_rectangle.height;
244 uint16_t full_width = width + 2 * border_width;
245 uint16_t full_height = height + 2 * border_width;
247 xcb_rectangle_t border_rectangles[] =
249 { width, 0, 2 * border_width, height + 2 * border_width },
250 { 0, height, width + 2 * border_width, 2 * border_width }
253 xcb_rectangle_t *presel_rectangles;
255 uint8_t win_depth = root_depth;
256 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
258 win_depth = geo->depth;
261 xcb_pixmap_t pix = xcb_generate_id(dpy);
262 xcb_create_pixmap(dpy, win_depth, pix, win, full_width, full_height);
264 xcb_gcontext_t gc = xcb_generate_id(dpy);
265 xcb_create_gc(dpy, gc, pix, 0, NULL);
267 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
268 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(border_rectangles), border_rectangles);
270 uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
271 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
272 switch (n->split_dir) {
274 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
275 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
278 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
279 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
282 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
283 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
286 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
287 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
291 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
292 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
293 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
294 free(presel_rectangles);
295 xcb_free_gc(dpy, gc);
296 xcb_free_pixmap(dpy, pix);
300 void window_close(node_t *n)
302 if (n == NULL || n->client->locked)
305 PRINTF("close window %X\n", n->client->window);
307 xcb_atom_t WM_DELETE_WINDOW;
308 xcb_window_t win = n->client->window;
309 xcb_client_message_event_t e;
311 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
313 WM_DELETE_WINDOW = reply->atom;
316 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
320 e.response_type = XCB_CLIENT_MESSAGE;
324 e.type = ewmh->WM_PROTOCOLS;
325 e.data.data32[0] = WM_DELETE_WINDOW;
326 e.data.data32[1] = XCB_CURRENT_TIME;
328 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
331 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
336 xcb_window_t win = n->client->window;
337 PRINTF("kill window %X\n", win);
339 xcb_kill_client(dpy, win);
344 void toggle_fullscreen(desktop_t *d, node_t *n)
349 client_t *c = n->client;
351 PRINTF("toggle fullscreen %X\n", c->window);
354 c->fullscreen = false;
355 xcb_atom_t values[] = {XCB_NONE};
356 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
359 c->fullscreen = true;
360 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
361 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
362 window_raise(c->window);
366 void toggle_floating(desktop_t *d, node_t *n)
368 if (n == NULL || n->client->transient || n->client->fullscreen)
371 PRINTF("toggle floating %X\n", n->client->window);
373 n->split_mode = MODE_AUTOMATIC;
374 client_t *c = n->client;
375 c->floating = !c->floating;
376 n->vacant = !n->vacant;
377 update_vacant_state(n->parent);
379 enable_shadow(c->window);
382 disable_shadow(c->window);
388 void toggle_locked(monitor_t *m, desktop_t *d, node_t *n)
393 client_t *c = n->client;
395 PRINTF("toggle locked %X\n", c->window);
397 c->locked = !c->locked;
398 window_draw_border(n, d->focus == n, m == mon);
401 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
403 if (value && mon->desk->focus == n)
405 n->client->urgent = value;
406 window_draw_border(n, d->focus == n, m == mon);
410 void set_shadow(xcb_window_t win, uint32_t value)
412 if (!apply_shadow_property)
414 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, 1, &value);
417 void enable_shadow(xcb_window_t win)
422 void disable_shadow(xcb_window_t win)
427 void list_windows(char *rsp)
431 for (monitor_t *m = mon_head; m != NULL; m = m->next)
432 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
433 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
434 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
435 strncat(rsp, line, REMLEN(rsp));
439 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
444 if (focused_monitor && focused_window) {
446 return focused_locked_border_color_pxl;
448 return focused_border_color_pxl;
449 } else if (focused_window) {
451 return urgent_border_color_pxl;
453 return active_locked_border_color_pxl;
455 return active_border_color_pxl;
458 return urgent_border_color_pxl;
460 return normal_locked_border_color_pxl;
462 return normal_border_color_pxl;
466 void update_floating_rectangle(client_t *c)
468 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
471 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
473 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
479 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
481 window_lower(motion_recorder);
482 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
487 *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
490 window_raise(motion_recorder);
493 void window_focus(xcb_window_t win)
495 window_location_t loc;
496 if (locate_window(win, &loc)) {
497 if (loc.node == mon->desk->focus)
499 focus_node(loc.monitor, loc.desktop, loc.node);
503 void window_border_width(xcb_window_t win, uint32_t bw)
505 uint32_t values[] = {bw};
506 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
509 void window_move(xcb_window_t win, int16_t x, int16_t y)
511 uint32_t values[] = {x, y};
512 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
515 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
517 uint32_t values[] = {w, h};
518 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
521 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
523 uint32_t values[] = {x, y, w, h};
524 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
527 void window_raise(xcb_window_t win)
529 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
530 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
533 void stack_tiled(desktop_t *d)
535 for (node_list_t *a = d->history->head; a != NULL; a = a->next)
536 if (a->latest && is_tiled(a->node->client))
537 window_lower(a->node->client->window);
540 void stack(desktop_t *d, node_t *n)
542 if (!is_tiled(n->client)) {
543 if (!adaptative_raise || !might_cover(d, n))
544 window_raise(n->client->window);
550 void window_lower(xcb_window_t win)
552 uint32_t values[] = {XCB_STACK_MODE_BELOW};
553 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
556 void window_set_visibility(xcb_window_t win, bool visible)
558 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
559 uint32_t values_on[] = {ROOT_EVENT_MASK};
560 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
562 xcb_map_window(dpy, win);
564 xcb_unmap_window(dpy, win);
565 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
568 void window_hide(xcb_window_t win)
570 window_set_visibility(win, false);
573 void window_show(xcb_window_t win)
575 window_set_visibility(win, true);
578 void toggle_visibility(void)
583 for (monitor_t *m = mon_head; m != NULL; m = m->next)
584 for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
585 window_set_visibility(n->client->window, visible);
587 update_input_focus();
590 void desktop_show(desktop_t *d)
594 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
595 window_show(n->client->window);
598 void desktop_hide(desktop_t *d)
602 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
603 window_hide(n->client->window);
606 void enable_motion_recorder(void)
608 PUTS("enable motion recorder");
609 window_raise(motion_recorder);
610 window_show(motion_recorder);
613 void disable_motion_recorder(void)
615 PUTS("disable motion recorder");
616 window_hide(motion_recorder);
619 void update_motion_recorder(void)
621 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
624 window_resize(motion_recorder, geo->width, geo->height);
625 PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
631 void update_input_focus(void)
633 set_input_focus(mon->desk->focus);
636 void set_input_focus(node_t *n)
640 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
643 void clear_input_focus(void)
645 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
648 void center_pointer(monitor_t *m)
650 int16_t cx = m->rectangle.x + m->rectangle.width / 2;
651 int16_t cy = m->rectangle.y + m->rectangle.height / 2;
652 window_lower(motion_recorder);
653 xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
654 window_raise(motion_recorder);