1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QEVDEVKEYBOARDHANDLER_P_H |
41 | #define QEVDEVKEYBOARDHANDLER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <qobject.h> |
55 | #include <QTimer> |
56 | #include <QDataStream> |
57 | |
58 | #include <memory> |
59 | |
60 | QT_BEGIN_NAMESPACE |
61 | |
62 | class QSocketNotifier; |
63 | |
64 | namespace QEvdevKeyboardMap { |
65 | const quint32 FileMagic = 0x514d4150; // 'QMAP' |
66 | |
67 | struct Mapping { |
68 | quint16 keycode; |
69 | quint16 unicode; |
70 | quint32 qtcode; |
71 | quint8 modifiers; |
72 | quint8 flags; |
73 | quint16 special; |
74 | |
75 | }; |
76 | |
77 | enum Flags { |
78 | IsDead = 0x01, |
79 | IsLetter = 0x02, |
80 | IsModifier = 0x04, |
81 | IsSystem = 0x08 |
82 | }; |
83 | |
84 | enum System { |
85 | SystemConsoleFirst = 0x0100, |
86 | SystemConsoleMask = 0x007f, |
87 | SystemConsoleLast = 0x017f, |
88 | SystemConsolePrevious = 0x0180, |
89 | SystemConsoleNext = 0x0181, |
90 | SystemReboot = 0x0200, |
91 | SystemZap = 0x0300 |
92 | }; |
93 | |
94 | struct Composing { |
95 | quint16 first; |
96 | quint16 second; |
97 | quint16 result; |
98 | }; |
99 | |
100 | enum Modifiers { |
101 | ModPlain = 0x00, |
102 | ModShift = 0x01, |
103 | ModAltGr = 0x02, |
104 | ModControl = 0x04, |
105 | ModAlt = 0x08, |
106 | ModShiftL = 0x10, |
107 | ModShiftR = 0x20, |
108 | ModCtrlL = 0x40, |
109 | ModCtrlR = 0x80 |
110 | // ModCapsShift = 0x100, // not supported! |
111 | }; |
112 | } |
113 | |
114 | inline QDataStream &operator>>(QDataStream &ds, QEvdevKeyboardMap::Mapping &m) |
115 | { |
116 | return ds >> m.keycode >> m.unicode >> m.qtcode >> m.modifiers >> m.flags >> m.special; |
117 | } |
118 | |
119 | inline QDataStream &operator<<(QDataStream &ds, const QEvdevKeyboardMap::Mapping &m) |
120 | { |
121 | return ds << m.keycode << m.unicode << m.qtcode << m.modifiers << m.flags << m.special; |
122 | } |
123 | |
124 | inline QDataStream &operator>>(QDataStream &ds, QEvdevKeyboardMap::Composing &c) |
125 | { |
126 | return ds >> c.first >> c.second >> c.result; |
127 | } |
128 | |
129 | inline QDataStream &operator<<(QDataStream &ds, const QEvdevKeyboardMap::Composing &c) |
130 | { |
131 | return ds << c.first << c.second << c.result; |
132 | } |
133 | |
134 | class QFdContainer |
135 | { |
136 | int m_fd; |
137 | Q_DISABLE_COPY_MOVE(QFdContainer); |
138 | public: |
139 | explicit QFdContainer(int fd = -1) noexcept : m_fd(fd) {} |
140 | ~QFdContainer() { reset(); } |
141 | |
142 | int get() const noexcept { return m_fd; } |
143 | |
144 | int release() noexcept { int result = m_fd; m_fd = -1; return result; } |
145 | void reset() noexcept; |
146 | void reset(int fd) { reset(); m_fd = fd; } |
147 | }; |
148 | |
149 | class QEvdevKeyboardHandler : public QObject |
150 | { |
151 | public: |
152 | QEvdevKeyboardHandler(const QString &device, QFdContainer &fd, bool disableZap, bool enableCompose, const QString &keymapFile); |
153 | ~QEvdevKeyboardHandler(); |
154 | |
155 | enum KeycodeAction { |
156 | None = 0, |
157 | |
158 | CapsLockOff = 0x01000000, |
159 | CapsLockOn = 0x01000001, |
160 | NumLockOff = 0x02000000, |
161 | NumLockOn = 0x02000001, |
162 | ScrollLockOff = 0x03000000, |
163 | ScrollLockOn = 0x03000001, |
164 | |
165 | Reboot = 0x04000000, |
166 | |
167 | PreviousConsole = 0x05000000, |
168 | NextConsole = 0x05000001, |
169 | SwitchConsoleFirst = 0x06000000, |
170 | SwitchConsoleLast = 0x0600007f, |
171 | SwitchConsoleMask = 0x0000007f |
172 | }; |
173 | |
174 | static std::unique_ptr<QEvdevKeyboardHandler> create(const QString &device, |
175 | const QString &specification, |
176 | const QString &defaultKeymapFile = QString()); |
177 | |
178 | static Qt::KeyboardModifiers toQtModifiers(quint8 mod) |
179 | { |
180 | Qt::KeyboardModifiers qtmod = Qt::NoModifier; |
181 | |
182 | if (mod & (QEvdevKeyboardMap::ModShift | QEvdevKeyboardMap::ModShiftL | QEvdevKeyboardMap::ModShiftR)) |
183 | qtmod |= Qt::ShiftModifier; |
184 | if (mod & (QEvdevKeyboardMap::ModControl | QEvdevKeyboardMap::ModCtrlL | QEvdevKeyboardMap::ModCtrlR)) |
185 | qtmod |= Qt::ControlModifier; |
186 | if (mod & QEvdevKeyboardMap::ModAlt) |
187 | qtmod |= Qt::AltModifier; |
188 | |
189 | return qtmod; |
190 | } |
191 | |
192 | bool loadKeymap(const QString &file); |
193 | void unloadKeymap(); |
194 | |
195 | void readKeycode(); |
196 | KeycodeAction processKeycode(quint16 keycode, bool pressed, bool autorepeat); |
197 | |
198 | void switchLang(); |
199 | |
200 | private: |
201 | void processKeyEvent(int nativecode, int unicode, int qtcode, |
202 | Qt::KeyboardModifiers modifiers, bool isPress, bool autoRepeat); |
203 | void switchLed(int, bool); |
204 | |
205 | QString m_device; |
206 | QFdContainer m_fd; |
207 | QSocketNotifier *m_notify; |
208 | |
209 | // keymap handling |
210 | quint8 m_modifiers; |
211 | quint8 m_locks[3]; |
212 | int m_composing; |
213 | quint16 m_dead_unicode; |
214 | quint8 m_langLock; |
215 | |
216 | bool m_no_zap; |
217 | bool m_do_compose; |
218 | |
219 | const QEvdevKeyboardMap::Mapping *m_keymap; |
220 | int m_keymap_size; |
221 | const QEvdevKeyboardMap::Composing *m_keycompose; |
222 | int m_keycompose_size; |
223 | |
224 | static const QEvdevKeyboardMap::Mapping s_keymap_default[]; |
225 | static const QEvdevKeyboardMap::Composing s_keycompose_default[]; |
226 | }; |
227 | |
228 | |
229 | QT_END_NAMESPACE |
230 | |
231 | #endif // QEVDEVKEYBOARDHANDLER_P_H |
232 | |