Skip to content

Commit

Permalink
sdl: gamecontroller, joystick: Add new bindings for SDL 2.0.6 and 2.…
Browse files Browse the repository at this point in the history
…0.4 (#313)
  • Loading branch information
malashin committed Feb 10, 2018
1 parent 9b68928 commit 930022a
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 122 deletions.
110 changes: 61 additions & 49 deletions BREAKING.md
Expand Up @@ -2,86 +2,98 @@

### v0.2..master

+ Renamed `Texture.GL_BindTexture()` to `Texture.GLBind()`
+ Renamed `Texture.GL_UnbindTexture()` to `Texture.GLUnbind()`
+ Renamed `LoadWAV_RW()` to `LoadWAVRW()`
+ Renamed `TouchId` to `TouchID` in `MultiGestureEvent` struct
+ Renamed `Unicode` to `unused` in `Keysym` struct (must have been a typo)
+ Renamed `ButtonId` to `ButtonID` in `MessageBoxButtonData` struct
+ Renamed `GL_LoadLibrary()` to `GLLoadLibrary()`
+ Renamed `GL_GetProcAddress()` to `GLGetProcAddress()`
+ Renamed `GL_UnloadLibrary()` to `GLUnloadLibrary()`
+ Renamed `GL_CreateContext()` to `GLCreateContext()`
+ Renamed `GL_DeleteContext()` to `GLDeleteContext()`
+ Renamed `GL_ExtensionSupported()` to `GLExtensionSupported()`
+ Renamed `GL_SetAttribute()` to `GLSetAttribute()`
+ Renamed `GL_GetAttribute()` to `GLGetAttribute()`
+ Renamed `GL_CreateContext()` to `GLCreateContext()`
+ Renamed `GL_GetDrawableSize()` to `GLGetDrawableSize()`
+ Renamed `GL_GetProcAddress()` to `GLGetProcAddress()`
+ Renamed `GL_GetSwapInterval()` to `GLGetSwapInterval()`
+ Renamed `GL_LoadLibrary()` to `GLLoadLibrary()`
+ Renamed `GL_MakeCurrent()` to `GLMakeCurrent()`
+ Renamed `GL_SetAttribute()` to `GLSetAttribute()`
+ Renamed `GL_SetSwapInterval()` to `GLSetSwapInterval()`
+ Renamed `GL_GetSwapInterval()` to `GLGetSwapInterval()`
+ Renamed `GL_GetDrawableSize()` to `GLGetDrawableSize()`
+ Renamed `GL_SwapWindow()` to `GLSwapWindow()`
+ Renamed `GL_DeleteContext()` to `GLDeleteContext()`
+ Renamed `LoadBMP_RW()` to `LoadBMPRW()`
+ Renamed `SaveBMP_RW()` to `SaveBMPRW()`
+ Renamed `GL_UnloadLibrary()` to `GLUnloadLibrary()`
+ Renamed `GameController.GetAttached()` to `GameController.Attached()`
+ Renamed `GameController.GetAxis()` to `GameController.Axis()`
+ Renamed `GameController.GetBindForAxis()` to `GameController.BindForAxis()`
+ Renamed `GameController.GetBindForButton()` to `GameController.BindForButton()`
+ Renamed `GameController.GetButton()` to `GameController.Button()`
+ Renamed `GameController.GetJoystick()` to `GameController.Joystick()`
+ Renamed `Id` to `ID` in `Finger` struct
+ Renamed `SW_YUVTexture` to `SWYUVTexture`
+ Renamed `LoadTyped_RW` to `LoadTypedRW`
+ Renamed `Load_RW` to `LoadRW`
+ Renamed `LoadTexture_RW` to `LoadTextureRW`
+ Renamed `LoadICO_RW()` to `LoadICORW()`
+ Renamed `LoadCUR_RW()` to `LoadCURRW()`
+ Renamed `Joystick.GetAttached()` to `Joystick.Attached()`
+ Renamed `Joystick.GetAxis()` to `Joystick.Axis()`
+ Renamed `Joystick.GetBall()` to `Joystick.Ball()`
+ Renamed `Joystick.GetButton()` to `Joystick.Button()`
+ Renamed `Joystick.GetGUID()` to `Joystick.GUID()`
+ Renamed `Joystick.GetHat()` to `Joystick.Hat()`
+ Renamed `LoadBMP_RW()` to `LoadBMPRW()`
+ Renamed `LoadBMP_RW()` to `LoadBMPRW()`
+ Renamed `LoadCUR_RW()` to `LoadCURRW()`
+ Renamed `LoadGIF_RW()` to `LoadGIFRW()`
+ Renamed `LoadICO_RW()` to `LoadICORW()`
+ Renamed `LoadJPG_RW()` to `LoadJPGRW()`
+ Renamed `LoadLBM_RW()` to `LoadLBMRW()`
+ Renamed `LoadMUSType_RW()` to `LoadMUSTypeRW()`
+ Renamed `LoadMUS_RW()` to `LoadMUSRW()`
+ Renamed `LoadPCX_RW()` to `LoadPCXRW()`
+ Renamed `LoadPNG_RW()` to `LoadPNGRW()`
+ Renamed `LoadPNM_RW()` to `LoadPNMRW()`
+ Renamed `LoadTGA_RW()` to `LoadTGARW()`
+ Renamed `LoadTIF_RW()` to `LoadTIFRW()`
+ Renamed `LoadTexture_RW` to `LoadTextureRW`
+ Renamed `LoadTyped_RW` to `LoadTypedRW`
+ Renamed `LoadWAV_RW()` to `LoadWAVRW()`
+ Renamed `LoadWAV_RW()` to `LoadWAVRW()`
+ Renamed `LoadWEBP_RW()` to `LoadWEBPRW()`
+ Renamed `LoadXCF_RW()` to `LoadXCFRW()`
+ Renamed `LoadXPM_RW()` to `LoadXPMRW()`
+ Renamed `LoadXV_RW()` to `LoadXVRW()`
+ Renamed `LoadWEBP_RW()` to `LoadWEBPRW()`
+ Renamed `SavePNG_RW()` to `SavePNGRW()`
+ Renamed `RenderUTF8_Solid()` to `RenderUTF8Solid()`
+ Renamed `RenderUTF8_Shaded()` to `RenderUTF8Shaded()`
+ Renamed `Load_RW` to `LoadRW`
+ Renamed `QuickLoad_RAW()` to `QuickLoadRAW()`
+ Renamed `QuickLoad_WAV()` to `QuickLoadWAV()`
+ Renamed `RenderUTF8_Blended()` to `RenderUTF8Blended()`
+ Renamed `RenderUTF8_Blended_Wrapped()` to `RenderUTF8BlendedWrapped()`
+ Renamed `LoadWAV_RW()` to `LoadWAVRW()`
+ Renamed `LoadMUS_RW()` to `LoadMUSRW()`
+ Renamed `LoadMUSType_RW()` to `LoadMUSTypeRW()`
+ Renamed `QuickLoad_WAV()` to `QuickLoadWAV()`
+ Renamed `QuickLoad_RAW()` to `QuickLoadRAW()`
+ Moved error to the last return value in `ShowMessageBox()`
+ Changed Mutex, Sem, Cond to have methods instead of functions
+ Merged `KeyUpEvent` and `KeyDownEvent` into `KeyboardEvent`
+ Haptic functions now return bool and/or error instead of int
+ Changed `GameControllerMapping()` into `GameController.Mapping()`
+ `GetDisplayName()` returns (string, error) instead of string
+ Renamed `RenderUTF8_Shaded()` to `RenderUTF8Shaded()`
+ Renamed `RenderUTF8_Solid()` to `RenderUTF8Solid()`
+ Renamed `SW_YUVTexture` to `SWYUVTexture`
+ Renamed `SaveBMP_RW()` to `SaveBMPRW()`
+ Renamed `SavePNG_RW()` to `SavePNGRW()`
+ Renamed `Texture.GL_BindTexture()` to `Texture.GLBind()`
+ Renamed `Texture.GL_UnbindTexture()` to `Texture.GLUnbind()`
+ Renamed `TouchId` to `TouchID` in `MultiGestureEvent` struct
+ Renamed `Unicode` to `unused` in `Keysym` struct (must have been a typo)
+ `GetCurrentVideoDriver()` returns (string, error) instead of string
+ `Window.Destroy()` returns error
+ `Window.GetID()` returns (uint32, error) instead of uint32
+ `GetDisplayName()` returns (string, error) instead of string
+ `GetNumRenderDrivers()` returns (int, error) instead of int
+ `GetRenderDriverInfo()` returns (int, error) instead of int
+ `Texture.Destroy()` returns error
+ `Renderer.Destroy()` returns error
+ `Renderer.GetViewport()` and `Renderer.GetClipRect()` now returns Rect instead of being passed a \*Rect
+ `NumHaptics()` returns (int, error) instead of int
+ `HapticName()` returns (string, error) instead of string
+ `HapticIndex()` returns (int, error) instead of int
+ `HapticOpenFromJoystick()` returns (\*Haptic, error) instead of \*Haptic
+ `Haptic.GetEffectStatus()` returns (int, error) instead of int
+ `Haptic.NumAxes()` returns (int, error) instead of int
+ `Haptic.NumEffects()` returns (int, error) instead of int
+ `Haptic.NumEffectsPlaying()` returns (int, error) instead of int
+ `Haptic.Query()` returns (uint32, error) instead of uint
+ `Haptic.GetEffectStatus()` returns (int, error) instead of int
+ `HapticIndex()` returns (int, error) instead of int
+ `HapticName()` returns (string, error) instead of string
+ `HapticOpenFromJoystick()` returns (\*Haptic, error) instead of \*Haptic
+ `LoadWAV()` requires string instead of (string ,\*AudioSpec)
+ `LoadWAVRW()` requires (\*RWops, bool) instead of (\*RWops, bool ,\*AudioSpec)
+ `NumHaptics()` returns (int, error) instead of int
+ `Renderer.Destroy()` returns error
+ `Renderer.GetViewport()` and `Renderer.GetClipRect()` now returns Rect instead of being passed a \*Rect
+ `Texture.Destroy()` returns error
+ `Window.Destroy()` returns error
+ `Window.GetID()` returns (uint32, error) instead of uint32
+ Changed Mutex, Sem, Cond to have methods instead of functions
+ Changed `GameControllerMapping()` into `GameController.Mapping()`
+ Haptic functions now return bool and/or error instead of int
+ Merged `KeyUpEvent` and `KeyDownEvent` into `KeyboardEvent`
+ Moved error to the last return value in `ShowMessageBox()`


- Unexported `Padding` in `AudioSpec` struct
- Unexported `goHintCallback`
- Unexported `Flags`, `Locked` and `LockData` in `Surface` struct
- Unexported `Padding` in `AudioSpec` struct
- Unexported `Rloss`, `Gloss`, `Bloss`, `Aloss`, `Rshift`, `Gshift`, `Bshift`, `Ashift`, `RefCount`, `Next` in `PixelFormat` struct
- Unexported `Version`, `RefCount` in `Palette` struct
- Unexported `Version`, `RefCount` in `Palette` struct
- Unexported `goHintCallback`
28 changes: 15 additions & 13 deletions sdl/TODO.md
Expand Up @@ -12,9 +12,11 @@ CPU Info

Game Controller

- [ ] SDL_GameControllerGetVendor()
- [ ] SDL_GameControllerGetProduct()
- [ ] SDL_GameControllerGetProductVersion()
- [x] SDL_GameControllerGetVendor()
- [x] SDL_GameControllerGetProduct()
- [x] SDL_GameControllerGetProductVersion()
- [x] SDL_GameControllerNumMappings()
- [x] SDL_GameControllerMappingForIndex()

Hints

Expand All @@ -26,16 +28,16 @@ Hints

Joystick

- [ ] SDL_JoystickGetDeviceVendor()
- [ ] SDL_JoystickGetDeviceProduct()
- [ ] SDL_JoystickGetDeviceProductVersion()
- [ ] SDL_JoystickGetDeviceType()
- [ ] SDL_JoystickGetDeviceInstanceID()
- [ ] SDL_JoystickGetVendor()
- [ ] SDL_JoystickGetProduct()
- [ ] SDL_JoystickGetProductVersion()
- [ ] SDL_JoystickGetType()
- [ ] SDL_JoystickGetAxisInitialState()
- [x] SDL_JoystickGetDeviceVendor()
- [x] SDL_JoystickGetDeviceProduct()
- [x] SDL_JoystickGetDeviceProductVersion()
- [x] SDL_JoystickGetDeviceType()
- [x] SDL_JoystickGetDeviceInstanceID()
- [x] SDL_JoystickGetVendor()
- [x] SDL_JoystickGetProduct()
- [x] SDL_JoystickGetProductVersion()
- [x] SDL_JoystickGetType()
- [x] SDL_JoystickGetAxisInitialState()

RW Ops

Expand Down
115 changes: 97 additions & 18 deletions sdl/gamecontroller.go
@@ -1,6 +1,48 @@
package sdl

// #include "sdl_wrapper.h"
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,0,4))
#pragma message("SDL_GameControllerFromInstanceID is not supported before SDL 2.0.4")
static SDL_GameController* SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
{
return NULL;
}
#endif
#if !(SDL_VERSION_ATLEAST(2,0,6))
#pragma message("SDL_GameControllerGetVendor is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetVendor(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerGetProduct is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetProduct(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerGetProductVersion is not supported before SDL 2.0.6")
static Uint16 SDL_GameControllerGetProductVersion(SDL_GameController* gamecontroller)
{
return 0;
}
#pragma message("SDL_GameControllerNumMappings is not supported before SDL 2.0.6")
static int SDL_GameControllerNumMappings(void)
{
return 0;
}
#pragma message("SDL_GameControllerMappingForIndex is not supported before SDL 2.0.6")
static char* SDL_GameControllerMappingForIndex(int mapping_index)
{
return NULL;
}
#endif
*/
import "C"
import "unsafe"
import "encoding/binary"
Expand Down Expand Up @@ -85,16 +127,30 @@ func GameControllerAddMapping(mappingString string) int {
return int(C.SDL_GameControllerAddMapping(_mappingString))
}

