-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathxlibnotifications.cpp
156 lines (133 loc) · 4.87 KB
/
xlibnotifications.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (C) 2013 Alexander Mezin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "xlibnotifications.h"
#include <cstring>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <X11/extensions/XI.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/XInput2.h>
XlibNotifications::XlibNotifications(Display *display, int device)
: m_display(display), m_device(device)
{
m_connection = XGetXCBConnection(display);
m_notifier = new QSocketNotifier(xcb_get_file_descriptor(m_connection),
QSocketNotifier::Read, this);
xcb_query_extension_cookie_t inputExtCookie =
xcb_query_extension(m_connection, std::strlen(INAME), INAME);
QScopedPointer<xcb_query_extension_reply_t, QScopedPointerPodDeleter>
inputExt(xcb_query_extension_reply(m_connection, inputExtCookie, nullptr));
if (!inputExt) {
return;
}
m_inputOpcode = inputExt->major_opcode;
const xcb_setup_t *setup = xcb_get_setup(m_connection);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_screen_t *screen = iter.data;
m_inputWindow = xcb_generate_id(m_connection);
xcb_create_window(m_connection, 0, m_inputWindow, screen->root, 0, 0, 1, 1,
0, XCB_WINDOW_CLASS_INPUT_ONLY, 0, 0, nullptr);
xcb_flush(m_connection);
XIEventMask masks[2];
unsigned char touchpadMask[] = { 0, 0, 0, 0 };
masks[0].deviceid = device;
masks[0].mask = touchpadMask;
masks[0].mask_len = sizeof(touchpadMask);
XISetMask(touchpadMask, XI_PropertyEvent);
unsigned char allMask[] = { 0, 0, 0, 0 };
masks[1].deviceid = XIAllDevices;
masks[1].mask = allMask;
masks[1].mask_len = sizeof(allMask);
XISetMask(allMask, XI_HierarchyChanged);
XISelectEvents(display, XDefaultRootWindow(display), masks,
sizeof(masks) / sizeof(XIEventMask));
XFlush(display);
connect(m_notifier, SIGNAL(activated(int)), SLOT(processEvents()));
m_notifier->setEnabled(true);
}
void XlibNotifications::processEvents()
{
while (XPending(m_display)) {
XEvent event;
XNextEvent(m_display, &event);
processEvent(&event);
}
}
struct XEventDataDeleter
{
XEventDataDeleter(Display *display, XGenericEventCookie *cookie)
: m_display(display), m_cookie(cookie)
{
XGetEventData(m_display, m_cookie);
}
~XEventDataDeleter()
{
if (m_cookie->data) {
XFreeEventData(m_display, m_cookie);
}
}
Display *m_display;
XGenericEventCookie *m_cookie;
};
void XlibNotifications::processEvent(XEvent *event)
{
if (event->xcookie.type != GenericEvent) {
return;
}
if (event->xcookie.extension != m_inputOpcode) {
return;
}
if (event->xcookie.evtype == XI_PropertyEvent) {
XEventDataDeleter helper(m_display, &event->xcookie);
if (!event->xcookie.data) {
return;
}
XIPropertyEvent *propEvent =
reinterpret_cast<XIPropertyEvent *>(event->xcookie.data);
Q_EMIT propertyChanged(propEvent->property);
} else if (event->xcookie.evtype == XI_HierarchyChanged) {
XEventDataDeleter helper(m_display, &event->xcookie);
if (!event->xcookie.data) {
return;
}
XIHierarchyEvent *hierarchyEvent =
reinterpret_cast<XIHierarchyEvent *>(event->xcookie.data);
for (uint16_t i = 0; i < hierarchyEvent->num_info; i++) {
if (hierarchyEvent->info[i].deviceid == m_device) {
if (hierarchyEvent->info[i].flags & XISlaveRemoved) {
Q_EMIT touchpadDetached();
return;
}
}
if (hierarchyEvent->info[i].use != XISlavePointer) {
continue;
}
if (hierarchyEvent->info[i].flags &
(XIDeviceEnabled | XIDeviceDisabled))
{
Q_EMIT devicePlugged(hierarchyEvent->info[i].deviceid);
}
}
}
}
XlibNotifications::~XlibNotifications()
{
xcb_destroy_window(m_connection, m_inputWindow);
xcb_flush(m_connection);
}
#include "moc_xlibnotifications.cpp"