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 plugins 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 QXCBKEYBOARD_H
41#define QXCBKEYBOARD_H
42
43#include "qxcbobject.h"
44
45#include <xcb/xcb_keysyms.h>
46#if QT_CONFIG(xkb)
47#define explicit dont_use_cxx_explicit
48#include <xcb/xkb.h>
49#undef explicit
50#endif
51
52#include <xkbcommon/xkbcommon.h>
53#include <QtXkbCommonSupport/private/qxkbcommon_p.h>
54
55#include <QEvent>
56
57QT_BEGIN_NAMESPACE
58
59class QXcbKeyboard : public QXcbObject
60{
61public:
62 QXcbKeyboard(QXcbConnection *connection);
63
64 ~QXcbKeyboard();
65
66 void initialize();
67 void selectEvents();
68
69 void handleKeyPressEvent(const xcb_key_press_event_t *event);
70 void handleKeyReleaseEvent(const xcb_key_release_event_t *event);
71
72 Qt::KeyboardModifiers translateModifiers(int s) const;
73 void updateKeymap(xcb_mapping_notify_event_t *event);
74 void updateKeymap();
75 QList<int> possibleKeys(const QKeyEvent *event) const;
76
77 // when XKEYBOARD not present on the X server
78 void updateXKBMods();
79 xkb_mod_mask_t xkbModMask(quint16 state);
80 void updateXKBStateFromCore(quint16 state);
81#if QT_CONFIG(xcb_xinput)
82 void updateXKBStateFromXI(void *modInfo, void *groupInfo);
83#endif
84#if QT_CONFIG(xkb)
85 // when XKEYBOARD is present on the X server
86 int coreDeviceId() const { return core_device_id; }
87 void updateXKBState(xcb_xkb_state_notify_event_t *state);
88#endif
89 void handleStateChanges(xkb_state_component changedComponents);
90
91protected:
92 void handleKeyEvent(xcb_window_t sourceWindow, QEvent::Type type, xcb_keycode_t code,
93 quint16 state, xcb_timestamp_t time, bool fromSendEvent);
94
95 void resolveMaskConflicts();
96
97 typedef QMap<xcb_keysym_t, int> KeysymModifierMap;
98 struct xkb_keymap *keymapFromCore(const KeysymModifierMap &keysymMods);
99
100 // when XKEYBOARD not present on the X server
101 void updateModifiers(const KeysymModifierMap &keysymMods);
102 KeysymModifierMap keysymsToModifiers();
103 // when XKEYBOARD is present on the X server
104 void updateVModMapping();
105 void updateVModToRModMapping();
106
107private:
108 bool m_config = false;
109 bool m_isAutoRepeat = false;
110 xcb_keycode_t m_autoRepeatCode = 0;
111
112 struct _mod_masks {
113 uint alt;
114 uint altgr;
115 uint meta;
116 uint super;
117 uint hyper;
118 };
119
120 _mod_masks rmod_masks;
121
122 // when XKEYBOARD not present on the X server
123 xcb_key_symbols_t *m_key_symbols = nullptr;
124 struct _xkb_mods {
125 xkb_mod_index_t shift;
126 xkb_mod_index_t lock;
127 xkb_mod_index_t control;
128 xkb_mod_index_t mod1;
129 xkb_mod_index_t mod2;
130 xkb_mod_index_t mod3;
131 xkb_mod_index_t mod4;
132 xkb_mod_index_t mod5;
133 };
134 _xkb_mods xkb_mods;
135#if QT_CONFIG(xkb)
136 // when XKEYBOARD is present on the X server
137 _mod_masks vmod_masks;
138 int core_device_id;
139#endif
140
141 QXkbCommon::ScopedXKBState m_xkbState;
142 QXkbCommon::ScopedXKBKeymap m_xkbKeymap;
143 QXkbCommon::ScopedXKBContext m_xkbContext;
144
145 bool m_superAsMeta = false;
146 bool m_hyperAsMeta = false;
147};
148
149QT_END_NAMESPACE
150
151#endif
152