SDL 2.0
SDL_haptic.c File Reference
#include "../SDL_internal.h"
#include "SDL_syshaptic.h"
#include "SDL_haptic_c.h"
#include "../joystick/SDL_joystick_c.h"
#include "SDL_assert.h"
+ Include dependency graph for SDL_haptic.c:

Go to the source code of this file.

Functions

int SDL_HapticInit (void)
 
static int ValidHaptic (SDL_Haptic *haptic)
 
int SDL_NumHaptics (void)
 Count the number of haptic devices attached to the system. More...
 
const char * SDL_HapticName (int device_index)
 Get the implementation dependent name of a haptic device. More...
 
SDL_Haptic * SDL_HapticOpen (int device_index)
 Opens a haptic device for use. More...
 
int SDL_HapticOpened (int device_index)
 Checks if the haptic device at index has been opened. More...
 
int SDL_HapticIndex (SDL_Haptic *haptic)
 Gets the index of a haptic device. More...
 
int SDL_MouseIsHaptic (void)
 Gets whether or not the current mouse has haptic capabilities. More...
 
SDL_Haptic * SDL_HapticOpenFromMouse (void)
 Tries to open a haptic device from the current mouse. More...
 
int SDL_JoystickIsHaptic (SDL_Joystick *joystick)
 Checks to see if a joystick has haptic features. More...
 
SDL_Haptic * SDL_HapticOpenFromJoystick (SDL_Joystick *joystick)
 Opens a haptic device for use from a joystick device. More...
 
void SDL_HapticClose (SDL_Haptic *haptic)
 Closes a haptic device previously opened with SDL_HapticOpen(). More...
 
void SDL_HapticQuit (void)
 
int SDL_HapticNumEffects (SDL_Haptic *haptic)
 Returns the number of effects a haptic device can store. More...
 
int SDL_HapticNumEffectsPlaying (SDL_Haptic *haptic)
 Returns the number of effects a haptic device can play at the same time. More...
 
unsigned int SDL_HapticQuery (SDL_Haptic *haptic)
 Gets the haptic device's supported features in bitwise manner. More...
 
int SDL_HapticNumAxes (SDL_Haptic *haptic)
 Gets the number of haptic axes the device has. More...
 
int SDL_HapticEffectSupported (SDL_Haptic *haptic, SDL_HapticEffect *effect)
 Checks to see if effect is supported by haptic. More...
 
int SDL_HapticNewEffect (SDL_Haptic *haptic, SDL_HapticEffect *effect)
 Creates a new haptic effect on the device. More...
 
static int ValidEffect (SDL_Haptic *haptic, int effect)
 
int SDL_HapticUpdateEffect (SDL_Haptic *haptic, int effect, SDL_HapticEffect *data)
 Updates the properties of an effect. More...
 
int SDL_HapticRunEffect (SDL_Haptic *haptic, int effect, Uint32 iterations)
 Runs the haptic effect on its associated haptic device. More...
 
int SDL_HapticStopEffect (SDL_Haptic *haptic, int effect)
 Stops the haptic effect on its associated haptic device. More...
 
void SDL_HapticDestroyEffect (SDL_Haptic *haptic, int effect)
 Destroys a haptic effect on the device. More...
 
int SDL_HapticGetEffectStatus (SDL_Haptic *haptic, int effect)
 Gets the status of the current effect on the haptic device. More...
 
int SDL_HapticSetGain (SDL_Haptic *haptic, int gain)
 Sets the global gain of the device. More...
 
int SDL_HapticSetAutocenter (SDL_Haptic *haptic, int autocenter)
 Sets the global autocenter of the device. More...
 
int SDL_HapticPause (SDL_Haptic *haptic)
 Pauses a haptic device. More...
 
int SDL_HapticUnpause (SDL_Haptic *haptic)
 Unpauses a haptic device. More...
 
int SDL_HapticStopAll (SDL_Haptic *haptic)
 Stops all the currently playing effects on a haptic device. More...
 
int SDL_HapticRumbleSupported (SDL_Haptic *haptic)
 Checks to see if rumble is supported on a haptic device. More...
 
int SDL_HapticRumbleInit (SDL_Haptic *haptic)
 Initializes the haptic device for simple rumble playback. More...
 
int SDL_HapticRumblePlay (SDL_Haptic *haptic, float strength, Uint32 length)
 Runs simple rumble on a haptic device. More...
 
int SDL_HapticRumbleStop (SDL_Haptic *haptic)
 Stops the simple rumble on a haptic device. More...
 

Variables

static SDL_Haptic * SDL_haptics = NULL
 

Function Documentation

◆ SDL_HapticClose()

void SDL_HapticClose ( SDL_Haptic *  haptic)

Closes a haptic device previously opened with SDL_HapticOpen().

Parameters
hapticHaptic device to close.

Definition at line 339 of file SDL_haptic.c.

