From 1800240eb3154c5460907917ae3f4659002efc47 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 30 Dec 2016 11:55:44 -0500 Subject: 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 --- sdl2/input/joystick.scm | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 sdl2/input/joystick.scm (limited to 'sdl2/input/joystick.scm') 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 +;;; +;;; 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 +;;; . + +;;; 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? + wrap-joystick unwrap-joystick + (lambda (joystick port) + (format port "#" + (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 -- cgit v1.2.3