* Implemented opening and closing of haptic devices. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Mon, 23 Jun 2008 09:01:58 +0000
branchgsoc2008_force_feedback
changeset 2476 242d8a668ebb
parent 2475 4b874e3a3a2c
child 2477 97f75ea43a93
* Implemented opening and closing of haptic devices.
include/SDL_haptic.h
src/haptic/SDL_haptic.c
src/haptic/linux/SDL_syshaptic.c
--- a/include/SDL_haptic.h	Sun Jun 01 19:11:49 2008 +0000
+++ b/include/SDL_haptic.h	Mon Jun 23 09:01:58 2008 +0000
@@ -65,12 +65,26 @@
 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
--- a/src/haptic/SDL_haptic.c	Sun Jun 01 19:11:49 2008 +0000
+++ b/src/haptic/SDL_haptic.c	Mon Jun 23 09:01:58 2008 +0000
@@ -113,7 +113,6 @@
       if (SDL_SYS_HapticOpen(haptic) < 0) {
          SDL_free(haptic);
          haptic = NULL;
-      } else {
       }
    }
    if (haptic) {
@@ -128,13 +127,55 @@
 
 
 /*
+ * 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);
 }
 
 /*
--- a/src/haptic/linux/SDL_syshaptic.c	Sun Jun 01 19:11:49 2008 +0000
+++ b/src/haptic/linux/SDL_syshaptic.c	Mon Jun 23 09:01:58 2008 +0000
@@ -180,20 +180,29 @@
 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 @@
 void
 SDL_SYS_HapticClose(SDL_Haptic * haptic)
 {
+   if (haptic->hwdata) {
+
+      /* Clean up */
+      close(haptic->hwdata->fd);
+
+      /* Free */
+      SDL_free(haptic->hwdata);
+      haptic->hwdata = NULL;
+
+   }
 }