[gnome-games] ui: Port gamepad configuration to libmanette



commit 3b77c6b1ff9e486afb47a4630d7b7d0072b74554
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Mon Nov 6 18:07:13 2017 +0100

    ui: Port gamepad configuration to libmanette

 src/ui/gamepad-configurer.vala           |   45 +++-----------
 src/ui/gamepad-mapper.vala               |   93 ++++++++++++++----------------
 src/ui/gamepad-tester.vala               |   57 ++++++++----------
 src/ui/preferences-page-controllers.vala |   32 ++++++----
 4 files changed, 98 insertions(+), 129 deletions(-)
---
diff --git a/src/ui/gamepad-configurer.vala b/src/ui/gamepad-configurer.vala
index a46e0aa..377c464 100644
--- a/src/ui/gamepad-configurer.vala
+++ b/src/ui/gamepad-configurer.vala
@@ -69,14 +69,13 @@ private class Games.GamepadConfigurer : Gtk.Box {
 
                        switch (value) {
                        case State.TEST:
-                               var user_mapping = mappings_manager.get_user_mapping (gamepad.guid);
-                               reset_button.set_sensitive (user_mapping != null);
+                               reset_button.set_sensitive (device.has_user_mapping ());
 
                                back_button.show ();
                                cancel_button.hide ();
                                action_bar.show ();
                                /* translators: testing a gamepad, %s is its name */
-                               header_bar.title = _("Testing %s").printf (gamepad.name);
+                               header_bar.title = _("Testing %s").printf (device.get_name ());
                                header_bar.get_style_context ().remove_class ("selection-mode");
                                stack.set_visible_child_name ("gamepad_tester");
 
@@ -90,7 +89,7 @@ private class Games.GamepadConfigurer : Gtk.Box {
                                cancel_button.show ();
                                action_bar.hide ();
                                /* translators: configuring a gamepad, %s is its name */
-                               header_bar.title = _("Configuring %s").printf (gamepad.name);
+                               header_bar.title = _("Configuring %s").printf (device.get_name ());
                                header_bar.get_style_context ().add_class ("selection-mode");
                                stack.set_visible_child_name ("gamepad_mapper");
 
@@ -128,20 +127,15 @@ private class Games.GamepadConfigurer : Gtk.Box {
        [GtkChild]
        private Gtk.Button cancel_button;
 
-       private Gamepad gamepad;
+       private Manette.Device device;
        private GamepadMapper mapper;
        private GamepadTester tester;
-       private GamepadMappingsManager mappings_manager;
 
-       construct {
-               mappings_manager = GamepadMappingsManager.get_instance ();
-       }
-
-       public GamepadConfigurer (Gamepad gamepad) {
-               this.gamepad = gamepad;
-               mapper = new GamepadMapper (gamepad, STANDARD_GAMEPAD_VIEW_CONFIGURATION, 
STANDARD_GAMEPAD_INPUTS);
+       public GamepadConfigurer (Manette.Device device) {
+               this.device = device;
+               mapper = new GamepadMapper (device, STANDARD_GAMEPAD_VIEW_CONFIGURATION, 
STANDARD_GAMEPAD_INPUTS);
                gamepad_mapper_holder.pack_start (mapper);
-               tester = new GamepadTester (gamepad, STANDARD_GAMEPAD_VIEW_CONFIGURATION);
+               tester = new GamepadTester (device, STANDARD_GAMEPAD_VIEW_CONFIGURATION);
                gamepad_tester_holder.pack_start (tester);
 
                state = State.TEST;
@@ -173,9 +167,7 @@ private class Games.GamepadConfigurer : Gtk.Box {
                message_dialog.response.connect ((response) => {
                        switch (response) {
                                case Gtk.ResponseType.ACCEPT:
-                                       mappings_manager.delete_mapping (gamepad.guid);
-                                       var sdl_string = mappings_manager.get_default_mapping (gamepad.guid);
-                                       set_gamepad_mapping (sdl_string);
+                                       device.remove_user_mapping ();
                                        reset_button.set_sensitive (false);
 
                                        break;
@@ -189,24 +181,7 @@ private class Games.GamepadConfigurer : Gtk.Box {
        }
 
        private void on_mapper_finished (string sdl_string) {
-               mappings_manager.save_mapping (gamepad.guid, gamepad.name, sdl_string);
-               set_gamepad_mapping (sdl_string);
-
+               device.save_user_mapping (sdl_string);
                state = State.TEST;
        }
-
-       private void set_gamepad_mapping (string? sdl_string) {
-               if (sdl_string == null) {
-                       gamepad.set_mapping (null);
-
-                       return;
-               }
-               try {
-                       var mapping = new GamepadMapping.from_sdl_string (sdl_string);
-                       gamepad.set_mapping (mapping);
-               }
-               catch (Error e) {
-                       critical (e.message);
-               }
-       }
 }
diff --git a/src/ui/gamepad-mapper.vala b/src/ui/gamepad-mapper.vala
index 38b3a0d..ab15db9 100644
--- a/src/ui/gamepad-mapper.vala
+++ b/src/ui/gamepad-mapper.vala
@@ -9,7 +9,7 @@ private class Games.GamepadMapper : Gtk.Box {
        [GtkChild]
        private Gtk.Label info_message;
 
-       private Gamepad gamepad;
+       private Manette.Device device;
        private GamepadMappingBuilder mapping_builder;
        private GamepadInput[] mapping_inputs;
        private GamepadInput input;
@@ -17,8 +17,8 @@ private class Games.GamepadMapper : Gtk.Box {
 
        private ulong gamepad_event_handler_id;
 
-       public GamepadMapper (Gamepad gamepad, GamepadViewConfiguration configuration, GamepadInput[] 
mapping_inputs) {
-               this.gamepad = gamepad;
+       public GamepadMapper (Manette.Device device, GamepadViewConfiguration configuration, GamepadInput[] 
mapping_inputs) {
+               this.device = device;
                this.mapping_inputs = mapping_inputs;
                try {
                        gamepad_view.set_configuration (configuration);
@@ -46,69 +46,64 @@ private class Games.GamepadMapper : Gtk.Box {
        }
 
        private void connect_to_gamepad () {
-               gamepad_event_handler_id = gamepad.event.connect ((event) => {
-                       switch (event.type) {
-                       case EventType.EVENT_GAMEPAD_BUTTON_RELEASE:
-                               on_button_event (event.gamepad_button);
-
-                               break;
-                       case EventType.EVENT_GAMEPAD_AXIS:
-                               on_axis_event (event.gamepad_axis);
-
-                               break;
-                       case EventType.EVENT_GAMEPAD_HAT:
-                               on_hat_event (event.gamepad_hat);
-
-                               break;
-                       default:
-                               break;
-                       }
-               });
+               gamepad_event_handler_id = device.event.connect (on_event);
        }
 
        private void disconnect_from_gamepad () {
                if (gamepad_event_handler_id != 0) {
-                       gamepad.disconnect (gamepad_event_handler_id);
+                       device.disconnect (gamepad_event_handler_id);
                        gamepad_event_handler_id = 0;
                }
        }
 
-       private void on_button_event (EventGamepadButton event) {
-               if (input.type == EventCode.EV_ABS)
-                       return;
+       private void on_event (Manette.Event event) {
+               switch (event.get_event_type ()) {
+               case Manette.EventType.EVENT_BUTTON_RELEASE:
+                       if (input.type == EventCode.EV_ABS)
+                               return;
 
-               var success = mapping_builder.set_button_mapping (event.gamepad_button.hardware_index,
-                                                                 input);
-               if (!success)
-                       return;
+                       if (!mapping_builder.set_button_mapping ((uint8) event.get_hardware_index (),
+                                                                    input))
+                               return;
 
-               next_input ();
-       }
+                       break;
+               case Manette.EventType.EVENT_ABSOLUTE:
+                       uint16 axis;
+                       double value;
 
-       private void on_axis_event (EventGamepadAxis event) {
-               if (input.type == EventCode.EV_KEY)
-                       return;
+                       if (input.type == EventCode.EV_KEY)
+                               return;
 
-               if (-0.8 < event.gamepad_axis.value < 0.8)
-                       return;
+                       if (!event.get_absolute (out axis, out value))
+                               return;
 
-               var success = mapping_builder.set_axis_mapping (event.gamepad_axis.hardware_index,
-                                                               input);
-               if (!success)
-                       return;
+                       if (-0.8 < value < 0.8)
+                               return;
 
-               next_input ();
-       }
+                       if (!mapping_builder.set_axis_mapping ((uint8) event.get_hardware_index (),
+                                                              input))
+                               return;
 
-       private void on_hat_event (EventGamepadHat event) {
-               if (event.gamepad_hat.value == 0)
-                       return;
+                       break;
+               case Manette.EventType.EVENT_HAT:
+                       uint16 axis;
+                       int8 value;
+
+                       if (!event.get_hat (out axis, out value))
+                               return;
+
+                       if (value == 0)
+                               return;
 
-               var success = mapping_builder.set_hat_mapping (event.gamepad_hat.hardware_index,
-                                                              event.gamepad_hat.value,
-                                                              input);
-               if (!success)
+                       if (!mapping_builder.set_hat_mapping ((uint8) event.get_hardware_index (),
+                                                             value,
+                                                             input))
+                               return;
+
+                       break;
+               default:
                        return;
+               }
 
                next_input ();
        }
diff --git a/src/ui/gamepad-tester.vala b/src/ui/gamepad-tester.vala
index c547476..3caaf28 100644
--- a/src/ui/gamepad-tester.vala
+++ b/src/ui/gamepad-tester.vala
@@ -5,15 +5,14 @@ private class Games.GamepadTester : Gtk.Box {
        [GtkChild]
        private GamepadView gamepad_view;
 
-       private Gamepad gamepad;
+       private Manette.Device device;
 
        private ulong gamepad_button_press_event_handler_id;
        private ulong gamepad_button_release_event_handler_id;
        private ulong gamepad_axis_event_handler_id;
-       private ulong gamepad_hat_event_handler_id;
 
-       public GamepadTester (Gamepad gamepad, GamepadViewConfiguration configuration) {
-               this.gamepad = gamepad;
+       public GamepadTester (Manette.Device device, GamepadViewConfiguration configuration) {
+               this.device = device;
                try {
                        gamepad_view.set_configuration (configuration);
                }
@@ -32,51 +31,45 @@ private class Games.GamepadTester : Gtk.Box {
        }
 
        private void connect_to_gamepad () {
-               gamepad_button_press_event_handler_id = gamepad.button_press_event.connect ((event) => {
-                       on_button_event (event.gamepad_button, true);
-               });
-               gamepad_button_release_event_handler_id = gamepad.button_release_event.connect ((event) => {
-                       on_button_event (event.gamepad_button, false);
-               });
-               gamepad_axis_event_handler_id = gamepad.axis_event.connect ((event) => {
-                       on_axis_event (event.gamepad_axis);
-               });
-               gamepad_hat_event_handler_id = gamepad.hat_event.connect ((event) => {
-                       on_hat_event (event.gamepad_hat);
-               });
+               gamepad_button_press_event_handler_id = device.button_press_event.connect 
(on_button_press_event);
+               gamepad_button_release_event_handler_id = device.button_release_event.connect 
(on_button_release_event);
+               gamepad_axis_event_handler_id = device.absolute_axis_event.connect (on_absolute_axis_event);
        }
 
        private void disconnect_from_gamepad () {
                if (gamepad_button_press_event_handler_id != 0) {
-                       gamepad.disconnect (gamepad_button_press_event_handler_id);
+                       device.disconnect (gamepad_button_press_event_handler_id);
                        gamepad_button_press_event_handler_id = 0;
                }
                if (gamepad_button_release_event_handler_id != 0) {
-                       gamepad.disconnect (gamepad_button_release_event_handler_id);
+                       device.disconnect (gamepad_button_release_event_handler_id);
                        gamepad_button_release_event_handler_id = 0;
                }
                if (gamepad_axis_event_handler_id != 0) {
-                       gamepad.disconnect (gamepad_axis_event_handler_id);
-                       gamepad_axis_event_handler_id = 0;
-               }
-               if (gamepad_hat_event_handler_id != 0) {
-                       gamepad.disconnect (gamepad_hat_event_handler_id);
+                       device.disconnect (gamepad_axis_event_handler_id);
                        gamepad_axis_event_handler_id = 0;
                }
        }
 
-       private void on_button_event (EventGamepadButton event, bool pressed) {
-               var highlight = pressed;
-               gamepad_view.highlight ({ EventCode.EV_KEY, event.button }, highlight);
+       private void on_button_press_event (Manette.Event event) {
+               uint16 button;
+
+               if (event.get_button (out button))
+                       gamepad_view.highlight ({ EventCode.EV_KEY, button }, true);
        }
 
-       private void on_axis_event (EventGamepadAxis event) {
-               var highlight = !(-0.8 < event.gamepad_axis.value < 0.8);
-               gamepad_view.highlight ({ EventCode.EV_ABS, event.axis }, highlight);
+       private void on_button_release_event (Manette.Event event) {
+               uint16 button;
+
+               if (event.get_button (out button))
+                       gamepad_view.highlight ({ EventCode.EV_KEY, button }, false);
        }
 
-       private void on_hat_event (EventGamepadHat event) {
-               var highlight = !(event.gamepad_hat.value == 0);
-               gamepad_view.highlight ({ EventCode.EV_ABS, event.axis }, highlight);
+       private void on_absolute_axis_event (Manette.Event event) {
+               uint16 axis;
+               double value;
+
+               if (event.get_absolute (out axis, out value))
+                       gamepad_view.highlight ({ EventCode.EV_ABS, axis }, !(-0.8 < value < 0.8));
        }
 }
diff --git a/src/ui/preferences-page-controllers.vala b/src/ui/preferences-page-controllers.vala
index 4f619a9..23a762f 100644
--- a/src/ui/preferences-page-controllers.vala
+++ b/src/ui/preferences-page-controllers.vala
@@ -12,7 +12,7 @@ private class Games.PreferencesPageControllers: Gtk.Stack, PreferencesPage {
        [GtkChild]
        private Gtk.HeaderBar default_header_bar;
 
-       private GamepadMonitor gamepad_monitor;
+       private Manette.Monitor monitor;
 
        private Binding header_bar_binding;
        private Binding immersive_mode_binding;
@@ -22,9 +22,9 @@ private class Games.PreferencesPageControllers: Gtk.Stack, PreferencesPage {
                header_bar = default_header_bar;
                immersive_mode = false;
 
-               gamepad_monitor = GamepadMonitor.get_instance ();
-               gamepad_monitor.gamepad_unplugged.connect (rebuild_gamepad_list);
-               gamepad_monitor.gamepad_plugged.connect (rebuild_gamepad_list);
+               monitor = new Manette.Monitor ();
+               monitor.device_connected.connect (rebuild_gamepad_list);
+               monitor.device_disconnected.connect (rebuild_gamepad_list);
                build_gamepad_list ();
        }
 
@@ -38,15 +38,17 @@ private class Games.PreferencesPageControllers: Gtk.Stack, PreferencesPage {
        }
 
        private void build_gamepad_list () {
+               Manette.Device device = null;
                var i = 0;
-               gamepad_monitor.foreach_gamepad ((gamepad) => {
+               var iterator = monitor.iterate ();
+               while (iterator.next (out device)) {
                        i += 1;
                        var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
-                       box.pack_start (new Gtk.Label (gamepad.name), false, false);
+                       box.pack_start (new Gtk.Label (device.get_name ()), false, false);
                        box.margin = 6;
                        box.show_all ();
                        gamepads_list_box.add (box);
-               });
+               };
        }
 
        private void clear_gamepad_list () {
@@ -55,17 +57,21 @@ private class Games.PreferencesPageControllers: Gtk.Stack, PreferencesPage {
 
        [GtkCallback]
        private void gamepads_list_box_row_activated (Gtk.ListBoxRow row_item) {
-               Gamepad? gamepad = null;
+               Manette.Device? device = null;
+               Manette.Device other_device = null;
                var i = 0;
                var row_index = row_item.get_index ();
-               gamepad_monitor.foreach_gamepad ((gamepad_) => {
+
+               var iterator = monitor.iterate ();
+               while (iterator.next (out other_device)) {
                        if (i++ == row_index)
-                               gamepad = gamepad_;
-               });
+                               device = other_device;
+               };
 
-               if (gamepad == null)
+               if (device == null)
                        return;
-               var configurer = new GamepadConfigurer(gamepad);
+
+               var configurer = new GamepadConfigurer(device);
                back_handler_id = configurer.back.connect (on_back);
                header_bar_binding = configurer.bind_property ("header-bar", this, "header-bar",
                                                               BindingFlags.SYNC_CREATE);


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