summaryrefslogtreecommitdiffstats
path: root/src/plugins/generic/evdevkeyboard/qevdevkeyboardmanager.cpp
blob: 8769b704954c61afaf00762496b57b2ca6701940 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qevdevkeyboardmanager.h"

#include <QStringList>
#include <QDebug>
#include <QCoreApplication>

#include <linux/input.h>
#include <linux/kd.h>

//#define QT_QPA_KEYMAP_DEBUG

QT_BEGIN_NAMESPACE

QEvdevKeyboardManager::QEvdevKeyboardManager(const QString &key, const QString &specification)
    : m_udev(0), m_udevMonitor(0), m_udevMonitorFileDescriptor(-1), m_udevSocketNotifier(0)
{
    Q_UNUSED(key);

    bool useUDev = false;
    QStringList args = specification.split(QLatin1Char(':'));
    QStringList devices;

    foreach (const QString &arg, args) {
        if (arg.startsWith("udev") && !arg.contains("no")) {
            useUDev = true;
        } else if (arg.startsWith("/dev/")) {
            // if device is specified try to use it
            devices.append(arg);
            args.removeAll(arg);
        }
    }

    m_spec = args.join(":");

    // add all keyboards for devices specified in the argument list
    foreach (const QString &device, devices)
        addKeyboard(device);

    // no udev and no devices specified, try a fallback
    if (!useUDev && devices.isEmpty()) {
        addKeyboard();
        return;
    }

    m_udev = udev_new();
    if (!m_udev) {
        qWarning() << "Failed to read udev configuration. Try to open a keyboard without it";
        addKeyboard();
    } else {
        // Look for already attached devices:
        parseConnectedDevices();
        // Watch for device add/remove
        startWatching();
    }
}

QEvdevKeyboardManager::~QEvdevKeyboardManager()
{
    // cleanup udev related resources
    stopWatching();
    if (m_udev)
        udev_unref(m_udev);

    // cleanup all resources of connected keyboards
    qDeleteAll(m_keyboards);
    m_keyboards.clear();
}

void QEvdevKeyboardManager::addKeyboard(const QString &devnode)
{
    QString specification = m_spec;
    QString deviceString = devnode;

    if (!deviceString.isEmpty()) {
        specification.append(":");
        specification.append(deviceString);
    } else {
        deviceString = "default";
    }

#ifdef QT_QPA_KEYMAP_DEBUG
    qWarning() << "Adding keyboard at" << deviceString;
#endif

    QEvdevKeyboardHandler *keyboard;
    keyboard = QEvdevKeyboardHandler::createLinuxInputKeyboardHandler("EvdevKeyboard", specification);
    if (keyboard)
        m_keyboards.insert(deviceString, keyboard);
    else
        qWarning() << "Failed to open keyboard";
}

void QEvdevKeyboardManager::removeKeyboard(const QString &devnode)
{
    if (m_keyboards.contains(devnode)) {
#ifdef QT_QPA_KEYMAP_DEBUG
    qWarning() << "Removing keyboard at" << devnode;
#endif
        QEvdevKeyboardHandler *keyboard = m_keyboards.value(devnode);
        m_keyboards.remove(devnode);
        delete keyboard;
    }
}

void QEvdevKeyboardManager::startWatching()
{
    m_udevMonitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_udevMonitor) {
#ifdef QT_QPA_KEYMAP_DEBUG
        qWarning("Unable to create an Udev monitor. No devices can be detected.");
#endif
        return;
    }

    udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "input", NULL);
    udev_monitor_enable_receiving(m_udevMonitor);
    m_udevMonitorFileDescriptor = udev_monitor_get_fd(m_udevMonitor);

    m_udevSocketNotifier = new QSocketNotifier(m_udevMonitorFileDescriptor, QSocketNotifier::Read, this);
    connect(m_udevSocketNotifier, SIGNAL(activated(int)), this, SLOT(deviceDetected()));
}

void QEvdevKeyboardManager::stopWatching()
{
    if (m_udevSocketNotifier)
        delete m_udevSocketNotifier;

    if (m_udevMonitor)
        udev_monitor_unref(m_udevMonitor);

    m_udevSocketNotifier = 0;
    m_udevMonitor = 0;
    m_udevMonitorFileDescriptor = 0;
}

void QEvdevKeyboardManager::deviceDetected()
{
    if (!m_udevMonitor)
        return;

    struct udev_device *dev;
    dev = udev_monitor_receive_device(m_udevMonitor);
    if (!dev)
        return;

    if (qstrcmp(udev_device_get_action(dev), "add") == 0) {
        checkDevice(dev);
    } else {
        // We can't determine what the device was, so we handle false positives outside of this class
        QString str = udev_device_get_devnode(dev);
        if (!str.isEmpty())
            removeKeyboard(str);
    }

    udev_device_unref(dev);
}

void QEvdevKeyboardManager::parseConnectedDevices()
{
    struct udev_enumerate *enumerate;
    struct udev_list_entry *devices;
    struct udev_list_entry *dev_list_entry;
    struct udev_device *dev;
    const char *str;

    enumerate = udev_enumerate_new(m_udev);
    udev_enumerate_add_match_subsystem(enumerate, "input");
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);

    udev_list_entry_foreach(dev_list_entry, devices) {
        str = udev_list_entry_get_name(dev_list_entry);
        dev = udev_device_new_from_syspath(m_udev, str);
        checkDevice(dev);
    }

    udev_enumerate_unref(enumerate);
}

void QEvdevKeyboardManager::checkDevice(udev_device *dev)
{
    const char *str;
    QString devnode;

    str = udev_device_get_devnode(dev);
    if (!str)
        return;

    devnode = str;

    dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
    if (!dev)
        return;

    str = udev_device_get_sysattr_value(dev, "capabilities/key");
    QStringList val = QString(str).split(' ', QString::SkipEmptyParts);

    bool ok;
    unsigned long long keys = val.last().toULongLong(&ok, 16);
    if (!ok)
        return;

    // Tests if the letter Q is valid for the device.  We may want to alter this test, but it seems mostly reliable.
    bool test = (keys >> KEY_Q) & 1;
    if (test) {
        addKeyboard(devnode);
        return;
    }
}