Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/daemon/x11_helper.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2010 Andriy Rysin (rysin@kde.org) | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "x11_helper.h" | ||||
20 | #include "debug.h" | ||||
21 | #include "../xkb_rules.h" | ||||
22 | | ||||
23 | #define explicit explicit_is_keyword_in_cpp | ||||
24 | #include <xcb/xkb.h> | ||||
25 | #undef explicit | ||||
26 | | ||||
27 | #include <QX11Info> | ||||
28 | #include <QCoreApplication> | ||||
29 | #include <QDebug> | ||||
30 | | ||||
31 | #include <KLocalizedString> | ||||
32 | | ||||
33 | #include <X11/X.h> | ||||
34 | #include <X11/Xlib.h> | ||||
35 | #include <X11/Xatom.h> | ||||
36 | #include <X11/XKBlib.h> | ||||
37 | #include <X11/extensions/XKBrules.h> | ||||
38 | #include <xcb/xcb.h> | ||||
39 | #include <fixx11h.h> | ||||
40 | | ||||
41 | // more information about the limit https://bugs.freedesktop.org/show_bug.cgi?id=19501 | ||||
42 | const int X11Helper::MAX_GROUP_COUNT = 4; | ||||
43 | const int X11Helper::ARTIFICIAL_GROUP_LIMIT_COUNT = 8; | ||||
44 | | ||||
45 | const char X11Helper::LEFT_VARIANT_STR[] = "("; | ||||
46 | const char X11Helper::RIGHT_VARIANT_STR[] = ")"; | ||||
47 | | ||||
48 | bool X11Helper::xkbSupported(int* xkbOpcode) | ||||
49 | { | ||||
50 | if (!QX11Info::isPlatformX11()) { | ||||
51 | return false; | ||||
52 | } | ||||
53 | // Verify the Xlib has matching XKB extension. | ||||
54 | | ||||
55 | int major = XkbMajorVersion; | ||||
56 | int minor = XkbMinorVersion; | ||||
57 | | ||||
58 | if (!XkbLibraryVersion(&major, &minor)) | ||||
59 | { | ||||
60 | qCWarning(KCM_KEYBOARD) << "Xlib XKB extension " << major << '.' << minor << | ||||
61 | " != " << XkbMajorVersion << '.' << XkbMinorVersion; | ||||
62 | return false; | ||||
63 | } | ||||
64 | | ||||
65 | // Verify the X server has matching XKB extension. | ||||
66 | | ||||
67 | int opcode_rtrn; | ||||
68 | int error_rtrn; | ||||
69 | int xkb_opcode; | ||||
70 | if( ! XkbQueryExtension(QX11Info::display(), &opcode_rtrn, &xkb_opcode, &error_rtrn, &major, &minor)) { | ||||
71 | qCWarning(KCM_KEYBOARD) << "X server XKB extension " << major << '.' << minor << | ||||
72 | " != " << XkbMajorVersion << '.' << XkbMinorVersion; | ||||
73 | return false; | ||||
74 | } | ||||
75 | | ||||
76 | if( xkbOpcode != NULL ) { | ||||
77 | *xkbOpcode = xkb_opcode; | ||||
78 | } | ||||
79 | | ||||
80 | return true; | ||||
81 | } | ||||
82 | | ||||
83 | XEventNotifier::XEventNotifier(): | ||||
84 | xkbOpcode(-1) | ||||
85 | { | ||||
86 | if( QCoreApplication::instance() == NULL ) { | ||||
87 | qCWarning(KCM_KEYBOARD) << "Layout Widget won't work properly without QCoreApplication instance"; | ||||
88 | } | ||||
89 | } | ||||
90 | | ||||
91 | void XEventNotifier::start() | ||||
92 | { | ||||
93 | qCDebug(KCM_KEYBOARD) << "qCoreApp" << QCoreApplication::instance(); | ||||
94 | if( QCoreApplication::instance() != NULL && X11Helper::xkbSupported(&xkbOpcode) ) { | ||||
95 | registerForXkbEvents(QX11Info::display()); | ||||
96 | | ||||
97 | // start the event loop | ||||
98 | QCoreApplication::instance()->installNativeEventFilter(this); | ||||
99 | } | ||||
100 | } | ||||
101 | | ||||
102 | void XEventNotifier::stop() | ||||
103 | { | ||||
104 | if( QCoreApplication::instance() != NULL ) { | ||||
105 | //TODO: unregister | ||||
106 | // XEventNotifier::unregisterForXkbEvents(QX11Info::display()); | ||||
107 | | ||||
108 | // stop the event loop | ||||
109 | QCoreApplication::instance()->removeNativeEventFilter(this); | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | bool XEventNotifier::isXkbEvent(xcb_generic_event_t* event) | ||||
114 | { | ||||
115 | // qDebug() << "event response type:" << (event->response_type & ~0x80) << xkbOpcode << ((event->response_type & ~0x80) == xkbOpcode + XkbEventCode); | ||||
116 | return (event->response_type & ~0x80) == xkbOpcode + XkbEventCode; | ||||
117 | } | ||||
118 | | ||||
119 | bool XEventNotifier::processOtherEvents(xcb_generic_event_t* /*event*/) | ||||
120 | { | ||||
121 | return true; | ||||
122 | } | ||||
123 | | ||||
124 | bool XEventNotifier::processXkbEvents(xcb_generic_event_t* event) | ||||
125 | { | ||||
126 | _xkb_event *xkbevt = reinterpret_cast<_xkb_event *>(event); | ||||
127 | if( XEventNotifier::isGroupSwitchEvent(xkbevt) ) { | ||||
128 | // qDebug() << "group switch event"; | ||||
129 | emit(layoutChanged()); | ||||
130 | } | ||||
131 | else if( XEventNotifier::isLayoutSwitchEvent(xkbevt) ) { | ||||
132 | // qDebug() << "layout switch event"; | ||||
133 | emit(layoutMapChanged()); | ||||
134 | } | ||||
135 | return true; | ||||
136 | } | ||||
137 | | ||||
138 | bool XEventNotifier::nativeEventFilter(const QByteArray &eventType, void *message, long *) | ||||
139 | { | ||||
140 | // qDebug() << "event type:" << eventType; | ||||
141 | if (eventType == "xcb_generic_event_t") { | ||||
142 | xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message); | ||||
143 | if( isXkbEvent(ev) ) { | ||||
144 | processXkbEvents(ev); | ||||
145 | } | ||||
146 | else { | ||||
147 | processOtherEvents(ev); | ||||
148 | } | ||||
149 | } | ||||
150 | return false; | ||||
151 | } | ||||
152 | | ||||
153 | //bool XEventNotifier::x11Event(XEvent * event) | ||||
154 | //{ | ||||
155 | // // qApp->x11ProcessEvent ( event ); | ||||
156 | // if( isXkbEvent(event) ) { | ||||
157 | // processXkbEvents(event); | ||||
158 | // } | ||||
159 | // else { | ||||
160 | // processOtherEvents(event); | ||||
161 | // } | ||||
162 | // return QWidget::x11Event(event); | ||||
163 | //} | ||||
164 | | ||||
165 | bool XEventNotifier::isGroupSwitchEvent(_xkb_event* xkbEvent) | ||||
166 | { | ||||
167 | // XkbEvent *xkbEvent = (XkbEvent*) event; | ||||
168 | #define GROUP_CHANGE_MASK \ | ||||
169 | ( XkbGroupStateMask | XkbGroupBaseMask | XkbGroupLatchMask | XkbGroupLockMask ) | ||||
170 | | ||||
171 | return xkbEvent->any.xkbType == XkbStateNotify && (xkbEvent->state_notify.changed & GROUP_CHANGE_MASK); | ||||
172 | } | ||||
173 | | ||||
174 | bool XEventNotifier::isLayoutSwitchEvent(_xkb_event* xkbEvent) | ||||
175 | { | ||||
176 | // XkbEvent *xkbEvent = (XkbEvent*) event; | ||||
177 | | ||||
178 | return //( (xkbEvent->any.xkb_type == XkbMapNotify) && (xkbEvent->map.changed & XkbKeySymsMask) ) || | ||||
179 | /* || ( (xkbEvent->any.xkb_type == XkbNamesNotify) && (xkbEvent->names.changed & XkbGroupNamesMask) || )*/ | ||||
180 | (xkbEvent->any.xkbType == XkbNewKeyboardNotify); | ||||
181 | } | ||||
182 | | ||||
183 | int XEventNotifier::registerForXkbEvents(Display* display) | ||||
184 | { | ||||
185 | int eventMask = XkbNewKeyboardNotifyMask | XkbStateNotifyMask; | ||||
186 | if( ! XkbSelectEvents(display, XkbUseCoreKbd, eventMask, eventMask) ) { | ||||
187 | qCWarning(KCM_KEYBOARD) << "Couldn't select desired XKB events"; | ||||
188 | return false; | ||||
189 | } | ||||
190 | return true; | ||||
191 | } | ||||
192 | | ||||
193 | QString getDisplayText(const QString& layout, const QString& variant, const XkbRules* rules) | ||||
194 | { | ||||
195 | if( variant.isEmpty() ) | ||||
196 | return layout; | ||||
197 | if( rules == nullptr || rules->version == QLatin1String("1.0") ) | ||||
198 | return i18nc("layout - variant", "%1 - %2", layout, variant); | ||||
199 | return variant; | ||||
200 | } |