src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Sun, 01 Jun 2008 18:46:51 +0000
branchgsoc2008_force_feedback
changeset 2474 3f80bf1528b4
parent 2472 3f73c88c9abb
child 2475 4b874e3a3a2c
permissions -rw-r--r--
Properly quit the SDL_haptic subsystem.
Added SDL_Haptic dummy driver.
bobbens@2472
     1
/*
bobbens@2472
     2
    SDL - Simple DirectMedia Layer
bobbens@2472
     3
    Copyright (C) 2008 Edgar Simo
bobbens@2472
     4
bobbens@2472
     5
    This library is free software; you can redistribute it and/or
bobbens@2472
     6
    modify it under the terms of the GNU Lesser General Public
bobbens@2472
     7
    License as published by the Free Software Foundation; either
bobbens@2472
     8
    version 2.1 of the License, or (at your option) any later version.
bobbens@2472
     9
bobbens@2472
    10
    This library is distributed in the hope that it will be useful,
bobbens@2472
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bobbens@2472
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bobbens@2472
    13
    Lesser General Public License for more details.
bobbens@2472
    14
bobbens@2472
    15
    You should have received a copy of the GNU Lesser General Public
bobbens@2472
    16
    License along with this library; if not, write to the Free Software
bobbens@2472
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bobbens@2472
    18
bobbens@2472
    19
    Sam Lantinga
bobbens@2472
    20
    slouken@libsdl.org
bobbens@2472
    21
*/
bobbens@2472
    22
#include "SDL_config.h"
bobbens@2472
    23
bobbens@2472
    24
#include "SDL_haptic_c.h"
bobbens@2472
    25
#include "SDL_syshaptic.h"
bobbens@2472
    26
bobbens@2472
    27
bobbens@2472
    28
static Uint8 SDL_numhaptics = 0;
bobbens@2472
    29
SDL_Haptic **SDL_haptics = NULL;
bobbens@2472
    30
static SDL_Haptic *default_haptic = NULL;
bobbens@2472
    31
bobbens@2472
    32
bobbens@2472
    33
/*
bobbens@2472
    34
 * Initializes the Haptic devices.
bobbens@2472
    35
 */
bobbens@2472
    36
int
bobbens@2472
    37
SDL_HapticInit(void)
bobbens@2472
    38
{  
bobbens@2472
    39
   int arraylen;
bobbens@2472
    40
   int status;
bobbens@2472
    41
bobbens@2472
    42
   SDL_numhaptics = 0;
bobbens@2472
    43
   status = SDL_SYS_HapticInit();
bobbens@2472
    44
   if (status >= 0) {
bobbens@2472
    45
      arraylen = (status + 1) * sizeof(*SDL_haptics);
bobbens@2472
    46
      SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
bobbens@2472
    47
      if (SDL_haptics == NULL) {
bobbens@2472
    48
         SDL_numhaptics = 0;
bobbens@2472
    49
      } else {
bobbens@2472
    50
         SDL_memset(SDL_haptics, 0, arraylen);
bobbens@2472
    51
         SDL_numhaptics = status;
bobbens@2472
    52
      }
bobbens@2472
    53
      status = 0;
bobbens@2472
    54
   }
bobbens@2472
    55
   default_haptic = NULL;
bobbens@2472
    56
bobbens@2472
    57
   return status;
bobbens@2472
    58
}
bobbens@2472
    59
bobbens@2472
    60
bobbens@2472
    61
/*
bobbens@2472
    62
 * Returns the number of available devices.
bobbens@2472
    63
 */
bobbens@2472
    64
int
bobbens@2472
    65
SDL_NumHaptics(void)
bobbens@2472
    66
{
bobbens@2472
    67
   return SDL_numhaptics;
bobbens@2472
    68
}
bobbens@2472
    69
bobbens@2472
    70
bobbens@2472
    71
/*
bobbens@2472
    72
 * Gets the name of a Haptic device by index.
bobbens@2472
    73
 */
bobbens@2472
    74
const char *
bobbens@2472
    75
SDL_HapticName(int device_index)
bobbens@2472
    76
{
bobbens@2472
    77
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    78
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    79
      return NULL;
bobbens@2472
    80
   }
bobbens@2472
    81
   return SDL_SYS_HapticName(device_index);
bobbens@2472
    82
}
bobbens@2472
    83
bobbens@2472
    84
bobbens@2472
    85
/*
bobbens@2472
    86
 * Opens a Haptic device
bobbens@2472
    87
 */
bobbens@2472
    88
SDL_Haptic *
bobbens@2472
    89
SDL_HapticOpen(int device_index)
bobbens@2472
    90
{
bobbens@2472
    91
   int i;
bobbens@2472
    92
   SDL_Haptic *haptic;
bobbens@2472
    93
bobbens@2472
    94
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    95
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    96
      return NULL;
bobbens@2472
    97
   }
bobbens@2472
    98
bobbens@2472
    99
   /* If the haptic is already open, return it */
bobbens@2472
   100
   for (i = 0; SDL_haptics[i]; ++i) {             
bobbens@2472
   101
      if (device_index == SDL_haptics[i]->index) {
bobbens@2472
   102
         haptic = SDL_haptics[i];
bobbens@2472
   103
         ++haptic->ref_count;
bobbens@2472
   104
         return haptic;
bobbens@2472
   105
      }
bobbens@2472
   106
   }
bobbens@2472
   107
bobbens@2472
   108
   /* Create and initialize the haptic */
bobbens@2472
   109
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2472
   110
   if (haptic != NULL) {
bobbens@2472
   111
      SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2472
   112
      haptic->index = device_index;
bobbens@2472
   113
      if (SDL_SYS_HapticOpen(haptic) < 0) {
bobbens@2472
   114
         SDL_free(haptic);
bobbens@2472
   115
         haptic = NULL;
bobbens@2472
   116
      } else {
bobbens@2472
   117
      }
bobbens@2472
   118
   }
bobbens@2472
   119
   if (haptic) {
bobbens@2472
   120
      /* Add haptic to list */
bobbens@2472
   121
      ++haptic->ref_count;
bobbens@2472
   122
      for (i = 0; SDL_haptics[i]; ++i)
bobbens@2472
   123
         /* Skip to next haptic */ ;
bobbens@2472
   124
      SDL_haptics[i] = haptic;
bobbens@2472
   125
   }
bobbens@2472
   126
   return haptic;
bobbens@2472
   127
}
bobbens@2474
   128
bobbens@2474
   129
bobbens@2474
   130
void
bobbens@2474
   131
SDL_HapticQuit(void)
bobbens@2474
   132
{
bobbens@2474
   133
   SDL_numhaptics = 0;
bobbens@2474
   134
bobbens@2474
   135
   SDL_SYS_HapticQuit();
bobbens@2474
   136
   if (SDL_haptics != NULL) {
bobbens@2474
   137
      SDL_free(SDL_haptics);
bobbens@2474
   138
      SDL_haptics = NULL;
bobbens@2474
   139
   }
bobbens@2474
   140
}