src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 08 Jul 2008 19:35:10 +0000
branchgsoc2008_force_feedback
changeset 2506 ba8e99fe92c1
parent 2505 abfcba0f3bd1
child 2507 8ef1d0f4d0c1
permissions -rw-r--r--
Added SDL_HapticOpened().
     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_syshaptic.h"
    25 #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
    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  * Checks to see if the haptic device is valid
    63  */
    64 static int
    65 ValidHaptic(SDL_Haptic ** haptic)
    66 {
    67    int valid;
    68    
    69    if (*haptic == NULL) {
    70       SDL_SetError("Haptic device hasn't been opened yet");
    71       valid = 0;
    72    } else {
    73       valid = 1;
    74    }
    75    return valid;
    76 }
    77 
    78 
    79 /*
    80  * Returns the number of available devices.
    81  */
    82 int
    83 SDL_NumHaptics(void)
    84 {
    85    return SDL_numhaptics;
    86 }
    87 
    88 
    89 /*
    90  * Gets the name of a Haptic device by index.
    91  */
    92 const char *
    93 SDL_HapticName(int device_index)
    94 {
    95    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
    96       SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
    97       return NULL;
    98    }
    99    return SDL_SYS_HapticName(device_index);
   100 }
   101 
   102 
   103 /*
   104  * Opens a Haptic device.
   105  */
   106 SDL_Haptic *
   107 SDL_HapticOpen(int device_index)
   108 {
   109    int i;
   110    SDL_Haptic *haptic;
   111 
   112    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
   113       SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
   114       return NULL;
   115    }
   116 
   117    /* If the haptic is already open, return it */
   118    for (i=0; SDL_haptics[i]; i++) {             
   119       if (device_index == SDL_haptics[i]->index) {
   120          haptic = SDL_haptics[i];
   121          ++haptic->ref_count;
   122          return haptic;
   123       }
   124    }
   125 
   126    /* Create the haptic device */
   127    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
   128    if (haptic == NULL) {
   129       SDL_OutOfMemory();
   130       return NULL;
   131    }
   132 
   133    /* Initialize the haptic device */
   134    SDL_memset(haptic, 0, (sizeof *haptic));
   135    haptic->index = device_index;
   136    if (SDL_SYS_HapticOpen(haptic) < 0) {
   137       SDL_free(haptic);
   138       return NULL;
   139    }
   140 
   141    /* Add haptic to list */
   142    ++haptic->ref_count;
   143    for (i=0; SDL_haptics[i]; i++)
   144       /* Skip to next haptic */ ;
   145    SDL_haptics[i] = haptic;
   146 
   147    return haptic;
   148 }
   149 
   150 
   151 /*
   152  * Returns 1 if the device has been opened.
   153  */
   154 int
   155 SDL_HapticOpened(int device_index)
   156 {
   157    int i, opened;
   158 
   159    opened = 0;
   160    for (i=0; SDL_haptics[i]; i++) {
   161       if (SDL_haptics[i]->index == (Uint8) device_index) {
   162          opened = 1;
   163          break;
   164       }
   165    }
   166    return opened;
   167 }
   168 
   169 
   170 /*
   171  * Returns the index to a haptic device.
   172  */
   173 int
   174 SDL_HapticIndex(SDL_Haptic * haptic)
   175 {
   176    if (!ValidHaptic(&haptic)) {
   177       return -1;
   178    }
   179 
   180    return haptic->index;
   181 }
   182 
   183 
   184 /*
   185  * Returns SDL_TRUE if joystick has haptic features.
   186  */
   187 int
   188 SDL_JoystickIsHaptic(SDL_Joystick * joystick)
   189 {
   190    int ret;
   191 
   192    /* Must be a valid joystick */
   193    if (!SDL_PrivateJoystickValid(&joystick)) {
   194       return -1;
   195    }
   196 
   197    ret = SDL_SYS_JoystickIsHaptic(joystick);
   198 
   199    if (ret > 0) return SDL_TRUE;
   200    else if (ret == 0) return SDL_FALSE;
   201    else return -1;
   202 }
   203 
   204 
   205 /*
   206  * Opens a haptic device from a joystick.
   207  */
   208 SDL_Haptic *
   209 SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
   210 {
   211    int i;
   212    SDL_Haptic *haptic;
   213 
   214    /* Must be a valid joystick */
   215    if (!SDL_PrivateJoystickValid(&joystick)) {
   216       return NULL;
   217    }
   218 
   219    /* Joystick must be haptic */
   220    if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
   221       return NULL;
   222    }
   223 
   224    /* Check to see if joystick's haptic is already open */
   225    for (i=0; SDL_haptics[i]; i++) {
   226       if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i],joystick)) {
   227          haptic = SDL_haptics[i];
   228          ++haptic->ref_count;
   229          return haptic;
   230       }
   231    }
   232 
   233    /* Create the haptic device */
   234    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
   235    if (haptic == NULL) {
   236       SDL_OutOfMemory();
   237       return NULL;
   238    }
   239 
   240    /* Initialize the haptic device */
   241    SDL_memset(haptic, 0, (sizeof *haptic));
   242    if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
   243       SDL_free(haptic);
   244       return NULL;
   245    }
   246 
   247    /* Add haptic to list */
   248    ++haptic->ref_count;
   249    for (i=0; SDL_haptics[i]; i++)
   250       /* Skip to next haptic */ ;
   251    SDL_haptics[i] = haptic;
   252 
   253    return haptic;
   254 }
   255 
   256 
   257 /*
   258  * Closes a SDL_Haptic device.
   259  */
   260 void
   261 SDL_HapticClose(SDL_Haptic * haptic)
   262 {
   263    int i;
   264 
   265    /* Must be valid */
   266    if (!ValidHaptic(&haptic)) {
   267       return;
   268    }
   269 
   270    /* Check if it's still in use */
   271    if (--haptic->ref_count < 0) {
   272       return;
   273    }
   274 
   275    /* Close it, properly removing effects if needed */
   276    for (i=0; i<haptic->neffects; i++) {
   277       if (haptic->effects[i].hweffect != NULL) {
   278          SDL_HapticDestroyEffect(haptic,i);
   279       }
   280    }
   281    SDL_SYS_HapticClose(haptic);
   282 
   283    /* Remove from the list */
   284    for (i = 0; SDL_haptics[i]; ++i) {
   285       if (haptic == SDL_haptics[i]) {
   286          SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
   287                (SDL_numhaptics - i) * sizeof(haptic));
   288          break;
   289       }
   290    }
   291 
   292    /* Free */
   293    SDL_free(haptic);
   294 }
   295 
   296 /*
   297  * Cleans up after the subsystem.
   298  */
   299 void
   300 SDL_HapticQuit(void)
   301 {
   302    SDL_numhaptics = 0;
   303 
   304    SDL_SYS_HapticQuit();
   305    if (SDL_haptics != NULL) {
   306       SDL_free(SDL_haptics);
   307       SDL_haptics = NULL;
   308    }
   309 }
   310 
   311 /*
   312  * Returns the number of effects a haptic device has.
   313  */
   314 int
   315 SDL_HapticNumEffects(SDL_Haptic * haptic)
   316 {
   317    if (!ValidHaptic(&haptic)) {
   318       return -1;
   319    }
   320 
   321    return haptic->neffects;
   322 }
   323 
   324 /*
   325  * Returns supported effects by the device.
   326  */
   327 unsigned int
   328 SDL_HapticQuery(SDL_Haptic * haptic)
   329 {
   330    if (!ValidHaptic(&haptic)) {
   331       return -1;
   332    }
   333 
   334    return haptic->supported;
   335 }
   336 
   337 /*
   338  * Checks to see if the device can support the effect.
   339  */
   340 int
   341 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
   342 {
   343    if (!ValidHaptic(&haptic)) {
   344       return -1;
   345    }
   346 
   347    if ((haptic->supported & effect->type) != 0)
   348       return SDL_TRUE;
   349    return SDL_FALSE;
   350 }
   351 
   352 /*
   353  * Creates a new haptic effect.
   354  */
   355 int
   356 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
   357 {
   358    int i;
   359 
   360    /* Check for device validity. */
   361    if (!ValidHaptic(&haptic)) {
   362       return -1;
   363    }
   364 
   365    /* Check to see if effect is supported */
   366    if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
   367       SDL_SetError("Haptic effect not supported by haptic device.");
   368       return -1;
   369    }
   370 
   371    /* See if there's a free slot */
   372    for (i=0; i<haptic->neffects; i++) {
   373       if (haptic->effects[i].hweffect == NULL) {
   374 
   375          /* Now let the backend create the real effect */
   376          if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
   377             return -1; /* Backend failed to create effect */
   378          }
   379          return i;
   380       }
   381    }
   382 
   383    SDL_SetError("Haptic device has no free space left.");
   384    return -1;
   385 }
   386 
   387 /*
   388  * Checks to see if an effect is valid.
   389  */
   390 static int
   391 ValidEffect(SDL_Haptic * haptic, int effect)
   392 {
   393    if ((effect < 0) || (effect >= haptic->neffects)) {
   394       SDL_SetError("Invalid haptic effect identifier.");
   395       return 0;
   396    }
   397    return 1;
   398 }
   399 
   400 /*
   401  * Updates an effect.
   402  */
   403 int
   404 SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
   405 {
   406    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   407       return -1;
   408    }
   409 
   410    /* Updates the effect */
   411    if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
   412       return -1;
   413    }
   414 
   415    return 0;
   416 }
   417 
   418 
   419 /*
   420  * Runs the haptic effect on the device.
   421  */
   422 int
   423 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
   424 {
   425    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   426       return -1;
   427    }
   428 
   429    /* Run the effect */
   430    if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
   431       return -1;
   432    }
   433 
   434    return 0;
   435 }
   436 
   437 /*
   438  * Stops the haptic effect on the device.
   439  */
   440 int
   441 SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
   442 {
   443    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   444       return -1;
   445    }
   446 
   447    /* Stop the effect */
   448    if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
   449       return -1;
   450    }
   451 
   452    return 0;
   453 }
   454 
   455 /*
   456  * Gets rid of a haptic effect.
   457  */
   458 void
   459 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
   460 {
   461    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   462       return;
   463    }
   464 
   465    /* Not allocated */
   466    if (haptic->effects[effect].hweffect == NULL) {
   467       return;
   468    }
   469 
   470    SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
   471 }
   472 
   473 /*
   474  * Gets the status of a haptic effect.
   475  */
   476 int
   477 SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
   478 {
   479    if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
   480       return -1;
   481    }
   482 
   483    if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
   484       SDL_SetError("Haptic device does not support status queries.");
   485       return -1;
   486    }
   487 
   488    return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
   489 }
   490 
   491 /*
   492  * Sets the global gain of the device.
   493  */
   494 int
   495 SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
   496 {
   497    if (!ValidHaptic(&haptic)) {
   498       return -1;
   499    }
   500 
   501    if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
   502       SDL_SetError("Haptic device does not support setting gain.");
   503       return -1;
   504    }
   505 
   506    if ((gain < 0) || (gain > 100)) {
   507       SDL_SetError("Haptic gain must be between 0 and 100.");
   508       return -1;
   509    }
   510 
   511    if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
   512       return -1;
   513    }
   514 
   515    return 0;
   516 }
   517 
   518 /*
   519  * Makes the device autocenter, 0 disables.
   520  */
   521 int
   522 SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
   523 {
   524    if (!ValidHaptic(&haptic)) {
   525       return -1;
   526    }
   527 
   528    if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
   529       SDL_SetError("Haptic device does not support setting autocenter.");
   530       return -1;
   531    }
   532 
   533    if ((autocenter < 0) || (autocenter > 100)) {
   534       SDL_SetError("Haptic autocenter must be between 0 and 100.");
   535       return -1;
   536    }                                           
   537 
   538    if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
   539       return -1;
   540    }
   541 
   542    return 0;
   543 }
   544 
   545