SDL2::haptic - SDL Haptic Subsystem Allowing You to Control Haptic (Force Feedback) Devices
use SDL2 qw[:haptic];
The SDL haptic subsystem allows you to control haptic (force feedback) devices.
The basic usage is as follows:
SDL_INIT_HAPTIC
SDL_HapticOpen( ... )
SDL_HapticOpenFromJoystick( ... )
SDL_HapticEffect
SDL_HapticNewEffect( ... )
SDL_HapticRunEffect( ... )
SDL_HapticDestroyEffect( ... )
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 }
These may be imported by name or with the :haptic tag.
:haptic
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_GetError( )
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.
undef
Expected parameters include:
device_index
Returns the name of the device or undef on failure; call SDL_GetError( ) for more information.
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( ... ).
SDL_HapticSetGain( ... )
SDL_HapticSetAutocenter( ... )
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.
Returns 1 if it has been opened, 0 if it hasn't or on failure; call SDL_GetError( ) for more information.
1
0
SDL_HapticIndex( ... )
Get the index of a haptic device.
haptic
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_TRUE
SDL_FALSE
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.
joystick
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.
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.
Returns a valid haptic device identifier on success or undef on failure; call SDL_GetError( ) for more information.
Close a haptic device previously opened with SDL_HapticOpen().
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().
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.
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.
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.
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.
effect
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.
Create a new haptic effect on a specified device.
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( ... ).
data
Returns 0 on success or a negative error code on failure; call SDL_GetError( ) for more information.
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.
iterations
SDL_HAPTIC_INFINITY
SDL_HapticStopEffect( ... )
Stop the haptic effect on its associated haptic device.
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.
SDL_HapticGetEffectStatus( ... )
Get the status of the current effect on the specified haptic device.
Device must support the SDL_HAPTIC_STATUS feature.
SDL_HAPTIC_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.
Set the global gain of the specified haptic device.
Device must support the SDL_HAPTIC_GAIN feature.
SDL_HAPTIC_GAIN
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.
SDL_HAPTIC_GAIN_MAX
100
gain
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.
SDL_HAPTIC_AUTOCENTER
autocenter
SDL_HapticPause( ... )
Pause a haptic device.
Device must support the SDL_HAPTIC_PAUSE feature. Call SDL_HapticUnpause( ... ) to resume playback.
SDL_HAPTIC_PAUSE
SDL_HapticUnpause( ... )
Do not modify the effects nor add new ones while the device is paused. That can cause all sorts of weird errors.
Unpause a haptic device.
Call to unpause after SDL_HapticPause( ... ).
SDL_HapticStopAll( ... )
Stop all the currently playing effects on a haptic device.
SDL_HapticRumbleSupported( ... )
Check whether rumble is supported on a haptic device.
SDL_HapticRumbleInit( ... )
Initialize a haptic device for simple rumble playback.
SDL_HapticRumblePlay( ... )
Run a simple rumble effect on a haptic device.
strength
length
SDL_HapticRumbleStop( ... )
Stop the simple rumble on a haptic device.
These may be imported by name or with the given tag.
Different haptic features a device can have. These values may all be imported with the :haptic tag.
SDL_HAPTIC_CONSTANT
SDL_HAPTIC_SINE
SDL_HAPTIC_LEFTRIGHT
SDL_HAPTIC_TRIANGLE
SDL_HAPTIC_SAWTOOTHUP
SDL_HAPTIC_SAWTOOTHDOWN
SDL_HAPTIC_RAMP
SDL_HAPTIC_SPRING
SDL_HAPTIC_DAMPER
SDL_HAPTIC_INERTIA
SDL_HAPTIC_FRICTION
SDL_HAPTIC_CUSTOM
These last few are features the device has, not effects:
SDL_HAPTIC_POLAR
SDL_HAPTIC_CARTESIAN
SDL_HAPTIC_SPHERICAL
SDL_HAPTIC_STEERING_AXIS
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.
Sanko Robinson <sanko@cpan.org>
To install SDL2::FFI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SDL2::FFI
CPAN shell
perl -MCPAN -e shell install SDL2::FFI
For more information on module installation, please visit the detailed CPAN module installation guide.