[gnome-games/wip/abhinavsingh/gamepad-config: 10/23] gamepad: Add GamepadMappingBuilder



commit f0bb8aefce4e89c6b628bd2f181984bde6fb6903
Author: theawless <theawless gmail com>
Date:   Wed Jun 21 02:55:48 2017 +0530

    gamepad: Add GamepadMappingBuilder
    
    This will be used to build the sdl string from user's inputs.

 src/Makefile.am                          |    1 +
 src/gamepad/gamepad-mapping-builder.vala |  116 ++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 0d36fa2..e3ffc01 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -99,6 +99,7 @@ gnome_games_SOURCES = \
        gamepad/gamepad.c \
        gamepad/gamepad-dpad.c \
        gamepad/gamepad-input.c \
+       gamepad/gamepad-mapping-builder.vala \
        gamepad/gamepad-mapping.c \
        gamepad/gamepad-mapping-error.c \
        gamepad/gamepad-mappings-manager.c \
diff --git a/src/gamepad/gamepad-mapping-builder.vala b/src/gamepad/gamepad-mapping-builder.vala
new file mode 100644
index 0000000..ee8678f
--- /dev/null
+++ b/src/gamepad/gamepad-mapping-builder.vala
@@ -0,0 +1,116 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadMappingBuilder : Object {
+       private struct GamepadInputSource {
+               GamepadInput input;
+               string source;
+       }
+
+       private const GamepadInputSource[] input_sources = {
+               { { EventCode.EV_ABS, EventCode.ABS_X }, "leftx" },
+               { { EventCode.EV_ABS, EventCode.ABS_Y }, "lefty" },
+               { { EventCode.EV_ABS, EventCode.ABS_RX }, "rightx" },
+               { { EventCode.EV_ABS, EventCode.ABS_RY }, "righty" },
+               { { EventCode.EV_KEY, EventCode.BTN_A }, "a" },
+               { { EventCode.EV_KEY, EventCode.BTN_B }, "b" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_DOWN }, "dpdown" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_LEFT }, "dpleft" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_RIGHT }, "dpright" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_UP }, "dpup" },
+               { { EventCode.EV_KEY, EventCode.BTN_MODE }, "guide" },
+               { { EventCode.EV_KEY, EventCode.BTN_SELECT }, "back" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL }, "leftshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR }, "rightshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_START }, "start" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBL }, "leftstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBR }, "rightstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL2 }, "lefttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR2 }, "righttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_Y }, "x" },
+               { { EventCode.EV_KEY, EventCode.BTN_X }, "y" },
+       };
+
+       private struct GamepadInputMapping {
+               string source_string;
+               string destination_string;
+       }
+
+       private GamepadInputMapping[] mappings;
+       private GamepadDPad[] dpads;
+
+       construct {
+               mappings = new GamepadInputMapping[]{};
+               dpads = new GamepadDPad[]{};
+       }
+
+       public string build_sdl_string () {
+               var sdl_string = "platform:Linux" + ",";
+
+               foreach (var mapping in mappings)
+                       sdl_string += mapping.source_string + ":" + mapping.destination_string + ",";
+
+               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, uint16 axis, GamepadInput 
source) {
+               var destination_string = "h" + hardware_index.to_string ();
+               destination_string += ".";
+
+               while (dpads.length <= hardware_index)
+                       dpads += GamepadDPad ();
+               var dpad = dpads[hardware_index];
+               var changed_value = hardware_value == 0 ? dpad.axis_values[axis] : hardware_value;
+               dpad.axis_values[axis] = hardware_value;
+
+               // add 4 so the remainder is positive
+               var dpad_position = (changed_value + axis + 4) % 4;
+               var dpad_position_2pow = Math.pow (2, dpad_position);
+               destination_string += dpad_position_2pow.to_string ();
+
+               return add_destination (destination_string, source);
+       }
+
+       private bool add_destination (string destination_string, GamepadInput source) {
+               if (is_mapping_present (destination_string))
+                       return false;
+
+               var source_string = get_mapping_source (source);
+               if (source_string == null) {
+                       warning ("Invalid input");
+
+                       return false;
+               }
+               GamepadInputMapping mapping = {source_string, destination_string};
+               mappings += mapping;
+
+               return true;
+       }
+
+       private bool is_mapping_present (string destination_string) {
+               foreach (var mapping in mappings)
+                       if (destination_string == mapping.destination_string)
+                               return true;
+
+               return false;
+       }
+
+       private string? get_mapping_source (GamepadInput input) {
+               foreach (var input_source in input_sources)
+                       if (input == input_source.input)
+                               return input_source.source;
+
+               return null;
+       }
+}


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