From 8fdbce652ff69079d8e4864c421f3a6e7e1348b9 Mon Sep 17 00:00:00 2001 From: Alan Griffiths Date: Wed, 10 Jul 2024 17:01:22 +0100 Subject: [PATCH] Do registration where it makes sense --- .../include/server}/mir/input/device.h | 0 src/platforms/evdev/libinput_device.cpp | 29 +++++++------------ src/platforms/evdev/libinput_device.h | 6 ++-- src/platforms/evdev/platform.cpp | 4 --- src/server/input/default_configuration.cpp | 3 +- src/server/input/default_input_device_hub.cpp | 11 ++++++- src/server/input/default_input_device_hub.h | 5 +++- .../input/test_single_seat_setup.cpp | 5 +++- .../input/test_default_input_device_hub.cpp | 5 +++- 9 files changed, 37 insertions(+), 31 deletions(-) rename {include/platform => src/include/server}/mir/input/device.h (100%) diff --git a/include/platform/mir/input/device.h b/src/include/server/mir/input/device.h similarity index 100% rename from include/platform/mir/input/device.h rename to src/include/server/mir/input/device.h diff --git a/src/platforms/evdev/libinput_device.cpp b/src/platforms/evdev/libinput_device.cpp index 13468141d13..17ed5fc3b9b 100644 --- a/src/platforms/evdev/libinput_device.cpp +++ b/src/platforms/evdev/libinput_device.cpp @@ -104,28 +104,19 @@ auto get_axis_source(libinput_event_pointer* pointer) -> MirPointerAxisSource default: return mir_pointer_axis_source_none; } } +} -class LibInputDeviceLedObserver : public mir::input::LedObserver +void mie::LibInputDevice::leds_set(KeyboardLeds leds) { -public: - explicit LibInputDeviceLedObserver(mie::LibInputDevice const* device) : device{device} {} - - void leds_set(mir::input::KeyboardLeds leds) override - { - int led = 0; - if (contains(leds, mir::input::KeyboardLed::caps_lock)) - led |= LIBINPUT_LED_CAPS_LOCK; - if (contains(leds, mir::input::KeyboardLed::num_lock)) - led |= LIBINPUT_LED_NUM_LOCK; - if (contains(leds, mir::input::KeyboardLed::scroll_lock)) - led |= LIBINPUT_LED_SCROLL_LOCK; - - libinput_device_led_update(device->device(), static_cast(led)); - } + int led = 0; + if (contains(leds, mir::input::KeyboardLed::caps_lock)) + led |= LIBINPUT_LED_CAPS_LOCK; + if (contains(leds, mir::input::KeyboardLed::num_lock)) + led |= LIBINPUT_LED_NUM_LOCK; + if (contains(leds, mir::input::KeyboardLed::scroll_lock)) + led |= LIBINPUT_LED_SCROLL_LOCK; -private: - mie::LibInputDevice const* device; -}; + libinput_device_led_update(device(), static_cast(led)); } mie::LibInputDevice::LibInputDevice(std::shared_ptr const& report, LibInputDevicePtr dev) diff --git a/src/platforms/evdev/libinput_device.h b/src/platforms/evdev/libinput_device.h index ad34180237a..205c3faa203 100644 --- a/src/platforms/evdev/libinput_device.h +++ b/src/platforms/evdev/libinput_device.h @@ -45,7 +45,7 @@ class OutputInfo; class InputReport; namespace evdev { -class LibInputDevice : public input::InputDevice +class LibInputDevice : public input::InputDevice, public mir::input::LedObserver { public: LibInputDevice(std::shared_ptr const& report, LibInputDevicePtr dev); @@ -59,7 +59,8 @@ class LibInputDevice : public input::InputDevice void apply_settings(TouchpadSettings const&) override; optional_value get_touchscreen_settings() const override; void apply_settings(TouchscreenSettings const&) override; - void associate_to_id(MirInputDeviceId id); + + void leds_set(KeyboardLeds leds) override; void process_event(libinput_event* event); ::libinput_device* device() const; @@ -104,7 +105,6 @@ class LibInputDevice : public input::InputDevice std::map last_seen_properties; void update_contact_data(ContactData &data, MirTouchAction action, libinput_event_touch* touch); - void try_stop_observing_leds(); }; } } diff --git a/src/platforms/evdev/platform.cpp b/src/platforms/evdev/platform.cpp index 271ad23ae8a..bbc022910af 100644 --- a/src/platforms/evdev/platform.cpp +++ b/src/platforms/evdev/platform.cpp @@ -28,7 +28,6 @@ #include "mir/input/input_device_registry.h" #include "mir/input/input_report.h" -#include "mir/input/device.h" #include "mir/fd.h" #include "mir/raii.h" @@ -432,9 +431,6 @@ void mie::Platform::device_added(libinput_device* dev) input_device_registry->add_device(devices.back()); - if (auto device = weak_device.lock()) - new_device->associate_to_id(device->id()); - log_info("Opened device: %s", describe(dev).c_str()); } catch(...) diff --git a/src/server/input/default_configuration.cpp b/src/server/input/default_configuration.cpp index 61048887876..b6260aa8c06 100644 --- a/src/server/input/default_configuration.cpp +++ b/src/server/input/default_configuration.cpp @@ -314,7 +314,8 @@ std::shared_ptr mir::DefaultServerConfiguration::the_ the_input_reading_multiplexer(), the_clock(), the_key_mapper(), - the_server_status_listener()); + the_server_status_listener(), + the_led_observer_registrar()); // lp:1675357: KeyRepeatDispatcher must be informed about removed input devices, otherwise // pressed keys get repeated indefinitely diff --git a/src/server/input/default_input_device_hub.cpp b/src/server/input/default_input_device_hub.cpp index 0cbc31887f8..f5fe01d7e28 100644 --- a/src/server/input/default_input_device_hub.cpp +++ b/src/server/input/default_input_device_hub.cpp @@ -32,6 +32,8 @@ #include "mir/log.h" #include "boost/throw_exception.hpp" +#include "mir/input/led_observer_registrar.h" +#include "mir/input/key_mapper.h" #include #include @@ -199,13 +201,15 @@ mi::DefaultInputDeviceHub::DefaultInputDeviceHub( std::shared_ptr const& input_multiplexer, std::shared_ptr const& clock, std::shared_ptr const& key_mapper, - std::shared_ptr const& server_status_listener) + std::shared_ptr const& server_status_listener, + std::shared_ptr led_observer_registrar) : seat{seat}, input_dispatchable{input_multiplexer}, device_queue(std::make_shared()), clock(clock), key_mapper(key_mapper), server_status_listener(server_status_listener), + led_observer_registrar{std::move(led_observer_registrar)}, device_id_generator{0} { input_dispatchable->add_watch(device_queue); @@ -492,6 +496,11 @@ auto mi::DefaultInputDeviceHub::add_device(std::shared_ptr const& d seat->add_device(*handle); dev->start(seat, input_dispatchable); + if (auto const observer = std::dynamic_pointer_cast(device)) + { + led_observer_registrar->register_interest(observer, handle->id()); + } + return handle; } else diff --git a/src/server/input/default_input_device_hub.h b/src/server/input/default_input_device_hub.h index 18d236d1334..cf362a92fd5 100644 --- a/src/server/input/default_input_device_hub.h +++ b/src/server/input/default_input_device_hub.h @@ -54,6 +54,7 @@ namespace input { class InputSink; class InputDeviceObserver; +class LedObserverRegistrar; class DefaultDevice; class Seat; class KeyMapper; @@ -86,7 +87,8 @@ class DefaultInputDeviceHub : std::shared_ptr const& input_multiplexer, std::shared_ptr const& clock, std::shared_ptr const& key_mapper, - std::shared_ptr const& server_status_listener); + std::shared_ptr const& server_status_listener, + std::shared_ptr led_observer_registrar); // InputDeviceRegistry - calls from mi::Platform auto add_device(std::shared_ptr const& device) -> std::weak_ptr override; @@ -118,6 +120,7 @@ class DefaultInputDeviceHub : std::shared_ptr const clock; std::shared_ptr const key_mapper; std::shared_ptr const server_status_listener; + std::shared_ptr const led_observer_registrar; ThreadSafeList> observers; /// Does not guarantee it's own threadsafety, non-const methods should not be called from multiple threads at once diff --git a/tests/integration-tests/input/test_single_seat_setup.cpp b/tests/integration-tests/input/test_single_seat_setup.cpp index 2815f713aa1..a399eb2d372 100644 --- a/tests/integration-tests/input/test_single_seat_setup.cpp +++ b/tests/integration-tests/input/test_single_seat_setup.cpp @@ -26,6 +26,7 @@ #include "mir/test/doubles/mock_touch_visualizer.h" #include "mir/test/doubles/mock_cursor_listener.h" #include "mir/test/doubles/mock_input_manager.h" +#include "mir/test/doubles/mock_led_observer_registrar.h" #include "mir/test/doubles/mock_seat_report.h" #include "mir/test/doubles/mock_server_status_listener.h" #include "mir/test/doubles/mock_scene_session.h" @@ -97,6 +98,7 @@ struct SingleSeatInputDeviceHubSetup : ::testing::Test ms::SessionContainer session_container; ms::BroadcastingSessionEventSink session_event_sink; mtd::FakeDisplayConfigurationObserverRegistrar display_config; + NiceMock led_observer_registrar; mi::BasicSeat seat{mt::fake_shared(mock_dispatcher), mt::fake_shared(mock_visualizer), mt::fake_shared(mock_cursor_listener), mt::fake_shared(display_config), mt::fake_shared(key_mapper), mt::fake_shared(clock), @@ -106,7 +108,8 @@ struct SingleSeatInputDeviceHubSetup : ::testing::Test mt::fake_shared(multiplexer), mt::fake_shared(clock), mt::fake_shared(key_mapper), - mt::fake_shared(mock_status_listener)}; + mt::fake_shared(mock_status_listener), + mt::fake_shared(led_observer_registrar)}; NiceMock mock_observer; mi::ConfigChanger changer{ mt::fake_shared(mock_input_manager), diff --git a/tests/unit-tests/input/test_default_input_device_hub.cpp b/tests/unit-tests/input/test_default_input_device_hub.cpp index 29cdef6116d..8a166efd8bb 100644 --- a/tests/unit-tests/input/test_default_input_device_hub.cpp +++ b/tests/unit-tests/input/test_default_input_device_hub.cpp @@ -21,6 +21,7 @@ #include "mir/test/doubles/mock_input_seat.h" #include "mir/test/doubles/mock_event_sink.h" #include "mir/test/doubles/mock_key_mapper.h" +#include "mir/test/doubles/mock_led_observer_registrar.h" #include "mir/test/doubles/mock_server_status_listener.h" #include "mir/test/doubles/advanceable_clock.h" #include "mir/test/fake_shared.h" @@ -67,6 +68,7 @@ MATCHER_P(WithName, name, struct InputDeviceHubTest : ::testing::Test { mir::dispatch::MultiplexingDispatchable multiplexer; + NiceMock led_observer_registrar; NiceMock mock_seat; NiceMock mock_key_mapper; NiceMock mock_server_status_listener; @@ -76,7 +78,8 @@ struct InputDeviceHubTest : ::testing::Test mt::fake_shared(multiplexer), mt::fake_shared(clock), mt::fake_shared(mock_key_mapper), - mt::fake_shared(mock_server_status_listener)}; + mt::fake_shared(mock_server_status_listener), + mt::fake_shared(led_observer_registrar)}; NiceMock mock_observer; NiceMock device{"device","dev-1", mi::DeviceCapability::unknown}; NiceMock another_device{"another_device","dev-2", mi::DeviceCapability::keyboard};