[gnome-games/wip/abhinavsingh/gamepad-reassign: 5/9] retro: Use ControllerSet in RetroInputManager



commit d0de5ee2c9a60fd81c76c75e963f233155f62489
Author: theawless <theawless gmail com>
Date:   Sat Aug 12 00:09:59 2017 +0530

    retro: Use ControllerSet in RetroInputManager
    
    This allows to setup retro input device manager with predefined ports.
    Also simplifies the plugging in and out of controllers.

 src/retro/retro-input-manager.vala |   96 +++++++++++++++++-------------------
 1 files changed, 45 insertions(+), 51 deletions(-)
---
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
index 26fdc52..abcd337 100644
--- a/src/retro/retro-input-manager.vala
+++ b/src/retro/retro-input-manager.vala
@@ -1,80 +1,74 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
+       public ControllerSet controller_set { set; get; }
+
        private Retro.VirtualGamepad keyboard;
        private GamepadMonitor gamepad_monitor;
-       private Retro.InputDevice?[] input_devices;
-       private int keyboard_port;
        private bool present_analog_sticks;
 
+       construct {
+               controller_set = new ControllerSet ();
+               controller_set.reset.connect (reset);
+
+               gamepad_monitor = GamepadMonitor.get_instance ();
+               gamepad_monitor.gamepad_plugged.connect (add_gamepad);
+       }
+
        public RetroInputManager (Gtk.Widget widget, bool present_analog_sticks) {
                this.present_analog_sticks = present_analog_sticks;
 
+               // Assumption: keyboard always exists.
                keyboard = new Retro.VirtualGamepad (widget);
                set_keyboard (widget);
+               controller_set.keyboard_port = 0;
+               set_controller_device (controller_set.keyboard_port, keyboard);
 
-               gamepad_monitor = GamepadMonitor.get_instance ();
-               gamepad_monitor.foreach_gamepad ((gamepad) => {
-                       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));
+               gamepad_monitor.foreach_gamepad (add_gamepad);
+       }
+
+       private void reset () {
+               foreach_controller ((port, controller) => {
+                       remove_controller_device (port);
                });
 
-               keyboard_port = input_devices.length;
-               input_devices += keyboard;
-               set_controller_device (keyboard_port, keyboard);
-               gamepad_monitor.gamepad_plugged.connect (handle_gamepad_plugged);
+               if (controller_set.has_gamepads) {
+                       controller_set.gamepads.foreach ((port, gamepad) => {
+                               var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
+                               set_controller_device (port, retro_gamepad);
+                       });
+               }
+               if (controller_set.has_keyboard)
+                       set_controller_device (controller_set.keyboard_port, keyboard);
        }
 
-       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;
+       private void add_gamepad (Gamepad gamepad) {
+               // Plug this gamepad to the port where the keyboard was plugged
+               var port = controller_set.keyboard_port;
+               controller_set.add_gamepad (port, gamepad);
                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));
-
-               // Assign keyboard to another unplugged port if exists and return
-               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;
-                               input_devices[keyboard_port] = keyboard;
-                               set_controller_device (keyboard_port, keyboard);
-
-                               return;
-                       }
-               }
+               gamepad.unplugged.connect (() => remove_gamepad (port));
 
-               // Now it means that there is no unplugged port so append keyboard to ports
-               keyboard_port = input_devices.length;
-               input_devices += keyboard;
-               set_controller_device (keyboard_port, keyboard);
+               // Assign keyboard to first unplugged port
+               controller_set.keyboard_port = controller_set.first_unplugged_port;
+               set_controller_device (controller_set.keyboard_port, keyboard);
        }
 
-       private void handle_gamepad_unplugged (int port) {
-               if (keyboard_port > port) {
-                       // Remove the controller and shift keyboard to "lesser" port
-                       input_devices[keyboard_port] = null;
-                       remove_controller_device (keyboard_port);
-                       keyboard_port = port;
-                       input_devices[keyboard_port] = keyboard;
-                       set_controller_device (keyboard_port, keyboard);
-               }
-               else {
-                       // Just remove the controller as no need to shift keyboard
-                       input_devices[port] = null;
-                       remove_controller_device (port);
+       private void remove_gamepad (uint port) {
+               controller_set.remove_gamepad (port);
+               remove_controller_device (port);
+
+               if (controller_set.has_keyboard && controller_set.keyboard_port > port) {
+                       // Shift keyboard to lesser port
+                       remove_controller_device (controller_set.keyboard_port);
+                       controller_set.keyboard_port = port;
+                       set_controller_device (controller_set.keyboard_port, keyboard);
                }
        }
 
        private bool set_rumble_state (uint port, Retro.RumbleEffect effect, uint16 strength) {
-               if (port > input_devices.length)
-                       return false;
-
-               if (input_devices[port] == null || input_devices[port] == keyboard)
+               if (controller_set.gamepads.contains (port))
                        return false;
 
                // TODO Transmit the rumble signal to the gamepad.


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