// GameControllerNumMappings returns the number of mappings installed.
func GameControllerNumMappings() int {
return int(C.SDL_GameControllerNumMappings())
}

// GameControllerMappingForIndex returns the game controller mapping string at a particular index.
func GameControllerMappingForIndex(index int) string {
mappingString := C.SDL_GameControllerMappingForIndex(C.int(index))
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}

// GameControllerMappingForGUID returns the game controller mapping string for a given GUID.
// (https://wiki.libsdl.org/SDL_GameControllerMappingForGUID)
func GameControllerMappingForGUID(guid JoystickGUID) string {
return C.GoString(C.SDL_GameControllerMappingForGUID(guid.c()))
mappingString := C.SDL_GameControllerMappingForGUID(guid.c())
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}

// IsGameController reports whether the given joystick is supported by the game controller interface.
// (https://wiki.libsdl.org/SDL_IsGameController)
func IsGameController(index int) bool {
return C.SDL_IsGameController(C.int(index)) > 0
return C.SDL_IsGameController(C.int(index)) == C.SDL_TRUE
}

// GameControllerNameForIndex returns the implementation dependent name for the game controller.
Expand All @@ -106,7 +162,13 @@ func GameControllerNameForIndex(index int) string {
// GameControllerOpen opens a gamecontroller for use.
// (https://wiki.libsdl.org/SDL_GameControllerOpen)
func GameControllerOpen(index int) *GameController {
return (*GameController)(unsafe.Pointer(C.SDL_GameControllerOpen(C.int(index))))
return (*GameController)(C.SDL_GameControllerOpen(C.int(index)))
}

// GameControllerFromInstanceID returns the GameController associated with an instance id.
// (https://wiki.libsdl.org/SDL_GameControllerFromInstanceID)
func GameControllerFromInstanceID(joyid JoystickID) *GameController {
return (*GameController)(C.SDL_GameControllerFromInstanceID(joyid.c()))
}

// Name returns the implementation dependent name for an opened game controller.
Expand All @@ -115,21 +177,38 @@ func (ctrl *GameController) Name() string {
return C.GoString(C.SDL_GameControllerName(ctrl.cptr()))
}

// GetAttached reports whether a controller has been opened and is currently connected.
// Vendor returns the USB vendor ID of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) Vendor() int {
return int(C.SDL_GameControllerGetVendor(ctrl.cptr()))
}

