Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlibinputkeyboard.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include <QtCore/QLoggingCategory>
7#include <QtGui/private/qguiapplication_p.h>
8#include <QtGui/private/qinputdevicemanager_p.h>
9#include <qpa/qwindowsysteminterface.h>
10#include <libinput.h>
11#if QT_CONFIG(xkbcommon)
12#include <xkbcommon/xkbcommon-keysyms.h>
13#include <xkbcommon/xkbcommon-names.h>
14#include <QtGui/private/qxkbcommon_p.h>
15#endif
16
17QT_BEGIN_NAMESPACE
18
19#if QT_CONFIG(xkbcommon)
20const int REPEAT_DELAY = 500;
21const int REPEAT_RATE = 100;
22#endif
23
24QLibInputKeyboard::QLibInputKeyboard()
25{
26#if QT_CONFIG(xkbcommon)
27 qCDebug(qLcLibInput) << "Using xkbcommon for key mapping";
28 m_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
29 if (!m_ctx) {
30 qWarning("Failed to create xkb context");
31 return;
32 }
33 m_keymap = xkb_keymap_new_from_names(m_ctx, nullptr, XKB_KEYMAP_COMPILE_NO_FLAGS);
34 if (!m_keymap) {
35 qCWarning(qLcLibInput, "Failed to compile keymap");
36 return;
37 }
38 m_state = xkb_state_new(m_keymap);
39 if (!m_state) {
40 qCWarning(qLcLibInput, "Failed to create xkb state");
41 return;
42 }
43
44 m_repeatTimer.setSingleShot(true);
45 connect(&m_repeatTimer, &QTimer::timeout, this, &QLibInputKeyboard::handleRepeat);
46#else
47 qCWarning(qLcLibInput) << "xkbcommon not available, not performing key mapping";
48#endif
49}
50
51QLibInputKeyboard::~QLibInputKeyboard()
52{
53#if QT_CONFIG(xkbcommon)
54 if (m_state)
55 xkb_state_unref(m_state);
56 if (m_keymap)
57 xkb_keymap_unref(m_keymap);
58 if (m_ctx)
59 xkb_context_unref(m_ctx);
60#endif
61}
62
63void QLibInputKeyboard::processKey(libinput_event_keyboard *e)
64{
65#if QT_CONFIG(xkbcommon)
66 if (!m_ctx || !m_keymap || !m_state)
67 return;
68
69 const uint32_t keycode = libinput_event_keyboard_get_key(e) + 8;
70 const xkb_keysym_t sym = xkb_state_key_get_one_sym(m_state, keycode);
71 const bool pressed = libinput_event_keyboard_get_key_state(e) == LIBINPUT_KEY_STATE_PRESSED;
72
73 // Modifiers here is the modifier state before the event, i.e. not
74 // including the current key in case it is a modifier. See the XOR
75 // logic in QKeyEvent::modifiers(). ### QTBUG-73826
76 Qt::KeyboardModifiers modifiers = QXkbCommon::modifiers(m_state);
77
78 const QString text = QXkbCommon::lookupString(m_state, keycode);
79 const int qtkey = QXkbCommon::keysymToQtKey(sym, modifiers, m_state, keycode);
80
81 xkb_state_update_key(m_state, keycode, pressed ? XKB_KEY_DOWN : XKB_KEY_UP);
82
83 Qt::KeyboardModifiers modifiersAfterStateChange = QXkbCommon::modifiers(m_state, sym);
84 QGuiApplicationPrivate::inputDeviceManager()->setKeyboardModifiers(modifiersAfterStateChange);
85
86 QWindowSystemInterface::handleExtendedKeyEvent(nullptr,
87 pressed ? QEvent::KeyPress : QEvent::KeyRelease,
88 qtkey, modifiers, keycode, sym, modifiers, text);
89
90 if (pressed && xkb_keymap_key_repeats(m_keymap, keycode)) {
91 m_repeatData.qtkey = qtkey;
92 m_repeatData.mods = modifiers;
93 m_repeatData.nativeScanCode = keycode;
94 m_repeatData.virtualKey = sym;
95 m_repeatData.nativeMods = modifiers;
96 m_repeatData.unicodeText = text;
97 m_repeatData.repeatCount = 1;
98 m_repeatTimer.setInterval(REPEAT_DELAY);
99 m_repeatTimer.start();
100 } else if (m_repeatTimer.isActive()) {
101 m_repeatTimer.stop();
102 }
103
104#else
105 Q_UNUSED(e);
106#endif
107}
108
109#if QT_CONFIG(xkbcommon)
110void QLibInputKeyboard::handleRepeat()
111{
112 QWindowSystemInterface::handleExtendedKeyEvent(nullptr, QEvent::KeyPress,
113 m_repeatData.qtkey, m_repeatData.mods,
114 m_repeatData.nativeScanCode, m_repeatData.virtualKey, m_repeatData.nativeMods,
115 m_repeatData.unicodeText, true, m_repeatData.repeatCount);
116 m_repeatData.repeatCount += 1;
117 m_repeatTimer.setInterval(REPEAT_RATE);
118 m_repeatTimer.start();
119}
120#endif
121
122QT_END_NAMESPACE