[gnome-games/wip/aplazas/libmanette: 4/15] retro: Make RetroGamepad use libmanette
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/aplazas/libmanette: 4/15] retro: Make RetroGamepad use libmanette
- Date: Wed, 8 Nov 2017 20:25:25 +0000 (UTC)
commit bf139ade946f020b66c811cc8020981de5ce4b2b
Author: Adrien Plazas <kekun plazas laposte net>
Date: Sun Nov 5 16:48:37 2017 +0100
retro: Make RetroGamepad use libmanette
src/retro/retro-gamepad.vala | 34 ++++++++++++++++++++++++++++------
src/retro/retro-input-manager.vala | 34 ++++++++++++++++++----------------
2 files changed, 46 insertions(+), 22 deletions(-)
---
diff --git a/src/retro/retro-gamepad.vala b/src/retro/retro-gamepad.vala
index 402e10b..abd7b88 100644
--- a/src/retro/retro-gamepad.vala
+++ b/src/retro/retro-gamepad.vala
@@ -1,23 +1,23 @@
// This file is part of GNOME Games. License: GPL-3.0+.
private class Games.RetroGamepad: Object, Retro.Controller {
- public Gamepad gamepad { get; construct; }
+ public Manette.Device device { get; construct; }
public bool present_analog_sticks { get; construct; }
private bool[] buttons;
private int16[] axes;
- public RetroGamepad (Gamepad gamepad, bool present_analog_sticks) {
- Object (gamepad: gamepad, present_analog_sticks: present_analog_sticks);
+ public RetroGamepad (Manette.Device device, bool present_analog_sticks) {
+ Object (device: device, present_analog_sticks: present_analog_sticks);
}
construct {
buttons = new bool[EventCode.KEY_MAX + 1];
axes = new int16[EventCode.ABS_MAX + 1];
- gamepad.button_press_event.connect ((event) => buttons[event.gamepad_button.button] = true);
- gamepad.button_release_event.connect ((event) => buttons[event.gamepad_button.button] =
false);
- gamepad.axis_event.connect ((event) => axes[event.gamepad_axis.axis] = (int16)
(event.gamepad_axis.value * int16.MAX));
+ device.button_press_event.connect (on_button_press_event);
+ device.button_release_event.connect (on_button_release_event);
+ device.absolute_axis_event.connect (on_absolute_axis_event);
}
public void poll () {}
@@ -116,4 +116,26 @@ private class Games.RetroGamepad: Object, Retro.Controller {
return 0;
}
}
+
+ private void on_button_press_event (Manette.Event event) {
+ uint16 button;
+
+ if (event.get_button (out button))
+ buttons[button] = true;
+ }
+
+ private void on_button_release_event (Manette.Event event) {
+ uint16 button;
+
+ if (event.get_button (out button))
+ buttons[button] = false;
+ }
+
+ private void on_absolute_axis_event (Manette.Event event) {
+ uint16 axis;
+ double value;
+
+ if (event.get_absolute (out axis, out value))
+ axes[axis] = (int16) (value * int16.MAX);
+ }
}
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
index 74258be..dad6bfb 100644
--- a/src/retro/retro-input-manager.vala
+++ b/src/retro/retro-input-manager.vala
@@ -3,7 +3,7 @@
private class Games.RetroInputManager : Object {
private Retro.Core core;
private Retro.Controller core_view_joypad;
- private GamepadMonitor gamepad_monitor;
+ private Manette.Monitor monitor;
private Retro.Controller?[] controllers;
private int core_view_joypad_port;
private bool present_analog_sticks;
@@ -22,35 +22,37 @@ private class Games.RetroInputManager : Object {
core.set_default_controller (default_mouse);
core.set_default_controller (default_pointer);
- gamepad_monitor = GamepadMonitor.get_instance ();
- gamepad_monitor.foreach_gamepad ((gamepad) => {
+ monitor = new Manette.Monitor ();
+ var iterator = monitor.iterate ();
+ Manette.Device device = null;
+ while (iterator.next (out device)) {
var port = controllers.length;
- var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
+ var retro_gamepad = new RetroGamepad (device, present_analog_sticks);
controllers += retro_gamepad;
core.set_controller (port, retro_gamepad);
- gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
- });
+ device.disconnected.connect (() => on_device_disconnected (port));
+ };
core_view_joypad_port = controllers.length;
controllers += core_view_joypad;
core.set_controller (core_view_joypad_port, core_view_joypad);
- gamepad_monitor.gamepad_plugged.connect (handle_gamepad_plugged);
+ monitor.device_connected.connect (on_device_connected);
}
- private void handle_gamepad_plugged (Gamepad gamepad) {
- // Plug this gamepad to the port where the CoreView's joypad was
- // plugged as a last resort.
+ private void on_device_connected (Manette.Device device) {
+ // Plug this device to the port where the CoreView's joypad was
+ // connected as a last resort.
var port = core_view_joypad_port;
- var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
+ var retro_gamepad = new RetroGamepad (device, present_analog_sticks);
controllers[port] = retro_gamepad;
core.set_controller (port, retro_gamepad);
- gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
+ device.disconnected.connect (() => on_device_disconnected (port));
- // Assign the CoreView's joypad to another unplugged port if it
+ // Assign the CoreView's joypad to another disconnected port if it
// exists and return.
for (var i = core_view_joypad_port; i < controllers.length; i++) {
if (controllers[i] == null) {
- // Found an unplugged port and so assigning core_view_joypad to it
+ // Found an disconnected port and so assigning core_view_joypad to it
core_view_joypad_port = i;
controllers[core_view_joypad_port] = core_view_joypad;
core.set_controller (core_view_joypad_port, core_view_joypad);
@@ -59,14 +61,14 @@ private class Games.RetroInputManager : Object {
}
}
- // Now it means that there is no unplugged port so append the
+ // Now it means that there is no disconnected port so append the
// CoreView's joypad to ports.
core_view_joypad_port = controllers.length;
controllers += core_view_joypad;
core.set_controller (core_view_joypad_port, core_view_joypad);
}
- private void handle_gamepad_unplugged (int port) {
+ private void on_device_disconnected (int port) {
if (core_view_joypad_port > port) {
// Remove the controller and shift the CoreView's joypad to
// "lesser" port.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]