340{
341 int i;
342 SDL_Haptic *hapticlist;
343 SDL_Haptic *hapticlistprev;
344
345 /* Must be valid */
346 if (!ValidHaptic(haptic)) {
347 return;
348 }
349
350 /* Check if it's still in use */
351 if (--haptic->ref_count > 0) {
352 return;
353 }
354
355 /* Close it, properly removing effects if needed */
356 for (i = 0; i < haptic->neffects; i++) {
357 if (haptic->effects[i].hweffect != NULL) {
359 }
360 }
362
363 /* Remove from the list */
364 hapticlist = SDL_haptics;
365 hapticlistprev = NULL;
366 while ( hapticlist )
367 {
368 if (haptic == hapticlist)
369 {
370 if ( hapticlistprev )
371 {
372 /* unlink this entry */
373 hapticlistprev->next = hapticlist->next;
374 }
375 else
376 {
377 SDL_haptics = haptic->next;
378 }
379
380 break;
381 }
382 hapticlistprev = hapticlist;
383 hapticlist = hapticlist->next;
384 }
385
386 /* Free */
388}
#define SDL_free
void SDL_HapticDestroyEffect(SDL_Haptic *haptic, int effect)
Destroys a haptic effect on the device.
Definition: SDL_haptic.c:592
static int ValidHaptic(SDL_Haptic *haptic)
Definition: SDL_haptic.c:56
static SDL_Haptic * SDL_haptics
Definition: SDL_haptic.c:32
void SDL_SYS_HapticClose(SDL_Haptic *haptic)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
static SDL_Haptic * haptic
Definition: testhaptic.c:25

References haptic, i, NULL, SDL_free, SDL_HapticDestroyEffect(), SDL_haptics, SDL_SYS_HapticClose(), and ValidHaptic().

Referenced by SDL_HapticQuit().

◆ SDL_HapticDestroyEffect()

void SDL_HapticDestroyEffect ( SDL_Haptic *  haptic,
int  effect 
)

Destroys a haptic effect on the device.

This will stop the effect if it's running. Effects are automatically destroyed when the device is closed.

Parameters
hapticDevice to destroy the effect on.
effectIdentifier of the effect to destroy.
See also
SDL_HapticNewEffect

Definition at line 592 of file SDL_haptic.c.

593{
594 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
595 return;
596 }
597
598 /* Not allocated */
599 if (haptic->effects[effect].hweffect == NULL) {
600 return;
601 }
602
603 SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
604}
static int ValidEffect(SDL_Haptic *haptic, int effect)
Definition: SDL_haptic.c:514
void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)

References haptic, NULL, SDL_SYS_HapticDestroyEffect(), ValidEffect(), and ValidHaptic().

Referenced by SDL_HapticClose().

◆ SDL_HapticEffectSupported()

int SDL_HapticEffectSupported ( SDL_Haptic *  haptic,
SDL_HapticEffect effect 
)

Checks to see if effect is supported by haptic.

Parameters
hapticHaptic device to check on.
effectEffect to check to see if it is supported.
Returns
SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
See also
SDL_HapticQuery
SDL_HapticNewEffect

Definition at line 462 of file SDL_haptic.c.

463{
464 if (!ValidHaptic(haptic)) {
465 return -1;
466 }
467
468 if ((haptic->supported & effect->type) != 0)
469 return SDL_TRUE;
470 return SDL_FALSE;
471}
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163

References haptic, SDL_FALSE, SDL_TRUE, SDL_HapticEffect::type, and ValidHaptic().

Referenced by SDL_HapticNewEffect().

◆ SDL_HapticGetEffectStatus()

int SDL_HapticGetEffectStatus ( SDL_Haptic *  haptic,
int  effect 
)

Gets the status of the current effect on the haptic device.

Device must support the SDL_HAPTIC_STATUS feature.

Parameters
hapticHaptic device to query the effect status on.
effectIdentifier of the effect to query its status.
Returns
0 if it isn't playing, 1 if it is playing or -1 on error.
See also
SDL_HapticRunEffect
SDL_HapticStopEffect

Definition at line 610 of file SDL_haptic.c.

611{
612 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
613 return -1;
614 }
615
616 if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
617 return SDL_SetError("Haptic: Device does not support status queries.");
618 }
619
620 return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
621}
#define SDL_SetError
#define SDL_HAPTIC_STATUS
Device can be queried for effect status.
Definition: SDL_haptic.h:300
int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect)

References haptic, SDL_HAPTIC_STATUS, SDL_SetError, SDL_SYS_HapticGetEffectStatus(), ValidEffect(), and ValidHaptic().

◆ SDL_HapticIndex()

int SDL_HapticIndex ( SDL_Haptic *  haptic)

Gets the index of a haptic device.

Parameters
hapticHaptic device to get the index of.
Returns
The index of the haptic device or -1 on error.
See also
SDL_HapticOpen
SDL_HapticOpened

Definition at line 204 of file SDL_haptic.c.

205{
206 if (!ValidHaptic(haptic)) {
207 return -1;
208 }
209
210 return haptic->index;
211}

References haptic, and ValidHaptic().

◆ SDL_HapticInit()

int SDL_HapticInit ( void  )

Definition at line 39 of file SDL_haptic.c.

40{
41 int status;
42
43 status = SDL_SYS_HapticInit();
44 if (status >= 0) {
45 status = 0;
46 }
47
48 return status;
49}
int SDL_SYS_HapticInit(void)

References SDL_SYS_HapticInit().

Referenced by SDL_InitSubSystem().

◆ SDL_HapticName()

const char * SDL_HapticName ( int  device_index)

Get the implementation dependent name of a haptic device.

This can be called before any joysticks are opened. If no name can be found, this function returns NULL.

Parameters
device_indexIndex of the device to get its name.
Returns
Name of the device or NULL on error.
See also
SDL_NumHaptics

Definition at line 97 of file SDL_haptic.c.

98{
99 if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
100 SDL_SetError("Haptic: There are %d haptic devices available",
102 return NULL;
103 }
104 return SDL_SYS_HapticName(device_index);
105}
int SDL_NumHaptics(void)
Count the number of haptic devices attached to the system.
Definition: SDL_haptic.c:87
const char * SDL_SYS_HapticName(int index)

