Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/tastenbrett/key.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public License as | ||||
6 | published by the Free Software Foundation; either version 2 of | ||||
7 | the License or (at your option) version 3 or any later version | ||||
8 | accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | defined in Section 14 of version 3 of the license. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "key.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QKeyEvent> | ||||
25 | #include <QMetaEnum> | ||||
26 | | ||||
27 | #include <X11/keysym.h> | ||||
28 | #include <xkbcommon/xkbcommon.h> | ||||
29 | | ||||
30 | #include "application.h" | ||||
31 | #include "shape.h" | ||||
32 | | ||||
33 | static QString xkbKeysymToName(xkb_keysym_t keysym) | ||||
34 | { | ||||
35 | QVarLengthArray<char, 32> chars(32); | ||||
36 | Q_ASSERT(chars.size() >= 0); // ensure cast to size_t | ||||
37 | | ||||
38 | const int size = xkb_keysym_get_name(keysym, chars.data(), static_cast<size_t>(chars.size())); | ||||
39 | if (Q_UNLIKELY(size > chars.size())) { | ||||
40 | chars.resize(size); | ||||
41 | xkb_keysym_get_name(keysym, chars.data(), static_cast<size_t>(chars.size())); | ||||
42 | } | ||||
43 | | ||||
44 | return QString::fromUtf8(chars.constData(), size); | ||||
45 | } | ||||
46 | | ||||
47 | static QString xkbKeysymToUtf8(xkb_keysym_t keysym) | ||||
48 | { | ||||
49 | QVarLengthArray<char, 32> chars(32); | ||||
50 | Q_ASSERT(chars.size() >= 0); // ensure cast to size_t | ||||
51 | | ||||
52 | const int size = xkb_keysym_to_utf8(keysym, chars.data(), static_cast<size_t>(chars.size())); | ||||
53 | if (Q_UNLIKELY(size > chars.size())) { | ||||
54 | chars.resize(size); | ||||
55 | xkb_keysym_to_utf8(keysym, chars.data(), static_cast<size_t>(chars.size())); | ||||
56 | } | ||||
57 | | ||||
58 | return QString::fromUtf8(chars.constData(), size); | ||||
59 | } | ||||
60 | | ||||
61 | static QString keySymToString(KeySym keysym) { | ||||
62 | // Strangely enough xkbcommons's UTF map is incomplete with regards to | ||||
63 | // dead keys. Extend it a bit. | ||||
64 | static QHash<unsigned long, char> deadMap { | ||||
65 | { XK_dead_grave, 0x0060 }, | ||||
66 | { XK_dead_acute, 0x00b4 }, | ||||
67 | { XK_dead_circumflex, 0x02c6 }, | ||||
68 | { XK_dead_tilde, 0x02dc }, | ||||
69 | { XK_dead_macron, 0x00af }, | ||||
70 | { XK_dead_breve, 0x02D8 }, | ||||
71 | { XK_dead_abovedot, 0x02D9 }, | ||||
72 | { XK_dead_diaeresis, 0x00A8 }, | ||||
73 | { XK_dead_abovering, 0x02DA }, | ||||
74 | { XK_dead_doubleacute, 0x02DD }, | ||||
75 | { XK_dead_caron, 0x02C7 }, | ||||
76 | { XK_dead_cedilla, 0x00B8 }, | ||||
77 | { XK_dead_ogonek, 0x02DB }, | ||||
78 | { XK_dead_iota, 0x0269 }, | ||||
79 | { XK_dead_voiced_sound, 0x309B }, | ||||
80 | { XK_dead_semivoiced_sound, 0x309A }, | ||||
81 | { XK_dead_belowdot, 0x0323 }, | ||||
82 | { XK_dead_hook, 0x0309 }, | ||||
83 | { XK_dead_horn, 0x031b }, | ||||
84 | { XK_dead_stroke, 0x0335 }, | ||||
85 | { XK_dead_abovecomma, 0x0312 }, | ||||
86 | { XK_dead_abovereversedcomma, 0x0314 }, | ||||
87 | { XK_dead_doublegrave, 0x030f }, | ||||
88 | { XK_dead_belowring, 0x0325 }, | ||||
89 | { XK_dead_belowmacron, 0x0331 }, | ||||
90 | { XK_dead_belowcircumflex, 0x032D }, | ||||
91 | { XK_dead_belowtilde, 0x0330 }, | ||||
92 | { XK_dead_belowbreve, 0x032e }, | ||||
93 | { XK_dead_belowdiaeresis, 0x0324 }, | ||||
94 | { XK_dead_invertedbreve, 0x0311 }, | ||||
95 | { XK_dead_belowcomma, 0x0326 }, | ||||
96 | { XK_dead_currency, 0x00A4 }, | ||||
97 | { XK_dead_a, 0x0061 }, | ||||
98 | { XK_dead_A, 0x0041 }, | ||||
99 | { XK_dead_e, 0x0065 }, | ||||
100 | { XK_dead_E, 0x0045 }, | ||||
101 | { XK_dead_i, 0x0069 }, | ||||
102 | { XK_dead_I, 0x0049 }, | ||||
103 | { XK_dead_o, 0x006f }, | ||||
104 | { XK_dead_O, 0x004f }, | ||||
105 | { XK_dead_u, 0x0075 }, | ||||
106 | { XK_dead_U, 0x0055 }, | ||||
107 | { XK_dead_small_schwa, 0x0259 }, | ||||
108 | { XK_dead_capital_schwa, 0x018F }, | ||||
109 | }; | ||||
110 | | ||||
111 | // XKB has fairly OK unicode maps, unfortunately it is | ||||
112 | // overzaelous and will for example return "U+001B" for | ||||
113 | // Esc which is a non-printable control character and | ||||
114 | // also not present in most fonts. As such it is | ||||
115 | // worthless to use and we'll discard unicode strings that | ||||
116 | // contain non-printable characters (ignore null). | ||||
117 | // This will lead to one of the stringy name fallbacks to handle | ||||
118 | // these cases and produce for example 'Escape' | ||||
119 | | ||||
120 | if (keysym == 0 /* NoSymbol */ || keysym == XK_VoidSymbol) { | ||||
121 | return QString(); | ||||
122 | } | ||||
123 | | ||||
124 | QString str; | ||||
125 | | ||||
126 | // Smartly xlib uses ulong and xkbcommon uses uint32 for syms, | ||||
127 | // so we'd best make sure that we can even cast the symbol before | ||||
128 | // tryint to do xkb mappings. Otherwise skip to fallbacks right away. | ||||
129 | const xkb_keysym_t xkbKeysym = static_cast<xkb_keysym_t>(keysym) ; | ||||
130 | if (static_cast<KeySym>(xkbKeysym) == keysym) { | ||||
131 | str = xkbKeysymToUtf8(xkbKeysym); | ||||
132 | | ||||
133 | for (const auto &c : str) { | ||||
134 | if (!c.isPrint() && !c.isNull()) { | ||||
135 | str = ""; | ||||
136 | break; | ||||
137 | } | ||||
138 | } | ||||
139 | | ||||
140 | if (str.isEmpty()) { | ||||
141 | str = xkbKeysymToName(xkbKeysym); | ||||
142 | } | ||||
143 | } | ||||
144 | | ||||
145 | if (str.isEmpty()) { | ||||
146 | str = XKeysymToString(keysym); | ||||
147 | // X11 keys can be of the form "Control_L". | ||||
148 | // Split them so they are easier on the eyes. | ||||
149 | str = str.replace('_', ' '); | ||||
150 | } | ||||
151 | | ||||
152 | if (deadMap.contains(keysym)) { | ||||
153 | str = QChar(deadMap[keysym]); | ||||
154 | } | ||||
155 | | ||||
156 | return str.replace('_', ' '); | ||||
157 | } | ||||
158 | | ||||
159 | KeyCap::KeyCap(const QString symbols[], QObject *parent) | ||||
160 | : QObject(parent) | ||||
161 | , topLeft(symbols[Level::TopLeft]) | ||||
162 | , topRight(symbols[Level::TopRight]) | ||||
163 | , bottomLeft(symbols[Level::BottomLeft]) | ||||
164 | , bottomRight(symbols[Level::BottomRight]) | ||||
165 | { | ||||
166 | } | ||||
167 | | ||||
168 | Key::Key(XkbKeyPtr key_, XkbDescPtr xkb_, QObject *parent) | ||||
169 | : XkbObject(xkb_, parent) | ||||
170 | , key(key_) | ||||
171 | , shape(new Shape(xkb->geom->shapes + key->shape_ndx, xkb, this)) | ||||
172 | , name(key_->name.name, XkbKeyNameLength) | ||||
173 | , nativeScanCode(nativeScanCodeFromName(name)) | ||||
174 | , cap(resolveCap()) | ||||
175 | , pressed(false) | ||||
176 | { | ||||
177 | qRegisterMetaType<Shape *>(); | ||||
178 | | ||||
179 | connect(Application::instance(), &Application::keyEvent, | ||||
180 | this, [this](QKeyEvent *event) | ||||
181 | { | ||||
182 | Q_ASSERT(event); | ||||
183 | if (event->nativeScanCode() == nativeScanCode) { | ||||
184 | pressed = event->type() == QKeyEvent::KeyPress; | ||||
185 | emit pressedChanged(); | ||||
186 | } | ||||
187 | }); | ||||
188 | } | ||||
189 | | ||||
190 | uint Key::nativeScanCodeFromName(const QByteArray &needle) | ||||
191 | { | ||||
192 | for (uint keycode = xkb->min_key_code; keycode <= xkb->max_key_code; ++keycode) { | ||||
193 | XkbKeyNameRec key = xkb->names->keys[keycode]; | ||||
194 | const QByteArray name(key.name, XkbKeyNameLength); | ||||
195 | if (name == needle) { | ||||
196 | return keycode; | ||||
197 | } | ||||
198 | } | ||||
199 | | ||||
200 | for (int i = 0; i < xkb->names->num_key_aliases; ++i) { | ||||
201 | XkbKeyAliasRec alias = xkb->names->key_aliases[i]; | ||||
202 | const QByteArray name(alias.alias, XkbKeyNameLength); | ||||
203 | if (name == needle) { | ||||
204 | return nativeScanCodeFromName(alias.real); | ||||
205 | } | ||||
206 | } | ||||
207 | | ||||
208 | return INVALID_KEYCODE; | ||||
209 | } | ||||
210 | | ||||
211 | KeyCap *Key::resolveCap() | ||||
212 | { | ||||
213 | // Documentation TLDR | ||||
214 | // - Levels are accessed by a modifier switching the keyboard to different symbols | ||||
215 | // such as hitting Shift and getting access to Shift+1=! | ||||
216 | // - Groups are an additional system which considers the entire keyboard switched | ||||
217 | // to a different symbol set. Such as the entire keyboard being Latin or Cyrillic. | ||||
218 | // Within each group there may be N Levels. The keyboard therefor has N Groups with | ||||
219 | // each having M levels. | ||||
220 | // For the purposes of key cap resolution we'll only look at the first group and the | ||||
221 | // first 4 levels within that group (top-left, top-right, bottom-left, bottom-right). | ||||
222 | | ||||
223 | const quint32 keycode = nativeScanCode; | ||||
224 | QString symbols[KeyCap::levelCount]; | ||||
225 | | ||||
226 | if (keycode == INVALID_KEYCODE) { | ||||
227 | return new KeyCap(symbols, this); | ||||
228 | } | ||||
229 | | ||||
230 | const int group = 0; | ||||
231 | // We iterate over the enum directly because it also represents | ||||
232 | // preference. TopLeft is the preferred location for unique mapping | ||||
233 | // such as 'F1' that can appear in all levels but we only want it shown | ||||
234 | // once in the TopLeft position. | ||||
235 | const auto levelEnum = QMetaEnum::fromType<KeyCap::Level>(); | ||||
236 | QVector<QString> seen; | ||||
237 | for (int i = 0; i < levelEnum.keyCount(); ++i) { | ||||
238 | int level = levelEnum.value(i); | ||||
239 | if (group >= XkbKeyNumGroups(xkb, keycode)) { | ||||
240 | continue; // group doesn't exist, shouldn't happen because we use group0 | ||||
241 | } | ||||
242 | if (level >= XkbKeyGroupWidth(xkb, keycode, group)) { | ||||
243 | continue; // level within group doesn't exist, can totally happen! | ||||
244 | } | ||||
245 | | ||||
246 | KeySym keysym = XkbKeySymEntry(xkb, keycode, level, group); | ||||
247 | const auto str = keySymToString(keysym); | ||||
248 | if (seen.contains(str)) { | ||||
249 | // Don't duplicate. e.g. 'F1' can appear in all levels | ||||
250 | continue; | ||||
251 | } | ||||
252 | seen << str; | ||||
253 | symbols[level] = str; | ||||
254 | } | ||||
255 | | ||||
256 | return new KeyCap(symbols, this); | ||||
257 | } |