Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qintegrityhidmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 Green Hills Software
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QList>
6#include <QPoint>
7#include <QGuiApplication>
8#include <qpa/qwindowsysteminterface.h>
9#include <device/hiddriver.h>
10
12
13class IntNotifier
14{
15 static const Value ActivityPriority = 2;
16protected:
18public:
20 {
21 CheckSuccess(CreateActivity(CurrentTask(), ActivityPriority, false, (Value)this, &act));
22 };
24 {
25 CheckSuccess(CloseActivity(act));
26 };
27 virtual void process_event() = 0;
28 virtual void async_wait() = 0;
29};
30
32{
33public:
34 HIDDeviceHandler(HIDDriver *hidd, HIDHandle hidh)
35 : driver(hidd), handle(hidh), currentPos(0, 0) { }
37 {
38 CheckSuccess(gh_hid_close(handle));
39 };
40 void process_event(void) override;
41 void async_wait(void) override;
42 HIDDriver *get_driver(void) { return driver; };
43 HIDHandle get_handle(void) { return handle; };
44private:
45 HIDDriver *driver;
46 HIDHandle handle;
47 QPoint currentPos;
48 Qt::MouseButtons buttons;
49};
50
52{
53public:
54 HIDDriverHandler(HIDDriver *hidd) : IntNotifier(), driver(hidd) { }
56 {
57 qDeleteAll(devices);
58 };
59 void process_event(void) override;
60 void async_wait(void) override;
61 void find_devices(void);
62private:
63 QHash<Value, HIDDeviceHandler *> devices;
64 HIDDriver *driver;
65};
66
68{
70}
71
73{
74 gh_hid_wait_for_new_device(driver, act);
75}
76
78{
79 Error err;
80 uintptr_t devicecontext;
81 uint32_t device_id;
82 HIDHandle handle;
83 HIDDeviceHandler *hidnot;
84
85 devicecontext = 0;
86 forever {
87 err = gh_hid_enum_devices(driver, &device_id, &devicecontext);
88 if (err == OperationNotImplemented)
89 break;
90 else if (err == Failure)
91 break;
92 if (!devices.contains(device_id)) {
93 err = gh_hid_init_device(driver, device_id, &handle);
94 if (err == Success) {
95 hidnot = new HIDDeviceHandler(driver, handle);
96 devices.insert(device_id, hidnot);
97 hidnot->async_wait();
98 }
99 }
100 }
101 if (err == OperationNotImplemented) {
102 /* fallback on legacy enumeration where we assume 0-based
103 * contiguous indexes */
104 device_id = 0;
105 err = Success;
106 do {
107 if (!devices.contains(device_id)) {
108 err = gh_hid_init_device(driver, device_id, &handle);
109 if (err != Success)
110 break;
111 hidnot = new HIDDeviceHandler(driver, handle);
112 devices.insert(device_id, hidnot);
113 hidnot->async_wait();
114 }
115 device_id++;
116 } while (err == Success);
117 }
118
119 async_wait();
120}
121
122
124{
125 HIDEvent event;
126 uint32_t num_events = 1;
127
128 while (gh_hid_get_event(handle, &event, &num_events) == Success) {
129 if (event.type == HID_TYPE_AXIS) {
130 switch (event.index) {
131 case HID_AXIS_ABSX:
132 currentPos.setX(event.value);
133 break;
134 case HID_AXIS_ABSY:
135 currentPos.setY(event.value);
136 break;
137 case HID_AXIS_RELX:
138 currentPos.setX(currentPos.x() + event.value);
139 break;
140 case HID_AXIS_RELY:
141 currentPos.setY(currentPos.y() + event.value);
142 break;
143 default:
144 /* ignore the rest for now */
145 break;
146 }
147 } else if (event.type == HID_TYPE_KEY) {
148 switch (event.index) {
149 case HID_BUTTON_LEFT:
150 if (event.value)
151 buttons |= Qt::LeftButton;
152 else
153 buttons &= ~Qt::LeftButton;
154 break;
155 case HID_BUTTON_MIDDLE:
156 if (event.value)
157 buttons |= Qt::MiddleButton;
158 else
159 buttons &= ~Qt::MiddleButton;
160 break;
161 case HID_BUTTON_RIGHT:
162 if (event.value)
163 buttons |= Qt::RightButton;
164 else
165 buttons &= ~Qt::RightButton;
166 break;
167 default:
168 /* ignore the rest for now */
169 break;
170 }
171 } else if (event.type == HID_TYPE_SYNC) {
172 QWindowSystemInterface::handleMouseEvent(0, currentPos, currentPos, buttons,
173 QGuiApplication::keyboardModifiers());
174 } else if (event.type == HID_TYPE_DISCONNECT) {
175 /* FIXME */
176 }
177 }
179}
180
182{
183 CheckSuccess(gh_hid_async_wait_for_event(handle, act));
184}
185
186void QIntegrityHIDManager::open_devices()
187{
188 HIDDriver *hidd;
189 uintptr_t context = 0;
190 HIDDriverHandler *hidnot;
191
192 while (gh_hid_enum_drivers(&hidd, &context) == Success) {
193 hidnot = new HIDDriverHandler(hidd);
194 m_drivers.append(hidnot);
195 hidnot->find_devices();
196 }
197}
198
200{
201 IntNotifier *notifier;
202 open_devices();
203 /* main loop */
204 forever {
205 WaitForActivity((Value *)&notifier);
206 notifier->process_event();
207 }
208}
209
210QIntegrityHIDManager::QIntegrityHIDManager(const QString &key, const QString &spec, QObject *parent)
211 : QThread(parent)
212{
213 start();
214}
215
217{
218 terminate();
219 qDeleteAll(m_drivers);
220}
221
222QT_END_NAMESPACE
void process_event(void) override
HIDDeviceHandler(HIDDriver *hidd, HIDHandle hidh)
HIDHandle get_handle(void)
HIDDriver * get_driver(void)
void async_wait(void) override
void process_event(void) override
HIDDriverHandler(HIDDriver *hidd)
void async_wait(void) override
virtual void process_event()=0
virtual void async_wait()=0