Skip to content

Commit

Permalink
sdl: haptic: Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
malashin authored and veeableful committed Dec 1, 2017
1 parent adeecf1 commit 4223939
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 41 deletions.
9 changes: 9 additions & 0 deletions BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
+ `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.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

- Unexported `Padding` in `AudioSpec` struct
- Unexported `goHintCallback`
Expand Down
119 changes: 78 additions & 41 deletions sdl/haptic.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,29 @@ func (h *Haptic) cptr() *C.SDL_Haptic {

// NumHaptics returns the number of haptic devices attached to the system.
// (https://wiki.libsdl.org/SDL_NumHaptics)
func NumHaptics() int {
return int(C.SDL_NumHaptics())
func NumHaptics() (int, error) {
i := int(C.SDL_NumHaptics())
return i, errorFromInt(i)
}

// HapticName returns the implementation dependent name of a haptic device.
// (https://wiki.libsdl.org/SDL_HapticName)
func HapticName(index int) string {
return (C.GoString)(C.SDL_HapticName(C.int(index)))
func HapticName(index int) (string, error) {
name := C.SDL_HapticName(C.int(index))
if name == nil {
return "", GetError()
}
return C.GoString(name), nil
}

// HapticOpen opens a haptic device for use.
// (https://wiki.libsdl.org/SDL_HapticOpen)
func HapticOpen(index int) (*Haptic, error) {
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpen(C.int(index))))
if haptic != nil {
return haptic, nil
if haptic == nil {
return nil, GetError()
}
return nil, GetError()
return haptic, nil
}

// HapticOpened reports whether the haptic device at the designated index has been opened.
Expand All @@ -233,8 +238,9 @@ func HapticOpened(index int) (bool, error) {

// HapticIndex returns the index of a haptic device.
// (https://wiki.libsdl.org/SDL_HapticIndex)
func HapticIndex(h *Haptic) int {
return int(C.SDL_HapticIndex(h.cptr()))
func HapticIndex(h *Haptic) (int, error) {
i := int(C.SDL_HapticIndex(h.cptr()))
return i, errorFromInt(i)
}

// MouseIsHaptic reports whether or not the current mouse has haptic capabilities.
Expand All @@ -248,10 +254,10 @@ func MouseIsHaptic() (bool, error) {
// (https://wiki.libsdl.org/SDL_HapticOpenFromMouse)
func HapticOpenFromMouse() (*Haptic, error) {
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromMouse()))
if haptic != nil {
return haptic, nil
if haptic == nil {
return nil, GetError()
}
return nil, GetError()
return haptic, nil
}

// JoystickIsHaptic reports whether a joystick has haptic features.
Expand All @@ -263,8 +269,12 @@ func JoystickIsHaptic(joy *Joystick) (bool, error) {

// HapticOpenFromJoystick opens a haptic device for use from a joystick device.
// (https://wiki.libsdl.org/SDL_HapticOpenFromJoystick)
func HapticOpenFromJoystick(joy *Joystick) *Haptic {
return (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromJoystick(joy.cptr())))
func HapticOpenFromJoystick(joy *Joystick) (*Haptic, error) {
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromJoystick(joy.cptr())))
if haptic == nil {
return nil, GetError()
}
return haptic, nil
}

// Close closes a haptic device previously opened with HapticOpen().
Expand All @@ -275,61 +285,78 @@ func (h *Haptic) Close() {

// NumAxes returns the number of haptic axes the device has.
// (https://wiki.libsdl.org/SDL_HapticNumAxes)
func (h *Haptic) NumAxes() int {
return int(C.SDL_HapticNumAxes(h.cptr()))
func (h *Haptic) NumAxes() (int, error) {
i := int(C.SDL_HapticNumAxes(h.cptr()))
return i, errorFromInt(i)
}

// NumEffects returns the number of effects a haptic device can store.
// (https://wiki.libsdl.org/SDL_HapticNumEffects)
func (h *Haptic) NumEffects() int {
return int(C.SDL_HapticNumEffects(h.cptr()))
func (h *Haptic) NumEffects() (int, error) {
i := int(C.SDL_HapticNumEffects(h.cptr()))
return i, errorFromInt(i)
}

// NumEffectsPlaying reutrns the number of effects a haptic device can play at the same time.
// (https://wiki.libsdl.org/SDL_HapticNumEffectsPlaying)
func (h *Haptic) NumEffectsPlaying() int {
return int(C.SDL_HapticNumEffectsPlaying(h.cptr()))
func (h *Haptic) NumEffectsPlaying() (int, error) {
i := int(C.SDL_HapticNumEffectsPlaying(h.cptr()))
return i, errorFromInt(i)
}

// Query returns haptic device's supported features in bitwise manner.
// (https://wiki.libsdl.org/SDL_HapticQuery)
func (h *Haptic) Query() uint {
return uint(C.SDL_HapticQuery(h.cptr()))
func (h *Haptic) Query() (uint32, error) {
i := uint32(C.SDL_HapticQuery(h.cptr()))
if i == 0 {
return 0, GetError()
}
return i, nil
}

// EffectSupported reports whether an effect is supported by a haptic device.
// (https://wiki.libsdl.org/SDL_HapticEffectSupported)
func (h *Haptic) EffectSupported(he *HapticEffect) (bool, error) {
_he := (*C.SDL_HapticEffect)(unsafe.Pointer(he))
ret := int(C.SDL_HapticEffectSupported(h.cptr(), _he))
ret := int(C.SDL_HapticEffectSupported(
h.cptr(),
(*C.SDL_HapticEffect)(unsafe.Pointer(he))))
return ret == C.SDL_TRUE, errorFromInt(ret)
}

// NewEffect creates a new haptic effect on a specified device.
// (https://wiki.libsdl.org/SDL_HapticNewEffect)
func (h *Haptic) NewEffect(he *HapticEffect) (int, error) {
_he := (*C.SDL_HapticEffect)(unsafe.Pointer(he))
ret := int(C.SDL_HapticNewEffect(h.cptr(), _he))
ret := int(C.SDL_HapticNewEffect(
h.cptr(),
(*C.SDL_HapticEffect)(unsafe.Pointer(he))))
return ret, errorFromInt(ret)
}

// UpdateEffect updates the properties of an effect.
// (https://wiki.libsdl.org/SDL_HapticUpdateEffect)
func (h *Haptic) UpdateEffect(effect int, data *HapticEffect) error {
_data := (*C.SDL_HapticEffect)(unsafe.Pointer(data))
return errorFromInt(int(C.SDL_HapticUpdateEffect(h.cptr(), C.int(effect), _data)))
return errorFromInt(int(
C.SDL_HapticUpdateEffect(
h.cptr(),
C.int(effect),
(*C.SDL_HapticEffect)(unsafe.Pointer(data)))))
}

// RunEffect runs the haptic effect on its associated haptic device.
// (https://wiki.libsdl.org/SDL_HapticRunEffect)
func (h *Haptic) RunEffect(effect int, iterations uint32) error {
return errorFromInt(int(C.SDL_HapticRunEffect(h.cptr(), C.int(effect), C.Uint32(iterations))))
return errorFromInt(int(
C.SDL_HapticRunEffect(
h.cptr(),
C.int(effect),
C.Uint32(iterations))))
}

// StopEffect stops the haptic effect on its associated haptic device.
// (https://wiki.libsdl.org/SDL_HapticStopEffect)
func (h *Haptic) StopEffect(effect int) error {
return errorFromInt(int(C.SDL_HapticStopEffect(h.cptr(), C.int(effect))))
return errorFromInt(int(
C.SDL_HapticStopEffect(h.cptr(), C.int(effect))))
}

// DestroyEffect destroys a haptic effect on the device.
Expand All @@ -340,61 +367,71 @@ func (h *Haptic) DestroyEffect(effect int) {

// GetEffectStatus returns the status of the current effect on the specified haptic device.
// (https://wiki.libsdl.org/SDL_HapticGetEffectStatus)
func (h *Haptic) GetEffectStatus(effect int) int {
return int(C.SDL_HapticGetEffectStatus(h.cptr(), C.int(effect)))
func (h *Haptic) GetEffectStatus(effect int) (int, error) {
i := int(C.SDL_HapticGetEffectStatus(h.cptr(), C.int(effect)))
return i, errorFromInt(i)

}

// SetGain sets the global gain of the specified haptic device.
// (https://wiki.libsdl.org/SDL_HapticSetGain)
func (h *Haptic) SetGain(gain int) error {
return errorFromInt(int(C.SDL_HapticSetGain(h.cptr(), C.int(gain))))
return errorFromInt(int(
C.SDL_HapticSetGain(h.cptr(), C.int(gain))))
}

// SetAutocenter sets the global autocenter of the device.
// (https://wiki.libsdl.org/SDL_HapticSetAutocenter)
func (h *Haptic) SetAutocenter(autocenter int) error {
return errorFromInt(int(C.SDL_HapticSetAutocenter(h.cptr(), C.int(autocenter))))
return errorFromInt(int(
C.SDL_HapticSetAutocenter(h.cptr(), C.int(autocenter))))
}

// Pause pauses a haptic device.
// (https://wiki.libsdl.org/SDL_HapticPause)
func (h *Haptic) Pause() error {
return errorFromInt(int(C.SDL_HapticPause(h.cptr())))
return errorFromInt(int(
C.SDL_HapticPause(h.cptr())))
}

// Unpause unpauses a haptic device.
// (https://wiki.libsdl.org/SDL_HapticUnpause)
func (h *Haptic) Unpause() error {
return errorFromInt(int(C.SDL_HapticUnpause(h.cptr())))
return errorFromInt(int(
C.SDL_HapticUnpause(h.cptr())))
}

// StopAll stops all the currently playing effects on a haptic device.
// (https://wiki.libsdl.org/SDL_HapticStopAll)
func (h *Haptic) StopAll() error {
return errorFromInt(int(C.SDL_HapticStopAll(h.cptr())))
return errorFromInt(int(
C.SDL_HapticStopAll(h.cptr())))
}

// RumbleSupported reports whether rumble is supported on a haptic device.
// (https://wiki.libsdl.org/SDL_HapticRumbleSupported)
func (h *Haptic) RumbleSupported() (ok bool, err error) {
func (h *Haptic) RumbleSupported() (bool, error) {
ret := int(C.SDL_HapticRumbleSupported(h.cptr()))
return ret == C.SDL_TRUE, errorFromInt(ret)
}

// RumbleInit initializes the haptic device for simple rumble playback.
// (https://wiki.libsdl.org/SDL_HapticRumbleInit)
func (h *Haptic) RumbleInit() error {
return errorFromInt(int(C.SDL_HapticRumbleInit(h.cptr())))
return errorFromInt(int(
C.SDL_HapticRumbleInit(h.cptr())))
}

// RumblePlay runs a simple rumble effect on a haptic device.
// (https://wiki.libsdl.org/SDL_HapticRumblePlay)
func (h *Haptic) RumblePlay(strength float32, length uint32) error {
return errorFromInt(int(C.SDL_HapticRumblePlay(h.cptr(), C.float(strength), C.Uint32(length))))
return errorFromInt(int(
C.SDL_HapticRumblePlay(h.cptr(), C.float(strength), C.Uint32(length))))
}

// RumbleStop stops the simple rumble on a haptic device.
// (https://wiki.libsdl.org/SDL_HapticRumbleStop)
func (h *Haptic) RumbleStop() error {
return errorFromInt(int(C.SDL_HapticRumbleStop(h.cptr())))
return errorFromInt(int(
C.SDL_HapticRumbleStop(h.cptr())))
}

0 comments on commit 4223939

Please sign in to comment.