42 #include "qinputdeviceinfo_linux_p.h"
45 #include <libevdev/libevdev.h>
48 #include <QSocketNotifier>
52 QInputDeviceManagerPrivate::QInputDeviceManagerPrivate(QObject *parent) :
54 currentFilter(QInputDevice::Unknown),
57 QTimer::singleShot(250,
this,SLOT(init()));
60 QInputDeviceManagerPrivate::~QInputDeviceManagerPrivate()
63 udev_monitor_unref(udevMonitor);
66 void QInputDeviceManagerPrivate::init()
71 udev_list_entry *devices;
72 udev_list_entry *dev_list_entry;
75 QString subsystem = QStringLiteral(
"input");
76 struct udev_enumerate *enumerate = 0;
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());
85 udev_monitor_enable_receiving(udevMonitor);
86 notifierFd = udev_monitor_get_fd(udevMonitor);
88 notifier =
new QSocketNotifier(notifierFd, QSocketNotifier::Read,
this);
89 connect(notifier, SIGNAL(activated(
int)),
this, SLOT(onUDevChanges()));
91 udev_enumerate_scan_devices(enumerate);
92 devices = udev_enumerate_get_list_entry(enumerate);
94 udev_list_entry_foreach(dev_list_entry, devices) {
96 path = udev_list_entry_get_name(dev_list_entry);
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);
105 udev_device_unref(dev);
107 udev_enumerate_unref(enumerate);
110 Q_FOREACH (
const QString &devicePath, deviceMap.keys()) {
111 Q_EMIT deviceAdded(devicePath);
116 QInputDevice::InputTypeFlags QInputDeviceManagerPrivate::getInputTypeFlags(
struct udev_device *dev)
118 QInputDevice::InputTypeFlags flags = QInputDevice::Unknown;
119 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_KEY"),
"1") == 0 ) {
120 flags |= QInputDevice::Button;
122 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_MOUSE"),
"1") == 0) {
123 flags |= QInputDevice::Mouse;
125 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_TOUCHPAD"),
"1") == 0) {
126 flags |= QInputDevice::TouchPad;
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;
132 if (qstrcmp(udev_device_get_property_value(dev,
"ID_INPUT_KEYBOARD"),
"1") == 0 ) {
133 flags |= QInputDevice::Keyboard;
135 if (!QString::fromLatin1(udev_device_get_property_value(dev,
"SW")).isEmpty()) {
136 flags |= QInputDevice::Switch;
142 QInputDevice *QInputDeviceManagerPrivate::addDevice(
struct udev_device *udev)
144 QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
146 if (eventPath.contains(QStringLiteral(
"event")))
147 eventPath.prepend(QStringLiteral(
"/dev/input/"));
149 if (deviceMap.contains(eventPath)) {
152 struct libevdev *dev = NULL;
155 QInputDevice *inputDevice;
156 inputDevice = addUdevDevice(udev);
160 eventPath = inputDevice->devicePath();
162 qDebug() <<
"Input device added:" << inputDevice->name() << inputDevice->devicePath() << inputDevice->type();
164 fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
168 rc = libevdev_new_from_fd(fd, &dev);
170 qWarning() <<
"Failed to init libevdev ("<< strerror(-rc) <<
")";
174 for (
int i = 0; i < EV_MAX; i++) {
175 if (i == EV_KEY || i == EV_SW || i == EV_REL
176 || i == EV_REL || i == EV_ABS) {
177 for (
int j = 0; j < libevdev_event_type_get_max(i); j++) {
178 if (libevdev_has_event_code(dev, i, j)) {
181 inputDevice->addButton(j);
184 inputDevice->addSwitch(j);
187 inputDevice->addRelativeAxis(j);
190 inputDevice->addAbsoluteAxis(j);
201 void QInputDeviceManagerPrivate::addDetails(
struct udev_device *)
205 void QInputDeviceManagerPrivate::removeDevice(
const QString &path)
208 Q_FOREACH (
const QString devicePath, deviceMap.keys()) {
209 if (devicePath.contains(path)) {
210 qDebug() <<
"Input device removed:" << deviceMap.value(devicePath)->name() << devicePath << deviceMap.value(devicePath)->type();
211 deviceMap.remove(devicePath);
212 Q_EMIT deviceRemoved(devicePath);
217 QInputDevice *QInputDeviceManagerPrivate::addUdevDevice(
struct udev_device *udev)
219 QInputDevice *iDevice;
221 struct udev_list_entry *list;
222 struct udev_list_entry *node;
224 list = udev_device_get_properties_list_entry (udev);
225 QString syspath = QString::fromLatin1(udev_device_get_syspath(udev));
226 QDir sysdir(syspath);
228 QStringList infoList = sysdir.entryList(QStringList() << QStringLiteral(
"event*"),QDir::Dirs);
230 if (infoList.count() > 0) {
231 QString token = infoList.at(0);
233 token.prepend(QStringLiteral(
"/dev/input/"));
234 iDevice =
new QInputDevice(
this);
235 iDevice->setDevicePath(token);
239 udev_list_entry_foreach (node, list) {
241 QString key = QString::fromLatin1(udev_list_entry_get_name(node));
242 QString value = QString::fromLatin1(udev_list_entry_get_value(node));
244 if (key == QStringLiteral(
"NAME")) {
245 iDevice->setName(value.remove(QStringLiteral(
"\"")));
248 iDevice->setType(getInputTypeFlags(udev));
252 void QInputDeviceManagerPrivate::onUDevChanges()
257 udev_device *dev = udev_monitor_receive_device(udevMonitor);
260 if (qstrcmp(udev_device_get_subsystem(dev),
"input") == 0 ) {
261 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
263 QString action = QString::fromStdString(udev_device_get_action(dev));
265 if (!eventPath.contains(QStringLiteral(
"/dev/input/")))
266 eventPath.prepend(QStringLiteral(
"/dev/input/"));
268 if (action == QStringLiteral(
"add")) {
269 if (deviceMap.contains(eventPath)){
270 udev_device_unref(dev);
274 QInputDevice *iDevice = addDevice(dev);
279 iDevice->setType(getInputTypeFlags(dev));
280 udev_device_unref(dev);
282 deviceMap.insert(eventPath,iDevice);
284 Q_EMIT deviceAdded(eventPath);
286 }
else if (action == QStringLiteral(
"remove")) {
287 removeDevice(eventPath);