[gnome-games/wip/abhinavsingh/keyboard-config: 1/6] keyboard: Add KeyboardMappingBuilder



commit 01a1f1af12180a52de4e58f2f898379caf201343
Author: theawless <theawless gmail com>
Date:   Mon Aug 28 04:30:17 2017 +0530

    keyboard: Add KeyboardMappingBuilder
    
    This will be used to build keyboard mapping correspondint to a standard
    gamepad.

 src/Makefile.am                            |    2 +
 src/keyboard/keyboard-mapping-builder.vala |   60 ++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 645eb45..6f57854 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -118,6 +118,8 @@ gnome_games_SOURCES = \
        grilo/grilo-cover.vala \
        grilo/grilo-media.vala \
        \
+       keyboard/keyboard-mapping-builder.vala \
+       \
        retro/retro-core-source.vala \
        retro/retro-error.vala \
        retro/retro-gamepad.vala \
diff --git a/src/keyboard/keyboard-mapping-builder.vala b/src/keyboard/keyboard-mapping-builder.vala
new file mode 100644
index 0000000..5b8b5bd
--- /dev/null
+++ b/src/keyboard/keyboard-mapping-builder.vala
@@ -0,0 +1,60 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.KeyboardMappingBuilder : Object {
+       public Retro.GamepadConfiguration mapping { private set; get; }
+       private static uint16[] GAMEPAD_KEYS;
+
+       static construct {
+               GAMEPAD_KEYS = new uint16[Retro.JoypadId.COUNT];
+               GAMEPAD_KEYS[Retro.JoypadId.B]      = EventCode.BTN_A;
+               GAMEPAD_KEYS[Retro.JoypadId.Y]      = EventCode.BTN_Y;
+               GAMEPAD_KEYS[Retro.JoypadId.SELECT] = EventCode.BTN_SELECT;
+               GAMEPAD_KEYS[Retro.JoypadId.START]  = EventCode.BTN_START;
+               GAMEPAD_KEYS[Retro.JoypadId.UP]     = EventCode.BTN_DPAD_UP;
+               GAMEPAD_KEYS[Retro.JoypadId.DOWN]   = EventCode.BTN_DPAD_DOWN;
+               GAMEPAD_KEYS[Retro.JoypadId.LEFT]   = EventCode.BTN_DPAD_LEFT;
+               GAMEPAD_KEYS[Retro.JoypadId.RIGHT]  = EventCode.BTN_DPAD_RIGHT;
+               GAMEPAD_KEYS[Retro.JoypadId.A]      = EventCode.BTN_B;
+               GAMEPAD_KEYS[Retro.JoypadId.X]      = EventCode.BTN_X;
+               GAMEPAD_KEYS[Retro.JoypadId.L]      = EventCode.BTN_TL;
+               GAMEPAD_KEYS[Retro.JoypadId.R]      = EventCode.BTN_TR;
+               GAMEPAD_KEYS[Retro.JoypadId.L2]     = EventCode.BTN_TL2;
+               GAMEPAD_KEYS[Retro.JoypadId.R2]     = EventCode.BTN_TR2;
+               GAMEPAD_KEYS[Retro.JoypadId.L3]     = EventCode.BTN_THUMBL;
+               GAMEPAD_KEYS[Retro.JoypadId.R3]     = EventCode.BTN_THUMBR;
+       }
+
+       construct {
+               mapping = new Retro.GamepadConfiguration ();
+       }
+
+       public bool set_input_mapping (GamepadInput input, uint16 keycode) {
+               var joypad_id = joypad_id_from_event_code (input.code);
+               if (joypad_id == -1)
+                       return false;
+
+               for (var i = 0; i < Retro.JoypadId.COUNT; ++i) {
+                       var key = mapping.get_button_key ((Retro.JoypadId) i);
+                       if (key == keycode) 
+                               return false;
+               }
+               mapping.set_button_key ((Retro.JoypadId) joypad_id, keycode);
+
+               return true;
+       }
+
+       public static uint16 event_code_from_joypad_id (Retro.JoypadId button) {
+               if ((int) button < Retro.JoypadId.COUNT)
+                       return GAMEPAD_KEYS[button];
+
+               return EventCode.EV_MAX;
+       }
+
+       public static int joypad_id_from_event_code (uint16 key) {
+               for (var i = 0; i < GAMEPAD_KEYS.length; ++i)
+                       if (key == GAMEPAD_KEYS[(Retro.JoypadId) i])
+                               return i;
+
+               return -1;
+       }
+}


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