HapticSetGain checks to see if device supports it. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 14:21:09 +0000
branchgsoc2008_force_feedback
changeset 2484 666472fd4cb0
parent 2483 9d52368ebcf5
child 2485 67978eea6d10
HapticSetGain checks to see if device supports it. Added HapticSetAutocenter().
include/SDL_haptic.h
src/haptic/SDL_haptic.c
src/haptic/SDL_syshaptic.h
src/haptic/linux/SDL_syshaptic.c
--- a/include/SDL_haptic.h	Tue Jul 01 14:09:53 2008 +0000
+++ b/include/SDL_haptic.h	Tue Jul 01 14:21:09 2008 +0000
@@ -256,6 +256,14 @@
  */
 extern DECLSPEC int SDL_HapticSetGain(SDL_Haptic * haptic, int gain);
 
+/*
+ * Sets the global autocenter of the device.  Autocenter should be between
+ * 0 and 100.  Setting it to 0 will disable autocentering.
+ *
+ * Returns 0 on success or -1 on failure.
+ */
+extern DECLSPEC int SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
+
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
--- a/src/haptic/SDL_haptic.c	Tue Jul 01 14:09:53 2008 +0000
+++ b/src/haptic/SDL_haptic.c	Tue Jul 01 14:21:09 2008 +0000
@@ -334,6 +334,11 @@
       return -1;
    }
 
+   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
+      SDL_SetError("Haptic device does not support setting gain.");
+      return -1;
+   }
+
    if ((gain < 0) || (gain > 100)) {
       SDL_SetError("Haptic gain must be between 0 and 100.");
       return -1;
@@ -346,4 +351,31 @@
    return 0;
 }
 
+/*
+ * Makes the device autocenter, 0 disables.
+ */
+int
+SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
+{
+   if (!ValidHaptic(&haptic)) {
+      return -1;
+   }
 
+   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
+      SDL_SetError("Haptic device does not support setting autocenter.");
+      return -1;
+   }
+
+   if ((autocenter < 0) || (autocenter > 100)) {
+      SDL_SetError("Haptic autocenter must be between 0 and 100.");
+      return -1;
+   }                                           
+
+   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
+      return -1;
+   }
+
+   return 0;
+}
+
+
--- a/src/haptic/SDL_syshaptic.h	Tue Jul 01 14:09:53 2008 +0000
+++ b/src/haptic/SDL_syshaptic.h	Tue Jul 01 14:21:09 2008 +0000
@@ -59,5 +59,6 @@
 extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic,
       struct haptic_effect * effect);
 extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain);
+extern int SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
 
 
--- a/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 14:09:53 2008 +0000
+++ b/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 14:21:09 2008 +0000
@@ -519,13 +519,36 @@
    ie.type = EV_FF;
    ie.code = FF_GAIN;
    ie.value = (0xFFFFUL * gain) / 100;
-   printf("%d\n",ie.value);
 
-   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) == -1) {
+   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
       SDL_SetError("Error setting gain.");
+      return -1;
    }
 
+   return 0;
 }
 
 
+/*
+ * Sets the autocentering.
+ */
+int
+SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
+{
+   struct input_event ie;
+
+   ie.type = EV_FF;
+   ie.code = FF_AUTOCENTER;
+   ie.value = (0xFFFFUL * autocenter) / 100;
+
+   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
+      SDL_SetError("Error setting autocenter.");
+      return -1;
+   }
+
+   return 0;
+}
+
+
+
 #endif /* SDL_HAPTIC_LINUX */