From 5d44fa5eb7bdc3f41cf03c192f40beddd235eb1e Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Mon, 23 Jun 2008 09:01:58 +0000 Subject: [PATCH] * Implemented opening and closing of haptic devices. --- include/SDL_haptic.h | 16 ++++++++++- src/haptic/SDL_haptic.c | 47 ++++++++++++++++++++++++++++++-- src/haptic/linux/SDL_syshaptic.c | 27 +++++++++++++++--- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index fd360cc17..1eef62d8d 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -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 diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 822c2d6b2..7ffb367b6 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -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); } /* diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 8aebcfada..fa29f9f53 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -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; + + } }