NAME
SDL2::haptic - SDL Haptic Subsystem Allowing You to Control Haptic (Force Feedback) Devices
SYNOPSIS
use SDL2 qw[:haptic];
DESCRIPTION
The SDL haptic subsystem allows you to control haptic (force feedback) devices.
The basic usage is as follows:
- - Initialize the subsystem (
SDL_INIT_HAPTIC
). - - Open a haptic device.
-
- -
SDL_HapticOpen( ... )
to open from index. - -
SDL_HapticOpenFromJoystick( ... )
to open from an existing joystick.
- -
- - Create an effect (
SDL_HapticEffect
). - - Upload the effect with
SDL_HapticNewEffect( ... )
. - - Run the effect with
SDL_HapticRunEffect( ... )
. - - (optional) Free the effect with
SDL_HapticDestroyEffect( ... )
. - - Close the haptic device with
SDL_HapticClose( ... )
.
Simple rumble example:
# Open the device
my $haptic = SDL_HapticOpen( 0 );
$haptic // return -1;
# Initialize simple rumble
return -1 if SDL_HapticRumbleInit( $haptic ) != 0;
# Play effect at 50% strength for 2 seconds
return -1 if SDL_HapticRumblePlay( $haptic, 0.5, 2000 ) != 0;
SDL_Delay( 2000 );
# Clean up
SDL_HapticClose( $haptic );
Complete example:
sub test_haptic ($joystick) {
my $effect_id;
# Open the device
my $haptic = SDL_HapticOpenFromJoystick($joystick);
$haptic // return -1; # Most likely joystick isn't haptic
# See if it can do sine waves
if ( ( SDL_HapticQuery($haptic) & SDL_HAPTIC_SINE ) == 0 ) {
SDL_HapticClose($haptic); # No sine effect
return -1;
}
# Create the effect
my $effect = SDL2::HapticEffect->new();
$effect->type(SDL_HAPTIC_SINE);
$effect->periodic->direction->type(SDL_HAPTIC_POLAR); # Polar coordinates
$effect->periodic->direction->dir->[0] = 18000; # Force comes from south
$effect->periodic->period(1000); # 1000 ms
$effect->periodic->magnitude(20000); # 20000/32767 strength
$effect->periodic->length(5000); # 5 seconds long
$effect->periodic->attack_length(1000); # Takes 1 second to get max strength
$effect->periodic->fade_length(1000); # Takes 1 second to fade away
# Upload the effect
$effect_id = SDL_HapticNewEffect( $haptic, \$effect );
# Test the effect
SDL_HapticRunEffect( $haptic, $effect_id, 1 );
SDL_Delay(5000); # Wait for the effect to finish
# We destroy the effect, although closing the device also does this
SDL_HapticDestroyEffect( $haptic, $effect_id );
# Close the device
SDL_HapticClose($haptic);
return 0; # Success
}
Functions
These may be imported by name or with the :haptic
tag.
SDL_NumHaptics( ... )
Count the number of haptic devices attached to the system.
Returns the number of haptic devices detected on the system or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticName( ... )
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 undef
.
Expected parameters include:
Returns the name of the device or undef
on failure; call SDL_GetError( )
for more information.
SDL_HapticOpen( ... )
Open 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( ... )
.
Expected parameters include:
Returns the device identifier or undef
on failure; call SDL_GetError( )
for more information.
SDL_HapticOpened( ... )
Check if the haptic device at the designated index has been opened.
Expected parameters include:
Returns 1
if it has been opened, 0
if it hasn't or on failure; call SDL_GetError( )
for more information.
SDL_HapticIndex( ... )
Get the index of a haptic device.
Expected parameters include:
haptic
- the SDL2::Haptic device to query
Returns the index of the specified haptic device or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_MouseIsHaptic( )
Query whether or not the current mouse has haptic capabilities.
Returns SDL_TRUE
if the mouse is haptic or SDL_FALSE
if it isn't.
SDL_HapticOpenFromMouse( )
Try to open a haptic device from the current mouse.
Returns the haptic device identifier or undef
on failure; call SDL_GetError( )
for more information.
SDL_JoystickIsHaptic( ... )
Query if a joystick has haptic features.
Expected parameters include:
joystick
- the SDL2::Joystick to test for haptic capabilities
Returns SDL_TRUE
if the joystick is haptic, SDL_FALSE
if it isn't, or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticOpenFromJoystick( ... )
Open 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 opened 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.
Expected parameters include:
joystick
- the SDL2::Joystick to create a haptic device from
Returns a valid haptic device identifier on success or undef
on failure; call SDL_GetError( )
for more information.
SDL_HapticClose( ... )
Close a haptic device previously opened with SDL_HapticOpen().
Expected parameters include:
haptic
- the SDL2::Haptic device to close
SDL_HapticNumEffects( ... )
Get 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().
Expected parameters include:
haptic
- the SDL2::Haptic device to query
Returns the number of effects the haptic device can store or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticNumEffectsPlaying( ... )
Get 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.
Expected parameters include:
haptic
- the SDL2::Haptic device to query maximum playing effects
Returns the number of effects the haptic device can play at the same time or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticQuery( ... )
Get the haptic device's supported features in bitwise manner.
Expected parameters include:
haptic
- the SDL2::Haptic device to query
Returns a list of supported haptic features in bitwise manner (OR'd), or 0
on failure; call SDL_GetError( )
for more information.
SDL_HapticNumAxes( ... )
Get the number of haptic axes the device has.
The number of haptic axes might be useful if working with the SDL2::HapticDirection effect.
Expected parameters include:
haptic
- the SDL2::Haptic device to query
Returns the number of axes on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticEffectSupported( ... )
Check to see if an effect is supported by a haptic device.
Expected parameters include:
haptic
- the SDL2::Haptic device to queryeffect
- the desired effect to query
Returns SDL_TRUE
if effect is supported, SDL_FALSE
if it isn't, or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticNewEffect( ... )
Create a new haptic effect on a specified device.
Expected parameters include:
haptic
- an SDL2::Haptic device to create the effect oneffect
- an SDL2::HapticEffect structure containing the properties of the effect to create
Returns the ID of the effect on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticUpdateEffect( ... )
Update the properties of an effect.
Can be used dynamically, although behavior when dynamically changing direction may be strange. Specifically the effect may re-upload itself and start playing from the start. You also cannot change the type either when running SDL_HapticUpdateEffect( ... )
.
Expected parameters include:
haptic
- the SDL2::Haptic device that has the effecteffect
- the identifier of the effect to updatedata
- an SDL2::HapticEffect structure containing the new effect properties to use
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticRunEffect( ... )
Run the haptic effect on its associated haptic device.
To repeat the effect over and over indefinitely, set iterations
to SDL_HAPTIC_INFINITY
. (Repeats the envelope - attack and fade.) To make one instance of the effect last indefinitely (so the effect does not fade), set the effect's `length` in its structure/union to SDL_HAPTIC_INFINITY
instead.
Expected parameters include:
haptic
- the SDL2::Haptic device to run the effect oneffect
- the ID of the haptic effect to runiterations
- the number of iterations to run the effect; useSDL_HAPTIC_INFINITY
to repeat forever
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticStopEffect( ... )
Stop the haptic effect on its associated haptic device.
Expected parameters include:
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticDestroyEffect( ... )
Destroy a haptic effect on the device.
This will stop the effect if it's running. Effects are automatically destroyed when the device is closed.
Expected parameters include:
haptic
- the SDL2::Haptic device to destroy the effect oneffect
- the ID of the haptic effect to destroy
SDL_HapticGetEffectStatus( ... )
Get the status of the current effect on the specified haptic device.
Device must support the SDL_HAPTIC_STATUS
feature.
Expected parameters include:
haptic
- the SDL2::Haptic device to query for the effect status oneffect
- the ID of the haptic effect to query its status
Returns 0
if it isn't playing, 1
if it is playing, or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticSetGain( ... )
Set the global gain of the specified haptic 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.
Expected parameters include:
haptic
- the SDL2::Haptic device to set the gain ongain
- value to set the gain to, should be between0
and100
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticSetAutocenter( ... )
Set 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.
Expected parameters include:
haptic
- the SDL2::Haptic device to set autocentering onautocenter
- value to set autocenter to (0-100)
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticPause( ... )
Pause 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.
Expected parameters include:
haptic
- the SDL2::Haptic device to pause
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticUnpause( ... )
Unpause a haptic device.
Call to unpause after SDL_HapticPause( ... )
.
Expected parameters include:
haptic
- the SDL2::Haptic device to unpause
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticStopAll( ... )
Stop all the currently playing effects on a haptic device.
Expected parameters include:
haptic
- the SDL2::Haptic device to stop
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticRumbleSupported( ... )
Check whether rumble is supported on a haptic device.
Expected parameters include:
Returns SDL_TRUE
if effect is supported, SDL_FALSE
if it isn't, or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticRumbleInit( ... )
Initialize a haptic device for simple rumble playback.
Expected parameters include:
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticRumblePlay( ... )
Run a simple rumble effect on a haptic device.
Expected parameters include:
haptic
- the haptic device to play the rumble effect onstrength
- strength of the rumble to play as a 0-1 float valuelength
- length of the rumble to play in milliseconds
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
SDL_HapticRumbleStop( ... )
Stop the simple rumble on a haptic device.
Expected parameters include:
Returns 0
on success or a negative error code on failure; call SDL_GetError( )
for more information.
Defined Values and Enumerations
These may be imported by name or with the given tag.
Haptic Features
Different haptic features a device can have. These values may all be imported with the :haptic
tag.
Haptic effects
SDL_HAPTIC_CONSTANT
- Constant haptic effectSDL_HAPTIC_SINE
- Periodic haptic effect that simulates sine wavesSDL_HAPTIC_LEFTRIGHT
- Haptic effect for direct control over high/low frequency motorsSDL_HAPTIC_TRIANGLE
- Periodic haptic effect that simulates triangular wavesSDL_HAPTIC_SAWTOOTHUP
- Periodic haptic effect that simulates saw tooth up wavesSDL_HAPTIC_SAWTOOTHDOWN
- Periodic haptic effect that simulates saw tooth down wavesSDL_HAPTIC_RAMP
- Ramp haptic effectSDL_HAPTIC_SPRING
- Condition haptic effect that simulates a spring. Effect is based on the axes positionSDL_HAPTIC_DAMPER
- Condition haptic effect that simulates dampening. Effect is based on the axes velocitySDL_HAPTIC_INERTIA
- Condition haptic effect that simulates inertia. Effect is based on the axes accelerationSDL_HAPTIC_FRICTION
- Condition haptic effect that simulates friction. Effect is based on the axes movementSDL_HAPTIC_CUSTOM
- User defined custom haptic effect
These last few are features the device has, not effects:
SDL_HAPTIC_GAIN
- Device supports setting the global gainSDL_HAPTIC_AUTOCENTER
- Device supports setting autocenterSDL_HAPTIC_STATUS
- Device supports querying effect statusSDL_HAPTIC_PAUSE
- Devices supports being paused
Direction encodings
SDL_HAPTIC_POLAR
- Uses polar coordinates for the directionSDL_HAPTIC_CARTESIAN
- Uses cartesian coordinates for the directionSDL_HAPTIC_SPHERICAL
- Uses spherical coordinates for the directionSDL_HAPTIC_STEERING_AXIS
- Use this value to play an effect on the steering wheel axis. This provides better compatibility across platforms and devices as SDL will guess the correct axis
Misc defines
LICENSE
Copyright (C) Sanko Robinson.
This library is free software; you can redistribute it and/or modify it under the terms found in the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted through this module.
AUTHOR
Sanko Robinson <sanko@cpan.org>