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
qxkbcommon.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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// Qt-Security score:significant reason:default
4
5#include "qxkbcommon_p.h"
6
7#include <private/qmakearray_p.h>
8
9#include <QtCore/private/qstringiterator_p.h>
10#include <QtCore/qvarlengtharray.h>
11#include <QtCore/QMetaMethod>
12
13#include <QtGui/QKeyEvent>
14#include <QtGui/private/qguiapplication_p.h>
15
16#include <qpa/qplatforminputcontext.h>
17#include <qpa/qplatformintegration.h>
18
20
21static int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers,
22 xkb_state *state, xkb_keycode_t code,
23 bool superAsMeta, bool hyperAsMeta);
24
25typedef struct xkb2qt
26{
27 unsigned int xkb;
28 unsigned int qt;
29
30 constexpr bool operator <=(const xkb2qt &that) const noexcept
31 {
32 return xkb <= that.xkb;
33 }
34
35 constexpr bool operator <(const xkb2qt &that) const noexcept
36 {
37 return xkb < that.xkb;
38 }
39} xkb2qt_t;
40
41template<std::size_t Xkb, std::size_t Qt>
42struct Xkb2Qt
43{
44 using Type = xkb2qt_t;
45 static constexpr Type data() noexcept { return Type{Xkb, Qt}; }
46};
47
48static constexpr const auto KeyTbl = qMakeArray(
49 QSortedData<
50 // misc keys
51
52 Xkb2Qt<XKB_KEY_Escape, Qt::Key_Escape>,
53 Xkb2Qt<XKB_KEY_Tab, Qt::Key_Tab>,
54 Xkb2Qt<XKB_KEY_ISO_Left_Tab, Qt::Key_Backtab>,
55 Xkb2Qt<XKB_KEY_BackSpace, Qt::Key_Backspace>,
56 Xkb2Qt<XKB_KEY_Return, Qt::Key_Return>,
57 Xkb2Qt<XKB_KEY_Insert, Qt::Key_Insert>,
58 Xkb2Qt<XKB_KEY_Delete, Qt::Key_Delete>,
59 Xkb2Qt<XKB_KEY_Clear, Qt::Key_Delete>,
60 Xkb2Qt<XKB_KEY_Pause, Qt::Key_Pause>,
61 Xkb2Qt<XKB_KEY_Print, Qt::Key_Print>,
62 Xkb2Qt<XKB_KEY_Sys_Req, Qt::Key_SysReq>,
63 Xkb2Qt<0x1005FF60, Qt::Key_SysReq>, // hardcoded Sun SysReq
64 Xkb2Qt<0x1007ff00, Qt::Key_SysReq>, // hardcoded X386 SysReq
65
66 // cursor movement
67
68 Xkb2Qt<XKB_KEY_Home, Qt::Key_Home>,
69 Xkb2Qt<XKB_KEY_End, Qt::Key_End>,
70 Xkb2Qt<XKB_KEY_Left, Qt::Key_Left>,
71 Xkb2Qt<XKB_KEY_Up, Qt::Key_Up>,
72 Xkb2Qt<XKB_KEY_Right, Qt::Key_Right>,
73 Xkb2Qt<XKB_KEY_Down, Qt::Key_Down>,
74 Xkb2Qt<XKB_KEY_Prior, Qt::Key_PageUp>,
75 Xkb2Qt<XKB_KEY_Next, Qt::Key_PageDown>,
76
77 // modifiers
78
79 Xkb2Qt<XKB_KEY_Shift_L, Qt::Key_Shift>,
80 Xkb2Qt<XKB_KEY_Shift_R, Qt::Key_Shift>,
81 Xkb2Qt<XKB_KEY_Shift_Lock, Qt::Key_Shift>,
82 Xkb2Qt<XKB_KEY_Control_L, Qt::Key_Control>,
83 Xkb2Qt<XKB_KEY_Control_R, Qt::Key_Control>,
84 Xkb2Qt<XKB_KEY_Meta_L, Qt::Key_Meta>,
85 Xkb2Qt<XKB_KEY_Meta_R, Qt::Key_Meta>,
86 Xkb2Qt<XKB_KEY_Alt_L, Qt::Key_Alt>,
87 Xkb2Qt<XKB_KEY_Alt_R, Qt::Key_Alt>,
88 Xkb2Qt<XKB_KEY_Caps_Lock, Qt::Key_CapsLock>,
89 Xkb2Qt<XKB_KEY_Num_Lock, Qt::Key_NumLock>,
90 Xkb2Qt<XKB_KEY_Scroll_Lock, Qt::Key_ScrollLock>,
91 Xkb2Qt<XKB_KEY_Super_L, Qt::Key_Super_L>,
92 Xkb2Qt<XKB_KEY_Super_R, Qt::Key_Super_R>,
93 Xkb2Qt<XKB_KEY_Menu, Qt::Key_Menu>,
94 Xkb2Qt<XKB_KEY_Hyper_L, Qt::Key_Hyper_L>,
95 Xkb2Qt<XKB_KEY_Hyper_R, Qt::Key_Hyper_R>,
96 Xkb2Qt<XKB_KEY_Help, Qt::Key_Help>,
97 Xkb2Qt<0x1000FF74, Qt::Key_Backtab>, // hardcoded HP backtab
98 Xkb2Qt<0x1005FF10, Qt::Key_F11>, // hardcoded Sun F36 (labeled F11)
99 Xkb2Qt<0x1005FF11, Qt::Key_F12>, // hardcoded Sun F37 (labeled F12)
100
101 // numeric and function keypad keys
102
103 Xkb2Qt<XKB_KEY_KP_Space, Qt::Key_Space>,
104 Xkb2Qt<XKB_KEY_KP_Tab, Qt::Key_Tab>,
105 Xkb2Qt<XKB_KEY_KP_Enter, Qt::Key_Enter>,
106 Xkb2Qt<XKB_KEY_KP_Home, Qt::Key_Home>,
107 Xkb2Qt<XKB_KEY_KP_Left, Qt::Key_Left>,
108 Xkb2Qt<XKB_KEY_KP_Up, Qt::Key_Up>,
109 Xkb2Qt<XKB_KEY_KP_Right, Qt::Key_Right>,
110 Xkb2Qt<XKB_KEY_KP_Down, Qt::Key_Down>,
111 Xkb2Qt<XKB_KEY_KP_Prior, Qt::Key_PageUp>,
112 Xkb2Qt<XKB_KEY_KP_Next, Qt::Key_PageDown>,
113 Xkb2Qt<XKB_KEY_KP_End, Qt::Key_End>,
114 Xkb2Qt<XKB_KEY_KP_Begin, Qt::Key_Clear>,
115 Xkb2Qt<XKB_KEY_KP_Insert, Qt::Key_Insert>,
116 Xkb2Qt<XKB_KEY_KP_Delete, Qt::Key_Delete>,
117 Xkb2Qt<XKB_KEY_KP_Equal, Qt::Key_Equal>,
118 Xkb2Qt<XKB_KEY_KP_Multiply, Qt::Key_Asterisk>,
119 Xkb2Qt<XKB_KEY_KP_Add, Qt::Key_Plus>,
120 Xkb2Qt<XKB_KEY_KP_Separator, Qt::Key_Comma>,
121 Xkb2Qt<XKB_KEY_KP_Subtract, Qt::Key_Minus>,
122 Xkb2Qt<XKB_KEY_KP_Decimal, Qt::Key_Period>,
123 Xkb2Qt<XKB_KEY_KP_Divide, Qt::Key_Slash>,
124
125 // special non-XF86 function keys
126
127 Xkb2Qt<XKB_KEY_Undo, Qt::Key_Undo>,
128 Xkb2Qt<XKB_KEY_Redo, Qt::Key_Redo>,
129 Xkb2Qt<XKB_KEY_Find, Qt::Key_Find>,
130 Xkb2Qt<XKB_KEY_Cancel, Qt::Key_Cancel>,
131
132 // International input method support keys
133
134 // International & multi-key character composition
135 Xkb2Qt<XKB_KEY_ISO_Level3_Shift, Qt::Key_AltGr>,
136 Xkb2Qt<XKB_KEY_Multi_key, Qt::Key_Multi_key>,
137 Xkb2Qt<XKB_KEY_Codeinput, Qt::Key_Codeinput>,
138 Xkb2Qt<XKB_KEY_SingleCandidate, Qt::Key_SingleCandidate>,
139 Xkb2Qt<XKB_KEY_MultipleCandidate, Qt::Key_MultipleCandidate>,
140 Xkb2Qt<XKB_KEY_PreviousCandidate, Qt::Key_PreviousCandidate>,
141
142 // Misc Functions
143 Xkb2Qt<XKB_KEY_Mode_switch, Qt::Key_Mode_switch>,
144 Xkb2Qt<XKB_KEY_script_switch, Qt::Key_Mode_switch>,
145
146 // Japanese keyboard support
147 Xkb2Qt<XKB_KEY_Kanji, Qt::Key_Kanji>,
148 Xkb2Qt<XKB_KEY_Muhenkan, Qt::Key_Muhenkan>,
149 //Xkb2Qt<XKB_KEY_Henkan_Mode, Qt::Key_Henkan_Mode>,
150 Xkb2Qt<XKB_KEY_Henkan_Mode, Qt::Key_Henkan>,
151 Xkb2Qt<XKB_KEY_Henkan, Qt::Key_Henkan>,
152 Xkb2Qt<XKB_KEY_Romaji, Qt::Key_Romaji>,
153 Xkb2Qt<XKB_KEY_Hiragana, Qt::Key_Hiragana>,
154 Xkb2Qt<XKB_KEY_Katakana, Qt::Key_Katakana>,
155 Xkb2Qt<XKB_KEY_Hiragana_Katakana, Qt::Key_Hiragana_Katakana>,
156 Xkb2Qt<XKB_KEY_Zenkaku, Qt::Key_Zenkaku>,
157 Xkb2Qt<XKB_KEY_Hankaku, Qt::Key_Hankaku>,
158 Xkb2Qt<XKB_KEY_Zenkaku_Hankaku, Qt::Key_Zenkaku_Hankaku>,
159 Xkb2Qt<XKB_KEY_Touroku, Qt::Key_Touroku>,
160 Xkb2Qt<XKB_KEY_Massyo, Qt::Key_Massyo>,
161 Xkb2Qt<XKB_KEY_Kana_Lock, Qt::Key_Kana_Lock>,
162 Xkb2Qt<XKB_KEY_Kana_Shift, Qt::Key_Kana_Shift>,
163 Xkb2Qt<XKB_KEY_Eisu_Shift, Qt::Key_Eisu_Shift>,
164 Xkb2Qt<XKB_KEY_Eisu_toggle, Qt::Key_Eisu_toggle>,
165 //Xkb2Qt<XKB_KEY_Kanji_Bangou, Qt::Key_Kanji_Bangou>,
166 //Xkb2Qt<XKB_KEY_Zen_Koho, Qt::Key_Zen_Koho>,
167 //Xkb2Qt<XKB_KEY_Mae_Koho, Qt::Key_Mae_Koho>,
168 Xkb2Qt<XKB_KEY_Kanji_Bangou, Qt::Key_Codeinput>,
169 Xkb2Qt<XKB_KEY_Zen_Koho, Qt::Key_MultipleCandidate>,
170 Xkb2Qt<XKB_KEY_Mae_Koho, Qt::Key_PreviousCandidate>,
171
172 // Korean keyboard support
173 Xkb2Qt<XKB_KEY_Hangul, Qt::Key_Hangul>,
174 Xkb2Qt<XKB_KEY_Hangul_Start, Qt::Key_Hangul_Start>,
175 Xkb2Qt<XKB_KEY_Hangul_End, Qt::Key_Hangul_End>,
176 Xkb2Qt<XKB_KEY_Hangul_Hanja, Qt::Key_Hangul_Hanja>,
177 Xkb2Qt<XKB_KEY_Hangul_Jamo, Qt::Key_Hangul_Jamo>,
178 Xkb2Qt<XKB_KEY_Hangul_Romaja, Qt::Key_Hangul_Romaja>,
179 //Xkb2Qt<XKB_KEY_Hangul_Codeinput, Qt::Key_Hangul_Codeinput>,
180 Xkb2Qt<XKB_KEY_Hangul_Codeinput, Qt::Key_Codeinput>,
181 Xkb2Qt<XKB_KEY_Hangul_Jeonja, Qt::Key_Hangul_Jeonja>,
182 Xkb2Qt<XKB_KEY_Hangul_Banja, Qt::Key_Hangul_Banja>,
183 Xkb2Qt<XKB_KEY_Hangul_PreHanja, Qt::Key_Hangul_PreHanja>,
184 Xkb2Qt<XKB_KEY_Hangul_PostHanja, Qt::Key_Hangul_PostHanja>,
185 //Xkb2Qt<XKB_KEY_Hangul_SingleCandidate,Qt::Key_Hangul_SingleCandidate>,
186 //Xkb2Qt<XKB_KEY_Hangul_MultipleCandidate,Qt::Key_Hangul_MultipleCandidate>,
187 //Xkb2Qt<XKB_KEY_Hangul_PreviousCandidate,Qt::Key_Hangul_PreviousCandidate>,
188 Xkb2Qt<XKB_KEY_Hangul_SingleCandidate, Qt::Key_SingleCandidate>,
189 Xkb2Qt<XKB_KEY_Hangul_MultipleCandidate,Qt::Key_MultipleCandidate>,
190 Xkb2Qt<XKB_KEY_Hangul_PreviousCandidate,Qt::Key_PreviousCandidate>,
191 Xkb2Qt<XKB_KEY_Hangul_Special, Qt::Key_Hangul_Special>,
192 //Xkb2Qt<XKB_KEY_Hangul_switch, Qt::Key_Hangul_switch>,
193 Xkb2Qt<XKB_KEY_Hangul_switch, Qt::Key_Mode_switch>,
194
195 // dead keys
196 Xkb2Qt<XKB_KEY_dead_grave, Qt::Key_Dead_Grave>,
197 Xkb2Qt<XKB_KEY_dead_acute, Qt::Key_Dead_Acute>,
198 Xkb2Qt<XKB_KEY_dead_circumflex, Qt::Key_Dead_Circumflex>,
199 Xkb2Qt<XKB_KEY_dead_tilde, Qt::Key_Dead_Tilde>,
200 Xkb2Qt<XKB_KEY_dead_macron, Qt::Key_Dead_Macron>,
201 Xkb2Qt<XKB_KEY_dead_breve, Qt::Key_Dead_Breve>,
202 Xkb2Qt<XKB_KEY_dead_abovedot, Qt::Key_Dead_Abovedot>,
203 Xkb2Qt<XKB_KEY_dead_diaeresis, Qt::Key_Dead_Diaeresis>,
204 Xkb2Qt<XKB_KEY_dead_abovering, Qt::Key_Dead_Abovering>,
205 Xkb2Qt<XKB_KEY_dead_doubleacute, Qt::Key_Dead_Doubleacute>,
206 Xkb2Qt<XKB_KEY_dead_caron, Qt::Key_Dead_Caron>,
207 Xkb2Qt<XKB_KEY_dead_cedilla, Qt::Key_Dead_Cedilla>,
208 Xkb2Qt<XKB_KEY_dead_ogonek, Qt::Key_Dead_Ogonek>,
209 Xkb2Qt<XKB_KEY_dead_iota, Qt::Key_Dead_Iota>,
210 Xkb2Qt<XKB_KEY_dead_voiced_sound, Qt::Key_Dead_Voiced_Sound>,
211 Xkb2Qt<XKB_KEY_dead_semivoiced_sound, Qt::Key_Dead_Semivoiced_Sound>,
212 Xkb2Qt<XKB_KEY_dead_belowdot, Qt::Key_Dead_Belowdot>,
213 Xkb2Qt<XKB_KEY_dead_hook, Qt::Key_Dead_Hook>,
214 Xkb2Qt<XKB_KEY_dead_horn, Qt::Key_Dead_Horn>,
215 Xkb2Qt<XKB_KEY_dead_stroke, Qt::Key_Dead_Stroke>,
216 Xkb2Qt<XKB_KEY_dead_abovecomma, Qt::Key_Dead_Abovecomma>,
217 Xkb2Qt<XKB_KEY_dead_abovereversedcomma, Qt::Key_Dead_Abovereversedcomma>,
218 Xkb2Qt<XKB_KEY_dead_doublegrave, Qt::Key_Dead_Doublegrave>,
219 Xkb2Qt<XKB_KEY_dead_belowring, Qt::Key_Dead_Belowring>,
220 Xkb2Qt<XKB_KEY_dead_belowmacron, Qt::Key_Dead_Belowmacron>,
221 Xkb2Qt<XKB_KEY_dead_belowcircumflex, Qt::Key_Dead_Belowcircumflex>,
222 Xkb2Qt<XKB_KEY_dead_belowtilde, Qt::Key_Dead_Belowtilde>,
223 Xkb2Qt<XKB_KEY_dead_belowbreve, Qt::Key_Dead_Belowbreve>,
224 Xkb2Qt<XKB_KEY_dead_belowdiaeresis, Qt::Key_Dead_Belowdiaeresis>,
225 Xkb2Qt<XKB_KEY_dead_invertedbreve, Qt::Key_Dead_Invertedbreve>,
226 Xkb2Qt<XKB_KEY_dead_belowcomma, Qt::Key_Dead_Belowcomma>,
227 Xkb2Qt<XKB_KEY_dead_currency, Qt::Key_Dead_Currency>,
228 Xkb2Qt<XKB_KEY_dead_a, Qt::Key_Dead_a>,
229 Xkb2Qt<XKB_KEY_dead_A, Qt::Key_Dead_A>,
230 Xkb2Qt<XKB_KEY_dead_e, Qt::Key_Dead_e>,
231 Xkb2Qt<XKB_KEY_dead_E, Qt::Key_Dead_E>,
232 Xkb2Qt<XKB_KEY_dead_i, Qt::Key_Dead_i>,
233 Xkb2Qt<XKB_KEY_dead_I, Qt::Key_Dead_I>,
234 Xkb2Qt<XKB_KEY_dead_o, Qt::Key_Dead_o>,
235 Xkb2Qt<XKB_KEY_dead_O, Qt::Key_Dead_O>,
236 Xkb2Qt<XKB_KEY_dead_u, Qt::Key_Dead_u>,
237 Xkb2Qt<XKB_KEY_dead_U, Qt::Key_Dead_U>,
238 Xkb2Qt<XKB_KEY_dead_small_schwa, Qt::Key_Dead_Small_Schwa>,
239 Xkb2Qt<XKB_KEY_dead_capital_schwa, Qt::Key_Dead_Capital_Schwa>,
240 Xkb2Qt<XKB_KEY_dead_greek, Qt::Key_Dead_Greek>,
241/* The following four XKB_KEY_dead keys got removed in libxkbcommon 1.6.0
242 The define check is kind of version check here. */
243#ifdef XKB_KEY_dead_lowline
244 Xkb2Qt<XKB_KEY_dead_lowline, Qt::Key_Dead_Lowline>,
245 Xkb2Qt<XKB_KEY_dead_aboveverticalline, Qt::Key_Dead_Aboveverticalline>,
246 Xkb2Qt<XKB_KEY_dead_belowverticalline, Qt::Key_Dead_Belowverticalline>,
247 Xkb2Qt<XKB_KEY_dead_longsolidusoverlay, Qt::Key_Dead_Longsolidusoverlay>,
248#endif
249
250 // Special keys from X.org - This include multimedia keys,
251 // wireless/bluetooth/uwb keys, special launcher keys, etc.
252 Xkb2Qt<XKB_KEY_XF86Back, Qt::Key_Back>,
253 Xkb2Qt<XKB_KEY_XF86Forward, Qt::Key_Forward>,
254 Xkb2Qt<XKB_KEY_XF86Stop, Qt::Key_Stop>,
255 Xkb2Qt<XKB_KEY_XF86Refresh, Qt::Key_Refresh>,
256 Xkb2Qt<XKB_KEY_XF86Favorites, Qt::Key_Favorites>,
257 Xkb2Qt<XKB_KEY_XF86AudioMedia, Qt::Key_LaunchMedia>,
258 Xkb2Qt<XKB_KEY_XF86OpenURL, Qt::Key_OpenUrl>,
259 Xkb2Qt<XKB_KEY_XF86HomePage, Qt::Key_HomePage>,
260 Xkb2Qt<XKB_KEY_XF86Search, Qt::Key_Search>,
261 Xkb2Qt<XKB_KEY_XF86AudioLowerVolume, Qt::Key_VolumeDown>,
262 Xkb2Qt<XKB_KEY_XF86AudioMute, Qt::Key_VolumeMute>,
263 Xkb2Qt<XKB_KEY_XF86AudioRaiseVolume, Qt::Key_VolumeUp>,
264 Xkb2Qt<XKB_KEY_XF86AudioPlay, Qt::Key_MediaPlay>,
265 Xkb2Qt<XKB_KEY_XF86AudioStop, Qt::Key_MediaStop>,
266 Xkb2Qt<XKB_KEY_XF86AudioPrev, Qt::Key_MediaPrevious>,
267 Xkb2Qt<XKB_KEY_XF86AudioNext, Qt::Key_MediaNext>,
268 Xkb2Qt<XKB_KEY_XF86AudioRecord, Qt::Key_MediaRecord>,
269 Xkb2Qt<XKB_KEY_XF86AudioPause, Qt::Key_MediaPause>,
270 Xkb2Qt<XKB_KEY_XF86Mail, Qt::Key_LaunchMail>,
271 Xkb2Qt<XKB_KEY_XF86MyComputer, Qt::Key_LaunchMedia>,
272 Xkb2Qt<XKB_KEY_XF86Memo, Qt::Key_Memo>,
273 Xkb2Qt<XKB_KEY_XF86ToDoList, Qt::Key_ToDoList>,
274 Xkb2Qt<XKB_KEY_XF86Calendar, Qt::Key_Calendar>,
275 Xkb2Qt<XKB_KEY_XF86PowerDown, Qt::Key_PowerDown>,
276 Xkb2Qt<XKB_KEY_XF86ContrastAdjust, Qt::Key_ContrastAdjust>,
277 Xkb2Qt<XKB_KEY_XF86Standby, Qt::Key_Standby>,
278 Xkb2Qt<XKB_KEY_XF86MonBrightnessUp, Qt::Key_MonBrightnessUp>,
279 Xkb2Qt<XKB_KEY_XF86MonBrightnessDown, Qt::Key_MonBrightnessDown>,
280 Xkb2Qt<XKB_KEY_XF86KbdLightOnOff, Qt::Key_KeyboardLightOnOff>,
281 Xkb2Qt<XKB_KEY_XF86KbdBrightnessUp, Qt::Key_KeyboardBrightnessUp>,
282 Xkb2Qt<XKB_KEY_XF86KbdBrightnessDown, Qt::Key_KeyboardBrightnessDown>,
283 Xkb2Qt<XKB_KEY_XF86PowerOff, Qt::Key_PowerOff>,
284 Xkb2Qt<XKB_KEY_XF86WakeUp, Qt::Key_WakeUp>,
285 Xkb2Qt<XKB_KEY_XF86Eject, Qt::Key_Eject>,
286 Xkb2Qt<XKB_KEY_XF86ScreenSaver, Qt::Key_ScreenSaver>,
287 Xkb2Qt<XKB_KEY_XF86WWW, Qt::Key_WWW>,
288 Xkb2Qt<XKB_KEY_XF86Sleep, Qt::Key_Sleep>,
289 Xkb2Qt<XKB_KEY_XF86LightBulb, Qt::Key_LightBulb>,
290 Xkb2Qt<XKB_KEY_XF86Shop, Qt::Key_Shop>,
291 Xkb2Qt<XKB_KEY_XF86History, Qt::Key_History>,
292 Xkb2Qt<XKB_KEY_XF86AddFavorite, Qt::Key_AddFavorite>,
293 Xkb2Qt<XKB_KEY_XF86HotLinks, Qt::Key_HotLinks>,
294 Xkb2Qt<XKB_KEY_XF86BrightnessAdjust, Qt::Key_BrightnessAdjust>,
295 Xkb2Qt<XKB_KEY_XF86Finance, Qt::Key_Finance>,
296 Xkb2Qt<XKB_KEY_XF86Community, Qt::Key_Community>,
297 Xkb2Qt<XKB_KEY_XF86AudioRewind, Qt::Key_AudioRewind>,
298 Xkb2Qt<XKB_KEY_XF86BackForward, Qt::Key_BackForward>,
299 Xkb2Qt<XKB_KEY_XF86ApplicationLeft, Qt::Key_ApplicationLeft>,
300 Xkb2Qt<XKB_KEY_XF86ApplicationRight, Qt::Key_ApplicationRight>,
301#ifdef XKB_KEY_XF86Assistant
302 Xkb2Qt<XKB_KEY_XF86Assistant, Qt::Key_Assistant>,
303#endif
304 Xkb2Qt<XKB_KEY_XF86Book, Qt::Key_Book>,
305 Xkb2Qt<XKB_KEY_XF86CD, Qt::Key_CD>,
306 Xkb2Qt<XKB_KEY_XF86Calculater, Qt::Key_Calculator>,
307 Xkb2Qt<XKB_KEY_XF86Calculator, Qt::Key_Calculator>,
308 Xkb2Qt<XKB_KEY_XF86Clear, Qt::Key_Clear>,
309 Xkb2Qt<XKB_KEY_XF86ClearGrab, Qt::Key_ClearGrab>,
310 Xkb2Qt<XKB_KEY_XF86Close, Qt::Key_Close>,
311 Xkb2Qt<XKB_KEY_XF86Copy, Qt::Key_Copy>,
312 Xkb2Qt<XKB_KEY_XF86Cut, Qt::Key_Cut>,
313 Xkb2Qt<XKB_KEY_XF86Display, Qt::Key_Display>,
314 Xkb2Qt<XKB_KEY_XF86DOS, Qt::Key_DOS>,
315 Xkb2Qt<XKB_KEY_XF86Documents, Qt::Key_Documents>,
316 Xkb2Qt<XKB_KEY_XF86Excel, Qt::Key_Excel>,
317 Xkb2Qt<XKB_KEY_XF86Explorer, Qt::Key_Explorer>,
318 Xkb2Qt<XKB_KEY_XF86Game, Qt::Key_Game>,
319 Xkb2Qt<XKB_KEY_XF86Go, Qt::Key_Go>,
320 Xkb2Qt<XKB_KEY_XF86iTouch, Qt::Key_iTouch>,
321 Xkb2Qt<XKB_KEY_XF86LogOff, Qt::Key_LogOff>,
322 Xkb2Qt<XKB_KEY_XF86Market, Qt::Key_Market>,
323 Xkb2Qt<XKB_KEY_XF86Meeting, Qt::Key_Meeting>,
324 Xkb2Qt<XKB_KEY_XF86MenuKB, Qt::Key_MenuKB>,
325 Xkb2Qt<XKB_KEY_XF86MenuPB, Qt::Key_MenuPB>,
326 Xkb2Qt<XKB_KEY_XF86MySites, Qt::Key_MySites>,
327 Xkb2Qt<XKB_KEY_XF86New, Qt::Key_New>,
328 Xkb2Qt<XKB_KEY_XF86News, Qt::Key_News>,
329 Xkb2Qt<XKB_KEY_XF86OfficeHome, Qt::Key_OfficeHome>,
330 Xkb2Qt<XKB_KEY_XF86Open, Qt::Key_Open>,
331 Xkb2Qt<XKB_KEY_XF86Option, Qt::Key_Option>,
332 Xkb2Qt<XKB_KEY_XF86Paste, Qt::Key_Paste>,
333#ifdef XKB_KEY_XF86PerformanceMode
334 Xkb2Qt<XKB_KEY_XF86PerformanceMode, Qt::Key_PerformanceMode>,
335#endif
336 Xkb2Qt<XKB_KEY_XF86Phone, Qt::Key_Phone>,
337#ifdef XKB_KEY_XF86PickupPhone
338 Xkb2Qt<XKB_KEY_XF86PickupPhone, Qt::Key_Call>,
339#endif
340#ifdef XKB_KEY_XF86HangupPhone
341 Xkb2Qt<XKB_KEY_XF86HangupPhone, Qt::Key_Hangup>,
342#endif
343 Xkb2Qt<XKB_KEY_XF86Reply, Qt::Key_Reply>,
344 Xkb2Qt<XKB_KEY_XF86Reload, Qt::Key_Reload>,
345 Xkb2Qt<XKB_KEY_XF86RotateWindows, Qt::Key_RotateWindows>,
346 Xkb2Qt<XKB_KEY_XF86RotationPB, Qt::Key_RotationPB>,
347 Xkb2Qt<XKB_KEY_XF86RotationKB, Qt::Key_RotationKB>,
348 Xkb2Qt<XKB_KEY_XF86Save, Qt::Key_Save>,
349#ifdef XKB_KEY_XF86SelectiveScreenshot
350 Xkb2Qt<XKB_KEY_XF86SelectiveScreenshot, Qt::Key_SelectiveScreenshot>,
351#endif
352 Xkb2Qt<XKB_KEY_XF86Send, Qt::Key_Send>,
353 Xkb2Qt<XKB_KEY_XF86Spell, Qt::Key_Spell>,
354 Xkb2Qt<XKB_KEY_XF86SplitScreen, Qt::Key_SplitScreen>,
355 Xkb2Qt<XKB_KEY_XF86Support, Qt::Key_Support>,
356 Xkb2Qt<XKB_KEY_XF86TaskPane, Qt::Key_TaskPane>,
357 Xkb2Qt<XKB_KEY_XF86Terminal, Qt::Key_Terminal>,
358 Xkb2Qt<XKB_KEY_XF86Tools, Qt::Key_Tools>,
359 Xkb2Qt<XKB_KEY_XF86Travel, Qt::Key_Travel>,
360 Xkb2Qt<XKB_KEY_XF86Video, Qt::Key_Video>,
361 Xkb2Qt<XKB_KEY_XF86Word, Qt::Key_Word>,
362 Xkb2Qt<XKB_KEY_XF86Xfer, Qt::Key_Xfer>,
363 Xkb2Qt<XKB_KEY_XF86ZoomIn, Qt::Key_ZoomIn>,
364 Xkb2Qt<XKB_KEY_XF86ZoomOut, Qt::Key_ZoomOut>,
365 Xkb2Qt<XKB_KEY_XF86Away, Qt::Key_Away>,
366 Xkb2Qt<XKB_KEY_XF86Messenger, Qt::Key_Messenger>,
367 Xkb2Qt<XKB_KEY_XF86WebCam, Qt::Key_WebCam>,
368 Xkb2Qt<XKB_KEY_XF86MailForward, Qt::Key_MailForward>,
369 Xkb2Qt<XKB_KEY_XF86Pictures, Qt::Key_Pictures>,
370 Xkb2Qt<XKB_KEY_XF86Music, Qt::Key_Music>,
371 Xkb2Qt<XKB_KEY_XF86Battery, Qt::Key_Battery>,
372 Xkb2Qt<XKB_KEY_XF86Bluetooth, Qt::Key_Bluetooth>,
373 Xkb2Qt<XKB_KEY_XF86WLAN, Qt::Key_WLAN>,
374 Xkb2Qt<XKB_KEY_XF86UWB, Qt::Key_UWB>,
375 Xkb2Qt<XKB_KEY_XF86AudioForward, Qt::Key_AudioForward>,
376 Xkb2Qt<XKB_KEY_XF86AudioRepeat, Qt::Key_AudioRepeat>,
377 Xkb2Qt<XKB_KEY_XF86AudioRandomPlay, Qt::Key_AudioRandomPlay>,
378 Xkb2Qt<XKB_KEY_XF86Subtitle, Qt::Key_Subtitle>,
379 Xkb2Qt<XKB_KEY_XF86AudioCycleTrack, Qt::Key_AudioCycleTrack>,
380 Xkb2Qt<XKB_KEY_XF86Time, Qt::Key_Time>,
381 Xkb2Qt<XKB_KEY_XF86Select, Qt::Key_Select>,
382 Xkb2Qt<XKB_KEY_XF86View, Qt::Key_View>,
383 Xkb2Qt<XKB_KEY_XF86TopMenu, Qt::Key_TopMenu>,
384 Xkb2Qt<XKB_KEY_XF86Red, Qt::Key_Red>,
385 Xkb2Qt<XKB_KEY_XF86Green, Qt::Key_Green>,
386 Xkb2Qt<XKB_KEY_XF86Yellow, Qt::Key_Yellow>,
387 Xkb2Qt<XKB_KEY_XF86Blue, Qt::Key_Blue>,
388 Xkb2Qt<XKB_KEY_XF86Bluetooth, Qt::Key_Bluetooth>,
389 Xkb2Qt<XKB_KEY_XF86Suspend, Qt::Key_Suspend>,
390 Xkb2Qt<XKB_KEY_XF86Hibernate, Qt::Key_Hibernate>,
391 Xkb2Qt<XKB_KEY_XF86TouchpadToggle, Qt::Key_TouchpadToggle>,
392 Xkb2Qt<XKB_KEY_XF86TouchpadOn, Qt::Key_TouchpadOn>,
393 Xkb2Qt<XKB_KEY_XF86TouchpadOff, Qt::Key_TouchpadOff>,
394 Xkb2Qt<XKB_KEY_XF86AudioMicMute, Qt::Key_MicMute>,
395 Xkb2Qt<XKB_KEY_XF86Keyboard, Qt::Key_Keyboard>,
396 Xkb2Qt<XKB_KEY_XF86Launch0, Qt::Key_Launch0>,
397 Xkb2Qt<XKB_KEY_XF86Launch1, Qt::Key_Launch1>,
398 Xkb2Qt<XKB_KEY_XF86Launch2, Qt::Key_Launch2>,
399 Xkb2Qt<XKB_KEY_XF86Launch3, Qt::Key_Launch3>,
400 Xkb2Qt<XKB_KEY_XF86Launch4, Qt::Key_Launch4>,
401 Xkb2Qt<XKB_KEY_XF86Launch5, Qt::Key_Launch5>,
402 Xkb2Qt<XKB_KEY_XF86Launch6, Qt::Key_Launch6>,
403 Xkb2Qt<XKB_KEY_XF86Launch7, Qt::Key_Launch7>,
404 Xkb2Qt<XKB_KEY_XF86Launch8, Qt::Key_Launch8>,
405 Xkb2Qt<XKB_KEY_XF86Launch9, Qt::Key_Launch9>,
406 Xkb2Qt<XKB_KEY_XF86LaunchA, Qt::Key_LaunchA>,
407 Xkb2Qt<XKB_KEY_XF86LaunchB, Qt::Key_LaunchB>,
408 Xkb2Qt<XKB_KEY_XF86LaunchC, Qt::Key_LaunchC>,
409 Xkb2Qt<XKB_KEY_XF86LaunchD, Qt::Key_LaunchD>,
410 Xkb2Qt<XKB_KEY_XF86LaunchE, Qt::Key_LaunchE>,
411 Xkb2Qt<XKB_KEY_XF86LaunchF, Qt::Key_LaunchF>
412 >::Data{}
413);
414
415xkb_keysym_t QXkbCommon::qxkbcommon_xkb_keysym_to_upper(xkb_keysym_t ks)
416{
417 xkb_keysym_t lower, upper;
418
419 xkbcommon_XConvertCase(ks, &lower, &upper);
420
421 return upper;
422}
423
424QString QXkbCommon::lookupString(struct xkb_state *state, xkb_keycode_t code)
425{
426 QVarLengthArray<char, 32> chars(32);
427 const int size = xkb_state_key_get_utf8(state, code, chars.data(), chars.size());
428 if (Q_UNLIKELY(size + 1 > chars.size())) { // +1 for NUL
429 chars.resize(size + 1);
430 xkb_state_key_get_utf8(state, code, chars.data(), chars.size());
431 }
432 return QString::fromUtf8(chars.constData(), size);
433}
434
435QString QXkbCommon::lookupStringNoKeysymTransformations(xkb_keysym_t keysym)
436{
437 QVarLengthArray<char, 32> chars(32);
438 const int size = xkb_keysym_to_utf8(keysym, chars.data(), chars.size());
439 if (size == 0)
440 return QString(); // the keysym does not have a Unicode representation
441
442 if (Q_UNLIKELY(size > chars.size())) {
443 chars.resize(size);
444 xkb_keysym_to_utf8(keysym, chars.data(), chars.size());
445 }
446 return QString::fromUtf8(chars.constData(), size - 1);
447}
448
449QList<xkb_keysym_t> QXkbCommon::toKeysym(QKeyEvent *event)
450{
451 QList<xkb_keysym_t> keysyms;
452 int qtKey = event->key();
453
454 if (qtKey >= Qt::Key_F1 && qtKey <= Qt::Key_F35) {
455 keysyms.append(XKB_KEY_F1 + (qtKey - Qt::Key_F1));
456 } else if (event->modifiers() & Qt::KeypadModifier) {
457 if (qtKey >= Qt::Key_0 && qtKey <= Qt::Key_9)
458 keysyms.append(XKB_KEY_KP_0 + (qtKey - Qt::Key_0));
459 } else if (isLatin1(qtKey) && event->text().isUpper()) {
460 keysyms.append(qtKey);
461 }
462
463 if (!keysyms.isEmpty())
464 return keysyms;
465
466 // check if we have a direct mapping
467 auto it = std::find_if(KeyTbl.cbegin(), KeyTbl.cend(), [&qtKey](xkb2qt_t elem) {
468 return elem.qt == static_cast<uint>(qtKey);
469 });
470 if (it != KeyTbl.end()) {
471 keysyms.append(it->xkb);
472 return keysyms;
473 }
474
475 QList<uint> ucs4;
476 if (event->text().isEmpty())
477 ucs4.append(qtKey);
478 else
479 ucs4 = event->text().toUcs4();
480
481 // From libxkbcommon keysym-utf.c:
482 // "We allow to represent any UCS character in the range U-00000000 to
483 // U-00FFFFFF by a keysym value in the range 0x01000000 to 0x01ffffff."
484 for (uint utf32 : std::as_const(ucs4))
485 keysyms.append(utf32 | 0x01000000);
486
487 return keysyms;
488}
489
490int QXkbCommon::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers)
491{
492 return keysymToQtKey(keysym, modifiers, nullptr, 0);
493}
494
495int QXkbCommon::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers,
496 xkb_state *state, xkb_keycode_t code,
497 bool superAsMeta, bool hyperAsMeta)
498{
499 // Note 1: All standard key sequences on linux (as defined in platform theme)
500 // that use a latin character also contain a control modifier, which is why
501 // checking for Qt::ControlModifier is sufficient here. It is possible to
502 // override QPlatformTheme::keyBindings() and provide custom sequences for
503 // QKeySequence::StandardKey. Custom sequences probably should respect this
504 // convention (alternatively, we could test against other modifiers here).
505 // Note 2: The possibleKeys() shorcut mechanism is not affected by this value
506 // adjustment and does its own thing.
507 if (modifiers & Qt::ControlModifier) {
508 // With standard shortcuts we should prefer a latin character, this is
509 // for checks like "some qkeyevent == QKeySequence::Copy" to work even
510 // when using for example 'russian' keyboard layout.
511 if (!QXkbCommon::isLatin1(keysym)) {
512 xkb_keysym_t latinKeysym = QXkbCommon::lookupLatinKeysym(state, code);
513 if (latinKeysym != XKB_KEY_NoSymbol)
514 keysym = latinKeysym;
515 }
516 }
517
518 return keysymToQtKey_internal(keysym, modifiers, state, code, superAsMeta, hyperAsMeta);
519}
520
521static int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers,
522 xkb_state *state, xkb_keycode_t code,
523 bool superAsMeta, bool hyperAsMeta)
524{
525 int qtKey = 0;
526
527 // lookup from direct mapping
528 if (keysym >= XKB_KEY_F1 && keysym <= XKB_KEY_F35) {
529 // function keys
530 qtKey = Qt::Key_F1 + (keysym - XKB_KEY_F1);
531 } else if (keysym >= XKB_KEY_KP_0 && keysym <= XKB_KEY_KP_9) {
532 // numeric keypad keys
533 qtKey = Qt::Key_0 + (keysym - XKB_KEY_KP_0);
534 } else if (QXkbCommon::isLatin1(keysym)) {
535 // Most Qt::Key values are determined by their upper-case version,
536 // where this is in the Latin-1 repertoire. So start with that:
537 qtKey = QXkbCommon::qxkbcommon_xkb_keysym_to_upper(keysym);
538 // However, Key_mu and Key_ydiaeresis are U+00B5 MICRO SIGN and
539 // U+00FF LATIN SMALL LETTER Y WITH DIAERESIS, both lower-case,
540 // with upper-case forms outside Latin-1, so use them as they are
541 // since they're the Qt::Key values.
542 if (!QXkbCommon::isLatin1(qtKey))
543 qtKey = keysym;
544 } else {
545 // check if we have a direct mapping
546 xkb2qt_t searchKey{keysym, 0};
547 auto it = std::lower_bound(KeyTbl.cbegin(), KeyTbl.cend(), searchKey);
548 if (it != KeyTbl.end() && !(searchKey < *it))
549 qtKey = it->qt;
550
551 // translate Super/Hyper keys to Meta if we're using them as the MetaModifier
552 if (superAsMeta && (qtKey == Qt::Key_Super_L || qtKey == Qt::Key_Super_R))
553 qtKey = Qt::Key_Meta;
554 if (hyperAsMeta && (qtKey == Qt::Key_Hyper_L || qtKey == Qt::Key_Hyper_R))
555 qtKey = Qt::Key_Meta;
556 }
557
558 if (qtKey)
559 return qtKey;
560
561 // lookup from unicode
562 QString text;
563 if (!state || modifiers & Qt::ControlModifier) {
564 // Control modifier changes the text to ASCII control character, therefore we
565 // can't use this text to map keysym to a qt key. We can use the same keysym
566 // (it is not affectd by transformation) to obtain untransformed text. For details
567 // see "Appendix A. Default Symbol Transformations" in the XKB specification.
568 text = QXkbCommon::lookupStringNoKeysymTransformations(keysym);
569 } else {
570 text = QXkbCommon::lookupString(state, code);
571 }
572 if (!text.isEmpty()) {
573 if (text.unicode()->isDigit()) {
574 // Ensures that also non-latin digits are mapped to corresponding qt keys,
575 // e.g CTRL + ۲ (arabic two), is mapped to CTRL + Qt::Key_2.
576 qtKey = Qt::Key_0 + text.unicode()->digitValue();
577 } else {
578 text = text.toUpper();
579 QStringIterator i(text);
580 qtKey = i.next(0);
581 }
582 }
583
584 return qtKey;
585}
586
587Qt::KeyboardModifiers QXkbCommon::modifiers(struct xkb_state *state, xkb_keysym_t keysym)
588{
589 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
590
591 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0)
592 modifiers |= Qt::ControlModifier;
593 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0)
594 modifiers |= Qt::AltModifier;
595 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE) > 0)
596 modifiers |= Qt::ShiftModifier;
597 if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_EFFECTIVE) > 0)
598 modifiers |= Qt::MetaModifier;
599
600 if (isKeypad(keysym))
601 modifiers |= Qt::KeypadModifier;
602
603 return modifiers;
604}
605
606// Possible modifier states.
607static const Qt::KeyboardModifiers ModsTbl[] = {
608 Qt::NoModifier, // 0
609 Qt::ShiftModifier, // 1
610 Qt::ControlModifier, // 2
611 Qt::ControlModifier | Qt::ShiftModifier, // 3
612 Qt::AltModifier, // 4
613 Qt::AltModifier | Qt::ShiftModifier, // 5
614 Qt::AltModifier | Qt::ControlModifier, // 6
615 Qt::AltModifier | Qt::ShiftModifier | Qt::ControlModifier, // 7
616 Qt::NoModifier // Fall-back to raw Key_*, for non-latin1 kb layouts
617};
618
619/*
620 Compatibility until all sub modules have transitioned to new API below
621*/
622QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event,
623 bool superAsMeta, bool hyperAsMeta)
624{
625 QList<int> result;
626 auto keyCombinations = possibleKeyCombinations(state, event, superAsMeta, hyperAsMeta);
627 for (auto keyCombination : keyCombinations)
628 result << keyCombination.toCombined();
629
630 return result;
631}
632
633QList<QKeyCombination> QXkbCommon::possibleKeyCombinations(xkb_state *state, const QKeyEvent *event,
634 bool superAsMeta, bool hyperAsMeta)
635{
636 QList<QKeyCombination> result;
637 quint32 keycode = event->nativeScanCode();
638 if (!keycode)
639 return result;
640
641 Qt::KeyboardModifiers modifiers = event->modifiers();
642 xkb_keymap *keymap = xkb_state_get_keymap(state);
643 // turn off the modifier bits which doesn't participate in shortcuts
644 Qt::KeyboardModifiers notNeeded = Qt::KeypadModifier | Qt::GroupSwitchModifier;
645 modifiers &= ~notNeeded;
646 // create a fresh kb state and test against the relevant modifier combinations
647 ScopedXKBState scopedXkbQueryState(xkb_state_new(keymap));
648 xkb_state *queryState = scopedXkbQueryState.get();
649 if (!queryState) {
650 qCWarning(lcQpaKeyMapper) << Q_FUNC_INFO << "failed to compile xkb keymap";
651 return result;
652 }
653 // get kb state from the master state and update the temporary state
654 xkb_layout_index_t lockedLayout = xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_LOCKED);
655 xkb_mod_mask_t latchedMods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
656 xkb_mod_mask_t lockedMods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
657 xkb_mod_mask_t depressedMods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
658 xkb_state_update_mask(queryState, depressedMods, latchedMods, lockedMods, 0, 0, lockedLayout);
659 // handle shortcuts for level three and above
660 xkb_layout_index_t layoutIndex = xkb_state_key_get_layout(queryState, keycode);
661 xkb_level_index_t levelIndex = 0;
662 if (layoutIndex != XKB_LAYOUT_INVALID) {
663 levelIndex = xkb_state_key_get_level(queryState, keycode, layoutIndex);
664 if (levelIndex == XKB_LEVEL_INVALID)
665 levelIndex = 0;
666 }
667 if (levelIndex <= 1)
668 xkb_state_update_mask(queryState, 0, latchedMods, lockedMods, 0, 0, lockedLayout);
669
670 xkb_keysym_t sym = xkb_state_key_get_one_sym(queryState, keycode);
671 if (sym == XKB_KEY_NoSymbol)
672 return result;
673
674 int baseQtKey = keysymToQtKey_internal(sym, modifiers, queryState, keycode, superAsMeta, hyperAsMeta);
675 if (baseQtKey)
676 result += QKeyCombination::fromCombined(baseQtKey + int(modifiers));
677
678 xkb_mod_index_t shiftMod = xkb_keymap_mod_get_index(keymap, "Shift");
679 xkb_mod_index_t altMod = xkb_keymap_mod_get_index(keymap, "Alt");
680 xkb_mod_index_t controlMod = xkb_keymap_mod_get_index(keymap, "Control");
681 xkb_mod_index_t metaMod = xkb_keymap_mod_get_index(keymap, "Meta");
682
683 Q_ASSERT(shiftMod < 32);
684 Q_ASSERT(altMod < 32);
685 Q_ASSERT(controlMod < 32);
686
687 xkb_mod_mask_t depressed;
688 int qtKey = 0;
689 // obtain a list of possible shortcuts for the given key event
690 for (uint i = 1; i < sizeof(ModsTbl) / sizeof(*ModsTbl) ; ++i) {
691 Qt::KeyboardModifiers neededMods = ModsTbl[i];
692 if ((modifiers & neededMods) == neededMods) {
693 if (i == 8) {
694 if (isLatin1(baseQtKey))
695 continue;
696 // add a latin key as a fall back key
697 sym = lookupLatinKeysym(state, keycode);
698 } else {
699 depressed = 0;
700 if (neededMods & Qt::AltModifier)
701 depressed |= (1 << altMod);
702 if (neededMods & Qt::ShiftModifier)
703 depressed |= (1 << shiftMod);
704 if (neededMods & Qt::ControlModifier)
705 depressed |= (1 << controlMod);
706 if (metaMod < 32 && neededMods & Qt::MetaModifier)
707 depressed |= (1 << metaMod);
708 xkb_state_update_mask(queryState, depressed, latchedMods, lockedMods, 0, 0, lockedLayout);
709 sym = xkb_state_key_get_one_sym(queryState, keycode);
710 }
711 if (sym == XKB_KEY_NoSymbol)
712 continue;
713
714 Qt::KeyboardModifiers mods = modifiers & ~neededMods;
715 qtKey = keysymToQtKey_internal(sym, mods, queryState, keycode, superAsMeta, hyperAsMeta);
716 if (!qtKey || qtKey == baseQtKey)
717 continue;
718
719 // catch only more specific shortcuts, i.e. Ctrl+Shift+= also generates Ctrl++ and +,
720 // but Ctrl++ is more specific than +, so we should skip the last one
721 bool ambiguous = false;
722 for (auto keyCombination : std::as_const(result)) {
723 if (keyCombination.key() == qtKey
724 && (keyCombination.keyboardModifiers() & mods) == mods) {
725 ambiguous = true;
726 break;
727 }
728 }
729 if (ambiguous)
730 continue;
731
732 result += QKeyCombination::fromCombined(qtKey + int(mods));
733 }
734 }
735
736 return result;
737}
738
739void QXkbCommon::verifyHasLatinLayout(xkb_keymap *keymap)
740{
741 const xkb_layout_index_t layoutCount = xkb_keymap_num_layouts(keymap);
742 const xkb_keycode_t minKeycode = xkb_keymap_min_keycode(keymap);
743 const xkb_keycode_t maxKeycode = xkb_keymap_max_keycode(keymap);
744
745 const xkb_keysym_t *keysyms = nullptr;
746 int nrLatinKeys = 0;
747 for (xkb_layout_index_t layout = 0; layout < layoutCount; ++layout) {
748 for (xkb_keycode_t code = minKeycode; code < maxKeycode; ++code) {
749 xkb_keymap_key_get_syms_by_level(keymap, code, layout, 0, &keysyms);
750 if (keysyms && isLatin1(keysyms[0]))
751 nrLatinKeys++;
752 if (nrLatinKeys > 10) // arbitrarily chosen threshold
753 return;
754 }
755 }
756 // This means that lookupLatinKeysym() will not find anything and latin
757 // key shortcuts might not work. This is a bug in the affected desktop
758 // environment. Usually can be solved via system settings by adding e.g. 'us'
759 // layout to the list of selected layouts, or by using command line, "setxkbmap
760 // -layout rus,en". The position of latin key based layout in the list of the
761 // selected layouts is irrelevant. Properly functioning desktop environments
762 // handle this behind the scenes, even if no latin key based layout has been
763 // explicitly listed in the selected layouts.
764 qCDebug(lcQpaKeyMapper, "no keyboard layouts with latin keys present");
765}
766
767xkb_keysym_t QXkbCommon::lookupLatinKeysym(xkb_state *state, xkb_keycode_t keycode)
768{
769 xkb_layout_index_t layout;
770 xkb_keysym_t sym = XKB_KEY_NoSymbol;
771 if (!state)
772 return sym;
773 xkb_keymap *keymap = xkb_state_get_keymap(state);
774 const xkb_layout_index_t layoutCount = xkb_keymap_num_layouts_for_key(keymap, keycode);
775 const xkb_layout_index_t currentLayout = xkb_state_key_get_layout(state, keycode);
776 // Look at user layouts in the order in which they are defined in system
777 // settings to find a latin keysym.
778 for (layout = 0; layout < layoutCount; ++layout) {
779 if (layout == currentLayout)
780 continue;
781 const xkb_keysym_t *syms = nullptr;
782 xkb_level_index_t level = xkb_state_key_get_level(state, keycode, layout);
783 if (xkb_keymap_key_get_syms_by_level(keymap, keycode, layout, level, &syms) != 1)
784 continue;
785 if (isLatin1(syms[0])) {
786 sym = syms[0];
787 break;
788 }
789 }
790
791 if (sym == XKB_KEY_NoSymbol)
792 return sym;
793
794 xkb_mod_mask_t latchedMods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
795 xkb_mod_mask_t lockedMods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
796
797 // Check for uniqueness, consider the following setup:
798 // setxkbmap -layout us,ru,us -variant dvorak,, -option 'grp:ctrl_alt_toggle' (set 'ru' as active).
799 // In this setup, the user would expect to trigger a ctrl+q shortcut by pressing ctrl+<physical x key>,
800 // because "US dvorak" is higher up in the layout settings list. This check verifies that an obtained
801 // 'sym' can not be acquired by any other layout higher up in the user's layout list. If it can be acquired
802 // then the obtained key is not unique. This prevents ctrl+<physical q key> from generating a ctrl+q
803 // shortcut in the above described setup. We don't want ctrl+<physical x key> and ctrl+<physical q key> to
804 // generate the same shortcut event in this case.
805 const xkb_keycode_t minKeycode = xkb_keymap_min_keycode(keymap);
806 const xkb_keycode_t maxKeycode = xkb_keymap_max_keycode(keymap);
807 ScopedXKBState queryState(xkb_state_new(keymap));
808 for (xkb_layout_index_t prevLayout = 0; prevLayout < layout; ++prevLayout) {
809 xkb_state_update_mask(queryState.get(), 0, latchedMods, lockedMods, 0, 0, prevLayout);
810 for (xkb_keycode_t code = minKeycode; code < maxKeycode; ++code) {
811 xkb_keysym_t prevSym = xkb_state_key_get_one_sym(queryState.get(), code);
812 if (prevSym == sym) {
813 sym = XKB_KEY_NoSymbol;
814 break;
815 }
816 }
817 }
818
819 return sym;
820}
821
822void QXkbCommon::setXkbContext(QPlatformInputContext *inputContext, struct xkb_context *context)
823{
824 if (!inputContext || !context)
825 return;
826
827 const char *const inputContextClassName = "QComposeInputContext";
828 const char *const normalizedSignature = "setXkbContext(xkb_context*)";
829
830 if (inputContext->objectName() != QLatin1StringView(inputContextClassName))
831 return;
832
833 static const QMetaMethod setXkbContext = [&]() {
834 int methodIndex = inputContext->metaObject()->indexOfMethod(normalizedSignature);
835 QMetaMethod method = inputContext->metaObject()->method(methodIndex);
836 Q_ASSERT(method.isValid());
837 if (!method.isValid())
838 qCWarning(lcQpaKeyMapper) << normalizedSignature << "not found on" << inputContextClassName;
839 return method;
840 }();
841
842 if (!setXkbContext.isValid())
843 return;
844
845 setXkbContext.invoke(inputContext, Qt::DirectConnection, Q_ARG(struct xkb_context*, context));
846}
847
848QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define XKB_MOD_NAME_ALT
#define XKB_MOD_NAME_SHIFT
#define XKB_MOD_NAME_LOGO
#define XKB_MOD_NAME_CTRL
static const Qt::KeyboardModifiers ModsTbl[]
static QT_BEGIN_NAMESPACE int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers, xkb_state *state, xkb_keycode_t code, bool superAsMeta, bool hyperAsMeta)
static constexpr const auto KeyTbl
xkb2qt_t Type
static constexpr Type data() noexcept
constexpr bool operator<=(const xkb2qt &that) const noexcept
unsigned int xkb
constexpr bool operator<(const xkb2qt &that) const noexcept
unsigned int qt