1 | // Copyright (C) 2020 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 | |
4 | #ifndef QXKBCOMMON_P_H |
5 | #define QXKBCOMMON_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtGui/qtguiglobal.h> |
19 | #include <QtCore/qstring.h> |
20 | #include <QtCore/qloggingcategory.h> |
21 | #include <QtCore/qlist.h> |
22 | #include <QtCore/private/qglobal_p.h> |
23 | |
24 | #include <xkbcommon/xkbcommon.h> |
25 | |
26 | #include <qpa/qplatformkeymapper.h> |
27 | |
28 | #include <memory> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class QEvent; |
33 | class QKeyEvent; |
34 | class QPlatformInputContext; |
35 | |
36 | class Q_GUI_EXPORT QXkbCommon |
37 | { |
38 | public: |
39 | static QString lookupString(struct xkb_state *state, xkb_keycode_t code); |
40 | static QString lookupStringNoKeysymTransformations(xkb_keysym_t keysym); |
41 | |
42 | static QList<xkb_keysym_t> toKeysym(QKeyEvent *event); |
43 | |
44 | static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers); |
45 | static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers, |
46 | xkb_state *state, xkb_keycode_t code, |
47 | bool superAsMeta = true, bool hyperAsMeta = true); |
48 | |
49 | // xkbcommon_* API is part of libxkbcommon internals, with modifications as |
50 | // described in the header of the implementation file. |
51 | static void xkbcommon_XConvertCase(xkb_keysym_t sym, xkb_keysym_t *lower, xkb_keysym_t *upper); |
52 | static xkb_keysym_t qxkbcommon_xkb_keysym_to_upper(xkb_keysym_t ks); |
53 | |
54 | static Qt::KeyboardModifiers modifiers(struct xkb_state *state, xkb_keysym_t keysym = XKB_KEY_VoidSymbol); |
55 | |
56 | static QList<int> possibleKeys(xkb_state *state, |
57 | const QKeyEvent *event, bool superAsMeta = false, bool hyperAsMeta = false); |
58 | static QList<QKeyCombination> possibleKeyCombinations(xkb_state *state, |
59 | const QKeyEvent *event, bool superAsMeta = false, bool hyperAsMeta = false); |
60 | |
61 | static void verifyHasLatinLayout(xkb_keymap *keymap); |
62 | static xkb_keysym_t lookupLatinKeysym(xkb_state *state, xkb_keycode_t keycode); |
63 | |
64 | static bool isLatin1(xkb_keysym_t sym) { |
65 | return sym >= 0x20 && sym <= 0xff; |
66 | } |
67 | static bool isKeypad(xkb_keysym_t sym) { |
68 | switch (sym) { |
69 | case XKB_KEY_KP_Space: |
70 | case XKB_KEY_KP_Tab: |
71 | case XKB_KEY_KP_Enter: |
72 | case XKB_KEY_KP_F1: |
73 | case XKB_KEY_KP_F2: |
74 | case XKB_KEY_KP_F3: |
75 | case XKB_KEY_KP_F4: |
76 | case XKB_KEY_KP_Home: |
77 | case XKB_KEY_KP_Left: |
78 | case XKB_KEY_KP_Up: |
79 | case XKB_KEY_KP_Right: |
80 | case XKB_KEY_KP_Down: |
81 | case XKB_KEY_KP_Prior: |
82 | case XKB_KEY_KP_Next: |
83 | case XKB_KEY_KP_End: |
84 | case XKB_KEY_KP_Begin: |
85 | case XKB_KEY_KP_Insert: |
86 | case XKB_KEY_KP_Delete: |
87 | case XKB_KEY_KP_Equal: |
88 | case XKB_KEY_KP_Multiply: |
89 | case XKB_KEY_KP_Add: |
90 | case XKB_KEY_KP_Separator: |
91 | case XKB_KEY_KP_Subtract: |
92 | case XKB_KEY_KP_Decimal: |
93 | case XKB_KEY_KP_Divide: |
94 | case XKB_KEY_KP_0: |
95 | case XKB_KEY_KP_1: |
96 | case XKB_KEY_KP_2: |
97 | case XKB_KEY_KP_3: |
98 | case XKB_KEY_KP_4: |
99 | case XKB_KEY_KP_5: |
100 | case XKB_KEY_KP_6: |
101 | case XKB_KEY_KP_7: |
102 | case XKB_KEY_KP_8: |
103 | case XKB_KEY_KP_9: |
104 | return true; |
105 | default: |
106 | return false; |
107 | } |
108 | } |
109 | |
110 | static void setXkbContext(QPlatformInputContext *inputContext, struct xkb_context *context); |
111 | |
112 | struct XKBStateDeleter { |
113 | void operator()(struct xkb_state *state) const { return xkb_state_unref(state); } |
114 | }; |
115 | struct XKBKeymapDeleter { |
116 | void operator()(struct xkb_keymap *keymap) const { return xkb_keymap_unref(keymap); } |
117 | }; |
118 | struct XKBContextDeleter { |
119 | void operator()(struct xkb_context *context) const { return xkb_context_unref(context); } |
120 | }; |
121 | using ScopedXKBState = std::unique_ptr<struct xkb_state, XKBStateDeleter>; |
122 | using ScopedXKBKeymap = std::unique_ptr<struct xkb_keymap, XKBKeymapDeleter>; |
123 | using ScopedXKBContext = std::unique_ptr<struct xkb_context, XKBContextDeleter>; |
124 | }; |
125 | |
126 | QT_END_NAMESPACE |
127 | |
128 | #endif // QXKBCOMMON_P_H |
129 | |