forked from chjj/compton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompton.c
7780 lines (6727 loc) · 208 KB
/
compton.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Compton - a compositor for X11
*
* Based on `xcompmgr` - Copyright (c) 2003, Keith Packard
*
* Copyright (c) 2011-2013, Christopher Jeffrey
* See LICENSE for more information.
*
*/
#include "compton.h"
#include <ctype.h>
// === Global constants ===
/// Name strings for window types.
const char * const WINTYPES[NUM_WINTYPES] = {
"unknown",
"desktop",
"dock",
"toolbar",
"menu",
"utility",
"splash",
"dialog",
"normal",
"dropdown_menu",
"popup_menu",
"tooltip",
"notify",
"combo",
"dnd",
};
/// Names of VSync modes.
const char * const VSYNC_STRS[NUM_VSYNC + 1] = {
"none", // VSYNC_NONE
"drm", // VSYNC_DRM
"opengl", // VSYNC_OPENGL
"opengl-oml", // VSYNC_OPENGL_OML
"opengl-swc", // VSYNC_OPENGL_SWC
"opengl-mswc", // VSYNC_OPENGL_MSWC
NULL
};
/// Names of backends.
const char * const BACKEND_STRS[NUM_BKEND + 1] = {
"xrender", // BKEND_XRENDER
"glx", // BKEND_GLX
"xr_glx_hybrid",// BKEND_XR_GLX_HYBRID
NULL
};
/// Function pointers to init VSync modes.
static bool (* const (VSYNC_FUNCS_INIT[NUM_VSYNC]))(session_t *ps) = {
[VSYNC_DRM ] = vsync_drm_init,
[VSYNC_OPENGL ] = vsync_opengl_init,
[VSYNC_OPENGL_OML ] = vsync_opengl_oml_init,
[VSYNC_OPENGL_SWC ] = vsync_opengl_swc_init,
[VSYNC_OPENGL_MSWC ] = vsync_opengl_mswc_init,
};
/// Function pointers to wait for VSync.
static int (* const (VSYNC_FUNCS_WAIT[NUM_VSYNC]))(session_t *ps) = {
#ifdef CONFIG_VSYNC_DRM
[VSYNC_DRM ] = vsync_drm_wait,
#endif
#ifdef CONFIG_VSYNC_OPENGL
[VSYNC_OPENGL ] = vsync_opengl_wait,
[VSYNC_OPENGL_OML ] = vsync_opengl_oml_wait,
#endif
};
/// Function pointers to deinitialize VSync.
static void (* const (VSYNC_FUNCS_DEINIT[NUM_VSYNC]))(session_t *ps) = {
#ifdef CONFIG_VSYNC_OPENGL
[VSYNC_OPENGL_SWC ] = vsync_opengl_swc_deinit,
[VSYNC_OPENGL_MSWC ] = vsync_opengl_mswc_deinit,
#endif
};
/// Names of root window properties that could point to a pixmap of
/// background.
const static char *background_props_str[] = {
"_XROOTPMAP_ID",
"_XSETROOT_ID",
0,
};
// === Global variables ===
/// Pointer to current session, as a global variable. Only used by
/// <code>error()</code> and <code>reset_enable()</code>, which could not
/// have a pointer to current session passed in.
session_t *ps_g = NULL;
// === Fading ===
/**
* Get the time left before next fading point.
*
* In milliseconds.
*/
static int
fade_timeout(session_t *ps) {
int diff = ps->o.fade_delta - get_time_ms() + ps->fade_time;
diff = normalize_i_range(diff, 0, ps->o.fade_delta * 2);
return diff;
}
/**
* Run fading on a window.
*
* @param steps steps of fading
*/
static void
run_fade(session_t *ps, win *w, unsigned steps) {
// If we have reached target opacity, return
if (w->opacity == w->opacity_tgt) {
return;
}
if (!w->fade)
w->opacity = w->opacity_tgt;
else if (steps) {
// Use double below because opacity_t will probably overflow during
// calculations
if (w->opacity < w->opacity_tgt)
w->opacity = normalize_d_range(
(double) w->opacity + (double) ps->o.fade_in_step * steps,
0.0, w->opacity_tgt);
else
w->opacity = normalize_d_range(
(double) w->opacity - (double) ps->o.fade_out_step * steps,
w->opacity_tgt, OPAQUE);
}
if (w->opacity != w->opacity_tgt) {
ps->idling = false;
}
}
/**
* Set fade callback of a window, and possibly execute the previous
* callback.
*
* @param exec_callback whether the previous callback is to be executed
*/
static void
set_fade_callback(session_t *ps, win *w,
void (*callback) (session_t *ps, win *w), bool exec_callback) {
void (*old_callback) (session_t *ps, win *w) = w->fade_callback;
w->fade_callback = callback;
// Must be the last line as the callback could destroy w!
if (exec_callback && old_callback) {
old_callback(ps, w);
// Although currently no callback function affects window state on
// next paint, it could, in the future
ps->idling = false;
}
}
// === Shadows ===
static double __attribute__((const))
gaussian(double r, double x, double y) {
return ((1 / (sqrt(2 * M_PI * r))) *
exp((- (x * x + y * y)) / (2 * r * r)));
}
static conv *
make_gaussian_map(double r) {
conv *c;
int size = ((int) ceil((r * 3)) + 1) & ~1;
int center = size / 2;
int x, y;
double t;
double g;
c = malloc(sizeof(conv) + size * size * sizeof(double));
c->size = size;
c->data = (double *) (c + 1);
t = 0.0;
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
g = gaussian(r, x - center, y - center);
t += g;
c->data[y * size + x] = g;
}
}
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
c->data[y * size + x] /= t;
}
}
return c;
}
/*
* A picture will help
*
* -center 0 width width+center
* -center +-----+-------------------+-----+
* | | | |
* | | | |
* 0 +-----+-------------------+-----+
* | | | |
* | | | |
* | | | |
* height +-----+-------------------+-----+
* | | | |
* height+ | | | |
* center +-----+-------------------+-----+
*/
static unsigned char
sum_gaussian(conv *map, double opacity,
int x, int y, int width, int height) {
int fx, fy;
double *g_data;
double *g_line = map->data;
int g_size = map->size;
int center = g_size / 2;
int fx_start, fx_end;
int fy_start, fy_end;
double v;
/*
* Compute set of filter values which are "in range",
* that's the set with:
* 0 <= x + (fx-center) && x + (fx-center) < width &&
* 0 <= y + (fy-center) && y + (fy-center) < height
*
* 0 <= x + (fx - center) x + fx - center < width
* center - x <= fx fx < width + center - x
*/
fx_start = center - x;
if (fx_start < 0) fx_start = 0;
fx_end = width + center - x;
if (fx_end > g_size) fx_end = g_size;
fy_start = center - y;
if (fy_start < 0) fy_start = 0;
fy_end = height + center - y;
if (fy_end > g_size) fy_end = g_size;
g_line = g_line + fy_start * g_size + fx_start;
v = 0;
for (fy = fy_start; fy < fy_end; fy++) {
g_data = g_line;
g_line += g_size;
for (fx = fx_start; fx < fx_end; fx++) {
v += *g_data++;
}
}
if (v > 1) v = 1;
return ((unsigned char) (v * opacity * 255.0));
}
/* precompute shadow corners and sides
to save time for large windows */
static void
presum_gaussian(session_t *ps, conv *map) {
int center = map->size / 2;
int opacity, x, y;
ps->cgsize = map->size;
if (ps->shadow_corner)
free(ps->shadow_corner);
if (ps->shadow_top)
free(ps->shadow_top);
ps->shadow_corner = malloc((ps->cgsize + 1) * (ps->cgsize + 1) * 26);
ps->shadow_top = malloc((ps->cgsize + 1) * 26);
for (x = 0; x <= ps->cgsize; x++) {
ps->shadow_top[25 * (ps->cgsize + 1) + x] =
sum_gaussian(map, 1, x - center, center, ps->cgsize * 2, ps->cgsize * 2);
for (opacity = 0; opacity < 25; opacity++) {
ps->shadow_top[opacity * (ps->cgsize + 1) + x] =
ps->shadow_top[25 * (ps->cgsize + 1) + x] * opacity / 25;
}
for (y = 0; y <= x; y++) {
ps->shadow_corner[25 * (ps->cgsize + 1) * (ps->cgsize + 1) + y * (ps->cgsize + 1) + x]
= sum_gaussian(map, 1, x - center, y - center, ps->cgsize * 2, ps->cgsize * 2);
ps->shadow_corner[25 * (ps->cgsize + 1) * (ps->cgsize + 1) + x * (ps->cgsize + 1) + y]
= ps->shadow_corner[25 * (ps->cgsize + 1) * (ps->cgsize + 1) + y * (ps->cgsize + 1) + x];
for (opacity = 0; opacity < 25; opacity++) {
ps->shadow_corner[opacity * (ps->cgsize + 1) * (ps->cgsize + 1)
+ y * (ps->cgsize + 1) + x]
= ps->shadow_corner[opacity * (ps->cgsize + 1) * (ps->cgsize + 1)
+ x * (ps->cgsize + 1) + y]
= ps->shadow_corner[25 * (ps->cgsize + 1) * (ps->cgsize + 1)
+ y * (ps->cgsize + 1) + x] * opacity / 25;
}
}
}
}
static XImage *
make_shadow(session_t *ps, double opacity,
int width, int height) {
XImage *ximage;
unsigned char *data;
int ylimit, xlimit;
int swidth = width + ps->cgsize;
int sheight = height + ps->cgsize;
int center = ps->cgsize / 2;
int x, y;
unsigned char d;
int x_diff;
int opacity_int = (int)(opacity * 25);
data = malloc(swidth * sheight * sizeof(unsigned char));
if (!data) return 0;
ximage = XCreateImage(ps->dpy, ps->vis, 8,
ZPixmap, 0, (char *) data, swidth, sheight, 8, swidth * sizeof(char));
if (!ximage) {
free(data);
return 0;
}
/*
* Build the gaussian in sections
*/
/*
* center (fill the complete data array)
*/
// If clear_shadow is enabled and the border & corner shadow (which
// later will be filled) could entirely cover the area of the shadow
// that will be displayed, do not bother filling other pixels. If it
// can't, we must fill the other pixels here.
/* if (!(clear_shadow && ps->o.shadow_offset_x <= 0 && ps->o.shadow_offset_x >= -ps->cgsize
&& ps->o.shadow_offset_y <= 0 && ps->o.shadow_offset_y >= -ps->cgsize)) { */
if (ps->cgsize > 0) {
d = ps->shadow_top[opacity_int * (ps->cgsize + 1) + ps->cgsize];
} else {
d = sum_gaussian(ps->gaussian_map,
opacity, center, center, width, height);
}
memset(data, d, sheight * swidth);
// }
/*
* corners
*/
ylimit = ps->cgsize;
if (ylimit > sheight / 2) ylimit = (sheight + 1) / 2;
xlimit = ps->cgsize;
if (xlimit > swidth / 2) xlimit = (swidth + 1) / 2;
for (y = 0; y < ylimit; y++) {
for (x = 0; x < xlimit; x++) {
if (xlimit == ps->cgsize && ylimit == ps->cgsize) {
d = ps->shadow_corner[opacity_int * (ps->cgsize + 1) * (ps->cgsize + 1)
+ y * (ps->cgsize + 1) + x];
} else {
d = sum_gaussian(ps->gaussian_map,
opacity, x - center, y - center, width, height);
}
data[y * swidth + x] = d;
data[(sheight - y - 1) * swidth + x] = d;
data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
data[y * swidth + (swidth - x - 1)] = d;
}
}
/*
* top/bottom
*/
x_diff = swidth - (ps->cgsize * 2);
if (x_diff > 0 && ylimit > 0) {
for (y = 0; y < ylimit; y++) {
if (ylimit == ps->cgsize) {
d = ps->shadow_top[opacity_int * (ps->cgsize + 1) + y];
} else {
d = sum_gaussian(ps->gaussian_map,
opacity, center, y - center, width, height);
}
memset(&data[y * swidth + ps->cgsize], d, x_diff);
memset(&data[(sheight - y - 1) * swidth + ps->cgsize], d, x_diff);
}
}
/*
* sides
*/
for (x = 0; x < xlimit; x++) {
if (xlimit == ps->cgsize) {
d = ps->shadow_top[opacity_int * (ps->cgsize + 1) + x];
} else {
d = sum_gaussian(ps->gaussian_map,
opacity, x - center, center, width, height);
}
for (y = ps->cgsize; y < sheight - ps->cgsize; y++) {
data[y * swidth + x] = d;
data[y * swidth + (swidth - x - 1)] = d;
}
}
/*
if (clear_shadow) {
// Clear the region in the shadow that the window would cover based
// on shadow_offset_{x,y} user provides
int xstart = normalize_i_range(- (int) ps->o.shadow_offset_x, 0, swidth);
int xrange = normalize_i_range(width - (int) ps->o.shadow_offset_x,
0, swidth) - xstart;
int ystart = normalize_i_range(- (int) ps->o.shadow_offset_y, 0, sheight);
int yend = normalize_i_range(height - (int) ps->o.shadow_offset_y,
0, sheight);
int y;
for (y = ystart; y < yend; y++) {
memset(&data[y * swidth + xstart], 0, xrange);
}
}
*/
return ximage;
}
/**
* Generate shadow <code>Picture</code> for a window.
*/
static bool
win_build_shadow(session_t *ps, win *w, double opacity) {
const int width = w->widthb;
const int height = w->heightb;
XImage *shadow_image = NULL;
Pixmap shadow_pixmap = None, shadow_pixmap_argb = None;
Picture shadow_picture = None, shadow_picture_argb = None;
GC gc = None;
shadow_image = make_shadow(ps, opacity, width, height);
if (!shadow_image)
return None;
shadow_pixmap = XCreatePixmap(ps->dpy, ps->root,
shadow_image->width, shadow_image->height, 8);
shadow_pixmap_argb = XCreatePixmap(ps->dpy, ps->root,
shadow_image->width, shadow_image->height, 32);
if (!shadow_pixmap || !shadow_pixmap_argb)
goto shadow_picture_err;
shadow_picture = XRenderCreatePicture(ps->dpy, shadow_pixmap,
XRenderFindStandardFormat(ps->dpy, PictStandardA8), 0, 0);
shadow_picture_argb = XRenderCreatePicture(ps->dpy, shadow_pixmap_argb,
XRenderFindStandardFormat(ps->dpy, PictStandardARGB32), 0, 0);
if (!shadow_picture || !shadow_picture_argb)
goto shadow_picture_err;
gc = XCreateGC(ps->dpy, shadow_pixmap, 0, 0);
if (!gc)
goto shadow_picture_err;
XPutImage(ps->dpy, shadow_pixmap, gc, shadow_image, 0, 0, 0, 0,
shadow_image->width, shadow_image->height);
XRenderComposite(ps->dpy, PictOpSrc, ps->cshadow_picture, shadow_picture,
shadow_picture_argb, 0, 0, 0, 0, 0, 0,
shadow_image->width, shadow_image->height);
assert(!w->shadow_paint.pixmap);
w->shadow_paint.pixmap = shadow_pixmap_argb;
assert(!w->shadow_paint.pict);
w->shadow_paint.pict = shadow_picture_argb;
// Sync it once and only once
xr_sync(ps, w->shadow_paint.pixmap, NULL);
XFreeGC(ps->dpy, gc);
XDestroyImage(shadow_image);
XFreePixmap(ps->dpy, shadow_pixmap);
XRenderFreePicture(ps->dpy, shadow_picture);
return true;
shadow_picture_err:
if (shadow_image)
XDestroyImage(shadow_image);
if (shadow_pixmap)
XFreePixmap(ps->dpy, shadow_pixmap);
if (shadow_pixmap_argb)
XFreePixmap(ps->dpy, shadow_pixmap_argb);
if (shadow_picture)
XRenderFreePicture(ps->dpy, shadow_picture);
if (shadow_picture_argb)
XRenderFreePicture(ps->dpy, shadow_picture_argb);
if (gc)
XFreeGC(ps->dpy, gc);
return false;
}
/**
* Generate a 1x1 <code>Picture</code> of a particular color.
*/
static Picture
solid_picture(session_t *ps, bool argb, double a,
double r, double g, double b) {
Pixmap pixmap;
Picture picture;
XRenderPictureAttributes pa;
XRenderColor c;
pixmap = XCreatePixmap(ps->dpy, ps->root, 1, 1, argb ? 32 : 8);
if (!pixmap) return None;
pa.repeat = True;
picture = XRenderCreatePicture(ps->dpy, pixmap,
XRenderFindStandardFormat(ps->dpy, argb
? PictStandardARGB32 : PictStandardA8),
CPRepeat,
&pa);
if (!picture) {
XFreePixmap(ps->dpy, pixmap);
return None;
}
c.alpha = a * 0xffff;
c.red = r * 0xffff;
c.green = g * 0xffff;
c.blue = b * 0xffff;
XRenderFillRectangle(ps->dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
XFreePixmap(ps->dpy, pixmap);
return picture;
}
// === Error handling ===
static void
discard_ignore(session_t *ps, unsigned long sequence) {
while (ps->ignore_head) {
if ((long) (sequence - ps->ignore_head->sequence) > 0) {
ignore_t *next = ps->ignore_head->next;
free(ps->ignore_head);
ps->ignore_head = next;
if (!ps->ignore_head) {
ps->ignore_tail = &ps->ignore_head;
}
} else {
break;
}
}
}
static void
set_ignore(session_t *ps, unsigned long sequence) {
if (ps->o.show_all_xerrors)
return;
ignore_t *i = malloc(sizeof(ignore_t));
if (!i) return;
i->sequence = sequence;
i->next = 0;
*ps->ignore_tail = i;
ps->ignore_tail = &i->next;
}
static int
should_ignore(session_t *ps, unsigned long sequence) {
discard_ignore(ps, sequence);
return ps->ignore_head && ps->ignore_head->sequence == sequence;
}
// === Windows ===
/**
* Get a specific attribute of a window.
*
* Returns a blank structure if the returned type and format does not
* match the requested type and format.
*
* @param ps current session
* @param w window
* @param atom atom of attribute to fetch
* @param length length to read
* @param rtype atom of the requested type
* @param rformat requested format
* @return a <code>winprop_t</code> structure containing the attribute
* and number of items. A blank one on failure.
*/
winprop_t
wid_get_prop_adv(const session_t *ps, Window w, Atom atom, long offset,
long length, Atom rtype, int rformat) {
Atom type = None;
int format = 0;
unsigned long nitems = 0, after = 0;
unsigned char *data = NULL;
if (Success == XGetWindowProperty(ps->dpy, w, atom, offset, length,
False, rtype, &type, &format, &nitems, &after, &data)
&& nitems && (AnyPropertyType == type || type == rtype)
&& (!rformat || format == rformat)
&& (8 == format || 16 == format || 32 == format)) {
return (winprop_t) {
.data.p8 = data,
.nitems = nitems,
.type = type,
.format = format,
};
}
cxfree(data);
return (winprop_t) {
.data.p8 = NULL,
.nitems = 0,
.type = AnyPropertyType,
.format = 0
};
}
/**
* Check if a window has rounded corners.
*/
static void
win_rounded_corners(session_t *ps, win *w) {
w->rounded_corners = false;
if (!w->bounding_shaped)
return;
// Fetch its bounding region
if (!w->border_size)
w->border_size = border_size(ps, w, true);
// Quit if border_size() returns None
if (!w->border_size)
return;
// Determine the minimum width/height of a rectangle that could mark
// a window as having rounded corners
unsigned short minwidth = max_i(w->widthb * (1 - ROUNDED_PERCENT),
w->widthb - ROUNDED_PIXELS);
unsigned short minheight = max_i(w->heightb * (1 - ROUNDED_PERCENT),
w->heightb - ROUNDED_PIXELS);
// Get the rectangles in the bounding region
int nrects = 0, i;
XRectangle *rects = XFixesFetchRegion(ps->dpy, w->border_size, &nrects);
if (!rects)
return;
// Look for a rectangle large enough for this window be considered
// having rounded corners
for (i = 0; i < nrects; ++i)
if (rects[i].width >= minwidth && rects[i].height >= minheight) {
w->rounded_corners = true;
break;
}
cxfree(rects);
}
/**
* Add a pattern to a condition linked list.
*/
static bool
condlst_add(session_t *ps, c2_lptr_t **pcondlst, const char *pattern) {
if (!pattern)
return false;
#ifdef CONFIG_C2
if (!c2_parse(ps, pcondlst, pattern))
exit(1);
#else
printf_errfq(1, "(): Condition support not compiled in.");
#endif
return true;
}
/**
* Determine the event mask for a window.
*/
static long
determine_evmask(session_t *ps, Window wid, win_evmode_t mode) {
long evmask = NoEventMask;
win *w = NULL;
// Check if it's a mapped frame window
if (WIN_EVMODE_FRAME == mode
|| ((w = find_win(ps, wid)) && IsViewable == w->a.map_state)) {
evmask |= PropertyChangeMask;
if (ps->o.track_focus && !ps->o.use_ewmh_active_win)
evmask |= FocusChangeMask;
}
// Check if it's a mapped client window
if (WIN_EVMODE_CLIENT == mode
|| ((w = find_toplevel(ps, wid)) && IsViewable == w->a.map_state)) {
if (ps->o.frame_opacity || ps->o.track_wdata || ps->track_atom_lst
|| ps->o.detect_client_opacity)
evmask |= PropertyChangeMask;
}
return evmask;
}
/**
* Find out the WM frame of a client window by querying X.
*
* @param ps current session
* @param wid window ID
* @return struct _win object of the found window, NULL if not found
*/
static win *
find_toplevel2(session_t *ps, Window wid) {
win *w = NULL;
// We traverse through its ancestors to find out the frame
while (wid && wid != ps->root && !(w = find_win(ps, wid))) {
Window troot;
Window parent;
Window *tchildren;
unsigned tnchildren;
// XQueryTree probably fails if you run compton when X is somehow
// initializing (like add it in .xinitrc). In this case
// just leave it alone.
if (!XQueryTree(ps->dpy, wid, &troot, &parent, &tchildren,
&tnchildren)) {
parent = 0;
break;
}
cxfree(tchildren);
wid = parent;
}
return w;
}
/**
* Recheck currently focused window and set its <code>w->focused</code>
* to true.
*
* @param ps current session
* @return struct _win of currently focused window, NULL if not found
*/
static win *
recheck_focus(session_t *ps) {
// Use EWMH _NET_ACTIVE_WINDOW if enabled
if (ps->o.use_ewmh_active_win) {
update_ewmh_active_win(ps);
return ps->active_win;
}
// Determine the currently focused window so we can apply appropriate
// opacity on it
Window wid = 0;
int revert_to;
XGetInputFocus(ps->dpy, &wid, &revert_to);
win *w = find_win_all(ps, wid);
#ifdef DEBUG_EVENTS
print_timestamp(ps);
printf_dbgf("(): %#010lx (%#010lx \"%s\") focused.\n", wid,
(w ? w->id: None), (w ? w->name: NULL));
#endif
// And we set the focus state here
if (w) {
win_set_focused(ps, w, true);
return w;
}
return NULL;
}
static bool
get_root_tile(session_t *ps) {
/*
if (ps->o.paint_on_overlay) {
return ps->root_picture;
} */
assert(!ps->root_tile_paint.pixmap);
ps->root_tile_fill = false;
bool fill = false;
Pixmap pixmap = None;
// Get the values of background attributes
for (int p = 0; background_props_str[p]; p++) {
winprop_t prop = wid_get_prop(ps, ps->root,
get_atom(ps, background_props_str[p]),
1L, XA_PIXMAP, 32);
if (prop.nitems) {
pixmap = *prop.data.p32;
fill = false;
free_winprop(&prop);
break;
}
free_winprop(&prop);
}
// Make sure the pixmap we got is valid
if (pixmap && !validate_pixmap(ps, pixmap))
pixmap = None;
// Create a pixmap if there isn't any
if (!pixmap) {
pixmap = XCreatePixmap(ps->dpy, ps->root, 1, 1, ps->depth);
fill = true;
}
// Create Picture
{
XRenderPictureAttributes pa = {
.repeat = True,
};
ps->root_tile_paint.pict = XRenderCreatePicture(
ps->dpy, pixmap, XRenderFindVisualFormat(ps->dpy, ps->vis),
CPRepeat, &pa);
}
// Fill pixmap if needed
if (fill) {
XRenderColor c;
c.red = c.green = c.blue = 0x8080;
c.alpha = 0xffff;
XRenderFillRectangle(ps->dpy, PictOpSrc, ps->root_tile_paint.pict, &c, 0, 0, 1, 1);
}
ps->root_tile_fill = fill;
ps->root_tile_paint.pixmap = pixmap;
#ifdef CONFIG_VSYNC_OPENGL
if (BKEND_GLX == ps->o.backend)
return glx_bind_pixmap(ps, &ps->root_tile_paint.ptex, ps->root_tile_paint.pixmap, 0, 0, 0);
#endif
return true;
}
/**
* Paint root window content.
*/
static void
paint_root(session_t *ps, XserverRegion reg_paint) {
if (!ps->root_tile_paint.pixmap)
get_root_tile(ps);
win_render(ps, NULL, 0, 0, ps->root_width, ps->root_height, 1.0, reg_paint,
NULL, ps->root_tile_paint.pict);
}
/**
* Get a rectangular region a window occupies, excluding shadow.
*/
static XserverRegion
win_get_region(session_t *ps, win *w, bool use_offset) {
XRectangle r;
r.x = (use_offset ? w->a.x: 0);
r.y = (use_offset ? w->a.y: 0);
r.width = w->widthb;
r.height = w->heightb;
return XFixesCreateRegion(ps->dpy, &r, 1);
}
/**
* Get a rectangular region a window occupies, excluding frame and shadow.
*/
static XserverRegion
win_get_region_noframe(session_t *ps, win *w, bool use_offset) {
const margin_t extents = win_calc_frame_extents(ps, w);
XRectangle r;
r.x = (use_offset ? w->a.x: 0) + extents.left;
r.y = (use_offset ? w->a.y: 0) + extents.top;
r.width = max_i(w->a.width - extents.left - extents.right, 0);
r.height = max_i(w->a.height - extents.top - extents.bottom, 0);
if (r.width > 0 && r.height > 0)
return XFixesCreateRegion(ps->dpy, &r, 1);
else
return XFixesCreateRegion(ps->dpy, NULL, 0);
}
/**
* Get a rectangular region a window (and possibly its shadow) occupies.
*
* Note w->shadow and shadow geometry must be correct before calling this
* function.
*/
static XserverRegion
win_extents(session_t *ps, win *w) {
XRectangle r;
r.x = w->a.x;
r.y = w->a.y;
r.width = w->widthb;
r.height = w->heightb;
if (w->shadow) {
XRectangle sr;
sr.x = w->a.x + w->shadow_dx;
sr.y = w->a.y + w->shadow_dy;
sr.width = w->shadow_width;
sr.height = w->shadow_height;
if (sr.x < r.x) {
r.width = (r.x + r.width) - sr.x;
r.x = sr.x;
}
if (sr.y < r.y) {
r.height = (r.y + r.height) - sr.y;
r.y = sr.y;
}
if (sr.x + sr.width > r.x + r.width) {
r.width = sr.x + sr.width - r.x;
}
if (sr.y + sr.height > r.y + r.height) {
r.height = sr.y + sr.height - r.y;
}
}
return XFixesCreateRegion(ps->dpy, &r, 1);
}
/**
* Retrieve the bounding shape of a window.
*/
static XserverRegion
border_size(session_t *ps, win *w, bool use_offset) {
// Start with the window rectangular region
XserverRegion fin = win_get_region(ps, w, use_offset);
// Only request for a bounding region if the window is shaped
if (w->bounding_shaped) {
/*
* if window doesn't exist anymore, this will generate an error
* as well as not generate a region. Perhaps a better XFixes
* architecture would be to have a request that copies instead
* of creates, that way you'd just end up with an empty region
* instead of an invalid XID.
*/
XserverRegion border = XFixesCreateRegionFromWindow(
ps->dpy, w->id, WindowRegionBounding);
if (!border)
return fin;
if (use_offset) {
// Translate the region to the correct place
XFixesTranslateRegion(ps->dpy, border,
w->a.x + w->a.border_width,
w->a.y + w->a.border_width);
}
// Intersect the bounding region we got with the window rectangle, to
// make sure the bounding region is not bigger than the window
// rectangle
XFixesIntersectRegion(ps->dpy, fin, fin, border);
XFixesDestroyRegion(ps->dpy, border);
}