Skip to content

Commit 4223939

Browse files
malashinveeableful
authored andcommitted
sdl: haptic: Update error handling
1 parent adeecf1 commit 4223939

File tree

2 files changed

+87
-41
lines changed

2 files changed

+87
-41
lines changed

BREAKING.md

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
+ `Texture.Destroy()` returns error
6868
+ `Renderer.Destroy()` returns error
6969
+ `Renderer.GetViewport()` and `Renderer.GetClipRect()` now returns Rect instead of being passed a *Rect
70+
+ `NumHaptics()` returns (int, error) instead of int
71+
+ `HapticName()` returns (string, error) instead of string
72+
+ `HapticIndex()` returns (int, error) instead of int
73+
+ `HapticOpenFromJoystick()` returns (*Haptic, error) instead of *Haptic
74+
+ `Haptic.NumAxes()` returns (int, error) instead of int
75+
+ `Haptic.NumEffects()` returns (int, error) instead of int
76+
+ `Haptic.NumEffectsPlaying()` returns (int, error) instead of int
77+
+ `Haptic.Query()` returns (uint32, error) instead of uint
78+
+ `Haptic.GetEffectStatus()` returns (int, error) instead of int
7079

7180
- Unexported `Padding` in `AudioSpec` struct
7281
- Unexported `goHintCallback`

sdl/haptic.go

