42#include "qinputdeviceinfo_linux_p.h"
45#include <libevdev/libevdev.h>
49#include <QSocketNotifier>
53QInputDeviceManagerPrivate::QInputDeviceManagerPrivate(QObject *parent) :
55 currentFilter(QInputDevice::Unknown),
59 QTimer::singleShot(250,
this,SLOT(init()));
62QInputDeviceManagerPrivate::~QInputDeviceManagerPrivate()
65 udev_monitor_unref(udevMonitor);
68void QInputDeviceManagerPrivate::init()
73 udev_list_entry *devices;
74 udev_list_entry *dev_list_entry;
77 QString subsystem = QStringLiteral(
"input");
78 struct udev_enumerate *enumerate = 0;
82 udevMonitor = udev_monitor_new_from_netlink(udevice,
"udev");
83 udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
84 enumerate = udev_enumerate_new(udevice);
85 udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
87 udev_monitor_enable_receiving(udevMonitor);
88 int notifierFd = udev_monitor_get_fd(udevMonitor);
90 QSocketNotifier *notifier =
new QSocketNotifier(notifierFd, QSocketNotifier::Read,
this);
91 connect(notifier, SIGNAL(activated(
int)),
this, SLOT(onUDevChanges()));
93 udev_enumerate_scan_devices(enumerate);
94 devices = udev_enumerate_get_list_entry(enumerate);
96 udev_list_entry_foreach(dev_list_entry, devices) {
98 path = udev_list_entry_get_name(dev_list_entry);
100 dev = udev_device_new_from_syspath(udevice, path);
101 if (qstrcmp(udev_device_get_subsystem(dev),
"input") == 0 ) {
102 QInputDevice *iDevice = addDevice(dev);
103 if (iDevice && !iDevice->devicePath().isEmpty()) {
104 deviceMap.insert(iDevice->devicePath(),iDevice);
107 udev_device_unref(dev);
109 udev_enumerate_unref(enumerate);
112 Q_FOREACH (
const QString &devicePath, deviceMap.keys()) {
113 Q_EMIT deviceAdded(devicePath);
118QInputDevice::InputTypeFlags QInputDeviceManagerPrivate::getInputTypeFlags(
struct udev_device *dev)
120 QInputDevice::InputTypeFlags flags = QInputDevice::Unknown;
121 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_KEY"),
"1") == 0 ) {
122 flags |= QInputDevice::Button;
124 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_MOUSE"),
"1") == 0) {
125 flags |= QInputDevice::Mouse;
127 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TOUCHPAD"),
"1") == 0) {
128 flags |= QInputDevice::TouchPad;
130 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TOUCHSCREEN"),
"1") == 0
131 || qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TABLET"),
"1") == 0) {
132 flags |= QInputDevice::TouchScreen;
134 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_KEYBOARD"),
"1") == 0 ) {
135 flags |= QInputDevice::Keyboard;
137 if (!QString::fromLatin1(udev_device_get_property_value(dev,
"SW")).isEmpty()) {
138 flags |= QInputDevice::Switch;
144QInputDevice *QInputDeviceManagerPrivate::addDevice(
struct udev_device *udev)
146 QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
148 if (eventPath.contains(QStringLiteral(
"event")))
149 eventPath.prepend(QStringLiteral(
"/dev/input/"));
151 if (deviceMap.contains(eventPath)) {
154 struct libevdev *dev = NULL;
157 QInputDevice *inputDevice;
158 inputDevice = addUdevDevice(udev);
162 eventPath = inputDevice->devicePath();
164 qDebug() <<
"Input device added:" << inputDevice->name() << inputDevice->devicePath() << inputDevice->type();
166 fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
170 rc = libevdev_new_from_fd(fd, &dev);
172 qWarning() <<
"Failed to init libevdev ("<< strerror(-rc) <<
")";
177 for (
int i = 0; i < EV_MAX; i++) {
178 if (i == EV_KEY || i == EV_SW || i == EV_REL
179 || i == EV_REL || i == EV_ABS) {
180 for (
int j = 0; j < libevdev_event_type_get_max(i); j++) {
181 if (libevdev_has_event_code(dev, i, j)) {
184 inputDevice->addButton(j);
187 inputDevice->addSwitch(j);
190 inputDevice->addRelativeAxis(j);
193 inputDevice->addAbsoluteAxis(j);
206void QInputDeviceManagerPrivate::addDetails(
struct udev_device *)
210void QInputDeviceManagerPrivate::removeDevice(
const QString &path)
213 Q_FOREACH (
const QString devicePath, deviceMap.keys()) {
214 if (devicePath.contains(path)) {
215 qDebug() <<
"Input device removed:" << deviceMap.value(devicePath)->name() << devicePath << deviceMap.value(devicePath)->type();
216 deviceMap.remove(devicePath);
217 Q_EMIT deviceRemoved(devicePath);
222QInputDevice *QInputDeviceManagerPrivate::addUdevDevice(
struct udev_device *udev)
224 QInputDevice *iDevice;
226 struct udev_list_entry *list;
227 struct udev_list_entry *node;
229 list = udev_device_get_properties_list_entry (udev);
230 QString syspath = QString::fromLatin1(udev_device_get_syspath(udev));
231 QDir sysdir(syspath);
233 QStringList infoList = sysdir.entryList(QStringList() << QStringLiteral(
"event*"),QDir::Dirs);
235 if (infoList.count() > 0) {
236 QString token = infoList.at(0);
238 token.prepend(QStringLiteral(
"/dev/input/"));
239 iDevice =
new QInputDevice(
this);
240 iDevice->setDevicePath(token);
244 udev_list_entry_foreach (node, list) {
246 QString key = QString::fromLatin1(udev_list_entry_get_name(node));
247 QString value = QString::fromLatin1(udev_list_entry_get_value(node));
249 if (key == QStringLiteral(
"NAME")) {
250 iDevice->setName(value.remove(QStringLiteral(
"\"")));
253 iDevice->setType(getInputTypeFlags(udev));
257void QInputDeviceManagerPrivate::onUDevChanges()
262 udev_device *dev = udev_monitor_receive_device(udevMonitor);
265 if (qstrcmp(udev_device_get_subsystem(dev),
"input") == 0 ) {
266 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
268 QString action = QString::fromStdString(udev_device_get_action(dev));
270 if (!eventPath.contains(QStringLiteral(
"/dev/input/")))
271 eventPath.prepend(QStringLiteral(
"/dev/input/"));
273 if (action == QStringLiteral(
"add")) {
274 if (deviceMap.contains(eventPath)){
275 udev_device_unref(dev);
279 QInputDevice *iDevice = addDevice(dev);
284 iDevice->setType(getInputTypeFlags(dev));
285 udev_device_unref(dev);
287 deviceMap.insert(eventPath,iDevice);
289 Q_EMIT deviceAdded(eventPath);
291 }
else if (action == QStringLiteral(
"remove")) {
292 removeDevice(eventPath);