Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make key repeat and delay configurable #3730

Merged
merged 13 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions debian/libmiral7.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ libmiral.so.7 libmiral7 #MINVER#
(c++)"miral::InputConfiguration::~InputConfiguration()@MIRAL_5.1" 5.1.0
(c++)"miral::WindowManagerTools::move_cursor_to(mir::geometry::generic::Point<float>)@MIRAL_5.1" 5.1.0
MIRAL_5.2@MIRAL_5.2 5.2.0
(c++)"miral::InputConfiguration::Keyboard::Keyboard()@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::Keyboard::Keyboard(miral::InputConfiguration::Keyboard const&)@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::Keyboard::operator=(miral::InputConfiguration::Keyboard)@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::Keyboard::set_repeat_delay(int)@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::Keyboard::set_repeat_rate(int)@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::Keyboard::~Keyboard()@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::keyboard()@MIRAL_5.2" 5.2.0
(c++)"miral::InputConfiguration::keyboard(miral::InputConfiguration::Keyboard const&)@MIRAL_5.2" 5.2.0
(c++)"miral::MinimalWindowManager::MinimalWindowManager(miral::WindowManagerTools const&, MirInputEventModifier, miral::FocusStealing)@MIRAL_5.0" 5.2.0
(c++)"miral::MinimalWindowManager::MinimalWindowManager(miral::WindowManagerTools const&, miral::FocusStealing)@MIRAL_5.0" 5.2.0
(c++)"miral::WindowManagementPolicy::~WindowManagementPolicy()@MIRAL_5.0" 5.2.0
65 changes: 60 additions & 5 deletions examples/mir_demo_server/server_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "server_example_input_filter.h"
#include "server_example_test_client.h"

#include <functional>
#include <miral/cursor_theme.h>
#include <miral/display_configuration_option.h>
#include <miral/input_configuration.h>
Expand All @@ -33,12 +34,14 @@
#include "mir/options/option.h"
#include "mir/report_exception.h"
#include "mir/server.h"
#include "mir/log.h"

#include <boost/exception/diagnostic_information.hpp>

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <mutex>

namespace mir { class AbnormalExit; }

Expand Down Expand Up @@ -120,14 +123,24 @@ try
miral::MirRunner runner{argc, argv, "mir/mir_demo_server.config"};

miral::InputConfiguration input_configuration;
auto mouse = input_configuration.mouse();
auto touchpad = input_configuration.touchpad();
auto keyboard = input_configuration.keyboard();
Comment on lines 125 to +128
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could grow a utility class that combines these and the repeated "apply" logic?

Also, we need to be careful: we're now accessing mouse, touchpad and keyboard on multiple threads.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, I like the revised approach

Copy link
Contributor Author

@tarek-y-ismail tarek-y-ismail Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, do you have anything particular in mind? The simplest solution I can think of is keeping the initialization logic as is and wrapping the application logic in a lambda with a mutex.

Something like:

