[gnome-games/wip/abhinavsingh/keyboard-config: 2/6] keyboard: Add KeyboardMappingManager
- From: Abhinav Singh <abhinavsingh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/abhinavsingh/keyboard-config: 2/6] keyboard: Add KeyboardMappingManager
- Date: Sun, 27 Aug 2017 23:12:46 +0000 (UTC)
commit 16b15a73d254dd5ce04f9a7903c5ce66ad67c2e8
Author: theawless <theawless gmail com>
Date: Mon Aug 28 04:31:50 2017 +0530
keyboard: Add KeyboardMappingManager
This will be used to load and save user keyboard mappings.
This commit also updates RetroInputManager to fetch keyboard mappings
from Manager and not use the default one.
src/Makefile.am | 1 +
src/keyboard/keyboard-mapping-manager.vala | 86 ++++++++++++++++++++++++++++
src/retro/retro-input-manager.vala | 14 ++++-
3 files changed, 100 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6f57854..ee14493 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -119,6 +119,7 @@ gnome_games_SOURCES = \
grilo/grilo-media.vala \
\
keyboard/keyboard-mapping-builder.vala \
+ keyboard/keyboard-mapping-manager.vala \
\
retro/retro-core-source.vala \
retro/retro-error.vala \
diff --git a/src/keyboard/keyboard-mapping-manager.vala b/src/keyboard/keyboard-mapping-manager.vala
new file mode 100644
index 0000000..a690223
--- /dev/null
+++ b/src/keyboard/keyboard-mapping-manager.vala
@@ -0,0 +1,86 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.KeyboardMappingManager : Object {
+ private const string MAPPING_FILE_NAME = "keyboard-mapping.txt";
+ private const string GROUP_NAME = "KeyboardMapping";
+
+ private Retro.GamepadConfiguration _mapping;
+ public Retro.GamepadConfiguration mapping {
+ set {
+ is_default_mapping = false;
+ _mapping = value;
+ }
+ get { return _mapping; }
+ }
+
+ private KeyFile mapping_key_file;
+ private File mapping_file;
+ public bool is_default_mapping { private set; get; }
+ private static KeyboardMappingManager? instance;
+
+ construct {
+ var config_dir = Application.get_config_dir ();
+ var path = Path.build_filename (config_dir, MAPPING_FILE_NAME);
+
+ mapping_file = File.new_for_path (path);
+ mapping_key_file = new KeyFile ();
+ }
+
+ private KeyboardMappingManager () {
+ reset_mapping ();
+ load_mapping ();
+ }
+
+ public static KeyboardMappingManager get_instance () {
+ if (instance == null)
+ instance = new KeyboardMappingManager ();
+
+ return instance;
+ }
+
+ public void reset_mapping () {
+ mapping = new Retro.GamepadConfiguration ();
+ mapping.set_to_default ();
+ is_default_mapping = true;
+ }
+
+ public void save_mapping () {
+ if (!mapping_file.query_exists ())
+ mapping_file.create (FileCreateFlags.NONE);
+
+ var enumc = (EnumClass) typeof (Retro.JoypadId).class_ref ();
+ foreach (var enumv in enumc.values) {
+ var button = enumv.value_nick;
+ var key = mapping.get_button_key ((Retro.JoypadId) enumv.value);
+ mapping_key_file.set_integer (GROUP_NAME, button, key);
+ }
+
+ mapping_key_file.save_to_file (mapping_file.get_path ());
+ }
+
+ private void load_mapping () {
+ if (!mapping_file.query_exists ()) {
+ debug ("User keyboard mapping file doesn't exist.");
+
+ return;
+ }
+
+ mapping_key_file.load_from_file (mapping_file.get_path (), KeyFileFlags.NONE);
+ mapping = new Retro.GamepadConfiguration ();
+ is_default_mapping = false;
+
+ var enumc = (EnumClass) typeof (Retro.JoypadId).class_ref ();
+ foreach (var enumv in enumc.values) {
+ var button = enumv.value_nick;
+ var key = mapping_key_file.get_integer (GROUP_NAME, button);
+ mapping.set_button_key ((Retro.JoypadId) enumv.value, (uint16) key);
+ }
+ }
+
+ public void delete_mapping () {
+ if (!mapping_file.query_exists ())
+ return;
+
+ mapping_file.delete ();
+ }
+}
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
index 26fdc52..c56d404 100644
--- a/src/retro/retro-input-manager.vala
+++ b/src/retro/retro-input-manager.vala
@@ -1,16 +1,28 @@
// This file is part of GNOME Games. License: GPL-3.0+.
private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
+ private Gtk.Widget widget;
private Retro.VirtualGamepad keyboard;
private GamepadMonitor gamepad_monitor;
+ private KeyboardMappingManager keyboard_mapping_manager;
private Retro.InputDevice?[] input_devices;
private int keyboard_port;
private bool present_analog_sticks;
+ construct {
+ keyboard_mapping_manager = KeyboardMappingManager.get_instance ();
+ keyboard_mapping_manager.notify["mapping"].connect (() => {
+ keyboard = new Retro.VirtualGamepad.with_configuration (widget,
keyboard_mapping_manager.mapping);
+ set_keyboard (widget);
+ set_controller_device (keyboard_port, keyboard);
+ });
+ }
+
public RetroInputManager (Gtk.Widget widget, bool present_analog_sticks) {
this.present_analog_sticks = present_analog_sticks;
+ this.widget = widget;
- keyboard = new Retro.VirtualGamepad (widget);
+ keyboard = new Retro.VirtualGamepad.with_configuration (widget,
keyboard_mapping_manager.mapping);
set_keyboard (widget);
gamepad_monitor = GamepadMonitor.get_instance ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]