References NULL, SDL_NumHaptics(), SDL_SetError, and SDL_SYS_HapticName().

◆ SDL_HapticNewEffect()

int SDL_HapticNewEffect ( SDL_Haptic *  haptic,
SDL_HapticEffect effect 
)

Creates a new haptic effect on the device.

Parameters
hapticHaptic device to create the effect on.
effectProperties of the effect to create.
Returns
The identifier of the effect on success or -1 on error.
See also
SDL_HapticUpdateEffect
SDL_HapticRunEffect
SDL_HapticDestroyEffect

Definition at line 477 of file SDL_haptic.c.

478{
479 int i;
480
481 /* Check for device validity. */
482 if (!ValidHaptic(haptic)) {
483 return -1;
484 }
485
486 /* Check to see if effect is supported */
488 return SDL_SetError("Haptic: Effect not supported by haptic device.");
489 }
490
491 /* See if there's a free slot */
492 for (i = 0; i < haptic->neffects; i++) {
493 if (haptic->effects[i].hweffect == NULL) {
494
495 /* Now let the backend create the real effect */
496 if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)
497 != 0) {
498 return -1; /* Backend failed to create effect */
499 }
500
501 SDL_memcpy(&haptic->effects[i].effect, effect,
502 sizeof(SDL_HapticEffect));
503 return i;
504 }
505 }
506
507 return SDL_SetError("Haptic: Device has no free space left.");
508}
#define SDL_memcpy
int SDL_HapticEffectSupported(SDL_Haptic *haptic, SDL_HapticEffect *effect)
Checks to see if effect is supported by haptic.
Definition: SDL_haptic.c:462
int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *base)
The generic template for any haptic effect.
Definition: SDL_haptic.h:801

References haptic, i, NULL, SDL_FALSE, SDL_HapticEffectSupported(), SDL_memcpy, SDL_SetError, SDL_SYS_HapticNewEffect(), and ValidHaptic().

Referenced by SDL_HapticRumbleInit().

◆ SDL_HapticNumAxes()

int SDL_HapticNumAxes ( SDL_Haptic *  haptic)

Gets the number of haptic axes the device has.

See also
SDL_HapticDirection

Definition at line 449 of file SDL_haptic.c.

450{
451 if (!ValidHaptic(haptic)) {
452 return -1;
453 }
454
455 return haptic->naxes;
456}

References haptic, and ValidHaptic().

◆ SDL_HapticNumEffects()

int SDL_HapticNumEffects ( SDL_Haptic *  haptic)

Returns the number of effects a haptic device can store.

On some platforms this isn't fully supported, and therefore is an approximation. Always check to see if your created effect was actually created and do not rely solely on SDL_HapticNumEffects().

Parameters
hapticThe haptic device to query effect max.
Returns
The number of effects the haptic device can store or -1 on error.
See also
SDL_HapticNumEffectsPlaying
SDL_HapticQuery

Definition at line 407 of file SDL_haptic.c.

408{
409 if (!ValidHaptic(haptic)) {
410 return -1;
411 }
412
413 return haptic->neffects;
414}

References haptic, and ValidHaptic().

◆ SDL_HapticNumEffectsPlaying()

int SDL_HapticNumEffectsPlaying ( SDL_Haptic *  haptic)

Returns the number of effects a haptic device can play at the same time.

This is not supported on all platforms, but will always return a value. Added here for the sake of completeness.

Parameters
hapticThe haptic device to query maximum playing effects.
Returns
The number of effects the haptic device can play at the same time or -1 on error.
See also
SDL_HapticNumEffects
SDL_HapticQuery

Definition at line 421 of file SDL_haptic.c.

422{
423 if (!ValidHaptic(haptic)) {
424 return -1;
425 }
426
427 return haptic->nplaying;
428}

References haptic, and ValidHaptic().

◆ SDL_HapticOpen()

SDL_Haptic * SDL_HapticOpen ( int  device_index)

Opens a haptic device for use.

The index passed as an argument refers to the N'th haptic device on this system.

When opening a haptic device, its gain will be set to maximum and autocenter will be disabled. To modify these values use SDL_HapticSetGain() and SDL_HapticSetAutocenter().

Parameters
device_indexIndex of the device to open.
Returns
Device identifier or NULL on error.
See also
SDL_HapticIndex
SDL_HapticOpenFromMouse
SDL_HapticOpenFromJoystick
SDL_HapticClose
SDL_HapticSetGain
SDL_HapticSetAutocenter
SDL_HapticPause
SDL_HapticStopAll

Definition at line 112 of file SDL_haptic.c.

113{
114 SDL_Haptic *haptic;
115 SDL_Haptic *hapticlist;
116
117 if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
118 SDL_SetError("Haptic: There are %d haptic devices available",
120 return NULL;
121 }
122
123 hapticlist = SDL_haptics;
124 /* If the haptic is already open, return it
125 * TODO: Should we create haptic instance IDs like the Joystick API?
126 */
127 while ( hapticlist )
128 {
129 if (device_index == hapticlist->index) {
130 haptic = hapticlist;
131 ++haptic->ref_count;
132 return haptic;
133 }
134 hapticlist = hapticlist->next;
135 }
136
137 /* Create the haptic device */
138 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
139 if (haptic == NULL) {
141 return NULL;
142 }
143
144 /* Initialize the haptic device */
145 SDL_memset(haptic, 0, (sizeof *haptic));
146 haptic->rumble_id = -1;
147 haptic->index = device_index;
148 if (SDL_SYS_HapticOpen(haptic) < 0) {
150 return NULL;
151 }
152
153 /* Add haptic to list */
154 ++haptic->ref_count;
155 /* Link the haptic in the list */
156 haptic->next = SDL_haptics;
158
159 /* Disable autocenter and set gain to max. */
160 if (haptic->supported & SDL_HAPTIC_GAIN)
162 if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
164
165 return haptic;
166}
#define SDL_memset
#define SDL_malloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
int SDL_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
Sets the global autocenter of the device.
Definition: SDL_haptic.c:672
int SDL_HapticSetGain(SDL_Haptic *haptic, int gain)
Sets the global gain of the device.
Definition: SDL_haptic.c:627
#define SDL_HAPTIC_AUTOCENTER
Device can set autocenter.
Definition: SDL_haptic.h:291
#define SDL_HAPTIC_GAIN
Device can set global gain.
Definition: SDL_haptic.h:282
int SDL_SYS_HapticOpen(SDL_Haptic *haptic)

