Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

* Implemented opening and closing of haptic devices.

  • Loading branch information
bobbens committed Jun 23, 2008
1 parent dd37f15 commit 5d44fa5eb7bdc3f41cf03c192f40beddd235eb1e
Showing with 82 additions and 8 deletions.
  1. +15 −1 include/SDL_haptic.h
  2. +44 −3 src/haptic/SDL_haptic.c
  3. +23 −4 src/haptic/linux/SDL_syshaptic.c
@@ -65,12 +65,26 @@ typedef struct _SDL_Haptic SDL_Haptic;
extern DECLSPEC int SDLCALL SDL_NumHaptics(void);

/*
* Get the implementation dependent name of a joystick.
* Get the implementation dependent name of a Haptic device.
* This can be called before any joysticks are opened.
* If no name can be found, this function returns NULL.
*/
extern DECLSPEC const char *SDLCALL SDL_HapticName(int device_index);

/*
* Opens a Haptic device for usage - the index passed as an
* argument refers to the N'th Haptic device on this system.
*
* This function returns a Haptic device identifier, or Null
* if an error occurred.
*/
extern DECLSPEC SDL_Haptic * SDL_HapticOpen(int device_index);

/*
* Closes a Haptic device previously opened with SDL_HapticOpen.
*/
extern DECLSPEC void SDL_HapticClose(SDL_Haptic * haptic);


/* Ends C function definitions when using C++ */
#ifdef __cplusplus
@@ -113,7 +113,6 @@ SDL_HapticOpen(int device_index)
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
haptic = NULL;
} else {
}
}
if (haptic) {
@@ -127,14 +126,56 @@ SDL_HapticOpen(int device_index)
}


/*
* Checks to see if the haptic device is valid
*/
static int
ValidHaptic(SDL_Haptic ** haptic)
{
int valid;

if (*haptic == NULL) {
SDL_SetError("Haptic device hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}


/*
* Closes a SDL_Haptic device.
*/
void
SDL_HapticClose(SDL_Haptic * haptic)
{
(void)haptic;
/* TODO */
int i;

/* Must be valid */
if (!ValidHaptic(&haptic)) {
return;
}

/* Check if it's still in use */
if (--haptic->ref_count < 0) {
return;
}

/* Close it */
SDL_SYS_HapticClose(haptic);

/* Remove from the list */
for (i = 0; SDL_haptics[i]; ++i) {
if (haptic == SDL_haptics[i]) {
SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
(SDL_numhaptics - i) * sizeof(haptic));
break;
}
}

/* Free */
SDL_free(haptic);
}

/*
@@ -180,20 +180,29 @@ SDL_SYS_HapticName(int index)
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
/* TODO finish
int fd;

/* Open the character device */
fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
if (fd < 0) {
SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
return (-1);
}

/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
SDL_OutOfMemory();
close(fd);
return (-1);
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));

/* Set the hwdata */
haptic->hwdata->fd = fd;

return 0;
*/
}


@@ -203,6 +212,16 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic)
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
if (haptic->hwdata) {

/* Clean up */
close(haptic->hwdata->fd);

/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;

}
}


0 comments on commit 5d44fa5

Please sign in to comment.