6 #include <xcb/xcb_event.h>
7 #include <xcb/xcb_icccm.h>
16 bool locate_window(xcb_window_t win, window_location_t *loc)
18 for (monitor_t *m = mon_head; m != NULL; m = m->next)
19 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
20 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n))
21 if (n->client->window == win) {
30 bool locate_desktop(char *name, desktop_location_t *loc)
32 for (monitor_t *m = mon_head; m != NULL; m = m->next)
33 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
34 if (strcmp(d->name, name) == 0) {
42 bool is_inside(monitor_t *m, xcb_point_t p)
44 xcb_rectangle_t r = m->rectangle;
45 return (r.x <= p.x && p.x < (r.x + r.width)
46 && r.y <= p.y && p.y < (r.y + r.height));
49 monitor_t *underlying_monitor(client_t *c)
51 xcb_point_t p = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
52 for (monitor_t *m = mon_head; m != NULL; m = m->next)
58 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
60 window_location_t loc;
61 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
62 uint8_t override_redirect = 0;
65 override_redirect = wa->override_redirect;
69 if (override_redirect || locate_window(win, &loc))
72 bool floating = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
74 handle_rules(win, &floating, &transient, &fullscreen, &takes_focus, &manage);
81 client_t *c = make_client(win);
82 update_floating_rectangle(c);
84 xcb_icccm_get_wm_class_reply_t reply;
85 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
86 strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
87 xcb_icccm_get_wm_class_reply_wipe(&reply);
93 node_t *birth = make_node();
97 split_mode = MODE_MANUAL;
99 insert_node(m, d, birth);
102 toggle_floating(birth);
104 if (d->focus != NULL && d->focus->client->fullscreen)
105 toggle_fullscreen(m, d->focus->client);
108 toggle_fullscreen(m, birth->client);
111 window_lower(c->window);
113 c->transient = transient;
116 focus_node(m, d, birth, false);
118 fit_monitor(m, birth->client);
121 window_show(c->window);
124 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
126 uint32_t values[] = {CLIENT_EVENT_MASK};
127 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
130 ewmh_set_wm_desktop(birth, d);
131 ewmh_update_client_list();
134 void adopt_orphans(void)
136 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, screen->root), NULL);
139 int len = xcb_query_tree_children_length(qtr);
140 xcb_window_t *wins = xcb_query_tree_children(qtr);
141 for (int i = 0; i < len; i++) {
143 xcb_window_t win = wins[i];
144 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
145 desktop_location_t loc;
146 if (ewmh_locate_desktop(idx, &loc))
147 manage_window(loc.monitor, loc.desktop, win);
149 manage_window(mon, mon->desk, win);
155 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
160 if (border_width < 1 || n->client->border_width < 1)
163 xcb_window_t win = n->client->window;
165 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
167 uint16_t width = actual_rectangle.width;
168 uint16_t height = actual_rectangle.height;
170 uint16_t full_width = width + 2 * border_width;
171 uint16_t full_height = height + 2 * border_width;
173 xcb_rectangle_t inner_rectangles[] =
175 { width, 0, 2 * border_width, height + 2 * border_width },
176 { 0, height, width + 2 * border_width, 2 * border_width }
179 xcb_rectangle_t main_rectangles[] =
181 { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
182 { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
185 xcb_rectangle_t outer_rectangles[] =
187 { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
188 { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
191 xcb_rectangle_t *presel_rectangles;
193 xcb_pixmap_t pix = xcb_generate_id(dpy);
194 xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
196 xcb_gcontext_t gc = xcb_generate_id(dpy);
197 xcb_create_gc(dpy, gc, pix, 0, NULL);
199 uint32_t main_border_color_pxl = get_main_border_color(n->client, focused_window, focused_monitor);
202 if (inner_border_width > 0) {
203 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
204 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
208 if (main_border_width > 0) {
209 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
210 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
214 if (outer_border_width > 0) {
215 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
216 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
219 if (split_mode == MODE_MANUAL && focused_monitor && focused_window) {
220 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
221 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
224 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
225 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
228 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
229 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
232 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
233 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
236 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
237 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
240 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
241 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
242 free(presel_rectangles);
245 /* apply border pixmap */
246 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
248 xcb_free_gc(dpy, gc);
249 xcb_free_pixmap(dpy, pix);
252 void window_close(node_t *n)
254 if (n == NULL || n->client->locked)
257 PRINTF("close window %X\n", n->client->window);
259 xcb_atom_t WM_DELETE_WINDOW;
260 xcb_window_t win = n->client->window;
261 xcb_client_message_event_t e;
263 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
265 WM_DELETE_WINDOW = reply->atom;
268 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
272 e.response_type = XCB_CLIENT_MESSAGE;
276 e.type = ewmh->WM_PROTOCOLS;
277 e.data.data32[0] = WM_DELETE_WINDOW;
278 e.data.data32[1] = XCB_CURRENT_TIME;
280 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
283 void window_kill(desktop_t *d, node_t *n)
288 PRINTF("kill window %X\n", n->client->window);
290 xcb_kill_client(dpy, n->client->window);
294 void toggle_fullscreen(monitor_t *m, client_t *c)
296 PRINTF("toggle fullscreen %X\n", c->window);
299 c->fullscreen = false;
300 xcb_atom_t values[] = {XCB_NONE};
301 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
303 window_lower(c->window);
305 c->fullscreen = true;
306 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
307 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
308 window_raise(c->window);
309 window_border_width(c->window, 0);
310 xcb_rectangle_t r = m->rectangle;
311 window_move_resize(c->window, r.x, r.y, r.width, r.height);
316 void toggle_floating(node_t *n)
318 if (n == NULL || n->client->transient)
321 PRINTF("toggle floating %X\n", n->client->window);
323 client_t *c = n->client;
324 c->floating = !c->floating;
325 n->vacant = !n->vacant;
326 update_vacant_state(n->parent);
328 window_raise(c->window);
329 else if (is_tiled(c))
330 window_lower(c->window);
334 void toggle_locked(client_t *c)
336 PRINTF("toggle locked %X\n", c->window);
338 c->locked = !c->locked;
341 void list_windows(char *rsp)
345 for (monitor_t *m = mon_head; m != NULL; m = m->next)
346 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
347 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
348 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
349 strncat(rsp, line, REMLEN(rsp));
353 uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_monitor)
358 if (focused_monitor && focused_window) {
360 return focused_locked_border_color_pxl;
362 return focused_border_color_pxl;
363 } else if (focused_window) {
365 return active_locked_border_color_pxl;
367 return active_border_color_pxl;
370 return urgent_border_color_pxl;
372 return normal_locked_border_color_pxl;
374 return normal_border_color_pxl;
378 void update_floating_rectangle(client_t *c)
380 xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
383 c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
386 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
390 void window_border_width(xcb_window_t win, uint32_t bw)
392 uint32_t values[] = {bw};
393 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
396 void window_move(xcb_window_t win, int16_t x, int16_t y)
398 uint32_t values[] = {x, y};
399 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
402 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
404 uint32_t values[] = {x, y, w, h};
405 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
408 void window_raise(xcb_window_t win)
410 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
411 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
414 void window_pseudo_raise(desktop_t *d, xcb_window_t win)
416 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n))
417 if (is_tiled(n->client) && n->client->window != win)
418 window_lower(n->client->window);
421 void window_lower(xcb_window_t win)
423 uint32_t values[] = {XCB_STACK_MODE_BELOW};
424 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
427 void window_set_visibility(xcb_window_t win, bool visible) {
428 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
429 uint32_t values_on[] = {ROOT_EVENT_MASK};
430 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_off);
432 xcb_map_window(dpy, win);
434 xcb_unmap_window(dpy, win);
435 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_on);
438 void window_hide(xcb_window_t win)
440 window_set_visibility(win, false);
443 void window_show(xcb_window_t win)
445 window_set_visibility(win, true);