References haptic, NULL, SDL_free, SDL_HAPTIC_AUTOCENTER, SDL_HAPTIC_GAIN, SDL_haptics, SDL_HapticSetAutocenter(), SDL_HapticSetGain(), SDL_malloc, SDL_memset, SDL_NumHaptics(), SDL_OutOfMemory, SDL_SetError, and SDL_SYS_HapticOpen().

Referenced by SDL_HapticOpenFromMouse().

◆ SDL_HapticOpened()

int SDL_HapticOpened ( int  device_index)

Checks if the haptic device at index has been opened.

Parameters
device_indexIndex to check to see if it has been opened.
Returns
1 if it has been opened or 0 if it hasn't.
See also
SDL_HapticOpen
SDL_HapticIndex

Definition at line 173 of file SDL_haptic.c.

174{
175 int opened;
176 SDL_Haptic *hapticlist;
177
178 /* Make sure it's valid. */
179 if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
180 SDL_SetError("Haptic: There are %d haptic devices available",
182 return 0;
183 }
184
185 opened = 0;
186 hapticlist = SDL_haptics;
187 /* TODO Should this use an instance ID? */
188 while ( hapticlist )
189 {
190 if (hapticlist->index == (Uint8) device_index) {
191 opened = 1;
192 break;
193 }
194 hapticlist = hapticlist->next;
195 }
196 return opened;
197}
uint8_t Uint8
Definition: SDL_stdinc.h:179

References SDL_haptics, SDL_NumHaptics(), and SDL_SetError.

◆ SDL_HapticOpenFromJoystick()

SDL_Haptic * SDL_HapticOpenFromJoystick ( SDL_Joystick *  joystick)

Opens a haptic device for use from a joystick device.

You must still close the haptic device separately. It will not be closed with the joystick.

When opening from a joystick you should first close the haptic device before closing the joystick device. If not, on some implementations the haptic device will also get unallocated and you'll be unable to use force feedback on that device.

Parameters
joystickJoystick to create a haptic device from.
Returns
A valid haptic device identifier on success or NULL on error.
See also
SDL_HapticOpen
SDL_HapticClose

Definition at line 273 of file SDL_haptic.c.

274{
275 SDL_Haptic *haptic;
276 SDL_Haptic *hapticlist;
277
278 /* Make sure there is room. */
279 if (SDL_NumHaptics() <= 0) {
280 SDL_SetError("Haptic: There are %d haptic devices available",
282 return NULL;
283 }
284
285 /* Must be a valid joystick */
286 if (!SDL_PrivateJoystickValid(joystick)) {
287 SDL_SetError("Haptic: Joystick isn't valid.");
288 return NULL;
289 }
290
291 /* Joystick must be haptic */
292 if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
293 SDL_SetError("Haptic: Joystick isn't a haptic device.");
294 return NULL;
295 }
296
297 hapticlist = SDL_haptics;
298 /* Check to see if joystick's haptic is already open */
299 while ( hapticlist )
300 {
301 if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
302 haptic = hapticlist;
303 ++haptic->ref_count;
304 return haptic;
305 }
306 hapticlist = hapticlist->next;
307 }
308
309 /* Create the haptic device */
310 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
311 if (haptic == NULL) {
313 return NULL;
314 }
315
316 /* Initialize the haptic device */
317 SDL_memset(haptic, 0, sizeof(SDL_Haptic));
318 haptic->rumble_id = -1;
319 if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
320 SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
322 return NULL;
323 }
324
325 /* Add haptic to list */
326 ++haptic->ref_count;
327 /* Link the haptic in the list */
328 haptic->next = SDL_haptics;
330
331 return haptic;
332}
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:394
int SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
int SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)

References haptic, NULL, SDL_free, SDL_haptics, SDL_malloc, SDL_memset, SDL_NumHaptics(), SDL_OutOfMemory, SDL_PrivateJoystickValid(), SDL_SetError, SDL_SYS_HapticOpenFromJoystick(), SDL_SYS_JoystickIsHaptic(), and SDL_SYS_JoystickSameHaptic().

◆ SDL_HapticOpenFromMouse()

SDL_Haptic * SDL_HapticOpenFromMouse ( void  )

Tries to open a haptic device from the current mouse.

Returns
The haptic device identifier or NULL on error.
See also
SDL_MouseIsHaptic
SDL_HapticOpen

Definition at line 230 of file SDL_haptic.c.

231{
232 int device_index;
233
234 device_index = SDL_SYS_HapticMouse();
235
236 if (device_index < 0) {
237 SDL_SetError("Haptic: Mouse isn't a haptic device.");
238 return NULL;
239 }
240
241 return SDL_HapticOpen(device_index);
242}
SDL_Haptic * SDL_HapticOpen(int device_index)
Opens a haptic device for use.
Definition: SDL_haptic.c:112
int SDL_SYS_HapticMouse(void)