// Product returns the USB product ID of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) Product() int {
return int(C.SDL_GameControllerGetProduct(ctrl.cptr()))
}

// ProductVersion returns the product version of an opened controller, if available, 0 otherwise.
func (ctrl *GameController) ProductVersion() int {
return int(C.SDL_GameControllerGetProductVersion(ctrl.cptr()))
}

// Attached reports whether a controller has been opened and is currently connected.
// (https://wiki.libsdl.org/SDL_GameControllerGetAttached)
func (ctrl *GameController) GetAttached() bool {
return C.SDL_GameControllerGetAttached(ctrl.cptr()) > 0
func (ctrl *GameController) Attached() bool {
return C.SDL_GameControllerGetAttached(ctrl.cptr()) == C.SDL_TRUE
}

// Mapping returns the current mapping of a Game Controller.
// (https://wiki.libsdl.org/SDL_GameControllerMapping)
func (ctrl *GameController) Mapping() string {
return C.GoString(C.SDL_GameControllerMapping(ctrl.cptr()))
mappingString := C.SDL_GameControllerMapping(ctrl.cptr())
defer C.free(unsafe.Pointer(mappingString))
return C.GoString(mappingString)
}

// GetJoystick returns the Joystick ID from a Game Controller. The game controller builds on the Joystick API, but to be able to use the Joystick's functions with a gamepad, you need to use this first to get the joystick object.
// Joystick returns the Joystick ID from a Game Controller. The game controller builds on the Joystick API, but to be able to use the Joystick's functions with a gamepad, you need to use this first to get the joystick object.
// (https://wiki.libsdl.org/SDL_GameControllerGetJoystick)
func (ctrl *GameController) GetJoystick() *Joystick {
func (ctrl *GameController) Joystick() *Joystick {
return (*Joystick)(unsafe.Pointer(C.SDL_GameControllerGetJoystick(ctrl.cptr())))
}

Expand Down Expand Up @@ -159,15 +238,15 @@ func GameControllerGetStringForAxis(axis GameControllerAxis) string {
return C.GoString(C.SDL_GameControllerGetStringForAxis(axis.c()))
}

// GetBindForAxis returns the SDL joystick layer binding for a controller button mapping.
// BindForAxis returns the SDL joystick layer binding for a controller button mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetBindForAxis)
func (ctrl *GameController) GetBindForAxis(axis GameControllerAxis) GameControllerButtonBind {
func (ctrl *GameController) BindForAxis(axis GameControllerAxis) GameControllerButtonBind {
return GameControllerButtonBind(C.SDL_GameControllerGetBindForAxis(ctrl.cptr(), axis.c()))
}

// GetAxis returns the current state of an axis control on a game controller.
// Axis returns the current state of an axis control on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerGetAxis)
func (ctrl *GameController) GetAxis(axis GameControllerAxis) int16 {
func (ctrl *GameController) Axis(axis GameControllerAxis) int16 {
return int16(C.SDL_GameControllerGetAxis(ctrl.cptr(), axis.c()))
}

Expand All @@ -185,15 +264,15 @@ func GameControllerGetStringForButton(btn GameControllerButton) string {
return C.GoString(C.SDL_GameControllerGetStringForButton(btn.c()))
}

// GetBindForButton returns the SDL joystick layer binding for this controller button mapping.
// BindForButton returns the SDL joystick layer binding for this controller button mapping.
// (https://wiki.libsdl.org/SDL_GameControllerGetBindForButton)
func (ctrl *GameController) GetBindForButton(btn GameControllerButton) GameControllerButtonBind {
func (ctrl *GameController) BindForButton(btn GameControllerButton) GameControllerButtonBind {
return GameControllerButtonBind(C.SDL_GameControllerGetBindForButton(ctrl.cptr(), btn.c()))
}

// GetButton returns the current state of a button on a game controller.
// Button returns the current state of a button on a game controller.
// (https://wiki.libsdl.org/SDL_GameControllerGetButton)
func (ctrl *GameController) GetButton(btn GameControllerButton) byte {
func (ctrl *GameController) Button(btn GameControllerButton) byte {
return byte(C.SDL_GameControllerGetButton(ctrl.cptr(), btn.c()))
}

Expand Down

0 comments on commit 930022a

Please sign in to comment.