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 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
73 xcb_rectangle_t rect = (is_tiled(c) ? c->tiled_rectangle : c->floating_rectangle);
76 pt->x = rect.x + rect.width;
77 pt->y = rect.y + (rect.height / 2);
80 pt->x = rect.x + (rect.width / 2);
81 pt->y = rect.y + rect.height;
85 pt->y = rect.y + (rect.height / 2);
88 pt->x = rect.x + (rect.width / 2);
94 monitor_t *monitor_from_point(xcb_point_t pt)
96 for (monitor_t *m = mon_head; m != NULL; m = m->next)
102 monitor_t *underlying_monitor(client_t *c)
104 xcb_point_t pt = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
105 return monitor_from_point(pt);
108 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
110 window_location_t loc;
111 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
112 uint8_t override_redirect = 0;
115 override_redirect = wa->override_redirect;
119 if (override_redirect || locate_window(win, &loc))
122 bool floating = false, follow = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
124 handle_rules(win, &m, &d, &floating, &follow, &transient, &fullscreen, &takes_focus, &manage);
132 client_t *c = make_client(win);
133 update_floating_rectangle(c);
135 xcb_icccm_get_wm_class_reply_t reply;
136 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
137 strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
138 xcb_icccm_get_wm_class_reply_wipe(&reply);
144 node_t *birth = make_node();
147 insert_node(m, d, birth);
149 disable_shadow(c->window);
152 toggle_floating(birth);
154 if (d->focus != NULL && d->focus->client->fullscreen)
155 toggle_fullscreen(m, d->focus->client);
158 toggle_fullscreen(m, birth->client);
161 window_lower(c->window);
163 c->transient = transient;
165 bool give_focus = takes_focus && (d == mon->desk || follow);
168 focus_node(m, d, birth);
170 xcb_rectangle_t *frect = &birth->client->floating_rectangle;
171 if (frect->x == 0 && frect->y == 0)
172 center(m->rectangle, frect);
174 fit_monitor(m, birth->client);
178 if (d == m->desk && visible)
179 window_show(c->window);
181 /* the same function is already called in `focus_node` but has no effects on unmapped windows */
183 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
185 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
186 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
189 ewmh_set_wm_desktop(birth, d);
190 ewmh_update_client_list();
193 void adopt_orphans(void)
195 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
198 int len = xcb_query_tree_children_length(qtr);
199 xcb_window_t *wins = xcb_query_tree_children(qtr);
200 for (int i = 0; i < len; i++) {
202 xcb_window_t win = wins[i];
204 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
205 desktop_location_t loc;
206 if (ewmh_locate_desktop(idx, &loc))
207 manage_window(loc.monitor, loc.desktop, win);
209 manage_window(mon, mon->desk, win);
215 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
217 if (n == NULL || border_width < 1 || n->client->border_width < 1)
220 xcb_window_t win = n->client->window;
221 uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
223 if (split_mode == MODE_AUTOMATIC || !focused_monitor || !focused_window) {
224 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
226 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
228 uint16_t width = actual_rectangle.width;
229 uint16_t height = actual_rectangle.height;
231 uint16_t full_width = width + 2 * border_width;
232 uint16_t full_height = height + 2 * border_width;
234 xcb_rectangle_t border_rectangles[] =
236 { width, 0, 2 * border_width, height + 2 * border_width },
237 { 0, height, width + 2 * border_width, 2 * border_width }
240 xcb_rectangle_t *presel_rectangles;
242 uint8_t win_depth = root_depth;
243 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
245 win_depth = geo->depth;
248 xcb_pixmap_t pix = xcb_generate_id(dpy);
249 xcb_create_pixmap(dpy, win_depth, pix, win, full_width, full_height);
251 xcb_gcontext_t gc = xcb_generate_id(dpy);
252 xcb_create_gc(dpy, gc, pix, 0, NULL);
254 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
255 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(border_rectangles), border_rectangles);
257 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
258 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
261 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
262 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
265 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
266 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
269 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
270 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
273 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
274 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
278 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
279 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
280 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
281 free(presel_rectangles);
282 xcb_free_gc(dpy, gc);
283 xcb_free_pixmap(dpy, pix);
287 void window_close(node_t *n)
289 if (n == NULL || n->client->locked)
292 PRINTF("close window %X\n", n->client->window);
294 xcb_atom_t WM_DELETE_WINDOW;
295 xcb_window_t win = n->client->window;
296 xcb_client_message_event_t e;
298 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
300 WM_DELETE_WINDOW = reply->atom;
303 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
307 e.response_type = XCB_CLIENT_MESSAGE;
311 e.type = ewmh->WM_PROTOCOLS;
312 e.data.data32[0] = WM_DELETE_WINDOW;
313 e.data.data32[1] = XCB_CURRENT_TIME;
315 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
318 void window_kill(desktop_t *d, node_t *n)
323 PRINTF("kill window %X\n", n->client->window);
325 xcb_kill_client(dpy, n->client->window);
329 void toggle_fullscreen(monitor_t *m, client_t *c)
331 PRINTF("toggle fullscreen %X\n", c->window);
334 c->fullscreen = false;
335 xcb_atom_t values[] = {XCB_NONE};
336 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
338 window_lower(c->window);
340 c->fullscreen = true;
341 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
342 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
343 window_raise(c->window);
344 window_border_width(c->window, 0);
345 xcb_rectangle_t r = m->rectangle;
346 window_move_resize(c->window, r.x, r.y, r.width, r.height);
351 void toggle_floating(node_t *n)
353 if (n == NULL || n->client->transient || n->client->fullscreen)
356 PRINTF("toggle floating %X\n", n->client->window);
358 client_t *c = n->client;
359 c->floating = !c->floating;
360 n->vacant = !n->vacant;
361 update_vacant_state(n->parent);
363 window_raise(c->window);
364 enable_shadow(c->window);
367 window_lower(c->window);
368 disable_shadow(c->window);
374 void toggle_locked(client_t *c)
376 PRINTF("toggle locked %X\n", c->window);
378 c->locked = !c->locked;
381 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
383 if (value && mon->desk->focus == n)
385 n->client->urgent = value;
391 void set_shadow(xcb_window_t win, uint32_t value)
393 if (!apply_shadow_property)
395 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, 1, &value);
398 void enable_shadow(xcb_window_t win)
403 void disable_shadow(xcb_window_t win)
408 void list_windows(char *rsp)
412 for (monitor_t *m = mon_head; m != NULL; m = m->next)
413 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
414 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
415 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
416 strncat(rsp, line, REMLEN(rsp));
420 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
425 if (focused_monitor && focused_window) {
427 return focused_locked_border_color_pxl;
429 return focused_border_color_pxl;
430 } else if (focused_window) {
432 return urgent_border_color_pxl;
434 return active_locked_border_color_pxl;
436 return active_border_color_pxl;
439 return urgent_border_color_pxl;
441 return normal_locked_border_color_pxl;
443 return normal_border_color_pxl;
447 void update_floating_rectangle(client_t *c)
449 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
452 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
454 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
460 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
462 window_lower(motion_recorder);
463 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
468 *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
471 window_raise(motion_recorder);
474 void window_focus(xcb_window_t win)
476 window_location_t loc;
477 if (locate_window(win, &loc)) {
478 if (loc.node == mon->desk->focus)
480 focus_node(loc.monitor, loc.desktop, loc.node);
484 void window_border_width(xcb_window_t win, uint32_t bw)
486 uint32_t values[] = {bw};
487 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
490 void window_move(xcb_window_t win, int16_t x, int16_t y)
492 uint32_t values[] = {x, y};
493 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
496 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
498 uint32_t values[] = {x, y, w, h};
499 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
502 void window_raise(xcb_window_t win)
504 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
505 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
508 void stack_tiled(desktop_t *d)
510 for (node_list_t *a = d->history->head; a != NULL; a = a->next)
511 if (a->latest && is_tiled(a->node->client))
512 window_lower(a->node->client->window);
515 void window_lower(xcb_window_t win)
517 uint32_t values[] = {XCB_STACK_MODE_BELOW};
518 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
521 void window_set_visibility(xcb_window_t win, bool visible)
523 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
524 uint32_t values_on[] = {ROOT_EVENT_MASK};
525 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
527 xcb_map_window(dpy, win);
529 xcb_unmap_window(dpy, win);
530 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
533 void window_hide(xcb_window_t win)
535 window_set_visibility(win, false);
538 void window_show(xcb_window_t win)
540 window_set_visibility(win, true);
543 void toggle_visibility(void)
548 for (monitor_t *m = mon_head; m != NULL; m = m->next)
549 for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
550 window_set_visibility(n->client->window, visible);
555 void enable_motion_recorder(void)
557 window_raise(motion_recorder);
558 window_show(motion_recorder);
561 void disable_motion_recorder(void)
563 window_hide(motion_recorder);
566 void clear_input_focus(void)
568 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);