Debian Codegraph @

wayland/wayland-shell/dkeyboard.cpp

raw
1 // SPDX-FileCopyrightText: 2022 Uniontech Software Technology Co.,Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dkeyboard.h"
6
7 #include <private/qxkbcommon_p.h>
8 #include <private/qguiapplication_p.h>
9 #include <private/qwaylanddisplay_p.h>
10 #include <private/qwaylandintegration_p.h>
11 #include <private/qwaylandwindow_p.h>
12
13 #include <qpa/qwindowsysteminterface.h>
14 #include <qpa/qplatformintegration.h>
15 #include <qpa/qplatforminputcontext.h>
16 #include <qpa/qplatformcursor.h>
17
18 #ifndef QT_DEBUG
19 Q_LOGGING_CATEGORY(dkeyboard, "dkeyboard.wayland.plugin" , QtInfoMsg);
20 #else
21 Q_LOGGING_CATEGORY(dkeyboard, "dkeyboard.wayland.plugin");
22 #endif
23
24 //#if QT_CONFIG(xkbcommon)
25 static QXkbCommon::ScopedXKBKeymap mXkbKeymap;
26 static QXkbCommon::ScopedXKBState mXkbState;
27 static uint32_t mNativeModifiers = 0;
28
29 namespace QtWaylandClient {
30
31 // from qtwayland...
32 static void handleKey(QWindow *window, ulong timestamp, QEvent::Type type, int key, Qt::KeyboardModifiers modifiers,
33 quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers,
34 const QString &text, bool autorepeat = false, ushort count = 1)
35 {
36 QPlatformInputContext *inputContext = QGuiApplicationPrivate::platformIntegration()->inputContext();
37 bool filtered = false;
38
39 bool usingInputContextFromCompositor = true;
40 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
41 QWaylandDisplay *display = static_cast<QWaylandIntegration *>(QGuiApplicationPrivate::platformIntegration())->display();
42 usingInputContextFromCompositor = display && !display->usingInputContextFromCompositor();
43 #endif
44 if (inputContext && usingInputContextFromCompositor) {
45 QKeyEvent event(type, key, modifiers, nativeScanCode, nativeVirtualKey,
46 nativeModifiers, text, autorepeat, count);
47 event.setTimestamp(timestamp);
48 filtered = inputContext->filterEvent(&event);
49 }
50
51 if (!filtered) {
52 if (type == QEvent::KeyPress && key == Qt::Key_Menu) {
53 auto cursor = window->screen()->handle()->cursor();
54 if (cursor) {
55 const QPoint globalPos = cursor->pos();
56 const QPoint pos = window->mapFromGlobal(globalPos);
57 QWindowSystemInterface::handleContextMenuEvent(window, false, pos, globalPos, modifiers);
58 }
59 }
60
61 QWindowSystemInterface::handleExtendedKeyEvent(window, timestamp, type, key, modifiers,
62 nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorepeat, count);
63 }
64 }
65
66 static bool createDefaultKeymap()
67 {
68 auto ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
69 if (!ctx)
70 return false;
71
72 struct xkb_rule_names names;
73 names.rules = "evdev";
74 names.model = "pc105";
75 names.layout = "us";
76 names.variant = "";
77 names.options = "";
78
79 mXkbKeymap.reset(xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS));
80 if (mXkbKeymap)
81 mXkbState.reset(xkb_state_new(mXkbKeymap.get()));
82
83 if (!mXkbKeymap || !mXkbState) {
84 qCWarning(dkeyboard) << "failed to create default keymap";
85 return false;
86 }
87
88 return true;
89 }
90 // #endif //QT_CONFIG(xkbcommon)
91
92
93 DKeyboard::DKeyboard(QObject *parent)
94 : QObject(parent)
95 {
96
97 }
98
99 DKeyboard::~DKeyboard()
100 {
101
102 }
103
104 void DKeyboard::handleKeyEvent(quint32 key, DDEKeyboard::KeyState state, quint32 time)
105 {
106 QWaylandWindow *window = dynamic_cast<QWaylandWindow *>(parent());
107
108 if (!window || !window->window() || window->isActive())
109 return;
110
111 //#if QT_CONFIG(xkbcommon)
112 if ((!mXkbKeymap || !mXkbState) && !createDefaultKeymap())
113 return;
114
115 QEvent::Type type = state == DDEKeyboard::KeyState::Pressed ? QEvent::KeyPress : QEvent::KeyRelease;
116 // qCDebug(dkeyboard) << __func__ << " key " << key << " state " << int(state) << " time " << time;
117
118 auto code = key + 8; // map to wl_keyboard::keymap_format::keymap_format_xkb_v1
119 xkb_keysym_t sym = xkb_state_key_get_one_sym(mXkbState.get(), code);
120 Qt::KeyboardModifiers modifiers = QXkbCommon::modifiers(mXkbState.get());
121 QString text = QXkbCommon::lookupString(mXkbState.get(), code);
122
123 int qtkey = QXkbCommon::keysymToQtKey(sym, modifiers, mXkbState.get(), code);
124 qCDebug(dkeyboard) << __func__ << "type" << type << "qtkey" << qtkey <<
125 "mNativeModifiers" << mNativeModifiers <<
126 "modifiers" << modifiers << "text" << text;
127 handleKey(window->window(), time, type, qtkey, modifiers, code, sym, mNativeModifiers, text);
128 //#endif
129 }
130
131 void DKeyboard::handleModifiersChanged(quint32 depressed, quint32 latched, quint32 locked, quint32 group)
132 {
133 qCDebug(dkeyboard) << __func__ << " depressed " << depressed <<
134 " latched " << latched <<
135 " locked " << locked <<
136 " group " << group;
137 if (mXkbState)
138 xkb_state_update_mask(mXkbState.get(),
139 depressed, latched, locked,
140 0, 0, group);
141 mNativeModifiers = depressed | latched | locked;
142 }
143
144 }