~verzegnassi-stefano/ubuntu-docviewer-app/desktop-support2

« back to all changes in this revision

Viewing changes to src/plugin/file-qml-plugin/InputInfo/linux/qinputdeviceinfo_linux.cpp

  • Committer: Stefano Verzegnassi
  • Date: 2016-02-03 13:57:41 UTC
  • Revision ID: stefano92.100@gmail.com-20160203135741-8hy2s2s6y22i0jzt
Forgot to 'bzr add' new files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2014 Canonical, Ltd. and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtSystems module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qinputdeviceinfo_linux_p.h"
 
43
 
 
44
#include <libudev.h>
 
45
#include <libevdev/libevdev.h>
 
46
#include <fcntl.h>
 
47
#include <QDebug>
 
48
#include <QSocketNotifier>
 
49
#include <QTimer>
 
50
#include <QDir>
 
51
 
 
52
QInputDeviceManagerPrivate::QInputDeviceManagerPrivate(QObject *parent) :
 
53
    QObject(parent),
 
54
    currentFilter(QInputDevice::Unknown),
 
55
    udevice(0)
 
56
{
 
57
    QTimer::singleShot(250,this,SLOT(init()));
 
58
}
 
59
 
 
60
QInputDeviceManagerPrivate::~QInputDeviceManagerPrivate()
 
61
{
 
62
    udev_unref(udevice);
 
63
    udev_monitor_unref(udevMonitor);
 
64
}
 
65
 
 
66
void QInputDeviceManagerPrivate::init()
 
67
{
 
68
    if (!udevice)
 
69
        udevice = udev_new();
 
70
 
 
71
    udev_list_entry *devices;
 
72
    udev_list_entry *dev_list_entry;
 
73
    udev_device *dev;
 
74
 
 
75
    QString subsystem = QStringLiteral("input");
 
76
    struct udev_enumerate *enumerate = 0;
 
77
 
 
78
    if (udevice) {
 
79
 
 
80
        udevMonitor = udev_monitor_new_from_netlink(udevice, "udev");
 
81
        udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
 
82
        enumerate = udev_enumerate_new(udevice);
 
83
        udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
 
84
 
 
85
        udev_monitor_enable_receiving(udevMonitor);
 
86
        notifierFd = udev_monitor_get_fd(udevMonitor);
 
87
 
 
88
        notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
 
89
        connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
 
90
 
 
91
        udev_enumerate_scan_devices(enumerate);
 
92
        devices = udev_enumerate_get_list_entry(enumerate);
 
93
 
 
94
        udev_list_entry_foreach(dev_list_entry, devices) {
 
95
            const char *path;
 
96
            path = udev_list_entry_get_name(dev_list_entry);
 
97
 
 
98
            dev = udev_device_new_from_syspath(udevice, path);
 
99
            if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
 
100
                QInputDevice *iDevice = addDevice(dev);
 
101
                if (iDevice && !iDevice->devicePath().isEmpty()) {
 
102
                    deviceMap.insert(iDevice->devicePath(),iDevice);
 
103
                }
 
104
            }
 
105
            udev_device_unref(dev);
 
106
        }
 
107
        udev_enumerate_unref(enumerate);
 
108
    }
 
109
 //   udev_unref(udevice);
 
110
    Q_FOREACH (const QString &devicePath, deviceMap.keys()) {
 
111
        Q_EMIT deviceAdded(devicePath);
 
112
    }
 
113
    Q_EMIT ready();
 
114
}
 
115
 
 
116
QInputDevice::InputTypeFlags QInputDeviceManagerPrivate::getInputTypeFlags(struct udev_device *dev)
 
117
{
 
118
    QInputDevice::InputTypeFlags flags = QInputDevice::Unknown;
 
119
    if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEY"), "1") == 0 ) {
 
120
        flags |= QInputDevice::Button;
 
121
    }
 
122
    if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0) {
 
123
        flags |= QInputDevice::Mouse;
 
124
    }
 
125
    if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0) {
 
126
        flags |= QInputDevice::TouchPad;
 
127
    }
 
128
    if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
 
129
            || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0) {
 
130
        flags |= QInputDevice::TouchScreen;
 
131
    }
 
132
    if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 ) {
 
133
        flags |= QInputDevice::Keyboard;
 
134
    }
 
135
    if (!QString::fromLatin1(udev_device_get_property_value(dev, "SW")).isEmpty()) {
 
136
        flags |= QInputDevice::Switch;
 
137
    }
 
138
 
 
139
    return flags;
 
140
}
 
141
 
 
142
QInputDevice *QInputDeviceManagerPrivate::addDevice(struct udev_device *udev)
 
