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 monitor_t *m = mon_head;
34 desktop_t *d = m->desk_head;
36 if (strcmp(d->name, name) == 0) {
48 void manage_window(xcb_window_t win)
50 window_location_t loc;
51 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
52 uint8_t override_redirect = 0;
55 override_redirect = wa->override_redirect;
59 if (override_redirect || locate_window(win, &loc))
62 bool floating = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
64 handle_rules(win, &floating, &transient, &fullscreen, &takes_focus, &manage);
71 desktop_t *desk = mon->desk;
72 client_t *c = make_client(win);
73 update_floating_rectangle(c);
75 xcb_icccm_get_wm_class_reply_t reply;
76 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
77 strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
78 xcb_icccm_get_wm_class_reply_wipe(&reply);
84 node_t *birth = make_node();
88 split_mode = MODE_MANUAL;
90 insert_node(desk, birth);
93 toggle_floating(birth);
95 if (desk->focus != NULL && desk->focus->client->fullscreen)
96 toggle_fullscreen(mon, desk->focus->client);
99 toggle_fullscreen(mon, birth->client);
102 window_lower(c->window);
104 c->transient = transient;
107 focus_node(mon, desk, birth, false);
109 fit_monitor(mon, birth->client);
111 window_show(c->window);
114 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
116 uint32_t values[] = {CLIENT_EVENT_MASK};
117 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
120 ewmh_set_wm_desktop(birth, desk);
121 ewmh_update_client_list();
124 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
129 if (border_width < 1 || n->client->border_width < 1)
132 xcb_window_t win = n->client->window;
134 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
136 uint16_t width = actual_rectangle.width;
137 uint16_t height = actual_rectangle.height;
139 uint16_t full_width = width + 2 * border_width;
140 uint16_t full_height = height + 2 * border_width;
142 xcb_rectangle_t inner_rectangles[] =
144 { width, 0, 2 * border_width, height + 2 * border_width },
145 { 0, height, width + 2 * border_width, 2 * border_width }
148 xcb_rectangle_t main_rectangles[] =
150 { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
151 { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
154 xcb_rectangle_t outer_rectangles[] =
156 { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
157 { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
160 xcb_rectangle_t *presel_rectangles;
162 xcb_pixmap_t pix = xcb_generate_id(dpy);
163 xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
165 xcb_gcontext_t gc = xcb_generate_id(dpy);
166 xcb_create_gc(dpy, gc, pix, 0, NULL);
168 uint32_t main_border_color_pxl = get_main_border_color(n->client, focused_window, focused_monitor);
171 if (inner_border_width > 0) {
172 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
173 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
177 if (main_border_width > 0) {
178 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
179 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
183 if (outer_border_width > 0) {
184 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
185 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
188 if (split_mode == MODE_MANUAL && focused_monitor && focused_window) {
189 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
190 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
193 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
194 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
197 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
198 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
201 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
202 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
205 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
206 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
209 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
210 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
211 free(presel_rectangles);
214 /* apply border pixmap */
215 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
217 xcb_free_gc(dpy, gc);
218 xcb_free_pixmap(dpy, pix);
221 void window_close(node_t *n)
223 if (n == NULL || n->client->locked)
226 PRINTF("close window %X\n", n->client->window);
228 xcb_atom_t WM_DELETE_WINDOW;
229 xcb_window_t win = n->client->window;
230 xcb_client_message_event_t e;
232 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
234 WM_DELETE_WINDOW = reply->atom;
237 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
241 e.response_type = XCB_CLIENT_MESSAGE;
245 e.type = ewmh->WM_PROTOCOLS;
246 e.data.data32[0] = WM_DELETE_WINDOW;
247 e.data.data32[1] = XCB_CURRENT_TIME;
249 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
252 void window_kill(desktop_t *d, node_t *n)
257 PRINTF("kill window %X\n", n->client->window);
259 xcb_kill_client(dpy, n->client->window);
263 void toggle_fullscreen(monitor_t *m, client_t *c)
265 PRINTF("toggle fullscreen %X\n", c->window);
268 c->fullscreen = false;
269 xcb_atom_t values[] = {XCB_NONE};
270 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
272 window_lower(c->window);
274 c->fullscreen = true;
275 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
276 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
277 window_raise(c->window);
278 window_border_width(c->window, 0);
279 xcb_rectangle_t r = m->rectangle;
280 window_move_resize(c->window, r.x, r.y, r.width, r.height);
284 void toggle_floating(node_t *n)
286 if (n == NULL || n->client->transient)
289 PRINTF("toggle floating %X\n", n->client->window);
291 client_t *c = n->client;
292 c->floating = !c->floating;
293 n->vacant = !n->vacant;
294 update_vacant_state(n->parent);
296 window_raise(c->window);
297 else if (is_tiled(c))
298 window_lower(c->window);
301 void toggle_locked(client_t *c)
303 PRINTF("toggle locked %X\n", c->window);
305 c->locked = !c->locked;
308 void list_windows(char *rsp)
312 for (monitor_t *m = mon_head; m != NULL; m = m->next)
313 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
314 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
315 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
316 strncat(rsp, line, REMLEN(rsp));
320 uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_monitor)
325 if (focused_monitor && focused_window) {
327 return focused_locked_border_color_pxl;
329 return focused_border_color_pxl;
330 } else if (focused_window) {
332 return active_locked_border_color_pxl;
334 return active_border_color_pxl;
337 return urgent_border_color_pxl;
339 return normal_locked_border_color_pxl;
341 return normal_border_color_pxl;
345 void update_floating_rectangle(client_t *c)
347 xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
350 c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
353 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
357 void window_border_width(xcb_window_t win, uint32_t bw)
359 uint32_t values[] = {bw};
360 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
363 void window_move(xcb_window_t win, int16_t x, int16_t y)
365 uint32_t values[] = {x, y};
366 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
369 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
371 uint32_t values[] = {x, y, w, h};
372 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
375 void window_raise(xcb_window_t win)
377 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
378 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
381 void window_lower(xcb_window_t win)
383 uint32_t values[] = {XCB_STACK_MODE_BELOW};
384 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
387 void window_set_visibility(xcb_window_t win, bool visible) {
388 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
389 uint32_t values_on[] = {ROOT_EVENT_MASK};
390 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_off);
392 xcb_map_window(dpy, win);
394 xcb_unmap_window(dpy, win);
395 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_on);
398 void window_hide(xcb_window_t win)
400 window_set_visibility(win, false);
403 void window_show(xcb_window_t win)
405 window_set_visibility(win, true);