Skip to content

Commit

Permalink
sdl: add bindings up to SDL2 2.0.28 and some previously missing ones …
Browse files Browse the repository at this point in the history
…as well

Signed-off-by: Lilis Iskandar <[email protected]>
  • Loading branch information
veeableful committed Dec 12, 2023
1 parent 413c898 commit e445841
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 81 deletions.
33 changes: 33 additions & 0 deletions sdl/TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
## 2.28.0

[x] SDL_HasWindowSurface, SDL_DestroyWindowSurface
[x] SDL_DISPLAYEVENT_MOVED
[x] SDL_HINT_ENABLE_SCREEN_KEYBOARD

## 2.26.0

[x] SDL_MouseWheelEvent: add mouseX and mouseY fields
[x] SDL_GetWindowSizeInPixels()
[x] SDL_ResetHints()
[x] SDL_GetJoystickGUIDInfo()
[x] SDL_HINT_JOYSTICK_HIDAPI_WII
[x] SDL_HINT_JOYSTICK_HIDAPI_WII_PLAYER_LED
[x] SDL_HINT_JOYSTICK_HIDAPI_XBOX_360
[x] SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED
[x] SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS
[x] SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE
[x] SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED
[x] SDL_HINT_JOYSTICK_HIDAPI_PS3
[x] SDL_HINT_JOYSTICK_HIDAPI_PS4
[x] SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE
[x] SDL_HINT_JOYSTICK_HIDAPI_PS5
[x] SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED
[x] SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE
[x] SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS
[x] SDL_SensorEvent and SDL_ControllerSensorEvent: add timestamp_us field
[x] SDL_SensorGetDataWithTimestamp() and SDL_GameControllerGetSensorDataWithTimestamp()
[x] SDL_HINT_HIDAPI_IGNORE_DEVICES
[x] SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALE
[x] SDL_SetPrimarySelectionText(), SDL_GetPrimarySelectionText(), and SDL_HasPrimarySelectionText()
[x] SDL_HINT_VIDEO_WAYLAND_EMULATE_MOUSE_WARP

## 2.24.0

[x] SDL_GetPointDisplayIndex
Expand Down
58 changes: 57 additions & 1 deletion sdl/clipboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
package sdl

// #include "sdl_wrapper.h"
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,26,0))
#if defined(WARN_OUTDATED)
#pragma message("SDL_SetPrimarySelectionText is not supported before SDL 2.26.0")
#pragma message("SDL_GetPrimarySelectionText is not supported before SDL 2.26.0")
#pragma message("SDL_HasPrimarySelectionText is not supported before SDL 2.26.0")
#endif
static inline int SDL_SetPrimarySelectionText(const char *text)
{
return -1;
}
static inline char * SDL_GetPrimarySelectionText(void)
{
return NULL;
}
static inline SDL_bool SDL_HasPrimarySelectionText(void)
{
return SDL_FALSE;
}
#endif
*/
import "C"
import "unsafe"

