summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/integrity/qintegrityhidmanager.cpp
blob: e79180b659ff0b9fc49d0da21f9e671439f7892d (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2015 Green Hills Software
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qintegrityhidmanager.h"
#include <QList>
#include <QPoint>
#include <QGuiApplication>
#include <qpa/qwindowsysteminterface.h>
#include <device/hiddriver.h>

QT_BEGIN_NAMESPACE

class IntNotifier
{
    static const Value ActivityPriority = 2;
protected:
    Activity act;
public:
    IntNotifier()
    {
        CheckSuccess(CreateActivity(CurrentTask(), ActivityPriority, false, (Value)this, &act));
    };
    ~IntNotifier()
    {
        CheckSuccess(CloseActivity(act));
    };
    virtual void process_event() = 0;
    virtual void async_wait() = 0;
};

class HIDDeviceHandler : IntNotifier
{
public:
    HIDDeviceHandler(HIDDriver *hidd, HIDHandle hidh)
        : driver(hidd), handle(hidh), currentPos(0, 0) { }
    ~HIDDeviceHandler()
    {
        CheckSuccess(gh_hid_close(handle));
    };
    void process_event(void) override;
    void async_wait(void) override;
    HIDDriver *get_driver(void) { return driver; };
    HIDHandle get_handle(void) { return handle; };
private:
    HIDDriver *driver;
    HIDHandle handle;
    QPoint currentPos;
    Qt::MouseButtons buttons;
};

class HIDDriverHandler : IntNotifier
{
public:
    HIDDriverHandler(HIDDriver *hidd) : IntNotifier(), driver(hidd) { }
    ~HIDDriverHandler()
    {
        qDeleteAll(devices);
    };
    void process_event(void) override;
    void async_wait(void) override;
    void find_devices(void);
private:
    QHash<Value, HIDDeviceHandler *> devices;
    HIDDriver *driver;
};

void HIDDriverHandler::process_event()
{
    find_devices();
}

void HIDDriverHandler::async_wait()
{
    gh_hid_wait_for_new_device(driver, act);
}

void HIDDriverHandler::find_devices()
{
    Error err;
    uintptr_t devicecontext;
    uint32_t device_id;
    HIDHandle handle;
    HIDDeviceHandler *hidnot;

    devicecontext = 0;
    forever {
        err = gh_hid_enum_devices(driver, &device_id, &devicecontext);
        if (err == OperationNotImplemented)
            break;
        else if (err == Failure)
            break;
        if (!devices.contains(device_id)) {
            err = gh_hid_init_device(driver, device_id, &handle);
            if (err == Success) {
                hidnot = new HIDDeviceHandler(driver, handle);
                devices.insert(device_id, hidnot);
                hidnot->async_wait();
            }
        }
    }
    if (err == OperationNotImplemented) {
        /* fallback on legacy enumeration where we assume 0-based
         * contiguous indexes */
        device_id = 0;
        err = Success;
        do {
            if (!devices.contains(device_id)) {
                err = gh_hid_init_device(driver, device_id, &handle);
                if (err != Success)
                    break;
                hidnot = new HIDDeviceHandler(driver, handle);
                devices.insert(device_id, hidnot);
                hidnot->async_wait();
            }
            device_id++;
        } while (err == Success);
    }

    async_wait();
}


void HIDDeviceHandler::process_event()
{
    HIDEvent event;
    uint32_t num_events = 1;

    while (gh_hid_get_event(handle, &event, &num_events) == Success) {
        if (event.type == HID_TYPE_AXIS) {
            switch (event.index) {
            case HID_AXIS_ABSX:
                currentPos.setX(event.value);
                break;
            case HID_AXIS_ABSY:
                currentPos.setY(event.value);
                break;
            case HID_AXIS_RELX:
                currentPos.setX(currentPos.x() + event.value);
                break;
            case HID_AXIS_RELY:
                currentPos.setY(currentPos.y() + event.value);
                break;
            default:
                /* ignore the rest for now */
                break;
            }
        } else if (event.type == HID_TYPE_KEY) {
            switch (event.index) {
            case HID_BUTTON_LEFT:
                if (event.value)
                    buttons |= Qt::LeftButton;
                else
                    buttons &= ~Qt::LeftButton;
                break;
            case HID_BUTTON_MIDDLE:
                if (event.value)
                    buttons |= Qt::MiddleButton;
                else
                    buttons &= ~Qt::MiddleButton;
                break;
            case HID_BUTTON_RIGHT:
                if (event.value)
                    buttons |= Qt::RightButton;
                else
                    buttons &= ~Qt::RightButton;
                break;
            default:
                /* ignore the rest for now */
                break;
            }
        } else if (event.type == HID_TYPE_SYNC) {
            QWindowSystemInterface::handleMouseEvent(0, currentPos, currentPos, buttons,
                                                     QGuiApplication::keyboardModifiers());
        } else if (event.type == HID_TYPE_DISCONNECT) {
            /* FIXME */
        }
    }
    async_wait();
}

void HIDDeviceHandler::async_wait()
{
    CheckSuccess(gh_hid_async_wait_for_event(handle, act));
}

void QIntegrityHIDManager::open_devices()
{
    HIDDriver *hidd;
    uintptr_t context = 0;
    HIDDriverHandler *hidnot;

    while (gh_hid_enum_drivers(&hidd, &context) == Success) {
        hidnot = new HIDDriverHandler(hidd);
        m_drivers.append(hidnot);
        hidnot->find_devices();
    }
}

void QIntegrityHIDManager::run()
{
    IntNotifier *notifier;
    open_devices();
    /* main loop */
    forever {
        WaitForActivity((Value *)&notifier);
        notifier->process_event();
    }
}

QIntegrityHIDManager::QIntegrityHIDManager(const QString &key, const QString &spec, QObject *parent)
    : QThread(parent)
{
    start();
}

QIntegrityHIDManager::~QIntegrityHIDManager()
{
    terminate();
    qDeleteAll(m_drivers);
}

QT_END_NAMESPACE