143
{
 
144
    QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
 
145
 
 
146
    if (eventPath.contains(QStringLiteral("event")))
 
147
        eventPath.prepend(QStringLiteral("/dev/input/"));
 
148
 
 
149
    if (deviceMap.contains(eventPath)) {
 
150
        return Q_NULLPTR;
 
151
    }
 
152
    struct libevdev *dev = NULL;
 
153
    int fd;
 
154
    int rc = 1;
 
155
    QInputDevice *inputDevice;
 
156
    inputDevice = addUdevDevice(udev);
 
157
    if (!inputDevice) {
 
158
        return Q_NULLPTR;
 
159
    }
 
160
    eventPath = inputDevice->devicePath();
 
161
 
 
162
    fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
 
163
    if (fd == -1) {
 
164
        return inputDevice;
 
165
    }
 
166
    rc = libevdev_new_from_fd(fd, &dev);
 
167
    if (rc < 0) {
 
168
        qWarning() << "Failed to init libevdev ("<< strerror(-rc) << ")";
 
169
        return Q_NULLPTR;
 
170
    }
 
171
 
 
172
    for (int i = 0; i < EV_MAX; i++) {
 
173
        if (i == EV_KEY || i == EV_SW || i == EV_REL
 
174
                || i == EV_REL || i == EV_ABS) {
 
175
            for (int j = 0; j <  libevdev_event_type_get_max(i); j++) {
 
176
                if (libevdev_has_event_code(dev, i, j)) {
 
177
                    switch (i) {
 
178
                    case EV_KEY:
 
179
                        inputDevice->addButton(j);
 
180
                        break;
 
181
                    case EV_SW:
 
182
                        inputDevice->addSwitch(j);
 
183
                        break;
 
184
                    case EV_REL:
 
185
                        inputDevice->addRelativeAxis(j);
 
186
                        break;
 
187
                    case EV_ABS:
 
188
                        inputDevice->addAbsoluteAxis(j);
 
189
                        break;
 
190
                    };
 
191
                }
 
192
            }
 
193
        }
 
194
    }
 
195
 
 
196
    return inputDevice;
 
197
}
 
198
 
 
199
void QInputDeviceManagerPrivate::addDetails(struct udev_device *)
 
200
{
 
201
}
 
202
 
 
203
void QInputDeviceManagerPrivate::removeDevice(const QString &path)
 
204
{
 
205
    // this path is not a full evdev path
 
206
    Q_FOREACH (const QString devicePath, deviceMap.keys()) {
 
207
        if (devicePath.contains(path)) {
 
208
            deviceMap.remove(devicePath);
 
209
            Q_EMIT deviceRemoved(devicePath);
 
210
        }
 
211
    }
 
212
}
 
213
 
 
214
QInputDevice *QInputDeviceManagerPrivate::addUdevDevice(struct udev_device *udev)
 
215
{
 
216
    QInputDevice *iDevice;
 
217
 
 
218
    struct udev_list_entry *list;
 
219
    struct udev_list_entry *node;
 
220
 
 
221
    list = udev_device_get_properties_list_entry (udev);
 
222
    QString syspath = QString::fromLatin1(udev_device_get_syspath(udev));
 
223
    QDir sysdir(syspath);
 
224
 
 
225
    QStringList infoList = sysdir.entryList(QStringList() << QStringLiteral("event*"),QDir::Dirs);
 
226
 
 
227
    if (infoList.count() > 0) {
 
228
        QString token = infoList.at(0);
 
229
 
 
230
        token.prepend(QStringLiteral("/dev/input/"));
 
231
        iDevice = new QInputDevice(this);
 
232
        iDevice->setDevicePath(token);
 
233
    } else {
 
234
        return Q_NULLPTR;
 
235
    }
 
236
    udev_list_entry_foreach (node, list) {
 
237
 
 
238
        QString key = QString::fromLatin1(udev_list_entry_get_name(node));
 
239
        QString value = QString::fromLatin1(udev_list_entry_get_value(node));
 
240
 
 
241
        if (key == QStringLiteral("NAME")) {
 
242
            iDevice->setName(value.remove(QStringLiteral("\"")));
 
243
        }
 
244
    }
 
245
    iDevice->setType(getInputTypeFlags(udev));
 
246
    return iDevice;
 
247
}
 
248
 
 
249
void QInputDeviceManagerPrivate::onUDevChanges()
 
250
{
 
251
    if (!udevMonitor)
 
252
        return;
 
253
 
 
254
    udev_device *dev = udev_monitor_receive_device(udevMonitor);
 
255
 
 
256
    if (dev) {
 
257
        if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
 
258
            QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
 
259
 
 
260
            QString action = QString::fromStdString(udev_device_get_action(dev));
 
261
 
 
262
            if (!eventPath.contains(QStringLiteral("/dev/input/")))
 
263
                eventPath.prepend(QStringLiteral("/dev/input/"));
 
264
 
 
265
            if (action == QStringLiteral("add")) {
 
266
                if (deviceMap.contains(eventPath)){
 
267
                    udev_device_unref(dev);
 
268
                    return;
 
269
                }
 
270
 
 
271
                QInputDevice *iDevice = addDevice(dev);
 
272
                if (!iDevice) {
 
273
                    delete iDevice;
 
274
                    return;
 
275
                }
 
276
                iDevice->setType(getInputTypeFlags(dev));
 
277
                udev_device_unref(dev);
 
278
 
 
279
                deviceMap.insert(eventPath,iDevice);
 
280
 
 
281
                Q_EMIT deviceAdded(eventPath);
 
282
 
 
283
            } else if (action == QStringLiteral("remove")) {
 
284
                removeDevice(eventPath);
 
285
            }
 
286
        }
 
287
    }
 
288
}