summaryrefslogtreecommitdiff
path: root/catbird/input-map.scm
blob: dc3bd85d952bb41c26597f3b2dde4df725593c15 (plain)
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
;;; Catbird Game Engine
;;; Copyright © 2022 David Thompson <davet@gnu.org>
;;;
;;; Catbird is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Catbird is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Catbird.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Keyboard, mouse, controller input specification.
;;
;;; Code:
(define-module (catbird input-map)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (make-input-map
            add-input
            remove-input
            key-press
            key-release
            text-input
            mouse-press
            mouse-release
            mouse-move
            mouse-wheel
            controller-press
            controller-release
            controller-move
            key-press-handler
            key-release-handler
            text-input-handler
            mouse-press-handler
            mouse-release-handler
            mouse-move-handler
            mouse-wheel-handler
            controller-press-handler
            controller-release-handler
            controller-move-handler))

(define (make-input-map)
  '())

(define (add-input input-map spec handler)
  (cons (cons spec handler)
        input-map))

(define (remove-input input-map spec)
  (delete spec input-map
          (match-lambda*
            ((a (b . _))
             (equal? a b)))))

(define* (key-press key #:optional (modifiers '()))
  `(keyboard ,key ,modifiers down))

(define* (key-release key #:optional (modifiers '()))
  `(keyboard ,key ,modifiers up))

(define (text-input)
  '(text-input))

(define (mouse-press button)
  `(mouse button ,button down))

(define (mouse-release button)
  `(mouse button ,button up))

(define* (mouse-move #:optional (buttons '()))
  `(mouse move ,buttons))

(define* (mouse-wheel)
  '(mouse wheel))

(define (controller-press id button)
  `(controller button ,id ,button down))

(define (controller-release id button)
  `(controller button ,id ,button up))

(define (controller-move id axis)
  `(controller axis ,id ,axis))

;; Chickadee is specific about which modifier keys are pressed and
;; makes distinctions between left and right ctrl, for example.  For
;; convenience, we want users to be able to specify simply 'ctrl' and
;; it will match both left and right.
(define (modifiers-match? spec-modifiers modifiers)
  (every (lambda (k)
           (case k
             ;; The specification is looking for a specific modifier
             ;; key.
             ((left-ctrl right-ctrl left-alt right-alt left-shift right-shift)
              (memq k modifiers))
             ;; The specification is looking for either left/right
             ;; modifier key.
             ((ctrl)
              (or (memq 'left-control modifiers)
                  (memq 'right-control modifiers)))
             ((alt)
              (or (memq 'left-alt modifiers)
                  (memq 'right-alt modifiers)))
             ((shift)
              (or (memq 'left-shift modifiers)
                  (memq 'right-shift modifiers)))))
         spec-modifiers))

(define (key-press-handler input-map key modifiers)
  (any (match-lambda
         ((('keyboard key* modifiers* 'down) . handler)
          (and (eq? key key*)
               (modifiers-match? modifiers* modifiers)
               handler))
         (_ #f))
       input-map))

(define (key-release-handler input-map key modifiers)
  (any (match-lambda
         ((('keyboard key* modifiers* 'up) . handler)
          (and (eq? key key*)
               (modifiers-match? modifiers modifiers*)
               handler))
         (_ #f))
       input-map))

(define (text-input-handler input-map)
  (any (match-lambda
         ((('text-input) . handler) handler)
         (_ #f))
       input-map))

(define (mouse-press-handler input-map button)
  (any (match-lambda
         ((('mouse 'button button* 'down) . handler)
          (and (eq? button button*)
               handler))
         (_ #f))
       input-map))

(define (mouse-release-handler input-map button)
  (any (match-lambda
         ((('mouse 'button button* 'up) . handler)
          (and (eq? button button*)
               handler))
         (_ #f))
       input-map))

(define (mouse-move-handler input-map buttons)
  (any (match-lambda
         ((('mouse 'move buttons*) . handler)
          (and (= (length buttons) (length buttons*))
               (every (lambda (b) (memq b buttons*)) buttons)
               handler))
         (_ #f))
       input-map))

(define (mouse-wheel-handler input-map)
  (any (match-lambda
         ((('mouse 'wheel) . handler)
          handler)
         (_ #f))
       input-map))

(define (controller-press-handler input-map controller-id button)
  (any (match-lambda
         ((('controller 'button controller-id* button* 'down) . handler)
          (and (= controller-id controller-id*)
               (eq? button button*)
               handler))
         (_ #f))
       input-map))

(define (controller-release-handler input-map controller-id button)
  (any (match-lambda
         ((('controller 'button controller-id* button* 'up) . handler)
          (and (= controller-id controller-id*)
               (eq? button button*)
               handler))
         (_ #f))
       input-map))

(define (controller-move-handler input-map controller-id axis)
  (any (match-lambda
         ((('controller 'axis controller-id* axis*) . handler)
          (and (= controller-id controller-id*)
               (eq? axis axis*)
               handler))
         (_ #f))
       input-map))