src/haptic/SDL_haptic.c
branchgsoc2008_force_feedback
changeset 2472 3f73c88c9abb
child 2474 3f80bf1528b4
equal deleted inserted replaced
2471:910af9032c73 2472:3f73c88c9abb
       
     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 and initialize the haptic */
       
   109    haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
       
   110    if (haptic != NULL) {
       
   111       SDL_memset(haptic, 0, (sizeof *haptic));
       
   112       haptic->index = device_index;
       
   113       if (SDL_SYS_HapticOpen(haptic) < 0) {
       
   114          SDL_free(haptic);
       
   115          haptic = NULL;
       
   116       } else {
       
   117       }
       
   118    }
       
   119    if (haptic) {
       
   120       /* Add haptic to list */
       
   121       ++haptic->ref_count;
       
   122       for (i = 0; SDL_haptics[i]; ++i)
       
   123          /* Skip to next haptic */ ;
       
   124       SDL_haptics[i] = haptic;
       
   125    }
       
   126    return haptic;
       
   127 }