1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 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
20 * ON 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.
41 void schedule_window(xcb_window_t win)
44 uint8_t override_redirect = 0;
45 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
48 override_redirect = wa->override_redirect;
52 if (override_redirect || locate_window(win, &loc)) {
56 /* ignore pending windows */
57 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
63 rule_consequence_t *csq = make_rule_conquence();
64 apply_rules(win, csq);
65 if (!schedule_rules(win, csq)) {
66 manage_window(win, csq, -1);
71 void manage_window(xcb_window_t win, rule_consequence_t *csq, int fd)
74 desktop_t *d = mon->desk;
75 node_t *f = mon->desk->focus;
77 parse_rule_consequence(fd, csq);
86 if (csq->node_desc[0] != '\0') {
87 coordinates_t ref = {m, d, f};
88 coordinates_t trg = {NULL, NULL, NULL};
89 if (node_from_desc(csq->node_desc, &ref, &trg) == SELECTOR_OK) {
94 } else if (csq->desktop_desc[0] != '\0') {
95 coordinates_t ref = {m, d, NULL};
96 coordinates_t trg = {NULL, NULL, NULL};
97 if (desktop_from_desc(csq->desktop_desc, &ref, &trg) == SELECTOR_OK) {
100 f = trg.desktop->focus;
102 } else if (csq->monitor_desc[0] != '\0') {
103 coordinates_t ref = {m, NULL, NULL};
104 coordinates_t trg = {NULL, NULL, NULL};
105 if (monitor_from_desc(csq->monitor_desc, &ref, &trg) == SELECTOR_OK) {
107 d = trg.monitor->desk;
108 f = trg.monitor->desk->focus;
115 f = mon->desk->focus;
118 if (csq->split_dir[0] != '\0' && f != NULL) {
120 if (parse_direction(csq->split_dir, &dir)) {
121 presel_dir(m, d, f, dir);
125 if (csq->split_ratio != 0 && f != NULL) {
126 presel_ratio(m, d, f, csq->split_ratio);
129 node_t *n = make_node(win);
130 client_t *c = make_client();
131 c->border_width = csq->border ? d->border_width : 0;
133 initialize_client(n);
134 update_floating_rectangle(n);
136 if (c->floating_rectangle.x == 0 && c->floating_rectangle.y == 0) {
140 c->min_width = csq->min_width;
141 c->max_width = csq->max_width;
142 c->min_height = csq->min_height;
143 c->max_height = csq->max_height;
145 monitor_t *mm = monitor_from_client(c);
146 embrace_client(mm, c);
147 adapt_geometry(&mm->rectangle, &m->rectangle, n);
153 snprintf(c->class_name, sizeof(c->class_name), "%s", csq->class_name);
154 snprintf(c->instance_name, sizeof(c->instance_name), "%s", csq->instance_name);
156 f = insert_node(m, d, n, f);
159 put_status(SBSC_MASK_NODE_MANAGE, "node_manage 0x%X 0x%X 0x%X 0x%X\n", m->id, d->id, win, f!=NULL?f->id:0);
161 if (f != NULL && f->client != NULL && csq->state != NULL && *(csq->state) == STATE_FLOATING) {
162 c->last_layer = c->layer = f->client->layer;
165 if (csq->layer != NULL) {
166 c->last_layer = c->layer = *(csq->layer);
169 if (csq->state != NULL) {
170 set_state(m, d, n, *(csq->state));
171 c->last_state = c->state;
174 set_locked(m, d, n, csq->locked);
175 set_sticky(m, d, n, csq->sticky);
176 set_private(m, d, n, csq->private);
180 bool give_focus = (csq->focus && (d == mon->desk || csq->follow));
184 } else if (csq->focus) {
185 activate_node(m, d, n);
190 uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
191 xcb_change_window_attributes(dpy, win, XCB_CW_EVENT_MASK, values);
199 /* the same function is already called in `focus_node` but has no effects on unmapped windows */
201 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
204 ewmh_set_wm_desktop(n, d);
205 ewmh_update_client_list(false);
210 void unmanage_window(xcb_window_t win)
213 if (locate_window(win, &loc)) {
214 put_status(SBSC_MASK_NODE_UNMANAGE, "node_unmanage 0x%X 0x%X 0x%X\n", loc.monitor->id, loc.desktop->id, win);
215 remove_node(loc.monitor, loc.desktop, loc.node);
216 if (frozen_pointer->window == win) {
217 frozen_pointer->action = ACTION_NONE;
219 arrange(loc.monitor, loc.desktop);
221 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
222 if (pr->win == win) {
223 remove_pending_rule(pr);
230 bool is_presel_window(xcb_window_t win)
232 xcb_icccm_get_wm_class_reply_t reply;
234 if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
235 if (streq(BSPWM_CLASS_NAME, reply.class_name) && streq(PRESEL_FEEDBACK_I, reply.instance_name)) {
238 xcb_icccm_get_wm_class_reply_wipe(&reply);
243 void initialize_presel_feedback(node_t *n)
245 if (n == NULL || n->presel == NULL || n->presel->feedback != XCB_NONE) {
249 xcb_window_t win = xcb_generate_id(dpy);
250 uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_SAVE_UNDER;
251 uint32_t values[] = {get_color_pixel(presel_feedback_color), 1};
252 xcb_create_window(dpy, XCB_COPY_FROM_PARENT, win, root, 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
253 XCB_COPY_FROM_PARENT, mask, values);
255 xcb_icccm_set_wm_class(dpy, win, sizeof(PRESEL_FEEDBACK_IC), PRESEL_FEEDBACK_IC);
256 stacking_list_t *s = stack_tail;
257 while (s != NULL && !IS_TILED(s->node->client)) {
261 window_above(win, s->node->id);
263 n->presel->feedback = win;
266 void draw_presel_feedback(monitor_t *m, desktop_t *d, node_t *n)
268 if (n == NULL || n->presel == NULL) {
272 bool exists = (n->presel->feedback != XCB_NONE);
274 initialize_presel_feedback(n);
277 int gap = gapless_monocle && (d->layout == LAYOUT_MONOCLE || (single_monocle && tiled_count(d->root) == 1))
281 presel_t *p = n->presel;
282 xcb_rectangle_t rect = n->rectangle;
286 xcb_rectangle_t presel_rect = rect;
288 switch (p->split_dir) {
290 presel_rect.height = p->split_ratio * rect.height;
293 presel_rect.width = (1 - p->split_ratio) * rect.width;
294 presel_rect.x = rect.width - presel_rect.width;
297 presel_rect.height = (1 - p->split_ratio) * rect.height;
298 presel_rect.y = rect.height - presel_rect.height;
301 presel_rect.width = p->split_ratio * rect.width;
305 window_move_resize(p->feedback, n->rectangle.x + presel_rect.x, n->rectangle.y + presel_rect.y,
306 presel_rect.width, presel_rect.height);
308 if (!exists && m->desk == d) {
309 window_show(p->feedback);
313 void refresh_presel_feebacks_in(node_t *n, desktop_t *d, monitor_t *m)
318 if (n->presel != NULL) {
319 draw_presel_feedback(m, d, n);
321 refresh_presel_feebacks_in(n->first_child, d, m);
322 refresh_presel_feebacks_in(n->second_child, d, m);
326 void update_colors(void)
328 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
329 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
330 update_colors_in(d->root, d, m);
335 void update_colors_in(node_t *n, desktop_t *d, monitor_t *m)
340 if (n->presel != NULL) {
341 uint32_t pxl = get_color_pixel(presel_feedback_color);
342 xcb_change_window_attributes(dpy, n->presel->feedback, XCB_CW_BACK_PIXEL, &pxl);
344 /* hack to induce back pixel refresh */
345 window_hide(n->presel->feedback);
346 window_show(n->presel->feedback);
350 draw_border(n, true, (m == mon));
351 } else if (n->client != NULL) {
352 draw_border(n, false, (m == mon));
354 update_colors_in(n->first_child, d, m);
355 update_colors_in(n->second_child, d, m);
360 void draw_border(node_t *n, bool focused_node, bool focused_monitor)
366 uint32_t border_color_pxl = get_border_color(focused_node, focused_monitor);
367 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
368 if (f->client->border_width > 0) {
369 window_draw_border(f->id, border_color_pxl);
374 void window_draw_border(xcb_window_t win, uint32_t border_color_pxl)
376 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
379 pointer_state_t *make_pointer_state(void)
381 pointer_state_t *p = malloc(sizeof(pointer_state_t));
384 p->node = p->vertical_fence = p->horizontal_fence = NULL;
386 p->window = XCB_NONE;
387 p->action = ACTION_NONE;
391 void adopt_orphans(void)
393 xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
398 int len = xcb_query_tree_children_length(qtr);
399 xcb_window_t *wins = xcb_query_tree_children(qtr);
401 for (int i = 0; i < len; i++) {
403 xcb_window_t win = wins[i];
404 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
405 schedule_window(win);
412 void window_close(node_t *n)
416 } else if (n->client != NULL) {
417 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
419 window_close(n->first_child);
420 window_close(n->second_child);
424 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
430 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
431 xcb_kill_client(dpy, f->id);
434 remove_node(m, d, n);
437 uint32_t get_border_color(bool focused_node, bool focused_monitor)
439 if (focused_monitor && focused_node) {
440 return get_color_pixel(focused_border_color);
441 } else if (focused_node) {
442 return get_color_pixel(active_border_color);
444 return get_color_pixel(normal_border_color);
448 void update_floating_rectangle(node_t *n)
450 client_t *c = n->client;
452 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
455 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
461 void restrain_floating_width(client_t *c, int *width)
466 if (c->min_width > 0 && *width < c->min_width) {
467 *width = c->min_width;
468 } else if (c->max_width > 0 && *width > c->max_width) {
469 *width = c->max_width;
473 void restrain_floating_height(client_t *c, int *height)
478 if (c->min_height > 0 && *height < c->min_height) {
479 *height = c->min_height;
480 } else if (c->max_height > 0 && *height > c->max_height) {
481 *height = c->max_height;
485 void restrain_floating_size(client_t *c, int *width, int *height)
487 restrain_floating_width(c, width);
488 restrain_floating_height(c, height);
491 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
493 window_lower(motion_recorder);
495 xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
500 xcb_point_t pt = {qpr->root_x, qpr->root_y};
501 for (stacking_list_t *s = stack_tail; s != NULL; s = s->prev) {
502 if (!s->node->client->visible) {
505 xcb_rectangle_t rect = get_rectangle(NULL, s->node);
506 if (is_inside(pt, rect)) {
507 if (s->node->id == qpr->child || is_presel_window(qpr->child)) {
515 *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
521 window_raise(motion_recorder);
524 void window_border_width(xcb_window_t win, uint32_t bw)
526 uint32_t values[] = {bw};
527 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
530 void window_move(xcb_window_t win, int16_t x, int16_t y)
532 uint32_t values[] = {x, y};
533 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
536 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
538 uint32_t values[] = {w, h};
539 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
542 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
544 uint32_t values[] = {x, y, w, h};
545 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
548 void window_raise(xcb_window_t win)
550 uint32_t values[] = {XCB_STACK_MODE_ABOVE};
551 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
554 void window_center(monitor_t *m, client_t *c)
556 xcb_rectangle_t *r = &c->floating_rectangle;
557 xcb_rectangle_t a = m->rectangle;
558 if (r->width >= a.width) {
561 r->x = a.x + (a.width - r->width) / 2;
563 if (r->height >= a.height) {
566 r->y = a.y + (a.height - r->height) / 2;
568 r->x -= c->border_width;
569 r->y -= c->border_width;
572 void window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
574 if (w2 == XCB_NONE) {
577 uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
578 uint32_t values[] = {w2, mode};
579 xcb_configure_window(dpy, w1, mask, values);
582 void window_above(xcb_window_t w1, xcb_window_t w2)
584 window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
587 void window_below(xcb_window_t w1, xcb_window_t w2)
589 window_stack(w1, w2, XCB_STACK_MODE_BELOW);
592 void window_lower(xcb_window_t win)
594 uint32_t values[] = {XCB_STACK_MODE_BELOW};
595 xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
598 void window_set_visibility(xcb_window_t win, bool visible)
600 uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
601 uint32_t values_on[] = {ROOT_EVENT_MASK};
602 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
604 xcb_map_window(dpy, win);
606 xcb_unmap_window(dpy, win);
608 xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
611 void window_hide(xcb_window_t win)
613 window_set_visibility(win, false);
616 void window_show(xcb_window_t win)
618 window_set_visibility(win, true);
621 void enable_motion_recorder(void)
623 window_raise(motion_recorder);
624 window_show(motion_recorder);
627 void disable_motion_recorder(void)
629 window_hide(motion_recorder);
632 void update_motion_recorder(void)
634 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
637 window_resize(motion_recorder, geo->width, geo->height);
643 void update_input_focus(void)
645 set_input_focus(mon->desk->focus);
648 void set_input_focus(node_t *n)
650 if (n == NULL || n->client == NULL) {
653 if (n->client->icccm_input) {
654 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_PARENT, n->id, XCB_CURRENT_TIME);
655 } else if (n->client->icccm_focus) {
656 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
661 void clear_input_focus(void)
663 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
666 void center_pointer(xcb_rectangle_t r)
668 int16_t cx = r.x + r.width / 2;
669 int16_t cy = r.y + r.height / 2;
670 window_lower(motion_recorder);
671 xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
672 window_raise(motion_recorder);
675 void get_atom(char *name, xcb_atom_t *atom)
677 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
686 void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
688 xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
691 bool has_proto(xcb_atom_t atom, xcb_icccm_get_wm_protocols_reply_t *protocols)
693 for (uint32_t i = 0; i < protocols->atoms_len; i++) {
694 if (protocols->atoms[i] == atom) {
701 void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
703 xcb_client_message_event_t e;
705 e.response_type = XCB_CLIENT_MESSAGE;
710 e.data.data32[0] = value;
711 e.data.data32[1] = XCB_CURRENT_TIME;
713 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);