Expand Down Expand Up @@ -32,3 +59,32 @@ func GetClipboardText() (string, error) {
func HasClipboardText() bool {
return C.SDL_HasClipboardText() > 0
}

// SetPrimarySelectionText puts UTF-8 text into the primary selection.
// (https://wiki.libsdl.org/SDL_SetPrimarySelectionText)
func SetPrimarySelectionText(text string) error {
_text := C.CString(text)
defer C.free(unsafe.Pointer(_text))
if C.SDL_SetPrimarySelectionText(_text) < 0 {
return GetError()
}
return nil
}

// GetPrimarySelectionText gets UTF-8 text from the primary selection.
// (https://wiki.libsdl.org/SDL_GetPrimarySelectionText)
func GetPrimarySelectionText() (string, error) {
text := C.SDL_GetPrimarySelectionText()
if text == nil {
return "", GetError()
}
defer C.SDL_free(unsafe.Pointer(text))
_text := C.GoString(text)
return _text, nil
}

// HasPrimarySelectionText queries whether the primary selection exists and contains a non-empty text string.
// (https://wiki.libsdl.org/SDL_HasPrimarySelectionText)
func HasPrimarySelectionText() bool {
return C.SDL_HasPrimarySelectionText() == C.SDL_TRUE
}
73 changes: 69 additions & 4 deletions sdl/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef struct SDL_SensorEvent {
Uint32 timestamp;
Sint32 which;
float data[6];
Uint64 timestamp_us;
} SDL_SensorEvent;
#endif
Expand All @@ -91,6 +92,22 @@ typedef struct SDL_SensorEvent {
#pragma message("SDL_TEXTEDITING_EXT is not supported before SDL 2.0.22")
#endif
#if !SDL_VERSION_ATLEAST(2,0,14)
#define SDL_CONTROLLERTOUCHPADDOWN (0x656)
#define SDL_CONTROLLERTOUCHPADMOTION (0x657)
#define SDL_CONTROLLERTOUCHPADUP (0x658)
#define SDL_CONTROLLERSENSORUPDATE (0x659)
typedef struct SDL_ControllerSensorEvent {
Uint32 type;
Uint32 timestamp;
SDL_JoystickID which;
Sint32 sensor;
float data[3];
Uint64 timestamp_us;
} SDL_ControllerSensorEvent;
#endif
#define SDL_TEXTEDITING_EXT (0x305)
#endif
Expand Down Expand Up @@ -130,6 +147,8 @@ typedef struct MouseWheelEvent
Uint32 direction; // Set to one of the SDL_MOUSEWHEEL_* defines. When FLIPPED the values in X and Y will be opposite. Multiply by -1 to change them back
float preciseX; // The amount scrolled horizontally, positive to the right and negative to the left, with float precision (added in 2.0.18)
float preciseY; // The amount scrolled vertically, positive away from the user and negative toward the user, with float precision (added in 2.0.18)
Sint32 mouseX; // X coordinate, relative to window (added in 2.26.0)
Sint32 mouseY; // Y coordinate, relative to window (added in 2.26.0)
} MouseWheelEvent;
typedef struct TouchFingerEvent
Expand Down Expand Up @@ -257,6 +276,10 @@ const (
CONTROLLERDEVICEADDED EventType = C.SDL_CONTROLLERDEVICEADDED // controller connected
CONTROLLERDEVICEREMOVED EventType = C.SDL_CONTROLLERDEVICEREMOVED // controller disconnected
CONTROLLERDEVICEREMAPPED EventType = C.SDL_CONTROLLERDEVICEREMAPPED // controller mapping updated
CONTROLLERTOUCHPADDOWN EventType = C.SDL_CONTROLLERTOUCHPADDOWN // Game controller touchpad was touched
CONTROLLERTOUCHPADMOTION EventType = C.SDL_CONTROLLERTOUCHPADMOTION // Game controller touchpad finger was moved
CONTROLLERTOUCHPADUP EventType = C.SDL_CONTROLLERTOUCHPADUP // Game controller touchpad finger was lifted
CONTROLLERSENSORUPDATE EventType = C.SDL_CONTROLLERSENSORUPDATE // Game controller sensor was updated

// Touch events
FINGERDOWN EventType = C.SDL_FINGERDOWN // user has touched input device
Expand Down Expand Up @@ -527,6 +550,8 @@ type MouseWheelEvent struct {
Direction uint32 // MOUSEWHEEL_NORMAL, MOUSEWHEEL_FLIPPED (>= SDL 2.0.4)
PreciseX float32 // The amount scrolled horizontally, positive to the right and negative to the left, with float precision (added in 2.0.18)
PreciseY float32 // The amount scrolled vertically, positive away from the user and negative toward the user, with float precision (added in 2.0.18)
MouseX int32 // X coordinate, relative to window (added in 2.26.0)
MouseY int32 // Y coordinate, relative to window (added in 2.26.0)
}
type cMouseWheelEvent C.MouseWheelEvent

Expand Down Expand Up @@ -744,6 +769,28 @@ func (e ControllerDeviceEvent) GetTimestamp() uint32 {
return e.Timestamp
}

// ControllerSensorEvent contains data from sensors such as accelerometer and gyroscope
// (TODO: https://wiki.libsdl.org/SDL_ControllerSensorEvent)
type ControllerSensorEvent struct {
Type EventType // SDL_CONTROLLERSENSORUPDATE
Timestamp uint32 // In milliseconds, populated using SDL_GetTicks()
Which JoystickID // The joystick instance id
Sensor int32 // The type of the sensor, one of the values of SensorType
Data [3]float32 // Up to 3 values from the sensor - additional values can be queried using SDL_SensorGetData()
TimestampUs uint64 // The timestamp of the sensor reading in microseconds, if the hardware provides this information.
}
type cControllerSensorEvent C.SDL_ControllerSensorEvent

// GetType returns the event type.
func (e ControllerSensorEvent) GetType() EventType {
return e.Type
}

// GetTimestamp returns the timestamp of the event.
func (e ControllerSensorEvent) GetTimestamp() uint32 {
return e.Timestamp
}

// AudioDeviceEvent contains audio device event information.
// (https://wiki.libsdl.org/SDL_AudioDeviceEvent)
type AudioDeviceEvent struct {
Expand Down Expand Up @@ -861,10 +908,11 @@ func (e DropEvent) GetTimestamp() uint32 {
// SensorEvent contains data from sensors such as accelerometer and gyroscope
// (https://wiki.libsdl.org/SDL_SensorEvent)
type SensorEvent struct {
Type EventType // SDL_SENSORUPDATE
Timestamp uint32 // In milliseconds, populated using SDL_GetTicks()
Which int32 // The instance ID of the sensor
Data [6]float32 // Up to 6 values from the sensor - additional values can be queried using SDL_SensorGetData()
Type EventType // SDL_SENSORUPDATE
Timestamp uint32 // In milliseconds, populated using SDL_GetTicks()
Which int32 // The instance ID of the sensor
Data [6]float32 // Up to 6 values from the sensor - additional values can be queried using SDL_SensorGetData()
TimestampUs uint64 // The timestamp of the sensor reading in microseconds, if the hardware provides this information.
}
type cSensorEvent C.SDL_SensorEvent

Expand Down Expand Up @@ -1175,6 +1223,8 @@ func goEvent(cevent *CEvent) Event {
Direction: uint32(e.direction),
PreciseX: float32(e.preciseX),
PreciseY: float32(e.preciseY),
MouseX: int32(e.mouseX),
MouseY: int32(e.mouseY),
}
case JOYAXISMOTION:
e := (*cJoyAxisEvent)(unsafe.Pointer(cevent))
Expand Down Expand Up @@ -1260,6 +1310,20 @@ func goEvent(cevent *CEvent) Event {
Timestamp: uint32(e.timestamp),
Which: JoystickID(e.which),
}
case CONTROLLERSENSORUPDATE:
e := (*cControllerSensorEvent)(unsafe.Pointer(cevent))
return ControllerSensorEvent{
Type: EventType(e._type),
Timestamp: uint32(e.timestamp),
Which: JoystickID(e.which),
Sensor: int32(e.sensor),
Data: [3]float32{
float32(e.data[0]),
float32(e.data[1]),
float32(e.data[2]),
},
TimestampUs: uint64(e.timestamp_us),
}
case AUDIODEVICEADDED, AUDIODEVICEREMOVED:
e := (*cAudioDeviceEvent)(unsafe.Pointer(cevent))
return AudioDeviceEvent{
Expand Down Expand Up @@ -1334,6 +1398,7 @@ func goEvent(cevent *CEvent) Event {
float32(e.data[4]),
float32(e.data[5]),
},
TimestampUs: uint64(e.timestamp_us),
}
case RENDER_TARGETS_RESET, RENDER_DEVICE_RESET:
// This is a CommonEvent as it doesn't currently (sdl-v2.0.22) have any additional fields
Expand Down
49 changes: 45 additions & 4 deletions sdl/gamecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static void SDL_GameControllerSetPlayerIndex(SDL_GameController *gamecontroller,
#pragma message("SDL_GameControllerSetLED is not supported before SDL 2.0.14")
#endif
static const char * SDLCALL SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
static const char * SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
{
return NULL;
}
Expand All @@ -162,12 +162,12 @@ static SDL_bool SDL_GameControllerHasAxis(SDL_GameController *gamecontroller, SD
return SDL_FALSE;
}
static SDL_bool SDLCALL SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
static SDL_bool SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
{
return SDL_FALSE;
}
static int SDLCALL SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
static int SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
{
return 0;
}
Expand Down Expand Up @@ -293,6 +293,18 @@ static Uint16 SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontr
return 0;
}
#endif
#if !(SDL_VERSION_ATLEAST(2,26,0))
#if defined(WARN_OUTDATED)
#pragma message("SDL_GameControllerGetSensorDataWithTimestamp is not supported before SDL 2.26.0")
#endif
static inline int SDL_GameControllerGetSensorDataWithTimestamp(SDL_GameController *gamecontroller, SDL_SensorType type, Uint64 *timestamp, float *data, int num_values)
{
return -1;
}
#endif
*/
import "C"
Expand Down Expand Up @@ -735,10 +747,39 @@ func (ctrl *GameController) SendEffect(data []byte) (err error) {

// GetSensorDataRate gets the data rate (number of events per second) of a game controller sensor.
// (https://wiki.libsdl.org/SDL_GameControllerGetSensorDataRate)
func (ctrl *GameController) SensorDataRate(typ SensorType) (rate float32) {
func (ctrl *GameController) GetSensorDataRate(typ SensorType) (rate float32) {
return float32(C.SDL_GameControllerGetSensorDataRate(ctrl.cptr(), C.SDL_SensorType(typ)))
}

// GameControllerGetSensorData gets the current state of a game controller sensor.
//
// The number of values and interpretation of the data is sensor dependent.
// (https://wiki.libsdl.org/SDL_GameControllerGetSensorData)
func (ctrl *GameController) GetSensorData(typ SensorType, data []float32) (err error) {
if data == nil {
return nil
}
_data := (*C.float)(unsafe.Pointer(&data[0]))
_numValues := C.int(len(data))
err = errorFromInt(int(C.SDL_GameControllerGetSensorData((*C.SDL_GameController)(ctrl), C.SDL_SensorType(typ), _data, _numValues)))
return
}

// GameeControllerGetSensorDataWithTimestamp gets current state of a game controller sensor with the timestamp of the last update.
//
// The number of values and interpretation of the data is sensor dependent.
// (https://wiki.libsdl.org/SDL_GameControllerGetSensorDataWithTimestamp)
func (ctrl *GameController) GetSensorDataWithTimestamp(typ SensorType, timestamp *uint64, data []float32) (err error) {
if data == nil {
return nil
}
_data := (*C.float)(unsafe.Pointer(&data[0]))
_numValues := C.int(len(data))
_timestamp := (*C.Uint64)(timestamp)
err = errorFromInt(int(C.SDL_GameControllerGetSensorDataWithTimestamp((*C.SDL_GameController)(ctrl), C.SDL_SensorType(typ), _timestamp, _data, _numValues)))
return
}

// HasRumble queries whether a game controller has rumble support.
// (https://wiki.libsdl.org/SDL_GameControllerHasRumble)
func (ctrl *GameController) HasRumble() bool {
Expand Down
Loading

0 comments on commit e445841

Please sign in to comment.