Debian Codegraph @

src/compton.h

raw
1 /**
2 * compton.h
3 */
4
5 // Throw everything in here.
6
7
8 // === Includes ===
9
10 #include "common.h"
11
12 #include <math.h>
13 #include <sys/select.h>
14 #include <limits.h>
15 #include <unistd.h>
16 #include <getopt.h>
17 #include <locale.h>
18 #include <signal.h>
19
20 #ifdef CONFIG_VSYNC_DRM
21 #include <fcntl.h>
22 // We references some definitions in drm.h, which could also be found in
23 // /usr/src/linux/include/drm/drm.h, but that path is probably even less
24 // reliable than libdrm
25 #include <drm.h>
26 #include <sys/ioctl.h>
27 #include <errno.h>
28 #endif
29
30 // == Functions ==
31
32 // inline functions must be made static to compile correctly under clang:
33 // http://clang.llvm.org/compatibility.html#inline
34
35 // Helper functions
36
37 static void
38 discard_ignore(session_t *ps, unsigned long sequence);
39
40 static void
41 set_ignore(session_t *ps, unsigned long sequence);
42
43 /**
44 * Ignore X errors caused by next X request.
45 */
46 static inline void
47 set_ignore_next(session_t *ps) {
48 set_ignore(ps, NextRequest(ps->dpy));
49 }
50
51 static int
52 should_ignore(session_t *ps, unsigned long sequence);
53
54 /**
55 * Reset filter on a <code>Picture</code>.
56 */
57 static inline void
58 xrfilter_reset(session_t *ps, Picture p) {
59 XRenderSetPictureFilter(ps->dpy, p, "Nearest", NULL, 0);
60 }
61
62 /**
63 * Subtract two unsigned long values.
64 *
65 * Truncate to 0 if the result is negative.
66 */
67 static inline unsigned long __attribute__((const))
68 sub_unslong(unsigned long a, unsigned long b) {
69 return (a > b) ? a - b : 0;
70 }
71
72 /**
73 * Set a <code>bool</code> array of all wintypes to true.
74 */
75 static inline void
76 wintype_arr_enable(bool arr[]) {
77 wintype_t i;
78
79 for (i = 0; i < NUM_WINTYPES; ++i) {
80 arr[i] = true;
81 }
82 }
83
84 /**
85 * Set a <code>switch_t</code> array of all unset wintypes to true.
86 */
87 static inline void
88 wintype_arr_enable_unset(switch_t arr[]) {
89 wintype_t i;
90
91 for (i = 0; i < NUM_WINTYPES; ++i)
92 if (UNSET == arr[i])
93 arr[i] = ON;
94 }
95
96 /**
97 * Check if a window ID exists in an array of window IDs.
98 *
99 * @param arr the array of window IDs
100 * @param count amount of elements in the array
101 * @param wid window ID to search for
102 */
103 static inline bool
104 array_wid_exists(const Window *arr, int count, Window wid) {
105 while (count--) {
106 if (arr[count] == wid) {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114 /**
115 * Convert a geometry_t value to XRectangle.
116 */
117 static inline XRectangle
118 geom_to_rect(session_t *ps, const geometry_t *src, const XRectangle *def) {
119 XRectangle rect_def = { .x = 0, .y = 0,
120 .width = ps->root_width, .height = ps->root_height };
121 if (!def) def = &rect_def;
122
123 XRectangle rect = { .x = src->x, .y = src->y,
124 .width = src->wid, .height = src->hei };
125 if (src->wid < 0) rect.width = def->width;
126 if (src->hei < 0) rect.height = def->height;
127 if (-1 == src->x) rect.x = def->x;
128 else if (src->x < 0) rect.x = ps->root_width + rect.x + 2 - rect.width;
129 if (-1 == src->y) rect.y = def->y;
130 else if (src->y < 0) rect.y = ps->root_height + rect.y + 2 - rect.height;
131 return rect;
132 }
133
134 /**
135 * Convert a XRectangle to a XServerRegion.
136 */
137 static inline XserverRegion
138 rect_to_reg(session_t *ps, const XRectangle *src) {
139 if (!src) return None;
140 XRectangle bound = { .x = 0, .y = 0,
141 .width = ps->root_width, .height = ps->root_height };
142 XRectangle res = { };
143 rect_crop(&res, src, &bound);
144 if (res.width && res.height)
145 return XFixesCreateRegion(ps->dpy, &res, 1);
146 return None;
147 }
148
149 /**
150 * Destroy a <code>Picture</code>.
151 */
152 inline static void
153 free_picture(session_t *ps, Picture *p) {
154 if (*p) {
155 XRenderFreePicture(ps->dpy, *p);
156 *p = None;
157 }
158 }
159
160 /**
161 * Destroy a <code>Pixmap</code>.
162 */
163 inline static void
164 free_pixmap(session_t *ps, Pixmap *p) {
165 if (*p) {
166 XFreePixmap(ps->dpy, *p);
167 *p = None;
168 }
169 }
170
171 /**
172 * Destroy a <code>Damage</code>.
173 */
174 inline static void
175 free_damage(session_t *ps, Damage *p) {
176 if (*p) {
177 // BadDamage will be thrown if the window is destroyed
178 set_ignore_next(ps);
179 XDamageDestroy(ps->dpy, *p);
180 *p = None;
181 }
182 }
183
184 /**
185 * Destroy a condition list.
186 */
187 static inline void
188 free_wincondlst(c2_lptr_t **pcondlst) {
189 #ifdef CONFIG_C2
190 while ((*pcondlst = c2_free_lptr(*pcondlst)))
191 continue;
192 #endif
193 }
194
195 /**
196 * Free Xinerama screen info.
197 */
198 static inline void
199 free_xinerama_info(session_t *ps) {
200 #ifdef CONFIG_XINERAMA
201 if (ps->xinerama_scr_regs) {
202 for (int i = 0; i < ps->xinerama_nscrs; ++i)
203 free_region(ps, &ps->xinerama_scr_regs[i]);
204 free(ps->xinerama_scr_regs);
205 }
206 cxfree(ps->xinerama_scrs);
207 ps->xinerama_scrs = NULL;
208 ps->xinerama_nscrs = 0;
209 #endif
210 }
211
212 /**
213 * Check whether a paint_t contains enough data.
214 */
215 static inline bool
216 paint_isvalid(session_t *ps, const paint_t *ppaint) {
217 // Don't check for presence of Pixmap here, because older X Composite doesn't
218 // provide it
219 if (!ppaint)
220 return false;
221
222 if (bkend_use_xrender(ps) && !ppaint->pict)
223 return false;
224
225 #ifdef CONFIG_VSYNC_OPENGL
226 if (BKEND_GLX == ps->o.backend && !glx_tex_binded(ppaint->ptex, None))
227 return false;
228 #endif
229
230 return true;
231 }
232
233 /**
234 * Bind texture in paint_t if we are using GLX backend.
235 */
236 static inline bool
237 paint_bind_tex_real(session_t *ps, paint_t *ppaint,
238 unsigned wid, unsigned hei, unsigned depth, bool force) {
239 #ifdef CONFIG_VSYNC_OPENGL
240 if (!ppaint->pixmap)
241 return false;
242
243 if (force || !glx_tex_binded(ppaint->ptex, ppaint->pixmap))
244 return glx_bind_pixmap(ps, &ppaint->ptex, ppaint->pixmap, wid, hei, depth);
245 #endif
246
247 return true;
248 }
249
250 static inline bool
251 paint_bind_tex(session_t *ps, paint_t *ppaint,
252 unsigned wid, unsigned hei, unsigned depth, bool force) {
253 if (BKEND_GLX == ps->o.backend)
254 return paint_bind_tex_real(ps, ppaint, wid, hei, depth, force);
255 return true;
256 }
257
258 /**
259 * Free data in a reg_data_t.
260 */
261 static inline void
262 free_reg_data(reg_data_t *pregd) {
263 cxfree(pregd->rects);
264 pregd->rects = NULL;
265 pregd->nrects = 0;
266 }
267
268 /**
269 * Free paint_t.
270 */
271 static inline void
272 free_paint(session_t *ps, paint_t *ppaint) {
273 free_paint_glx(ps, ppaint);
274 free_picture(ps, &ppaint->pict);
275 free_pixmap(ps, &ppaint->pixmap);
276 }
277
278 /**
279 * Free w->paint.
280 */
281 static inline void
282 free_wpaint(session_t *ps, win *w) {
283 free_paint(ps, &w->paint);
284 free_fence(ps, &w->fence);
285 }
286
287 /**
288 * Destroy all resources in a <code>struct _win</code>.
289 */
290 static inline void
291 free_win_res(session_t *ps, win *w) {
292 free_win_res_glx(ps, w);
293 free_region(ps, &w->extents);
294 free_paint(ps, &w->paint);
295 free_region(ps, &w->border_size);
296 free_paint(ps, &w->shadow_paint);
297 free_damage(ps, &w->damage);
298 free_region(ps, &w->reg_ignore);
299 free(w->name);
300 free(w->class_instance);
301 free(w->class_general);
302 free(w->role);
303 }
304
305 /**
306 * Free root tile related things.
307 */
308 static inline void
309 free_root_tile(session_t *ps) {
310 free_picture(ps, &ps->root_tile_paint.pict);
311 free_texture(ps, &ps->root_tile_paint.ptex);
312 if (ps->root_tile_fill)
313 free_pixmap(ps, &ps->root_tile_paint.pixmap);
314 ps->root_tile_paint.pixmap = None;
315 ps->root_tile_fill = false;
316 }
317
318 /**
319 * Get current system clock in milliseconds.
320 */
321 static inline time_ms_t
322 get_time_ms(void) {
323 struct timeval tv;
324
325 gettimeofday(&tv, NULL);
326
327 return tv.tv_sec % SEC_WRAP * 1000 + tv.tv_usec / 1000;
328 }
329
330 /**
331 * Convert time from milliseconds to struct timeval.
332 */
333 static inline struct timeval
334 ms_to_tv(int timeout) {
335 return (struct timeval) {
336 .tv_sec = timeout / MS_PER_SEC,
337 .tv_usec = timeout % MS_PER_SEC * (US_PER_SEC / MS_PER_SEC)
338 };
339 }
340
341 /**
342 * Whether an event is DamageNotify.
343 */
344 static inline bool
345 isdamagenotify(session_t *ps, const XEvent *ev) {
346 return ps->damage_event + XDamageNotify == ev->type;
347 }
348
349 /**
350 * Create a XTextProperty of a single string.
351 */
352 static inline XTextProperty *
353 make_text_prop(session_t *ps, char *str) {
354 XTextProperty *pprop = ccalloc(1, XTextProperty);
355
356 if (XmbTextListToTextProperty(ps->dpy, &str, 1, XStringStyle, pprop)) {
357 cxfree(pprop->value);
358 free(pprop);
359 pprop = NULL;
360 }
361
362 return pprop;
363 }
364
365
366 /**
367 * Set a single-string text property on a window.
368 */
369 static inline bool
370 wid_set_text_prop(session_t *ps, Window wid, Atom prop_atom, char *str) {
371 XTextProperty *pprop = make_text_prop(ps, str);
372 if (!pprop) {
373 printf_errf("(\"%s\"): Failed to make text property.", str);
374 return false;
375 }
376
377 XSetTextProperty(ps->dpy, wid, pprop, prop_atom);
378 cxfree(pprop->value);
379 cxfree(pprop);
380
381 return true;
382 }
383
384 static void
385 run_fade(session_t *ps, win *w, unsigned steps);
386
387 static void
388 set_fade_callback(session_t *ps, win *w,
389 void (*callback) (session_t *ps, win *w), bool exec_callback);
390
391 /**
392 * Execute fade callback of a window if fading finished.
393 */
394 static inline void
395 check_fade_fin(session_t *ps, win *w) {
396 if (w->fade_callback && w->opacity == w->opacity_tgt) {
397 // Must be the last line as the callback could destroy w!
398 set_fade_callback(ps, w, NULL, true);
399 }
400 }
401
402 static void
403 set_fade_callback(session_t *ps, win *w,
404 void (*callback) (session_t *ps, win *w), bool exec_callback);
405
406 static double
407 gaussian(double r, double x, double y);
408
409 static conv *
410 make_gaussian_map(double r);
411
412 static unsigned char
413 sum_gaussian(conv *map, double opacity,
414 int x, int y, int width, int height);
415
416 static void
417 presum_gaussian(session_t *ps, conv *map);
418
419 static XImage *
420 make_shadow(session_t *ps, double opacity, int width, int height);
421
422 static bool
423 win_build_shadow(session_t *ps, win *w, double opacity);
424
425 static Picture
426 solid_picture(session_t *ps, bool argb, double a,
427 double r, double g, double b);
428
429 /**
430 * Stop listening for events on a particular window.
431 */
432 static inline void
433 win_ev_stop(session_t *ps, win *w) {
434 // Will get BadWindow if the window is destroyed
435 set_ignore_next(ps);
436 XSelectInput(ps->dpy, w->id, 0);
437
438 if (w->client_win) {
439 set_ignore_next(ps);
440 XSelectInput(ps->dpy, w->client_win, 0);
441 }
442
443 if (ps->shape_exists) {
444 set_ignore_next(ps);
445 XShapeSelectInput(ps->dpy, w->id, 0);
446 }
447 }
448
449 /**
450 * Get the children of a window.
451 *
452 * @param ps current session
453 * @param w window to check
454 * @param children [out] an array of child window IDs
455 * @param nchildren [out] number of children
456 * @return 1 if successful, 0 otherwise
457 */
458 static inline bool
459 wid_get_children(session_t *ps, Window w,
460 Window **children, unsigned *nchildren) {
461 Window troot, tparent;
462
463 if (!XQueryTree(ps->dpy, w, &troot, &tparent, children, nchildren)) {
464 *nchildren = 0;
465 return false;
466 }
467
468 return true;
469 }
470
471 /**
472 * Check if a window is bounding-shaped.
473 */
474 static inline bool
475 wid_bounding_shaped(const session_t *ps, Window wid) {
476 if (ps->shape_exists) {
477 Bool bounding_shaped = False, clip_shaped = False;
478 int x_bounding, y_bounding, x_clip, y_clip;
479 unsigned int w_bounding, h_bounding, w_clip, h_clip;
480
481 XShapeQueryExtents(ps->dpy, wid, &bounding_shaped,
482 &x_bounding, &y_bounding, &w_bounding, &h_bounding,
483 &clip_shaped, &x_clip, &y_clip, &w_clip, &h_clip);
484 return bounding_shaped;
485 }
486
487 return false;
488 }
489
490 /**
491 * Determine if a window change affects <code>reg_ignore</code> and set
492 * <code>reg_ignore_expire</code> accordingly.
493 */
494 static inline void
495 update_reg_ignore_expire(session_t *ps, const win *w) {
496 if (w->to_paint && WMODE_SOLID == w->mode)
497 ps->reg_ignore_expire = true;
498 }
499
500 /**
501 * Check whether a window has WM frames.
502 */
503 static inline bool __attribute__((pure))
504 win_has_frame(const win *w) {
505 return w->a.border_width
506 || w->frame_extents.top || w->frame_extents.left
507 || w->frame_extents.right || w->frame_extents.bottom;
508 }
509
510 /**
511 * Calculate the extents of the frame of the given window based on EWMH
512 * _NET_FRAME_EXTENTS and the X window border width.
513 */
514 static inline margin_t __attribute__((pure))
515 win_calc_frame_extents(session_t *ps, const win *w) {
516 margin_t result = w->frame_extents;
517 result.top = max_i(result.top, w->a.border_width);
518 result.left = max_i(result.left, w->a.border_width);
519 result.bottom = max_i(result.bottom, w->a.border_width);
520 result.right = max_i(result.right, w->a.border_width);
521 return result;
522 }
523
524 static inline void
525 wid_set_opacity_prop(session_t *ps, Window wid, opacity_t val) {
526 const unsigned long v = val;
527 XChangeProperty(ps->dpy, wid, ps->atom_opacity, XA_CARDINAL, 32,
528 PropModeReplace, (unsigned char *) &v, 1);
529 }
530
531 static inline void
532 wid_rm_opacity_prop(session_t *ps, Window wid) {
533 XDeleteProperty(ps->dpy, wid, ps->atom_opacity);
534 }
535
536 /**
537 * Dump an drawable's info.
538 */
539 static inline void
540 dump_drawable(session_t *ps, Drawable drawable) {
541 Window rroot = None;
542 int x = 0, y = 0;
543 unsigned width = 0, height = 0, border = 0, depth = 0;
544 if (XGetGeometry(ps->dpy, drawable, &rroot, &x, &y, &width, &height,
545 &border, &depth)) {
546 printf_dbgf("(%#010lx): x = %u, y = %u, wid = %u, hei = %d, b = %u, d = %u\n", drawable, x, y, width, height, border, depth);
547 }
548 else {
549 printf_dbgf("(%#010lx): Failed\n", drawable);
550 }
551 }
552
553 static void
554 win_rounded_corners(session_t *ps, win *w);
555
556 /**
557 * Validate a pixmap.
558 *
559 * Detect whether the pixmap is valid with XGetGeometry. Well, maybe there
560 * are better ways.
561 */
562 static inline bool
563 validate_pixmap(session_t *ps, Pixmap pxmap) {
564 if (!pxmap) return false;
565
566 Window rroot = None;
567 int rx = 0, ry = 0;
568 unsigned rwid = 0, rhei = 0, rborder = 0, rdepth = 0;
569 return XGetGeometry(ps->dpy, pxmap, &rroot, &rx, &ry,
570 &rwid, &rhei, &rborder, &rdepth) && rwid && rhei;
571 }
572
573 /**
574 * Validate pixmap of a window, and destroy pixmap and picture if invalid.
575 */
576 static inline void
577 win_validate_pixmap(session_t *ps, win *w) {
578 // Destroy pixmap and picture, if invalid
579 if (!validate_pixmap(ps, w->paint.pixmap))
580 free_paint(ps, &w->paint);
581 }
582
583 /**
584 * Wrapper of c2_match().
585 */
586 static inline bool
587 win_match(session_t *ps, win *w, c2_lptr_t *condlst, const c2_lptr_t **cache) {
588 #ifdef CONFIG_C2
589 return c2_match(ps, w, condlst, cache);
590 #else
591 return false;
592 #endif
593 }
594
595 static bool
596 condlst_add(session_t *ps, c2_lptr_t **pcondlst, const char *pattern);
597
598 static long
599 determine_evmask(session_t *ps, Window wid, win_evmode_t mode);
600
601 /**
602 * Clear leader cache of all windows.
603 */
604 static void
605 clear_cache_win_leaders(session_t *ps) {
606 for (win *w = ps->list; w; w = w->next)
607 w->cache_leader = None;
608 }
609
610 static win *
611 find_toplevel2(session_t *ps, Window wid);
612
613 /**
614 * Find matched window.
615 */
616 static inline win *
617 find_win_all(session_t *ps, const Window wid) {
618 if (!wid || PointerRoot == wid || wid == ps->root || wid == ps->overlay)
619 return NULL;
620
621 win *w = find_win(ps, wid);
622 if (!w) w = find_toplevel(ps, wid);
623 if (!w) w = find_toplevel2(ps, wid);
624 return w;
625 }
626
627 static Window
628 win_get_leader_raw(session_t *ps, win *w, int recursions);
629
630 /**
631 * Get the leader of a window.
632 *
633 * This function updates w->cache_leader if necessary.
634 */
635 static inline Window
636 win_get_leader(session_t *ps, win *w) {
637 return win_get_leader_raw(ps, w, 0);
638 }
639
640 /**
641 * Return whether a window group is really focused.
642 *
643 * @param leader leader window ID
644 * @return true if the window group is focused, false otherwise
645 */
646 static inline bool
647 group_is_focused(session_t *ps, Window leader) {
648 if (!leader)
649 return false;
650
651 for (win *w = ps->list; w; w = w->next) {
652 if (win_get_leader(ps, w) == leader && !w->destroyed
653 && win_is_focused_real(ps, w))
654 return true;
655 }
656
657 return false;
658 }
659
660 static win *
661 recheck_focus(session_t *ps);
662
663 static bool
664 get_root_tile(session_t *ps);
665
666 static void
667 paint_root(session_t *ps, XserverRegion reg_paint);
668
669 static XserverRegion
670 win_get_region(session_t *ps, win *w, bool use_offset);
671
672 static XserverRegion
673 win_get_region_noframe(session_t *ps, win *w, bool use_offset);
674
675 static XserverRegion
676 win_extents(session_t *ps, win *w);
677
678 static XserverRegion
679 border_size(session_t *ps, win *w, bool use_offset);
680
681 static Window
682 find_client_win(session_t *ps, Window w);
683
684 static void
685 get_frame_extents(session_t *ps, win *w, Window client);
686
687 static win *
688 paint_preprocess(session_t *ps, win *list);
689
690 static void
691 render_(session_t *ps, int x, int y, int dx, int dy, int wid, int hei,
692 double opacity, bool argb, bool neg,
693 Picture pict, glx_texture_t *ptex,
694 XserverRegion reg_paint, const reg_data_t *pcache_reg
695 #ifdef CONFIG_VSYNC_OPENGL_GLSL
696 , const glx_prog_main_t *pprogram
697 #endif
698 );
699
700 #ifdef CONFIG_VSYNC_OPENGL_GLSL
701 #define \
702 render(ps, x, y, dx, dy, wid, hei, opacity, argb, neg, pict, ptex, reg_paint, pcache_reg, pprogram) \
703 render_(ps, x, y, dx, dy, wid, hei, opacity, argb, neg, pict, ptex, reg_paint, pcache_reg, pprogram)
704 #else
705 #define \
706 render(ps, x, y, dx, dy, wid, hei, opacity, argb, neg, pict, ptex, reg_paint, pcache_reg, pprogram) \
707 render_(ps, x, y, dx, dy, wid, hei, opacity, argb, neg, pict, ptex, reg_paint, pcache_reg)
708 #endif
709
710 static inline void
711 win_render(session_t *ps, win *w, int x, int y, int wid, int hei,
712 double opacity, XserverRegion reg_paint, const reg_data_t *pcache_reg,
713 Picture pict) {
714 const int dx = (w ? w->a.x: 0) + x;
715 const int dy = (w ? w->a.y: 0) + y;
716 const bool argb = (w && (WMODE_ARGB == w->mode || ps->o.force_win_blend));
717 const bool neg = (w && w->invert_color);
718
719 render(ps, x, y, dx, dy, wid, hei, opacity, argb, neg,
720 pict, (w ? w->paint.ptex: ps->root_tile_paint.ptex),
721 reg_paint, pcache_reg, (w ? &ps->o.glx_prog_win: NULL));
722 }
723
724 static inline void
725 set_tgt_clip(session_t *ps, XserverRegion reg, const reg_data_t *pcache_reg) {
726 switch (ps->o.backend) {
727 case BKEND_XRENDER:
728 case BKEND_XR_GLX_HYBRID:
729 XFixesSetPictureClipRegion(ps->dpy, ps->tgt_buffer.pict, 0, 0, reg);
730 break;
731 #ifdef CONFIG_VSYNC_OPENGL
732 case BKEND_GLX:
733 glx_set_clip(ps, reg, pcache_reg);
734 break;
735 #endif
736 default:
737 assert(false);
738 }
739 }
740
741 static bool
742 xr_blur_dst(session_t *ps, Picture tgt_buffer,
743 int x, int y, int wid, int hei, XFixed **blur_kerns,
744 XserverRegion reg_clip);
745
746 /**
747 * Normalize a convolution kernel.
748 */
749 static inline void
750 normalize_conv_kern(int wid, int hei, XFixed *kern) {
751 double sum = 0.0;
752 for (int i = 0; i < wid * hei; ++i)
753 sum += XFixedToDouble(kern[i]);
754 double factor = 1.0 / sum;
755 for (int i = 0; i < wid * hei; ++i)
756 kern[i] = XDoubleToFixed(XFixedToDouble(kern[i]) * factor);
757 }
758
759 static void
760 paint_all(session_t *ps, XserverRegion region, XserverRegion region_real, win *t);
761
762 static void
763 add_damage(session_t *ps, XserverRegion damage);
764
765 static void
766 repair_win(session_t *ps, win *w);
767
768 static wintype_t
769 wid_get_prop_wintype(session_t *ps, Window w);
770
771 static void
772 map_win(session_t *ps, Window id);
773
774 static void
775 finish_map_win(session_t *ps, win *w);
776
777 static void
778 finish_unmap_win(session_t *ps, win *w);
779
780 static void
781 unmap_callback(session_t *ps, win *w);
782
783 static void
784 unmap_win(session_t *ps, win *w);
785
786 static bool
787 wid_get_opacity_prop(session_t *ps, Window wid, opacity_t def, opacity_t *out);
788
789 /**
790 * Reread opacity property of a window.
791 */
792 static inline void
793 win_update_opacity_prop(session_t *ps, win *w) {
794 // get frame opacity first
795 w->has_opacity_prop =
796 wid_get_opacity_prop(ps, w->id, OPAQUE, &w->opacity_prop);
797
798 if (w->has_opacity_prop)
799 // opacity found
800 return;
801
802 if (ps->o.detect_client_opacity && w->client_win && w->id == w->client_win)
803 // checking client opacity not allowed
804 return;
805
806 // get client opacity
807 w->has_opacity_prop =
808 wid_get_opacity_prop(ps, w->client_win, OPAQUE, &w->opacity_prop);
809 }
810
811 static double
812 get_opacity_percent(win *w);
813
814 static void
815 win_determine_mode(session_t *ps, win *w);
816
817 static void
818 calc_opacity(session_t *ps, win *w);
819
820 static void
821 calc_dim(session_t *ps, win *w);
822
823 static Window
824 wid_get_prop_window(session_t *ps, Window wid, Atom aprop);
825
826 static void
827 win_update_leader(session_t *ps, win *w);
828
829 static void
830 win_set_leader(session_t *ps, win *w, Window leader);
831
832 static void
833 win_update_focused(session_t *ps, win *w);
834
835 /**
836 * Run win_update_focused() on all windows with the same leader window.
837 *
838 * @param leader leader window ID
839 */
840 static inline void
841 group_update_focused(session_t *ps, Window leader) {
842 if (!leader)
843 return;
844
845 for (win *w = ps->list; w; w = w->next) {
846 if (win_get_leader(ps, w) == leader && !w->destroyed)
847 win_update_focused(ps, w);
848 }
849
850 return;
851 }
852
853 static inline void
854 win_set_focused(session_t *ps, win *w, bool focused);
855
856 static void
857 win_on_focus_change(session_t *ps, win *w);
858
859 static void
860 win_determine_fade(session_t *ps, win *w);
861
862 static void
863 win_update_shape_raw(session_t *ps, win *w);
864
865 static void
866 win_update_shape(session_t *ps, win *w);
867
868 static void
869 win_update_prop_shadow_raw(session_t *ps, win *w);
870
871 static void
872 win_update_prop_shadow(session_t *ps, win *w);
873
874 static void
875 win_set_shadow(session_t *ps, win *w, bool shadow_new);
876
877 static void
878 win_determine_shadow(session_t *ps, win *w);
879
880 static void
881 win_set_invert_color(session_t *ps, win *w, bool invert_color_new);
882
883 static void
884 win_determine_invert_color(session_t *ps, win *w);
885
886 static void
887 win_set_blur_background(session_t *ps, win *w, bool blur_background_new);
888
889 static void
890 win_determine_blur_background(session_t *ps, win *w);
891
892 static void
893 win_on_wtype_change(session_t *ps, win *w);
894
895 static void
896 win_on_factor_change(session_t *ps, win *w);
897
898 static void
899 calc_win_size(session_t *ps, win *w);
900
901 static void
902 calc_shadow_geometry(session_t *ps, win *w);
903
904 static void
905 win_upd_wintype(session_t *ps, win *w);
906
907 static void
908 win_mark_client(session_t *ps, win *w, Window client);
909
910 static void
911 win_unmark_client(session_t *ps, win *w);
912
913 static void
914 win_recheck_client(session_t *ps, win *w);
915
916 static bool
917 add_win(session_t *ps, Window id, Window prev);
918
919 static void
920 restack_win(session_t *ps, win *w, Window new_above);
921
922 static void
923 configure_win(session_t *ps, XConfigureEvent *ce);
924
925 static void
926 circulate_win(session_t *ps, XCirculateEvent *ce);
927
928 static void
929 finish_destroy_win(session_t *ps, win *w);
930
931 static void
932 destroy_callback(session_t *ps, win *w);
933
934 static void
935 destroy_win(session_t *ps, Window id);
936
937 static void
938 damage_win(session_t *ps, XDamageNotifyEvent *de);
939
940 static int
941 xerror(Display *dpy, XErrorEvent *ev);
942
943 static void
944 expose_root(session_t *ps, XRectangle *rects, int nrects);
945
946 static Window
947 wid_get_prop_window(session_t *ps, Window wid, Atom aprop);
948
949 static bool
950 wid_get_name(session_t *ps, Window w, char **name);
951
952 static bool
953 wid_get_role(session_t *ps, Window w, char **role);
954
955 static int
956 win_get_prop_str(session_t *ps, win *w, char **tgt,
957 bool (*func_wid_get_prop_str)(session_t *ps, Window wid, char **tgt));
958
959 static inline int
960 win_get_name(session_t *ps, win *w) {
961 int ret = win_get_prop_str(ps, w, &w->name, wid_get_name);
962
963 #ifdef DEBUG_WINDATA
964 printf_dbgf("(%#010lx): client = %#010lx, name = \"%s\", "
965 "ret = %d\n", w->id, w->client_win, w->name, ret);
966 #endif
967
968 return ret;
969 }
970
971 static inline int
972 win_get_role(session_t *ps, win *w) {
973 int ret = win_get_prop_str(ps, w, &w->role, wid_get_role);
974
975 #ifdef DEBUG_WINDATA
976 printf_dbgf("(%#010lx): client = %#010lx, role = \"%s\", "
977 "ret = %d\n", w->id, w->client_win, w->role, ret);
978 #endif
979
980 return ret;
981 }
982
983 static bool
984 win_get_class(session_t *ps, win *w);
985
986 #ifdef DEBUG_EVENTS
987 static int
988 ev_serial(XEvent *ev);
989
990 static const char *
991 ev_name(session_t *ps, XEvent *ev);
992
993 static Window
994 ev_window(session_t *ps, XEvent *ev);
995 #endif
996
997 static void __attribute__ ((noreturn))
998 usage(int ret);
999
1000 static bool
1001 register_cm(session_t *ps);
1002
1003 inline static void
1004 ev_focus_in(session_t *ps, XFocusChangeEvent *ev);
1005
1006 inline static void
1007 ev_focus_out(session_t *ps, XFocusChangeEvent *ev);
1008
1009 inline static void
1010 ev_create_notify(session_t *ps, XCreateWindowEvent *ev);
1011
1012 inline static void
1013 ev_configure_notify(session_t *ps, XConfigureEvent *ev);
1014
1015 inline static void
1016 ev_destroy_notify(session_t *ps, XDestroyWindowEvent *ev);
1017
1018 inline static void
1019 ev_map_notify(session_t *ps, XMapEvent *ev);
1020
1021 inline static void
1022 ev_unmap_notify(session_t *ps, XUnmapEvent *ev);
1023
1024 inline static void
1025 ev_reparent_notify(session_t *ps, XReparentEvent *ev);
1026
1027 inline static void
1028 ev_circulate_notify(session_t *ps, XCirculateEvent *ev);
1029
1030 inline static void
1031 ev_expose(session_t *ps, XExposeEvent *ev);
1032
1033 static void
1034 update_ewmh_active_win(session_t *ps);
1035
1036 inline static void
1037 ev_property_notify(session_t *ps, XPropertyEvent *ev);
1038
1039 inline static void
1040 ev_damage_notify(session_t *ps, XDamageNotifyEvent *ev);
1041
1042 inline static void
1043 ev_shape_notify(session_t *ps, XShapeEvent *ev);
1044
1045 /**
1046 * Get a region of the screen size.
1047 */
1048 inline static XserverRegion
1049 get_screen_region(session_t *ps) {
1050 XRectangle r;
1051
1052 r.x = 0;
1053 r.y = 0;
1054 r.width = ps->root_width;
1055 r.height = ps->root_height;
1056 return XFixesCreateRegion(ps->dpy, &r, 1);
1057 }
1058
1059 /**
1060 * Resize a region.
1061 */
1062 static inline void
1063 resize_region(session_t *ps, XserverRegion region, short mod) {
1064 if (!mod || !region) return;
1065
1066 int nrects = 0, nnewrects = 0;
1067 XRectangle *newrects = NULL;
1068 XRectangle *rects = XFixesFetchRegion(ps->dpy, region, &nrects);
1069 if (!rects || !nrects)
1070 goto resize_region_end;
1071
1072 // Allocate memory for new rectangle list, because I don't know if it's
1073 // safe to write in the memory Xlib allocates
1074 newrects = calloc(nrects, sizeof(XRectangle));
1075 if (!newrects) {
1076 printf_errf("(): Failed to allocate memory.");
1077 exit(1);
1078 }
1079
1080 // Loop through all rectangles
1081 for (int i = 0; i < nrects; ++i) {
1082 int x1 = max_i(rects[i].x - mod, 0);
1083 int y1 = max_i(rects[i].y - mod, 0);
1084 int x2 = min_i(rects[i].x + rects[i].width + mod, ps->root_width);
1085 int y2 = min_i(rects[i].y + rects[i].height + mod, ps->root_height);
1086 int wid = x2 - x1;
1087 int hei = y2 - y1;
1088 if (wid <= 0 || hei <= 0)
1089 continue;
1090 newrects[nnewrects].x = x1;
1091 newrects[nnewrects].y = y1;
1092 newrects[nnewrects].width = wid;
1093 newrects[nnewrects].height = hei;
1094 ++nnewrects;
1095 }
1096
1097 // Set region
1098 XFixesSetRegion(ps->dpy, region, newrects, nnewrects);
1099
1100 resize_region_end:
1101 cxfree(rects);
1102 free(newrects);
1103 }
1104
1105 /**
1106 * Dump a region.
1107 */
1108 static inline void
1109 dump_region(const session_t *ps, XserverRegion region) {
1110 int nrects = 0;
1111 XRectangle *rects = NULL;
1112 if (!rects && region)
1113 rects = XFixesFetchRegion(ps->dpy, region, &nrects);
1114
1115 printf_dbgf("(%#010lx): %d rects\n", region, nrects);
1116 if (!rects) return;
1117 for (int i = 0; i < nrects; ++i)
1118 printf("Rect #%d: %8d, %8d, %8d, %8d\n", i, rects[i].x, rects[i].y,
1119 rects[i].width, rects[i].height);
1120 putchar('\n');
1121 fflush(stdout);
1122
1123 cxfree(rects);
1124 }
1125
1126 /**
1127 * Check if a region is empty.
1128 *
1129 * Keith Packard said this is slow:
1130 * http://lists.freedesktop.org/archives/xorg/2007-November/030467.html
1131 *
1132 * @param ps current session
1133 * @param region region to check for
1134 * @param pcache_rects a place to cache the dumped rectangles
1135 * @param ncache_nrects a place to cache the number of dumped rectangles
1136 */
1137 static inline bool
1138 is_region_empty(const session_t *ps, XserverRegion region,
1139 reg_data_t *pcache_reg) {
1140 int nrects = 0;
1141 XRectangle *rects = XFixesFetchRegion(ps->dpy, region, &nrects);
1142
1143 if (pcache_reg) {
1144 pcache_reg->rects = rects;
1145 pcache_reg->nrects = nrects;
1146 }
1147 else
1148 cxfree(rects);
1149
1150 return !nrects;
1151 }
1152
1153 /**
1154 * Add a window to damaged area.
1155 *
1156 * @param ps current session
1157 * @param w struct _win element representing the window
1158 */
1159 static inline void
1160 add_damage_win(session_t *ps, win *w) {
1161 if (w->extents) {
1162 add_damage(ps, copy_region(ps, w->extents));
1163 }
1164 }
1165
1166 #if defined(DEBUG_EVENTS) || defined(DEBUG_RESTACK)
1167 static bool
1168 ev_window_name(session_t *ps, Window wid, char **name);
1169 #endif
1170
1171 inline static void
1172 ev_handle(session_t *ps, XEvent *ev);
1173
1174 static bool
1175 fork_after(session_t *ps);
1176
1177 #ifdef CONFIG_LIBCONFIG
1178 /**
1179 * Wrapper of libconfig's <code>config_lookup_int</code>.
1180 *
1181 * To convert <code>int</code> value <code>config_lookup_bool</code>
1182 * returns to <code>bool</code>.
1183 */
1184 static inline void
1185 lcfg_lookup_bool(const config_t *config, const char *path,
1186 bool *value) {
1187 int ival;
1188
1189 if (config_lookup_bool(config, path, &ival))
1190 *value = ival;
1191 }
1192
1193 /**
1194 * Wrapper of libconfig's <code>config_lookup_int</code>.
1195 *
1196 * To deal with the different value types <code>config_lookup_int</code>
1197 * returns in libconfig-1.3 and libconfig-1.4.
1198 */
1199 static inline int
1200 lcfg_lookup_int(const config_t *config, const char *path, int *value) {
1201 #ifndef CONFIG_LIBCONFIG_LEGACY
1202 return config_lookup_int(config, path, value);
1203 #else
1204 long lval;
1205 int ret;
1206
1207 if ((ret = config_lookup_int(config, path, &lval)))
1208 *value = lval;
1209
1210 return ret;
1211 #endif
1212 }
1213
1214 static FILE *
1215 open_config_file(char *cpath, char **path);
1216
1217 static void
1218 parse_cfg_condlst(session_t *ps, const config_t *pcfg, c2_lptr_t **pcondlst,
1219 const char *name);
1220
1221 static void
1222 parse_config(session_t *ps, struct options_tmp *pcfgtmp);
1223 #endif
1224
1225 static void
1226 get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass);
1227
1228 static void
1229 init_atoms(session_t *ps);
1230
1231 static void
1232 update_refresh_rate(session_t *ps);
1233
1234 static bool
1235 swopti_init(session_t *ps);
1236
1237 static void
1238 swopti_handle_timeout(session_t *ps, struct timeval *ptv);
1239
1240 #ifdef CONFIG_VSYNC_OPENGL
1241 /**
1242 * Ensure we have a GLX context.
1243 */
1244 static inline bool
1245 ensure_glx_context(session_t *ps) {
1246 // Create GLX context
1247 if (!glx_has_context(ps))
1248 glx_init(ps, false);
1249
1250 return ps->psglx->context;
1251 }
1252 #endif
1253
1254 static bool
1255 vsync_drm_init(session_t *ps);
1256
1257 #ifdef CONFIG_VSYNC_DRM
1258 static int
1259 vsync_drm_wait(session_t *ps);
1260 #endif
1261
1262 static bool
1263 vsync_opengl_init(session_t *ps);
1264
1265 static bool
1266 vsync_opengl_oml_init(session_t *ps);
1267
1268 static bool
1269 vsync_opengl_swc_init(session_t *ps);
1270
1271 static bool
1272 vsync_opengl_mswc_init(session_t *ps);
1273
1274 #ifdef CONFIG_VSYNC_OPENGL
1275 static int
1276 vsync_opengl_wait(session_t *ps);
1277
1278 static int
1279 vsync_opengl_oml_wait(session_t *ps);
1280
1281 static void
1282 vsync_opengl_swc_deinit(session_t *ps);
1283
1284 static void
1285 vsync_opengl_mswc_deinit(session_t *ps);
1286 #endif
1287
1288 static void
1289 vsync_wait(session_t *ps);
1290
1291 static void
1292 init_alpha_picts(session_t *ps);
1293
1294 static bool
1295 init_dbe(session_t *ps);
1296
1297 static bool
1298 init_overlay(session_t *ps);
1299
1300 static void
1301 redir_start(session_t *ps);
1302
1303 static void
1304 redir_stop(session_t *ps);
1305
1306 static inline time_ms_t
1307 timeout_get_newrun(const timeout_t *ptmout) {
1308 return ptmout->firstrun + (max_l((ptmout->lastrun + (time_ms_t) (ptmout->interval * TIMEOUT_RUN_TOLERANCE) - ptmout->firstrun) / ptmout->interval, (ptmout->lastrun + (time_ms_t) (ptmout->interval * (1 - TIMEOUT_RUN_TOLERANCE)) - ptmout->firstrun) / ptmout->interval) + 1) * ptmout->interval;
1309 }
1310
1311 static time_ms_t
1312 timeout_get_poll_time(session_t *ps);
1313
1314 static void
1315 timeout_clear(session_t *ps);
1316
1317 static bool
1318 tmout_unredir_callback(session_t *ps, timeout_t *tmout);
1319
1320 static bool
1321 mainloop(session_t *ps);
1322
1323 #ifdef CONFIG_XINERAMA
1324 static void
1325 cxinerama_upd_scrs(session_t *ps);
1326 #endif
1327
1328 /**
1329 * Get the Xinerama screen a window is on.
1330 *
1331 * Return an index >= 0, or -1 if not found.
1332 */
1333 static inline void
1334 cxinerama_win_upd_scr(session_t *ps, win *w) {
1335 #ifdef CONFIG_XINERAMA
1336 w->xinerama_scr = -1;
1337 for (XineramaScreenInfo *s = ps->xinerama_scrs;
1338 s < ps->xinerama_scrs + ps->xinerama_nscrs; ++s)
1339 if (s->x_org <= w->a.x && s->y_org <= w->a.y
1340 && s->x_org + s->width >= w->a.x + w->widthb
1341 && s->y_org + s->height >= w->a.y + w->heightb) {
1342 w->xinerama_scr = s - ps->xinerama_scrs;
1343 return;
1344 }
1345 #endif
1346 }
1347
1348 static void
1349 cxinerama_upd_scrs(session_t *ps);
1350
1351 static session_t *
1352 session_init(session_t *ps_old, int argc, char **argv);
1353
1354 static void
1355 session_destroy(session_t *ps);
1356
1357 static void
1358 session_run(session_t *ps);
1359
1360 static void
1361 reset_enable(int __attribute__((unused)) signum);
1362
1363 // vim: set et sw=2 :