diff options
author | David Thompson <dthompson@vistahigherlearning.com> | 2016-12-30 11:55:44 -0500 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2016-12-31 13:31:18 -0500 |
commit | 1800240eb3154c5460907917ae3f4659002efc47 (patch) | |
tree | 3cc5f5365d34fb441a3ab00e6698201abaf57bda | |
parent | b833f588b3d9528e7fc650d841ee7a46b6c0ba78 (diff) |
Add bindings for game controller input.
* sdl2.scm (%default-init-flags): Add 'joystick' to default flags.
(sdl-init): Translate to 'joystick' to SDL2 init bit flag.
* sdl2/bindings.scm (SDL_INIT_JOYSTICK, SDL_JOYSTICK_POWER_UNKNOWN,
SDL_JOYSTICK_POWER_EMPTY, SDL_JOYSTICK_POWER_LOW,
SDL_JOYSTICK_POWER_MEDIUM, SDL_JOYSTICK_POWER_FULL,
SDL_JOYSTICK_POWER_WIRED, SDL_JOYSTICK_POWER_MAX,
SDL_CONTROLLER_AXIS_INVALID, SDL_CONTROLLER_AXIS_LEFTX,
SDL_CONTROLLER_AXIS_LEFTY, SDL_CONTROLLER_AXIS_RIGHTX,
SDL_CONTROLLER_AXIS_RIGHTY, SDL_CONTROLLER_AXIS_TRIGGERLEFT,
SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_CONTROLLER_AXIS_MAX,
SDL_CONTROLLER_BUTTON_INVALID, SDL_CONTROLLER_BUTTON_A,
SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_X,
SDL_CONTROLLER_BUTTON_Y, SDL_CONTROLLER_BUTTON_BACK,
SDL_CONTROLLER_BUTTON_GUIDE, SDL_CONTROLLER_BUTTON_START,
SDL_CONTROLLER_BUTTON_LEFTSTICK, SDL_CONTROLLER_BUTTON_RIGHTSTICK,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_DOWN,
SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
SDL_CONTROLLER_BUTTON_MAX): New variables.
(sdl-joystick-open, sdl-joystick-close,
sdl-joystick-current-power-level, sdl-joystick-event-state,
sdl-joystick-from-instance-id, sdl-joystick-get-attached,
sdl-joystick-get-axis, sdl-joystick-get-ball, sdl-joystick-get-button,
sdl-joystick-get-device-guid, sdl-joystick-get-guide,
sdl-joystick-get-guid-from-string, sdl-joystick-get-guid-string,
sdl-joystick-get-hat, sdl-joystick-instance-id, sdl-joystick-name,
sdl-joystick-name-for-index, sdl-joystick-num-axes,
sdl-joystick-num-balls, sdl-joystick-num-buttons, sdl-joystick-num-hats,
sdl-num-joysticks, sdl-joystick-update, sdl-game-controller-add-mapping,
sdl-game-controller-open, sdl-game-controller-close,
sdl-game-controller-event-state, sdl-game-controller-from-instance-id,
sdl-game-controller-get-attached, sdl-game-controller-get-axis,
sdl-game-controller-get-axis-from-string,
sdl-game-controller-get-string-from-axis,
sdl-game-controller-get-string-for-axis, sdl-game-controller-get-button,
sdl-game-controller-get-button-from-string,
sdl-game-controller-get-string-from-button,
sdl-game-controller-get-joystick, sdl-game-controller-mapping,
sdl-game-controller-mapping-for-guid, sdl-game-controller-name,
sdl-game-controller-name-for-index, sdl-game-controller-update,
sdl-is-game-controller): New procedures.
* sdl2/events.scm (make-joystick-ball-event, joystick-ball-event?,
joystick-ball-event-timestamp, joystick-ball-event-which,
joystick-ball-event-ball, joystick-ball-event-x-rel,
joystick-ball-event-y-rel, make-joystick-hat-event, joystick-hat-event?,
joystick-hat-event-timestamp, joystick-hat-event-which,
joystick-hat-event-hat, joystick-hat-event-value,
make-joystick-device-event, joystick-device-event?,
joystick-device-event-timestamp, joystick-device-event-which,
joystick-device-event-action, make-controller-axis-event,
controller-axis-event?, controller-axis-event-timestamp,
controller-axis-event-which, controller-axis-event-axis,
controller-axis-event-value, make-controller-button-event,
controller-button-event?, controller-button-down-event?,
controller-button-up-event?, controller-button-event-timestamp,
controller-button-event-which, controller-button-event-button,
controller-button-event-pressed?, make-controller-device-event,
controller-device-event?, controller-device-event-timestamp,
controller-device-event-which, controller-device-event-action,
parse-joystick-ball-event, parse-joystick-hat-event,
parse-joystick-device-event, parse-controller-axis-event,
parse-controller-button-event, parse-controller-device-event): New
procedures.
(poll-event): Parse joystick/controller events.
* sdl2/input/joystick.scm: New file.
* sdl2/input/game-controller.scm: New file.
* Makefile.am (SOURCES): Add them
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | sdl2.scm | 3 | ||||
-rw-r--r-- | sdl2/bindings.scm | 172 | ||||
-rw-r--r-- | sdl2/events.scm | 239 | ||||
-rw-r--r-- | sdl2/input/game-controller.scm | 155 | ||||
-rw-r--r-- | sdl2/input/joystick.scm | 114 |
6 files changed, 684 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am index 9bddc71..4412978 100644 --- a/Makefile.am +++ b/Makefile.am @@ -48,6 +48,8 @@ SOURCES = \ sdl2/render.scm \ sdl2/video.scm \ sdl2/events.scm \ + sdl2/input/joystick.scm \ + sdl2/input/game-controller.scm \ sdl2/input/text.scm if WITH_LIBSDL2_IMAGE @@ -45,7 +45,7 @@ color-a)) (define %default-init-flags - '(timer audio video haptic game-controller events)) + '(timer audio video joystick haptic game-controller events)) (define (sdl-error-string) "Return the current SDL error string." @@ -77,6 +77,7 @@ The possible flags are 'timer', 'audio', 'video', 'haptic', ('video ffi:SDL_INIT_VIDEO) ('haptic ffi:SDL_INIT_HAPTIC) ('game-controller ffi:SDL_INIT_GAMECONTROLLER) + ('joystick ffi:SDL_INIT_JOYSTICK) ('events ffi:SDL_INIT_EVENTS)) subsystems)))) (unless (zero? (ffi:sdl-init flags)) diff --git a/sdl2/bindings.scm b/sdl2/bindings.scm index e48ce64..1eb4813 100644 --- a/sdl2/bindings.scm +++ b/sdl2/bindings.scm @@ -71,6 +71,7 @@ RETURN-TYPE and accept arguments of ARG-TYPES." (define-public SDL_INIT_TIMER #x00000001) (define-public SDL_INIT_AUDIO #x00000010) (define-public SDL_INIT_VIDEO #x00000020) +(define-public SDL_INIT_JOYSTICK #x00000200) (define-public SDL_INIT_HAPTIC #x00001000) (define-public SDL_INIT_GAMECONTROLLER #x00002000) (define-public SDL_INIT_EVENTS #x00004000) @@ -949,3 +950,174 @@ RETURN-TYPE and accept arguments of ARG-TYPES." (define-public AUDIO_F32LSB #x8120) (define-public AUDIO_F32MSB #x9120) (define-public AUDIO_F32 AUDIO_F32LSB) + + +;;; +;;; Joystick +;;; + +(define-public SDL_JOYSTICK_POWER_UNKNOWN -1) +(define-public SDL_JOYSTICK_POWER_EMPTY 0) +(define-public SDL_JOYSTICK_POWER_LOW 1) +(define-public SDL_JOYSTICK_POWER_MEDIUM 2) +(define-public SDL_JOYSTICK_POWER_FULL 3) +(define-public SDL_JOYSTICK_POWER_WIRED 4) +(define-public SDL_JOYSTICK_POWER_MAX 5) + +(define-foreign sdl-joystick-open + '* "SDL_JoystickOpen" (list int)) + +(define-foreign sdl-joystick-close + void "SDL_JoystickClose" '(*)) + +(define-foreign sdl-joystick-current-power-level + int "SDL_JoystickCurrentPowerLevel" '(*)) + +(define-foreign sdl-joystick-event-state + int "SDL_JoystickEventState" (list int)) + +(define-foreign sdl-joystick-from-instance-id + '* "SDL_JoystickFromInstanceID" (list int32)) + +(define-foreign sdl-joystick-get-attached + sdl-bool "SDL_JoystickGetAttached" '(*)) + +(define-foreign sdl-joystick-get-axis + int16 "SDL_JoystickGetAxis" (list '* int)) + +(define-foreign sdl-joystick-get-ball + int "SDL_JoystickGetBall" (list '* int '* '*)) + +(define-foreign sdl-joystick-get-button + uint8 "SDL_JoystickGetButton" (list '* int)) + +(define-foreign sdl-joystick-get-device-guid + '* "SDL_JoystickGetDeviceGUID" (list int)) + +(define-foreign sdl-joystick-get-guid + '* "SDL_JoystickGetGUID" (list int)) + +(define-foreign sdl-joystick-get-guid-from-string + '* "SDL_JoystickGetGUIDFromString" '(*)) + +(define-foreign sdl-joystick-get-guid-string + void "SDL_JoystickGetGUIDString" (list '* '* int)) + +(define-foreign sdl-joystick-get-hat + uint8 "SDL_JoystickGetHat" (list '* int)) + +(define-foreign sdl-joystick-instance-id + int32 "SDL_JoystickInstanceID" '(*)) + +(define-foreign sdl-joystick-name + '* "SDL_JoystickName" '(*)) + +(define-foreign sdl-joystick-name-for-index + '* "SDL_JoystickNameForIndex" (list int)) + +(define-foreign sdl-joystick-num-axes + int "SDL_JoystickNumAxes" '(*)) + +(define-foreign sdl-joystick-num-balls + int "SDL_JoystickNumBalls" '(*)) + +(define-foreign sdl-joystick-num-buttons + int "SDL_JoystickNumButtons" '(*)) + +(define-foreign sdl-joystick-num-hats + int "SDL_JoystickNumHats" '(*)) + +(define-foreign sdl-num-joysticks + int "SDL_NumJoysticks" '()) + +(define-foreign sdl-joystick-update + void "SDL_JoystickUpdate" '()) + + +;;; +;;; Game Controllers +;;; + +(define-public SDL_CONTROLLER_AXIS_INVALID -1) +(define-public SDL_CONTROLLER_AXIS_LEFTX 0) +(define-public SDL_CONTROLLER_AXIS_LEFTY 1) +(define-public SDL_CONTROLLER_AXIS_RIGHTX 2) +(define-public SDL_CONTROLLER_AXIS_RIGHTY 3) +(define-public SDL_CONTROLLER_AXIS_TRIGGERLEFT 4) +(define-public SDL_CONTROLLER_AXIS_TRIGGERRIGHT 5) +(define-public SDL_CONTROLLER_AXIS_MAX 6) + +(define-public SDL_CONTROLLER_BUTTON_INVALID -1) +(define-public SDL_CONTROLLER_BUTTON_A 0) +(define-public SDL_CONTROLLER_BUTTON_B 1) +(define-public SDL_CONTROLLER_BUTTON_X 2) +(define-public SDL_CONTROLLER_BUTTON_Y 3) +(define-public SDL_CONTROLLER_BUTTON_BACK 4) +(define-public SDL_CONTROLLER_BUTTON_GUIDE 5) +(define-public SDL_CONTROLLER_BUTTON_START 6) +(define-public SDL_CONTROLLER_BUTTON_LEFTSTICK 7) +(define-public SDL_CONTROLLER_BUTTON_RIGHTSTICK 8) +(define-public SDL_CONTROLLER_BUTTON_LEFTSHOULDER 9) +(define-public SDL_CONTROLLER_BUTTON_RIGHTSHOULDER 10) +(define-public SDL_CONTROLLER_BUTTON_DPAD_UP 11) +(define-public SDL_CONTROLLER_BUTTON_DPAD_DOWN 12) +(define-public SDL_CONTROLLER_BUTTON_DPAD_LEFT 13) +(define-public SDL_CONTROLLER_BUTTON_DPAD_RIGHT 14) +(define-public SDL_CONTROLLER_BUTTON_MAX 15) + +(define-foreign sdl-game-controller-add-mapping + int "SDL_GameControllerAddMapping" '(*)) + +(define-foreign sdl-game-controller-open + '* "SDL_GameControllerOpen" (list int)) + +(define-foreign sdl-game-controller-close + void "SDL_GameControllerClose" '(*)) + +(define-foreign sdl-game-controller-event-state + int "SDL_GameControllerEventState" (list int)) + +(define-foreign sdl-game-controller-from-instance-id + '* "SDL_GameControllerFromInstanceID" (list int32)) + +(define-foreign sdl-game-controller-get-attached + sdl-bool "SDL_GameControllerGetAttached" '(*)) + +(define-foreign sdl-game-controller-get-axis + int16 "SDL_GameControllerGetAxis" (list '* int)) + +(define-foreign sdl-game-controller-get-axis-from-string + int "SDL_GameControllerGetAxisFromString" '(*)) + +(define-foreign sdl-game-controller-get-string-for-axis + '* "SDL_GameControllerGetStringForAxis" (list int)) + +(define-foreign sdl-game-controller-get-button + uint8 "SDL_GameControllerGetButton" (list '* int)) + +(define-foreign sdl-game-controller-get-button-from-string + int "SDL_GameControllerGetButtonFromString" '(*)) + +(define-foreign sdl-game-controller-get-string-for-button + '* "SDL_GameControllerGetStringForButton" (list int)) + +(define-foreign sdl-game-controller-get-joystick + '* "SDL_GameControllerGetJoystick" '(*)) + +(define-foreign sdl-game-controller-mapping + '* "SDL_GameControllerMapping" '(*)) + +(define-foreign sdl-game-controller-mapping-for-guid + '* "SDL_GameControllerMappingForGUID" '(*)) + +(define-foreign sdl-game-controller-name + '* "SDL_GameControllerName" '(*)) + +(define-foreign sdl-game-controller-name-for-index + '* "SDL_GameControllerNameForIndex" (list int)) + +(define-foreign sdl-game-controller-update + void "SDL_GameControllerUpdate" '()) + +(define-foreign sdl-is-game-controller + sdl-bool "SDL_IsGameController" (list int)) diff --git a/sdl2/events.scm b/sdl2/events.scm index c9e391b..f207348 100644 --- a/sdl2/events.scm +++ b/sdl2/events.scm @@ -116,6 +116,49 @@ joystick-button-event-button joystick-button-event-pressed? + make-joystick-ball-event + joystick-ball-event? + joystick-ball-event-timestamp + joystick-ball-event-which + joystick-ball-event-ball + joystick-ball-event-x-rel + joystick-ball-event-y-rel + + make-joystick-hat-event + joystick-hat-event? + joystick-hat-event-timestamp + joystick-hat-event-which + joystick-hat-event-hat + joystick-hat-event-value + + make-joystick-device-event + joystick-device-event? + joystick-device-event-timestamp + joystick-device-event-which + joystick-device-event-action + + make-controller-axis-event + controller-axis-event? + controller-axis-event-timestamp + controller-axis-event-which + controller-axis-event-axis + controller-axis-event-value + + make-controller-button-event + controller-button-event? + controller-button-down-event? + controller-button-up-event? + controller-button-event-timestamp + controller-button-event-which + controller-button-event-button + controller-button-event-pressed? + + make-controller-device-event + controller-device-event? + controller-device-event-timestamp + controller-device-event-which + controller-device-event-action + poll-event)) (define (make-sdl-event) @@ -1000,6 +1043,186 @@ (make-joystick-button-event timestamp which button (= state ffi:SDL_PRESSED))))) +(define-record-type <joystick-ball-event> + (make-joystick-ball-event timestamp which ball x-rel y-rel) + joystick-ball-event? + (timestamp joystick-ball-event-timestamp) + (which joystick-ball-event-which) + (ball joystick-ball-event-ball) + (x-rel joystick-ball-event-x-rel) + (y-rel joystick-ball-event-y-rel)) + +(define (parse-joystick-ball-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32 ; which + uint8 ; ball + uint8 ; padding1 + uint8 ; padding2 + uint8 ; padding3 + int16 ; xrel + int16)) ; yrel + + (match (parse-c-struct ptr types) + ((_ timestamp which ball xrel yrel) + (make-joystick-ball-event timestamp which ball xrel yrel)))) + +(define-record-type <joystick-hat-event> + (make-joystick-hat-event timestamp which hat value) + joystick-hat-event? + (timestamp joystick-hat-event-timestamp) + (which joystick-hat-event-which) + (hat joystick-hat-event-hat) + (value joystick-hat-event-value)) + +(define (parse-joystick-hat-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32 ; which + uint8 ; hat + uint8)) ; value + + (match (parse-c-struct ptr types) + ((_ timestamp which hat value) + ;; TODO: Parse 'value' and convert to symbol. + (make-joystick-hat-event timestamp which hat value)))) + +(define-record-type <joystick-device-event> + (make-joystick-device-event timestamp which action) + joystick-device-event? + (timestamp joystick-device-event-timestamp) + (which joystick-device-event-which) + (action joystick-device-event-action)) ; added or removed + +(define (parse-joystick-device-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32)) ; which + + (match (parse-c-struct ptr types) + ((type timestamp which) + (make-joystick-device-event timestamp which + (if (= type ffi:SDL_JOYDEVICEADDED) + 'added + 'removed))))) + + +;;; +;;; Game Controller +;;; + +(define-record-type <controller-axis-event> + (make-controller-axis-event timestamp which axis value) + controller-axis-event? + (timestamp controller-axis-event-timestamp) + (which controller-axis-event-which) + (axis controller-axis-event-axis) + (value controller-axis-event-value)) + +(define (parse-controller-axis-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32 ; which + uint8 ; axis + uint8 ; padding1 + uint8 ; padding2 + uint8 ; padding3 + int16)) ; value + + (define (int->axis-symbol axis) + (cond + ((= axis ffi:SDL_CONTROLLER_AXIS_LEFTX) 'left-x) + ((= axis ffi:SDL_CONTROLLER_AXIS_LEFTY) 'left-y) + ((= axis ffi:SDL_CONTROLLER_AXIS_RIGHTX) 'right-x) + ((= axis ffi:SDL_CONTROLLER_AXIS_RIGHTY) 'right-y) + ((= axis ffi:SDL_CONTROLLER_AXIS_TRIGGERLEFT) 'trigger-left) + ((= axis ffi:SDL_CONTROLLER_AXIS_TRIGGERRIGHT) 'trigger-right))) + + (match (parse-c-struct ptr types) + ((_ timestamp which axis _ _ _ value) + (make-controller-axis-event timestamp + which + (int->axis-symbol axis) + value)))) + +(define-record-type <controller-button-event> + (make-controller-button-event timestamp which button pressed?) + controller-button-event? + (timestamp controller-button-event-timestamp) + (which controller-button-event-which) + (button controller-button-event-button) + (pressed? controller-button-event-pressed?)) + +(define (controller-button-down-event? event) + (and (controller-button-event? event) + (controller-button-event-pressed? event))) + +(define (controller-button-up-event? event) + (and (controller-button-event? event) + (not (controller-button-event-pressed? event)))) + +(define (parse-controller-button-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32 ; which + uint8 ; button + uint8)) ; state + + (define (int->button-symbol button) + (cond + ((= button ffi:SDL_CONTROLLER_BUTTON_A) 'a) + ((= button ffi:SDL_CONTROLLER_BUTTON_B) 'b) + ((= button ffi:SDL_CONTROLLER_BUTTON_X) 'x) + ((= button ffi:SDL_CONTROLLER_BUTTON_Y) 'y) + ((= button ffi:SDL_CONTROLLER_BUTTON_BACK) 'back) + ((= button ffi:SDL_CONTROLLER_BUTTON_GUIDE) 'guide) + ((= button ffi:SDL_CONTROLLER_BUTTON_START) 'start) + ((= button ffi:SDL_CONTROLLER_BUTTON_LEFTSTICK) 'left-stick) + ((= button ffi:SDL_CONTROLLER_BUTTON_RIGHTSTICK) 'right-stick) + ((= button ffi:SDL_CONTROLLER_BUTTON_LEFTSHOULDER) 'left-shoulder) + ((= button ffi:SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) 'right-shoulder) + ((= button ffi:SDL_CONTROLLER_BUTTON_DPAD_UP) 'dpad-up) + ((= button ffi:SDL_CONTROLLER_BUTTON_DPAD_DOWN) 'dpad-down) + ((= button ffi:SDL_CONTROLLER_BUTTON_DPAD_LEFT) 'dpad-left) + ((= button ffi:SDL_CONTROLLER_BUTTON_DPAD_RIGHT) 'dpad-right))) + + (match (parse-c-struct ptr types) + ((_ timestamp which button state) + (make-controller-button-event timestamp + which + (int->button-symbol button) + (= state ffi:SDL_PRESSED))))) + +(define-record-type <controller-device-event> + (make-controller-device-event timestamp which action) + controller-device-event? + (timestamp controller-device-event-timestamp) + (which controller-device-event-which) + (action controller-device-event-action)) + +(define (parse-controller-device-event ptr) + (define types + (list uint32 ; type + uint32 ; timestamp + int32)) ; which + + (match (parse-c-struct ptr types) + ((type timestamp which) + (make-controller-device-event timestamp + which + (cond + ((= type ffi:SDL_CONTROLLERDEVICEADDED) + 'added) + ((= type ffi:SDL_CONTROLLERDEVICEREMOVED) + 'removed) + ((= type ffi:SDL_CONTROLLERDEVICEREMAPPED) + 'remapped)))))) + ;;; ;;; Event management @@ -1028,7 +1251,23 @@ (parse-mouse-motion-event ptr)) ((= type ffi:SDL_JOYAXISMOTION) (parse-joystick-axis-event ptr)) + ((= type ffi:SDL_JOYBALLMOTION) + (parse-joystick-ball-event ptr)) + ((= type ffi:SDL_JOYHATMOTION) + (parse-joystick-hat-event ptr)) ((or (= type ffi:SDL_JOYBUTTONDOWN) (= type ffi:SDL_JOYBUTTONUP)) (parse-joystick-button-event ptr)) + ((or (= type ffi:SDL_JOYDEVICEADDED) + (= type ffi:SDL_JOYDEVICEREMOVED)) + (parse-joystick-device-event ptr)) + ((= type ffi:SDL_CONTROLLERAXISMOTION) + (parse-controller-axis-event ptr)) + ((or (= type ffi:SDL_CONTROLLERBUTTONDOWN) + (= type ffi:SDL_CONTROLLERBUTTONUP)) + (parse-controller-button-event ptr)) + ((or (= type ffi:SDL_CONTROLLERDEVICEADDED) + (= type ffi:SDL_CONTROLLERDEVICEREMOVED) + (= type ffi:SDL_CONTROLLERDEVICEREMAPPED)) + (parse-controller-device-event ptr)) (else 'fixme:unsupported-event)))))) diff --git a/sdl2/input/game-controller.scm b/sdl2/input/game-controller.scm new file mode 100644 index 0000000..66441a9 --- /dev/null +++ b/sdl2/input/game-controller.scm @@ -0,0 +1,155 @@ +;;; guile-sdl2 --- FFI bindings for SDL2 +;;; Copyright © 2016 David Thompson <davet@gnu.org> +;;; +;;; This file is part of guile-sdl2. +;;; +;;; Guile-sdl2 is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Lesser General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; Guile-sdl2 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 Lesser General Public +;;; License along with guile-sdl2. If not, see +;;; <http://www.gnu.org/licenses/>. + +;;; Commentary: +;; +;; Joystick input. +;; +;;; Code: + +(define-module (sdl2 input game-controller) + #:use-module (ice-9 match) + #:use-module (srfi srfi-9) + #:use-module (srfi srfi-9 gnu) + #:use-module (system foreign) + #:use-module ((sdl2 bindings) #:prefix ffi:) + #:use-module (sdl2) + #:export (open-game-controller + close-game-controller + game-controller? + game-controller-attached? + game-controller-joystick + game-controller-name + game-controller-axis + game-controller-button-pressed? + game-controller-index?)) + +(define-wrapped-pointer-type <game-controller> + game-controller? + wrap-game-controller unwrap-game-controller + (lambda (game-controller port) + (format port "#<game-controller ~a>" + (game-controller-name game-controller)))) + +(define-record-type <game-controller> + (make-game-controller pointer joystick) + game-controller? + (pointer unwrap-game-controller) + (joystick %game-controller-joystick)) + +(set-record-type-printer! <game-controller> + (lambda (game-controller port) + (format port "#<game-controller ~s>" + (game-controller-name game-controller)))) + +(define wrap-joystick (@@ (sdl2 input joystick) wrap-joystick)) + +(define (open-game-controller joystick-index) + "Return a game controller object for the physical joystick device +associated with ." + (let ((ptr (ffi:sdl-game-controller-open joystick-index))) + (if (null-pointer? ptr) + (sdl-error "open-game-controller" "failed to open game controller") + (let ((joystick (wrap-joystick + (ffi:sdl-game-controller-get-joystick ptr)))) + (make-game-controller ptr joystick))))) + +(define (close-game-controller controller) + "Close CONTROLLER." + (ffi:sdl-game-controller-close (unwrap-game-controller controller))) + +(define (game-controller-joystick controller) + "Return the underlying joystick object associated with CONTROLLER." + (%game-controller-joystick controller)) + +(define (game-controller-attached? controller) + "Return #t if CONTROLLER is currently in use." + (= (ffi:sdl-game-controller-get-attached (unwrap-game-controller controller)) 1)) + +(define (game-controller-name controller) + "Return the human readable name for CONTROLLER." + (pointer->string (ffi:sdl-game-controller-name (unwrap-game-controller controller)))) + +(define (axis-symbol->int axis) + (match axis + ('left-x ffi:SDL_CONTROLLER_AXIS_LEFTX) + ('left-y ffi:SDL_CONTROLLER_AXIS_LEFTY) + ('right-x ffi:SDL_CONTROLLER_AXIS_RIGHTX) + ('right-x ffi:SDL_CONTROLLER_AXIS_RIGHTY) + ('trigger-left ffi:SDL_CONTROLLER_AXIS_TRIGGERLEFT) + ('trigger-right ffi:SDL_CONTROLLER_AXIS_TRIGGERRIGHT))) + +(define (game-controller-axis controller axis) + "Return a number in the range [-32768, 32767] representing the +current state of AXIS on CONTROLLER. + +AXIS may be one of the following symbols: +- left-x +- left-y +- right-x +- right-y +- trigger-left +- trigger-right" + (ffi:sdl-game-controller-get-axis (unwrap-game-controller controller) + (axis-symbol->int axis))) + +(define (button-symbol->int button) + (match button + ('a ffi:SDL_CONTROLLER_BUTTON_A) + ('b ffi:SDL_CONTROLLER_BUTTON_B) + ('x ffi:SDL_CONTROLLER_BUTTON_X) + ('y ffi:SDL_CONTROLLER_BUTTON_Y) + ('back ffi:SDL_CONTROLLER_BUTTON_BACK) + ('guide ffi:SDL_CONTROLLER_BUTTON_GUIDE) + ('start ffi:SDL_CONTROLLER_BUTTON_START) + ('left-stick ffi:SDL_CONTROLLER_BUTTON_LEFTSTICK) + ('right-stick ffi:SDL_CONTROLLER_BUTTON_RIGHTSTICK) + ('left-shoulder ffi:SDL_CONTROLLER_BUTTON_LEFTSHOULDER) + ('right-shoulder ffi:SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) + ('dpad-up ffi:SDL_CONTROLLER_BUTTON_DPAD_UP) + ('dpad-down ffi:SDL_CONTROLLER_BUTTON_DPAD_DOWN) + ('dpad-left ffi:SDL_CONTROLLER_BUTTON_DPAD_LEFT) + ('dpad-right ffi:SDL_CONTROLLER_BUTTON_DPAD_RIGHT))) + +(define (game-controller-button-pressed? controller button) + "Return #t if BUTTON is pressed on CONTROLLER. + +BUTTON may be one of the following symbols: +- a +- b +- x +- y +- back +- guide +- start +- left-stick +- right-stick +- left-shoulder +- right-shoulder +- dpad-up +- dpad-down +- dpad-left +- dpad-right" + (= (ffi:sdl-game-controller-get-button (unwrap-game-controller controller) + (button-symbol->int button)) + 1)) + +(define (game-controller-index? joystick-index) + "Return #t if JOYSTICK-INDEX is a valid game controller index." + (= (ffi:sdl-is-game-controller joystick-index) 1)) diff --git a/sdl2/input/joystick.scm b/sdl2/input/joystick.scm new file mode 100644 index 0000000..ecab8ec --- /dev/null +++ b/sdl2/input/joystick.scm @@ -0,0 +1,114 @@ +;;; guile-sdl2 --- FFI bindings for SDL2 +;;; Copyright © 2016 David Thompson <davet@gnu.org> +;;; +;;; This file is part of guile-sdl2. +;;; +;;; Guile-sdl2 is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Lesser General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; Guile-sdl2 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 Lesser General Public +;;; License along with guile-sdl2. If not, see +;;; <http://www.gnu.org/licenses/>. + +;;; Commentary: +;; +;; Joystick input. +;; +;;; Code: + +(define-module (sdl2 input joystick) + #:use-module (ice-9 format) + #:use-module (ice-9 match) + #:use-module (system foreign) + #:use-module ((sdl2 bindings) #:prefix ffi:) + #:use-module (sdl2) + #:export (num-joysticks + open-joystick + close-joystick + joystick? + joystick-instance-id + joystick-power-level + joystick-num-axes + joystick-num-balls + joystick-num-buttons + joystick-num-hats)) + +(define (num-joysticks) + "Return the current number of connected joystick devices." + (ffi:sdl-num-joysticks)) + +(define-wrapped-pointer-type <joystick> + joystick? + wrap-joystick unwrap-joystick + (lambda (joystick port) + (format port "#<joystick id: ~d>" + (joystick-instance-id joystick)))) + +(define (open-joystick device-index) + "Return a joystick object for the physical joystick device +associated with DEVICE-INDEX." + (let ((ptr (ffi:sdl-joystick-open device-index))) + (if (null-pointer? ptr) + (sdl-error "open-joystick" "failed to open joystick") + (wrap-joystick ptr)))) + +(define (close-joystick joystick) + "Close JOYSTICK." + (ffi:sdl-joystick-close (unwrap-joystick joystick))) + +(define (joystick-instance-id joystick) + "Return the instance id of JOYSTICK." + (ffi:sdl-joystick-instance-id (unwrap-joystick joystick))) + +(define (joystick-attached? joystick) + "Return #t if JOYSTICK has been opened." + (= (ffi:sdl-joystick-get-attached (unwrap-joystick joystick)) 1)) + +(define (joystick-power-level joystick) + "Return the symbolic battery power level for JOYSTICK, either +'unknown', 'empty', 'low', 'medium', 'full', or 'wired'." + (let ((level (ffi:sdl-joystick-current-power-level + (unwrap-joystick joystick)))) + (cond + ((= level ffi:SDL_JOYSTICK_POWER_UNKNOWN) 'unknown) + ((= level ffi:SDL_JOYSTICK_POWER_EMPTY) 'empty) + ((= level ffi:SDL_JOYSTICK_POWER_LOW) 'low) + ((= level ffi:SDL_JOYSTICK_POWER_MEDIUM) 'medium) + ((= level ffi:SDL_JOYSTICK_POWER_FULL) 'full) + ((= level ffi:SDL_JOYSTICK_POWER_WIRED) 'wired)))) + +(define (joystick-num-axes joystick) + "Return the number of axes for JOYSTICK." + (ffi:sdl-joystick-num-axes (unwrap-joystick joystick))) + +(define (joystick-num-balls joystick) + "Return the number of balls for JOYSTICK." + (ffi:sdl-joystick-num-balls (unwrap-joystick joystick))) + +(define (joystick-num-buttons joystick) + "Return the number of buttons for JOYSTICK." + (ffi:sdl-joystick-num-buttons (unwrap-joystick joystick))) + +(define (joystick-num-hats joystick) + "Return the number of hats for JOYSTICK." + (ffi:sdl-joystick-num-hats (unwrap-joystick joystick))) + +(define (joystick-axis joystick axis-index) + "Return a number in the range [-32768, 32767] representing the +current state of AXIS-INDEX on JOYSTICK. On most joysticks, use 0 to +query the X axis and 1 to query the Y axis." + (ffi:sdl-joystick-get-axis (unwrap-joystick joystick) axis-index)) + +(define (joystick-button-pressed? joystick button-index) + "Return #t if BUTTON-INDEX is pressed on JOYSTICK. Button indices +start from 0." + (= (ffi:sdl-joystick-get-button (unwrap-joystick joystick) button-index) 1)) + +;; TODO: joystick-hat and joystick-ball |