events: keyboard: Correct exported symbols.
[guile-sdl2.git] / sdl2 / events.scm
CommitLineData
2751b1a7
DT
1;;; guile-sdl2 --- FFI bindings for SDL2
2;;; Copyright © 2015 David Thompson <davet@gnu.org>
3;;;
4;;; This file is part of guile-sdl2.
5;;;
6;;; Guile-sdl2 is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU Lesser General Public License as
8;;; published by the Free Software Foundation; either version 3 of the
9;;; License, or (at your option) any later version.
10;;;
11;;; Guile-sdl2 is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;; General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU Lesser General Public
17;;; License along with guile-sdl2. If not, see
18;;; <http://www.gnu.org/licenses/>.
19
20;;; Commentary:
21;;
22;; SDL event handling.
23;;
24;;; Code:
25
26(define-module (sdl2 events)
27 #:use-module (ice-9 hash-table)
28 #:use-module (ice-9 match)
29 #:use-module (rnrs bytevectors)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-4)
32 #:use-module (srfi srfi-9)
33 #:use-module (system foreign)
34 #:use-module ((sdl2 bindings) #:prefix ffi:)
35 #:use-module (sdl2)
36 #:export (make-window-event
37 window-event?
38 window-event-timestamp
39 window-event-window-id
40 window-event-type
41 window-event-vector
42
43 make-keyboard-event
44 keyboard-event?
f2210cb0
DT
45 keyboard-down-event?
46 keyboard-up-event?
2751b1a7
DT
47 keyboard-event-timestamp
48 keyboard-event-window-id
49 keyboard-event-pressed?
50 keyboard-event-repeat
3c0cdecf
DT
51 keyboard-event-key
52 keyboard-event-scancode
53 keyboard-event-modifiers
2751b1a7 54
2818dfb3
DT
55 make-mouse-button-event
56 mouse-button-event?
57 mouse-button-down-event?
58 mouse-button-up-event?
59 mouse-button-event-timestamp
60 mouse-button-event-window-id
61 mouse-button-event-which
62 mouse-button-event-button
63 mouse-button-event-pressed?
64 mouse-button-event-clicks
65 mouse-button-event-x
66 mouse-button-event-y
67
2fd54505
DT
68 make-mouse-motion-event
69 mouse-motion-event?
70 mouse-motion-event-timestamp
71 mouse-motion-event-window-id
72 mouse-motion-event-which
73 mouse-motion-event-buttons
74 mouse-motion-event-x
75 mouse-motion-event-y
76 mouse-motion-event-x-rel
77 mouse-motion-event-y-rel
78
0e442c81
DT
79 make-joystick-axis-event
80 joystick-axis-event?
81 joystick-axis-event-timestamp
82 joystick-axis-event-which
83 joystick-axis-event-axis
84 joystick-axis-event-value
85
28503ef3
DT
86 make-joystick-button-event
87 joystick-button-event?
88 joystick-button-down-event?
89 joystick-button-up-event?
90 joystick-button-event-timestamp
91 joystick-button-event-which
92 joystick-button-event-button
93 joystick-button-event-pressed?
94
2751b1a7
DT
95 poll-event))
96
97(define (make-sdl-event)
98 (make-u8vector 56 0)) ; sizeof(SDL_Event) is 56
99
100(define (sdl-event-type event)
101 (u32vector-ref event 0))
102
103\f
104;;;
105;;; Window
106;;;
107
108(define-record-type <window-event>
109 (make-window-event timestamp window-id type vector)
110 window-event?
111 (timestamp window-event timestamp)
112 (window-id window-event-window-id)
113 (type window-event-type)
114 (vector window-event-vector))
115
116(define (parse-window-event ptr)
117 (define (type-symbol n)
118 (list-ref '(none
119 shown
120 hidden
121 exposed
122 moved
123 resized
124 size-changed
125 minimized
126 maximized
127 restored
128 enter
129 leave
130 focus-gained
131 focus-lost
132 close)
5679a233 133 (1- n)))
2751b1a7
DT
134
135 (define types
136 (list uint32 ; type
137 uint32 ; timestamp
138 uint32 ; windowID
139 uint8 ; event
140 uint8 ; padding1
141 uint8 ; padding2
142 uint8 ; padding3
143 int32 ; data1
144 int32)) ; data2
145
146 (match (parse-c-struct ptr types)
147 ((_ timestamp id event _ _ _ data1 data2)
148 (make-window-event timestamp id (type-symbol event) (cons data1 data2)))))
149
150\f
151;;;
152;;; Keyboard
153;;;
154
155(define-record-type <keyboard-event>
156 (make-keyboard-event timestamp window-id pressed? repeat
157 key scancode modifiers)
158 keyboard-event?
159 (timestamp keyboard-event-timestamp)
160 (window-id keyboard-event-window-id)
161 (pressed? keyboard-event-pressed?)
162 (repeat keyboard-event-repeat)
163 (key keyboard-event-key)
164 (scancode keyboard-event-scancode)
165 (modifiers keyboard-event-modifiers))
166
f2210cb0
DT
167(define (keyboard-down-event? e)
168 "Return #t if E is a key press event."
169 (and (keyboard-event? e)
170 (keyboard-event-pressed? e)))
171
172(define (keyboard-up-event? e)
173 "Return #t if E is a key release event."
174 (and (keyboard-event? e)
175 (not (keyboard-event-pressed? e))))
176
2751b1a7
DT
177(define keycode-map
178 (alist->hash-table
179 `((,ffi:SDLK_UNKNOWN . unknown)
180 (,ffi:SDLK_RETURN . return)
181 (,ffi:SDLK_ESCAPE . escape)
182 (,ffi:SDLK_BACKSPACE . backspace)
183 (,ffi:SDLK_TAB . tab)
184 (,ffi:SDLK_SPACE . space)
185 (,ffi:SDLK_EXCLAIM . exclaim)
186 (,ffi:SDLK_QUOTEDBL . double-quote)
187 (,ffi:SDLK_HASH . hash)
188 (,ffi:SDLK_PERCENT . percent)
189 (,ffi:SDLK_DOLLAR . dollar)
190 (,ffi:SDLK_AMPERSAND . ampersand)
191 (,ffi:SDLK_QUOTE . quote)
192 (,ffi:SDLK_LEFTPAREN . left-paren)
193 (,ffi:SDLK_RIGHTPAREN . right-paren)
194 (,ffi:SDLK_ASTERISK . asterisk)
195 (,ffi:SDLK_PLUS . plus)
196 (,ffi:SDLK_COMMA . comma)
197 (,ffi:SDLK_MINUS . minus)
198 (,ffi:SDLK_PERIOD . period)
199 (,ffi:SDLK_SLASH . slash)
200 (,ffi:SDLK_0 . 0)
201 (,ffi:SDLK_1 . 1)
202 (,ffi:SDLK_2 . 2)
203 (,ffi:SDLK_3 . 3)
204 (,ffi:SDLK_4 . 4)
205 (,ffi:SDLK_5 . 5)
206 (,ffi:SDLK_6 . 6)
207 (,ffi:SDLK_7 . 7)
208 (,ffi:SDLK_8 . 8)
209 (,ffi:SDLK_9 . 9)
210 (,ffi:SDLK_COLON . colon)
211 (,ffi:SDLK_SEMICOLON . semicolon)
212 (,ffi:SDLK_LESS . less)
213 (,ffi:SDLK_EQUALS . equals)
214 (,ffi:SDLK_GREATER . greater)
215 (,ffi:SDLK_QUESTION . question)
216 (,ffi:SDLK_AT . at)
217 (,ffi:SDLK_LEFTBRACKET . left-bracket)
218 (,ffi:SDLK_BACKSLASH . backslash)
219 (,ffi:SDLK_RIGHTBRACKET . right-bracket)
220 (,ffi:SDLK_CARET . caret)
221 (,ffi:SDLK_UNDERSCORE . underscore)
222 (,ffi:SDLK_BACKQUOTE . backquote)
223 (,ffi:SDLK_a . a)
224 (,ffi:SDLK_b . b)
225 (,ffi:SDLK_c . c)
226 (,ffi:SDLK_d . d)
227 (,ffi:SDLK_e . e)
228 (,ffi:SDLK_f . f)
229 (,ffi:SDLK_g . g)
230 (,ffi:SDLK_h . h)
231 (,ffi:SDLK_i . i)
232 (,ffi:SDLK_j . j)
233 (,ffi:SDLK_k . k)
234 (,ffi:SDLK_l . l)
235 (,ffi:SDLK_m . m)
236 (,ffi:SDLK_n . n)
237 (,ffi:SDLK_o . o)
238 (,ffi:SDLK_p . p)
239 (,ffi:SDLK_q . q)
240 (,ffi:SDLK_r . r)
241 (,ffi:SDLK_s . s)
242 (,ffi:SDLK_t . t)
243 (,ffi:SDLK_u . u)
244 (,ffi:SDLK_v . v)
245 (,ffi:SDLK_w . w)
246 (,ffi:SDLK_x . x)
247 (,ffi:SDLK_y . y)
248 (,ffi:SDLK_z . z)
249 (,ffi:SDLK_CAPSLOCK . caps-lock)
250 (,ffi:SDLK_F1 . f1)
251 (,ffi:SDLK_F2 . f2)
252 (,ffi:SDLK_F3 . f3)
253 (,ffi:SDLK_F4 . f4)
254 (,ffi:SDLK_F5 . f5)
255 (,ffi:SDLK_F6 . f6)
256 (,ffi:SDLK_F7 . f7)
257 (,ffi:SDLK_F8 . f8)
258 (,ffi:SDLK_F9 . f9)
259 (,ffi:SDLK_F10 . f10)
260 (,ffi:SDLK_F11 . f11)
261 (,ffi:SDLK_F12 . f12)
262 (,ffi:SDLK_PRINTSCREEN . print-screen)
263 (,ffi:SDLK_SCROLLLOCK . scroll-lock)
264 (,ffi:SDLK_PAUSE . pause)
265 (,ffi:SDLK_INSERT . insert)
266 (,ffi:SDLK_HOME . home)
267 (,ffi:SDLK_PAGEUP . page-up)
268 (,ffi:SDLK_DELETE . delete)
269 (,ffi:SDLK_END . end)
270 (,ffi:SDLK_PAGEDOWN . page-down)
271 (,ffi:SDLK_RIGHT . right)
272 (,ffi:SDLK_LEFT . left)
273 (,ffi:SDLK_DOWN . down)
274 (,ffi:SDLK_UP . up)
275 (,ffi:SDLK_NUMLOCKCLEAR . num-lock-clear)
276 (,ffi:SDLK_KP_DIVIDE . keypad-divide)
277 (,ffi:SDLK_KP_MULTIPLY . keypad-multiply)
278 (,ffi:SDLK_KP_MINUS . keypad-minus)
279 (,ffi:SDLK_KP_PLUS . keypad-plus)
280 (,ffi:SDLK_KP_ENTER . keypad-enter)
281 (,ffi:SDLK_KP_1 . keypad-1)
282 (,ffi:SDLK_KP_2 . keypad-2)
283 (,ffi:SDLK_KP_3 . keypad-3)
284 (,ffi:SDLK_KP_4 . keypad-4)
285 (,ffi:SDLK_KP_5 . keypad-5)
286 (,ffi:SDLK_KP_6 . keypad-6)
287 (,ffi:SDLK_KP_7 . keypad-7)
288 (,ffi:SDLK_KP_8 . keypad-8)
289 (,ffi:SDLK_KP_9 . keypad-9)
290 (,ffi:SDLK_KP_0 . keypad-0)
291 (,ffi:SDLK_KP_PERIOD . keypad-period)
292 (,ffi:SDLK_APPLICATION . application)
293 (,ffi:SDLK_POWER . power)
294 (,ffi:SDLK_KP_EQUALS . keypad-equals)
295 (,ffi:SDLK_F13 . f13)
296 (,ffi:SDLK_F14 . f14)
297 (,ffi:SDLK_F15 . f15)
298 (,ffi:SDLK_F16 . f16)
299 (,ffi:SDLK_F17 . f17)
300 (,ffi:SDLK_F18 . f18)
301 (,ffi:SDLK_F19 . f19)
302 (,ffi:SDLK_F20 . f20)
303 (,ffi:SDLK_F21 . f21)
304 (,ffi:SDLK_F22 . f22)
305 (,ffi:SDLK_F23 . f23)
306 (,ffi:SDLK_F24 . f24)
307 (,ffi:SDLK_EXECUTE . execute)
308 (,ffi:SDLK_HELP . help)
309 (,ffi:SDLK_MENU . menu)
310 (,ffi:SDLK_SELECT . select)
311 (,ffi:SDLK_STOP . stop)
312 (,ffi:SDLK_AGAIN . again)
313 (,ffi:SDLK_UNDO . undo)
314 (,ffi:SDLK_CUT . cut)
315 (,ffi:SDLK_COPY . copy)
316 (,ffi:SDLK_PASTE . paste)
317 (,ffi:SDLK_FIND . find)
318 (,ffi:SDLK_MUTE . mute)
319 (,ffi:SDLK_VOLUMEUP . volume-up)
320 (,ffi:SDLK_VOLUMEDOWN . volume-down)
321 (,ffi:SDLK_KP_COMMA . keypad-comma)
322 (,ffi:SDLK_KP_EQUALSAS400 . keypad-equalsas400)
323 (,ffi:SDLK_ALTERASE . alt-erase)
324 (,ffi:SDLK_SYSREQ . sysreq)
325 (,ffi:SDLK_CANCEL . cancel)
326 (,ffi:SDLK_CLEAR . clear)
327 (,ffi:SDLK_PRIOR . prior)
328 (,ffi:SDLK_RETURN2 . return-2)
329 (,ffi:SDLK_SEPARATOR . separator)
330 (,ffi:SDLK_OUT . out)
331 (,ffi:SDLK_OPER . oper)
332 (,ffi:SDLK_CLEARAGAIN . clear-again)
333 (,ffi:SDLK_CRSEL . crsel)
334 (,ffi:SDLK_EXSEL . exsel)
335 (,ffi:SDLK_KP_00 . keypad-00)
336 (,ffi:SDLK_KP_000 . keypad-000)
337 (,ffi:SDLK_THOUSANDSSEPARATOR . thousands-separator)
338 (,ffi:SDLK_DECIMALSEPARATOR . decimal-separator)
339 (,ffi:SDLK_CURRENCYUNIT . currency-unit)
340 (,ffi:SDLK_CURRENCYSUBUNIT . currency-subunit)
341 (,ffi:SDLK_KP_LEFTPAREN . keypad-left-paren)
342 (,ffi:SDLK_KP_RIGHTPAREN . keypad-right-paren)
343 (,ffi:SDLK_KP_LEFTBRACE . keypad-left-brace)
344 (,ffi:SDLK_KP_RIGHTBRACE . keypad-right-brace)
345 (,ffi:SDLK_KP_TAB . keypad-tab)
346 (,ffi:SDLK_KP_BACKSPACE . keypad-backspace)
347 (,ffi:SDLK_KP_A . keypad-a)
348 (,ffi:SDLK_KP_B . keypad-b)
349 (,ffi:SDLK_KP_C . keypad-c)
350 (,ffi:SDLK_KP_D . keypad-d)
351 (,ffi:SDLK_KP_E . keypad-e)
352 (,ffi:SDLK_KP_F . keypad-f)
353 (,ffi:SDLK_KP_XOR . keypad-xor)
354 (,ffi:SDLK_KP_POWER . keypad-power)
355 (,ffi:SDLK_KP_PERCENT . keypad-percent)
356 (,ffi:SDLK_KP_LESS . keypad-less)
357 (,ffi:SDLK_KP_GREATER . keypad-greater)
358 (,ffi:SDLK_KP_AMPERSAND . keypad-ampersand)
359 (,ffi:SDLK_KP_DBLAMPERSAND . keypad-double-ampersand)
360 (,ffi:SDLK_KP_VERTICALBAR . keypad-vertical-bar)
361 (,ffi:SDLK_KP_DBLVERTICALBAR . keypad-double-vertical-bar)
362 (,ffi:SDLK_KP_COLON . keypad-colon)
363 (,ffi:SDLK_KP_HASH . keypad-hash)
364 (,ffi:SDLK_KP_SPACE . keypad-space)
365 (,ffi:SDLK_KP_AT . keypad-at)
366 (,ffi:SDLK_KP_EXCLAM . keypad-exclam)
367 (,ffi:SDLK_KP_MEMSTORE . keypad-mem-store)
368 (,ffi:SDLK_KP_MEMRECALL . keypad-mem-recall)
369 (,ffi:SDLK_KP_MEMCLEAR . keypad-mem-clear)
370 (,ffi:SDLK_KP_MEMADD . keypad-mem-add)
371 (,ffi:SDLK_KP_MEMSUBTRACT . keypad-mem-subtract)
372 (,ffi:SDLK_KP_MEMMULTIPLY . keypad-mem-multiply)
373 (,ffi:SDLK_KP_MEMDIVIDE . keypad-mem-divide)
374 (,ffi:SDLK_KP_PLUSMINUS . keypad-plus-minus)
375 (,ffi:SDLK_KP_CLEAR . keypad-clear)
376 (,ffi:SDLK_KP_CLEARENTRY . keypad-clear-entry)
377 (,ffi:SDLK_KP_BINARY . keypad-binary)
378 (,ffi:SDLK_KP_OCTAL . keypad-octal)
379 (,ffi:SDLK_KP_DECIMAL . keypad-decimal)
380 (,ffi:SDLK_KP_HEXADECIMAL . keypad-hexadecimal)
381 (,ffi:SDLK_LCTRL . left-control)
382 (,ffi:SDLK_LSHIFT . left-shift)
383 (,ffi:SDLK_LALT . left-alt)
384 (,ffi:SDLK_LGUI . left-gui)
385 (,ffi:SDLK_RCTRL . right-control)
386 (,ffi:SDLK_RSHIFT . right-shift)
387 (,ffi:SDLK_RALT . right-alt)
388 (,ffi:SDLK_RGUI . right-gui)
389 (,ffi:SDLK_MODE . mode)
390 (,ffi:SDLK_AUDIONEXT . audio-next)
391 (,ffi:SDLK_AUDIOPREV . audio-prev)
392 (,ffi:SDLK_AUDIOSTOP . audio-stop)
393 (,ffi:SDLK_AUDIOPLAY . audio-play)
394 (,ffi:SDLK_AUDIOMUTE . audio-mute)
395 (,ffi:SDLK_MEDIASELECT . media-select)
396 (,ffi:SDLK_WWW . www)
397 (,ffi:SDLK_MAIL . mail)
398 (,ffi:SDLK_CALCULATOR . calculator)
399 (,ffi:SDLK_COMPUTER . computer)
400 (,ffi:SDLK_AC_SEARCH . ac-search)
401 (,ffi:SDLK_AC_HOME . ac-home)
402 (,ffi:SDLK_AC_BACK . ac-back)
403 (,ffi:SDLK_AC_FORWARD . ac-forward)
404 (,ffi:SDLK_AC_STOP . ac-stop)
405 (,ffi:SDLK_AC_REFRESH . ac-refresh)
406 (,ffi:SDLK_AC_BOOKMARKS . ac-bookmarks)
407 (,ffi:SDLK_BRIGHTNESSDOWN . brightness-down)
408 (,ffi:SDLK_BRIGHTNESSUP . brightness-up)
409 (,ffi:SDLK_DISPLAYSWITCH . display-switch)
410 (,ffi:SDLK_KBDILLUMTOGGLE . keyboard-illum-toggle)
411 (,ffi:SDLK_KBDILLUMDOWN . keyboard-illum-down)
412 (,ffi:SDLK_KBDILLUMUP . keyboard-illum-up)
413 (,ffi:SDLK_EJECT . eject)
414 (,ffi:SDLK_SLEEP . sleep))))
415
416(define scancode-map
417 (alist->hash-table
418 `((,ffi:SDL_SCANCODE_UNKNOWN . unknown)
419 (,ffi:SDL_SCANCODE_A . a)
420 (,ffi:SDL_SCANCODE_B . b)
421 (,ffi:SDL_SCANCODE_C . c)
422 (,ffi:SDL_SCANCODE_D . d)
423 (,ffi:SDL_SCANCODE_E . e)
424 (,ffi:SDL_SCANCODE_F . f)
425 (,ffi:SDL_SCANCODE_G . g)
426 (,ffi:SDL_SCANCODE_H . h)
427 (,ffi:SDL_SCANCODE_I . i)
428 (,ffi:SDL_SCANCODE_J . j)
429 (,ffi:SDL_SCANCODE_K . k)
430 (,ffi:SDL_SCANCODE_L . l)
431 (,ffi:SDL_SCANCODE_M . m)
432 (,ffi:SDL_SCANCODE_N . n)
433 (,ffi:SDL_SCANCODE_O . o)
434 (,ffi:SDL_SCANCODE_P . p)
435 (,ffi:SDL_SCANCODE_Q . q)
436 (,ffi:SDL_SCANCODE_R . r)
437 (,ffi:SDL_SCANCODE_S . s)
438 (,ffi:SDL_SCANCODE_T . t)
439 (,ffi:SDL_SCANCODE_U . u)
440 (,ffi:SDL_SCANCODE_V . v)
441 (,ffi:SDL_SCANCODE_W . w)
442 (,ffi:SDL_SCANCODE_X . x)
443 (,ffi:SDL_SCANCODE_Y . y)
444 (,ffi:SDL_SCANCODE_Z . z)
445 (,ffi:SDL_SCANCODE_1 . 1)
446 (,ffi:SDL_SCANCODE_2 . 2)
447 (,ffi:SDL_SCANCODE_3 . 3)
448 (,ffi:SDL_SCANCODE_4 . 4)
449 (,ffi:SDL_SCANCODE_5 . 5)
450 (,ffi:SDL_SCANCODE_6 . 6)
451 (,ffi:SDL_SCANCODE_7 . 7)
452 (,ffi:SDL_SCANCODE_8 . 8)
453 (,ffi:SDL_SCANCODE_9 . 9)
454 (,ffi:SDL_SCANCODE_0 . 0)
455 (,ffi:SDL_SCANCODE_RETURN . return)
456 (,ffi:SDL_SCANCODE_ESCAPE . escape)
457 (,ffi:SDL_SCANCODE_BACKSPACE . backspace)
458 (,ffi:SDL_SCANCODE_TAB . tab)
459 (,ffi:SDL_SCANCODE_SPACE . space)
460 (,ffi:SDL_SCANCODE_MINUS . minus)
461 (,ffi:SDL_SCANCODE_EQUALS . equals)
462 (,ffi:SDL_SCANCODE_LEFTBRACKET . left-bracket)
463 (,ffi:SDL_SCANCODE_RIGHTBRACKET . right-bracket)
464 (,ffi:SDL_SCANCODE_BACKSLASH . backslash)
465 (,ffi:SDL_SCANCODE_NONUSHASH . nonushash)
466 (,ffi:SDL_SCANCODE_SEMICOLON . semicolon)
467 (,ffi:SDL_SCANCODE_APOSTROPHE . apostrophe)
468 (,ffi:SDL_SCANCODE_GRAVE . grave)
469 (,ffi:SDL_SCANCODE_COMMA . comma)
470 (,ffi:SDL_SCANCODE_PERIOD . period)
471 (,ffi:SDL_SCANCODE_SLASH . slash)
472 (,ffi:SDL_SCANCODE_CAPSLOCK . caps-lock)
473 (,ffi:SDL_SCANCODE_F1 . f1)
474 (,ffi:SDL_SCANCODE_F2 . f2)
475 (,ffi:SDL_SCANCODE_F3 . f3)
476 (,ffi:SDL_SCANCODE_F4 . f4)
477 (,ffi:SDL_SCANCODE_F5 . f5)
478 (,ffi:SDL_SCANCODE_F6 . f6)
479 (,ffi:SDL_SCANCODE_F7 . f7)
480 (,ffi:SDL_SCANCODE_F8 . f8)
481 (,ffi:SDL_SCANCODE_F9 . f9)
482 (,ffi:SDL_SCANCODE_F10 . f10)
483 (,ffi:SDL_SCANCODE_F11 . f11)
484 (,ffi:SDL_SCANCODE_F12 . f12)
485 (,ffi:SDL_SCANCODE_PRINTSCREEN . print-screen)
486 (,ffi:SDL_SCANCODE_SCROLLLOCK . scroll-lock)
487 (,ffi:SDL_SCANCODE_PAUSE . pause)
488 (,ffi:SDL_SCANCODE_INSERT . insert)
489 (,ffi:SDL_SCANCODE_HOME . home)
490 (,ffi:SDL_SCANCODE_PAGEUP . page-up)
491 (,ffi:SDL_SCANCODE_DELETE . delete)
492 (,ffi:SDL_SCANCODE_END . end)
493 (,ffi:SDL_SCANCODE_PAGEDOWN . page-down)
494 (,ffi:SDL_SCANCODE_RIGHT . right)
495 (,ffi:SDL_SCANCODE_LEFT . left)
496 (,ffi:SDL_SCANCODE_DOWN . down)
497 (,ffi:SDL_SCANCODE_UP . up)
498 (,ffi:SDL_SCANCODE_NUMLOCKCLEAR . num-lock-clear)
499 (,ffi:SDL_SCANCODE_KP_DIVIDE . keypad-divide)
500 (,ffi:SDL_SCANCODE_KP_MULTIPLY . keypad-multiply)
501 (,ffi:SDL_SCANCODE_KP_MINUS . keypad-minus)
502 (,ffi:SDL_SCANCODE_KP_PLUS . keypad-plus)
503 (,ffi:SDL_SCANCODE_KP_ENTER . keypad-enter)
504 (,ffi:SDL_SCANCODE_KP_1 . keypad-1)
505 (,ffi:SDL_SCANCODE_KP_2 . keypad-2)
506 (,ffi:SDL_SCANCODE_KP_3 . keypad-3)
507 (,ffi:SDL_SCANCODE_KP_4 . keypad-4)
508 (,ffi:SDL_SCANCODE_KP_5 . keypad-5)
509 (,ffi:SDL_SCANCODE_KP_6 . keypad-6)
510 (,ffi:SDL_SCANCODE_KP_7 . keypad-7)
511 (,ffi:SDL_SCANCODE_KP_8 . keypad-8)
512 (,ffi:SDL_SCANCODE_KP_9 . keypad-9)
513 (,ffi:SDL_SCANCODE_KP_0 . keypad-0)
514 (,ffi:SDL_SCANCODE_KP_PERIOD . keypad-period)
515 (,ffi:SDL_SCANCODE_NONUSBACKSLASH . nonusbackslash)
516 (,ffi:SDL_SCANCODE_APPLICATION . application)
517 (,ffi:SDL_SCANCODE_POWER . power)
518 (,ffi:SDL_SCANCODE_KP_EQUALS . keypad-equals)
519 (,ffi:SDL_SCANCODE_F13 . f13)
520 (,ffi:SDL_SCANCODE_F14 . f14)
521 (,ffi:SDL_SCANCODE_F15 . f15)
522 (,ffi:SDL_SCANCODE_F16 . f16)
523 (,ffi:SDL_SCANCODE_F17 . f17)
524 (,ffi:SDL_SCANCODE_F18 . f18)
525 (,ffi:SDL_SCANCODE_F19 . f19)
526 (,ffi:SDL_SCANCODE_F20 . f20)
527 (,ffi:SDL_SCANCODE_F21 . f21)
528 (,ffi:SDL_SCANCODE_F22 . f22)
529 (,ffi:SDL_SCANCODE_F23 . f23)
530 (,ffi:SDL_SCANCODE_F24 . f24)
531 (,ffi:SDL_SCANCODE_EXECUTE . execute)
532 (,ffi:SDL_SCANCODE_HELP . help)
533 (,ffi:SDL_SCANCODE_MENU . menu)
534 (,ffi:SDL_SCANCODE_SELECT . select)
535 (,ffi:SDL_SCANCODE_STOP . stop)
536 (,ffi:SDL_SCANCODE_AGAIN . again)
537 (,ffi:SDL_SCANCODE_UNDO . undo)
538 (,ffi:SDL_SCANCODE_CUT . cut)
539 (,ffi:SDL_SCANCODE_COPY . copy)
540 (,ffi:SDL_SCANCODE_PASTE . paste)
541 (,ffi:SDL_SCANCODE_FIND . find)
542 (,ffi:SDL_SCANCODE_MUTE . mute)
543 (,ffi:SDL_SCANCODE_VOLUMEUP . volume-up)
544 (,ffi:SDL_SCANCODE_VOLUMEDOWN . volume-down)
545 (,ffi:SDL_SCANCODE_KP_COMMA . keypad-comma)
546 (,ffi:SDL_SCANCODE_KP_EQUALSAS400 . keypad-equalsas400)
547 (,ffi:SDL_SCANCODE_INTERNATIONAL1 . international-1)
548 (,ffi:SDL_SCANCODE_INTERNATIONAL2 . international-2)
549 (,ffi:SDL_SCANCODE_INTERNATIONAL3 . international-3)
550 (,ffi:SDL_SCANCODE_INTERNATIONAL4 . international-4)
551 (,ffi:SDL_SCANCODE_INTERNATIONAL5 . international-5)
552 (,ffi:SDL_SCANCODE_INTERNATIONAL6 . international-6)
553 (,ffi:SDL_SCANCODE_INTERNATIONAL7 . international-7)
554 (,ffi:SDL_SCANCODE_INTERNATIONAL8 . international-8)
555 (,ffi:SDL_SCANCODE_INTERNATIONAL9 . international-9)
556 (,ffi:SDL_SCANCODE_LANG1 . lang-1)
557 (,ffi:SDL_SCANCODE_LANG2 . lang-2)
558 (,ffi:SDL_SCANCODE_LANG3 . lang-3)
559 (,ffi:SDL_SCANCODE_LANG4 . lang-4)
560 (,ffi:SDL_SCANCODE_LANG5 . lang-5)
561 (,ffi:SDL_SCANCODE_LANG6 . lang-6)
562 (,ffi:SDL_SCANCODE_LANG7 . lang-7)
563 (,ffi:SDL_SCANCODE_LANG8 . lang-8)
564 (,ffi:SDL_SCANCODE_LANG9 . lang-9)
565 (,ffi:SDL_SCANCODE_ALTERASE . alt-erase)
566 (,ffi:SDL_SCANCODE_SYSREQ . sysreq)
567 (,ffi:SDL_SCANCODE_CANCEL . cancel)
568 (,ffi:SDL_SCANCODE_CLEAR . clear)
569 (,ffi:SDL_SCANCODE_PRIOR . prior)
570 (,ffi:SDL_SCANCODE_RETURN2 . return-2)
571 (,ffi:SDL_SCANCODE_SEPARATOR . separator)
572 (,ffi:SDL_SCANCODE_OUT . out)
573 (,ffi:SDL_SCANCODE_OPER . oper)
574 (,ffi:SDL_SCANCODE_CLEARAGAIN . clear-again)
575 (,ffi:SDL_SCANCODE_CRSEL . crsel)
576 (,ffi:SDL_SCANCODE_EXSEL . exsel)
577 (,ffi:SDL_SCANCODE_KP_00 . keypad-00)
578 (,ffi:SDL_SCANCODE_KP_000 . keypad-000)
579 (,ffi:SDL_SCANCODE_THOUSANDSSEPARATOR . thousands-separator)
580 (,ffi:SDL_SCANCODE_DECIMALSEPARATOR . decimal-separator)
581 (,ffi:SDL_SCANCODE_CURRENCYUNIT . currency-unit)
582 (,ffi:SDL_SCANCODE_CURRENCYSUBUNIT . current-subunit)
583 (,ffi:SDL_SCANCODE_KP_LEFTPAREN . keypad-left-paren)
584 (,ffi:SDL_SCANCODE_KP_RIGHTPAREN . keypad-right-paren)
585 (,ffi:SDL_SCANCODE_KP_LEFTBRACE . keypad-left-brace)
586 (,ffi:SDL_SCANCODE_KP_RIGHTBRACE . keypad-right-brace)
587 (,ffi:SDL_SCANCODE_KP_TAB . keypad-tab)
588 (,ffi:SDL_SCANCODE_KP_BACKSPACE . keypad-backspace)
589 (,ffi:SDL_SCANCODE_KP_A . keypad-a)
590 (,ffi:SDL_SCANCODE_KP_B . keypad-b)
591 (,ffi:SDL_SCANCODE_KP_C . keypad-c)
592 (,ffi:SDL_SCANCODE_KP_D . keypad-d)
593 (,ffi:SDL_SCANCODE_KP_E . keypad-e)
594 (,ffi:SDL_SCANCODE_KP_F . keypad-f)
595 (,ffi:SDL_SCANCODE_KP_XOR . keypad-xor)
596 (,ffi:SDL_SCANCODE_KP_POWER . keypad-power)
597 (,ffi:SDL_SCANCODE_KP_PERCENT . keypad-percent)
598 (,ffi:SDL_SCANCODE_KP_LESS . keypad-less)
599 (,ffi:SDL_SCANCODE_KP_GREATER . keypad-greater)
600 (,ffi:SDL_SCANCODE_KP_AMPERSAND . keypad-ampersand)
601 (,ffi:SDL_SCANCODE_KP_DBLAMPERSAND . keypad-double-ampersand)
602 (,ffi:SDL_SCANCODE_KP_VERTICALBAR . keypad-vertical-bar)
603 (,ffi:SDL_SCANCODE_KP_DBLVERTICALBAR . keypad-double-vertical-bar)
604 (,ffi:SDL_SCANCODE_KP_COLON . keypad-colon)
605 (,ffi:SDL_SCANCODE_KP_HASH . keypad-hash)
606 (,ffi:SDL_SCANCODE_KP_SPACE . keypad-space)
607 (,ffi:SDL_SCANCODE_KP_AT . keypad-at)
608 (,ffi:SDL_SCANCODE_KP_EXCLAM . keypad-exclam)
609 (,ffi:SDL_SCANCODE_KP_MEMSTORE . keypad-mem-store)
610 (,ffi:SDL_SCANCODE_KP_MEMRECALL . keypad-mem-recall)
611 (,ffi:SDL_SCANCODE_KP_MEMCLEAR . keypad-mem-clear)
612 (,ffi:SDL_SCANCODE_KP_MEMADD . keypad-mem-add)
613 (,ffi:SDL_SCANCODE_KP_MEMSUBTRACT . keypadd-mem-subtract)
614 (,ffi:SDL_SCANCODE_KP_MEMMULTIPLY . keypad-mem-multiply)
615 (,ffi:SDL_SCANCODE_KP_MEMDIVIDE . keypad-mem-divide)
616 (,ffi:SDL_SCANCODE_KP_PLUSMINUS . keypad-plus-minus)
617 (,ffi:SDL_SCANCODE_KP_CLEAR . keypad-clear)
618 (,ffi:SDL_SCANCODE_KP_CLEARENTRY . keypad-clear-entry)
619 (,ffi:SDL_SCANCODE_KP_BINARY . keypad-binary)
620 (,ffi:SDL_SCANCODE_KP_OCTAL . keypad-octal)
621 (,ffi:SDL_SCANCODE_KP_DECIMAL . keypad-decimal)
622 (,ffi:SDL_SCANCODE_KP_HEXADECIMAL . keypad-hexadecimal)
623 (,ffi:SDL_SCANCODE_LCTRL . left-control)
624 (,ffi:SDL_SCANCODE_LSHIFT . left-shift)
625 (,ffi:SDL_SCANCODE_LALT . left-alt)
626 (,ffi:SDL_SCANCODE_LGUI . left-gui)
627 (,ffi:SDL_SCANCODE_RCTRL . right-control)
628 (,ffi:SDL_SCANCODE_RSHIFT . right-shift)
629 (,ffi:SDL_SCANCODE_RALT . right-alt)
630 (,ffi:SDL_SCANCODE_RGUI . right-cui)
631 (,ffi:SDL_SCANCODE_MODE . mode)
632 (,ffi:SDL_SCANCODE_AUDIONEXT . audio-next)
633 (,ffi:SDL_SCANCODE_AUDIOPREV . audio-prev)
634 (,ffi:SDL_SCANCODE_AUDIOSTOP . audio-stop)
635 (,ffi:SDL_SCANCODE_AUDIOPLAY . audio-play)
636 (,ffi:SDL_SCANCODE_AUDIOMUTE . audio-mute)
637 (,ffi:SDL_SCANCODE_MEDIASELECT . media-select)
638 (,ffi:SDL_SCANCODE_WWW . www)
639 (,ffi:SDL_SCANCODE_MAIL . mail)
640 (,ffi:SDL_SCANCODE_CALCULATOR . calculator)
641 (,ffi:SDL_SCANCODE_COMPUTER . computer)
642 (,ffi:SDL_SCANCODE_AC_SEARCH . ac-search)
643 (,ffi:SDL_SCANCODE_AC_HOME . ac-home)
644 (,ffi:SDL_SCANCODE_AC_BACK . ac-back)
645 (,ffi:SDL_SCANCODE_AC_FORWARD . ac-forward)
646 (,ffi:SDL_SCANCODE_AC_STOP . ac-stop)
647 (,ffi:SDL_SCANCODE_AC_REFRESH . ac-refresh)
648 (,ffi:SDL_SCANCODE_AC_BOOKMARKS . ac-bookmarks)
649 (,ffi:SDL_SCANCODE_BRIGHTNESSDOWN . brightness-down)
650 (,ffi:SDL_SCANCODE_BRIGHTNESSUP . brightness-up)
651 (,ffi:SDL_SCANCODE_DISPLAYSWITCH . display-switch)
652 (,ffi:SDL_SCANCODE_KBDILLUMTOGGLE . keyboard-illum-toggle)
653 (,ffi:SDL_SCANCODE_KBDILLUMDOWN . keyboard-illum-down)
654 (,ffi:SDL_SCANCODE_KBDILLUMUP . keyboard-illum-up)
655 (,ffi:SDL_SCANCODE_EJECT . eject)
656 (,ffi:SDL_SCANCODE_SLEEP . sleep)
657 (,ffi:SDL_SCANCODE_APP1 . app-1)
658 (,ffi:SDL_SCANCODE_APP2 . app-2))))
659
660(define (parse-keyboard-event ptr)
661 (define types
662 (list uint32 ; type
663 uint32 ; timestamp
664 uint32 ; windowID
665 uint8 ; state
666 uint8 ; repeat
667 uint8 ; padding2
668 uint8 ; padding3
669 (list int ; scancode
670 int ; sym
671 uint16 ; mod
672 uint32))) ; unused
673
674 (define (mod->list mod)
675 (fold (lambda (pair prev)
676 (match pair
677 ((sym . bit)
678 (if (zero? (logand mod bit))
679 prev
680 (cons sym prev)))))
681 '()
682 `((left-shift . ,ffi:KMOD_LSHIFT)
683 (right-shift . ,ffi:KMOD_RSHIFT)
684 (left-control . ,ffi:KMOD_LCTRL)
685 (right-control . ,ffi:KMOD_RCTRL)
686 (left-alt . ,ffi:KMOD_LALT)
687 (right-alt . ,ffi:KMOD_RALT)
688 (left-gui . ,ffi:KMOD_LGUI)
689 (right-gui . ,ffi:KMOD_RGUI)
690 (num-lock . ,ffi:KMOD_NUM)
691 (caps-lock . ,ffi:KMOD_CAPS)
692 (alt-gr . ,ffi:KMOD_MODE))))
693
694 (define (keycode->symbol keycode)
695 (hash-ref keycode-map keycode))
696
697 (define (scancode->symbol scancode)
698 (hash-ref scancode-map scancode))
699
700 (match (parse-c-struct ptr types)
701 ((_ timestamp window-id state repeat _ _ (scancode sym mod _))
702 (make-keyboard-event timestamp
703 window-id
704 (= state ffi:SDL_PRESSED)
705 repeat
706 (keycode->symbol sym)
707 (scancode->symbol scancode)
708 (mod->list mod)))))
709
710\f
711;;;
2818dfb3
DT
712;;; Mouse
713;;;
714
715(define-record-type <mouse-button-event>
716 (make-mouse-button-event timestamp window-id which
717 button pressed? clicks x y)
718 mouse-button-event?
719 (timestamp mouse-button-event-timestamp)
720 (window-id mouse-button-event-window-id)
721 (which mouse-button-event-which)
722 (button mouse-button-event-button)
723 (pressed? mouse-button-event-pressed?)
724 (clicks mouse-button-event-clicks)
725 (x mouse-button-event-x)
726 (y mouse-button-event-y))
727
728(define (mouse-button-down-event? e)
729 "Return #t if E is a mouse button down event."
730 (and (mouse-button-event? e)
731 (mouse-button-event-pressed? e)))
732
733(define (mouse-button-up-event? e)
734 "Return #t if E is a mouse button up event."
735 (and (mouse-button-event? e)
736 (not (mouse-button-event-pressed? e))))
737
738(define (parse-mouse-button-event ptr)
739 (define types
740 (list uint32 ; type
741 uint32 ; timestamp
742 uint32 ; windowID
743 uint32 ; which
744 uint8 ; button
745 uint8 ; state
746 uint8 ; clicks
747 uint8 ; padding1
748 int32 ; x
749 int32)) ; y
750
751 (define (button->symbol n)
752 (list-ref '(left middle right x1 x2) (1- n)))
753
754 (match (parse-c-struct ptr types)
755 ((_ timestamp window-id which button state clicks _ x y)
756 (make-mouse-button-event timestamp
757 window-id
758 which
759 (button->symbol button)
760 (= state ffi:SDL_PRESSED)
761 clicks
762 x y))))
763
2fd54505
DT
764(define-record-type <mouse-motion-event>
765 (make-mouse-motion-event timestamp window-id which buttons x y x-rel y-rel)
766 mouse-motion-event?
767 (timestamp mouse-motion-event-timestamp)
768 (window-id mouse-motion-event-window-id)
769 (which mouse-motion-event-which)
770 (buttons mouse-motion-event-buttons)
771 (x mouse-motion-event-x)
772 (y mouse-motion-event-y)
773 (x-rel mouse-motion-event-x-rel)
774 (y-rel mouse-motion-event-y-rel))
775
776(define (parse-mouse-motion-event ptr)
777 (define types
778 (list uint32 ; type
779 uint32 ; timestamp
780 uint32 ; windowID
781 uint32 ; which
782 uint32 ; state
783 int32 ; x
784 int32 ; y
785 int32 ; xrel
786 int32)) ; yrel
787
788 (define (button-mask->list mask)
789 (fold (lambda (pair prev)
790 (match pair
791 ((sym . bit)
792 (if (zero? (logand mask bit))
793 prev
794 (cons sym prev)))))
795 '()
796 `((left . ,ffi:SDL_BUTTON_LMASK)
797 (middle . ,ffi:SDL_BUTTON_MMASK)
798 (right . ,ffi:SDL_BUTTON_RMASK)
799 (x1 . ,ffi:SDL_BUTTON_X1MASK)
800 (x2 . ,ffi:SDL_BUTTON_X2MASK))))
801
802 (match (parse-c-struct ptr types)
803 ((_ timestamp window-id which state x y xrel yrel)
804 (make-mouse-motion-event timestamp
805 window-id
806 which
807 (button-mask->list state)
808 x y xrel yrel))))
809
2818dfb3
DT
810\f
811;;;
0e442c81
DT
812;;; Joystick
813;;;
814
815(define-record-type <joystick-axis-event>
816 (make-joystick-axis-event timestamp which axis value)
817 joystick-axis-event?
818 (timestamp joystick-axis-event-timestamp)
819 (which joystick-axis-event-which)
820 (axis joystick-axis-event-axis)
821 (value joystick-axis-event-value))
822
823(define (parse-joystick-axis-event ptr)
824 (define types
825 (list uint32 ; type
826 uint32 ; timestamp
827 int32 ; which
828 uint8 ; axis
829 uint8 ; padding1
830 uint8 ; padding2
831 uint8 ; padding3
832 int16 ; value
833 uint16)) ; padding4
834
835 (match (parse-c-struct ptr types)
836 ((_ timestamp which axis _ _ _ value _)
837 (make-joystick-axis-event timestamp which axis value))))
838
28503ef3
DT
839(define-record-type <joystick-button-event>
840 (make-joystick-button-event timestamp which button pressed?)
841 joystick-button-event?
842 (timestamp joystick-button-event-timestamp)
843 (which joystick-button-which)
844 (button joystick-button-event-button)
845 (pressed? joystick-button-event-pressed?))
846
847(define (joystick-button-down-event? e)
848 "Return #t if E is a joystick button press event."
849 (and (joystick-button-event? e)
850 (joystick-button-event-pressed? e)))
851
852(define (joystick-button-up-event? e)
853 "Return #t if E is a joystick button release event."
854 (and (joystick-button-event? e)
855 (not (joystick-button-event-pressed? e))))
856
857(define (parse-joystick-button-event ptr)
858 (define types
859 (list uint32 ; type
860 uint32 ; timestamp
861 int32 ; which
862 uint8 ; button
863 uint8 ; state
864 uint8 ; padding1
865 uint8)) ; padding2
866
867 (match (parse-c-struct ptr types)
868 ((_ timestamp which button state _ _)
869 (make-joystick-button-event timestamp which button
870 (= state ffi:SDL_PRESSED)))))
871
0e442c81
DT
872\f
873;;;
2751b1a7
DT
874;;; Event management
875;;;
876
877(define (poll-event)
878 (let* ((e (make-sdl-event))
879 (ptr (bytevector->pointer e))
880 (result (ffi:sdl-poll-event ptr)))
881 (and (= result 1)
882 (let ((type (sdl-event-type e)))
883 (cond
884 ((= type ffi:SDL_WINDOWEVENT)
885 (parse-window-event ptr))
886 ((or (= type ffi:SDL_KEYDOWN)
887 (= type ffi:SDL_KEYUP))
888 (parse-keyboard-event ptr))
2818dfb3
DT
889 ((or (= type ffi:SDL_MOUSEBUTTONDOWN)
890 (= type ffi:SDL_MOUSEBUTTONUP))
891 (parse-mouse-button-event ptr))
2fd54505
DT
892 ((= type ffi:SDL_MOUSEMOTION)
893 (parse-mouse-motion-event ptr))
0e442c81
DT
894 ((= type ffi:SDL_JOYAXISMOTION)
895 (parse-joystick-axis-event ptr))
28503ef3
DT
896 ((or (= type ffi:SDL_JOYBUTTONDOWN)
897 (= type ffi:SDL_JOYBUTTONUP))
898 (parse-joystick-button-event ptr))
2751b1a7 899 (else 'fixme:unsupported-event))))))