6 #include <xcb/xcb_event.h>
14 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
16 bool locate_window(xcb_window_t win, window_location_t *loc)
19 desktop_t *d = desk_head;
25 n = first_extrema(d->root);
27 if (n->client->window == win) {
40 void window_draw_border(node_t *n, bool focused)
45 if (border_width < 1 || n->client->border_width < 1)
48 xcb_window_t win = n->client->window;
50 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
52 uint16_t width = actual_rectangle.width;
53 uint16_t height = actual_rectangle.height;
55 uint16_t full_width = width + 2 * border_width;
56 uint16_t full_height = height + 2 * border_width;
58 xcb_rectangle_t inner_rectangles[] =
60 { width, 0, 2 * border_width, height + 2 * border_width },
61 { 0, height, width + 2 * border_width, 2 * border_width }
64 xcb_rectangle_t main_rectangles[] =
66 { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
67 { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
70 xcb_rectangle_t outer_rectangles[] =
72 { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
73 { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
76 xcb_rectangle_t *presel_rectangles;
78 xcb_pixmap_t pix = xcb_generate_id(dpy);
79 xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
81 xcb_gcontext_t gc = xcb_generate_id(dpy);
82 xcb_create_gc(dpy, gc, pix, 0, NULL);
84 uint32_t main_border_color_pxl = get_main_border_color(n->client, focused);
87 if (inner_border_width > 0) {
88 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
89 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
93 if (main_border_width > 0) {
94 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
95 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
99 if (outer_border_width > 0) {
100 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
101 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
104 if (split_mode == MODE_MANUAL && focused) {
105 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
106 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
109 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
110 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
113 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
114 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
117 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
118 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
121 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
122 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
125 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
126 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
127 free(presel_rectangles);
130 /* apply border pixmap */
131 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
133 xcb_free_gc(dpy, gc);
134 xcb_free_pixmap(dpy, pix);
137 void window_close(node_t *n)
139 if (n == NULL || n->client->locked)
142 PRINTF("close window %X\n", n->client->window);
144 xcb_atom_t WM_DELETE_WINDOW;
145 xcb_window_t win = n->client->window;
146 xcb_client_message_event_t e;
148 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
150 WM_DELETE_WINDOW = reply->atom;
153 warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
157 e.response_type = XCB_CLIENT_MESSAGE;
161 e.type = ewmh->WM_PROTOCOLS;
162 e.data.data32[0] = WM_DELETE_WINDOW;
163 e.data.data32[1] = XCB_CURRENT_TIME;
165 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
168 void window_kill(desktop_t *d, node_t *n)
173 PRINTF("kill window %X\n", n->client->window);
175 xcb_kill_client(dpy, n->client->window);
179 void toggle_fullscreen(client_t *c)
181 PRINTF("toggle fullscreen %X\n", c->window);
184 c->fullscreen = false;
185 xcb_atom_t values[] = {XCB_NONE};
186 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
188 c->fullscreen = true;
189 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
190 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
191 window_raise(c->window);
192 window_border_width(c->window, 0);
193 window_move_resize(c->window, 0, 0, screen_width, screen_height);
197 void toggle_floating(node_t *n)
199 if (n == NULL || n->client->transient)
202 PRINTF("toggle floating %X\n", n->client->window);
204 client_t *c = n->client;
205 c->floating = !c->floating;
206 n->vacant = !n->vacant;
207 update_vacant_state(n->parent);
209 window_raise(c->window);
210 else if (is_tiled(c))
211 window_lower(c->window);
214 void toggle_locked(client_t *c)
216 PRINTF("toggle locked %X\n", c->window);
218 c->locked = !c->locked;
221 void list_windows(char *rsp)
225 desktop_t *d = desk_head;
228 node_t *n = first_extrema(d->root);
230 snprintf(line, sizeof(line), "0x%X\n", n->client->window);
231 strncat(rsp, line, REMLEN(rsp));
238 uint32_t get_main_border_color(client_t *c, bool focused)
245 return active_locked_border_color_pxl;
247 return active_border_color_pxl;
250 return urgent_border_color_pxl;
252 return normal_locked_border_color_pxl;
254 return normal_border_color_pxl;
258 void update_floating_rectangle(client_t *c)
260 xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
263 c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
266 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 1, 1};
270 void window_border_width(xcb_window_t win, uint32_t bw)
272 uint32_t values[] = {bw};
273 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
276 void window_move(xcb_window_t win, int16_t x, int16_t y)
278 uint32_t values[] = {x, y};
279 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
282 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
284 uint32_t values[] = {x, y, w, h};
285 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
288 void window_raise(xcb_window_t win)
290 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
291 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
294 void window_lower(xcb_window_t win)
296 uint32_t values[] = {XCB_STACK_MODE_BELOW};
297 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
300 void window_set_visibility(xcb_window_t win, bool visible) {
301 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
302 uint32_t values_on[] = {ROOT_EVENT_MASK};
303 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_off);
305 xcb_map_window(dpy, win);
307 xcb_unmap_window(dpy, win);
308 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_on);
311 void window_hide(xcb_window_t win)
313 window_set_visibility(win, false);
316 void window_show(xcb_window_t win)
318 window_set_visibility(win, true);