[gnome-games/wip/abhinavsingh/gamepad-config: 38/40] gamepad: Add GamepadInverseMapping



commit ab781ee49dad35db178a62c8b839d136ccf147a3
Author: theawless <theawless gmail com>
Date:   Sun May 28 04:23:14 2017 +0530

    gamepad: Add GamepadInverseMapping
    
    This will be used to create the sdl string from user's input mapping.

 src/Makefile.am                          |    1 +
 src/gamepad/gamepad-inverse-mapping.vala |  100 ++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index fde9547..b285f3c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -79,6 +79,7 @@ gnome_games_SOURCES = \
        gamepad/gamepad.c \
        gamepad/gamepad-dpad.c \
        gamepad/gamepad-input.c \
+       gamepad/gamepad-inverse-mapping.vala \
        gamepad/gamepad-mapping.c \
        gamepad/gamepad-mapping-error.c \
        gamepad/gamepad-mappings-manager.c \
diff --git a/src/gamepad/gamepad-inverse-mapping.vala b/src/gamepad/gamepad-inverse-mapping.vala
new file mode 100644
index 0000000..e289969
--- /dev/null
+++ b/src/gamepad/gamepad-inverse-mapping.vala
@@ -0,0 +1,100 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadInverseMapping : Object {
+       private Gamepad gamepad;
+
+       private GamepadInput[] inputs;
+       private string[] sources = { "a", "b", "x", "y",
+                                    "leftstick", "rightstick",
+                                    "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger",
+                                    "dpup", "dpleft", "dpdown", "dpright",
+                                    "back", "guide", "start",
+                                    "leftx", "lefty", "rightx", "righty" };
+       private string[] destinations;
+
+       construct {
+               destinations = new string[sources.length];
+               inputs = GamepadInput.get_standard_inputs ();
+       }
+
+       public GamepadInverseMapping (Gamepad gamepad) {
+               this.gamepad = gamepad;
+       }
+
+       public string get_sdl_string () {
+               var sdl_string = gamepad.guid + "," + gamepad.name + ",";
+               sdl_string += "platform:Linux" + ",";
+
+               for (var i = 0; i < destinations.length; ++i)
+                       if (destinations[i] != null)
+                               sdl_string += sources[i] + ":" + destinations[i] + ",";
+
+               return sdl_string;
+       }
+
+       public bool set_button_mapping (uint8 hardware_index, GamepadInput source) {
+               var destination_string = "b" + hardware_index.to_string ();
+
+               return add_destination (destination_string, source);
+       }
+
+       public bool set_axis_mapping (uint8 hardware_index, GamepadInput source) {
+               var destination_string = "a" + hardware_index.to_string ();
+
+               return add_destination (destination_string, source);
+       }
+
+       public bool set_hat_mapping (uint8 hardware_index, int32 hardware_value, GamepadInput source) {
+               var destination_string = "h" + hardware_index.to_string ();
+               destination_string += ".";
+
+               // doesn't work for many cases
+               switch (source.code) {
+               case EventCode.BTN_DPAD_UP:
+               case EventCode.BTN_DPAD_DOWN:
+                       destination_string += (hardware_value == 1) ? "1" : "3";
+
+                       break;
+               case EventCode.BTN_DPAD_LEFT:
+               case EventCode.BTN_DPAD_RIGHT:
+                       destination_string += (hardware_value == 1) ? "4" : "2";
+
+                       break;
+               default:
+                       break;
+               }
+
+               return add_destination (destination_string, source);
+       }
+
+       private bool add_destination (string destination_string, GamepadInput source) {
+               if (index_in_destinations (destination_string) != -1)
+                       return false;
+
+               var i = index_in_inputs (source);
+               if (i == -1) {
+                       warning ("Invalid input");
+
+                       return false;
+               }
+               destinations[i] = destination_string;
+
+               return true;
+       }
+
+       private int index_in_destinations (string destination_string) {
+               for (var i = 0; i < destinations.length ; ++i)
+                       if (destination_string == destinations[i])
+                               return i;
+
+               return -1;
+       }
+
+       private int index_in_inputs (GamepadInput input) {
+               for (var i = 0; i < inputs.length ; ++i)
+                       if (input == inputs[i])
+                               return i;
+
+               return -1;
+       }
+}


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