forked from pijulius/picom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin.c
2572 lines (2221 loc) · 79.5 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 <[email protected]>
#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 "list.h"
#include "log.h"
#include "picom.h"
#include "region.h"
#include "render.h"
#include "utils.h"
#include "win_defs.h"
#include "wm.h"
#include "x.h"
#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;
// 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.
/**
* Reread opacity property of a window.
*/
static void win_update_opacity_prop(struct x_connection *c, struct atom *atoms,
struct managed_win *w, bool detect_client_opacity);
static void win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms,
struct managed_win *w);
static bool
win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct managed_win *w);
/**
* Update window EWMH fullscreen state.
*/
bool win_update_prop_fullscreen(struct x_connection *c, const struct atom *atoms,
struct managed_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);
static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t client);
/// 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_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 managed_win *w) { \
region_t ret; \
pixman_region32_init(&ret); \
fun(w, &ret); \
return ret; \
}
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);
}
/**
* Update focused state of a window.
*/
static void win_update_focused(session_t *ps, struct managed_win *w) {
if (w->focused_force != UNSET) {
w->focused = w->focused_force;
} else {
bool is_wmwin = win_is_wmwin(w);
w->focused = win_is_focused_raw(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 && is_wmwin) ||
(ps->o.mark_ovredir_focused && w->base.id == w->client_win && !is_wmwin) ||
(w->a.map_state == XCB_MAP_STATE_VIEWABLE &&
c2_match(ps->c2_state, w, ps->o.focus_blacklist, NULL))) {
w->focused = true;
}
// If window grouping detection is enabled, mark the window active if
// its group is
auto active_leader = wm_active_leader(ps->wm);
if (ps->o.track_leader && active_leader &&
win_get_leader(ps, w) == active_leader) {
w->focused = true;
}
}
}
struct group_callback_data {
struct session *ps;
xcb_window_t leader;
};
static inline int group_on_factor_change_callback(struct win *w, void *data_) {
struct group_callback_data *data = data_;
if (!w->managed) {
return 0;
}
auto mw = (struct managed_win *)w;
if (data->leader == win_get_leader(data->ps, mw)) {
win_on_factor_change(data->ps, mw);
}
return 0;
}
/**
* 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;
}
struct group_callback_data data = {
.ps = ps,
.leader = leader,
};
wm_foreach(ps->wm, group_on_factor_change_callback, &data);
}
static inline int group_is_focused_callback(struct win *w, void *data_) {
struct group_callback_data *data = data_;
if (!w->managed) {
return 0;
}
auto mw = (struct managed_win *)w;
if (data->leader == win_get_leader(data->ps, mw) && win_is_focused_raw(mw)) {
return 1;
}
return 0;
}
/**
* 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;
}
struct group_callback_data data = {
.ps = ps,
.leader = leader,
};
return wm_foreach(ps->wm, group_is_focused_callback, &data);
}
/**
* Set leader of a window.
*/
static inline void win_set_leader(session_t *ps, struct managed_win *w, xcb_window_t nleader) {
xcb_window_t cache_leader_old = win_get_leader(ps, w);
w->leader = nleader;
// Forcefully do this to deal with the case when a child window
// gets mapped before parent, or when the window is a waypoint
win_stack_foreach_managed(i, wm_stack_end(ps->wm)) {
i->cache_leader = XCB_NONE;
}
// Update the old and new window group and active_leader if the
// window could affect their state.
xcb_window_t cache_leader = win_get_leader(ps, w);
if (win_is_focused_raw(w) && cache_leader_old != cache_leader) {
wm_set_active_leader(ps->wm, cache_leader);
group_on_factor_change(ps, cache_leader_old);
group_on_factor_change(ps, cache_leader);
}
}
/**
* 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) {
xcb_pixmap_t pixmap = XCB_NONE;
pixmap = 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;
if (pixmap != XCB_NONE) {
xcb_free_pixmap(base->c->c, pixmap);
}
}
}
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);
if (w->shadow_image) {
assert(w->shadow);
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 managed_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);
}
}
}
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->c, xcb_composite_name_window_pixmap_checked(b->c->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));
if (!w->win_image) {
log_error("Failed to bind pixmap");
xcb_free_pixmap(b->c->c, pixmap);
win_set_flags(w, WIN_FLAGS_IMAGE_ERROR);
return false;
}
win_clear_flags(w, WIN_FLAGS_PIXMAP_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);
}
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 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);
// 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 managed_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);
}
if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_FRAME_EXTENTS)) {
win_update_frame_extents(&ps->c, ps->atoms, w, 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 (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_CLIENT_LEADER) ||
win_fetch_and_unset_property_stale(w, ps->atoms->aWM_TRANSIENT_FOR)) {
auto new_leader = win_get_leader_property(&ps->c, ps->atoms, w->client_win,
ps->o.detect_transient,
ps->o.detect_client_leader);
if (w->leader != new_leader) {
win_set_leader(ps, w, new_leader);
win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED);
}
}
win_clear_all_properties_stale(w);
}
static void map_win_start(struct managed_win *w);
/// Handle non-image flags. This phase might set IMAGES_STALE flags
void win_process_update_flags(session_t *ps, struct managed_win *w) {
log_trace("Processing flags for window %#010x (%s), was rendered: %d, flags: "
"%#" PRIx64,
w->base.id, w->name, w->to_paint, w->flags);
if (win_check_flags_all(w, WIN_FLAGS_MAPPED)) {
map_win_start(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;
}
// Check client first, because later property updates need accurate client
// window information
if (win_check_flags_all(w, WIN_FLAGS_CLIENT_STALE)) {
log_debug("Rechecking client window for %#010x (%s)", w->base.id, w->name);
auto client_win = win_get_client_window(&ps->c, ps->wm, ps->atoms, w);
if (w->client_win && w->client_win != client_win) {
win_unmark_client(w);
}
log_debug("New client window for %#010x (%s): %#010x", w->base.id,
w->name, client_win);
win_mark_client(ps, w, client_win);
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)) {
// 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);
damaged = true;
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_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)) {
damaged = true;
win_clear_flags(w, WIN_FLAGS_POSITION_STALE);
}
win_update_monitor(&ps->monitors, 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 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;
}
// Not a loop
while (win_check_flags_any(w, WIN_FLAGS_PIXMAP_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.
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);
}
// break here, loop always run only once
break;
}
// Clear stale image flags
if (win_check_flags_any(w, WIN_FLAGS_PIXMAP_STALE)) {
win_clear_flags(w, WIN_FLAGS_PIXMAP_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(struct x_connection *c, struct atom *atoms, struct managed_win *w) {
char **strlst = NULL;
int nstr = 0;
if (!w->client_win) {
return 0;
}
if (!(wid_get_text_prop(c, atoms, w->client_win, 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(c, atoms, w->client_win, 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;
}
int win_update_role(struct x_connection *c, struct atom *atoms, struct managed_win *w) {
char **strlst = NULL;
int nstr = 0;
if (!wid_get_text_prop(c, atoms, w->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",
w->base.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;
}
static bool wid_get_opacity_prop(struct x_connection *c, struct atom *atoms,
xcb_window_t wid, opacity_t def, opacity_t *out) {
bool ret = false;
*out = def;
winprop_t prop =
x_get_prop(c, wid, 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_raw(const struct managed_win *w) {
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.
if (w->frame_opacity_for_same_colors)
return WMODE_TRANS;
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)) {
if (w->frame_opacity_for_same_colors)
return WMODE_TRANS;
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 managed_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 managed_win *w) {
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 (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(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;
}
/// Finish the unmapping of a window (e.g. after fading has finished).
/// Doesn't free `w`
void unmap_win_finish(session_t *ps, struct managed_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.
if (!win_check_flags_all(w, WIN_FLAGS_PIXMAP_NONE)) {
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_IMAGE_ERROR);
}
assert(w->running_animation == NULL);
}
struct window_transition_data {
struct managed_win *w;
session_t *ps;
// TODO(yshui) switch to only pass backend_data after the legacy backend removal
// struct backend_base *backend_data;
uint64_t refcount;
};
/**
* 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;
}
return false;
}
/**
* 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 managed_win *w) {
winprop_t prop =
x_get_prop(c, w->base.id, 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_MAPPED);
// 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);
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);
if (shadow_new) {
// Mark the new extents as damaged if the shadow is added
assert(!w->shadow_image);
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
add_damage(ps, &extents);
win_release_shadow(ps->backend_data, w);
}
// 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;
}
w->shadow = shadow_new;
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) {