include/SDL_haptic.h
author Edgar Simo <bobbens@gmail.com>
Wed, 02 Jul 2008 08:24:35 +0000
branchgsoc2008_force_feedback
changeset 2489 96adc8025331
parent 2488 8e2bdbccf7ff
child 2495 66c02abeef0e
permissions -rw-r--r--
Exposed some of the joystick stuff to the haptic subsystem.
Added SDL_JoystickIsHaptic().
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 2008 Edgar Simo
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /**
    24  * \file SDL_haptic.h
    25  *
    26  * Include file for SDL haptic subsystem
    27  */
    28 
    29 #ifndef _SDL_haptic_h
    30 #define _SDL_haptic_h
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_error.h"
    34 #include "SDL_joystick.h"
    35 
    36 #include "begin_code.h"
    37 /* Set up for C function definitions, even when using C++ */
    38 #ifdef __cplusplus
    39 /* *INDENT-OFF* */
    40 extern "C" {
    41    /* *INDENT-ON* */                                                         
    42 #endif
    43 
    44 /* The haptic structure used to identify an SDL haptic */
    45 struct _SDL_Haptic;                                                     
    46 typedef struct _SDL_Haptic SDL_Haptic;
    47 
    48 
    49 /* Different effects that can be generated */
    50 #define SDL_HAPTIC_CONSTANT   (1<<0)
    51 #define SDL_HAPTIC_SINE       (1<<1)
    52 #define SDL_HAPTIC_SQUARE     (1<<2)
    53 #define SDL_HAPTIC_TRIANGLE   (1<<3)
    54 #define SDL_HAPTIC_SAWTOOTHUP (1<<4)
    55 #define SDL_HAPTIC_SAWTOOTHDOWN (1<<5)
    56 #define SDL_HAPTIC_RAMP       (1<<6)
    57 #define SDL_HAPTIC_SPRING     (1<<7)
    58 #define SDL_HAPTIC_FRICTION   (1<<8)
    59 #define SDL_HAPTIC_DAMPER     (1<<9)
    60 #define SDL_HAPTIC_INERTIA    (1<<10)
    61 #define SDL_HAPTIC_CUSTOM     (1<<11)
    62 /* These last two are features the device has, not effects */
    63 #define SDL_HAPTIC_GAIN       (1<<12)
    64 #define SDL_HAPTIC_AUTOCENTER (1<<13)
    65 
    66 
    67 /*
    68  * All values max at 32767 (0x7fff).  Signed values also can be negative.
    69  * Time values unless specified otherwise are in milliseconds.
    70  *
    71  * Common parts:
    72  * 
    73  * Replay:
    74  *    Uint16 length;    Duration of effect.
    75  *    Uint16 delay;     Delay before starting effect.
    76  *
    77  * Trigger:
    78  *    Uint16 button;    Button that triggers effect.
    79  *    Uint16 interval;  How soon before effect can be triggered again.
    80  *
    81  * Envelope:
    82  *    Uint16 attack_length;   Duration of the attack.
    83  *    Uint16 attack_level;    Level at the start of the attack.
    84  *    Uint16 fade_length;     Duration of the fade out.
    85  *    Uint16 fade_level;      Level at the end of the fade.
    86  */
    87 typedef struct SDL_HapticConstant {
    88    /* Header */
    89    Uint16 type; /* SDL_HAPTIC_CONSTANT */
    90    Uint16 direction;
    91 
    92    /* Replay */
    93    Uint16 length;
    94    Uint16 delay;
    95 
    96    /* Trigger */
    97    Uint16 button;
    98    Uint16 interval;
    99 
   100    /* Constant */
   101    Sint16 level; /* Strength of the constant effect. */
   102 
   103    /* Envelope */
   104    Uint16 attack_length;
   105    Uint16 attack_level;
   106    Uint16 fade_length;
   107    Uint16 fade_level;
   108 } SDL_HapticConstant;
   109 typedef struct SDL_HapticPeriodic {
   110    /* Header */
   111    Uint16 type; /* SDL_HAPTIC_{SINE,SQUARE,TRIANGLE,SAWTOOTHUP,SAWTOOTHDOWN} */
   112    Uint16 direction;
   113 
   114    /* Replay */
   115    Uint16 length;
   116    Uint16 delay;
   117 
   118    /* Trigger */
   119    Uint16 button;
   120    Uint16 interval;
   121 
   122    /* Periodic */
   123    Uint16 period; /* Period of the wave */
   124    Sint16 magnitude; /* Peak value */
   125    Sint16 offset; /* Mean value of the wave */
   126    Uint16 phase; /* Horizontal shift */
   127 
   128    /* Envelope */
   129    Uint16 attack_length;
   130    Uint16 attack_level;
   131    Uint16 fade_length;
   132    Uint16 fade_level;
   133 } SDL_HapticPeriodic;
   134 typedef struct SDL_HapticCondition {
   135    /* Header */
   136    Uint16 type; /* SDL_HAPTIC_{SPRING,DAMPER,INERTIA,FRICTION} */
   137    Uint16 direction;
   138 
   139    /* Replay */
   140    Uint16 length;
   141    Uint16 delay;
   142 
   143    /* Trigger */
   144    Uint16 button;
   145    Uint16 interval;
   146 
   147    /* Condition */
   148    Uint16 right_sat; /* Level when joystick is to the right. */
   149    Uint16 left_sat; /* Level when joystick is to the left */
   150    Sint16 right_coeff; /* How fast to increase the force towards the right */
   151    Sint16 left_coeff; /* How fast to increase the force towards the left */
   152    Uint16 deadband; /* Size of the dead zone */
   153    Sint16 center; /* Position of the dead zone */
   154 } SDL_HapticCondition;
   155 typedef struct SDL_HapticRamp {
   156    /* Header */
   157    Uint16 type; /* SDL_HAPTIC_RAMP */
   158    Uint16 direction;
   159 
   160    /* Replay */
   161    Uint16 length;
   162    Uint16 delay;
   163 
   164    /* Trigger */
   165    Uint16 button;
   166    Uint16 interval;
   167 
   168    /* Ramp */
   169    Sint16 start;
   170    Sint16 end;
   171 
   172    /* Envelope */
   173    Uint16 attack_length;
   174    Uint16 attack_level;
   175    Uint16 fade_length;
   176    Uint16 fade_level;
   177 } SDL_HapticRamp;
   178 
   179 typedef union SDL_HapticEffect {
   180    /* Common for all force feedback effects */
   181    Uint16 type; /* Effect type */
   182    SDL_HapticConstant constant; /* Constant effect */
   183    SDL_HapticPeriodic periodic; /* Periodic effect */
   184    SDL_HapticCondition condition; /* Condition effect */
   185    SDL_HapticRamp ramp; /* Ramp effect */
   186 } SDL_HapticEffect;
   187 
   188 
   189 /* Function prototypes */
   190 /*
   191  * Count the number of joysticks attached to the system
   192  */
   193 extern DECLSPEC int SDLCALL SDL_NumHaptics(void);
   194 
   195 /*
   196  * Get the implementation dependent name of a Haptic device.
   197  * This can be called before any joysticks are opened.
   198  * If no name can be found, this function returns NULL.
   199  */
   200 extern DECLSPEC const char *SDLCALL SDL_HapticName(int device_index);
   201 
   202 /*
   203  * Opens a Haptic device for usage - the index passed as an
   204  * argument refers to the N'th Haptic device on this system.
   205  *
   206  * This function returns a Haptic device identifier, or Null
   207  * if an error occurred.
   208  */
   209 extern DECLSPEC SDL_Haptic * SDL_HapticOpen(int device_index);
   210 
   211 /*
   212  * Checks to see if a joystick has haptic features.
   213  *
   214  * Returns SDL_TRUE if the joystick is haptic, SDL_FALSE if it isn't
   215  * and -1 on error.
   216  */
   217 extern DECLSPEC int SDL_JoystickIsHaptic(SDL_Joystick * joystick);
   218 
   219 /*
   220  * Opens a Haptic device for usage from a Joystick device.
   221  *
   222  * Returns a valid pointer to a haptic device on success or NULL
   223  * if an error occurred.
   224  */
   225 extern DECLSPEC SDL_Haptic * SDL_HapticOpenFromJoystick(SDL_Joystick * joystick);
   226 
   227 /* 
   228  * Closes a Haptic device previously opened with SDL_HapticOpen.
   229  */
   230 extern DECLSPEC void SDL_HapticClose(SDL_Haptic * haptic);
   231 
   232 /*
   233  * Returns the number of effects a haptic device can store.
   234  */
   235 extern DECLSPEC int SDL_HapticNumEffects(SDL_Haptic * haptic);
   236 
   237 /*
   238  * Returns the supported effects.  Individual effects can be queried by
   239  * bitwise operators.
   240  *
   241  * Example:  (SDL_HapticQueryEffects(haptic) & SDL_HAPTIC_CONSTANT)
   242  */
   243 extern DECLSPEC unsigned int SDL_HapticQueryEffects(SDL_Haptic * haptic);
   244 
   245 /*
   246  * Checks to see if effect is supported by haptic.
   247  *
   248  * Returns SDL_TRUE if effect is supported, SDL_FALSE if it isn't and -1
   249  * on error.
   250  */
   251 extern DECLSPEC int SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect);
   252 
   253 /*
   254  * Creates a new haptic effect on the device.
   255  *
   256  * Returns the id of the effect on success, -1 on failure.
   257  */
   258 extern DECLSPEC int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect);
   259 
   260 /*
   261  * Uploads an effect.  Can be used dynamically, although behaviour when
   262  * dynamically changing direction may be strange.  Specifically the effect
   263  * may reupload itself and start playing from the start.  You cannot change
   264  * the type either when running UpdateEffect.
   265  *
   266  * Returns the id of the effect on success, -1 on failure.
   267  */
   268 extern DECLSPEC int SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data);
   269 
   270 /*
   271  * Runs the haptic effect on it's assosciated haptic device.
   272  *
   273  * Returns 0 on success or -1 on failure.
   274  */
   275 extern DECLSPEC int SDL_HapticRunEffect(SDL_Haptic * haptic, int effect);
   276 
   277 /*
   278  * Stops the haptic effect on it's assosciated haptic device.
   279  *
   280  * Returns 0 on success or -1 on failure.
   281  */
   282 extern DECLSPEC int SDL_HapticStopEffect(SDL_Haptic * haptic, int effect);
   283 
   284 /*
   285  * Destroys a haptic effect on the device.  This will stop the effect if it's
   286  * running.
   287  */
   288 extern DECLSPEC void SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect);
   289 
   290 /*
   291  * Sets the global gain of the device.  Gain should be between 0 and 100.
   292  *
   293  * Returns 0 on success or -1 on failure.
   294  */
   295 extern DECLSPEC int SDL_HapticSetGain(SDL_Haptic * haptic, int gain);
   296 
   297 /*
   298  * Sets the global autocenter of the device.  Autocenter should be between
   299  * 0 and 100.  Setting it to 0 will disable autocentering.
   300  *
   301  * Returns 0 on success or -1 on failure.
   302  */
   303 extern DECLSPEC int SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
   304 
   305 
   306 /* Ends C function definitions when using C++ */
   307 #ifdef __cplusplus
   308 /* *INDENT-OFF* */
   309 }
   310 /* *INDENT-ON* */
   311 #endif
   312 #include "close_code.h"
   313 
   314 #endif /* _SDL_haptic_h */
   315 
   316 /* vi: set ts=4 sw=4 expandtab: */
   317 
   318