events: joystick: Add button bindings.
authorDavid Thompson <dthompson2@worcester.edu>
Thu, 17 Dec 2015 02:25:58 +0000 (21:25 -0500)
committerDavid Thompson <dthompson2@worcester.edu>
Thu, 17 Dec 2015 02:28:06 +0000 (21:28 -0500)
* sdl2/events.scm (<joystick-button-event>): New record type.
  (make-joystick-button-event, joystick-button-event?,
  joystick-button-down-event?, joystick-button-up-event?,
  joystick-button-event-timestamp, joystick-button-event-which,
  joystick-button-event-button, joystick-button-event-pressed?,
  parse-joystick-button-event): New procedures.
  (poll-event): Add support for joystick button events.

sdl2/events.scm

index 883f32b..cc78710 100644 (file)
             joystick-axis-event-axis
             joystick-axis-event-value
 
+            make-joystick-button-event
+            joystick-button-event?
+            joystick-button-down-event?
+            joystick-button-up-event?
+            joystick-button-event-timestamp
+            joystick-button-event-which
+            joystick-button-event-button
+            joystick-button-event-pressed?
+
             poll-event))
 
 (define (make-sdl-event)
     ((_ timestamp which axis _ _ _ value _)
      (make-joystick-axis-event timestamp which axis value))))
 
+(define-record-type <joystick-button-event>
+  (make-joystick-button-event timestamp which button pressed?)
+  joystick-button-event?
+  (timestamp joystick-button-event-timestamp)
+  (which joystick-button-which)
+  (button joystick-button-event-button)
+  (pressed? joystick-button-event-pressed?))
+
+(define (joystick-button-down-event? e)
+  "Return #t if E is a joystick button press event."
+  (and (joystick-button-event? e)
+       (joystick-button-event-pressed? e)))
+
+(define (joystick-button-up-event? e)
+  "Return #t if E is a joystick button release event."
+  (and (joystick-button-event? e)
+       (not (joystick-button-event-pressed? e))))
+
+(define (parse-joystick-button-event ptr)
+  (define types
+    (list uint32  ; type
+          uint32  ; timestamp
+          int32   ; which
+          uint8   ; button
+          uint8   ; state
+          uint8   ; padding1
+          uint8)) ; padding2
+
+  (match (parse-c-struct ptr types)
+    ((_ timestamp which button state _ _)
+     (make-joystick-button-event timestamp which button
+                                 (= state ffi:SDL_PRESSED)))))
+
 \f
 ;;;
 ;;; Event management
              (parse-mouse-motion-event ptr))
             ((= type ffi:SDL_JOYAXISMOTION)
              (parse-joystick-axis-event ptr))
+            ((or (= type ffi:SDL_JOYBUTTONDOWN)
+                 (= type ffi:SDL_JOYBUTTONUP))
+             (parse-joystick-button-event ptr))
             (else 'fixme:unsupported-event))))))