Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
* Implemented opening and closing of haptic devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbens committed Jun 23, 2008
1 parent dd37f15 commit 5d44fa5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
16 changes: 15 additions & 1 deletion include/SDL_haptic.h
Expand Up @@ -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
Expand Down
47 changes: 44 additions & 3 deletions src/haptic/SDL_haptic.c
Expand Up @@ -113,7 +113,6 @@ SDL_HapticOpen(int device_index)
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
haptic = NULL;
} else {
}
}
if (haptic) {
Expand All @@ -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);
}

/*
Expand Down
27 changes: 23 additions & 4 deletions src/haptic/linux/SDL_syshaptic.c
Expand Up @@ -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;
*/
}


Expand All @@ -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;

}
}


Expand Down

0 comments on commit 5d44fa5

Please sign in to comment.