ButtonRebindsFilter: Support disabled buttons

It's sometimes wanted that you disable certain buttons on the device,
such as an annoyingly place side button on a drawing tablet. This now
makes it possible to do so by putting "Disabled" in the config. The
rebind filter will then ensure the events are stopped and none are
emitted.
This commit is contained in:
Joshua Goins 2024-07-11 15:30:39 -04:00 committed by Joshua Goins
parent 5b17454aa5
commit 09003d03bc
2 changed files with 27 additions and 2 deletions

View file

@ -224,16 +224,26 @@ bool ButtonRebindsFilter::tabletToolButtonEvent(uint button, bool pressed, const
void ButtonRebindsFilter::insert(TriggerType type, const Trigger &trigger, const QStringList &entry)
{
if (entry.size() != 2) {
if (entry.empty()) {
qCWarning(KWIN_BUTTONREBINDS) << "Failed to rebind to" << entry;
return;
}
if (entry.first() == QLatin1String("Key")) {
if (entry.size() != 2) {
qCWarning(KWIN_BUTTONREBINDS) << "Invalid key" << entry;
return;
}
const auto keys = QKeySequence::fromString(entry.at(1), QKeySequence::PortableText);
if (!keys.isEmpty()) {
m_actions.at(type).insert(trigger, keys);
}
} else if (entry.first() == QLatin1String("MouseButton")) {
if (entry.size() != 2) {
qCWarning(KWIN_BUTTONREBINDS) << "Invalid mouse button" << entry;
return;
}
bool ok = false;
const MouseButton mb{entry.last().toUInt(&ok)};
if (ok) {
@ -242,6 +252,12 @@ void ButtonRebindsFilter::insert(TriggerType type, const Trigger &trigger, const
qCWarning(KWIN_BUTTONREBINDS) << "Could not convert" << entry << "into a mouse button";
}
} else if (entry.first() == QLatin1String("TabletToolButton")) {
if (entry.size() != 2) {
qCWarning(KWIN_BUTTONREBINDS)
<< "Invalid tablet tool button" << entry;
return;
}
bool ok = false;
const TabletToolButton tb{entry.last().toUInt(&ok)};
if (ok) {
@ -249,6 +265,8 @@ void ButtonRebindsFilter::insert(TriggerType type, const Trigger &trigger, const
} else {
qCWarning(KWIN_BUTTONREBINDS) << "Could not convert" << entry << "into a mouse button";
}
} else if (entry.first() == QLatin1String("Disabled")) {
m_actions.at(type).insert(trigger, DisabledButton{});
}
}
@ -269,6 +287,10 @@ bool ButtonRebindsFilter::send(TriggerType type, const Trigger &trigger, bool pr
if (const auto tb = std::get_if<TabletToolButton>(&action)) {
return sendTabletToolButton(tb->button, pressed, timestamp);
}
if (std::get_if<DisabledButton>(&action)) {
// Intentional, we don't want to anything to anybody
return true;
}
return false;
}

View file

@ -68,6 +68,9 @@ public:
{
quint32 button;
};
struct DisabledButton
{
};
explicit ButtonRebindsFilter();
bool pointerEvent(KWin::MouseEvent *event, quint32 nativeButton) override;
@ -83,7 +86,7 @@ private:
bool sendTabletToolButton(quint32 button, bool pressed, std::chrono::microseconds time);
InputDevice m_inputDevice;
std::array<QHash<Trigger, std::variant<QKeySequence, MouseButton, TabletToolButton>>, LastType> m_actions;
std::array<QHash<Trigger, std::variant<QKeySequence, MouseButton, TabletToolButton, DisabledButton>>, LastType> m_actions;
KConfigWatcher::Ptr m_configWatcher;
std::optional<KWin::TabletToolId> m_tabletTool;
};