[gnome-games] retro: Refactor input manager



commit bd652b70e88dc30c2c052c234d90347b625a8d63
Author: theawless <theawless gmail com>
Date:   Thu Apr 6 15:45:33 2017 +0530

    retro: Refactor input manager
    
    Replace the use of is_port_plugged array and gamepads array with
    input_devices array.
    
    Saving the plugged state of each gamepad is redundant and by using the
    base class Retro.InputDevice we can handle gamepads and keyboard
    together to avoid redundancy and errors.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780920

 src/retro/retro-input-manager.vala |   47 ++++++++++++++++--------------------
 1 files changed, 21 insertions(+), 26 deletions(-)
---
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
index 0a22eb9..1eb8561 100644
--- a/src/retro/retro-input-manager.vala
+++ b/src/retro/retro-input-manager.vala
@@ -3,8 +3,7 @@
 private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
        private Retro.VirtualGamepad keyboard;
        private GamepadMonitor gamepad_monitor;
-       private bool[] is_port_plugged;
-       private Gamepad?[] gamepads;
+       private Retro.InputDevice?[] input_devices;
        private int keyboard_port;
        private bool present_analog_sticks;
 
@@ -12,21 +11,19 @@ private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
                this.present_analog_sticks = present_analog_sticks;
 
                keyboard = new Retro.VirtualGamepad (widget);
-               gamepad_monitor = GamepadMonitor.get_instance ();
-
                set_keyboard (new Retro.Keyboard (widget));
 
+               gamepad_monitor = GamepadMonitor.get_instance ();
                gamepad_monitor.foreach_gamepad ((gamepad) => {
-                       var port = is_port_plugged.length;
-                       is_port_plugged += true;
-                       gamepads += gamepad;
-                       set_controller_device (port, new RetroGamepad (gamepad, present_analog_sticks));
+                       var port = input_devices.length;
+                       var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
+                       input_devices += retro_gamepad;
+                       set_controller_device (port, retro_gamepad);
                        gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
                });
 
-               keyboard_port = is_port_plugged.length;
-               is_port_plugged += true;
-               gamepads += null;
+               keyboard_port = input_devices.length;
+               input_devices += keyboard;
                set_controller_device (keyboard_port, keyboard);
                gamepad_monitor.gamepad_plugged.connect (handle_gamepad_plugged);
        }
@@ -34,17 +31,17 @@ private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
        private void handle_gamepad_plugged (Gamepad gamepad) {
                // Plug this gamepad to the port where the keyboard was plugged as a last resort
                var port = keyboard_port;
+               var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
+               input_devices[port] = retro_gamepad;
+               set_controller_device (port, retro_gamepad);
                gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
-               set_controller_device (keyboard_port, new RetroGamepad (gamepad, present_analog_sticks));
-               gamepads[port] = gamepad;
 
                // Assign keyboard to another unplugged port if exists and return
-               for (var i = keyboard_port; i < is_port_plugged.length; i++) {
-                       if (!is_port_plugged[i]) {
+               for (var i = keyboard_port; i < input_devices.length; i++) {
+                       if (input_devices[i] == null) {
                                // Found an unplugged port and so assigning keyboard to it
                                keyboard_port = i;
-                               is_port_plugged[keyboard_port] = true;
-                               gamepads[keyboard_port] = null;
+                               input_devices[keyboard_port] = keyboard;
                                set_controller_device (keyboard_port, keyboard);
 
                                return;
@@ -52,34 +49,32 @@ private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
                }
 
                // Now it means that there is no unplugged port so append keyboard to ports
-               keyboard_port = is_port_plugged.length;
-               is_port_plugged += true;
+               keyboard_port = input_devices.length;
+               input_devices += keyboard;
                set_controller_device (keyboard_port, keyboard);
        }
 
        private void handle_gamepad_unplugged (int port) {
                if (keyboard_port > port) {
                        // Remove the controller and shift keyboard to "lesser" port
-                       is_port_plugged[keyboard_port] = false;
-                       gamepads[keyboard_port] = null;
+                       input_devices[keyboard_port] = null;
                        remove_controller_device (keyboard_port);
                        keyboard_port = port;
-                       gamepads[keyboard_port] = null;
+                       input_devices[keyboard_port] = keyboard;
                        set_controller_device (keyboard_port, keyboard);
                }
                else {
                        // Just remove the controller as no need to shift keyboard
-                       is_port_plugged[port] = false;
-                       gamepads[port] = null;
+                       input_devices[port] = null;
                        remove_controller_device (port);
                }
        }
 
        private bool set_rumble_state (uint port, Retro.RumbleEffect effect, uint16 strength) {
-               if (port > gamepads.length)
+               if (port > input_devices.length)
                        return false;
 
-               if (gamepads[port] == null)
+               if (input_devices[port] == null || input_devices[port] == keyboard)
                        return false;
 
                // TODO Transmit the rumble signal to the gamepad.


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