src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 16:19:54 +0000
branchgsoc2008_force_feedback
changeset 2486 24dd8b8669fa
parent 2485 67978eea6d10
child 2488 8e2bdbccf7ff
permissions -rw-r--r--
Added SDL_HAPTIC_CUSTOM (not supported on linux).
Exposed SDL_HapticEffectSupported().
     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 #include "SDL_config.h"
    23 
    24 #include "SDL_haptic_c.h"
    25 #include "SDL_syshaptic.h"
    26 
    27 
    28 static Uint8 SDL_numhaptics = 0;
    29 SDL_Haptic **SDL_haptics = NULL;
    30 static SDL_Haptic *default_haptic = NULL;
    31 
    32 
    33 /*
    34  * Initializes the Haptic devices.
    35  */
    36 int
    37 SDL_HapticInit(void)
    38 {  
    39    int arraylen;
    40    int status;
    41 
    42    SDL_numhaptics = 0;
    43    status = SDL_SYS_HapticInit();
    44    if (status >= 0) {
    45       arraylen = (status + 1) * sizeof(*SDL_haptics);
    46       SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
    47       if (SDL_haptics == NULL) {
    48          SDL_numhaptics = 0;
    49       } else {
    50          SDL_memset(SDL_haptics, 0, arraylen);
    51          SDL_numhaptics = status;
    52       }
    53       status = 0;
    54    }
    55    default_haptic = NULL;
    56 
    57    return status;
    58 }
    59 
    60 
    61 /*
    62  * Returns the number of available devices.
    63  */
    64 int
    65 SDL_NumHaptics(void)
    66 {
    67    return SDL_numhaptics;
    68 }
    69 
    70 
    71 /*
    72  * Gets the name of a Haptic device by index.
    73  */
    74 const char *
    75 SDL_HapticName(int device_index)
    76 {
    77    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
    78       SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
    79       return NULL;
    80    }
    81    return SDL_SYS_HapticName(device_index);
    82 }
    83 
    84 
    85 /*
    86  * Opens a Haptic device.
    87  */
    88 SDL_Haptic *
    89 SDL_HapticOpen(int device_index)
    90 {
    91    int i;
    92    SDL_Haptic *haptic;
    93 
    94    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
    95       SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
    96       return NULL;
    97    }
    98 
    99    /* If the haptic is already open, return it */
   100    for (i=0; SDL_haptics[i]; i++) {             
   101       if (device_index == SDL_haptics[i]->index) {
   102          haptic = SDL_haptics[i];
   103          ++haptic->ref_count;
   104          return haptic;
   105       }
   106    }
   107 
   108    /* Create the haptic device */
   109    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
   110    if (haptic == NULL) {
   111       SDL_OutOfMemory();
   112       return NULL;
   113    }
   114 
   115    /* Initialize the haptic device */
   116    SDL_memset(haptic, 0, (sizeof *haptic));
   117    haptic->index = device_index;
   118    if (SDL_SYS_HapticOpen(haptic) < 0) {
   119       SDL_free(haptic);
   120       return NULL;
   121    }
   122 
   123    /* Add haptic to list */
   124    ++haptic->ref_count;
   125    for (i = 0; SDL_haptics[i]; ++i)
   126       /* Skip to next haptic */ ;
   127    SDL_haptics[i] = haptic;
   128 
   129    return haptic;
   130 }
   131 
   132 
   133 /*
   134  * Checks to see if the haptic device is valid
   135  */
   136 static int
   137 ValidHaptic(SDL_Haptic ** haptic)
   138 {
   139    int valid;
   140 
   141    if (*haptic == NULL) {
   142       SDL_SetError("Haptic device hasn't been opened yet");
   143       valid = 0;
   144    } else {
   145       valid = 1;
   146    }
   147    return valid;
   148 }
   149 
   150 
   151 /*
   152  * Closes a SDL_Haptic device.
   153  */
   154 void
   155 SDL_HapticClose(SDL_Haptic * haptic)
   156 {
   157    int i;
   158 
   159    /* Must be valid */
   160    if (!ValidHaptic(&haptic)) {
   161       return;
   162    }
   163 
   164    /* Check if it's still in use */
   165    if (--haptic->ref_count < 0) {
   166       return;
   167    }
   168 
   169    /* Close it, properly removing effects if needed */
   170    for (i=0; i<haptic->neffects; i++) {
   171       if (haptic->effects[i].hweffect != NULL) {
   172          SDL_HapticDestroyEffect(haptic,i);
   173       }
   174    }
   175    SDL_SYS_HapticClose(haptic);
   176 
   177    /* Remove from the list */
   178    for (i = 0; SDL_haptics[i]; ++i) {
   179       if (haptic == SDL_haptics[i]) {
   180          SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
   181                (SDL_numhaptics - i) * sizeof(haptic));
   182          break;
   183       }
   184    }
   185 
   186    /* Free */
   187    SDL_free(haptic);
   188 }
   189 
   190 /*
   191  * Cleans up after the subsystem.
   192  */
   193 void
   194 SDL_HapticQuit(void)
   195 {
   196    SDL_numhaptics = 0;
   197 
   198    SDL_SYS_HapticQuit();
   199    if (SDL_haptics != NULL) {
   200       SDL_free(SDL_haptics);
   201       SDL_haptics = NULL;
   202    }
   203 }
   204 
   205 /*
   206  * Returns the number of effects a haptic device has.
   207  */
   208 int
   209 SDL_HapticNumEffects(SDL_Haptic * haptic)
   210 {
   211    if (!ValidHaptic(&haptic)) {
   212       return -1;
   213    }
   214 
   215    return haptic->neffects;
   216 }
   217 
   218 /*
   219  * Returns supported effects by the device.
   220  */
   221 unsigned int
   222 SDL_HapticQueryEffects(SDL_Haptic * haptic)
   223 {
   224    if (!ValidHaptic(&haptic)) {
   225       return -1;
   226    }
   227 
   228    return haptic->supported;
   229 }
   230 
   231 /*
   232  * Checks to see if the device can support the effect.
   233  */
   234 int
   235 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
   236 {
   237    if (!ValidHaptic(&haptic)) {
   238       return -1;
   239    }
   240 
   241    if ((haptic->supported & effect->type) != 0)
   242       return SDL_TRUE;
   243    return SDL_FALSE;
   244 }
   245 
   246 /*
   247  * Creates a new haptic effect.
   248  */
   249 int
   250 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
   251 {
   252    int i;
   253 
   254    /* Check for device validity. */
   255    if (!ValidHaptic(&haptic)) {
   256       return -1;
   257    }
   258 
   259    /* Check to see if effect is supported */
   260    if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
   261       SDL_SetError("Haptic effect not supported by haptic device.");
   262       return -1;
   263    }
   264 
   265    /* See if there's a free slot */
   266    for (i=0; i<haptic->neffects; i++) {
   267       if (haptic->effects[i].hweffect == NULL) {
   268 
   269          /* Now let the backend create the real effect */
   270          if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
   271             return -1; /* Backend failed to create effect */
   272          }
   273          return i;
   274       }
   275    }
   276 
   277    SDL_SetError("Haptic device has no free space left.");
   278    return -1;
   279 }
   280 
   281 /*
   282  * Checks to see if an effect is valid.
   283  */
   284 static int
   285 ValidEffect(SDL_Haptic * haptic, int effect)
   286 {
   287    if ((effect < 0) || (effect >= haptic->neffects)) {
   288       SDL_SetError("Invalid haptic effect identifier.");
   289       return 0;
   290    }
   291    return 1;
   292 }
   293 
   294 /*
   295  * Runs the haptic effect on the device.
   296  */
   297 int
   298 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
   299 {
   300    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   301       return -1;
   302    }
   303 
   304    /* Run the effect */
   305    if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
   306       return -1;
   307    }
   308 
   309    return 0;
   310 }
   311 
   312 /*
   313  * Stops the haptic effect on the device.
   314  */
   315 int
   316 SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
   317 {
   318    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   319       return -1;
   320    }
   321 
   322    /* Stop the effect */
   323    if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
   324       return -1;
   325    }
   326 
   327    return 0;
   328 }
   329 
   330 /*
   331  * Gets rid of a haptic effect.
   332  */
   333 void
   334 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
   335 {
   336    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   337       return;
   338    }
   339 
   340    /* Not allocated */
   341    if (haptic->effects[effect].hweffect == NULL) {
   342       return;
   343    }
   344 
   345    SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
   346 }
   347 
   348 /*
   349  * Sets the global gain of the device.
   350  */
   351 int
   352 SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
   353 {
   354    if (!ValidHaptic(&haptic)) {
   355       return -1;
   356    }
   357 
   358    if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
   359       SDL_SetError("Haptic device does not support setting gain.");
   360       return -1;
   361    }
   362 
   363    if ((gain < 0) || (gain > 100)) {
   364       SDL_SetError("Haptic gain must be between 0 and 100.");
   365       return -1;
   366    }
   367 
   368    if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
   369       return -1;
   370    }
   371 
   372    return 0;
   373 }
   374 
   375 /*
   376  * Makes the device autocenter, 0 disables.
   377  */
   378 int
   379 SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
   380 {
   381    if (!ValidHaptic(&haptic)) {
   382       return -1;
   383    }
   384 
   385    if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
   386       SDL_SetError("Haptic device does not support setting autocenter.");
   387       return -1;
   388    }
   389 
   390    if ((autocenter < 0) || (autocenter > 100)) {
   391       SDL_SetError("Haptic autocenter must be between 0 and 100.");
   392       return -1;
   393    }                                           
   394 
   395    if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
   396       return -1;
   397    }
   398 
   399    return 0;
   400 }
   401 
   402