[gnome-games] gamepad: Add LinuxRawGamepad.centered_axis_value()



commit 946937a820822b86196cb884f39ff4824cbd7566
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sat Aug 27 08:50:55 2016 +0200

    gamepad: Add LinuxRawGamepad.centered_axis_value()
    
    This allows to properly compute the value of axis event's value even if
    its range is different from [-abs_info.maximum:abs_info.maximum].
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770468

 src/gamepad/gamepad.vala                 |    2 +-
 src/gamepad/linux/linux-raw-gamepad.vala |   17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)
---
diff --git a/src/gamepad/gamepad.vala b/src/gamepad/gamepad.vala
index ff168fa..eb388be 100644
--- a/src/gamepad/gamepad.vala
+++ b/src/gamepad/gamepad.vala
@@ -89,7 +89,7 @@ private class Games.Gamepad : Object {
 
                        break;
                case GamepadInputType.BUTTON:
-                       button_event (event.button, (bool) value);
+                       button_event (event.button, value > 0);
 
                        break;
                }
diff --git a/src/gamepad/linux/linux-raw-gamepad.vala b/src/gamepad/linux/linux-raw-gamepad.vala
index 0747623..ec078c6 100644
--- a/src/gamepad/linux/linux-raw-gamepad.vala
+++ b/src/gamepad/linux/linux-raw-gamepad.vala
@@ -156,7 +156,8 @@ private class Games.LinuxRawGamepad : Object, RawGamepad {
                                break;
                        default:
                                var axis = abs_map[code];
-                               axis_event (axis, (double) event.value / abs_info[axis].maximum);
+                               var value = centered_axis_value (abs_info[axis], event.value);
+                               axis_event (axis, value);
 
                                break;
                        }
@@ -224,6 +225,20 @@ private class Games.LinuxRawGamepad : Object, RawGamepad {
                        return StandardGamepadButton.UNKNOWN;
                }
        }
+
+       private static double centered_axis_value (Linux.Input.AbsInfo abs_info, int32 value) {
+               var min_absolute = ((int64) abs_info.minimum).abs ();
+
+               var max_normalized = ((int64) abs_info.maximum) + min_absolute;
+               var value_normalized = ((int64) value) + min_absolute;
+
+               var max_centered = max_normalized / 2;
+               var value_centered = value_normalized - max_normalized + max_centered;
+
+               var divisor = value_centered < 0 ? max_centered + 1 : max_centered;
+
+               return (double) value_centered / (double) divisor;
+       }
 }
 
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]