42 #include "qinputdeviceinfo_linux_p.h"
45 #include <libevdev/libevdev.h>
48 #include <QSocketNotifier>
51 QInputDeviceInfoPrivate::QInputDeviceInfoPrivate(QObject *parent) :
55 QTimer::singleShot(250,
this,SLOT(init()));
58 void QInputDeviceInfoPrivate::init()
63 struct udev_list_entry *devices, *dev_list_entry;
64 struct udev_device *dev;
66 QString subsystem = QStringLiteral(
"input");
67 struct udev_enumerate *enumerate = 0;
71 udevMonitor = udev_monitor_new_from_netlink(udev,
"udev");
72 udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
73 enumerate = udev_enumerate_new(udev);
74 udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
77 udev_monitor_enable_receiving(udevMonitor);
78 notifierFd = udev_monitor_get_fd(udevMonitor);
80 notifier =
new QSocketNotifier(notifierFd, QSocketNotifier::Read,
this);
81 connect(notifier, SIGNAL(activated(
int)),
this, SLOT(onUDevChanges()));
84 udev_enumerate_scan_devices(enumerate);
85 devices = udev_enumerate_get_list_entry(enumerate);
87 udev_list_entry_foreach(dev_list_entry, devices) {
89 path = udev_list_entry_get_name(dev_list_entry);
91 dev = udev_device_new_from_syspath(udev, path);
93 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
95 if (qstrcmp(udev_device_get_subsystem(dev),
"input") == 0 ) {
97 if (eventPath.contains(QStringLiteral(
"event"))) {
98 eventPath.prepend(QStringLiteral(
"/dev/input/"));
100 QInputDevice *iDevice = addDevice(eventPath);
104 qDebug() <<
"*** ADDING DEVICE" << eventPath;
106 iDevice->setTypes(getInputTypes(dev));
108 if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
109 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
111 if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
112 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
114 deviceList.append(iDevice);
115 deviceMap.insert(eventPath,iDevice);
116 Q_EMIT newDevice(eventPath);
121 udev_enumerate_unref(enumerate);
126 QInputDeviceInfo::InputTypes QInputDeviceInfoPrivate::getInputTypes(
struct udev_device *dev)
128 qDebug() <<
"******* have input type Keyboard" << udev_device_get_property_value(dev,
"ID_INPUT_KEYBOARD");
129 qDebug() <<
"******* have input type Mouse" << udev_device_get_property_value(dev,
"ID_INPUT_MOUSE");
130 qDebug() <<
"******* have input type Touchpad" << udev_device_get_property_value(dev,
"ID_INPUT_TOUCHPAD");
131 qDebug() <<
"******* have input type Touchscreen" << udev_device_get_property_value(dev,
"ID_INPUT_TOUCHSCREEN");
132 qDebug() <<
"******* have input type Tablet" << udev_device_get_property_value(dev,
"ID_INPUT_TABLET");
133 qDebug() <<
"******* have input type joystick" << udev_device_get_property_value(dev,
"ID_INPUT_JOYSTICK");
134 QInputDeviceInfo::InputTypes types = QInputDeviceInfo::Unknown;
135 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_KEYBOARD"),
"1") == 0 )
136 types |= QInputDeviceInfo::Keyboard;
138 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_MOUSE"),
"1") == 0)
139 types |= QInputDeviceInfo::Mouse;
141 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TOUCHPAD"),
"1") == 0)
142 types |= QInputDeviceInfo::TouchPad;
144 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TOUCHSCREEN"),
"1") == 0
145 || qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TABLET"),
"1") == 0)
146 types |= QInputDeviceInfo::TouchScreen;
151 QInputDevice *QInputDeviceInfoPrivate::addDevice(
const QString &path)
153 QInputDevice *inputDevice =
new QInputDevice(
this);
155 struct libevdev *dev = NULL;
158 fd = open(path.toLatin1(), O_RDONLY|O_NONBLOCK);
161 qDebug() <<
"Failed to open";
164 rc = libevdev_new_from_fd(fd, &dev);
166 qDebug() <<
"Failed to init libevdev ("<< strerror(-rc) <<
")";
170 inputDevice->setName(QString::fromLatin1(libevdev_get_name(dev)));
171 inputDevice->setDevicePath(path);
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)) {
179 inputDevice->addButton(j);
182 inputDevice->addSwitch(j);
185 inputDevice->addRelativeAxis(j);
188 inputDevice->addAbsoluteAxis(j);
198 void QInputDeviceInfoPrivate::removeDevice(
const QString &path)
200 for (
int i = 0; i < deviceList.size(); ++i) {
201 if (deviceList.at(i)->devicePath() == path) {
202 delete deviceList.takeAt(i);
203 deviceMap.remove(path);
204 Q_EMIT deviceRemoved(path);
209 void QInputDeviceInfoPrivate::onUDevChanges()
211 struct udev_device *dev = udev_monitor_receive_device(udevMonitor);
213 if (qstrcmp(udev_device_get_subsystem(dev),
"input") == 0 ) {
214 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
216 if (eventPath.contains(QStringLiteral(
"input")))
219 QString action = QString::fromStdString(udev_device_get_action(dev));
221 if (!eventPath.contains(QStringLiteral(
"/dev/input/")))
222 eventPath.prepend(QStringLiteral(
"/dev/input/"));
224 if (action == QStringLiteral(
"add")) {
226 QInputDevice *iDevice = addDevice(eventPath);
230 iDevice->setTypes(getInputTypes(dev));
231 udev_device_unref(dev);
233 if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
234 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
236 if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
237 iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
239 deviceList.append(iDevice);
240 deviceMap.insert(eventPath,iDevice);
242 Q_EMIT newDevice(eventPath);
244 }
else if (action == QStringLiteral(
"remove")) {
245 removeDevice(eventPath);