]> git.lizzy.rs Git - bspwm.git/blob - window.c
Neutralize obscuring windows in `pseudo_focus`
[bspwm.git] / window.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "bspwm.h"
28 #include "ewmh.h"
29 #include "monitor.h"
30 #include "query.h"
31 #include "rule.h"
32 #include "settings.h"
33 #include "stack.h"
34 #include "tree.h"
35 #include "subscribe.h"
36 #include "messages.h"
37 #include "window.h"
38
39 void schedule_window(xcb_window_t win)
40 {
41         coordinates_t loc;
42         uint8_t override_redirect = 0;
43         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
44
45         if (wa != NULL) {
46                 override_redirect = wa->override_redirect;
47                 free(wa);
48         }
49
50         if (override_redirect || locate_window(win, &loc))
51                 return;
52
53         /* ignore pending windows */
54         for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next)
55                 if (pr->win == win)
56                         return;
57
58         rule_consequence_t *csq = make_rule_conquence();
59         apply_rules(win, csq);
60         if (!schedule_rules(win, csq)) {
61                 manage_window(win, csq, -1);
62                 free(csq);
63         }
64 }
65
66 void manage_window(xcb_window_t win, rule_consequence_t *csq, int fd)
67 {
68         monitor_t *m = mon;
69         desktop_t *d = mon->desk;
70         node_t *f = mon->desk->focus;
71
72         parse_rule_consequence(fd, csq);
73
74         if (!csq->manage) {
75                 free(csq->layer);
76                 free(csq->state);
77                 disable_floating_atom(win);
78                 window_show(win);
79                 return;
80         }
81
82         PRINTF("manage %X\n", win);
83
84         if (csq->node_desc[0] != '\0') {
85                 coordinates_t ref = {m, d, f};
86                 coordinates_t trg = {NULL, NULL, NULL};
87                 if (node_from_desc(csq->node_desc, &ref, &trg)) {
88                         m = trg.monitor;
89                         d = trg.desktop;
90                         f = trg.node;
91                 }
92         } else if (csq->desktop_desc[0] != '\0') {
93                 coordinates_t ref = {m, d, NULL};
94                 coordinates_t trg = {NULL, NULL, NULL};
95                 if (desktop_from_desc(csq->desktop_desc, &ref, &trg)) {
96                         m = trg.monitor;
97                         d = trg.desktop;
98                         f = trg.desktop->focus;
99                 }
100         } else if (csq->monitor_desc[0] != '\0') {
101                 coordinates_t ref = {m, NULL, NULL};
102                 coordinates_t trg = {NULL, NULL, NULL};
103                 if (monitor_from_desc(csq->monitor_desc, &ref, &trg)) {
104                         m = trg.monitor;
105                         d = trg.monitor->desk;
106                         f = trg.monitor->desk->focus;
107                 }
108         }
109
110         if (csq->sticky) {
111                 m = mon;
112                 d = mon->desk;
113                 f = mon->desk->focus;
114         }
115
116         if (csq->split_dir[0] != '\0' && f != NULL) {
117                 direction_t dir;
118                 if (parse_direction(csq->split_dir, &dir)) {
119                         f->split_mode = MODE_MANUAL;
120                         f->split_dir = dir;
121                 }
122         }
123
124         if (csq->split_ratio != 0 && f != NULL) {
125                 f->split_ratio = csq->split_ratio;
126         }
127
128         client_t *c = make_client(win, csq->border ? d->border_width : 0);
129         update_floating_rectangle(c);
130         if (c->floating_rectangle.x == 0 && c->floating_rectangle.y == 0)
131                 csq->center = true;
132         c->min_width = csq->min_width;
133         c->max_width = csq->max_width;
134         c->min_height = csq->min_height;
135         c->max_height = csq->max_height;
136         monitor_t *mm = monitor_from_client(c);
137         embrace_client(mm, c);
138         translate_client(mm, m, c);
139
140         if (csq->center) {
141                 window_center(m, c);
142         }
143
144         snprintf(c->class_name, sizeof(c->class_name), "%s", csq->class_name);
145         snprintf(c->instance_name, sizeof(c->instance_name), "%s", csq->instance_name);
146
147         node_t *n = make_node();
148         n->client = c;
149
150         put_status(SBSC_MASK_WINDOW_MANAGE, "window_manage %s %s 0x%X 0x%X\n", m->name, d->name, f!=NULL?f->client->window:0, win);
151         insert_node(m, d, n, f);
152
153         if (f != NULL && f->client != NULL && csq->state != NULL && *(csq->state) == STATE_FLOATING) {
154                 c->layer = f->client->layer;
155         }
156
157         if (csq->layer != NULL) {
158                 c->layer = *(csq->layer);
159         }
160
161         disable_floating_atom(c->window);
162         set_state(m, d, n, csq->state != NULL ? *(csq->state) : c->state);
163         set_locked(m, d, n, csq->locked);
164         set_sticky(m, d, n, csq->sticky);
165         set_private(m, d, n, csq->private);
166
167         arrange(m, d);
168
169         bool give_focus = (csq->focus && (d == mon->desk || csq->follow));
170
171         if (give_focus) {
172                 focus_node(m, d, n);
173         } else if (csq->focus) {
174                 pseudo_focus(m, d, n);
175         } else {
176                 stack(n, false);
177         }
178
179         uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
180         xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
181
182         if (visible) {
183                 if (d == m->desk)
184                         window_show(n->client->window);
185                 else
186                         window_hide(n->client->window);
187         }
188
189         /* the same function is already called in `focus_node` but has no effects on unmapped windows */
190         if (give_focus) {
191                 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
192         }
193
194         num_clients++;
195         ewmh_set_wm_desktop(n, d);
196         ewmh_update_client_list();
197         free(csq->layer);
198         free(csq->state);
199 }
200
201 void unmanage_window(xcb_window_t win)
202 {
203         coordinates_t loc;
204         if (locate_window(win, &loc)) {
205                 PRINTF("unmanage %X\n", win);
206                 put_status(SBSC_MASK_WINDOW_UNMANAGE, "window_unmanage 0x%X\n", win);
207                 remove_node(loc.monitor, loc.desktop, loc.node);
208                 if (frozen_pointer->window == win)
209                         frozen_pointer->action = ACTION_NONE;
210                 arrange(loc.monitor, loc.desktop);
211         } else {
212                 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
213                         if (pr->win == win) {
214                                 remove_pending_rule(pr);
215                                 return;
216                         }
217                 }
218         }
219 }
220
221 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
222 {
223         if (n == NULL || n->client->border_width < 1) {
224                 return;
225         }
226
227         xcb_window_t win = n->client->window;
228         uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
229
230         if (n->split_mode == MODE_AUTOMATIC) {
231                 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
232         } else {
233                 unsigned int border_width = n->client->border_width;
234                 uint32_t presel_border_color_pxl;
235                 get_color(presel_border_color, win, &presel_border_color_pxl);
236
237                 xcb_rectangle_t actual_rectangle = get_rectangle(NULL, n->client);
238
239                 uint16_t width = actual_rectangle.width;
240                 uint16_t height = actual_rectangle.height;
241
242                 uint16_t full_width = width + 2 * border_width;
243                 uint16_t full_height = height + 2 * border_width;
244
245                 xcb_rectangle_t border_rectangles[] =
246                 {
247                         { width, 0, 2 * border_width, height + 2 * border_width },
248                         { 0, height, width + 2 * border_width, 2 * border_width }
249                 };
250
251                 xcb_rectangle_t *presel_rectangles;
252
253                 uint8_t win_depth = root_depth;
254                 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
255                 if (geo != NULL)
256                         win_depth = geo->depth;
257                 free(geo);
258
259                 xcb_pixmap_t pixmap = xcb_generate_id(dpy);
260                 xcb_create_pixmap(dpy, win_depth, pixmap, win, full_width, full_height);
261
262                 xcb_gcontext_t gc = xcb_generate_id(dpy);
263                 xcb_create_gc(dpy, gc, pixmap, 0, NULL);
264
265                 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
266                 xcb_poly_fill_rectangle(dpy, pixmap, gc, LENGTH(border_rectangles), border_rectangles);
267
268                 uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
269                 presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
270                 switch (n->split_dir) {
271                         case DIR_UP:
272                                 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
273                                 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
274                                 break;
275                         case DIR_DOWN:
276                                 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
277                                 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
278                                 break;
279                         case DIR_LEFT:
280                                 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
281                                 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
282                                 break;
283                         case DIR_RIGHT:
284                                 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
285                                 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
286                                 break;
287                 }
288                 xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
289                 xcb_poly_fill_rectangle(dpy, pixmap, gc, 2, presel_rectangles);
290                 xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pixmap);
291                 free(presel_rectangles);
292                 xcb_free_gc(dpy, gc);
293                 xcb_free_pixmap(dpy, pixmap);
294         }
295 }
296
297 pointer_state_t *make_pointer_state(void)
298 {
299         pointer_state_t *p = malloc(sizeof(pointer_state_t));
300         p->monitor = NULL;
301         p->desktop = NULL;
302         p->node = p->vertical_fence = p->horizontal_fence = NULL;
303         p->client = NULL;
304         p->window = XCB_NONE;
305         p->action = ACTION_NONE;
306         return p;
307 }
308
309 /* Returns true if a contains b */
310 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
311 {
312         return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
313                 a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
314 }
315
316 xcb_rectangle_t get_rectangle(monitor_t *m, client_t *c)
317 {
318         switch (c->state) {
319                 case STATE_TILED:
320                         return c->tiled_rectangle;
321                         break;
322                 case STATE_PSEUDO_TILED:
323                 case STATE_FLOATING:
324                         return c->floating_rectangle;
325                         break;
326                 case STATE_FULLSCREEN:
327                         return m->rectangle;
328                         break;
329          }
330 }
331
332 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
333 {
334         xcb_rectangle_t rect = get_rectangle(NULL, c);
335
336         switch (dir) {
337                 case DIR_RIGHT:
338                         pt->x = rect.x + rect.width;
339                         pt->y = rect.y + (rect.height / 2);
340                         break;
341                 case DIR_DOWN:
342                         pt->x = rect.x + (rect.width / 2);
343                         pt->y = rect.y + rect.height;
344                         break;
345                 case DIR_LEFT:
346                         pt->x = rect.x;
347                         pt->y = rect.y + (rect.height / 2);
348                         break;
349                 case DIR_UP:
350                         pt->x = rect.x + (rect.width / 2);
351                         pt->y = rect.y;
352                         break;
353         }
354 }
355
356 void adopt_orphans(void)
357 {
358         xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
359         if (qtr == NULL)
360                 return;
361
362         PUTS("adopt orphans");
363
364         int len = xcb_query_tree_children_length(qtr);
365         xcb_window_t *wins = xcb_query_tree_children(qtr);
366         for (int i = 0; i < len; i++) {
367                 uint32_t idx;
368                 xcb_window_t win = wins[i];
369                 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1)
370                         schedule_window(win);
371         }
372
373         free(qtr);
374 }
375
376 void window_close(node_t *n)
377 {
378         if (n == NULL || n->client->locked)
379                 return;
380
381         PRINTF("close window %X\n", n->client->window);
382
383         send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
384 }
385
386 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
387 {
388         if (n == NULL)
389                 return;
390
391         xcb_window_t win = n->client->window;
392         PRINTF("kill window %X\n", win);
393
394         xcb_kill_client(dpy, win);
395         remove_node(m, d, n);
396 }
397
398 void set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
399 {
400         if (n == NULL || n->client->layer == l) {
401                 return;
402         }
403
404         client_t *c = n->client;
405
406         c->layer = l;
407
408         put_status(SBSC_MASK_WINDOW_LAYER, "window_layer %s 0x%X\n", LAYERSTR(l), c->window);
409
410         if (d->focus == n) {
411                 neutralize_obscuring_windows(m, d, n);
412         }
413
414         stack(n, (d->focus == n));
415 }
416
417 void set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
418 {
419         if (n == NULL || n->client->state == s) {
420                 return;
421         }
422
423         client_t *c = n->client;
424
425         c->last_state = c->state;
426         c->state = s;
427
428         switch (c->last_state) {
429                 case STATE_TILED:
430                 case STATE_PSEUDO_TILED:
431                         break;
432                 case STATE_FLOATING:
433                         set_floating(m, d, n, false);
434                         break;
435                 case STATE_FULLSCREEN:
436                         set_fullscreen(m, d, n, false);
437                         break;
438         }
439
440         put_status(SBSC_MASK_WINDOW_STATE, "window_state %s off 0x%X\n", STATESTR(c->last_state), c->window);
441
442         switch (c->state) {
443                 case STATE_TILED:
444                 case STATE_PSEUDO_TILED:
445                         break;
446                 case STATE_FLOATING:
447                         set_floating(m, d, n, true);
448                         break;
449                 case STATE_FULLSCREEN:
450                         set_fullscreen(m, d, n, true);
451                         break;
452         }
453
454         put_status(SBSC_MASK_WINDOW_STATE, "window_state %s on 0x%X\n", STATESTR(c->state), c->window);
455 }
456
457 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
458 {
459         if (n == NULL) {
460                 return;
461         }
462
463         client_t *c = n->client;
464
465         n->split_mode = MODE_AUTOMATIC;
466         n->vacant = value;
467         update_vacant_state(n->parent);
468
469         if (value) {
470                 enable_floating_atom(c->window);
471                 unrotate_brother(n);
472         } else {
473                 disable_floating_atom(c->window);
474                 rotate_brother(n);
475                 if (d->focus == n) {
476                         neutralize_obscuring_windows(m, d, n);
477                 }
478         }
479
480         stack(n, (d->focus == n));
481 }
482
483 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
484 {
485         if (n == NULL) {
486                 return;
487         }
488
489         client_t *c = n->client;
490
491         n->split_mode = MODE_AUTOMATIC;
492         n->vacant = value;
493         update_vacant_state(n->parent);
494
495         if (value) {
496                 ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_FULLSCREEN);
497                 c->last_layer = c->layer;
498                 c->layer = LAYER_ABOVE;
499         } else {
500                 ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_FULLSCREEN);
501                 c->layer = c->last_layer;
502                 if (d->focus == n) {
503                         neutralize_obscuring_windows(m, d, n);
504                 }
505         }
506
507         stack(n, (d->focus == n));
508 }
509
510 void neutralize_obscuring_windows(monitor_t *m, desktop_t *d, node_t *n)
511 {
512         bool dirty = false;
513         for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
514                 if (a != n) {
515                         if (IS_FULLSCREEN(a->client) && stack_cmp(n->client, a->client) < 0) {
516                                 set_state(m, d, a, a->client->last_state);
517                                 dirty = true;
518                         }
519                 }
520         }
521         if (dirty) {
522                 arrange(m, d);
523         }
524 }
525
526 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
527 {
528         if (n == NULL || n->client->locked == value)
529                 return;
530
531         client_t *c = n->client;
532
533         put_status(SBSC_MASK_WINDOW_FLAG, "window_flag locked %s 0x%X\n", ONOFFSTR(value), c->window);
534
535         c->locked = value;
536         window_draw_border(n, d->focus == n, m == mon);
537 }
538
539 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
540 {
541         if (n == NULL || n->client->sticky == value)
542                 return;
543
544         client_t *c = n->client;
545
546         put_status(SBSC_MASK_WINDOW_FLAG, "window_flag sticky %s 0x%X\n", ONOFFSTR(value), c->window);
547
548         if (d != m->desk)
549                 transfer_node(m, d, n, m, m->desk, m->desk->focus);
550
551         c->sticky = value;
552         if (value) {
553                 ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_STICKY);
554                 m->num_sticky++;
555         } else {
556                 ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_STICKY);
557                 m->num_sticky--;
558         }
559
560         window_draw_border(n, d->focus == n, m == mon);
561 }
562
563 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
564 {
565         if (n == NULL || n->client->private == value)
566                 return;
567
568         client_t *c = n->client;
569
570         put_status(SBSC_MASK_WINDOW_FLAG, "window_flag private %s 0x%X\n", ONOFFSTR(value), c->window);
571
572         c->private = value;
573         update_privacy_level(n, value);
574         window_draw_border(n, d->focus == n, m == mon);
575 }
576
577 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
578 {
579         if (value && mon->desk->focus == n)
580                 return;
581         n->client->urgent = value;
582         window_draw_border(n, d->focus == n, m == mon);
583
584         put_status(SBSC_MASK_WINDOW_FLAG, "window_flag urgent %s 0x%X\n", ONOFFSTR(value), n->client->window);
585         put_status(SBSC_MASK_REPORT);
586 }
587
588 void set_floating_atom(xcb_window_t win, uint32_t value)
589 {
590         if (!apply_floating_atom)
591                 return;
592         set_atom(win, _BSPWM_FLOATING_WINDOW, value);
593 }
594
595 void enable_floating_atom(xcb_window_t win)
596 {
597         set_floating_atom(win, 1);
598 }
599
600 void disable_floating_atom(xcb_window_t win)
601 {
602         set_floating_atom(win, 0);
603 }
604
605 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
606 {
607         if (c == NULL)
608                 return 0;
609
610         uint32_t pxl = 0;
611
612         if (focused_monitor && focused_window) {
613                 if (c->locked)
614                         get_color(focused_locked_border_color, c->window, &pxl);
615                 else if (c->sticky)
616                         get_color(focused_sticky_border_color, c->window, &pxl);
617                 else if (c->private)
618                         get_color(focused_private_border_color, c->window, &pxl);
619                 else
620                         get_color(focused_border_color, c->window, &pxl);
621         } else if (focused_window) {
622                 if (c->urgent)
623                         get_color(urgent_border_color, c->window, &pxl);
624                 else if (c->locked)
625                         get_color(active_locked_border_color, c->window, &pxl);
626                 else if (c->sticky)
627                         get_color(active_sticky_border_color, c->window, &pxl);
628                 else if (c->private)
629                         get_color(active_private_border_color, c->window, &pxl);
630                 else
631                         get_color(active_border_color, c->window, &pxl);
632         } else {
633                 if (c->urgent)
634                         get_color(urgent_border_color, c->window, &pxl);
635                 else if (c->locked)
636                         get_color(normal_locked_border_color, c->window, &pxl);
637                 else if (c->sticky)
638                         get_color(normal_sticky_border_color, c->window, &pxl);
639                 else if (c->private)
640                         get_color(normal_private_border_color, c->window, &pxl);
641                 else
642                         get_color(normal_border_color, c->window, &pxl);
643         }
644
645         return pxl;
646 }
647
648 void update_floating_rectangle(client_t *c)
649 {
650         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
651
652         if (geo != NULL)
653                 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
654
655         free(geo);
656 }
657
658 void restrain_floating_width(client_t *c, int *width)
659 {
660         if (*width < 1)
661                 *width = 1;
662         if (c->min_width > 0 && *width < c->min_width)
663                 *width = c->min_width;
664         else if (c->max_width > 0 && *width > c->max_width)
665                 *width = c->max_width;
666 }
667
668 void restrain_floating_height(client_t *c, int *height)
669 {
670         if (*height < 1)
671                 *height = 1;
672         if (c->min_height > 0 && *height < c->min_height)
673                 *height = c->min_height;
674         else if (c->max_height > 0 && *height > c->max_height)
675                 *height = c->max_height;
676 }
677
678 void restrain_floating_size(client_t *c, int *width, int *height)
679 {
680         restrain_floating_width(c, width);
681         restrain_floating_height(c, height);
682 }
683
684 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
685 {
686         window_lower(motion_recorder);
687
688         xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
689
690         if (qpr != NULL) {
691                 if (win != NULL)
692                         *win = qpr->child;
693                 if (pt != NULL)
694                         *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
695                 free(qpr);
696         }
697
698         window_raise(motion_recorder);
699 }
700
701 void window_border_width(xcb_window_t win, uint32_t bw)
702 {
703         uint32_t values[] = {bw};
704         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
705 }
706
707 void window_move(xcb_window_t win, int16_t x, int16_t y)
708 {
709         uint32_t values[] = {x, y};
710         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
711 }
712
713 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
714 {
715         uint32_t values[] = {w, h};
716         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
717 }
718
719 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
720 {
721         uint32_t values[] = {x, y, w, h};
722         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
723 }
724
725 void window_raise(xcb_window_t win)
726 {
727         uint32_t values[] = {XCB_STACK_MODE_ABOVE};
728         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
729 }
730
731 void window_center(monitor_t *m, client_t *c)
732 {
733         xcb_rectangle_t *r = &c->floating_rectangle;
734         xcb_rectangle_t a = m->rectangle;
735         if (r->width >= a.width)
736                 r->x = a.x;
737         else
738                 r->x = a.x + (a.width - r->width) / 2;
739         if (r->height >= a.height)
740                 r->y = a.y;
741         else
742                 r->y = a.y + (a.height - r->height) / 2;
743         r->x -= c->border_width;
744         r->y -= c->border_width;
745 }
746
747 void window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
748 {
749         if (w2 == XCB_NONE)
750                 return;
751         uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
752         uint32_t values[] = {w2, mode};
753         xcb_configure_window(dpy, w1, mask, values);
754 }
755
756 void window_above(xcb_window_t w1, xcb_window_t w2)
757 {
758         window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
759 }
760
761 void window_below(xcb_window_t w1, xcb_window_t w2)
762 {
763         window_stack(w1, w2, XCB_STACK_MODE_BELOW);
764 }
765
766 void window_lower(xcb_window_t win)
767 {
768         uint32_t values[] = {XCB_STACK_MODE_BELOW};
769         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
770 }
771
772 void window_set_visibility(xcb_window_t win, bool visible)
773 {
774         uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
775         uint32_t values_on[] = {ROOT_EVENT_MASK};
776         xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
777         if (visible)
778                 xcb_map_window(dpy, win);
779         else
780                 xcb_unmap_window(dpy, win);
781         xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
782 }
783
784 void window_hide(xcb_window_t win)
785 {
786         PRINTF("window hide %X\n", win);
787         window_set_visibility(win, false);
788 }
789
790 void window_show(xcb_window_t win)
791 {
792         PRINTF("window show %X\n", win);
793         window_set_visibility(win, true);
794 }
795
796 void toggle_visibility(void)
797 {
798         visible = !visible;
799         if (!visible)
800                 clear_input_focus();
801         for (monitor_t *m = mon_head; m != NULL; m = m->next)
802                 for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
803                         window_set_visibility(n->client->window, visible);
804         if (visible)
805                 update_input_focus();
806 }
807
808 void enable_motion_recorder(void)
809 {
810         PUTS("motion recorder on");
811         window_raise(motion_recorder);
812         window_show(motion_recorder);
813 }
814
815 void disable_motion_recorder(void)
816 {
817         PUTS("motion recorder off");
818         window_hide(motion_recorder);
819 }
820
821 void update_motion_recorder(void)
822 {
823         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
824
825         if (geo != NULL) {
826                 window_resize(motion_recorder, geo->width, geo->height);
827                 PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
828         }
829
830         free(geo);
831 }
832
833 void update_input_focus(void)
834 {
835         set_input_focus(mon->desk->focus);
836 }
837
838 void set_input_focus(node_t *n)
839 {
840         if (n == NULL) {
841                 clear_input_focus();
842         } else {
843                 if (n->client->icccm_focus)
844                         send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
845                 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
846         }
847 }
848
849 void clear_input_focus(void)
850 {
851         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
852 }
853
854 void center_pointer(xcb_rectangle_t r)
855 {
856         int16_t cx = r.x + r.width / 2;
857         int16_t cy = r.y + r.height / 2;
858         window_lower(motion_recorder);
859         xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
860         window_raise(motion_recorder);
861 }
862
863 void get_atom(char *name, xcb_atom_t *atom)
864 {
865         xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
866         if (reply != NULL)
867                 *atom = reply->atom;
868         else
869                 *atom = XCB_NONE;
870         free(reply);
871 }
872
873 void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
874 {
875         xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
876 }
877
878 bool has_proto(xcb_atom_t atom, xcb_icccm_get_wm_protocols_reply_t *protocols)
879 {
880         for (uint32_t i = 0; i < protocols->atoms_len; i++)
881                 if (protocols->atoms[i] == atom)
882                         return true;
883         return false;
884 }
885
886 void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
887 {
888         xcb_client_message_event_t e;
889
890         e.response_type = XCB_CLIENT_MESSAGE;
891         e.window = win;
892         e.format = 32;
893         e.sequence = 0;
894         e.type = property;
895         e.data.data32[0] = value;
896         e.data.data32[1] = XCB_CURRENT_TIME;
897
898         xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
899 }