-
-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathwin.c
2145 lines (1867 loc) · 69.8 KB
/
win.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
// SPDX-License-Identifier: MIT
// Copyright (c) 2011-2013, Christopher Jeffrey
// Copyright (c) 2013 Richard Grenville <pyxlcy@gmail.com>
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@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 <picom/types.h>
#include "atom.h"
#include "c2.h"
#include "common.h"
#include "compiler.h"
#include "config.h"
#include "dbus.h"
#include "log.h"
#include "picom.h"
#include "region.h"
#include "render.h"
#include "utils/misc.h"
#include "x.h"
#include "defs.h"
#include "wm.h"
#include "win.h"
#define OPAQUE (0xffffffff)
static const int ROUNDED_PIXELS = 1;
static const double ROUNDED_PERCENT = 0.05;
// TODO(yshui)
//
// Right now, how window properties/states/information (let's just call them states)
// are calculated is a huge mess.
//
// We can divide a window's states (i.e. fields in struct managed_win) in to two groups:
// one is "raw" window states, those come directly from the X server; the other is
// computed window states, which is calculated based on the raw properties, and user
// configurations like rules etc.
//
// Right now what we do is when some raw states are updated, we set some flags to
// recalculate relevant computed states. This is really hard to get right, because it's
// tedious to figure out the influence a raw window state has. And it is also imprecise,
// just look at our `win_on_factor_changed` - it is so difficult to get the recalculation
// right, so we basically use "factor change" as a catch-all, basically any changes to raw
// states will cause it to be called. And we recalculate everything there, kind of
// destroying the whole point.
//
// A better way is doing this the other way around, we shouldn't need to do anything when
// updating a raw state. Instead, the computed states should declare which raw states they
// depend on, so we can go through the computed states, only recalculate the ones whose
// dependencies have changed. The c2 rules are kind of already calculated this way, we
// should unify the rest of the computed states. This would simplify the code as well.
static void
win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, struct win *w);
static bool win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct win *w);
/**
* Update leader of a window.
*/
static xcb_window_t
win_get_leader_property(struct x_connection *c, struct atom *atoms, xcb_window_t wid,
bool detect_transient, bool detect_client_leader);
/// 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 win *w, region_t *res) { \
fun(w, res); \
win_region_remove_corners_local(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 win *w) { \
region_t ret; \
pixman_region32_init(&ret); \
fun(w, &ret); \
return ret; \
}
/**
* Update focused state of a window.
*/
static bool win_is_focused(session_t *ps, struct win *w) {
bool is_wmwin = win_is_wmwin(w);
if (w->a.map_state == XCB_MAP_STATE_VIEWABLE && (w->is_focused || w->is_group_focused)) {
return true;
}
// 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 && is_wmwin) ||
(ps->o.mark_ovredir_focused && wm_ref_client_of(w->tree_ref) == NULL && !is_wmwin) ||
(w->a.map_state == XCB_MAP_STATE_VIEWABLE &&
c2_match(ps->c2_state, w, ps->o.focus_blacklist, NULL))) {
return true;
}
return false;
}
struct group_callback_data {
struct session *ps;
xcb_window_t leader;
};
/**
* Get a rectangular region a window occupies, excluding shadow.
*/
static void win_get_region_local(const struct 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 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 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 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 win *w) {
log_debug("Releasing pixmap of window %#010x (%s)", win_id(w), w->name);
if (w->win_image) {
xcb_pixmap_t pixmap = XCB_NONE;
pixmap = base->ops.release_image(base, w->win_image);
w->win_image = NULL;
if (pixmap != XCB_NONE) {
xcb_free_pixmap(base->c->c, pixmap);
}
}
}
static inline void win_release_shadow(backend_t *base, struct win *w) {
log_debug("Releasing shadow of window %#010x (%s)", win_id(w), w->name);
if (w->shadow_image) {
xcb_pixmap_t pixmap = XCB_NONE;
pixmap = base->ops.release_image(base, w->shadow_image);
w->shadow_image = NULL;
if (pixmap != XCB_NONE) {
xcb_free_pixmap(base->c->c, pixmap);
}
}
}
static inline void win_release_mask(backend_t *base, struct win *w) {
if (w->mask_image) {
xcb_pixmap_t pixmap = XCB_NONE;
pixmap = base->ops.release_image(base, w->mask_image);
w->mask_image = NULL;
if (pixmap != XCB_NONE) {
xcb_free_pixmap(base->c->c, pixmap);
}
}
}
void win_release_images(struct backend_base *backend, struct 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.
assert(w->win_image == NULL || !win_check_flags_all(w, WIN_FLAGS_PIXMAP_STALE));
win_release_pixmap(backend, w);
win_release_shadow(backend, w);
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 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 win *w);
/**
* Reread opacity property of a window.
*/
bool win_update_opacity_prop(struct x_connection *c, struct atom *atoms, struct win *w,
bool detect_client_opacity) {
bool old_has_opacity_prop = w->has_opacity_prop;
// get frame opacity first
w->has_opacity_prop =
wid_get_opacity_prop(c, atoms, win_id(w), OPAQUE, &w->opacity_prop);
if (w->has_opacity_prop) {
// opacity found
return old_has_opacity_prop != w->has_opacity_prop;
}
auto client_win = wm_ref_client_of(w->tree_ref);
if (!detect_client_opacity || client_win == NULL) {
return old_has_opacity_prop != w->has_opacity_prop;
}
// get client opacity
w->has_opacity_prop = wid_get_opacity_prop(c, atoms, wm_ref_win_id(client_win),
OPAQUE, &w->opacity_prop);
return old_has_opacity_prop != w->has_opacity_prop;
}
// TODO(yshui) make WIN_FLAGS_FACTOR_CHANGED more fine-grained, or find a better
// alternative
// way to do all this.
/// 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 win *w) {
// we cannot receive property change when window has been destroyed
assert(w->state != WSTATE_DESTROYED);
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_WINDOW_TYPE)) {
if (win_update_wintype(&ps->c, ps->atoms, w)) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_WINDOW_OPACITY) &&
win_update_opacity_prop(&ps->c, ps->atoms, w, ps->o.detect_client_opacity)) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_FRAME_EXTENTS)) {
auto client_win = win_client_id(w, /*fallback_to_self=*/false);
win_update_frame_extents(&ps->c, ps->atoms, w, client_win, ps->o.frame_opacity);
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->c, ps->atoms, 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->c, ps->atoms, 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->c, ps->atoms, w) == 1) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_COMPTON_SHADOW)) {
if (win_update_prop_shadow(&ps->c, ps->atoms, w)) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_WM_STATE)) {
if (win_update_prop_fullscreen(&ps->c, ps->atoms, w)) {
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
if (ps->o.track_leader &&
(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_fetch_and_unset_property_stale(w, XCB_ATOM_WM_HINTS))) {
auto client_win = win_client_id(w, /*fallback_to_self=*/true);
auto new_leader = win_get_leader_property(&ps->c, ps->atoms, client_win,
ps->o.detect_transient,
ps->o.detect_client_leader);
wm_ref_set_leader(ps->wm, w->tree_ref, new_leader);
}
win_clear_all_properties_stale(w);
}
/// Handle primary flags. These flags are set as direct results of raw X11 window data
/// changes.
void win_process_primary_flags(session_t *ps, struct win *w) {
log_trace("Processing flags for window %#010x (%s), was rendered: %d, flags: "
"%#" PRIx64,
win_id(w), w->name, w->to_paint, w->flags);
if (win_check_flags_all(w, WIN_FLAGS_MAPPED)) {
win_map_start(ps, w);
win_clear_flags(w, WIN_FLAGS_MAPPED);
}
if (w->state != WSTATE_MAPPED) {
// Window is not mapped, so we ignore all its changes until it's mapped
// again.
return;
}
if (win_check_flags_all(w, WIN_FLAGS_CLIENT_STALE)) {
win_on_client_update(ps, w);
win_clear_flags(w, WIN_FLAGS_CLIENT_STALE);
}
if (win_check_flags_any(w, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE)) {
// For damage calculation purposes, we don't care if the window
// is mapped in X server, we only care if we rendered it last
// frame.
//
// We do not process window flags for unmapped windows even when
// it was rendered, so an window fading out won't move even if the
// underlying unmapped window is moved. When the window is
// mapped again when it's still fading out, it should have the
// same effect as a mapped window being moved, meaning we have
// to add both the previous and the new window extents to
// damage.
//
// All that is basically me saying what really matters is if the
// window was rendered last frame, not if it's mapped in X server.
if (w->to_paint) {
// Mark the old extents of this window as damaged. The new
// extents will be marked damaged below, after the window
// extents are updated.
add_damage_from_win(ps, w);
}
// Update window geometry
w->g = w->pending_g;
// Whether a window is fullscreen changes based on its geometry
win_update_is_fullscreen(ps, w);
if (win_check_flags_all(w, WIN_FLAGS_SIZE_STALE)) {
win_on_win_size_change(w, ps->o.shadow_offset_x,
ps->o.shadow_offset_y, ps->o.shadow_radius);
win_update_bounding_shape(&ps->c, w, ps->shape_exists,
ps->o.detect_rounded_corners);
win_clear_flags(w, WIN_FLAGS_SIZE_STALE);
// Window shape/size changed, invalidate the images we built
// log_trace("free out dated pict");
win_set_flags(w, WIN_FLAGS_PIXMAP_STALE |
WIN_FLAGS_FACTOR_CHANGED | WIN_FLAGS_DAMAGED);
win_release_mask(ps->backend_data, w);
win_release_shadow(ps->backend_data, w);
ps->pending_updates = true;
free_paint(ps, &w->paint);
free_paint(ps, &w->shadow_paint);
}
if (win_check_flags_all(w, WIN_FLAGS_POSITION_STALE)) {
win_clear_flags(w, WIN_FLAGS_POSITION_STALE);
win_set_flags(w, WIN_FLAGS_DAMAGED);
}
}
if (win_check_flags_all(w, WIN_FLAGS_PROPERTY_STALE)) {
win_update_properties(ps, w);
win_clear_flags(w, WIN_FLAGS_PROPERTY_STALE);
}
}
/// Handle secondary flags. These flags are set during the processing of primary flags.
/// Flags are separated into primaries and secondaries because processing of secondary
/// flags must happen after primary flags of ALL windows are processed, to make sure some
/// global states (e.g. active window group) are consistent because they will be used in
/// the processing of secondary flags.
void win_process_secondary_flags(session_t *ps, struct win *w) {
if (w->state != WSTATE_MAPPED) {
return;
}
// Handle window focus change. Set appropriate flags if focused states of
// this window changed in the wm tree.
bool new_focused = wm_focused_win(ps->wm) == w->tree_ref;
bool new_group_focused = wm_focused_leader(ps->wm) == wm_ref_leader(w->tree_ref);
if (new_focused != w->is_focused) {
log_debug("Window %#010x (%s) focus state changed from %d to %d",
win_id(w), w->name, w->is_focused, new_focused);
w->is_focused = new_focused;
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
// Send D-Bus signal
if (ps->o.dbus) {
if (new_focused) {
cdbus_ev_win_focusin(session_get_cdbus(ps), w);
} else {
cdbus_ev_win_focusout(session_get_cdbus(ps), w);
}
}
}
if (new_group_focused != w->is_group_focused) {
log_debug("Window %#010x (%s) group focus state changed from %d to %d",
win_id(w), w->name, w->is_group_focused, new_group_focused);
w->is_group_focused = new_group_focused;
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
if (w->flags == 0) {
return;
}
auto old_options = win_options(w);
region_t extents;
pixman_region32_init(&extents);
// Save old window extents. If window goes from having a shadow to not
// having a shadow, we need to add the old, having-shadow extents to
// damage.
win_extents(w, &extents);
// 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);
}
if (win_check_flags_all(w, WIN_FLAGS_DAMAGED)) {
// Add damage, has to be done last so the window has the latest geometry
// information.
add_damage_from_win(ps, w);
win_clear_flags(w, WIN_FLAGS_DAMAGED);
}
auto new_options = win_options(w);
if (win_options_no_damage(&old_options, &new_options)) {
pixman_region32_fini(&extents);
return;
}
add_damage_from_win(ps, w); // Only for legacy backends
if (new_options.shadow != old_options.shadow && !new_options.shadow) {
add_damage(ps, &extents);
win_release_shadow(ps->backend_data, w);
}
pixman_region32_fini(&extents);
}
void win_process_image_flags(session_t *ps, struct win *w) {
// Assert that the MAPPED flag is already handled.
assert(!win_check_flags_all(w, WIN_FLAGS_MAPPED));
if (w->state != WSTATE_MAPPED) {
// Flags of invisible windows are processed when they are mapped
return;
}
if (!win_check_flags_any(w, WIN_FLAGS_PIXMAP_STALE) ||
win_check_flags_all(w, WIN_FLAGS_PIXMAP_ERROR) ||
// We don't need to do anything here for legacy backends
ps->backend_data == NULL) {
win_clear_flags(w, WIN_FLAGS_PIXMAP_STALE);
return;
}
// Image needs to be updated, update it.
win_clear_flags(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.
auto pixmap = x_new_id(&ps->c);
auto e = xcb_request_check(
ps->c.c, xcb_composite_name_window_pixmap_checked(ps->c.c, win_id(w), pixmap));
if (e != NULL) {
log_debug("Failed to get named pixmap for window %#010x(%s): %s. "
"Retaining its current window image",
win_id(w), w->name, x_strerror(e));
free(e);
return;
}
log_debug("New named pixmap for %#010x (%s) : %#010x", win_id(w), w->name, pixmap);
// Must release images first, otherwise breaks NVIDIA driver
win_release_pixmap(ps->backend_data, w);
w->win_image = ps->backend_data->ops.bind_pixmap(
ps->backend_data, pixmap, x_get_visual_info(&ps->c, w->a.visual));
if (!w->win_image) {
log_error("Failed to bind pixmap");
xcb_free_pixmap(ps->c.c, pixmap);
win_set_flags(w, WIN_FLAGS_PIXMAP_ERROR);
}
}
/**
* Check if a window has rounded corners.
* XXX This is really dumb
*/
static bool attr_pure win_has_rounded_corners(const struct 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(struct x_connection *c, struct atom *atoms, struct win *w) {
char **strlst = NULL;
int nstr = 0;
auto client_win = win_client_id(w, /*fallback_to_self=*/true);
if (!(wid_get_text_prop(c, atoms, client_win, atoms->a_NET_WM_NAME, &strlst, &nstr))) {
log_debug("(%#010x): _NET_WM_NAME unset, falling back to WM_NAME.", client_win);
if (!wid_get_text_prop(c, atoms, client_win, atoms->aWM_NAME, &strlst, &nstr)) {
log_debug("Unsetting window name for %#010x", 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", win_id(w),
client_win, w->name, ret);
return ret;
}
int win_update_role(struct x_connection *c, struct atom *atoms, struct win *w) {
char **strlst = NULL;
int nstr = 0;
auto client_win = win_client_id(w, /*fallback_to_self=*/true);
if (!wid_get_text_prop(c, atoms, client_win, 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", win_id(w),
client_win, w->role, ret);
return ret;
}
/**
* Check if a window is bounding-shaped.
*/
static inline bool win_bounding_shaped(struct x_connection *c, xcb_window_t wid) {
xcb_shape_query_extents_reply_t *reply;
bool bounding_shaped;
reply = xcb_shape_query_extents_reply(c->c, xcb_shape_query_extents(c->c, wid), NULL);
bounding_shaped = reply && reply->bounding_shaped;
free(reply);
return bounding_shaped;
}
static wintype_t
wid_get_prop_wintype(struct x_connection *c, struct atom *atoms, xcb_window_t wid) {
winprop_t prop =
x_get_prop(c, wid, 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 (get_atom_with_nul(atoms, WINTYPES[j].atom, c->c) ==
(xcb_atom_t)prop.p32[i]) {
free_winprop(&prop);
return j;
}
}
}
free_winprop(&prop);
return WINTYPE_UNKNOWN;
}
// XXX should distinguish between frame has alpha and window body has alpha
bool win_has_alpha(const struct 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 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_raw(const struct win *w) {
if (win_has_alpha(w)) {
if (wm_ref_client_of(w->tree_ref) == NULL) {
// 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;
}
winmode_t win_calc_mode(const struct win *w) {
if (win_animatable_get(w, WIN_SCRIPT_OPACITY) < 1.0) {
return WMODE_TRANS;
}
return win_calc_mode_raw(w);
}
/**
* 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
*/
static double win_calc_opacity_target(session_t *ps, const struct win *w, bool focused) {
double opacity = 1;
if (w->state == WSTATE_UNMAPPED || w->state == WSTATE_DESTROYED) {
// be consistent
return 0;
}
// Try obeying opacity property and window type opacity firstly
if (w->has_opacity_prop) {
opacity = ((double)w->opacity_prop) / OPAQUE;
} else if (!safe_isnan(w->options.opacity)) {
opacity = w->options.opacity;
} 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 (w->is_focused) {
opacity = ps->o.active_opacity;
} else if (!focused) {
// Respect inactive_opacity in some cases
opacity = ps->o.inactive_opacity;
}
}
// respect inactive override
if (ps->o.inactive_opacity_override && !focused) {
opacity = ps->o.inactive_opacity;
}
return opacity;
}
/// Finish the unmapping of a window (e.g. after fading has finished).
/// Doesn't free `w`
void unmap_win_finish(session_t *ps, struct win *w) {
w->reg_ignore_valid = false;
// We are in unmap_win, this window definitely was viewable
if (ps->backend_data) {
// Only the pixmap needs to be freed and reacquired when mapping.
// Shadow image can be preserved.
win_release_pixmap(ps->backend_data, w);
} else {
assert(!w->win_image);
assert(!w->shadow_image);
}
free_paint(ps, &w->paint);
free_paint(ps, &w->shadow_paint);
// Try again at binding images when the window is mapped next time
if (w->state != WSTATE_DESTROYED) {
win_clear_flags(w, WIN_FLAGS_PIXMAP_ERROR);
}
assert(w->running_animation_instance == NULL);
}
/**
* Determine whether a window is to be dimmed.
*/
static void win_update_dim(session_t *ps, struct win *w, bool focused) {
// Make sure we do nothing if the window is unmapped / being destroyed
if (w->state == WSTATE_UNMAPPED) {
return;
}
if (ps->o.inactive_dim > 0 && !focused) {
w->options.dim = ps->o.inactive_dim;
} else {
w->options.dim = 0;
}
}
/**
* 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(struct x_connection *c, struct atom *atoms, struct win *w) {
winprop_t prop =
x_get_prop(c, win_id(w), 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);
}
/**
* Determine if a window should have shadow, and update things depending
* on shadow state.
*/
static void win_determine_shadow(session_t *ps, struct win *w) {
log_debug("Determining shadow of window %#010x (%s)", win_id(w), w->name);
w->options.shadow = TRI_UNKNOWN;
if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) {
return;
}
if (!ps->o.wintype_option[w->window_type].shadow) {
log_debug("Shadow disabled by wintypes");
w->options.shadow = TRI_FALSE;
} else if (c2_match(ps->c2_state, w, ps->o.shadow_blacklist, NULL)) {
log_debug("Shadow disabled by shadow-exclude");
w->options.shadow = TRI_FALSE;
} else if (ps->o.shadow_ignore_shaped && w->bounding_shaped && !w->rounded_corners) {
log_debug("Shadow disabled by shadow-ignore-shaped");
w->options.shadow = TRI_FALSE;
} else if (w->prop_shadow == 0) {
log_debug("Shadow disabled by shadow property");
w->options.shadow = TRI_FALSE;
}
}
/**
* Reread _COMPTON_SHADOW property from a window and update related
* things.
*/
static bool win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct win *w) {
long long attr_shadow_old = w->prop_shadow;
win_update_prop_shadow_raw(c, atoms, w);
return w->prop_shadow != attr_shadow_old;
}
/**
* Update window EWMH fullscreen state.
*/
bool win_update_prop_fullscreen(struct x_connection *c, const struct atom *atoms,
struct win *w) {
auto prop = x_get_prop(c, win_client_id(w, /*fallback_to_self=*/true),
atoms->a_NET_WM_STATE, 12, XCB_ATOM_ATOM, 0);
bool is_fullscreen = false;
for (uint32_t i = 0; i < prop.nitems; i++) {
if (prop.atom[i] == atoms->a_NET_WM_STATE_FULLSCREEN) {
is_fullscreen = true;
break;
}
}
free_winprop(&prop);
bool changed = w->is_ewmh_fullscreen != is_fullscreen;
w->is_ewmh_fullscreen = is_fullscreen;
return changed;
}
static void win_determine_clip_shadow_above(session_t *ps, struct win *w) {
bool should_crop = (ps->o.wintype_option[w->window_type].clip_shadow_above ||
c2_match(ps->c2_state, w, ps->o.shadow_clip_list, NULL));
w->options.clip_shadow_above = should_crop ? TRI_TRUE : TRI_UNKNOWN;
}
/**
* Determine if a window should have color inverted.
*/
static void win_determine_invert_color(session_t *ps, struct win *w) {
w->options.invert_color = TRI_UNKNOWN;
if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) {
return;
}
if (c2_match(ps->c2_state, w, ps->o.invert_color_list, NULL)) {
w->options.invert_color = TRI_TRUE;
}
}
/**
* Determine if a window should have background blurred.
*/
static void win_determine_blur_background(session_t *ps, struct win *w) {
log_debug("Determining blur-background of window %#010x (%s)", win_id(w), w->name);
w->options.blur_background = TRI_UNKNOWN;
if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) {
return;
}
bool blur_background_new = ps->o.blur_method != BLUR_METHOD_NONE;
if (blur_background_new) {
if (!ps->o.wintype_option[w->window_type].blur_background) {
log_debug("Blur background disabled by wintypes");
w->options.blur_background = TRI_FALSE;
} else if (c2_match(ps->c2_state, w, ps->o.blur_background_blacklist, NULL)) {
log_debug("Blur background disabled by blur-background-exclude");
w->options.blur_background = TRI_FALSE;
}
}
}
/**
* Determine if a window should have rounded corners.
*/
static void win_determine_rounded_corners(session_t *ps, struct win *w) {
void *radius_override = NULL;
bool blacklisted = c2_match(ps->c2_state, w, ps->o.rounded_corners_blacklist, NULL);
if (blacklisted) {
w->options.corner_radius = 0;
return;
}
bool matched = c2_match(ps->c2_state, w, ps->o.corner_radius_rules, &radius_override);
if (matched) {
log_debug("Window %#010x (%s) matched corner rule! %d", win_id(w),
w->name, (int)(long)radius_override);
}
// Don't round full screen windows & excluded windows,
// unless we find a corner override in corner_radius_rules
if (!matched && w->is_fullscreen) {
w->options.corner_radius = 0;
log_debug("Not rounding corners for window %#010x", win_id(w));
} else {
if (matched) {
w->options.corner_radius = (int)(long)radius_override;
} else {
w->options.corner_radius = -1;
}
log_debug("Rounding corners for window %#010x", win_id(w));
// Initialize the border color to an invalid value
w->border_col[0] = w->border_col[1] = w->border_col[2] =
w->border_col[3] = -1.0F;
}
}
/**
* Determine custom window shader to use for a window.
*/
static void win_determine_fg_shader(session_t *ps, struct win *w) {
if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) {
return;
}
void *val = NULL;
w->options.shader = NULL;
if (c2_match(ps->c2_state, w, ps->o.window_shader_fg_rules, &val)) {
struct shader_info *shader = NULL;
HASH_FIND_STR(ps->shaders, val, shader);
w->options.shader = shader;
}
}
/**
* Update window opacity according to opacity rules.
*/
void win_update_opacity_rule(session_t *ps, struct win *w) {
if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) {
return;
}
double opacity = NAN;
void *val = NULL;
if (c2_match(ps->c2_state, w, ps->o.opacity_rules, &val)) {
opacity = ((double)(long)val) / 100.0;
}
w->options.opacity = opacity;
}