src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Mon, 23 Jun 2008 09:01:58 +0000
branchgsoc2008_force_feedback
changeset 2476 242d8a668ebb
parent 2475 4b874e3a3a2c
child 2477 97f75ea43a93
permissions -rw-r--r--
* Implemented opening and closing of haptic devices.
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@2475
    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
      }
bobbens@2472
   117
   }
bobbens@2472
   118
   if (haptic) {
bobbens@2472
   119
      /* Add haptic to list */
bobbens@2472
   120
      ++haptic->ref_count;
bobbens@2472
   121
      for (i = 0; SDL_haptics[i]; ++i)
bobbens@2472
   122
         /* Skip to next haptic */ ;
bobbens@2472
   123
      SDL_haptics[i] = haptic;
bobbens@2472
   124
   }
bobbens@2472
   125
   return haptic;
bobbens@2472
   126
}
bobbens@2474
   127
bobbens@2474
   128
bobbens@2475
   129
/*
bobbens@2476
   130
 * Checks to see if the haptic device is valid
bobbens@2476
   131
 */
bobbens@2476
   132
static int
bobbens@2476
   133
ValidHaptic(SDL_Haptic ** haptic)
bobbens@2476
   134
{
bobbens@2476
   135
   int valid;
bobbens@2476
   136
bobbens@2476
   137
   if (*haptic == NULL) {
bobbens@2476
   138
      SDL_SetError("Haptic device hasn't been opened yet");
bobbens@2476
   139
      valid = 0;
bobbens@2476
   140
   } else {
bobbens@2476
   141
      valid = 1;
bobbens@2476
   142
   }
bobbens@2476
   143
   return valid;
bobbens@2476
   144
}
bobbens@2476
   145
bobbens@2476
   146
bobbens@2476
   147
/*
bobbens@2475
   148
 * Closes a SDL_Haptic device.
bobbens@2475
   149
 */
bobbens@2475
   150
void
bobbens@2475
   151
SDL_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   152
{
bobbens@2476
   153
   int i;
bobbens@2476
   154
bobbens@2476
   155
   /* Must be valid */
bobbens@2476
   156
   if (!ValidHaptic(&haptic)) {
bobbens@2476
   157
      return;
bobbens@2476
   158
   }
bobbens@2476
   159
bobbens@2476
   160
   /* Check if it's still in use */
bobbens@2476
   161
   if (--haptic->ref_count < 0) {
bobbens@2476
   162
      return;
bobbens@2476
   163
   }
bobbens@2476
   164
bobbens@2476
   165
   /* Close it */
bobbens@2476
   166
   SDL_SYS_HapticClose(haptic);
bobbens@2476
   167
bobbens@2476
   168
   /* Remove from the list */
bobbens@2476
   169
   for (i = 0; SDL_haptics[i]; ++i) {
bobbens@2476
   170
      if (haptic == SDL_haptics[i]) {
bobbens@2476
   171
         SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
bobbens@2476
   172
               (SDL_numhaptics - i) * sizeof(haptic));
bobbens@2476
   173
         break;
bobbens@2476
   174
      }
bobbens@2476
   175
   }
bobbens@2476
   176
bobbens@2476
   177
   /* Free */
bobbens@2476
   178
   SDL_free(haptic);
bobbens@2475
   179
}
bobbens@2475
   180
bobbens@2475
   181
/*
bobbens@2475
   182
 * Cleans up after the subsystem.
bobbens@2475
   183
 */
bobbens@2474
   184
void
bobbens@2474
   185
SDL_HapticQuit(void)
bobbens@2474
   186
{
bobbens@2474
   187
   SDL_numhaptics = 0;
bobbens@2474
   188
bobbens@2474
   189
   SDL_SYS_HapticQuit();
bobbens@2474
   190
   if (SDL_haptics != NULL) {
bobbens@2474
   191
      SDL_free(SDL_haptics);
bobbens@2474
   192
      SDL_haptics = NULL;
bobbens@2474
   193
   }
bobbens@2474
   194
}