+78-41
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,29 @@ func (h *Haptic) cptr() *C.SDL_Haptic {
201201

202202
// NumHaptics returns the number of haptic devices attached to the system.
203203
// (https://wiki.libsdl.org/SDL_NumHaptics)
204-
func NumHaptics() int {
205-
return int(C.SDL_NumHaptics())
204+
func NumHaptics() (int, error) {
205+
i := int(C.SDL_NumHaptics())
206+
return i, errorFromInt(i)
206207
}
207208

208209
// HapticName returns the implementation dependent name of a haptic device.
209210
// (https://wiki.libsdl.org/SDL_HapticName)
210-
func HapticName(index int) string {
211-
return (C.GoString)(C.SDL_HapticName(C.int(index)))
211+
func HapticName(index int) (string, error) {
212+
name := C.SDL_HapticName(C.int(index))
213+
if name == nil {
214+
return "", GetError()
215+
}
216+
return C.GoString(name), nil
212217
}
213218

214219
// HapticOpen opens a haptic device for use.
215220
// (https://wiki.libsdl.org/SDL_HapticOpen)
216221
func HapticOpen(index int) (*Haptic, error) {
217222
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpen(C.int(index))))
218-
if haptic != nil {
219-
return haptic, nil
223+
if haptic == nil {
224+
return nil, GetError()
220225
}
221-
return nil, GetError()
226+
return haptic, nil
222227
}
223228

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

234239
// HapticIndex returns the index of a haptic device.
235240
// (https://wiki.libsdl.org/SDL_HapticIndex)
236-
func HapticIndex(h *Haptic) int {
237-
return int(C.SDL_HapticIndex(h.cptr()))
241+
func HapticIndex(h *Haptic) (int, error) {
242+
i := int(C.SDL_HapticIndex(h.cptr()))
243+
return i, errorFromInt(i)
238244
}
239245

240246
// MouseIsHaptic reports whether or not the current mouse has haptic capabilities.
@@ -248,10 +254,10 @@ func MouseIsHaptic() (bool, error) {
248254
// (https://wiki.libsdl.org/SDL_HapticOpenFromMouse)
249255
func HapticOpenFromMouse() (*Haptic, error) {
250256
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromMouse()))
251-
if haptic != nil {
252-
return haptic, nil
257+
if haptic == nil {
258+
return nil, GetError()
253259
}
254-
return nil, GetError()
260+
return haptic, nil
255261
}
256262

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

264270
// HapticOpenFromJoystick opens a haptic device for use from a joystick device.
265271
// (https://wiki.libsdl.org/SDL_HapticOpenFromJoystick)
266-
func HapticOpenFromJoystick(joy *Joystick) *Haptic {
267-
return (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromJoystick(joy.cptr())))
272+
func HapticOpenFromJoystick(joy *Joystick) (*Haptic, error) {
273+
haptic := (*Haptic)(unsafe.Pointer(C.SDL_HapticOpenFromJoystick(joy.cptr())))
274+
if haptic == nil {
275+
return nil, GetError()
276+
}
277+
return haptic, nil
268278
}
269279

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

276286
// NumAxes returns the number of haptic axes the device has.
277287
// (https://wiki.libsdl.org/SDL_HapticNumAxes)
278-
func (h *Haptic) NumAxes() int {
279-
return int(C.SDL_HapticNumAxes(h.cptr()))
288+
func (h *Haptic) NumAxes() (int, error) {
289+
i := int(C.SDL_HapticNumAxes(h.cptr()))
290+
return i, errorFromInt(i)
280291
}
281292

282293
// NumEffects returns the number of effects a haptic device can store.
283294
// (https://wiki.libsdl.org/SDL_HapticNumEffects)
284-
func (h *Haptic) NumEffects() int {
285-
return int(C.SDL_HapticNumEffects(h.cptr()))
295+
func (h *Haptic) NumEffects() (int, error) {
296+
i := int(C.SDL_HapticNumEffects(h.cptr()))
297+
return i, errorFromInt(i)
286298
}
287299

288300
// NumEffectsPlaying reutrns the number of effects a haptic device can play at the same time.
289301
// (https://wiki.libsdl.org/SDL_HapticNumEffectsPlaying)
290-
func (h *Haptic) NumEffectsPlaying() int {
291-
return int(C.SDL_HapticNumEffectsPlaying(h.cptr()))
302+
func (h *Haptic) NumEffectsPlaying() (int, error) {
303+
i := int(C.SDL_HapticNumEffectsPlaying(h.cptr()))
304+
return i, errorFromInt(i)
292305
}
293306

294307
// Query returns haptic device's supported features in bitwise manner.
295308
// (https://wiki.libsdl.org/SDL_HapticQuery)
296-
func (h *Haptic) Query() uint {
297-
return uint(C.SDL_HapticQuery(h.cptr()))
309+
func (h *Haptic) Query() (uint32, error) {
310+
i := uint32(C.SDL_HapticQuery(h.cptr()))
311+
if i == 0 {
312+
return 0, GetError()
313+
}
314+
return i, nil
298315
}
299316

300317
// EffectSupported reports whether an effect is supported by a haptic device.
301318
// (https://wiki.libsdl.org/SDL_HapticEffectSupported)
302319
func (h *Haptic) EffectSupported(he *HapticEffect) (bool, error) {
303-
_he := (*C.SDL_HapticEffect)(unsafe.Pointer(he))
304-
ret := int(C.SDL_HapticEffectSupported(h.cptr(), _he))
320+
ret := int(C.SDL_HapticEffectSupported(
321+
h.cptr(),
322+
(*C.SDL_HapticEffect)(unsafe.Pointer(he))))
305323
return ret == C.SDL_TRUE, errorFromInt(ret)
306324
}
307325

308326
// NewEffect creates a new haptic effect on a specified device.
309327
// (https://wiki.libsdl.org/SDL_HapticNewEffect)
310328
func (h *Haptic) NewEffect(he *HapticEffect) (int, error) {
311-
_he := (*C.SDL_HapticEffect)(unsafe.Pointer(he))
312-
ret := int(C.SDL_HapticNewEffect(h.cptr(), _he))
329+
ret := int(C.SDL_HapticNewEffect(
330+
h.cptr(),
331+
(*C.SDL_HapticEffect)(unsafe.Pointer(he))))
313332
return ret, errorFromInt(ret)
314333
}
315334

316335
// UpdateEffect updates the properties of an effect.
317336
// (https://wiki.libsdl.org/SDL_HapticUpdateEffect)
318337
func (h *Haptic) UpdateEffect(effect int, data *HapticEffect) error {
319-
_data := (*C.SDL_HapticEffect)(unsafe.Pointer(data))
320-
return errorFromInt(int(C.SDL_HapticUpdateEffect(h.cptr(), C.int(effect), _data)))
338+
return errorFromInt(int(
339+
C.SDL_HapticUpdateEffect(
340+
h.cptr(),
341+
C.int(effect),
342+
(*C.SDL_HapticEffect)(unsafe.Pointer(data)))))
321343
}
322344

323345
// RunEffect runs the haptic effect on its associated haptic device.
324346
// (https://wiki.libsdl.org/SDL_HapticRunEffect)
325347
func (h *Haptic) RunEffect(effect int, iterations uint32) error {
326-
return errorFromInt(int(C.SDL_HapticRunEffect(h.cptr(), C.int(effect), C.Uint32(iterations))))
348+
return errorFromInt(int(
349+
C.SDL_HapticRunEffect(
350+
h.cptr(),
351+
C.int(effect),
352+
C.Uint32(iterations))))
327353
}
328354

329355
// StopEffect stops the haptic effect on its associated haptic device.
330356
// (https://wiki.libsdl.org/SDL_HapticStopEffect)
331357
func (h *Haptic) StopEffect(effect int) error {
332-
return errorFromInt(int(C.SDL_HapticStopEffect(h.cptr(), C.int(effect))))
358+
return errorFromInt(int(
359+
C.SDL_HapticStopEffect(h.cptr(), C.int(effect))))
333360
}
334361

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

341368
// GetEffectStatus returns the status of the current effect on the specified haptic device.
342369
// (https://wiki.libsdl.org/SDL_HapticGetEffectStatus)
343-
func (h *Haptic) GetEffectStatus(effect int) int {
344-
return int(C.SDL_HapticGetEffectStatus(h.cptr(), C.int(effect)))
370+
func (h *Haptic) GetEffectStatus(effect int) (int, error) {
371+
i := int(C.SDL_HapticGetEffectStatus(h.cptr(), C.int(effect)))
372+
return i, errorFromInt(i)
373+
345374
}
346375

347376
// SetGain sets the global gain of the specified haptic device.
348377
// (https://wiki.libsdl.org/SDL_HapticSetGain)
349378
func (h *Haptic) SetGain(gain int) error {
350-
return errorFromInt(int(C.SDL_HapticSetGain(h.cptr(), C.int(gain))))
379+
return errorFromInt(int(
380+
C.SDL_HapticSetGain(h.cptr(), C.int(gain))))
351381
}
352382

353383
// SetAutocenter sets the global autocenter of the device.
354384
// (https://wiki.libsdl.org/SDL_HapticSetAutocenter)
355385
func (h *Haptic) SetAutocenter(autocenter int) error {
356-
return errorFromInt(int(C.SDL_HapticSetAutocenter(h.cptr(), C.int(autocenter))))
386+
return errorFromInt(int(
387+
C.SDL_HapticSetAutocenter(h.cptr(), C.int(autocenter))))
357388
}
358389

359390
// Pause pauses a haptic device.
360391
// (https://wiki.libsdl.org/SDL_HapticPause)
361392
func (h *Haptic) Pause() error {
362-
return errorFromInt(int(C.SDL_HapticPause(h.cptr())))
393+
return errorFromInt(int(
394+
C.SDL_HapticPause(h.cptr())))
363395
}
364396

365397
// Unpause unpauses a haptic device.
366398
// (https://wiki.libsdl.org/SDL_HapticUnpause)
367399
func (h *Haptic) Unpause() error {
368-
return errorFromInt(int(C.SDL_HapticUnpause(h.cptr())))
400+
return errorFromInt(int(
401+
C.SDL_HapticUnpause(h.cptr())))
369402
}
370403

371404
// StopAll stops all the currently playing effects on a haptic device.
372405
// (https://wiki.libsdl.org/SDL_HapticStopAll)
373406
func (h *Haptic) StopAll() error {
374-
return errorFromInt(int(C.SDL_HapticStopAll(h.cptr())))
407+
return errorFromInt(int(
408+
C.SDL_HapticStopAll(h.cptr())))
375409
}
376410

377411
// RumbleSupported reports whether rumble is supported on a haptic device.
378412
// (https://wiki.libsdl.org/SDL_HapticRumbleSupported)
379-
func (h *Haptic) RumbleSupported() (ok bool, err error) {
413+
func (h *Haptic) RumbleSupported() (bool, error) {
380414
ret := int(C.SDL_HapticRumbleSupported(h.cptr()))
381415
return ret == C.SDL_TRUE, errorFromInt(ret)
382416
}
383417

384418
// RumbleInit initializes the haptic device for simple rumble playback.
385419
// (https://wiki.libsdl.org/SDL_HapticRumbleInit)
386420
func (h *Haptic) RumbleInit() error {
387-
return errorFromInt(int(C.SDL_HapticRumbleInit(h.cptr())))
421+
return errorFromInt(int(
422+
C.SDL_HapticRumbleInit(h.cptr())))
388423
}
389424

390425
// RumblePlay runs a simple rumble effect on a haptic device.
391426
// (https://wiki.libsdl.org/SDL_HapticRumblePlay)
392427
func (h *Haptic) RumblePlay(strength float32, length uint32) error {
393-
return errorFromInt(int(C.SDL_HapticRumblePlay(h.cptr(), C.float(strength), C.Uint32(length))))
428+
return errorFromInt(int(
429+
C.SDL_HapticRumblePlay(h.cptr(), C.float(strength), C.Uint32(length))))
394430
}
395431

396432
// RumbleStop stops the simple rumble on a haptic device.
397433
// (https://wiki.libsdl.org/SDL_HapticRumbleStop)
398434
func (h *Haptic) RumbleStop() error {
399-
return errorFromInt(int(C.SDL_HapticRumbleStop(h.cptr())))
435+
return errorFromInt(int(
436+
C.SDL_HapticRumbleStop(h.cptr())))
400437
}

0 commit comments

Comments
 (0)