References NULL, SDL_HapticOpen(), SDL_SetError, and SDL_SYS_HapticMouse().

◆ SDL_HapticPause()

int SDL_HapticPause ( SDL_Haptic *  haptic)

Pauses a haptic device.

Device must support the SDL_HAPTIC_PAUSE feature. Call SDL_HapticUnpause() to resume playback.

Do not modify the effects nor add new ones while the device is paused. That can cause all sorts of weird errors.

Parameters
hapticHaptic device to pause.
Returns
0 on success or -1 on error.
See also
SDL_HapticUnpause

Definition at line 697 of file SDL_haptic.c.

698{
699 if (!ValidHaptic(haptic)) {
700 return -1;
701 }
702
703 if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
704 return SDL_SetError("Haptic: Device does not support setting pausing.");
705 }
706
708}
#define SDL_HAPTIC_PAUSE
Device can be paused.
Definition: SDL_haptic.h:310
int SDL_SYS_HapticPause(SDL_Haptic *haptic)

References haptic, SDL_HAPTIC_PAUSE, SDL_SetError, SDL_SYS_HapticPause(), and ValidHaptic().

◆ SDL_HapticQuery()

unsigned int SDL_HapticQuery ( SDL_Haptic *  haptic)

Gets the haptic device's supported features in bitwise manner.

Example:

printf("We have constant haptic effect!\n");
}
#define SDL_HapticQuery
#define SDL_HAPTIC_CONSTANT
Constant effect supported.
Definition: SDL_haptic.h:163
Parameters
hapticThe haptic device to query.
Returns
Haptic features in bitwise manner (OR'd).
See also
SDL_HapticNumEffects
SDL_HapticEffectSupported

Definition at line 435 of file SDL_haptic.c.

436{
437 if (!ValidHaptic(haptic)) {
438 return 0; /* same as if no effects were supported */
439 }
440
441 return haptic->supported;
442}

References haptic, and ValidHaptic().

◆ SDL_HapticQuit()

void SDL_HapticQuit ( void  )

Definition at line 394 of file SDL_haptic.c.

395{
396 while (SDL_haptics) {
398 }
399
401}
void SDL_HapticClose(SDL_Haptic *haptic)
Closes a haptic device previously opened with SDL_HapticOpen().
Definition: SDL_haptic.c:339
void SDL_SYS_HapticQuit(void)

References SDL_HapticClose(), SDL_haptics, and SDL_SYS_HapticQuit().

Referenced by SDL_QuitSubSystem().

◆ SDL_HapticRumbleInit()

int SDL_HapticRumbleInit ( SDL_Haptic *  haptic)

Initializes the haptic device for simple rumble playback.

Parameters
hapticHaptic device to initialize for simple rumble playback.
Returns
0 on success or -1 on error.
See also
SDL_HapticOpen
SDL_HapticRumbleSupported
SDL_HapticRumblePlay
SDL_HapticRumbleStop

Definition at line 758 of file SDL_haptic.c.

759{
760 SDL_HapticEffect *efx = &haptic->rumble_effect;
761
762 if (!ValidHaptic(haptic)) {
763 return -1;
764 }
765
766 /* Already allocated. */
767 if (haptic->rumble_id >= 0) {
768 return 0;
769 }
770
771 SDL_zerop(efx);
772 if (haptic->supported & SDL_HAPTIC_SINE) {
773 efx->type = SDL_HAPTIC_SINE;
775 efx->periodic.period = 1000;
776 efx->periodic.magnitude = 0x4000;
777 efx->periodic.length = 5000;
778 efx->periodic.attack_length = 0;
779 efx->periodic.fade_length = 0;
780 } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { /* XInput? */
782 efx->leftright.length = 5000;
783 efx->leftright.large_magnitude = 0x4000;
784 efx->leftright.small_magnitude = 0x4000;
785 } else {
786 return SDL_SetError("Device doesn't support rumble");
787 }
788
789 haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
790 if (haptic->rumble_id >= 0) {
791 return 0;
792 }
793 return -1;
794}
int SDL_HapticNewEffect(SDL_Haptic *haptic, SDL_HapticEffect *effect)
Creates a new haptic effect on the device.
Definition: SDL_haptic.c:477
#define SDL_HAPTIC_SINE
Sine wave effect supported.
Definition: SDL_haptic.h:172
#define SDL_HAPTIC_LEFTRIGHT
Left/Right effect supported.
Definition: SDL_haptic.h:183
#define SDL_HAPTIC_CARTESIAN
Uses cartesian coordinates for the direction.
Definition: SDL_haptic.h:330
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
SDL_HapticDirection direction
Definition: SDL_haptic.h:555
SDL_HapticLeftRight leftright
Definition: SDL_haptic.h:808
SDL_HapticPeriodic periodic
Definition: SDL_haptic.h:805

References SDL_HapticPeriodic::attack_length, SDL_HapticPeriodic::direction, SDL_HapticPeriodic::fade_length, haptic, SDL_HapticLeftRight::large_magnitude, SDL_HapticEffect::leftright, SDL_HapticPeriodic::length, SDL_HapticLeftRight::length, SDL_HapticPeriodic::magnitude, SDL_HapticPeriodic::period, SDL_HapticEffect::periodic, SDL_HAPTIC_CARTESIAN, SDL_HAPTIC_LEFTRIGHT, SDL_HAPTIC_SINE, SDL_HapticNewEffect(), SDL_SetError, SDL_zerop, SDL_HapticLeftRight::small_magnitude, SDL_HapticDirection::type, SDL_HapticEffect::type, and ValidHaptic().

◆ SDL_HapticRumblePlay()

int SDL_HapticRumblePlay ( SDL_Haptic *  haptic,
float  strength,
Uint32  length 
)

Runs simple rumble on a haptic device.

Parameters
hapticHaptic device to play rumble effect on.
strengthStrength of the rumble to play as a 0-1 float value.
lengthLength of the rumble to play in milliseconds.
Returns
0 on success or -1 on error.
See also
SDL_HapticRumbleSupported
SDL_HapticRumbleInit
SDL_HapticRumbleStop

Definition at line 800 of file SDL_haptic.c.

801{
802 SDL_HapticEffect *efx;
803 Sint16 magnitude;
804
805 if (!ValidHaptic(haptic)) {
806 return -1;
807 }
808
809 if (haptic->rumble_id < 0) {
810 return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
811 }
812
813 /* Clamp strength. */
814 if (strength > 1.0f) {
815 strength = 1.0f;
816 } else if (strength < 0.0f) {
817 strength = 0.0f;
818 }
819 magnitude = (Sint16)(32767.0f*strength);
820
821 efx = &haptic->rumble_effect;
822 if (efx->type == SDL_HAPTIC_SINE) {
823 efx->periodic.magnitude = magnitude;
824 efx->periodic.length = length;
825 } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
826 efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
827 efx->leftright.length = length;
828 } else {
829 SDL_assert(0 && "This should have been caught elsewhere");
830 }
831
832 if (SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect) < 0) {
833 return -1;
834 }
835
836 return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
837}
#define SDL_assert(condition)
Definition: SDL_assert.h:169
int SDL_HapticRunEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
Runs the haptic effect on its associated haptic device.
Definition: SDL_haptic.c:555
int SDL_HapticUpdateEffect(SDL_Haptic *haptic, int effect, SDL_HapticEffect *data)
Updates the properties of an effect.
Definition: SDL_haptic.c:527
GLuint GLsizei GLsizei * length
int16_t Sint16
Definition: SDL_stdinc.h:185

