-
Notifications
You must be signed in to change notification settings - Fork 1
/
os_wayland.go
1785 lines (1650 loc) · 43.9 KB
/
os_wayland.go
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: Unlicense OR MIT
//go:build (linux && !android && !nowayland) || freebsd
// +build linux,!android,!nowayland freebsd
package wm
import (
"bytes"
"errors"
"fmt"
"image"
"io"
"io/ioutil"
"math"
"os"
"os/exec"
"strconv"
"sync"
"time"
"unsafe"
syscall "golang.org/x/sys/unix"
"github.com/cybriq/p9/pkg/gel/gio/app/internal/xkb"
"github.com/cybriq/p9/pkg/gel/gio/f32"
"github.com/cybriq/p9/pkg/gel/gio/internal/fling"
"github.com/cybriq/p9/pkg/gel/gio/io/clipboard"
"github.com/cybriq/p9/pkg/gel/gio/io/key"
"github.com/cybriq/p9/pkg/gel/gio/io/pointer"
"github.com/cybriq/p9/pkg/gel/gio/io/system"
"github.com/cybriq/p9/pkg/gel/gio/unit"
)
// Use wayland-scanner to generate glue code for the xdg-shell and xdg-decoration extensions.
//go:generate wayland-scanner client-header /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml wayland_xdg_shell.h
//go:generate wayland-scanner private-code /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml wayland_xdg_shell.c
//go:generate wayland-scanner client-header /usr/share/wayland-protocols/unstable/text-input/text-input-unstable-v3.xml wayland_text_input.h
//go:generate wayland-scanner private-code /usr/share/wayland-protocols/unstable/text-input/text-input-unstable-v3.xml wayland_text_input.c
//go:generate wayland-scanner client-header /usr/share/wayland-protocols/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml wayland_xdg_decoration.h
//go:generate wayland-scanner private-code /usr/share/wayland-protocols/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml wayland_xdg_decoration.c
//go:generate sed -i "1s;^;// +build linux,!android,!nowayland freebsd\\n\\n;" wayland_xdg_shell.c
//go:generate sed -i "1s;^;// +build linux,!android,!nowayland freebsd\\n\\n;" wayland_xdg_decoration.c
//go:generate sed -i "1s;^;// +build linux,!android,!nowayland freebsd\\n\\n;" wayland_text_input.c
/*
#cgo linux pkg-config: wayland-client wayland-cursor
#cgo freebsd openbsd LDFLAGS: -lwayland-client -lwayland-cursor
#cgo freebsd CFLAGS: -I/usr/local/include
#cgo freebsd LDFLAGS: -L/usr/local/lib
#include <stdlib.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include "wayland_text_input.h"
#include "wayland_xdg_shell.h"
#include "wayland_xdg_decoration.h"
extern const struct wl_registry_listener gio_registry_listener;
extern const struct wl_surface_listener gio_surface_listener;
extern const struct xdg_surface_listener gio_xdg_surface_listener;
extern const struct xdg_toplevel_listener gio_xdg_toplevel_listener;
extern const struct xdg_wm_base_listener gio_xdg_wm_base_listener;
extern const struct wl_callback_listener gio_callback_listener;
extern const struct wl_output_listener gio_output_listener;
extern const struct wl_seat_listener gio_seat_listener;
extern const struct wl_pointer_listener gio_pointer_listener;
extern const struct wl_touch_listener gio_touch_listener;
extern const struct wl_keyboard_listener gio_keyboard_listener;
extern const struct zwp_text_input_v3_listener gio_zwp_text_input_v3_listener;
extern const struct wl_data_device_listener gio_data_device_listener;
extern const struct wl_data_offer_listener gio_data_offer_listener;
extern const struct wl_data_source_listener gio_data_source_listener;
*/
import "C"
type wlDisplay struct {
disp *C.struct_wl_display
reg *C.struct_wl_registry
compositor *C.struct_wl_compositor
wm *C.struct_xdg_wm_base
imm *C.struct_zwp_text_input_manager_v3
shm *C.struct_wl_shm
dataDeviceManager *C.struct_wl_data_device_manager
decor *C.struct_zxdg_decoration_manager_v1
seat *wlSeat
xkb *xkb.Context
outputMap map[C.uint32_t]*C.struct_wl_output
outputConfig map[*C.struct_wl_output]*wlOutput
// Notification pipe fds.
notify struct {
read, write int
}
repeat repeatState
}
type wlSeat struct {
disp *wlDisplay
seat *C.struct_wl_seat
name C.uint32_t
pointer *C.struct_wl_pointer
touch *C.struct_wl_touch
keyboard *C.struct_wl_keyboard
im *C.struct_zwp_text_input_v3
// The most recent input serial.
serial C.uint32_t
pointerFocus *window
keyboardFocus *window
touchFoci map[C.int32_t]*window
// Clipboard support.
dataDev *C.struct_wl_data_device
// offers is a map from active wl_data_offers to
// the list of mime types they support.
offers map[*C.struct_wl_data_offer][]string
// clipboard is the wl_data_offer for the clipboard.
clipboard *C.struct_wl_data_offer
// mimeType is the chosen mime type of clipboard.
mimeType string
// source represents the clipboard content of the most recent
// clipboard write, if any.
source *C.struct_wl_data_source
// content is the data belonging to source.
content []byte
}
type repeatState struct {
rate int
delay time.Duration
key uint32
win Callbacks
stopC chan struct{}
start time.Duration
last time.Duration
mu sync.Mutex
now time.Duration
}
type window struct {
w Callbacks
disp *wlDisplay
surf *C.struct_wl_surface
wmSurf *C.struct_xdg_surface
topLvl *C.struct_xdg_toplevel
decor *C.struct_zxdg_toplevel_decoration_v1
ppdp, ppsp float32
scroll struct {
time time.Duration
steps image.Point
dist f32.Point
}
pointerBtns pointer.Buttons
lastPos f32.Point
lastTouch f32.Point
cursor struct {
theme *C.struct_wl_cursor_theme
cursor *C.struct_wl_cursor
surf *C.struct_wl_surface
}
fling struct {
yExtrapolation fling.Extrapolation
xExtrapolation fling.Extrapolation
anim fling.Animation
start bool
dir f32.Point
}
stage system.Stage
dead bool
lastFrameCallback *C.struct_wl_callback
mu sync.Mutex
animating bool
opts *Options
needAck bool
// The most recent configure serial waiting to be ack'ed.
serial C.uint32_t
width int
height int
newScale bool
scale int
// readClipboard tracks whether a ClipboardEvent is requested.
readClipboard bool
// writeClipboard is set whenever a clipboard write is requested.
writeClipboard *string
}
type poller struct {
pollfds [2]syscall.PollFd
// buf is scratch space for draining the notification pipe.
buf [100]byte
}
type wlOutput struct {
width int
height int
physWidth int
physHeight int
transform C.int32_t
scale int
windows []*window
}
// callbackMap maps Wayland native handles to corresponding Go
// references. It is necessary because the the Wayland client API
// forces the use of callbacks and storing pointers to Go values
// in C is forbidden.
var callbackMap sync.Map
// clipboardMimeTypes is a list of supported clipboard mime types, in
// order of preference.
var clipboardMimeTypes = []string{"text/plain;charset=utf8", "UTF8_STRING",
"text/plain", "TEXT", "STRING",
}
func init() {
wlDriver = newWLWindow
}
func newWLWindow(window Callbacks, opts *Options) error {
d, err := newWLDisplay()
if err != nil {
return err
}
w, err := d.createNativeWindow(opts)
if err != nil {
d.destroy()
return err
}
w.w = window
go func() {
defer d.destroy()
defer w.destroy()
w.w.SetDriver(w)
if err := w.loop(); err != nil {
panic(err)
}
}()
return nil
}
func (d *wlDisplay) writeClipboard(content []byte) error {
s := d.seat
if s == nil {
return nil
}
// Clear old offer.
if s.source != nil {
C.wl_data_source_destroy(s.source)
s.source = nil
s.content = nil
}
if d.dataDeviceManager == nil || s.dataDev == nil {
return nil
}
s.content = content
s.source = C.wl_data_device_manager_create_data_source(d.dataDeviceManager)
C.wl_data_source_add_listener(s.source, &C.gio_data_source_listener,
unsafe.Pointer(s.seat),
)
for _, mime := range clipboardMimeTypes {
C.wl_data_source_offer(s.source, C.CString(mime))
}
C.wl_data_device_set_selection(s.dataDev, s.source, s.serial)
return nil
}
func (d *wlDisplay) readClipboard() (io.ReadCloser, error) {
s := d.seat
if s == nil {
return nil, nil
}
if s.clipboard == nil {
return nil, nil
}
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
// wl_data_offer_receive performs and implicit dup(2) of the write end
// of the pipe. Close our version.
defer w.Close()
cmimeType := C.CString(s.mimeType)
defer C.free(unsafe.Pointer(cmimeType))
C.wl_data_offer_receive(s.clipboard, cmimeType, C.int(w.Fd()))
return r, nil
}
func (d *wlDisplay) createNativeWindow(opts *Options) (*window, error) {
if d.compositor == nil {
return nil, errors.New("wayland: no compositor available")
}
if d.wm == nil {
return nil, errors.New("wayland: no xdg_wm_base available")
}
if d.shm == nil {
return nil, errors.New("wayland: no wl_shm available")
}
if len(d.outputMap) == 0 {
return nil, errors.New("wayland: no outputs available")
}
var scale int
for _, conf := range d.outputConfig {
if s := conf.scale; s > scale {
scale = s
}
}
ppdp := detectUIScale()
w := &window{
disp: d,
scale: scale,
newScale: scale != 1,
ppdp: ppdp,
ppsp: ppdp,
}
w.surf = C.wl_compositor_create_surface(d.compositor)
if w.surf == nil {
w.destroy()
return nil, errors.New("wayland: wl_compositor_create_surface failed")
}
callbackStore(unsafe.Pointer(w.surf), w)
w.wmSurf = C.xdg_wm_base_get_xdg_surface(d.wm, w.surf)
if w.wmSurf == nil {
w.destroy()
return nil, errors.New("wayland: xdg_wm_base_get_xdg_surface failed")
}
w.topLvl = C.xdg_surface_get_toplevel(w.wmSurf)
if w.topLvl == nil {
w.destroy()
return nil, errors.New("wayland: xdg_surface_get_toplevel failed")
}
w.cursor.theme = C.wl_cursor_theme_load(nil, 32, d.shm)
if w.cursor.theme == nil {
w.destroy()
return nil, errors.New("wayland: wl_cursor_theme_load failed")
}
cname := C.CString("left_ptr")
defer C.free(unsafe.Pointer(cname))
w.cursor.cursor = C.wl_cursor_theme_get_cursor(w.cursor.theme, cname)
if w.cursor.cursor == nil {
w.destroy()
return nil, errors.New("wayland: wl_cursor_theme_get_cursor failed")
}
w.cursor.surf = C.wl_compositor_create_surface(d.compositor)
if w.cursor.surf == nil {
w.destroy()
return nil, errors.New("wayland: wl_compositor_create_surface failed")
}
C.xdg_wm_base_add_listener(d.wm, &C.gio_xdg_wm_base_listener,
unsafe.Pointer(w.surf),
)
C.wl_surface_add_listener(w.surf, &C.gio_surface_listener,
unsafe.Pointer(w.surf),
)
C.xdg_surface_add_listener(w.wmSurf, &C.gio_xdg_surface_listener,
unsafe.Pointer(w.surf),
)
C.xdg_toplevel_add_listener(w.topLvl, &C.gio_xdg_toplevel_listener,
unsafe.Pointer(w.surf),
)
w.setOptions(opts)
if d.decor != nil {
// Request server side decorations.
w.decor = C.zxdg_decoration_manager_v1_get_toplevel_decoration(d.decor,
w.topLvl,
)
C.zxdg_toplevel_decoration_v1_set_mode(w.decor,
C.ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE,
)
}
w.updateOpaqueRegion()
C.wl_surface_commit(w.surf)
return w, nil
}
func callbackDelete(k unsafe.Pointer) {
callbackMap.Delete(k)
}
func callbackStore(k unsafe.Pointer, v interface{}) {
callbackMap.Store(k, v)
}
func callbackLoad(k unsafe.Pointer) interface{} {
v, exists := callbackMap.Load(k)
if !exists {
panic("missing callback entry")
}
return v
}
//export gio_onSeatCapabilities
func gio_onSeatCapabilities(data unsafe.Pointer, seat *C.struct_wl_seat,
caps C.uint32_t,
) {
s := callbackLoad(data).(*wlSeat)
s.updateCaps(caps)
}
// flushOffers remove all wl_data_offers that isn't the clipboard
// content.
func (s *wlSeat) flushOffers() {
for o := range s.offers {
if o == s.clipboard {
continue
}
// We're only interested in clipboard offers.
delete(s.offers, o)
callbackDelete(unsafe.Pointer(o))
C.wl_data_offer_destroy(o)
}
}
func (s *wlSeat) destroy() {
if s.source != nil {
C.wl_data_source_destroy(s.source)
s.source = nil
}
if s.im != nil {
C.zwp_text_input_v3_destroy(s.im)
s.im = nil
}
if s.pointer != nil {
C.wl_pointer_release(s.pointer)
}
if s.touch != nil {
C.wl_touch_release(s.touch)
}
if s.keyboard != nil {
C.wl_keyboard_release(s.keyboard)
}
s.clipboard = nil
s.flushOffers()
if s.dataDev != nil {
C.wl_data_device_release(s.dataDev)
}
if s.seat != nil {
callbackDelete(unsafe.Pointer(s.seat))
C.wl_seat_release(s.seat)
}
}
func (s *wlSeat) updateCaps(caps C.uint32_t) {
if s.im == nil && s.disp.imm != nil {
s.im = C.zwp_text_input_manager_v3_get_text_input(s.disp.imm, s.seat)
C.zwp_text_input_v3_add_listener(s.im,
&C.gio_zwp_text_input_v3_listener, unsafe.Pointer(s.seat),
)
}
switch {
case s.pointer == nil && caps&C.WL_SEAT_CAPABILITY_POINTER != 0:
s.pointer = C.wl_seat_get_pointer(s.seat)
C.wl_pointer_add_listener(s.pointer, &C.gio_pointer_listener,
unsafe.Pointer(s.seat),
)
case s.pointer != nil && caps&C.WL_SEAT_CAPABILITY_POINTER == 0:
C.wl_pointer_release(s.pointer)
s.pointer = nil
}
switch {
case s.touch == nil && caps&C.WL_SEAT_CAPABILITY_TOUCH != 0:
s.touch = C.wl_seat_get_touch(s.seat)
C.wl_touch_add_listener(s.touch, &C.gio_touch_listener,
unsafe.Pointer(s.seat),
)
case s.touch != nil && caps&C.WL_SEAT_CAPABILITY_TOUCH == 0:
C.wl_touch_release(s.touch)
s.touch = nil
}
switch {
case s.keyboard == nil && caps&C.WL_SEAT_CAPABILITY_KEYBOARD != 0:
s.keyboard = C.wl_seat_get_keyboard(s.seat)
C.wl_keyboard_add_listener(s.keyboard, &C.gio_keyboard_listener,
unsafe.Pointer(s.seat),
)
case s.keyboard != nil && caps&C.WL_SEAT_CAPABILITY_KEYBOARD == 0:
C.wl_keyboard_release(s.keyboard)
s.keyboard = nil
}
}
//export gio_onSeatName
func gio_onSeatName(data unsafe.Pointer, seat *C.struct_wl_seat, name *C.char) {
}
//export gio_onXdgSurfaceConfigure
func gio_onXdgSurfaceConfigure(data unsafe.Pointer,
wmSurf *C.struct_xdg_surface, serial C.uint32_t,
) {
w := callbackLoad(data).(*window)
w.mu.Lock()
w.serial = serial
w.needAck = true
w.mu.Unlock()
w.setStage(system.StageRunning)
w.draw(true)
}
//export gio_onToplevelClose
func gio_onToplevelClose(data unsafe.Pointer, topLvl *C.struct_xdg_toplevel) {
w := callbackLoad(data).(*window)
w.dead = true
}
//export gio_onToplevelConfigure
func gio_onToplevelConfigure(data unsafe.Pointer, topLvl *C.struct_xdg_toplevel,
width, height C.int32_t, states *C.struct_wl_array,
) {
w := callbackLoad(data).(*window)
if width != 0 && height != 0 {
w.mu.Lock()
defer w.mu.Unlock()
w.width = int(width)
w.height = int(height)
w.updateOpaqueRegion()
}
}
//export gio_onOutputMode
func gio_onOutputMode(data unsafe.Pointer, output *C.struct_wl_output,
flags C.uint32_t, width, height, refresh C.int32_t,
) {
if flags&C.WL_OUTPUT_MODE_CURRENT == 0 {
return
}
d := callbackLoad(data).(*wlDisplay)
c := d.outputConfig[output]
c.width = int(width)
c.height = int(height)
}
//export gio_onOutputGeometry
func gio_onOutputGeometry(data unsafe.Pointer, output *C.struct_wl_output,
x, y, physWidth, physHeight, subpixel C.int32_t, make, model *C.char,
transform C.int32_t,
) {
d := callbackLoad(data).(*wlDisplay)
c := d.outputConfig[output]
c.transform = transform
c.physWidth = int(physWidth)
c.physHeight = int(physHeight)
}
//export gio_onOutputScale
func gio_onOutputScale(data unsafe.Pointer, output *C.struct_wl_output,
scale C.int32_t,
) {
d := callbackLoad(data).(*wlDisplay)
c := d.outputConfig[output]
c.scale = int(scale)
}
//export gio_onOutputDone
func gio_onOutputDone(data unsafe.Pointer, output *C.struct_wl_output) {
d := callbackLoad(data).(*wlDisplay)
conf := d.outputConfig[output]
for _, w := range conf.windows {
w.draw(true)
}
}
//export gio_onSurfaceEnter
func gio_onSurfaceEnter(data unsafe.Pointer, surf *C.struct_wl_surface,
output *C.struct_wl_output,
) {
w := callbackLoad(data).(*window)
conf := w.disp.outputConfig[output]
var found bool
for _, w2 := range conf.windows {
if w2 == w {
found = true
break
}
}
if !found {
conf.windows = append(conf.windows, w)
}
w.updateOutputs()
}
//export gio_onSurfaceLeave
func gio_onSurfaceLeave(data unsafe.Pointer, surf *C.struct_wl_surface,
output *C.struct_wl_output,
) {
w := callbackLoad(data).(*window)
conf := w.disp.outputConfig[output]
for i, w2 := range conf.windows {
if w2 == w {
conf.windows = append(conf.windows[:i], conf.windows[i+1:]...)
break
}
}
w.updateOutputs()
}
//export gio_onRegistryGlobal
func gio_onRegistryGlobal(data unsafe.Pointer, reg *C.struct_wl_registry,
name C.uint32_t, cintf *C.char, version C.uint32_t,
) {
d := callbackLoad(data).(*wlDisplay)
switch C.GoString(cintf) {
case "wl_compositor":
d.compositor = (*C.struct_wl_compositor)(C.wl_registry_bind(reg, name,
&C.wl_compositor_interface, 3,
),
)
case "wl_output":
output := (*C.struct_wl_output)(C.wl_registry_bind(reg, name,
&C.wl_output_interface, 2,
),
)
C.wl_output_add_listener(output, &C.gio_output_listener,
unsafe.Pointer(d.disp),
)
d.outputMap[name] = output
d.outputConfig[output] = new(wlOutput)
case "wl_seat":
if d.seat != nil {
break
}
s := (*C.struct_wl_seat)(C.wl_registry_bind(reg, name,
&C.wl_seat_interface, 5,
),
)
if s == nil {
// No support for v5 protocol.
break
}
d.seat = &wlSeat{
disp: d,
name: name,
seat: s,
offers: make(map[*C.struct_wl_data_offer][]string),
touchFoci: make(map[C.int32_t]*window),
}
callbackStore(unsafe.Pointer(s), d.seat)
C.wl_seat_add_listener(s, &C.gio_seat_listener, unsafe.Pointer(s))
if d.dataDeviceManager == nil {
break
}
d.seat.dataDev = C.wl_data_device_manager_get_data_device(d.dataDeviceManager,
s,
)
if d.seat.dataDev == nil {
break
}
callbackStore(unsafe.Pointer(d.seat.dataDev), d.seat)
C.wl_data_device_add_listener(d.seat.dataDev,
&C.gio_data_device_listener, unsafe.Pointer(d.seat.dataDev),
)
case "wl_shm":
d.shm = (*C.struct_wl_shm)(C.wl_registry_bind(reg, name,
&C.wl_shm_interface, 1,
),
)
case "xdg_wm_base":
d.wm = (*C.struct_xdg_wm_base)(C.wl_registry_bind(reg, name,
&C.xdg_wm_base_interface, 1,
),
)
case "zxdg_decoration_manager_v1":
d.decor = (*C.struct_zxdg_decoration_manager_v1)(C.wl_registry_bind(reg,
name, &C.zxdg_decoration_manager_v1_interface, 1,
),
)
// TODO: Implement and test text-input support.
/*case "zwp_text_input_manager_v3":
d.imm = (*C.struct_zwp_text_input_manager_v3)(C.wl_registry_bind(reg, name, &C.zwp_text_input_manager_v3_interface, 1))*/
case "wl_data_device_manager":
d.dataDeviceManager = (*C.struct_wl_data_device_manager)(C.wl_registry_bind(reg,
name, &C.wl_data_device_manager_interface, 3,
),
)
}
}
//export gio_onDataOfferOffer
func gio_onDataOfferOffer(data unsafe.Pointer, offer *C.struct_wl_data_offer,
mime *C.char,
) {
s := callbackLoad(data).(*wlSeat)
s.offers[offer] = append(s.offers[offer], C.GoString(mime))
}
//export gio_onDataOfferSourceActions
func gio_onDataOfferSourceActions(data unsafe.Pointer,
offer *C.struct_wl_data_offer, acts C.uint32_t,
) {
}
//export gio_onDataOfferAction
func gio_onDataOfferAction(data unsafe.Pointer, offer *C.struct_wl_data_offer,
act C.uint32_t,
) {
}
//export gio_onDataDeviceOffer
func gio_onDataDeviceOffer(data unsafe.Pointer,
dataDev *C.struct_wl_data_device, id *C.struct_wl_data_offer,
) {
s := callbackLoad(data).(*wlSeat)
callbackStore(unsafe.Pointer(id), s)
C.wl_data_offer_add_listener(id, &C.gio_data_offer_listener,
unsafe.Pointer(id),
)
s.offers[id] = nil
}
//export gio_onDataDeviceEnter
func gio_onDataDeviceEnter(data unsafe.Pointer,
dataDev *C.struct_wl_data_device, serial C.uint32_t,
surf *C.struct_wl_surface, x, y C.wl_fixed_t, id *C.struct_wl_data_offer,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
s.flushOffers()
}
//export gio_onDataDeviceLeave
func gio_onDataDeviceLeave(data unsafe.Pointer,
dataDev *C.struct_wl_data_device,
) {
}
//export gio_onDataDeviceMotion
func gio_onDataDeviceMotion(data unsafe.Pointer,
dataDev *C.struct_wl_data_device, t C.uint32_t, x, y C.wl_fixed_t,
) {
}
//export gio_onDataDeviceDrop
func gio_onDataDeviceDrop(data unsafe.Pointer, dataDev *C.struct_wl_data_device,
) {
}
//export gio_onDataDeviceSelection
func gio_onDataDeviceSelection(data unsafe.Pointer,
dataDev *C.struct_wl_data_device, id *C.struct_wl_data_offer,
) {
s := callbackLoad(data).(*wlSeat)
defer s.flushOffers()
s.clipboard = nil
loop:
for _, want := range clipboardMimeTypes {
for _, got := range s.offers[id] {
if want != got {
continue
}
s.clipboard = id
s.mimeType = got
break loop
}
}
}
//export gio_onRegistryGlobalRemove
func gio_onRegistryGlobalRemove(data unsafe.Pointer, reg *C.struct_wl_registry,
name C.uint32_t,
) {
d := callbackLoad(data).(*wlDisplay)
if s := d.seat; s != nil && name == s.name {
s.destroy()
d.seat = nil
}
if output, exists := d.outputMap[name]; exists {
C.wl_output_destroy(output)
delete(d.outputMap, name)
delete(d.outputConfig, output)
}
}
//export gio_onTouchDown
func gio_onTouchDown(data unsafe.Pointer, touch *C.struct_wl_touch,
serial, t C.uint32_t, surf *C.struct_wl_surface, id C.int32_t,
x, y C.wl_fixed_t,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
w := callbackLoad(unsafe.Pointer(surf)).(*window)
s.touchFoci[id] = w
w.lastTouch = f32.Point{
X: fromFixed(x) * float32(w.scale),
Y: fromFixed(y) * float32(w.scale),
}
w.w.Event(pointer.Event{
Type: pointer.Press,
Source: pointer.Touch,
Position: w.lastTouch,
PointerID: pointer.ID(id),
Time: time.Duration(t) * time.Millisecond,
Modifiers: w.disp.xkb.Modifiers(),
},
)
}
//export gio_onTouchUp
func gio_onTouchUp(data unsafe.Pointer, touch *C.struct_wl_touch,
serial, t C.uint32_t, id C.int32_t,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
w := s.touchFoci[id]
delete(s.touchFoci, id)
w.w.Event(pointer.Event{
Type: pointer.Release,
Source: pointer.Touch,
Position: w.lastTouch,
PointerID: pointer.ID(id),
Time: time.Duration(t) * time.Millisecond,
Modifiers: w.disp.xkb.Modifiers(),
},
)
}
//export gio_onTouchMotion
func gio_onTouchMotion(data unsafe.Pointer, touch *C.struct_wl_touch,
t C.uint32_t, id C.int32_t, x, y C.wl_fixed_t,
) {
s := callbackLoad(data).(*wlSeat)
w := s.touchFoci[id]
w.lastTouch = f32.Point{
X: fromFixed(x) * float32(w.scale),
Y: fromFixed(y) * float32(w.scale),
}
w.w.Event(pointer.Event{
Type: pointer.Move,
Position: w.lastTouch,
Source: pointer.Touch,
PointerID: pointer.ID(id),
Time: time.Duration(t) * time.Millisecond,
Modifiers: w.disp.xkb.Modifiers(),
},
)
}
//export gio_onTouchFrame
func gio_onTouchFrame(data unsafe.Pointer, touch *C.struct_wl_touch) {
}
//export gio_onTouchCancel
func gio_onTouchCancel(data unsafe.Pointer, touch *C.struct_wl_touch) {
s := callbackLoad(data).(*wlSeat)
for id, w := range s.touchFoci {
delete(s.touchFoci, id)
w.w.Event(pointer.Event{
Type: pointer.Cancel,
Source: pointer.Touch,
},
)
}
}
//export gio_onPointerEnter
func gio_onPointerEnter(data unsafe.Pointer, pointer *C.struct_wl_pointer,
serial C.uint32_t, surf *C.struct_wl_surface, x, y C.wl_fixed_t,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
w := callbackLoad(unsafe.Pointer(surf)).(*window)
s.pointerFocus = w
w.setCursor(pointer, serial)
w.lastPos = f32.Point{X: fromFixed(x), Y: fromFixed(y)}
}
//export gio_onPointerLeave
func gio_onPointerLeave(data unsafe.Pointer, p *C.struct_wl_pointer,
serial C.uint32_t, surface *C.struct_wl_surface,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
}
//export gio_onPointerMotion
func gio_onPointerMotion(data unsafe.Pointer, p *C.struct_wl_pointer,
t C.uint32_t, x, y C.wl_fixed_t,
) {
s := callbackLoad(data).(*wlSeat)
w := s.pointerFocus
w.resetFling()
w.onPointerMotion(x, y, t)
}
//export gio_onPointerButton
func gio_onPointerButton(data unsafe.Pointer, p *C.struct_wl_pointer,
serial, t, wbtn, state C.uint32_t,
) {
s := callbackLoad(data).(*wlSeat)
s.serial = serial
w := s.pointerFocus
// From linux-event-codes.h.
const (
BTN_LEFT = 0x110
BTN_RIGHT = 0x111
BTN_MIDDLE = 0x112
)
var btn pointer.Buttons
switch wbtn {
case BTN_LEFT:
btn = pointer.ButtonPrimary
case BTN_RIGHT:
btn = pointer.ButtonSecondary
case BTN_MIDDLE:
btn = pointer.ButtonTertiary
default:
return
}
var typ pointer.Type
switch state {
case 0:
w.pointerBtns &^= btn
typ = pointer.Release
case 1:
w.pointerBtns |= btn
typ = pointer.Press
}
w.flushScroll()
w.resetFling()
w.w.Event(pointer.Event{
Type: typ,
Source: pointer.Mouse,
Buttons: w.pointerBtns,
Position: w.lastPos,
Time: time.Duration(t) * time.Millisecond,
Modifiers: w.disp.xkb.Modifiers(),
},
)
}
//export gio_onPointerAxis
func gio_onPointerAxis(data unsafe.Pointer, p *C.struct_wl_pointer,
t, axis C.uint32_t, value C.wl_fixed_t,
) {
s := callbackLoad(data).(*wlSeat)
w := s.pointerFocus
v := fromFixed(value)
w.resetFling()
if w.scroll.dist == (f32.Point{}) {
w.scroll.time = time.Duration(t) * time.Millisecond
}
switch axis {
case C.WL_POINTER_AXIS_HORIZONTAL_SCROLL:
w.scroll.dist.X += v
case C.WL_POINTER_AXIS_VERTICAL_SCROLL:
w.scroll.dist.Y += v
}
}
//export gio_onPointerFrame
func gio_onPointerFrame(data unsafe.Pointer, p *C.struct_wl_pointer) {
s := callbackLoad(data).(*wlSeat)
w := s.pointerFocus
w.flushScroll()
w.flushFling()
}
func (w *window) flushFling() {
if !w.fling.start {
return
}
w.fling.start = false
estx, esty := w.fling.xExtrapolation.Estimate(), w.fling.yExtrapolation.Estimate()
w.fling.xExtrapolation = fling.Extrapolation{}
w.fling.yExtrapolation = fling.Extrapolation{}
vel := float32(math.Sqrt(float64(estx.Velocity*estx.Velocity + esty.Velocity*esty.Velocity)))
_, _, c := w.config()
if !w.fling.anim.Start(c, time.Now(), vel) {
return
}
invDist := 1 / vel
w.fling.dir.X = estx.Velocity * invDist
w.fling.dir.Y = esty.Velocity * invDist
// Wake up the window loop.
w.disp.wakeup()
}
//export gio_onPointerAxisSource
func gio_onPointerAxisSource(data unsafe.Pointer, pointer *C.struct_wl_pointer,
source C.uint32_t,
) {
}
//export gio_onPointerAxisStop
func gio_onPointerAxisStop(data unsafe.Pointer, p *C.struct_wl_pointer,
t, axis C.uint32_t,
) {
s := callbackLoad(data).(*wlSeat)