6 #include <xcb/xcb_event.h>
15 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
17 bool locate_window(xcb_window_t win, window_location_t *loc)
20 desktop_t *d = desk_head;
26 n = first_extrema(d->root);
28 if (n->client->window == win) {
41 void window_draw_border(node_t *n, bool focused)
46 if (border_width < 1 || n->client->border_width < 1)
49 xcb_window_t win = n->client->window;
51 xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
53 uint16_t width = actual_rectangle.width;
54 uint16_t height = actual_rectangle.height;
56 uint16_t full_width = width + 2 * border_width;
57 uint16_t full_height = height + 2 * border_width;
59 xcb_rectangle_t inner_rectangles[] =
61 { width, 0, 2 * border_width, height + 2 * border_width },
62 { 0, height, width + 2 * border_width, 2 * border_width }
65 xcb_rectangle_t main_rectangles[] =
67 { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
68 { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
71 xcb_rectangle_t outer_rectangles[] =
73 { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
74 { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
77 xcb_rectangle_t *presel_rectangles;
79 xcb_pixmap_t pix = xcb_generate_id(dpy);
80 xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
82 xcb_gcontext_t gc = xcb_generate_id(dpy);
83 xcb_create_gc(dpy, gc, pix, 0, NULL);
85 uint32_t main_border_color_pxl = get_main_border_color(n->client, focused);
88 if (inner_border_width > 0) {
89 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
90 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
94 if (main_border_width > 0) {
95 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
96 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
100 if (outer_border_width > 0) {
101 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
102 xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
105 if (split_mode == MODE_MANUAL && focused) {
106 uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
107 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
110 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
111 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
114 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
115 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
118 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
119 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
122 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
123 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
126 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
127 xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
128 free(presel_rectangles);
131 /* apply border pixmap */
132 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
134 xcb_free_gc(dpy, gc);
135 xcb_free_pixmap(dpy, pix);
138 void window_close(node_t *n)
140 if (n == NULL || n->client->locked)
143 PRINTF("close window %X\n", n->client->window);
145 xcb_atom_t WM_DELETE_WINDOW;
146 xcb_window_t win = n->client->window;
147 xcb_client_message_event_t e;
149 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
151 WM_DELETE_WINDOW = reply->atom;
154 PUTS("could not acquire WM_DELETE_WINDOW atom");
158 e.response_type = XCB_CLIENT_MESSAGE;
162 e.type = ewmh->WM_PROTOCOLS;
163 e.data.data32[0] = WM_DELETE_WINDOW;
164 e.data.data32[1] = XCB_CURRENT_TIME;
166 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
169 void window_kill(desktop_t *d, node_t *n)
174 PRINTF("kill window %X\n", n->client->window);
176 xcb_kill_client(dpy, n->client->window);
180 void toggle_fullscreen(client_t *c)
183 c->fullscreen = false;
184 xcb_atom_t values[] = {XCB_NONE};
185 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
186 xcb_rectangle_t rect;
188 rect = c->floating_rectangle;
190 rect = c->tiled_rectangle;
191 window_border_width(c->window, c->border_width);
192 window_move_resize(c->window, rect.x, rect.y, rect.width, rect.height);
194 c->fullscreen = true;
195 xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
196 xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
197 window_raise(c->window);
198 window_border_width(c->window, 0);
199 window_move_resize(c->window, 0, 0, screen_width, screen_height);
203 void toggle_floating(node_t *n)
205 if (n == NULL || n->client->transient)
208 PUTS("toggle floating");
210 client_t *c = n->client;
211 c->floating = !c->floating;
212 n->vacant = !n->vacant;
213 update_vacant_state(n->parent);
215 window_raise(c->window);
216 else if (is_tiled(c))
217 window_lower(c->window);
221 void toggle_locked(client_t *c)
223 c->locked = !c->locked;
226 void list_windows(char *rsp)
230 desktop_t *d = desk_head;
233 node_t *n = first_extrema(d->root);
235 sprintf(line, "0x%X\n", n->client->window);
243 uint32_t get_main_border_color(client_t *c, bool focused)
250 return active_locked_border_color_pxl;
252 return active_border_color_pxl;
255 return urgent_border_color_pxl;
257 return normal_locked_border_color_pxl;
259 return normal_border_color_pxl;
263 void update_floating_rectangle(client_t *c)
265 xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
268 c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
271 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 320, 240};
275 void window_border_width(xcb_window_t win, uint32_t bw)
277 uint32_t values[] = {bw};
278 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
281 void window_move(xcb_window_t win, int16_t x, int16_t y)
283 uint32_t values[] = {x, y};
284 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
287 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
289 uint32_t values[] = {x, y, w, h};
290 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
293 void window_raise(xcb_window_t win)
295 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
296 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
299 void window_lower(xcb_window_t win)
301 uint32_t values[] = {XCB_STACK_MODE_BELOW};
302 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
305 void window_set_visibility(xcb_window_t win, bool visible) {
306 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
307 uint32_t values_on[] = {ROOT_EVENT_MASK};
308 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_off);
310 xcb_map_window(dpy, win);
312 xcb_unmap_window(dpy, win);
313 xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_on);
316 void window_hide(xcb_window_t win)
318 window_set_visibility(win, false);
321 void window_show(xcb_window_t win)
323 window_set_visibility(win, true);