References haptic, SDL_HapticLeftRight::large_magnitude, SDL_HapticEffect::leftright, SDL_HapticPeriodic::length, SDL_HapticLeftRight::length, SDL_HapticPeriodic::magnitude, SDL_HapticEffect::periodic, SDL_assert, SDL_HAPTIC_LEFTRIGHT, SDL_HAPTIC_SINE, SDL_HapticRunEffect(), SDL_HapticUpdateEffect(), SDL_SetError, SDL_HapticLeftRight::small_magnitude, SDL_HapticEffect::type, and ValidHaptic().

◆ SDL_HapticRumbleStop()

int SDL_HapticRumbleStop ( SDL_Haptic *  haptic)

Stops the simple rumble on a haptic device.

Parameters
hapticHaptic to stop the rumble on.
Returns
0 on success or -1 on error.
See also
SDL_HapticRumbleSupported
SDL_HapticRumbleInit
SDL_HapticRumblePlay

Definition at line 843 of file SDL_haptic.c.

844{
845 if (!ValidHaptic(haptic)) {
846 return -1;
847 }
848
849 if (haptic->rumble_id < 0) {
850 return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
851 }
852
853 return SDL_HapticStopEffect(haptic, haptic->rumble_id);
854}
int SDL_HapticStopEffect(SDL_Haptic *haptic, int effect)
Stops the haptic effect on its associated haptic device.
Definition: SDL_haptic.c:574

References haptic, SDL_HapticStopEffect(), SDL_SetError, and ValidHaptic().

◆ SDL_HapticRumbleSupported()

int SDL_HapticRumbleSupported ( SDL_Haptic *  haptic)

Checks to see if rumble is supported on a haptic device.

Parameters
hapticHaptic device to check to see if it supports rumble.
Returns
SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
See also
SDL_HapticRumbleInit
SDL_HapticRumblePlay
SDL_HapticRumbleStop

Definition at line 744 of file SDL_haptic.c.

745{
746 if (!ValidHaptic(haptic)) {
747 return -1;
748 }
749
750 /* Most things can use SINE, but XInput only has LEFTRIGHT. */
751 return ((haptic->supported & (SDL_HAPTIC_SINE|SDL_HAPTIC_LEFTRIGHT)) != 0);
752}

References haptic, SDL_HAPTIC_LEFTRIGHT, SDL_HAPTIC_SINE, and ValidHaptic().

◆ SDL_HapticRunEffect()

int SDL_HapticRunEffect ( SDL_Haptic *  haptic,
int  effect,
Uint32  iterations 
)

Runs the haptic effect on its associated haptic device.

If iterations are SDL_HAPTIC_INFINITY, it'll run the effect over and over repeating the envelope (attack and fade) every time. If you only want the effect to last forever, set SDL_HAPTIC_INFINITY in the effect's length parameter.

Parameters
hapticHaptic device to run the effect on.
effectIdentifier of the haptic effect to run.
iterationsNumber of iterations to run the effect. Use SDL_HAPTIC_INFINITY for infinity.
Returns
0 on success or -1 on error.
See also
SDL_HapticStopEffect
SDL_HapticDestroyEffect
SDL_HapticGetEffectStatus

Definition at line 555 of file SDL_haptic.c.

556{
557 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
558 return -1;
559 }
560
561 /* Run the effect */
562 if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)
563 < 0) {
564 return -1;
565 }
566
567 return 0;
568}
int SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations)
static int iterations
Definition: testsprite2.c:45

