[gnome-games] retro: Add RetroGamepad



commit 2defe533fabe68fddb05f125eb339483efa3a353
Author: Megh Parikh <meghprkh gmail com>
Date:   Mon Jul 18 22:47:49 2016 +0530

    retro: Add RetroGamepad
    
    This class maps a Gamepad to a libretro InputDevice.
    
    This is part of a series of commits to add gamepad support.

 src/Makefile.am              |    1 +
 src/retro/retro-gamepad.vala |  105 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 36dd2b9..d06af17 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -73,6 +73,7 @@ gnome_games_SOURCES = \
        grilo/grilo-media.vala \
        \
        retro/retro-error.vala \
+       retro/retro-gamepad.vala \
        retro/retro-log.vala \
        retro/retro-runner.vala \
        \
diff --git a/src/retro/retro-gamepad.vala b/src/retro/retro-gamepad.vala
new file mode 100644
index 0000000..7341958
--- /dev/null
+++ b/src/retro/retro-gamepad.vala
@@ -0,0 +1,105 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private class Games.RetroGamepad: Object, Retro.InputDevice {
+       public Gamepad gamepad { get; construct; }
+
+       private bool[] buttons;
+       private int16[] axes;
+
+       public RetroGamepad (Gamepad gamepad) {
+               Object (gamepad: gamepad);
+       }
+
+       construct {
+               buttons = new bool[StandardGamepadButton.HOME + 1];
+               axes = new int16[4];
+
+               gamepad.button_event.connect ((button, value) => buttons[button] = value);
+               gamepad.axis_event.connect ((axis, value) => axes[axis] = (int16) (value * int16.MAX));
+       }
+
+       public void poll () {}
+
+       public int16 get_input_state (Retro.DeviceType device, uint index, uint id) {
+               switch (device) {
+               case Retro.DeviceType.JOYPAD:
+                       return get_button_pressed ((Retro.JoypadId) id) ? int16.MAX : 0;
+               case Retro.DeviceType.ANALOG:
+                       return get_analog_value ((Retro.AnalogIndex) index, (Retro.AnalogId) id);
+               default:
+                       return 0;
+               }
+       }
+
+       public Retro.DeviceType get_device_type () {
+               return Retro.DeviceType.ANALOG;
+       }
+
+       public uint64 get_device_capabilities () {
+               return (1 << Retro.DeviceType.JOYPAD) | (1 << Retro.DeviceType.ANALOG);
+       }
+
+       public bool get_button_pressed (Retro.JoypadId button) {
+               switch (button) {
+               case Retro.JoypadId.B:
+                       return buttons[StandardGamepadButton.A];
+               case Retro.JoypadId.Y:
+                       return buttons[StandardGamepadButton.X];
+               case Retro.JoypadId.SELECT:
+                       return buttons[StandardGamepadButton.SELECT];
+               case Retro.JoypadId.START:
+                       return buttons[StandardGamepadButton.START];
+               case Retro.JoypadId.UP:
+                       return buttons[StandardGamepadButton.DPAD_UP];
+               case Retro.JoypadId.DOWN:
+                       return buttons[StandardGamepadButton.DPAD_DOWN];
+               case Retro.JoypadId.LEFT:
+                       return buttons[StandardGamepadButton.DPAD_LEFT];
+               case Retro.JoypadId.RIGHT:
+                       return buttons[StandardGamepadButton.DPAD_RIGHT];
+               case Retro.JoypadId.A:
+                       return buttons[StandardGamepadButton.B];
+               case Retro.JoypadId.X:
+                       return buttons[StandardGamepadButton.Y];
+               case Retro.JoypadId.L:
+                       return buttons[StandardGamepadButton.SHOULDER_L];
+               case Retro.JoypadId.R:
+                       return buttons[StandardGamepadButton.SHOULDER_R];
+               case Retro.JoypadId.L2:
+                       return buttons[StandardGamepadButton.TRIGGER_L];
+               case Retro.JoypadId.R2:
+                       return buttons[StandardGamepadButton.TRIGGER_R];
+               case Retro.JoypadId.L3:
+                       return buttons[StandardGamepadButton.STICK_L];
+               case Retro.JoypadId.R3:
+                       return buttons[StandardGamepadButton.STICK_R];
+               default:
+                       return false;
+               }
+       }
+
+       public int16 get_analog_value (Retro.AnalogIndex index, Retro.AnalogId id) {
+               switch (index) {
+               case Retro.AnalogIndex.LEFT:
+                       switch (id) {
+                       case Retro.AnalogId.X:
+                               return axes[StandardGamepadAxis.LEFT_X];
+                       case Retro.AnalogId.Y:
+                               return axes[StandardGamepadAxis.LEFT_Y];
+                       default:
+                               return 0;
+                       }
+               case Retro.AnalogIndex.RIGHT:
+                       switch (id) {
+                       case Retro.AnalogId.X:
+                               return axes[StandardGamepadAxis.RIGHT_X];
+                       case Retro.AnalogId.Y:
+                               return axes[StandardGamepadAxis.RIGHT_Y];
+                       default:
+                               return 0;
+                       }
+               default:
+                       return 0;
+               }
+       }
+}


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