[gnome-games] retro: Add RetroInputManager
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] retro: Add RetroInputManager
- Date: Mon, 18 Jul 2016 18:54:06 +0000 (UTC)
commit 7f0e96413a0c0e8407173b0d99f4dce2626a222d
Author: Megh Parikh <meghprkh gmail com>
Date: Mon Jul 18 22:50:05 2016 +0530
retro: Add RetroInputManager
This class manages all input devices and binds them to a libretro
InputDeviceManager and will be used by RetroRunner.
This is part of a series of commits to add gamepad support.
src/Makefile.am | 1 +
src/retro/retro-input-manager.vala | 69 ++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index d06af17..c49b199 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -74,6 +74,7 @@ gnome_games_SOURCES = \
\
retro/retro-error.vala \
retro/retro-gamepad.vala \
+ retro/retro-input-manager.vala \
retro/retro-log.vala \
retro/retro-runner.vala \
\
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
new file mode 100644
index 0000000..79b8bae
--- /dev/null
+++ b/src/retro/retro-input-manager.vala
@@ -0,0 +1,69 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private class Games.RetroInputManager : Object {
+ public RetroGtk.InputDeviceManager input { get; private set; }
+
+ private RetroGtk.VirtualGamepad keyboard;
+ private GamepadMonitor gamepad_monitor;
+ private bool[] is_port_plugged;
+ private int keyboard_port;
+
+ public RetroInputManager (Gtk.Widget widget) {
+ input = new RetroGtk.InputDeviceManager ();
+ keyboard = new RetroGtk.VirtualGamepad (widget);
+ gamepad_monitor = GamepadMonitor.get_instance ();
+
+ input.set_keyboard (new RetroGtk.Keyboard (widget));
+
+ gamepad_monitor.foreach_gamepad ((gamepad) => {
+ var port = is_port_plugged.length;
+ is_port_plugged += true;
+ input.set_controller_device (port, new RetroGamepad (gamepad));
+ gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
+ });
+
+ keyboard_port = is_port_plugged.length;
+ is_port_plugged += true;
+ input.set_controller_device (keyboard_port, keyboard);
+ gamepad_monitor.gamepad_plugged.connect (handle_gamepad_plugged);
+ }
+
+ 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;
+ gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
+ input.set_controller_device (keyboard_port, new RetroGamepad (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]) {
+ // Found an unplugged port and so assigning keyboard to it
+ keyboard_port = i;
+ is_port_plugged[keyboard_port] = true;
+ input.set_controller_device (keyboard_port, keyboard);
+
+ return;
+ }
+ }
+
+ // Now it means that there is no unplugged port so append keyboard to ports
+ keyboard_port = is_port_plugged.length;
+ is_port_plugged += true;
+ input.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;
+ input.remove_controller_device (keyboard_port);
+ keyboard_port = port;
+ input.set_controller_device (keyboard_port, keyboard);
+ }
+ else {
+ // Just remove the controller as no need to shift keyboard
+ is_port_plugged[port] = false;
+ input.remove_controller_device (port);
+ }
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]