References haptic, iterations, SDL_SYS_HapticRunEffect(), ValidEffect(), and ValidHaptic().

Referenced by SDL_HapticRumblePlay().

◆ SDL_HapticSetAutocenter()

int SDL_HapticSetAutocenter ( SDL_Haptic *  haptic,
int  autocenter 
)

Sets the global autocenter of the device.

Autocenter should be between 0 and 100. Setting it to 0 will disable autocentering.

Device must support the SDL_HAPTIC_AUTOCENTER feature.

Parameters
hapticHaptic device to set autocentering on.
autocenterValue to set autocenter to, 0 disables autocentering.
Returns
0 on success or -1 on error.
See also
SDL_HapticQuery

Definition at line 672 of file SDL_haptic.c.

673{
674 if (!ValidHaptic(haptic)) {
675 return -1;
676 }
677
678 if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
679 return SDL_SetError("Haptic: Device does not support setting autocenter.");
680 }
681
682 if ((autocenter < 0) || (autocenter > 100)) {
683 return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
684 }
685
686 if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
687 return -1;
688 }
689
690 return 0;
691}
int SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)

References haptic, SDL_HAPTIC_AUTOCENTER, SDL_SetError, SDL_SYS_HapticSetAutocenter(), and ValidHaptic().

Referenced by SDL_HapticOpen().

◆ SDL_HapticSetGain()

int SDL_HapticSetGain ( SDL_Haptic *  haptic,
int  gain 
)

Sets the global gain of the device.

Device must support the SDL_HAPTIC_GAIN feature.

The user may specify the maximum gain by setting the environment variable SDL_HAPTIC_GAIN_MAX which should be between 0 and 100. All calls to SDL_HapticSetGain() will scale linearly using SDL_HAPTIC_GAIN_MAX as the maximum.

Parameters
hapticHaptic device to set the gain on.
gainValue to set the gain to, should be between 0 and 100.
Returns
0 on success or -1 on error.
See also
SDL_HapticQuery

Definition at line 627 of file SDL_haptic.c.

628{
629 const char *env;
630 int real_gain, max_gain;
631
632 if (!ValidHaptic(haptic)) {
633 return -1;
634 }
635
636 if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
637 return SDL_SetError("Haptic: Device does not support setting gain.");
638 }
639
640 if ((gain < 0) || (gain > 100)) {
641 return SDL_SetError("Haptic: Gain must be between 0 and 100.");
642 }
643
644 /* We use the envvar to get the maximum gain. */
645 env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
646 if (env != NULL) {
647 max_gain = SDL_atoi(env);
648
649 /* Check for sanity. */
650 if (max_gain < 0)
651 max_gain = 0;
652 else if (max_gain > 100)
653 max_gain = 100;
654
655 /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
656 real_gain = (gain * max_gain) / 100;
657 } else {
658 real_gain = gain;
659 }
660
661 if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
662 return -1;
663 }
664
665 return 0;
666}
#define SDL_getenv
#define SDL_atoi
int SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)

References haptic, NULL, SDL_atoi, SDL_getenv, SDL_HAPTIC_GAIN, SDL_SetError, SDL_SYS_HapticSetGain(), and ValidHaptic().

Referenced by SDL_HapticOpen().

◆ SDL_HapticStopAll()

int SDL_HapticStopAll ( SDL_Haptic *  haptic)

Stops all the currently playing effects on a haptic device.

Parameters
hapticHaptic device to stop.
Returns
0 on success or -1 on error.

Definition at line 731 of file SDL_haptic.c.

732{
733 if (!ValidHaptic(haptic)) {
734 return -1;
735 }
736
738}
int SDL_SYS_HapticStopAll(SDL_Haptic *haptic)

References haptic, SDL_SYS_HapticStopAll(), and ValidHaptic().

◆ SDL_HapticStopEffect()

int SDL_HapticStopEffect ( SDL_Haptic *  haptic,
int  effect 
)

Stops the haptic effect on its associated haptic device.

Parameters
hapticHaptic device to stop the effect on.
effectIdentifier of the effect to stop.
Returns
0 on success or -1 on error.
See also
SDL_HapticRunEffect
SDL_HapticDestroyEffect

Definition at line 574 of file SDL_haptic.c.

575{
576 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
577 return -1;
578 }
579
580 /* Stop the effect */
581 if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
582 return -1;
583 }
584
585 return 0;
586}
int SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)

References haptic, SDL_SYS_HapticStopEffect(), ValidEffect(), and ValidHaptic().

Referenced by SDL_HapticRumbleStop().

◆ SDL_HapticUnpause()

int SDL_HapticUnpause ( SDL_Haptic *  haptic)

Unpauses a haptic device.

Call to unpause after SDL_HapticPause().

Parameters
hapticHaptic device to unpause.
Returns
0 on success or -1 on error.
See also
SDL_HapticPause

Definition at line 714 of file SDL_haptic.c.

715{
716 if (!ValidHaptic(haptic)) {
717 return -1;
718 }
719
720 if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
721 return 0; /* Not going to be paused, so we pretend it's unpaused. */
722 }
723
725}
int SDL_SYS_HapticUnpause(SDL_Haptic *haptic)

References haptic, SDL_HAPTIC_PAUSE, SDL_SYS_HapticUnpause(), and ValidHaptic().

◆ SDL_HapticUpdateEffect()

int SDL_HapticUpdateEffect ( SDL_Haptic *  haptic,
int  effect,
SDL_HapticEffect data 
)

Updates the properties of an effect.

