summaryrefslogtreecommitdiffstats
path: root/src/platformsupport/input/libinput/qlibinputhandler.cpp
blob: ef45533f1a9e2d6833658d3e6675495f05164b0c (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qlibinputhandler_p.h"
#include "qlibinputpointer_p.h"
#include "qlibinputkeyboard_p.h"
#include "qlibinputtouch_p.h"

#include <libudev.h>
#include <libinput.h>
#include <QtCore/QLoggingCategory>
#include <QtCore/QSocketNotifier>
#include <QtCore/private/qcore_unix_p.h>
#include <private/qguiapplication_p.h>
#include <private/qinputdevicemanager_p_p.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcLibInput, "qt.qpa.input")

static int liOpen(const char *path, int flags, void *user_data)
{
    Q_UNUSED(user_data);
    return qt_safe_open(path, flags);
}

static void liClose(int fd, void *user_data)
{
    Q_UNUSED(user_data);
    qt_safe_close(fd);
}

static const struct libinput_interface liInterface = {
    liOpen,
    liClose
};

static void liLogHandler(libinput *libinput, libinput_log_priority priority, const char *format, va_list args)
{
    Q_UNUSED(libinput);
    Q_UNUSED(priority);

    char buf[512];
    int n = vsnprintf(buf, sizeof(buf), format, args);
    if (n > 0) {
        if (buf[n - 1] == '\n')
            buf[n - 1] = '\0';
        qCDebug(qLcLibInput, "libinput: %s", buf);
    }
}

QLibInputHandler::QLibInputHandler(const QString &key, const QString &spec)
{
    Q_UNUSED(key);
    Q_UNUSED(spec);

    m_udev = udev_new();
    if (Q_UNLIKELY(!m_udev))
        qFatal("Failed to get udev context for libinput");

    m_li = libinput_udev_create_context(&liInterface, nullptr, m_udev);
    if (Q_UNLIKELY(!m_li))
        qFatal("Failed to get libinput context");

    libinput_log_set_handler(m_li, liLogHandler);
    if (qLcLibInput().isDebugEnabled())
        libinput_log_set_priority(m_li, LIBINPUT_LOG_PRIORITY_DEBUG);

    if (Q_UNLIKELY(libinput_udev_assign_seat(m_li, "seat0")))
        qFatal("Failed to assign seat");

    m_liFd = libinput_get_fd(m_li);
    m_notifier.reset(new QSocketNotifier(m_liFd, QSocketNotifier::Read));

    connect(m_notifier.data(), &QSocketNotifier::activated, this, &QLibInputHandler::onReadyRead);

    m_pointer.reset(new QLibInputPointer);
    m_keyboard.reset(new QLibInputKeyboard);
    m_touch.reset(new QLibInputTouch);

    QInputDeviceManager *manager = QGuiApplicationPrivate::inputDeviceManager();
    connect(manager, &QInputDeviceManager::cursorPositionChangeRequested, this, [this](const QPoint &pos) {
        m_pointer->setPos(pos);
    });

    // Process the initial burst of DEVICE_ADDED events.
    onReadyRead();
}

QLibInputHandler::~QLibInputHandler()
{
    if (m_li)
        libinput_unref(m_li);

    if (m_udev)
        udev_unref(m_udev);
}

void QLibInputHandler::onReadyRead()
{
    if (libinput_dispatch(m_li)) {
        qWarning("libinput_dispatch failed");
        return;
    }

    libinput_event *ev;
    while ((ev = libinput_get_event(m_li)) != nullptr) {
        processEvent(ev);
        libinput_event_destroy(ev);
    }
}

void QLibInputHandler::processEvent(libinput_event *ev)
{
    libinput_event_type type = libinput_event_get_type(ev);
    libinput_device *dev = libinput_event_get_device(ev);

    switch (type) {
    case LIBINPUT_EVENT_DEVICE_ADDED:
    {
        // This is not just for hotplugging, it is also called for each input
        // device libinput reads from on startup. Hence it is suitable for doing
        // touch device registration.
        QInputDeviceManagerPrivate *inputManagerPriv = QInputDeviceManagerPrivate::get(
            QGuiApplicationPrivate::inputDeviceManager());
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TOUCH)) {
            m_touch->registerDevice(dev);
            int &count(m_devCount[QInputDeviceManager::DeviceTypeTouch]);
            ++count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypeTouch, count);
        }
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_POINTER)) {
            int &count(m_devCount[QInputDeviceManager::DeviceTypePointer]);
            ++count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypePointer, count);
        }
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
            int &count(m_devCount[QInputDeviceManager::DeviceTypeKeyboard]);
            ++count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypeKeyboard, count);
        }
        break;
    }
    case LIBINPUT_EVENT_DEVICE_REMOVED:
    {
        QInputDeviceManagerPrivate *inputManagerPriv = QInputDeviceManagerPrivate::get(
            QGuiApplicationPrivate::inputDeviceManager());
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TOUCH)) {
            m_touch->unregisterDevice(dev);
            int &count(m_devCount[QInputDeviceManager::DeviceTypeTouch]);
            --count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypeTouch, count);
        }
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_POINTER)) {
            int &count(m_devCount[QInputDeviceManager::DeviceTypePointer]);
            --count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypePointer, count);
        }
        if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
            int &count(m_devCount[QInputDeviceManager::DeviceTypeKeyboard]);
            --count;
            inputManagerPriv->setDeviceCount(QInputDeviceManager::DeviceTypeKeyboard, count);
        }
        break;
    }
    case LIBINPUT_EVENT_POINTER_BUTTON:
        m_pointer->processButton(libinput_event_get_pointer_event(ev));
        break;
    case LIBINPUT_EVENT_POINTER_MOTION:
        m_pointer->processMotion(libinput_event_get_pointer_event(ev));
        break;
    case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
        m_pointer->processAbsMotion(libinput_event_get_pointer_event(ev));
        break;
#if QT_CONFIG(libinput_hires_wheel_support)
    case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
#else
    case LIBINPUT_EVENT_POINTER_AXIS:
#endif
        m_pointer->processAxis(libinput_event_get_pointer_event(ev));
        break;
    case LIBINPUT_EVENT_KEYBOARD_KEY:
        m_keyboard->processKey(libinput_event_get_keyboard_event(ev));
        break;
    case LIBINPUT_EVENT_TOUCH_DOWN:
        m_touch->processTouchDown(libinput_event_get_touch_event(ev));
        break;
    case LIBINPUT_EVENT_TOUCH_MOTION:
        m_touch->processTouchMotion(libinput_event_get_touch_event(ev));
        break;
    case LIBINPUT_EVENT_TOUCH_UP:
        m_touch->processTouchUp(libinput_event_get_touch_event(ev));
        break;
    case LIBINPUT_EVENT_TOUCH_CANCEL:
        m_touch->processTouchCancel(libinput_event_get_touch_event(ev));
        break;
    case LIBINPUT_EVENT_TOUCH_FRAME:
        m_touch->processTouchFrame(libinput_event_get_touch_event(ev));
        break;
    default:
        break;
    }
}

QT_END_NAMESPACE