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))
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))
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 p)
66 xcb_rectangle_t r = m->rectangle;
67 return (r.x <= p.x && p.x < (r.x + r.width)
68 && r.y <= p.y && p.y < (r.y + r.height));
71 monitor_t *underlying_monitor(client_t *c)
73 xcb_point_t p = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
74 for (monitor_t *m = mon_head; m != NULL; m = m->next)
80 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
82 window_location_t loc;
83 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
84 uint8_t override_redirect = 0;
87 override_redirect = wa->override_redirect;
91 if (override_redirect || locate_window(win, &loc))
94 bool floating = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
96 handle_rules(win, &m, &d, &floating, &transient, &fullscreen, &takes_focus, &manage);
104 client_t *c = make_client(win);
105 update_floating_rectangle(c);
107 xcb_icccm_get_wm_class_reply_t reply;
108 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
109 strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
110 xcb_icccm_get_wm_class_reply_wipe(&reply);
116 node_t *birth = make_node();
120 split_mode = MODE_MANUAL;
122 insert_node(m, d, birth);
124 disable_shadow(c->window);
127 toggle_floating(birth);
129 if (d->focus != NULL && d->focus->client->fullscreen)
130 toggle_fullscreen(m, d->focus->client);
133 toggle_fullscreen(m, birth->client);
136 window_lower(c->window);
138 c->transient = transient;
141 focus_node(m, d, birth, false);
143 xcb_rectangle_t *frect = &birth->client->floating_rectangle;
144 if (frect->x == 0 && frect->y == 0)
145 center(m->rectangle, frect);
147 fit_monitor(m, birth->client);
151 if (d == m->desk && visible)
152 window_show(c->window);
155 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
157 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
158 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
161 ewmh_set_wm_desktop(birth, d);
162 ewmh_update_client_list();
165 void adopt_orphans(void)
167 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
170 int len = xcb_query_tree_children_length(qtr);
171 xcb_window_t *wins = xcb_query_tree_children(qtr);
172 for (int i = 0; i < len; i++) {
174 xcb_window_t win = wins[i];
176 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
177 desktop_location_t loc;
178 if (ewmh_locate_desktop(idx, &loc))
179 manage_window(loc.monitor, loc.desktop, win);
181 manage_window(mon, mon->desk, win);
187 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
192 if (border_width < 1 || n->client->border_width < 1)
195 xcb_window_t win = n->client->window;
197 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
199 uint16_t width = actual_rectangle.width;
200 uint16_t height = actual_rectangle.height;
202 uint16_t full_width = width + 2 * border_width;
203 uint16_t full_height = height + 2 * border_width;
205 xcb_rectangle_t inner_rectangles[] =
207 { width, 0, 2 * border_width, height + 2 * border_width },
208 { 0, height, width + 2 * border_width, 2 * border_width }
211 xcb_rectangle_t main_rectangles[] =
213 { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
214 { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
217 xcb_rectangle_t outer_rectangles[] =
219 { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
220 { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
223 xcb_rectangle_t *presel_rectangles;
225 xcb_pixmap_t pix = xcb_generate_id(dpy);
226 xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
228 xcb_gcontext_t gc = xcb_generate_id(dpy);
229 xcb_create_gc(dpy, gc, pix, 0, NULL);
231 uint32_t main_border_color_pxl = get_main_border_color(n->client, focused_window, focused_monitor);
234 if (inner_border_width > 0) {
235 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
236 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
240 if (main_border_width > 0) {
241 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
242 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
246 if (outer_border_width > 0) {
247 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
248 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
251 if (split_mode == MODE_MANUAL && focused_monitor && focused_window) {
252 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
253 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
256 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
257 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
260 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
261 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
264 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
265 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
268 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
269 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
272 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
273 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
274 free(presel_rectangles);
277 /* apply border pixmap */
278 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
280 xcb_free_gc(dpy, gc);
281 xcb_free_pixmap(dpy, pix);
284 void window_close(node_t *n)
286 if (n == NULL || n->client->locked)
289 PRINTF("close window %X\n", n->client->window);
291 xcb_atom_t WM_DELETE_WINDOW;
292 xcb_window_t win = n->client->window;
293 xcb_client_message_event_t e;
295 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
297 WM_DELETE_WINDOW = reply->atom;
300 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
304 e.response_type = XCB_CLIENT_MESSAGE;
308 e.type = ewmh->WM_PROTOCOLS;
309 e.data.data32[0] = WM_DELETE_WINDOW;
310 e.data.data32[1] = XCB_CURRENT_TIME;
312 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
315 void window_kill(desktop_t *d, node_t *n)
320 PRINTF("kill window %X\n", n->client->window);
322 xcb_kill_client(dpy, n->client->window);
326 void toggle_fullscreen(monitor_t *m, client_t *c)
328 PRINTF("toggle fullscreen %X\n", c->window);
331 c->fullscreen = false;
332 xcb_atom_t values[] = {XCB_NONE};
333 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
335 window_lower(c->window);
337 c->fullscreen = true;
338 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
339 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
340 window_raise(c->window);
341 window_border_width(c->window, 0);
342 xcb_rectangle_t r = m->rectangle;
343 window_move_resize(c->window, r.x, r.y, r.width, r.height);
348 void toggle_floating(node_t *n)
350 if (n == NULL || n->client->transient)
353 PRINTF("toggle floating %X\n", n->client->window);
355 client_t *c = n->client;
356 c->floating = !c->floating;
357 n->vacant = !n->vacant;
358 update_vacant_state(n->parent);
360 window_raise(c->window);
361 else if (is_tiled(c))
362 window_lower(c->window);
364 enable_shadow(c->window);
366 disable_shadow(c->window);
370 void toggle_locked(client_t *c)
372 PRINTF("toggle locked %X\n", c->window);
374 c->locked = !c->locked;
377 void set_shadow(xcb_window_t win, uint32_t value)
379 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, sizeof(value), &value);
382 void enable_shadow(xcb_window_t win)
387 void disable_shadow(xcb_window_t win)
392 void list_windows(char *rsp)
396 for (monitor_t *m = mon_head; m != NULL; m = m->next)
397 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
398 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
399 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
400 strncat(rsp, line, REMLEN(rsp));
404 uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_monitor)
409 if (focused_monitor && focused_window) {
411 return focused_locked_border_color_pxl;
413 return focused_border_color_pxl;
414 } else if (focused_window) {
416 return urgent_border_color_pxl;
418 return active_locked_border_color_pxl;
420 return active_border_color_pxl;
423 return urgent_border_color_pxl;
425 return normal_locked_border_color_pxl;
427 return normal_border_color_pxl;
431 void update_floating_rectangle(client_t *c)
433 xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
436 c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
439 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
444 void save_pointer_position(xcb_point_t *pos)
446 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
448 *pos = (xcb_point_t) {qpr->root_x, qpr->root_y};
453 void window_focus(xcb_window_t win)
455 window_location_t loc;
456 if (locate_window(win, &loc)) {
457 if (loc.node == mon->desk->focus)
459 select_monitor(loc.monitor);
460 select_desktop(loc.desktop);
461 focus_node(loc.monitor, loc.desktop, loc.node, true);
465 void window_border_width(xcb_window_t win, uint32_t bw)
467 uint32_t values[] = {bw};
468 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
471 void window_move(xcb_window_t win, int16_t x, int16_t y)
473 uint32_t values[] = {x, y};
474 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
477 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
479 uint32_t values[] = {x, y, w, h};
480 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
483 void window_raise(xcb_window_t win)
485 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
486 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
489 void window_pseudo_raise(desktop_t *d, xcb_window_t win)
491 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n))
492 if (is_tiled(n->client) && n->client->window != win)
493 window_lower(n->client->window);
496 void window_lower(xcb_window_t win)
498 uint32_t values[] = {XCB_STACK_MODE_BELOW};
499 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
502 void window_set_visibility(xcb_window_t win, bool visible) {
503 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
504 uint32_t values_on[] = {ROOT_EVENT_MASK};
505 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
507 xcb_map_window(dpy, win);
509 xcb_unmap_window(dpy, win);
510 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
513 void window_hide(xcb_window_t win)
515 window_set_visibility(win, false);
518 void window_show(xcb_window_t win)
520 window_set_visibility(win, true);
523 void toggle_visibility(void)
526 for (monitor_t *m = mon_head; m != NULL; m = m->next)
527 for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n))
528 window_set_visibility(n->client->window, visible);