Move keycodeForKeysym from ButtonRebindsFilter into Xkb

So we can use it in other places
This commit is contained in:
Arjen Hiemstra 2022-09-01 15:04:21 +02:00
parent 31cf10a6aa
commit 076203c926
3 changed files with 24 additions and 21 deletions

View file

@ -93,26 +93,6 @@ bool InputDevice::isTouchpad() const
return false;
}
static std::optional<int> keycodeFromKeysym(xkb_keysym_t keysym)
{
auto xkb = KWin::input()->keyboard()->xkb();
auto layout = xkb_state_serialize_layout(xkb->state(), XKB_STATE_LAYOUT_EFFECTIVE);
const xkb_keycode_t max = xkb_keymap_max_keycode(xkb->keymap());
for (xkb_keycode_t keycode = xkb_keymap_min_keycode(xkb->keymap()); keycode < max; keycode++) {
uint levelCount = xkb_keymap_num_levels_for_key(xkb->keymap(), keycode, layout);
for (uint currentLevel = 0; currentLevel < levelCount; currentLevel++) {
const xkb_keysym_t *syms;
uint num_syms = xkb_keymap_key_get_syms_by_level(xkb->keymap(), keycode, layout, currentLevel, &syms);
for (uint sym = 0; sym < num_syms; sym++) {
if (syms[sym] == keysym) {
return {keycode - 8};
}
}
}
}
return {};
}
ButtonRebindsFilter::ButtonRebindsFilter()
: KWin::Plugin()
, KWin::InputEventFilter()
@ -170,7 +150,7 @@ bool ButtonRebindsFilter::pointerEvent(QMouseEvent *event, quint32 nativeButton)
return false;
}
// KKeyServer returns upper case syms, lower it to not confuse modifiers handling
auto keyCode = keycodeFromKeysym(sym);
auto keyCode = KWin::input()->keyboard()->xkb()->keycodeFromKeysym(sym);
if (!keyCode) {
qCWarning(KWIN_BUTTONREBINDS) << "Could not convert" << keys << "sym: " << sym << "to keycode";
return false;

View file

@ -619,4 +619,22 @@ void Xkb::setSeat(KWaylandServer::SeatInterface *seat)
m_seat = QPointer<KWaylandServer::SeatInterface>(seat);
}
std::optional<int> Xkb::keycodeFromKeysym(xkb_keysym_t keysym)
{
auto layout = xkb_state_serialize_layout(m_state, XKB_STATE_LAYOUT_EFFECTIVE);
const xkb_keycode_t max = xkb_keymap_max_keycode(m_keymap);
for (xkb_keycode_t keycode = xkb_keymap_min_keycode(m_keymap); keycode < max; keycode++) {
uint levelCount = xkb_keymap_num_levels_for_key(m_keymap, keycode, layout);
for (uint currentLevel = 0; currentLevel < levelCount; currentLevel++) {
const xkb_keysym_t *syms;
uint num_syms = xkb_keymap_key_get_syms_by_level(m_keymap, keycode, layout, currentLevel, &syms);
for (uint sym = 0; sym < num_syms; sym++) {
if (syms[sym] == keysym) {
return {keycode - EVDEV_OFFSET};
}
}
}
}
return {};
}
}

View file

@ -16,6 +16,9 @@
#include <KConfigGroup>
#include <QLoggingCategory>
#include <optional>
Q_DECLARE_LOGGING_CATEGORY(KWIN_XKB)
struct xkb_context;
@ -104,6 +107,8 @@ public:
void setSeat(KWaylandServer::SeatInterface *seat);
QByteArray keymapContents() const;
std::optional<int> keycodeFromKeysym(xkb_keysym_t keysym);
Q_SIGNALS:
void ledsChanged(const LEDs &leds);
void modifierStateChanged();