Can be used dynamically, although behavior when dynamically changing direction may be strange. Specifically the effect may reupload itself and start playing from the start. You cannot change the type either when running SDL_HapticUpdateEffect().

Parameters
hapticHaptic device that has the effect.
effectIdentifier of the effect to update.
dataNew effect properties to use.
Returns
0 on success or -1 on error.
See also
SDL_HapticNewEffect
SDL_HapticRunEffect
SDL_HapticDestroyEffect

Definition at line 527 of file SDL_haptic.c.

529{
530 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
531 return -1;
532 }
533
534 /* Can't change type dynamically. */
535 if (data->type != haptic->effects[effect].effect.type) {
536 return SDL_SetError("Haptic: Updating effect type is illegal.");
537 }
538
539 /* Updates the effect */
540 if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
541 0) {
542 return -1;
543 }
544
545 SDL_memcpy(&haptic->effects[effect].effect, data,
546 sizeof(SDL_HapticEffect));
547 return 0;
548}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data)

References haptic, SDL_memcpy, SDL_SetError, SDL_SYS_HapticUpdateEffect(), ValidEffect(), and ValidHaptic().

Referenced by SDL_HapticRumblePlay().

◆ SDL_JoystickIsHaptic()

int SDL_JoystickIsHaptic ( SDL_Joystick *  joystick)

Checks to see if a joystick has haptic features.

Parameters
joystickJoystick to test for haptic capabilities.
Returns
SDL_TRUE if the joystick is haptic, SDL_FALSE if it isn't or -1 if an error occurred.
See also
SDL_HapticOpenFromJoystick

Definition at line 249 of file SDL_haptic.c.

250{
251 int ret;
252
253 /* Must be a valid joystick */
254 if (!SDL_PrivateJoystickValid(joystick)) {
255 return -1;
256 }
257
258 ret = SDL_SYS_JoystickIsHaptic(joystick);
259
260 if (ret > 0)
261 return SDL_TRUE;
262 else if (ret == 0)
263 return SDL_FALSE;
264 else
265 return -1;
266}

References SDL_FALSE, SDL_PrivateJoystickValid(), SDL_SYS_JoystickIsHaptic(), and SDL_TRUE.

◆ SDL_MouseIsHaptic()

int SDL_MouseIsHaptic ( void  )

Gets whether or not the current mouse has haptic capabilities.

Returns
SDL_TRUE if the mouse is haptic, SDL_FALSE if it isn't.
See also
SDL_HapticOpenFromMouse

Definition at line 218 of file SDL_haptic.c.

219{
220 if (SDL_SYS_HapticMouse() < 0)
221 return SDL_FALSE;
222 return SDL_TRUE;
223}

References SDL_FALSE, SDL_SYS_HapticMouse(), and SDL_TRUE.

◆ SDL_NumHaptics()

int SDL_NumHaptics ( void  )

Count the number of haptic devices attached to the system.

Returns
Number of haptic devices detected on the system.

Definition at line 87 of file SDL_haptic.c.

88{
89 return SDL_SYS_NumHaptics();
90}
int SDL_SYS_NumHaptics(void)

References SDL_SYS_NumHaptics().

Referenced by SDL_HapticName(), SDL_HapticOpen(), SDL_HapticOpened(), and SDL_HapticOpenFromJoystick().

◆ ValidEffect()

static int ValidEffect ( SDL_Haptic *  haptic,
int  effect 
)
static

Definition at line 514 of file SDL_haptic.c.

515{
516 if ((effect < 0) || (effect >= haptic->neffects)) {
517 SDL_SetError("Haptic: Invalid effect identifier.");
518 return 0;
519 }
520 return 1;
521}

References haptic, and SDL_SetError.

Referenced by SDL_HapticDestroyEffect(), SDL_HapticGetEffectStatus(), SDL_HapticRunEffect(), SDL_HapticStopEffect(), and SDL_HapticUpdateEffect().

◆ ValidHaptic()

static int ValidHaptic ( SDL_Haptic *  haptic)
static

Definition at line 56 of file SDL_haptic.c.

57{
58 int valid;
59 SDL_Haptic *hapticlist;
60
61 valid = 0;
62 if (haptic != NULL) {
63 hapticlist = SDL_haptics;
64 while ( hapticlist )
65 {
66 if (hapticlist == haptic) {
67 valid = 1;
68 break;
69 }
70 hapticlist = hapticlist->next;
71 }
72 }
73
74 /* Create the error here. */
75 if (valid == 0) {
76 SDL_SetError("Haptic: Invalid haptic device identifier");
77 }
78
79 return valid;
80}

References haptic, NULL, SDL_haptics, and SDL_SetError.

Referenced by SDL_HapticClose(), SDL_HapticDestroyEffect(), SDL_HapticEffectSupported(), SDL_HapticGetEffectStatus(), SDL_HapticIndex(), SDL_HapticNewEffect(), SDL_HapticNumAxes(), SDL_HapticNumEffects(), SDL_HapticNumEffectsPlaying(), SDL_HapticPause(), SDL_HapticQuery(), SDL_HapticRumbleInit(), SDL_HapticRumblePlay(), SDL_HapticRumbleStop(), SDL_HapticRumbleSupported(), SDL_HapticRunEffect(), SDL_HapticSetAutocenter(), SDL_HapticSetGain(), SDL_HapticStopAll(), SDL_HapticStopEffect(), SDL_HapticUnpause(), and SDL_HapticUpdateEffect().

Variable Documentation

◆ SDL_haptics

SDL_Haptic* SDL_haptics = NULL
static