1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation and/or
11 * other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
40 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
41 uint8_t override_redirect = 0;
44 override_redirect = wa->override_redirect;
48 if (override_redirect || locate_window(win, &loc))
51 rule_consequence_t *csq = make_rule_conquence();
52 handle_rules(win, &m, &d, csq);
58 disable_floating_atom(win);
63 PRINTF("manage %X\n", win);
65 client_t *c = make_client(win);
66 update_floating_rectangle(c);
67 translate_client(monitor_from_client(c), m, c);
70 c->frame = csq->frame;
72 xcb_icccm_get_wm_class_reply_t reply;
73 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
74 snprintf(c->class_name, sizeof(c->class_name), "%s", reply.class_name);
75 xcb_icccm_get_wm_class_reply_wipe(&reply);
78 csq->floating = csq->floating || d->floating;
80 node_t *n = make_node();
83 insert_node(m, d, n, d->focus);
85 disable_floating_atom(c->window);
86 set_floating(n, csq->floating);
87 set_locked(m, d, n, csq->locked);
88 set_sticky(m, d, n, csq->sticky);
89 set_private(m, d, n, csq->private);
91 if (d->focus != NULL && d->focus->client->fullscreen)
92 set_fullscreen(d->focus, false);
94 set_fullscreen(n, csq->fullscreen);
95 c->transient = csq->transient;
99 bool give_focus = (csq->focus && (d == mon->desk || csq->follow));
106 stack(n, STACK_ABOVE);
108 uint32_t values[] = {get_event_mask(n->client)};
109 xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
113 window_show(n->client->window);
115 window_hide(n->client->window);
118 /* the same function is already called in `focus_node` but has no effects on unmapped windows */
120 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
124 ewmh_set_wm_desktop(n, d);
125 ewmh_update_client_list();
128 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
130 if (n == NULL || (n->client->border_width < 1 && !n->client->frame)) {
134 if (n->client->frame) {
135 draw_frame_background(n, focused_window, focused_monitor);
139 xcb_window_t win = n->client->window;
140 uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
142 if (n->split_mode == MODE_AUTOMATIC) {
143 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
145 unsigned int border_width = n->client->border_width;
146 uint32_t presel_border_color_pxl;
147 get_color(presel_border_color, win, &presel_border_color_pxl);
149 xcb_rectangle_t actual_rectangle = get_rectangle(n->client);
151 uint16_t width = actual_rectangle.width;
152 uint16_t height = actual_rectangle.height;
154 uint16_t full_width = width + 2 * border_width;
155 uint16_t full_height = height + 2 * border_width;
157 xcb_rectangle_t border_rectangles[] =
159 { width, 0, 2 * border_width, height + 2 * border_width },
160 { 0, height, width + 2 * border_width, 2 * border_width }
163 xcb_rectangle_t *presel_rectangles;
165 uint8_t win_depth = root_depth;
166 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
168 win_depth = geo->depth;
171 xcb_pixmap_t pixmap = xcb_generate_id(dpy);
172 xcb_create_pixmap(dpy, win_depth, pixmap, win, full_width, full_height);
174 xcb_gcontext_t gc = xcb_generate_id(dpy);
175 xcb_create_gc(dpy, gc, pixmap, 0, NULL);
177 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
178 xcb_poly_fill_rectangle(dpy, pixmap, gc, LENGTH(border_rectangles), border_rectangles);
180 uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
181 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
182 switch (n->split_dir) {
184 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
185 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
188 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
189 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
192 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
193 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
196 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
197 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
200 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
201 xcb_poly_fill_rectangle(dpy, pixmap, gc, 2, presel_rectangles);
202 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pixmap);
203 free(presel_rectangles);
204 xcb_free_gc(dpy, gc);
205 xcb_free_pixmap(dpy, pixmap);
209 void draw_frame_background(node_t *n, bool focused_window, bool focused_monitor)
214 xcb_window_t win = n->client->window;
215 uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
216 uint32_t opacity = (focused_window ? (focused_monitor ? focused_frame_opacity : active_frame_opacity) : normal_frame_opacity) * 0xffffffff;
217 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 32, 1, &opacity);
218 uint8_t win_depth = root_depth;
219 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
221 win_depth = geo->depth;
223 xcb_rectangle_t rectangle = get_rectangle(n->client);
224 rectangle.x = rectangle.y = 0;
225 uint16_t width = rectangle.width;
226 uint16_t height = rectangle.height;
227 xcb_pixmap_t pixmap = xcb_generate_id(dpy);
228 xcb_create_pixmap(dpy, win_depth, pixmap, win, width, height);
229 xcb_gcontext_t gc = xcb_generate_id(dpy);
230 xcb_create_gc(dpy, gc, pixmap, 0, NULL);
231 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
232 xcb_poly_fill_rectangle(dpy, pixmap, gc, 1, &rectangle);
233 if (n->split_mode == MODE_MANUAL) {
234 xcb_rectangle_t presel_rectangle = rectangle;
235 uint32_t presel_border_color_pxl;
236 get_color(presel_border_color, win, &presel_border_color_pxl);
237 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
238 switch (n->split_dir) {
240 presel_rectangle.height = n->split_ratio * rectangle.height;
243 presel_rectangle.width = (1 - n->split_ratio) * rectangle.width;
244 presel_rectangle.x = rectangle.width - presel_rectangle.width;
247 presel_rectangle.height = (1 - n->split_ratio) * rectangle.height;
248 presel_rectangle.y = rectangle.height - presel_rectangle.height;
251 presel_rectangle.width = n->split_ratio * rectangle.width;
254 xcb_poly_fill_rectangle(dpy, pixmap, gc, 1, &presel_rectangle);
256 xcb_copy_area(dpy, pixmap, win, gc, 0, 0, 0, 0, width, height);
257 xcb_free_gc(dpy, gc);
258 xcb_free_pixmap(dpy, pixmap);
261 pointer_state_t *make_pointer_state(void)
263 pointer_state_t *p = malloc(sizeof(pointer_state_t));
266 p->node = p->vertical_fence = p->horizontal_fence = NULL;
268 p->window = XCB_NONE;
269 p->action = ACTION_NONE;
273 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
275 return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width)
276 && a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
279 xcb_rectangle_t get_rectangle(client_t *c)
282 return c->tiled_rectangle;
284 return c->floating_rectangle;
287 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
289 xcb_rectangle_t rect = get_rectangle(c);
292 pt->x = rect.x + rect.width;
293 pt->y = rect.y + (rect.height / 2);
296 pt->x = rect.x + (rect.width / 2);
297 pt->y = rect.y + rect.height;
301 pt->y = rect.y + (rect.height / 2);
304 pt->x = rect.x + (rect.width / 2);
310 void adopt_orphans(void)
312 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
316 PUTS("adopt orphans");
318 int len = xcb_query_tree_children_length(qtr);
319 xcb_window_t *wins = xcb_query_tree_children(qtr);
320 for (int i = 0; i < len; i++) {
322 xcb_window_t win = wins[i];
323 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
325 if (ewmh_locate_desktop(idx, &loc))
326 manage_window(loc.monitor, loc.desktop, win);
328 manage_window(mon, mon->desk, win);
334 void window_close(node_t *n)
336 if (n == NULL || n->client->locked)
339 PRINTF("close window %X\n", n->client->window);
341 send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
344 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
349 xcb_window_t win = n->client->window;
350 PRINTF("kill window %X\n", win);
352 xcb_kill_client(dpy, win);
353 remove_node(m, d, n);
356 void set_fullscreen(node_t *n, bool value)
358 if (n == NULL || n->client->frame || n->client->fullscreen == value)
361 client_t *c = n->client;
363 PRINTF("fullscreen %X: %s\n", c->window, BOOLSTR(value));
365 c->fullscreen = value;
367 ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_FULLSCREEN);
369 ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_FULLSCREEN);
370 stack(n, STACK_ABOVE);
373 void set_floating(node_t *n, bool value)
375 if (n == NULL || n->client->transient || n->client->fullscreen || n->client->frame || n->client->floating == value)
378 PRINTF("floating %X: %s\n", n->client->window, BOOLSTR(value));
380 n->split_mode = MODE_AUTOMATIC;
381 client_t *c = n->client;
382 c->floating = n->vacant = value;
383 update_vacant_state(n->parent);
386 enable_floating_atom(c->window);
389 disable_floating_atom(c->window);
393 stack(n, STACK_ABOVE);
396 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
398 if (n == NULL || n->client->locked == value)
401 client_t *c = n->client;
403 PRINTF("set locked %X: %s\n", c->window, BOOLSTR(value));
406 window_draw_border(n, d->focus == n, m == mon);
409 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
411 if (n == NULL || n->client->sticky == value)
414 client_t *c = n->client;
416 PRINTF("set sticky %X: %s\n", c->window, BOOLSTR(value));
419 transfer_node(m, d, n, m, m->desk, m->desk->focus);
423 ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_STICKY);
426 ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_STICKY);
430 window_draw_border(n, d->focus == n, m == mon);
433 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
435 if (n == NULL || n->client->private == value)
438 client_t *c = n->client;
440 PRINTF("set private %X: %s\n", c->window, BOOLSTR(value));
443 update_privacy_level(n, value);
444 window_draw_border(n, d->focus == n, m == mon);
447 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
449 if (value && mon->desk->focus == n)
451 n->client->urgent = value;
452 window_draw_border(n, d->focus == n, m == mon);
456 void set_floating_atom(xcb_window_t win, uint32_t value)
458 if (!apply_floating_atom)
460 set_atom(win, _BSPWM_FLOATING_WINDOW, value);
463 void enable_floating_atom(xcb_window_t win)
465 set_floating_atom(win, 1);
468 void disable_floating_atom(xcb_window_t win)
470 set_floating_atom(win, 0);
473 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
480 if (focused_monitor && focused_window) {
482 get_color(focused_locked_border_color, c->window, &pxl);
484 get_color(focused_sticky_border_color, c->window, &pxl);
486 get_color(focused_private_border_color, c->window, &pxl);
488 get_color(focused_border_color, c->window, &pxl);
489 } else if (focused_window) {
491 get_color(urgent_border_color, c->window, &pxl);
493 get_color(active_locked_border_color, c->window, &pxl);
495 get_color(active_sticky_border_color, c->window, &pxl);
497 get_color(active_private_border_color, c->window, &pxl);
499 get_color(active_border_color, c->window, &pxl);
502 get_color(urgent_border_color, c->window, &pxl);
504 get_color(normal_locked_border_color, c->window, &pxl);
506 get_color(normal_sticky_border_color, c->window, &pxl);
508 get_color(normal_private_border_color, c->window, &pxl);
510 get_color(normal_border_color, c->window, &pxl);
516 void update_floating_rectangle(client_t *c)
518 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
521 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
523 c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
529 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
531 window_lower(motion_recorder);
533 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
539 *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
543 window_raise(motion_recorder);
546 bool window_focus(xcb_window_t win)
549 if (locate_window(win, &loc)) {
550 if (loc.node != mon->desk->focus)
551 focus_node(loc.monitor, loc.desktop, loc.node);
557 void window_border_width(xcb_window_t win, uint32_t bw)
559 uint32_t values[] = {bw};
560 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
563 void window_move(xcb_window_t win, int16_t x, int16_t y)
565 uint32_t values[] = {x, y};
566 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
569 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
571 uint32_t values[] = {w, h};
572 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
575 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
577 uint32_t values[] = {x, y, w, h};
578 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
581 void window_raise(xcb_window_t win)
583 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
584 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
587 void window_center(monitor_t *m, client_t *c)
589 xcb_rectangle_t *r = &c->floating_rectangle;
590 xcb_rectangle_t a = m->rectangle;
591 if (r->width >= a.width)
594 r->x = a.x + (a.width - r->width) / 2;
595 if (r->height >= a.height)
598 r->y = a.y + (a.height - r->height) / 2;
601 void window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
605 uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
606 uint32_t values[] = {w2, mode};
607 xcb_configure_window(dpy, w1, mask, values);
610 void window_above(xcb_window_t w1, xcb_window_t w2)
612 window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
615 void window_below(xcb_window_t w1, xcb_window_t w2)
617 window_stack(w1, w2, XCB_STACK_MODE_BELOW);
620 void window_lower(xcb_window_t win)
622 uint32_t values[] = {XCB_STACK_MODE_BELOW};
623 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
626 void window_set_visibility(xcb_window_t win, bool visible)
628 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
629 uint32_t values_on[] = {ROOT_EVENT_MASK};
630 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
632 xcb_map_window(dpy, win);
634 xcb_unmap_window(dpy, win);
635 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
638 void window_hide(xcb_window_t win)
640 PRINTF("window hide %X\n", win);
641 window_set_visibility(win, false);
644 void window_show(xcb_window_t win)
646 PRINTF("window show %X\n", win);
647 window_set_visibility(win, true);
650 void toggle_visibility(void)
655 for (monitor_t *m = mon_head; m != NULL; m = m->next)
656 for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
657 window_set_visibility(n->client->window, visible);
659 update_input_focus();
662 void enable_motion_recorder(void)
664 PUTS("enable motion recorder");
665 window_raise(motion_recorder);
666 window_show(motion_recorder);
669 void disable_motion_recorder(void)
671 PUTS("disable motion recorder");
672 window_hide(motion_recorder);
675 void update_motion_recorder(void)
677 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
680 window_resize(motion_recorder, geo->width, geo->height);
681 PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
687 void update_input_focus(void)
689 set_input_focus(mon->desk->focus);
692 void set_input_focus(node_t *n)
697 if (n->client->icccm_focus)
698 send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
699 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
703 void clear_input_focus(void)
705 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
708 void center_pointer(monitor_t *m)
710 int16_t cx = m->rectangle.x + m->rectangle.width / 2;
711 int16_t cy = m->rectangle.y + m->rectangle.height / 2;
712 window_lower(motion_recorder);
713 xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
714 window_raise(motion_recorder);
717 void get_atom(char *name, xcb_atom_t *atom)
719 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
727 void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
729 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
732 bool has_proto(xcb_atom_t atom, xcb_icccm_get_wm_protocols_reply_t *protocols)
734 for (uint32_t i = 0; i < protocols->atoms_len; i++)
735 if (protocols->atoms[i] == atom)
740 void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
742 xcb_client_message_event_t e;
744 e.response_type = XCB_CLIENT_MESSAGE;
749 e.data.data32[0] = value;
750 e.data.data32[1] = XCB_CURRENT_TIME;
752 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
755 uint32_t get_event_mask(client_t *c)
757 return CLIENT_EVENT_MASK | (c->frame ? XCB_EVENT_MASK_EXPOSURE : 0) | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0);