-
-
Notifications
You must be signed in to change notification settings - Fork 620
Expand file tree
/
Copy pathwin.c
More file actions
2894 lines (2472 loc) · 84.2 KB
/
win.c
File metadata and controls
2894 lines (2472 loc) · 84.2 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2011-2013, Christopher Jeffrey
// Copyright (c) 2013 Richard Grenville <pyxlcy@gmail.com>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/composite.h>
#include <xcb/damage.h>
#include <xcb/render.h>
#include <xcb/xcb.h>
#include <xcb/xcb_renderutil.h>
#include "atom.h"
#include "backend/backend.h"
#include "c2.h"
#include "common.h"
#include "compiler.h"
#include "config.h"
#include "list.h"
#include "log.h"
#include "picom.h"
#include "region.h"
#include "render.h"
#include "string_utils.h"
#include "types.h"
#include "uthash_extra.h"
#include "utils.h"
#include "x.h"
#ifdef CONFIG_DBUS
#include "dbus.h"
#endif
#ifdef CONFIG_OPENGL
// TODO(yshui) Get rid of this include
#include "opengl.h"
#endif
#include "win.h"
// TODO(yshui) Make more window states internal
struct managed_win_internal {
struct managed_win base;
};
#define OPAQUE (0xffffffff)
static const int WIN_GET_LEADER_MAX_RECURSION = 20;
static const int ROUNDED_PIXELS = 1;
static const double ROUNDED_PERCENT = 0.05;
/**
* Retrieve the <code>WM_CLASS</code> of a window and update its
* <code>win</code> structure.
*/
static bool win_update_class(session_t *ps, struct managed_win *w);
static int win_update_role(session_t *ps, struct managed_win *w);
static void win_update_wintype(session_t *ps, struct managed_win *w);
static int win_update_name(session_t *ps, struct managed_win *w);
/**
* Reread opacity property of a window.
*/
static void win_update_opacity_prop(session_t *ps, struct managed_win *w);
static void win_update_opacity_target(session_t *ps, struct managed_win *w);
/**
* Retrieve frame extents from a window.
*/
static void
win_update_frame_extents(session_t *ps, struct managed_win *w, xcb_window_t client);
static void win_update_prop_shadow_raw(session_t *ps, struct managed_win *w);
static void win_update_prop_shadow(session_t *ps, struct managed_win *w);
/**
* Update leader of a window.
*/
static void win_update_leader(session_t *ps, struct managed_win *w);
/// Generate a "no corners" region function, from a function that returns the
/// region via a region_t pointer argument. Corners of the window will be removed from
/// the returned region.
/// Function signature has to be (win *, region_t *)
#define gen_without_corners(fun) \
void fun##_without_corners(const struct managed_win *w, region_t *res) { \
fun(w, res); \
win_region_remove_corners(w, res); \
}
/// Generate a "return by value" function, from a function that returns the
/// region via a region_t pointer argument.
/// Function signature has to be (win *)
#define gen_by_val(fun) \
region_t fun##_by_val(const struct managed_win *w) { \
region_t ret; \
pixman_region32_init(&ret); \
fun(w, &ret); \
return ret; \
}
/**
* Clear leader cache of all windows.
*/
static inline void clear_cache_win_leaders(session_t *ps) {
win_stack_foreach_managed(w, &ps->window_stack) {
w->cache_leader = XCB_NONE;
}
}
static xcb_window_t win_get_leader_raw(session_t *ps, struct managed_win *w, int recursions);
/**
* Get the leader of a window.
*
* This function updates w->cache_leader if necessary.
*/
static inline xcb_window_t win_get_leader(session_t *ps, struct managed_win *w) {
return win_get_leader_raw(ps, w, 0);
}
/**
* Whether the real content of the window is visible.
*
* A window is not considered "real" visible if it's fading out. Because in that case a
* cached version of the window is displayed.
*/
static inline bool attr_pure win_is_real_visible(const struct managed_win *w) {
return w->state != WSTATE_UNMAPPED && w->state != WSTATE_DESTROYING &&
w->state != WSTATE_UNMAPPING;
}
/**
* Update focused state of a window.
*/
static void win_update_focused(session_t *ps, struct managed_win *w) {
if (UNSET != w->focused_force) {
w->focused = w->focused_force;
} else {
w->focused = win_is_focused_raw(ps, w);
// Use wintype_focus, and treat WM windows and override-redirected
// windows specially
if (ps->o.wintype_option[w->window_type].focus ||
(ps->o.mark_wmwin_focused && w->wmwin) ||
(ps->o.mark_ovredir_focused && w->base.id == w->client_win && !w->wmwin) ||
(w->a.map_state == XCB_MAP_STATE_VIEWABLE &&
c2_match(ps, w, ps->o.focus_blacklist, NULL))) {
w->focused = true;
}
// If window grouping detection is enabled, mark the window active if
// its group is
if (ps->o.track_leader && ps->active_leader &&
win_get_leader(ps, w) == ps->active_leader) {
w->focused = true;
}
}
}
/**
* Run win_on_factor_change() on all windows with the same leader window.
*
* @param leader leader window ID
*/
static inline void group_on_factor_change(session_t *ps, xcb_window_t leader) {
if (!leader) {
return;
}
HASH_ITER2(ps->windows, w) {
assert(!w->destroyed);
if (!w->managed) {
continue;
}
auto mw = (struct managed_win *)w;
if (win_get_leader(ps, mw) == leader) {
win_on_factor_change(ps, mw);
}
}
}
static inline const char *win_get_name_if_managed(const struct win *w) {
if (!w->managed) {
return "(unmanaged)";
}
auto mw = (struct managed_win *)w;
return mw->name;
}
/**
* Return whether a window group is really focused.
*
* @param leader leader window ID
* @return true if the window group is focused, false otherwise
*/
static inline bool group_is_focused(session_t *ps, xcb_window_t leader) {
if (!leader) {
return false;
}
HASH_ITER2(ps->windows, w) {
assert(!w->destroyed);
if (!w->managed) {
continue;
}
auto mw = (struct managed_win *)w;
if (win_get_leader(ps, mw) == leader && win_is_focused_raw(ps, mw)) {
return true;
}
}
return false;
}
/**
* Get a rectangular region a window occupies, excluding shadow.
*/
static void win_get_region_local(const struct managed_win *w, region_t *res) {
assert(w->widthb >= 0 && w->heightb >= 0);
pixman_region32_fini(res);
pixman_region32_init_rect(res, 0, 0, (uint)w->widthb, (uint)w->heightb);
}
/**
* Get a rectangular region a window occupies, excluding frame and shadow.
*/
void win_get_region_noframe_local(const struct managed_win *w, region_t *res) {
const margin_t extents = win_calc_frame_extents(w);
int x = extents.left;
int y = extents.top;
int width = max2(w->widthb - (extents.left + extents.right), 0);
int height = max2(w->heightb - (extents.top + extents.bottom), 0);
pixman_region32_fini(res);
if (width > 0 && height > 0) {
pixman_region32_init_rect(res, x, y, (uint)width, (uint)height);
} else {
pixman_region32_init(res);
}
}
gen_without_corners(win_get_region_noframe_local);
void win_get_region_frame_local(const struct managed_win *w, region_t *res) {
const margin_t extents = win_calc_frame_extents(w);
auto outer_width = w->widthb;
auto outer_height = w->heightb;
pixman_region32_fini(res);
pixman_region32_init_rects(
res,
(rect_t[]){
// top
{.x1 = 0, .y1 = 0, .x2 = outer_width, .y2 = extents.top},
// bottom
{.x1 = 0, .y1 = outer_height - extents.bottom, .x2 = outer_width, .y2 = outer_height},
// left
{.x1 = 0, .y1 = 0, .x2 = extents.left, .y2 = outer_height},
// right
{.x1 = outer_width - extents.right, .y1 = 0, .x2 = outer_width, .y2 = outer_height},
},
4);
// limit the frame region to inside the window
region_t reg_win;
pixman_region32_init_rects(®_win, (rect_t[]){{0, 0, outer_width, outer_height}}, 1);
pixman_region32_intersect(res, ®_win, res);
pixman_region32_fini(®_win);
}
gen_by_val(win_get_region_frame_local);
/**
* Add a window to damaged area.
*
* @param ps current session
* @param w struct _win element representing the window
*/
void add_damage_from_win(session_t *ps, const struct managed_win *w) {
// XXX there was a cached extents region, investigate
// if that's better
// TODO(yshui) use the bounding shape when the window is shaped, otherwise the
// damage would be excessive
region_t extents;
pixman_region32_init(&extents);
win_extents(w, &extents);
add_damage(ps, &extents);
pixman_region32_fini(&extents);
}
/// Release the images attached to this window
static inline void win_release_pixmap(backend_t *base, struct managed_win *w) {
log_debug("Releasing pixmap of window %#010x (%s)", w->base.id, w->name);
assert(w->win_image);
if (w->win_image) {
base->ops->release_image(base, w->win_image);
w->win_image = NULL;
// Bypassing win_set_flags, because `w` might have been destroyed
w->flags |= WIN_FLAGS_PIXMAP_NONE;
}
}
static inline void win_release_shadow(backend_t *base, struct managed_win *w) {
log_debug("Releasing shadow of window %#010x (%s)", w->base.id, w->name);
assert(w->shadow_image);
if (w->shadow_image) {
base->ops->release_image(base, w->shadow_image);
w->shadow_image = NULL;
// Bypassing win_set_flags, because `w` might have been destroyed
w->flags |= WIN_FLAGS_SHADOW_NONE;
}
}
static inline void win_release_mask(backend_t *base, struct managed_win *w) {
log_debug("Releasing mask of window %#010x (%s)", w->base.id, w->name);
assert(w->mask_image);
if (w->mask_image) {
base->ops->release_image(base, w->mask_image);
w->mask_image = NULL;
// Bypassing win_set_flags, because `w` might have been destroyed
w->flags |= WIN_FLAGS_MASK_NONE;
}
}
static inline bool win_bind_pixmap(struct backend_base *b, struct managed_win *w) {
assert(!w->win_image);
auto pixmap = x_new_id(b->c);
auto e = xcb_request_check(
b->c, xcb_composite_name_window_pixmap_checked(b->c, w->base.id, pixmap));
if (e) {
log_error("Failed to get named pixmap for window %#010x(%s)", w->base.id,
w->name);
free(e);
return false;
}
log_debug("New named pixmap for %#010x (%s) : %#010x", w->base.id, w->name, pixmap);
w->win_image =
b->ops->bind_pixmap(b, pixmap, x_get_visual_info(b->c, w->a.visual), true);
if (!w->win_image) {
log_error("Failed to bind pixmap");
win_set_flags(w, WIN_FLAGS_IMAGE_ERROR);
return false;
}
win_clear_flags(w, WIN_FLAGS_PIXMAP_NONE);
return true;
}
bool win_bind_mask(struct backend_base *b, struct managed_win *w) {
assert(!w->mask_image);
assert(w->corner_radius != 0 || w->bounding_shaped);
auto reg_bound_local = win_get_bounding_shape_global_by_val(w);
pixman_region32_translate(®_bound_local, -w->g.x, -w->g.y);
w->mask_image = b->ops->make_mask(
b, (geometry_t){.width = w->widthb, .height = w->heightb}, ®_bound_local);
pixman_region32_fini(®_bound_local);
if (!w->mask_image) {
win_set_flags(w, WIN_FLAGS_MASK_NONE);
return false;
}
b->ops->set_image_property(b, IMAGE_PROPERTY_CORNER_RADIUS, w->mask_image,
(double[]){w->corner_radius});
win_clear_flags(w, WIN_FLAGS_MASK_NONE);
return true;
}
bool win_bind_shadow(struct backend_base *b, struct managed_win *w, struct color c,
struct backend_shadow_context *sctx) {
assert(!w->shadow_image);
assert(w->shadow);
if (w->mask_image && b->ops->shadow_from_mask) {
w->shadow_image = b->ops->shadow_from_mask(b, w->mask_image, sctx, c);
} else {
w->shadow_image = b->ops->render_shadow(b, w->widthb, w->heightb, sctx, c);
}
if (!w->shadow_image) {
log_error("Failed to bind shadow image, shadow will be disabled "
"for %#010x (%s)",
w->base.id, w->name);
win_set_flags(w, WIN_FLAGS_SHADOW_NONE);
w->shadow = false;
return false;
}
log_debug("New shadow for %#010x (%s)", w->base.id, w->name);
win_clear_flags(w, WIN_FLAGS_SHADOW_NONE);
return true;
}
void win_release_images(struct backend_base *backend, struct managed_win *w) {
// We don't want to decide what we should do if the image we want to
// release is stale (do we clear the stale flags or not?) But if we are
// not releasing any images anyway, we don't care about the stale flags.
if (!win_check_flags_all(w, WIN_FLAGS_PIXMAP_NONE)) {
assert(!win_check_flags_all(w, WIN_FLAGS_PIXMAP_STALE));
win_release_pixmap(backend, w);
}
if (!win_check_flags_all(w, WIN_FLAGS_SHADOW_NONE)) {
assert(!win_check_flags_all(w, WIN_FLAGS_SHADOW_STALE));
win_release_shadow(backend, w);
}
if (!win_check_flags_all(w, WIN_FLAGS_MASK_NONE)) {
assert(!win_check_flags_all(w, WIN_FLAGS_MASK_STALE));
win_release_mask(backend, w);
}
}
/// Returns true if the `prop` property is stale, as well as clears the stale
/// flag.
static bool win_fetch_and_unset_property_stale(struct managed_win *w, xcb_atom_t prop);
/// Returns true if any of the properties are stale, as well as clear all the
/// stale flags.
static void win_clear_all_properties_stale(struct managed_win *w);
/// Fetch new window properties from the X server, and run appropriate updates.
/// Might set WIN_FLAGS_FACTOR_CHANGED
static void win_update_properties(session_t *ps, struct managed_win *w) {
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_WINDOW_TYPE)) {
win_update_wintype(ps, w);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_WINDOW_OPACITY)) {
win_update_opacity_prop(ps, w);
// we cannot receive OPACITY change when window has been destroyed
assert(w->state != WSTATE_DESTROYING);
win_update_opacity_target(ps, w);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_FRAME_EXTENTS)) {
win_update_frame_extents(ps, w, w->client_win);
add_damage_from_win(ps, w);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_NAME) ||
win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_NAME)) {
if (win_update_name(ps, w) == 1) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_CLASS)) {
if (win_update_class(ps, w)) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_WINDOW_ROLE)) {
if (win_update_role(ps, w) == 1) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_COMPTON_SHADOW)) {
win_update_prop_shadow(ps, w);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_CLIENT_LEADER) ||
win_fetch_and_unset_property_stale(w, ps->atoms->aWM_TRANSIENT_FOR)) {
win_update_leader(ps, w);
}
win_clear_all_properties_stale(w);
}
/// Handle non-image flags. This phase might set IMAGES_STALE flags
void win_process_update_flags(session_t *ps, struct managed_win *w) {
// Whether the window was visible before we process the mapped flag. i.e.
// is the window just mapped.
bool was_visible = win_is_real_visible(w);
log_trace("Processing flags for window %#010x (%s), was visible: %d", w->base.id,
w->name, was_visible);
if (win_check_flags_all(w, WIN_FLAGS_MAPPED)) {
map_win_start(ps, w);
win_clear_flags(w, WIN_FLAGS_MAPPED);
}
if (!win_is_real_visible(w)) {
// Flags of invisible windows are processed when they are mapped
return;
}
// Check client first, because later property updates need accurate client
// window information
if (win_check_flags_all(w, WIN_FLAGS_CLIENT_STALE)) {
win_recheck_client(ps, w);
win_clear_flags(w, WIN_FLAGS_CLIENT_STALE);
}
bool damaged = false;
if (win_check_flags_any(w, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE)) {
if (was_visible) {
// Mark the old extents of this window as damaged. The new
// extents will be marked damaged below, after the window
// extents are updated.
//
// If the window is just mapped, we don't need to mark the
// old extent as damaged. (It's possible that the window
// was in fading and is interrupted by being mapped. In
// that case, the fading window will be added to damage by
// map_win_start, so we don't need to do it here)
add_damage_from_win(ps, w);
}
// Update window geometry
w->g = w->pending_g;
if (win_check_flags_all(w, WIN_FLAGS_SIZE_STALE)) {
win_on_win_size_change(ps, w);
win_update_bounding_shape(ps, w);
damaged = true;
win_clear_flags(w, WIN_FLAGS_SIZE_STALE);
}
if (win_check_flags_all(w, WIN_FLAGS_POSITION_STALE)) {
damaged = true;
win_clear_flags(w, WIN_FLAGS_POSITION_STALE);
}
win_update_monitor(ps->randr_nmonitors, ps->randr_monitor_regs, w);
}
if (win_check_flags_all(w, WIN_FLAGS_PROPERTY_STALE)) {
win_update_properties(ps, w);
win_clear_flags(w, WIN_FLAGS_PROPERTY_STALE);
}
// Factor change flags could be set by previous stages, so must be handled
// last
if (win_check_flags_all(w, WIN_FLAGS_FACTOR_CHANGED)) {
win_on_factor_change(ps, w);
win_clear_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
// Add damage, has to be done last so the window has the latest geometry
// information.
if (damaged) {
add_damage_from_win(ps, w);
}
}
void win_process_image_flags(session_t *ps, struct managed_win *w) {
assert(!win_check_flags_all(w, WIN_FLAGS_MAPPED));
if (w->state == WSTATE_UNMAPPED || w->state == WSTATE_DESTROYING ||
w->state == WSTATE_UNMAPPING) {
// Flags of invisible windows are processed when they are mapped
return;
}
// Not a loop
while (win_check_flags_any(w, WIN_FLAGS_IMAGES_STALE) &&
!win_check_flags_all(w, WIN_FLAGS_IMAGE_ERROR)) {
// Image needs to be updated, update it.
if (!ps->backend_data) {
// We are using legacy backend, nothing to do here.
break;
}
if (win_check_flags_all(w, WIN_FLAGS_PIXMAP_STALE)) {
// Check to make sure the window is still mapped,
// otherwise we won't be able to rebind pixmap after
// releasing it, yet we might still need the pixmap for
// rendering.
assert(w->state != WSTATE_UNMAPPING && w->state != WSTATE_DESTROYING);
if (!win_check_flags_all(w, WIN_FLAGS_PIXMAP_NONE)) {
// Must release images first, otherwise breaks
// NVIDIA driver
win_release_pixmap(ps->backend_data, w);
}
win_bind_pixmap(ps->backend_data, w);
}
if (win_check_flags_all(w, WIN_FLAGS_MASK_STALE)) {
if (!win_check_flags_all(w, WIN_FLAGS_MASK_NONE)) {
win_release_mask(ps->backend_data, w);
}
if (w->bounding_shaped || w->corner_radius != 0) {
win_bind_mask(ps->backend_data, w);
}
}
if (win_check_flags_all(w, WIN_FLAGS_SHADOW_STALE)) {
if (!win_check_flags_all(w, WIN_FLAGS_SHADOW_NONE)) {
win_release_shadow(ps->backend_data, w);
}
if (w->shadow) {
win_bind_shadow(ps->backend_data, w,
(struct color){.red = ps->o.shadow_red,
.green = ps->o.shadow_green,
.blue = ps->o.shadow_blue,
.alpha = ps->o.shadow_opacity},
ps->shadow_context);
}
}
// break here, loop always run only once
break;
}
// Clear stale image flags
if (win_check_flags_any(w, WIN_FLAGS_IMAGES_STALE)) {
win_clear_flags(w, WIN_FLAGS_IMAGES_STALE);
}
}
/**
* Check if a window has rounded corners.
* XXX This is really dumb
*/
static bool attr_pure win_has_rounded_corners(const struct managed_win *w) {
if (!w->bounding_shaped) {
return false;
}
// Quit if border_size() returns XCB_NONE
if (!pixman_region32_not_empty((region_t *)&w->bounding_shape)) {
return false;
}
// Determine the minimum width/height of a rectangle that could mark
// a window as having rounded corners
auto minwidth =
(uint16_t)max2(w->widthb * (1 - ROUNDED_PERCENT), w->widthb - ROUNDED_PIXELS);
auto minheight =
(uint16_t)max2(w->heightb * (1 - ROUNDED_PERCENT), w->heightb - ROUNDED_PIXELS);
// Get the rectangles in the bounding region
int nrects = 0;
const rect_t *rects =
pixman_region32_rectangles((region_t *)&w->bounding_shape, &nrects);
// Look for a rectangle large enough for this window be considered
// having rounded corners
for (int i = 0; i < nrects; ++i) {
if (rects[i].x2 - rects[i].x1 >= minwidth &&
rects[i].y2 - rects[i].y1 >= minheight) {
return true;
}
}
return false;
}
int win_update_name(session_t *ps, struct managed_win *w) {
char **strlst = NULL;
int nstr = 0;
if (!w->client_win) {
return 0;
}
if (!(wid_get_text_prop(ps, w->client_win, ps->atoms->a_NET_WM_NAME, &strlst, &nstr))) {
log_debug("(%#010x): _NET_WM_NAME unset, falling back to "
"WM_NAME.",
w->client_win);
if (!wid_get_text_prop(ps, w->client_win, ps->atoms->aWM_NAME, &strlst, &nstr)) {
log_debug("Unsetting window name for %#010x", w->client_win);
free(w->name);
w->name = NULL;
return -1;
}
}
int ret = 0;
if (!w->name || strcmp(w->name, strlst[0]) != 0) {
ret = 1;
free(w->name);
w->name = strdup(strlst[0]);
}
free(strlst);
log_debug("(%#010x): client = %#010x, name = \"%s\", "
"ret = %d",
w->base.id, w->client_win, w->name, ret);
return ret;
}
static int win_update_role(session_t *ps, struct managed_win *w) {
char **strlst = NULL;
int nstr = 0;
if (!wid_get_text_prop(ps, w->client_win, ps->atoms->aWM_WINDOW_ROLE, &strlst, &nstr)) {
return -1;
}
int ret = 0;
if (!w->role || strcmp(w->role, strlst[0]) != 0) {
ret = 1;
free(w->role);
w->role = strdup(strlst[0]);
}
free(strlst);
log_trace("(%#010x): client = %#010x, role = \"%s\", "
"ret = %d",
w->base.id, w->client_win, w->role, ret);
return ret;
}
/**
* Check if a window is bounding-shaped.
*/
static inline bool win_bounding_shaped(const session_t *ps, xcb_window_t wid) {
if (ps->shape_exists) {
xcb_shape_query_extents_reply_t *reply;
Bool bounding_shaped;
reply = xcb_shape_query_extents_reply(
ps->c, xcb_shape_query_extents(ps->c, wid), NULL);
bounding_shaped = reply && reply->bounding_shaped;
free(reply);
return bounding_shaped;
}
return false;
}
static wintype_t wid_get_prop_wintype(session_t *ps, xcb_window_t wid) {
winprop_t prop =
x_get_prop(ps->c, wid, ps->atoms->a_NET_WM_WINDOW_TYPE, 32L, XCB_ATOM_ATOM, 32);
for (unsigned i = 0; i < prop.nitems; ++i) {
for (wintype_t j = 1; j < NUM_WINTYPES; ++j) {
if (ps->atoms_wintypes[j] == (xcb_atom_t)prop.p32[i]) {
free_winprop(&prop);
return j;
}
}
}
free_winprop(&prop);
return WINTYPE_UNKNOWN;
}
static bool
wid_get_opacity_prop(session_t *ps, xcb_window_t wid, opacity_t def, opacity_t *out) {
bool ret = false;
*out = def;
winprop_t prop = x_get_prop(ps->c, wid, ps->atoms->a_NET_WM_WINDOW_OPACITY, 1L,
XCB_ATOM_CARDINAL, 32);
if (prop.nitems) {
*out = *prop.c32;
ret = true;
}
free_winprop(&prop);
return ret;
}
// XXX should distinguish between frame has alpha and window body has alpha
bool win_has_alpha(const struct managed_win *w) {
return w->pictfmt && w->pictfmt->type == XCB_RENDER_PICT_TYPE_DIRECT &&
w->pictfmt->direct.alpha_mask;
}
bool win_client_has_alpha(const struct managed_win *w) {
return w->client_pictfmt && w->client_pictfmt->type == XCB_RENDER_PICT_TYPE_DIRECT &&
w->client_pictfmt->direct.alpha_mask;
}
winmode_t win_calc_mode(const struct managed_win *w) {
if (w->opacity < 1.0) {
return WMODE_TRANS;
}
if (win_has_alpha(w)) {
if (w->client_win == XCB_NONE) {
// This is a window not managed by the WM, and it has
// alpha, so it's transparent. No need to check WM frame.
return WMODE_TRANS;
}
// The WM window has alpha
if (win_client_has_alpha(w)) {
// The client window also has alpha, the entire window is
// transparent
return WMODE_TRANS;
}
if (win_has_frame(w)) {
// The client window doesn't have alpha, but we have a WM
// frame window, which has alpha.
return WMODE_FRAME_TRANS;
}
// Although the WM window has alpha, the frame window has 0 size,
// so consider the window solid
}
if (w->frame_opacity != 1.0 && win_has_frame(w)) {
return WMODE_FRAME_TRANS;
}
// log_trace("Window %#010x(%s) is solid", w->client_win, w->name);
return WMODE_SOLID;
}
/**
* Calculate and return the opacity target of a window.
*
* The priority of opacity settings are:
*
* inactive_opacity_override (if set, and unfocused) > _NET_WM_WINDOW_OPACITY (if
* set) > opacity-rules (if matched) > window type default opacity >
* active/inactive opacity
*
* @param ps current session
* @param w struct _win object representing the window
*
* @return target opacity
*/
double win_calc_opacity_target(session_t *ps, const struct managed_win *w) {
double opacity = 1;
if (w->state == WSTATE_UNMAPPED) {
// be consistent
return 0;
}
if (w->state == WSTATE_UNMAPPING || w->state == WSTATE_DESTROYING) {
return 0;
}
// Try obeying opacity property and window type opacity firstly
if (w->has_opacity_prop) {
opacity = ((double)w->opacity_prop) / OPAQUE;
} else if (w->opacity_is_set) {
opacity = w->opacity_set;
} else if (!safe_isnan(ps->o.wintype_option[w->window_type].opacity)) {
opacity = ps->o.wintype_option[w->window_type].opacity;
} else {
// Respect active_opacity only when the window is physically
// focused
if (win_is_focused_raw(ps, w))
opacity = ps->o.active_opacity;
else if (!w->focused)
// Respect inactive_opacity in some cases
opacity = ps->o.inactive_opacity;
}
// respect inactive override
if (ps->o.inactive_opacity_override && !w->focused) {
opacity = ps->o.inactive_opacity;
}
return opacity;
}
/**
* Determine whether a window is to be dimmed.
*/
bool win_should_dim(session_t *ps, const struct managed_win *w) {
// Make sure we do nothing if the window is unmapped / being destroyed
if (w->state == WSTATE_UNMAPPED) {
return false;
}
if (ps->o.inactive_dim > 0 && !(w->focused)) {
return true;
} else {
return false;
}
}
/**
* Determine if a window should fade on opacity change.
*/
bool win_should_fade(session_t *ps, const struct managed_win *w) {
// To prevent it from being overwritten by last-paint value if the window
// is
if (w->fade_force != UNSET) {
return w->fade_force;
}
if (ps->o.no_fading_openclose && w->in_openclose) {
return false;
}
if (ps->o.no_fading_destroyed_argb && w->state == WSTATE_DESTROYING &&
win_has_alpha(w) && w->client_win && w->client_win != w->base.id) {
// deprecated
return false;
}
if (w->fade_excluded) {
return false;
}
return ps->o.wintype_option[w->window_type].fade;
}
/**
* Reread _COMPTON_SHADOW property from a window.
*
* The property must be set on the outermost window, usually the WM frame.
*/
void win_update_prop_shadow_raw(session_t *ps, struct managed_win *w) {
winprop_t prop = x_get_prop(ps->c, w->base.id, ps->atoms->a_COMPTON_SHADOW, 1,
XCB_ATOM_CARDINAL, 32);
if (!prop.nitems) {
w->prop_shadow = -1;
} else {
w->prop_shadow = *prop.c32;
}
free_winprop(&prop);
}
static void win_set_shadow(session_t *ps, struct managed_win *w, bool shadow_new) {
if (w->shadow == shadow_new) {
return;
}
log_debug("Updating shadow property of window %#010x (%s) to %d", w->base.id,
w->name, shadow_new);
// We don't handle property updates of non-visible windows until they are
// mapped.
assert(w->state != WSTATE_UNMAPPED && w->state != WSTATE_DESTROYING &&
w->state != WSTATE_UNMAPPING);
// Keep a copy of window extent before the shadow change. Will be used for
// calculation of damaged region
region_t extents;
pixman_region32_init(&extents);
win_extents(w, &extents);
// Apply the shadow change
w->shadow = shadow_new;
if (ps->redirected) {
// Add damage for shadow change
// Window extents need update on shadow state change
// Shadow geometry currently doesn't change on shadow state change
// calc_shadow_geometry(ps, w);
// Note: because the release and creation of the shadow images are
// delayed. When multiple shadow changes happen in a row, without
// rendering phase between them, there could be a stale shadow
// image attached to the window even if w->shadow was previously
// false. And vice versa. So we check the STALE flag before
// asserting the existence of the shadow image.
if (w->shadow) {
// Mark the new extents as damaged if the shadow is added
assert(!w->shadow_image ||
win_check_flags_all(w, WIN_FLAGS_SHADOW_STALE) ||
ps->o.legacy_backends);
pixman_region32_clear(&extents);
win_extents(w, &extents);
add_damage_from_win(ps, w);
} else {
// Mark the old extents as damaged if the shadow is
// removed
assert(w->shadow_image ||
win_check_flags_all(w, WIN_FLAGS_SHADOW_STALE) ||
ps->o.legacy_backends);
add_damage(ps, &extents);
}
// Delayed update of shadow image
// By setting WIN_FLAGS_SHADOW_STALE, we ask win_process_flags to
// re-create or release the shaodw in based on whether w->shadow
// is set.
win_set_flags(w, WIN_FLAGS_SHADOW_STALE);
// Only set pending_updates if we are redirected. Otherwise change
// of a shadow won't have influence on whether we should redirect.
ps->pending_updates = true;
}
pixman_region32_fini(&extents);
}
/**
* Determine if a window should have shadow, and update things depending
* on shadow state.
*/
static void win_determine_shadow(session_t *ps, struct managed_win *w) {
log_debug("Determining shadow of window %#010x (%s)", w->base.id, w->name);
bool shadow_new = w->shadow;
if (w->shadow_force != UNSET) {
shadow_new = w->shadow_force;
} else if (w->a.map_state == XCB_MAP_STATE_VIEWABLE) {
shadow_new = true;
if (!ps->o.wintype_option[w->window_type].shadow) {
log_debug("Shadow disabled by wintypes");
shadow_new = false;
} else if (c2_match(ps, w, ps->o.shadow_blacklist, NULL)) {
log_debug("Shadow disabled by shadow-exclude");
shadow_new = false;
} else if (ps->o.shadow_ignore_shaped && w->bounding_shaped &&