AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylandbridge.sgl

1;;; Run in an owned headless compositor with the same input driver as live.sgl.
2(import (sigil test) (sigil shell prelude) (sigil ui raster) (sigil ui wayland)
3 (sigil ui window))
4
5(define bridge-shots (getenv "SIGIL_UI_TEST_SHOTS"))
6(define bridge-driver-command (getenv "SIGIL_UI_TEST_DRIVER"))
7(define bridge-driver-arguments
8 (if (file? bridge-driver-command) (list bridge-driver-command) (split bridge-driver-command " ")))
9(define bridge-driver
10 (apply process-spawn (car bridge-driver-arguments)
11 (append (cdr bridge-driver-arguments) (list die-with-parent: #t))))
12(define bridge-driver-in (process-stdin bridge-driver))
13(define bridge-driver-out (process-stdout bridge-driver))
14(unless (starts-with? (read-line bridge-driver-out) "INPUT-READY")
15 (error "Bridge input driver did not start"))
16(define (bridge-drive . words)
17 (display (string-join words " ") bridge-driver-in)
18 (newline bridge-driver-in)
19 (flush-output-port bridge-driver-in)
20 (unless (starts-with? (read-line bridge-driver-out) "ACK ")
21 (error "Bridge input driver did not acknowledge" words)))
22(define (bridge-frame surface)
23 (define found #f)
24 (wait-until
25 (begin
26 (each event (wayland-dispatch surface 20)
27 (when (eq? (car event) 'frame) (set! found #t)))
28 found)
29 timeout: 3 interval: 0.001 message: "Subsurface frame callback was not delivered"))
30(define (bridge-pump surface)
31 (let loop ((left 20))
32 (when (> left 0) (wayland-dispatch surface 10) (loop (- left 1)))))
33(define (bridge-pixel bytes width x y)
34 (define offset (* 3 (+ x (* width y))))
35 (+ (* 65536 (bytevector-u8-ref bytes offset))
36 (* 256 (bytevector-u8-ref bytes (+ offset 1)))
37 (bytevector-u8-ref bytes (+ offset 2))))
38(define (bridge-shot)
39 (define file (path bridge-shots "bridge.ppm"))
40 ($ grim -t ppm -o HEADLESS-1 ,file timeout: 5)
41 (define bytes (read-file-bytes file))
42 (let loop ((index 2) (fields '()) (number #f))
43 (define byte (bytevector-u8-ref bytes index))
44 (cond
45 ((<= 48 byte 57) (loop (+ index 1) fields (+ (* 10 (or number 0)) (- byte 48))))
46 (number
47 (if (= (length fields) 2)
48 (list (car fields) (cadr fields) (bytevector-copy bytes (+ index 1) (bytevector-length bytes)))
49 (loop (+ index 1) (append fields (list number)) #f)))
50 (else (loop (+ index 1) fields #f)))))
52(test-group "native embedding bridge"
53 (test "dispatch after teardown reports close without flushing a deferred child canvas"
54 (define parent (open-wayland-surface title: "Bridge deferred close"))
55 (define page (open-wayland-subsurface parent width: 16 height: 16 overlay?: #f))
56 (dynamic-wind
57 (lambda () #f)
58 (lambda ()
59 (wayland-present! parent
60 (make-canvas (wayland-surface-device-width parent) (wayland-surface-device-height parent)))
61 (define image (make-canvas 16 16 #x208040))
62 (wayland-present! page image)
63 (assert-false (wayland-present! page image))
64 (wayland-surface-close! parent)
65 (assert-equal '((close)) (wayland-dispatch page 0))
66 (assert-equal '((close)) (wayland-dispatch parent 0)))
67 (lambda () (wayland-surface-close! parent))))
69 (test "transparent chrome, page and alpha overlay have separate frame callbacks"
70 (define parent (open-wayland-surface transparent?: #t title: "Bridge composition"))
71 (dynamic-wind
72 (lambda () #f)
73 (lambda ()
74 ;; Mapping a tiled toplevel may configure it again. Settle that before
75 ;; choosing chrome/page dimensions and masks for the actual size.
76 (wayland-present! parent
77 (make-canvas (wayland-surface-device-width parent) (wayland-surface-device-height parent)))
78 (bridge-frame parent)
79 (define width (wayland-surface-device-width parent))
80 (define height (wayland-surface-device-height parent))
81 (define chrome (make-canvas width height #x204080))
82 (define alpha (make-bytevector (* width height) 0))
83 ;; Keep an opaque strip of chrome across the top; the page is below it.
84 (let loop ((index 0))
85 (when (< index (* width 16))
86 (bytevector-u8-set! alpha index 255)
87 (loop (+ index 1))))
88 (wayland-set-alpha! parent alpha)
89 (wayland-present! parent chrome)
90 (bridge-frame parent)
91 (define page (open-wayland-subsurface parent width: width height: height overlay?: #f))
92 (wayland-present! page (make-canvas width height #x208040))
93 (bridge-frame page)
94 (define overlay (open-wayland-subsurface parent x: 32 y: 32 width: 64 height: 32))
95 (define mask (make-bytevector (* 64 32) 0))
96 (let loop ((index 0))
97 (when (< index (* 64 16))
98 (bytevector-u8-set! mask index 255)
99 (loop (+ index 1))))
100 (wayland-set-alpha! overlay mask)
101 (wayland-present! overlay (make-canvas 64 32 #xff0000))
102 (bridge-frame overlay)
103 (define shot (bridge-shot))
104 (define pixels (caddr shot))
105 (assert-equal #x204080 (bridge-pixel pixels (car shot) 40 4))
106 (assert-equal #x208040 (bridge-pixel pixels (car shot) 8 80))
107 (assert-equal #xff0000 (bridge-pixel pixels (car shot) 40 36))
108 (assert-equal #x208040 (bridge-pixel pixels (car shot) 40 56))
109 (bridge-drive "move" "40" "36")
110 (bridge-drive "button" "272" "1")
111 (bridge-drive "button" "272" "0")
112 (define buttons '())
113 (wait-until
114 (begin
115 (set! buttons (append buttons (filter (lambda (event) (eq? (car event) 'pointer-button))
116 (wayland-dispatch parent 20))))
117 (>= (length buttons) 2))
118 timeout: 2 interval: 0.001)
119 (assert-equal '(pointer-button press left 40 36) (car buttons))
120 (assert-false (cdr (assq 'frame-pending? (wayland-surface-buffer-state overlay))))
121 ;; Parent-first close must invalidate both children, and be repeatable.
122 (wayland-surface-close! parent)
123 (assert-true (wayland-surface-closed? page))
124 (assert-true (wayland-surface-closed? overlay))
125 (wayland-surface-close! page)
126 (wayland-surface-close! overlay))
127 (lambda () (wayland-surface-close! parent))))
129 (test "compositor layout updates and text clipboard serve another client"
130 (define parent (open-wayland-surface title: "Bridge clipboard"))
131 (define reader #f)
132 (define owner #f)
133 (dynamic-wind
134 (lambda () #f)
135 (lambda ()
136 (assert-false (wayland-clipboard-set! parent "before input"))
137 (assert-false (window-clipboard-set! parent "before input"))
138 (wayland-present! parent
139 (make-canvas (wayland-surface-device-width parent) (wayland-surface-device-height parent) #x102030))
140 (bridge-frame parent)
141 (bridge-drive "keymap" "us")
142 (bridge-drive "key" "21")
143 (bridge-pump parent)
144 (define before (wayland-keymap parent))
145 (assert-true (string? before))
146 (bridge-drive "keymap" "de")
147 (bridge-drive "key" "21")
148 (bridge-pump parent)
149 (assert-false (equal? before (wayland-keymap parent)))
150 (define text (str "Unicode clipboard: λ — " (make-string 131072 #\x)))
151 (assert-true (window-clipboard-set! parent ""))
152 (assert-equal "" (window-clipboard-read parent))
153 (assert-true (window-clipboard-set! parent text))
154 (assert-equal text (window-clipboard-read parent))
155 (assert-equal text (wayland-clipboard-read parent))
156 (define log (path bridge-shots "clipboard-reader.log"))
157 (set! reader (spawn wl-paste --no-newline out: ,log err: ,(str log ".err")))
158 (wait-until
159 (begin (wayland-dispatch parent 10) (not (process-alive? reader)))
160 timeout: 5 interval: 0.001 message: "External clipboard reader did not finish")
161 (assert-equal 0 (process-wait reader))
162 (assert-equal text (read-text log))
163 (define external (str "External Unicode clipboard: λ " (make-string 131072 #\y)))
164 (define input (path bridge-shots "clipboard-owner.txt"))
165 (write-text input external)
166 (set! owner (spawn wl-copy --foreground --type "text/plain;charset=utf-8"
167 in: ,input out: ,(path bridge-shots "clipboard-owner.log") err: 'merge))
168 (wait-until
169 (begin (wayland-dispatch parent 20) (equal? external (wayland-clipboard-read parent)))
170 timeout: 5 interval: 0.01 message: "External clipboard ownership was not observed")
171 (assert-equal external (window-clipboard-read parent))
172 ;; Keep a real owner and offer alive while preventing it from serving
173 ;; the new receive request. The incoming pipe must time out and close.
174 (process-signal! owner 'stop)
175 (define descriptors (length (directory-list "/proc/self/fd")))
176 (define started (current-jiffy))
177 (assert-false (wayland-clipboard-read parent))
178 (define elapsed (/ (- (current-jiffy) started) (jiffies-per-second)))
179 (assert-true (and (>= elapsed 1.9) (< elapsed 2.75)))
180 (assert-equal descriptors (length (directory-list "/proc/self/fd")))
181 (process-signal! owner 'cont)
182 (assert-false (wayland-clipboard-set! parent (make-string 4194305 #\x)))
183 (assert-false (window-clipboard-set! parent (make-string 4194305 #\x)))
184 (bridge-drive "keymap" "us"))
185 (lambda ()
186 (when reader
187 (when (process-alive? reader) (process-signal! reader 'kill))
188 (process-wait reader))
189 (when owner
190 (when (process-alive? owner) (process-signal! owner 'kill))
191 (process-wait owner))
192 (wayland-surface-close! parent))))
194 (test "an overlay uses its parent's scale for buffers and logical position"
195 (out swaymsg output HEADLESS-1 scale 2 timeout: 5)
196 (define parent (open-wayland-surface title: "Bridge scale"))
197 (dynamic-wind
198 (lambda () #f)
199 (lambda ()
200 (wayland-present! parent
201 (make-canvas (wayland-surface-device-width parent) (wayland-surface-device-height parent) #x204080))
202 (bridge-frame parent)
203 (define overlay (open-wayland-subsurface parent x: 16 y: 16 width: 16 height: 16))
204 (assert-equal 'subsurface (wayland-surface-role overlay))
205 (assert-equal 2 (wayland-surface-scale overlay))
206 (wayland-present! overlay (make-canvas 32 32 #xff0000))
207 (bridge-frame overlay)
208 (define shot (bridge-shot))
209 (assert-equal #xff0000 (bridge-pixel (caddr shot) (car shot) 40 40))
210 (wayland-surface-close! overlay))
211 (lambda ()
212 (wayland-surface-close! parent)
213 (out swaymsg output HEADLESS-1 scale 1 timeout: 5))))
215 (test "a queued compositor release precedes an overdue synthetic repeat"
216 (out swaymsg input "type:keyboard" repeat_delay 50 timeout: 5)
217 (define parent (open-wayland-surface title: "Bridge repeat ordering"))
218 (dynamic-wind
219 (lambda () #f)
220 (lambda ()
221 (wayland-present! parent
222 (make-canvas (wayland-surface-device-width parent) (wayland-surface-device-height parent)))
223 (bridge-frame parent)
224 (bridge-drive "press" "30")
225 (define pressed #f)
226 (wait-until
227 (begin
228 (each event (wayland-dispatch parent 10)
229 (when (and (eq? (car event) 'key) (eq? (cadr event) 'press))
230 (set! pressed #t)))
231 pressed)
232 timeout: 2 interval: 0.001)
233 (bridge-drive "release" "30")
234 (sleep 0.15)
235 (define events (wayland-dispatch parent 0))
236 (assert-true (pair? (filter (lambda (event)
237 (and (eq? (car event) 'key) (eq? (cadr event) 'release))) events)))
238 (assert-equal '() (filter (lambda (event)
239 (and (eq? (car event) 'key) (eq? (cadr event) 'repeat))) events)))
240 (lambda ()
241 (bridge-drive "release" "30")
242 (out swaymsg input "type:keyboard" repeat_delay 200 timeout: 5)
243 (wayland-surface-close! parent)))))