[gnome-games] gamepad: Add GamepadMonitor



commit 443222f4458534dcc2af30eaa94aafea1e11becf
Author: Megh Parikh <meghprkh gmail com>
Date:   Mon Jul 18 22:45:51 2016 +0530

    gamepad: Add GamepadMonitor
    
    This class gives us access to the plugged in Gamepads and also notifies us
    on gamepads being plugged.
    
    This is part of a series of commits to add gamepad support.

 src/Makefile.am                  |    1 +
 src/gamepad/gamepad-monitor.vala |   66 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6b6d202..36dd2b9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -61,6 +61,7 @@ gnome_games_SOURCES = \
        gamepad/gamepad-mapping.vala \
        gamepad/gamepad-mapping-error.vala \
        gamepad/gamepad-mappings-manager.vala \
+       gamepad/gamepad-monitor.vala \
        gamepad/linux/linux-raw-gamepad.vala \
        gamepad/linux/linux-raw-gamepad-monitor.vala \
        gamepad/raw-gamepad.vala \
diff --git a/src/gamepad/gamepad-monitor.vala b/src/gamepad/gamepad-monitor.vala
new file mode 100644
index 0000000..1016bc9
--- /dev/null
+++ b/src/gamepad/gamepad-monitor.vala
@@ -0,0 +1,66 @@
+// This file is part of GNOME Games. License: GPLv3
+
+/**
+ * This class provides a way to the client to monitor gamepads
+ *
+ * The client interfaces with this class primarily
+ */
+private class Games.GamepadMonitor : Object {
+       /**
+        * Emitted when a gamepad is plugged in.
+        * This would be emitted once even if RawGamepadMonitor emits it multiple
+        * times
+        * @param  gamepad    The gamepad
+        */
+       public signal void gamepad_plugged (Gamepad gamepad);
+
+       private static GamepadMonitor instance;
+
+       private GenericSet<Gamepad?> gamepads;
+
+       private GamepadMonitor () {
+               gamepads = new GenericSet<Gamepad?> (direct_hash, direct_equal);
+               var raw_gamepad_monitor = LinuxRawGamepadMonitor.get_instance ();
+               raw_gamepad_monitor.gamepad_plugged.connect (on_raw_gamepad_plugged);
+               raw_gamepad_monitor.foreach_gamepad ((raw_gamepad) => add_gamepad (raw_gamepad));
+       }
+
+       public static GamepadMonitor get_instance () {
+               if (instance == null)
+                       instance = new GamepadMonitor ();
+
+               return instance;
+       }
+
+       public void foreach_gamepad (GamepadCallback callback) {
+               foreach (var gamepad in gamepads)
+                       callback (gamepad);
+       }
+
+       private Gamepad? add_gamepad (RawGamepad raw_gamepad) {
+               Gamepad gamepad;
+               try {
+                       gamepad = new Gamepad (raw_gamepad);
+               }
+               catch (GamepadMappingError e) {
+                       return null;
+               }
+
+               gamepads.add (gamepad);
+               gamepad.unplugged.connect (remove_gamepad);
+
+               return gamepad;
+       }
+
+       private void on_raw_gamepad_plugged (RawGamepad raw_gamepad) {
+               var gamepad = add_gamepad (raw_gamepad);
+
+               // To emit only once even if RawGamepadMonitor emits it multiple times
+               if (gamepad != null)
+                       gamepad_plugged (gamepad);
+       }
+
+       private void remove_gamepad (Gamepad gamepad) {
+               gamepads.remove (gamepad);
+       }
+}


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