-
Notifications
You must be signed in to change notification settings - Fork 2
/
gwwm.scm
1332 lines (1262 loc) · 55 KB
/
gwwm.scm
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
(define-module (gwwm)
#:use-module ((system foreign) #:select (make-pointer scm->pointer))
#:use-module (ice-9 control)
#:use-module ((rnrs base) #:select (assert))
#:use-module (ice-9 curried-definitions)
#:use-module (ice-9 format)
#:use-module (ice-9 getopt-long)
#:use-module (ice-9 q)
#:use-module (oop goops describe)
#:use-module (oop goops)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-145)
#:use-module (srfi srfi-189)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-2)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-71)
#:use-module (system repl server)
#:use-module (util572 box)
#:use-module (libinput)
#:use-module (bytestructures guile)
#:use-module (bytestructure-class)
#:use-module (xkbcommon xkbcommon)
#:use-module (wayland server display)
#:use-module (wayland list)
#:use-module (wayland server listener)
#:use-module (wayland server protocol wayland)
#:use-module (wayland signal)
#:use-module ((wlroots types scene) #:hide (.state))
#:use-module (wlroots backend libinput)
#:use-module (wlroots backend wayland)
#:use-module (wlroots backend)
#:use-module (wlroots types gamma-control)
#:use-module (wlroots render allocator)
#:use-module (wlroots render renderer)
#:use-module (wlroots time)
#:use-module (wlroots types idle-notify)
#:use-module (wlroots types compositor)
#:use-module (wlroots types export-dmabuf)
#:use-module (wlroots types cursor)
#:use-module (wlroots types data-control)
#:use-module (wlroots types data-device)
#:use-module (wlroots types input-device)
#:use-module (wlroots types input-inhibitor)
#:use-module (wlroots types keyboard)
#:use-module (wlroots types layer-shell)
#:use-module (wlroots types touch)
#:use-module (wlroots types output)
#:use-module (wlroots types server-decoration)
#:use-module (wlroots types screencopy)
#:use-module (wlroots types output-layout)
#:use-module (wlroots types output-management)
#:use-module (wlroots types pointer)
#:use-module (wlroots types pointer-gestures)
#:use-module (wlroots types presentation-time)
#:use-module (wlroots types primary-selection)
#:use-module (wlroots types seat)
#:use-module (wlroots types subcompositor)
#:use-module (wlroots types viewporter)
#:use-module (wlroots types xcursor-manager)
#:use-module (wlroots types xdg-activation)
#:use-module (wlroots types xdg-output)
#:use-module (wlroots types xdg-decoration)
#:use-module (wlroots types xdg-shell)
#:use-module (wlroots types)
#:use-module (wlroots util box)
#:use-module (gwwm client)
;; #:use-module (gwwm x-client)
#:use-module (gwwm commands)
#:use-module (gwwm config)
#:use-module (gwwm configuration)
#:use-module (gwwm hooks)
#:use-module (gwwm i18n)
#:use-module (gwwm keyboard)
#:use-module (gwwm touch)
#:use-module (gwwm pointer)
#:use-module (gwwm keymap)
#:use-module (gwwm layout tile)
#:use-module (gwwm layout)
#:use-module (gwwm listener)
#:use-module (gwwm monitor)
#:use-module (gwwm utils ref)
#:use-module (gwwm utils srfi-215)
#:use-module (gwwm utils)
#:use-module (ice-9 eval-string)
#:use-module (ice-9 suspendable-ports)
#:duplicates (merge-accessors merge-generics replace warn-override-core warn last)
#:export (main keymap-global-set
background-layer
bottom-layer
tile-layer
fullscreen-layer
float-layer
top-layer
overlay-layer
no-focus-layer))
(eval-when (expand load eval)
(load-extension "libgwwm" "scm_init_gwwm"))
(define* (keymap-global-set key command #:optional release-command)
(if release-command
(keymap-set (global-keymap) key command release-command)
(keymap-set (global-keymap) key command)))
(define-syntax-rule (define-dy name objname)
(begin (define-once name
(let ((%o #f))
(lambda* (#:optional objname)
(if objname
(begin
(set! %o objname)
%o)
%o))))
(export name)))
(define-dy gwwm-config config)
(define-dy gwwm-display display)
(define-dy gwwm-backend backend)
(define-dy gwwm-session session)
(define-dy gwwm-output-layout output-layout)
(define-dy entire-layout-box box)
(define-dy gwwm-renderer renderer)
(define-dy gwwm-allocator allocator)
(define-dy gwwm-cursor cursor)
(define-dy gwwm-seat seat)
(define-dy gwwm-scene scene)
(define-dy gwwm-xdg-shell xdg-shell)
(define-dy gwwm-xcursor-manager manager)
(define-dy gwwm-compositor compositor)
(define-dy gwwm-activation activation)
(define-dy gwwm-layer-shell layer-shell)
(define-dy gwwm-idle idle)
(define-dy gwwm-data-control-manager m)
(define-dy gwwm-pointer-gestures g)
(define-dy gwwm-input-inhibit-manager input-inhibit-manager)
(define-dy gwwm-scene-output-layout l)
(define-dy grabc c)
(define-dy grabcx x)
(define-dy grabcy y)
(define-dy cursor-mode m)
(cursor-mode 'normal)
(define-once exclusive-focus
(let ((%o (nothing)))
(lambda* (#:optional (surface (nothing)))
(if (nothing? surface)
(maybe-ref/default %o #f)
(begin
(set! %o (just surface))
surface)))))
(define-dy gwwm-output-manager mgr)
(define (init-global-keybind)
(keymap-global-set (kbd (s S space)) togglefloating)
(keymap-global-set (kbd (s S c)) killclient)
(keymap-global-set (kbd (s f)) togglefullscreen)
(keymap-global-set (kbd (s j)) (lambda () (focusstack 1)))
(keymap-global-set (kbd (s k)) (lambda () (focusstack -1)))
(keymap-global-set (kbd (s bracketleft)) (lambda () (incmaster -1)))
(keymap-global-set (kbd (s bracketright)) (lambda () (incmaster 1)))
(keymap-global-set (kbd (s e)) (lambda () (spawn "emacs" '("emacs"))))
(keymap-global-set (kbd (s mouse-left)) (lambda () (moveresize 'move)))
(keymap-global-set (kbd (s mouse-middle)) togglefloating)
(keymap-global-set (kbd (s mouse-right)) (lambda () (moveresize 'resize)))
(keymap-global-set (kbd (s S q)) gwwm-quit)
(for-each (lambda (a)
(keymap-global-set
(kbd* `(C M ,(string->symbol (string-append
"F" (number->string a)))))
(lambda () (chvt a))))
(iota 12 1))
(define (tagkeys k)
(keymap-global-set (kbd* `(s ,k)) (lambda () (view k)))
(keymap-global-set (kbd* `(s S ,k)) (lambda () (tag k)))
(keymap-global-set (kbd* `(C s S ,k)) (lambda () (toggletag k))))
(for-each tagkeys (iota 10 0)))
(define option-spec
'((version (single-char #\v) (value #f))
(help (single-char #\h) (value #f))
(eval (single-char #\e) (value #t))
(load (single-char #\l) (value #t))))
(define-public (parse-command-line args)
(let* ((options (getopt-long args option-spec))
(help-wanted (option-ref options 'help #f))
(version-wanted (option-ref options 'version #f))
(eval-wanted (option-ref options 'eval #f))
(load-wanted (option-ref options 'load #f)))
(when load-wanted
(let ((o (delay (primitive-load load-wanted))))
(add-hook! gwwm-after-init-hook (lambda _ (force o)))))
(when eval-wanted
(let ((o (delay (eval-string eval-wanted #:module (resolve-module '(gwwm))))))
(add-hook! gwwm-after-init-hook (lambda _ (force o)))))
(if (or version-wanted help-wanted)
(begin (when version-wanted
(display (string-append "gwwm " %version "\n")))
(when help-wanted
(display (G_ "\
gwwm [OPTION]
-v --version Display version
-h --help Display this help
-l --load=FILE load FILE after gwwm init
-e --eval=EXPR eval EXPR after gwwm init
")))
(exit 0)))))
(define-once global-keymap
(make-parameter (make-keymap)))
(define (setup-server)
(or (false-if-exception (and (spawn-server (make-unix-domain-server-socket))
(add-hook! gwwm-cleanup-hook stop-server-and-clients! )))
(send-log DEBUG (G_ "repl setup fail."))))
;; (primitive-load-path "gwwm/startup.scm")
(define (setup-socket)
(let ((socket (wl-display-add-socket-auto (gwwm-display))))
(if socket
(begin (setenv "WAYLAND_DISPLAY" socket)
(send-log DEBUG
(format #f (G_ "set WAYLAND_DISPLAY to ~S.") socket)
'SOCKET socket))
(begin
(send-log EMERGENCY (G_ "wl-display-add-socket-auto fail.") 'SOCKET socket)
(exit 1)))))
(define (config-setup)
(add-to-load-path
(string-append
(get-xdg-config-home)
"/" "gwwm"))
(gwwm-config (load-init-file)))
(define (closemon m)
(assume (is-a? m <gwwm-monitor>))
(for-each (lambda (c)
(let ((geom (client-geom c)))
(when (and (client-floating? c)
(> (box-x geom)
(box-width (monitor-area m))))
(client-resize c (modify-instance geom
(x (- x (box-width (monitor-window-area m))))) #t))
(when (equal? (client-monitor c) m)
(setmon
c
(current-monitor)))))
(client-list)))
(define-once background-layer #f)
(define-once bottom-layer #f)
(define-once tile-layer #f)
(define-once fullscreen-layer #f)
(define-once float-layer #f)
(define-once top-layer #f)
(define-once overlay-layer #f)
(define-once no-focus-layer #f)
(define (surface-notify-enter s kb)
(wlr-seat-keyboard-notify-enter
(gwwm-seat)
s
(and kb (.keycodes kb) )
(if kb (.num-keycodes kb) 0)
(and kb(.modifiers kb))))
(define-public (focusclient c lift)
(let/ec return
(unless (exclusive-focus)
(if c
(let ((old (~ (gwwm-seat) 'keyboard-state 'focused-surface)))
(unless (equal? (client-surface c) old)
(when lift
(client-scene-raise-to-top c))
(when (is-a? c <gwwm-client>)
(set! (current-monitor) (client-monitor c))
(set! (client-urgent? c) 0)
(client-restack-surface c))
(when old
(or
(and-let* ((l (wlr-layer-surface-v1-try-from-wlr-surface old)))
(if (and (.mapped old)
(member (~ l 'current 'layer) '(2 3)))
(return)))
(activate-surface old #f)))
(surface-notify-enter (client-surface c)
(wlr-seat-get-keyboard (gwwm-seat)))
(activate-surface (client-surface c) #t)))
(wlr-seat-keyboard-notify-clear-focus (gwwm-seat))))))
(define (setmon c m)
(pk 'setmon)
(let ((old (client-monitor c))
(surface (client-surface c)))
(unless (equal? old m)
(run-hook client-set-monitor-hook c old m)
(set! (client-monitor c) m)
(when old
(wlr-surface-send-leave
surface
(monitor-output old))
(arrange old))
(when m
(wlr-surface-send-enter surface (monitor-output m))
(arrange m))
(focusclient (focustop (current-monitor)) #t))))
(define (arrange-interactive-layer m)
(send-log DEBUG "arrange-interactive-layer" 'm m)
(let* ((layers (slot-ref m 'layers)))
(any (lambda (c)
(let* ((surface (client-surface c))
(lsurface (client-super-surface c)))
(if (= 1 (~ lsurface 'current 'keyboard-interactive))
(begin (focusclient #f #f)
(exclusive-focus surface)
(surface-notify-enter surface (wlr-seat-get-keyboard (gwwm-seat)))
#t)
#f)))
(append (car (list-ref layers 3)) (car (list-ref layers 2))))))
(define (arrangelayer m q-list box exclusive?)
(define (arrange-layer-client c)
(let ((exclusive-zone
(.exclusive-zone
(.current (client-super-surface c)))))
(when (eq? exclusive? (> exclusive-zone 0) )
(wlr-scene-layer-surface-v1-configure
(slot-ref c 'scene-layer-surface)
(monitor-area m)
box)
(modify-instance* (client-geom c)
(x (.x (.node (client-scene c))))
(y (.y (.node (client-scene c)))))
(client-scene-move
c
(box-x (client-geom c))
(box-y (client-geom c))))))
(for-each arrange-layer-client (car q-list)))
(define-public (arrangelayers m)
(pk 'arrangelayers)
(let* ((l (reverse (slot-ref m 'layers)))
(box (shallow-clone (monitor-area m))))
(for-each (cut arrangelayer m <> box #t) l)
(unless (equal? box (monitor-window-area m))
(pk box)
(set! (monitor-window-area m) (shallow-clone box))
(arrange m))
(for-each (cut arrangelayer m <> box #f) l)
(arrange-interactive-layer m)))
(define (update-monitor m config)
(pk 'update-monitor)
(let* ((output (monitor-output m))
(config-head (wlr-output-configuration-head-v1-create config output))
(box (wlr-output-layout-get-box (gwwm-output-layout) output))
(state (.state config-head)))
(set! (monitor-area m) box)
(set! (monitor-window-area m) (shallow-clone box))
(and=> (monitor-scene-output m)
(cut wlr-scene-output-set-position
<>
(box-x box)
(box-y box)))
(arrangelayers m)
(arrange m)
(modify-instance* state
(x (box-x box))
(y (box-y box))
(enabled (.enabled output))
(scale (.scale output)))))
(define (update-monitors listener data)
(let ((config (wlr-output-configuration-v1-create)))
(entire-layout-box (wlr-output-layout-get-box (gwwm-output-layout)))
(for-each (cut update-monitor <> config) (monitor-list))
(and-let* ((m (current-monitor))
((.enabled (monitor-output m))))
(for-each (lambda (c)
(when (and (not (client-monitor c)) (client-mapped? c))
(setmon c m))) (client-list)))
(wlr-output-manager-v1-set-configuration (gwwm-output-manager) config)))
(define (create-keyboard device)
(send-log INFO "new keyboard create" 'device device)
(let* ((wl-kb (wlr-keyboard-from-input-device device))
(kb (make <gwwm-keyboard> #:device device))
(context (xkb-context-new XKB_CONTEXT_NO_FLAGS))
(xkb-rule-names
(xkb-rules))
(keymap (xkb-keymap-new
context
xkb-rule-names
#:flags
XKB_KEYMAP_COMPILE_NO_FLAGS)))
(wlr-keyboard-set-keymap wl-kb keymap)
(keyboard-set-repeat-info kb (repeat-rate) 600)
(run-hook create-keyboard-hook kb)
(add-listen wl-kb 'modifiers
(lambda (listener wlr-keyboard)
(let* ((modifiers(.modifiers wlr-keyboard)))
(wlr-seat-set-keyboard
(gwwm-seat) wl-kb)
(run-hook modifiers-event-hook kb)
(wlr-seat-keyboard-notify-modifiers
(gwwm-seat) modifiers)))
#:destroy-when device)
(add-listen wl-kb 'key
(lambda (listener event)
(let* ((seat (gwwm-seat))
(keybinding (@@ (gwwm keybind) keybinding)))
(run-hook keypress-event-hook kb event)
(unless (and (not (.active-inhibitor
(gwwm-input-inhibit-manager)))
(keybinding
(wlr-keyboard-get-modifiers wl-kb)
(+ 8 (.keycode event))
(eq? (.state event) 'WL_KEYBOARD_KEY_STATE_PRESSED)))
(wlr-seat-set-keyboard seat wl-kb)
(wlr-seat-keyboard-notify-key
seat
(.time-msec event)
(.keycode event)
(if (eq? (.state event) 'WL_KEYBOARD_KEY_STATE_PRESSED)
1 0)))))
#:destroy-when device)))
(define (request-start-drag listener event)
(send-log DEBUG "request start drag")
(let* ((seat (~ event 'drag 'seat)))
(if (wlr-seat-validate-pointer-grab-serial
seat
(.origin event)
(.serial event))
(wlr-seat-start-pointer-drag seat (.drag event) (.serial event))
(wlr-data-source-destroy (~ event 'drag 'source)))))
(define* (motionnotify #:optional (time 0))
(when (zero? time)
(idle-activity))
(when (sloppyfocus?)
(set! (current-monitor) (monitor-at (.x (gwwm-cursor)) (.y (gwwm-cursor)))))
(run-hook motion-notify-hook time)
(let ((cursor (gwwm-cursor)))
(case (cursor-mode)
((move) (client-resize
(grabc)
(make <wlr-box>
#:x (inexact->exact (round (- (.x cursor) (grabcx))))
#:y (inexact->exact (round (- (.y cursor) (grabcy))))
#:width (box-width (client-geom (grabc)))
#:height (box-height (client-geom (grabc))))
#t))
((resize) (client-resize
(grabc)
(make <wlr-box>
#:x (box-x (client-geom (grabc)))
#:y (box-y (client-geom (grabc)))
#:width
(inexact->exact
(round (- (.x cursor)
(box-x (client-geom (grabc))))))
#:height
(inexact->exact
(round (- (.y cursor)
(box-y (client-geom (grabc)))))))
#t))
((normal)
(maybe-let*-values
(((c surface sx sy) (client-at cursor)))
(when (and (not surface) (zero? time))
(wlr-cursor-set-xcursor
(gwwm-cursor)
(gwwm-xcursor-manager)
(cursor-normal-image)))
(pointerfocus c surface sx sy time))))))
(define (idle-activity . _)
(wlr-idle-notifier-v1-notify-activity (gwwm-idle) (gwwm-seat)))
(define (cursor-setup)
(define ((cursor/button cursor) listener event)
(let* ((pressed (eq? (.state event) 'WLR_BUTTON_PRESSED)))
(idle-activity)
(run-hook cursor-button-event-hook event)
(case (cursor-mode)
((normal)
(maybe-let*-values
(((c scene-obj sx sy) (client-at cursor)))
(focusclient c #t))
(let* ((keyboard (wlr-seat-get-keyboard (gwwm-seat)))
(mods (if keyboard (wlr-keyboard-get-modifiers
keyboard) 0)))
(unless ((@@ (gwwm keybind) keybinding)
mods
(+ 8 (.button event))
pressed)
(wlr-seat-pointer-notify-button
(gwwm-seat)
(.time-msec event)
(.button event)
(.state event)))))
(else => (lambda (o)
(unless pressed
(and-let* (((eq? o 'resize))
(c (grabc)))
(client-set-resizing! c #f))
(wlr-cursor-set-xcursor
cursor
(gwwm-xcursor-manager)
(cursor-normal-image))
(cursor-mode 'normal)
(set! (current-monitor)
(monitor-at (.x cursor) (.y cursor)))
(setmon (grabc) (current-monitor))))))))
(let ((cursor (gwwm-cursor (wlr-cursor-create))))
(add-listen cursor 'touch-up
(lambda (listener event)
(let-slots event (touch time-msec touch-id)
(send-log INFO "touch-up"
'touch touch
'time-msec time-msec
'touch-id touch-id)
(wlr-seat-touch-notify-up (gwwm-seat) time-msec touch-id)
(wlr-seat-touch-point-clear-focus (gwwm-seat) time-msec touch-id)))
#:remove-when-destroy? #f)
(add-listen cursor 'touch-down
(lambda (listener event)
(let-slots event (touch time-msec touch-id x y)
(send-log INFO "touch-down"
'touch touch
'time-msec time-msec
'touch-id touch-id
'x x
'y y)
(let* ((o (wlr-cursor-absolute-to-layout-coords
(gwwm-cursor)
(.base touch)
x
y)))
(maybe-let*-values
(((c surface sx sy) (client-at (car o) (cdr o))))
(when (wlr-surface? surface)
(wlr-seat-touch-point-focus
(gwwm-seat)
surface
time-msec touch-id sx sy)
(wlr-seat-touch-notify-down
(gwwm-seat)
surface
time-msec touch-id sx sy))))))
#:remove-when-destroy? #f)
(add-listen cursor 'touch-motion
(lambda (listener event)
(let-slots event (touch time-msec touch-id x y)
(let ((o (wlr-cursor-absolute-to-layout-coords
(gwwm-cursor)
(.base touch)
x
y)))
(maybe-let*-values
(((c surface sx sy) (client-at (car o) (cdr o))))
(wlr-seat-touch-notify-motion (gwwm-seat) time-msec touch-id sx sy)))))
#:remove-when-destroy? #f)
(add-listen cursor 'touch-cancel
(lambda (listener event)
(let-slots event (touch time-msec touch-id)
(send-log INFO "touch-cancel"
'touch touch
'time-msec time-msec
'touch-id touch-id)
(maybe-let*-values
(((c surface sx sy) (client-at (gwwm-cursor))))
(when (wlr-surface? surface)
(wlr-seat-touch-notify-cancel
(gwwm-seat)
surface)))))
#:remove-when-destroy? #f)
(add-listen cursor 'touch-frame
(lambda (listener data)
;; (send-log INFO "touch-frame" )
(wlr-seat-touch-notify-frame (gwwm-seat)))
#:remove-when-destroy? #f)
(add-listen cursor 'axis
(lambda (listener event)
(run-hook axis-event-hook event)
(let-slots event (time-msec orientation delta delta-discrete
source)
(wlr-seat-pointer-notify-axis
(gwwm-seat)
time-msec
orientation
delta
delta-discrete
source))
(idle-activity))
#:remove-when-destroy? #f)
(add-listen cursor 'swipe-begin
(lambda (listener event)
(let-slots event (pointer time-msec fingers)
(send-log INFO "swipe-begin"
'pointer pointer
'time-msec time-msec
'fingers fingers)
(wlr-pointer-gestures-v1-send-swipe-begin
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
fingers)))
#:remove-when-destroy? #f)
(add-listen cursor 'swipe-update
(lambda (listener event)
(let-slots event (pointer time-msec fingers dx dy)
(wlr-pointer-gestures-v1-send-swipe-update
(gwwm-pointer-gestures)
(gwwm-seat)
fingers
dx
dy)))
#:remove-when-destroy? #f)
(add-listen cursor 'swipe-end
(lambda (listener event)
(let-slots event (pointer time-msec cancelled)
(send-log INFO "swipe-end"
'pointer pointer
'time-msec time-msec
'cancelled cancelled)
(wlr-pointer-gestures-v1-send-swipe-end
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
cancelled)))
#:remove-when-destroy? #f)
(add-listen cursor 'pinch-begin
(lambda (listener event)
(let-slots event (pointer time-msec fingers)
(send-log INFO "pinch-begin"
'pointer pointer
'time-msec time-msec
'fingers fingers )
(wlr-pointer-gestures-v1-send-pinch-begin
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
fingers)))
#:remove-when-destroy? #f)
(add-listen cursor 'pinch-update
(lambda (listener event)
(let-slots event (pointer time-msec fingers dx dy scale rotation)
(wlr-pointer-gestures-v1-send-pinch-update
(gwwm-pointer-gestures)
(gwwm-seat)
fingers
dx
dy
scale
rotation)))
#:remove-when-destroy? #f)
(add-listen cursor 'pinch-end
(lambda (listener event)
(let-slots event (pointer time-msec cancelled)
(send-log INFO "pinch-end"
'pointer pointer
'time-msec time-msec
'cancelled cancelled)
(wlr-pointer-gestures-v1-send-pinch-end
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
cancelled)))
#:remove-when-destroy? #f)
(add-listen cursor 'hold-begin
(lambda (listener event)
(let-slots event (pointer time-msec fingers)
(send-log INFO "hold-begin"
'pointer pointer
'time-msec time-msec
'fingers fingers )
(wlr-pointer-gestures-v1-send-hold-begin
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
fingers)))
#:remove-when-destroy? #f)
(add-listen cursor 'hold-end
(lambda (listener event)
(let-slots event (pointer time-msec cancelled)
(send-log INFO "hold-end"
'pointer pointer
'time-msec time-msec
'cancelled cancelled)
(wlr-pointer-gestures-v1-send-hold-end
(gwwm-pointer-gestures)
(gwwm-seat)
time-msec
cancelled)))
#:remove-when-destroy? #f)
(add-listen cursor 'frame
(lambda (listener cursor)
(run-hook cursor-frame-event-hook cursor)
(wlr-seat-pointer-notify-frame (gwwm-seat)))
#:remove-when-destroy? #f)
(add-listen cursor 'motion
(lambda (listener event)
(wlr-cursor-move (gwwm-cursor)
(.base (.pointer event))
(.delta-x event)
(.delta-y event))
(motionnotify (.time-msec event)))
#:remove-when-destroy? #f)
(add-listen cursor 'motion-absolute
(lambda (listener event)
(let-slots event (pointer x y time-msec)
(wlr-cursor-warp-absolute
(gwwm-cursor)
(.base pointer) x y)
(motionnotify time-msec)))
#:remove-when-destroy? #f)
(add-listen cursor 'button (cursor/button cursor)
#:remove-when-destroy? #f)))
(define (activate-surface surface activate?)
;; (assert (wlr-surface? surface))
(let ((super-surface (super-surface-try-from-wlr-surface surface)))
(cond ((and
(wlr-xdg-surface? super-surface)
(eq? (.role super-surface)
'WLR_XDG_SURFACE_ROLE_TOPLEVEL))
(wlr-xdg-toplevel-set-activated
(wlr-xdg-surface-toplevel super-surface)
activate? )))))
(define (seat-setup display)
(let ((seat (gwwm-seat (wlr-seat-create (gwwm-display) "seat0"))))
(add-listen seat 'request-set-cursor
(lambda (listener event)
(when (eq? (cursor-mode) 'normal)
(let-slots event (seat-client surface hostpot-x hostpot-y)
(when (equal? seat-client
(~ seat 'pointer-state 'focused-client))
(wlr-cursor-set-surface (gwwm-cursor)
surface
hostpot-x
hostpot-y))))))
(add-listen seat 'request-set-selection
(lambda (listener event)
(run-hook selection-hook event)
(wlr-seat-set-selection seat (.source event)
(.serial event))))
(add-listen seat 'request-start-drag request-start-drag)
(add-listen seat 'request-set-primary-selection
(lambda (listener event)
(send-log INFO "seat request set primary selection")
(wlr-seat-set-primary-selection seat (.source event) (.serial event))))
(add-listen (.keyboard-state seat) 'focus-change
(lambda (listener event)
(let* ((old-client (and=> (.old-surface event)
client-from-wlr-surface))
(new-client (and=> (.new-surface event)
client-from-wlr-surface)))
(run-hook keyboard-focus-change-hook
seat old-client new-client)))
#:destroy-when seat)
(add-listen seat 'start-drag
(lambda (listener drag)
(and-let* (drag
(icon (.icon drag))
(scene (wlr-scene-subsurface-tree-create
no-focus-layer
(.surface icon)))
(drag-move
(lambda _
(wlr-scene-node-set-position
(.node scene)
(inexact->exact
(round (+ (.x (gwwm-cursor))
(.dx (.current (.surface icon))))))
(inexact->exact
(round (+ (.y (gwwm-cursor))
(.dy (.current (.surface icon))))))))))
(add-hook! motion-notify-hook drag-move)
(motionnotify)
(add-listen icon 'destroy
(lambda (listener data)
(remove-hook! motion-notify-hook drag-move)
(wlr-scene-node-destroy (.node scene))
(focusclient (current-client) #t)
(motionnotify))
#:remove-when-destroy? #f))))))
(define non-zero? (negate zero?))
(define (create-pointer device)
(define p (make <gwwm-pointer> #:device device))
(and-let* (((wlr-input-device-is-libinput device))
(libinput-device (wlr-libinput-get-device-handle device)))
(when (non-zero? (libinput-device-config-tap-get-finger-count libinput-device))
(libinput-device-config-tap-set-enabled
libinput-device 'LIBINPUT_CONFIG_TAP_ENABLED)
(libinput-device-config-tap-set-drag-enabled
libinput-device 'LIBINPUT_CONFIG_DRAG_ENABLED)
(libinput-device-config-tap-set-drag-lock-enabled
libinput-device 'LIBINPUT_CONFIG_DRAG_LOCK_ENABLED))
(set! (pointer-natural-scroll? p) #t)
(set! (pointer-disable-while-typing? p) #t)
(set! (pointer-left-handed? p) #f)
(set! (pointer-middle-emulation? p) #f)
(unless (= (libinput-device-config-scroll-get-methods libinput-device) 0)
(libinput-device-config-scroll-set-method libinput-device 'LIBINPUT_CONFIG_SCROLL_2FG))
(unless (= (libinput-device-config-click-get-methods libinput-device) 0)
(libinput-device-config-click-set-method libinput-device 'LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS))
(when (non-zero? (libinput-device-config-send-events-get-modes libinput-device ))
(libinput-device-config-send-events-set-mode libinput-device
LIBINPUT_CONFIG_SEND_EVENTS_ENABLED))
(when (libinput-device-config-accel-is-available libinput-device)
(libinput-device-config-accel-set-profile libinput-device 'LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE)
(libinput-device-config-accel-set-speed libinput-device 0.0)))
(wlr-cursor-attach-input-device (gwwm-cursor) device))
(define (init-monitor m)
(assert (is-a? m <gwwm-monitor>))
(let* ((name (monitor-name m))
(rule (or (find (lambda (x) (equal? x name)) (monitor-rules))
(default-monitor-rules)))
(output (monitor-output m))
(scale (.scale rule)))
(wlr-output-set-scale output scale)
(wlr-xcursor-manager-load (gwwm-xcursor-manager) scale)
(wlr-output-set-transform output (.reflect rule))))
(define (backend-setup display)
(define (add-seat-capabilitie seat o)
(wlr-seat-set-capabilities seat
(logior (.capabilities seat)
(bs:enum->integer
%wl-seat-capability-enum o))))
(define (backend/new-output listener wlr-output)
(let* ((m (make <gwwm-monitor>
#:wlr-output wlr-output
#:layouts (make-list 2 tile-layout))))
(set! (wlr-output->monitor wlr-output) m)
(init-monitor m)
(when (wlr-output-init-render wlr-output (gwwm-allocator) (gwwm-renderer))
(wlr-output-set-mode wlr-output (wlr-output-preferred-mode wlr-output))
(wlr-output-enable wlr-output #t)
(let ((state (make <wlr-output-state>)))
(wlr-output-state-set-enabled state #t)
(send-log INFO "enable adaptive-sync" 'monitor m)
(wlr-output-state-set-adaptive-sync-enabled state #t)
(unless (wlr-output-test-state wlr-output state)
(send-log INFO "enable adaptive-sync fail" 'monitor m))
(when (wlr-output-commit-state wlr-output state)
(cond ((wlr-output-is-wl wlr-output)
(wlr-wl-output-set-title wlr-output "gwwm/wayland")))
(q-push! (%monitors) m)
(let ((lo (wlr-output-layout-add-auto (gwwm-output-layout) wlr-output))
(scene-output (wlr-scene-output-create (gwwm-scene) wlr-output)))
(wlr-scene-output-layout-add-output
(gwwm-scene-output-layout)
lo
scene-output)
(set! (monitor-scene-output m) scene-output)
(add-listen scene-output 'destroy
(lambda _ (set! (monitor-scene-output m) #f))))
(add-listen (monitor-output m) 'frame render-monitor)
(add-listen (monitor-output m) 'request-state
(lambda (listener event)
(send-log INFO "request-state")
(wlr-output-commit-state wlr-output (.state event))))
(add-listen (monitor-output m) 'destroy cleanup-monitor)
(wlr-output-commit-state wlr-output state)
(run-hook create-monitor-hook m)
(wlr-output-state-finish state)))
)))
(define (backend/new-input listener device)
(case (.type device)
((WLR_INPUT_DEVICE_KEYBOARD)
(create-keyboard device)
(unless (null? (keyboard-list))
(add-seat-capabilitie
(gwwm-seat)
'WL_SEAT_CAPABILITY_KEYBOARD)))
((WLR_INPUT_DEVICE_POINTER)
(create-pointer device)
(add-seat-capabilitie
(gwwm-seat)
'WL_SEAT_CAPABILITY_POINTER))
((WLR_INPUT_DEVICE_TOUCH)
(make <gwwm-touch> #:device device)
(wlr-cursor-attach-input-device (gwwm-cursor) device)
(add-seat-capabilitie
(gwwm-seat)
'WL_SEAT_CAPABILITY_TOUCH))
((WLR_INPUT_DEVICE_SWITCH)
(send-log WARNING "TODO"))
((WLR_INPUT_DEVICE_TABLET_TOOL)
(send-log WARNING "TODO"))
((WLR_INPUT_DEVICE_TABLET_PAD)
(send-log WARNING "TODO"))
(else (send-log WARNING "unknow input device"))))
(let* ((backend session (wlr-backend-autocreate(gwwm-display))))
(unless backend
(send-log ERROR (G_ "gwwm Couldn't create backend"))
(exit 1))
(and=> backend gwwm-backend)
(and=> session gwwm-session)
(add-listen backend 'new-input backend/new-input)
(add-listen backend 'new-output backend/new-output)))
(define (output-manager-setup display)
(let ((output-manager (gwwm-output-manager (wlr-output-manager-v1-create display))))
(add-listen output-manager 'apply
(lambda (listener config)
(output-manager-apply-or-test config #f)))
(add-listen output-manager 'test
(lambda (listener config)
(output-manager-apply-or-test config #t)))))
(define (return-scene-node n)
(case n
((0) background-layer)
((1) bottom-layer)
((2) top-layer)
((3) overlay-layer)))
(define (gwwm-setup)
(gwwm-display (wl-display-create))
(backend-setup (gwwm-display))
(or (and=> (wlr-renderer-autocreate (gwwm-backend)) gwwm-renderer)
(begin (send-log ERROR (G_ "gwwm Couldn't create renderer"))
(exit 1)))
(wlr-renderer-init-wl-display (gwwm-renderer) (gwwm-display))
(or (and=> (wlr-allocator-autocreate
(gwwm-backend)
(gwwm-renderer)) gwwm-allocator)
(begin (send-log ERROR (G_ "gwwm Couldn't create allocator"))
(exit 1)))
(cursor-setup)
(gwwm-xcursor-manager (wlr-xcursor-manager-create #f 24))
(seat-setup (gwwm-display))
(gwwm-pointer-gestures (wlr-pointer-gestures-v1-create (gwwm-display)))
(gwwm-xdg-shell (wlr-xdg-shell-create (gwwm-display) 5))
(add-listen (gwwm-xdg-shell) 'new-surface
(lambda (listener xdg-surface)
(when (eq? (.role xdg-surface)
'WLR_XDG_SURFACE_ROLE_TOPLEVEL)
(let* ((scene
(wlr-scene-xdg-surface-create tile-layer
xdg-surface))
(c (make <gwwm-xdg-client>
#:scene scene
#:super-surface xdg-surface)))
(set! (client-border-width c) (borderpx))
(run-hook create-client-hook c)))))
(gwwm-compositor (wlr-compositor-create (gwwm-display) 5 (gwwm-renderer)))
(wlr-subcompositor-create (gwwm-display))
(gwwm-activation (wlr-xdg-activation-v1-create (gwwm-display)))
(add-listen (gwwm-activation) 'request-activate
(lambda (listener event)
(and-let* ((c (client-from-wlr-surface (.surface event))))
(unless (equal? c (current-client))
(set! (client-urgent? c) #t)))))
(gwwm-layer-shell (wlr-layer-shell-v1-create (gwwm-display) 4))
(add-listen (gwwm-layer-shell) 'new-surface
(lambda (listener layer-surface)
(unless (.output layer-surface)
(set! (.output layer-surface)
(monitor-output (current-monitor))))
(let* ((scene-surface (wlr-scene-layer-surface-v1-create
(pk 'ret
(return-scene-node
(~ layer-surface 'pending 'layer)))
layer-surface))
(scene (.tree scene-surface))
(c (make <gwwm-layer-client>
#:super-surface layer-surface
#:scene scene
#:monitor (wlr-output->monitor (.output layer-surface))
#:scene-layer-surface scene-surface)))
(set! (surface->scene (client-surface c)) (client-scene c))