+    std::mutex config_mutex;
+
+    auto config_applier = [&] mutable
+    {
+        input_configuration.mouse(mouse);
+        input_configuration.touchpad(touchpad);
+        input_configuration.keyboard(keyboard);
+    };
 
     miral::ConfigFile test{runner, "mir_demo_server.input", miral::ConfigFile::Mode::reload_on_change,
         [&](auto& in, auto path)
     {
         std::cout << "** Reloading: " << path << std::endl;
 
+        std::lock_guard lock{config_mutex};
 
         for (std::string line; std::getline(in, line);)
         {
@@ -185,17 +196,14 @@ try
             }
         }
 
-        input_configuration.mouse(mouse);
-        input_configuration.touchpad(touchpad);
-        input_configuration.keyboard(keyboard);
+        config_applier();
     }};
 
     runner.add_start_callback(
         [&]
         {
-            input_configuration.mouse(mouse);
-            input_configuration.touchpad(touchpad);
-            input_configuration.keyboard(keyboard);
+            std::lock_guard lock{config_mutex};
+            config_applier();

But I don't really think this needs its own utility class, just having something like this as an example should be enough.

Edit: Moved the lock to be external to config_applier since we also need to protect mouse, keyboard, and touchpad from being modified as we apply the config or vice-versa

Copy link
Collaborator

@AlanGriffiths AlanGriffiths Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is always possible to have data and related functions separately, but it is clearer to the reader when they are grouped as a class. In particular, it is easier to check invariants when it is clear which code can access the data.

In this case, I think the logic around config_mutex, mouse, touchpad and keyboard is complex enough to make isolating this data desirable:

class InputConfigFile : miral::ConfigFile
{
public:
    InputConfigFile(miral::MirRunner& runner, std::filesystem::path file) :
        ConfigFile{runner, file, ConfigFile::Mode::reload_on_change, [this](auto args...) {loader(args...); }}
    {
        runner.add_start_callback(
            [&]
            {
                std::lock_guard lock{config_mutex};
                config_applier();
            });
    }

    void operator()(mir::Server& server)
    {
        input_configuration(server);
    }

private:
    miral::InputConfiguration input_configuration;
    auto mouse = input_configuration.mouse();
    auto touchpad = input_configuration.touchpad();
    auto keyboard = input_configuration.keyboard();
    std::mutex config_mutex;

    void config_applier() const
    {
        input_configuration.mouse(mouse);
        input_configuration.touchpad(touchpad);
        input_configuration.keyboard(keyboard);
    };

    
    void loader(std::istream& istream, std::filesystem::path const& path)
    {
        std::lock_guard lock{config_mutex};
        // ...
    }
};

It is quick to see that the data is only touched by this code, and that it is appropriately locked.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a version without the undefined behaviour and compilation errors:

class InputConfigFile
{
public:
    InputConfigFile(miral::MirRunner& runner, std::filesystem::path file) :
        config_file{
            runner,
            file,
            miral::ConfigFile::Mode::reload_on_change,
            [this](std::istream& istream, std::filesystem::path const& path) {loader(istream, path); }}
    {
        runner.add_start_callback([this]
            {
                std::lock_guard lock{config_mutex};
                apply_config();
            });
    }

    void operator()(mir::Server& server)
    {
        input_configuration(server);
    }

private:
    miral::InputConfiguration input_configuration;
    miral::InputConfiguration::Mouse mouse = input_configuration.mouse();
    miral::InputConfiguration::Touchpad touchpad = input_configuration.touchpad();
    miral::InputConfiguration::Keyboard keyboard = input_configuration.keyboard();
    miral::ConfigFile config_file;
    std::mutex config_mutex;

    void apply_config()
    {
        input_configuration.mouse(mouse);
        input_configuration.touchpad(touchpad);
        input_configuration.keyboard(keyboard);
    };


    void loader(std::istream& in, std::filesystem::path const& path)
    {
        std::cout << "** Reloading: " << path << std::endl;

        std::lock_guard lock{config_mutex};

        for (std::string line; std::getline(in, line);)
        {
            std::cout << line << std::endl;

            if (line == "mir_pointer_handedness_right")
                mouse.handedness(mir_pointer_handedness_right);
            if (line == "mir_pointer_handedness_left")
                mouse.handedness(mir_pointer_handedness_left);

            if (line == "mir_touchpad_scroll_mode_none")
                touchpad.scroll_mode(mir_touchpad_scroll_mode_none);
            if (line == "mir_touchpad_scroll_mode_two_finger_scroll")
                touchpad.scroll_mode(mir_touchpad_scroll_mode_two_finger_scroll);
            if (line == "mir_touchpad_scroll_mode_edge_scroll")
                touchpad.scroll_mode(mir_touchpad_scroll_mode_edge_scroll);
            if (line == "mir_touchpad_scroll_mode_button_down_scroll")
                touchpad.scroll_mode(mir_touchpad_scroll_mode_button_down_scroll);

            if (line.contains("="))
            {
                auto const eq = line.find_first_of("=");
                auto const key = line.substr(0, eq);
                auto const value = line.substr(eq+1);

                auto const parse_and_validate = [](std::string const& key, std::string_view val) -> std::optional<int>
                {
                    auto const int_val = std::atoi(val.data());
                    if (int_val < 0)
                    {
                        mir::log_warning(
                            "Config value %s does not support negative values. Ignoring the supplied value (%d)...",
                            key.c_str(), int_val);
                        return std::nullopt;
                    }

                    return int_val;
                };

                if (key == "repeat_rate")
                {
                    auto const parsed = parse_and_validate(key, value);
                    if (parsed)
                        keyboard.set_repeat_rate(*parsed);
                }

                if (key == "repeat_delay")
                {
                    auto const parsed = parse_and_validate(key, value);
                    if (parsed)
                        keyboard.set_repeat_delay(*parsed);
                }
            }
        }

        apply_config();
    }
};

One small annoyance is that this isn't Copyable, and that means the run_with() parameter needs passing with std::ref(). Hoisting the state into a shared Self would work, but seems overkill.

std::mutex config_mutex;

auto config_applier = [&] mutable
{
input_configuration.mouse(mouse);
input_configuration.touchpad(touchpad);
input_configuration.keyboard(keyboard);
};

miral::ConfigFile test{runner, "mir_demo_server.input", miral::ConfigFile::Mode::reload_on_change,
[&input_configuration](auto& in, auto path)
[&](auto& in, auto path)
{
std::cout << "** Reloading: " << path << std::endl;

auto mouse = input_configuration.mouse();
auto touchpad = input_configuration.touchpad();
std::lock_guard lock{config_mutex};

for (std::string line; std::getline(in, line);)
{
Expand All @@ -146,11 +159,53 @@ try
touchpad.scroll_mode(mir_touchpad_scroll_mode_edge_scroll);
if (line == "mir_touchpad_scroll_mode_button_down_scroll")
touchpad.scroll_mode(mir_touchpad_scroll_mode_button_down_scroll);

if (line.contains("="))
{
auto const eq = line.find_first_of("=");
auto const key = line.substr(0, eq);
auto const value = line.substr(eq+1);

auto const parse_and_validate = [](std::string const& key, std::string_view val) -> std::optional<int>
{
auto const int_val = std::atoi(val.data());
if (int_val < 0)
{
mir::log_warning(
"Config value %s does not support negative values. Ignoring the supplied value (%d)...",
key.c_str(), int_val);
return std::nullopt;
}

return int_val;
};

if (key == "repeat_rate")
{
auto const parsed = parse_and_validate(key, value);
if (parsed)
keyboard.set_repeat_rate(*parsed);
}

if (key == "repeat_delay")
{
auto const parsed = parse_and_validate(key, value);
if (parsed)
keyboard.set_repeat_delay(*parsed);
}
}
}
input_configuration.mouse(mouse);
input_configuration.touchpad(touchpad);

config_applier();
}};

runner.add_start_callback(
[&]
{
std::lock_guard lock{config_mutex};
config_applier();
});

runner.set_exception_handler(exception_handler);

std::function<void()> shutdown_hook{[]{}};
Expand Down
24 changes: 24 additions & 0 deletions include/miral/miral/input_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class InputConfiguration

class Mouse;
class Touchpad;
class Keyboard;

auto mouse() -> Mouse;
void mouse(Mouse const& val);
auto touchpad() -> Touchpad;
void touchpad(Touchpad const& val);
auto keyboard() -> Keyboard;
void keyboard(Keyboard const& val);

private:
class Self;
Expand Down Expand Up @@ -115,6 +118,27 @@ class InputConfiguration::Touchpad
void scroll_mode(std::optional<MirTouchpadScrollMode>const& val);
void tap_to_click(std::optional<bool>const& val);

private:
friend class InputConfiguration::Self;
class Self;
std::shared_ptr<Self> self;
};

/** Input configuration for keyboard devices
* \remark Since MirAL 5.3
*/
class InputConfiguration::Keyboard
{
public:
Keyboard();
~Keyboard();

Keyboard(Keyboard const& that);
auto operator=(Keyboard that) -> Keyboard&;

void set_repeat_rate(int new_rate);
void set_repeat_delay(int new_delay);

private:
friend class InputConfiguration::Self;
class Self;
Expand Down
34 changes: 34 additions & 0 deletions include/server/mir/shell/keyboard_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIR_SHELL_KEYBOARD_HELPER_
#define MIR_SHELL_KEYBOARD_HELPER_

#include <optional>
namespace mir
{
namespace shell
{
class KeyboardHelper
{
public:
virtual ~KeyboardHelper() = default;
virtual void repeat_info_changed(std::optional<int> rate, int delay) const = 0;
};
}
}

#endif
3 changes: 3 additions & 0 deletions src/include/server/mir/default_server_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SurfaceStack;

namespace shell
{
class AccessibilityManager;
class DisplayConfigurationController;
class InputTargeter;
class FocusController;
Expand Down Expand Up @@ -281,6 +282,7 @@ class DefaultServerConfiguration : public virtual ServerConfiguration

virtual std::shared_ptr<shell::PersistentSurfaceStore> the_persistent_surface_store();
virtual std::shared_ptr<shell::ShellReport> the_shell_report();
virtual std::shared_ptr<shell::AccessibilityManager> the_accessibility_manager();
/** @} */

/** @name internal scene configuration
Expand Down Expand Up @@ -430,6 +432,7 @@ class DefaultServerConfiguration : public virtual ServerConfiguration
CachedPtr<SharedLibraryProberReport> shared_library_prober_report;
CachedPtr<shell::Shell> shell;
CachedPtr<shell::ShellReport> shell_report;
CachedPtr<shell::AccessibilityManager> accessibility_manager;
CachedPtr<shell::decoration::Manager> decoration_manager;
CachedPtr<scene::ApplicationNotRespondingDetector> application_not_responding_detector;
CachedPtr<cookie::Authority> cookie_authority;
Expand Down
4 changes: 4 additions & 0 deletions src/include/server/mir/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SessionAuthorizer;
}
namespace shell
{
class AccessibilityManager;
class DisplayLayout;
class DisplayConfigurationController;
class FocusController;
Expand Down Expand Up @@ -438,6 +439,9 @@ class Server

auto the_token_authority() const ->
std::shared_ptr<shell::TokenAuthority>;

auto the_accessibility_manager() const ->
std::shared_ptr<shell::AccessibilityManager>;
/** @} */

/** @name Client side support
Expand Down
54 changes: 54 additions & 0 deletions src/include/server/mir/shell/accessibility_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIR_SHELL_ACCESSIBILITY_MANAGER_H
#define MIR_SHELL_ACCESSIBILITY_MANAGER_H

#include <memory>
#include <optional>
#include <vector>

namespace mir
{
namespace shell
{
class KeyboardHelper;
class AccessibilityManager
{
public:
void register_keyboard_helper(std::shared_ptr<shell::KeyboardHelper> const&);

std::optional<int> repeat_rate() const;
int repeat_delay() const;

void repeat_rate(int new_rate);
void repeat_delay(int new_rate);
void override_key_repeat_settings(bool enable);

void notify_helpers() const;

private:
std::vector<std::shared_ptr<shell::KeyboardHelper>> keyboard_helpers;

// 25 rate and 600 delay are the default in Weston and Sway
int repeat_rate_{25};
int repeat_delay_{600};
bool enable_key_repeat{true};
};
}
}

#endif
Loading
Loading