on xenial help sign in
update the InputInfo snapshot
commitf75178971f12825f9453bb74879d9c35a2b1972c authorMichael Zanetti <michael.zanetti@canonical.com> dateThu 1 Oct 2015, 12:58 (11 years ago) parentcb37a01
9 files changed+443−264
plugins/Unity/InputInfo/CMakeLists.txt+1−1 plugins/Unity/InputInfo/linux/qinputdeviceinfo_linux.cpp+131−83 plugins/Unity/InputInfo/linux/qinputdeviceinfo_linux_p.h+13−6 plugins/Unity/InputInfo/plugin.cpp+2−2 plugins/Unity/InputInfo/qdeclarativeinputdeviceinfo.cpp → plugins/Unity/InputInfo/qdeclarativeinputdevicemodel.cpp+78−24 plugins/Unity/InputInfo/qdeclarativeinputdeviceinfo_p.h → plugins/Unity/InputInfo/qdeclarativeinputdevicemodel_p.h+27−11 plugins/Unity/InputInfo/qinputinfo.cpp+126−17 plugins/Unity/InputInfo/qinputinfo.h+57−56 qml/Components/UnityInputInfo/UnityInputInfo.qml+8−64
plugins/Unity/InputInfo/CMakeLists.txt+1−1
@@ -21,7 +21,7 @@ include_directories(
2121set(InputInfo_SOURCES
2222 plugin.cpp
2323 qinputinfo.cpp
24− qdeclarativeinputdeviceinfo.cpp
24+ qdeclarativeinputdevicemodel.cpp
2525 linux/qinputdeviceinfo_linux.cpp
2626)
2727
plugins/Unity/InputInfo/linux/qinputdeviceinfo_linux.cpp+131−83
@@ -47,39 +47,46 @@
4747#include <QDebug>
4848#include <QSocketNotifier>
4949#include <QTimer>
50+#include <QDir>
5051
51−QInputDeviceInfoPrivate::QInputDeviceInfoPrivate(QObject *parent) :
52− QObject(parent)
53− , udev(0)
52+QInputDeviceManagerPrivate::QInputDeviceManagerPrivate(QObject *parent) :
53+ QObject(parent),
54+ currentFilter(QInputDevice::Unknown),
55+ udevice(0)
5456{
55− QTimer::singleShot(250, this, &QInputDeviceInfoPrivate::init);
57+ QTimer::singleShot(250,this,SLOT(init()));
5658}
5759
58−void QInputDeviceInfoPrivate::init()
60+QInputDeviceManagerPrivate::~QInputDeviceManagerPrivate()
5961{
60− if (!udev)
61− udev = udev_new();
62+ udev_unref(udevice);
63+ udev_monitor_unref(udevMonitor);
64+}
65+
66+void QInputDeviceManagerPrivate::init()
67+{
68+ if (!udevice)
69+ udevice = udev_new();
6270
63− struct udev_list_entry *devices, *dev_list_entry;
64− struct udev_device *dev;
71+ udev_list_entry *devices;
72+ udev_list_entry *dev_list_entry;
73+ udev_device *dev;
6574
6675 QString subsystem = QStringLiteral("input");
6776 struct udev_enumerate *enumerate = 0;
6877
69− if (udev) {
78+ if (udevice) {
7079
71− udevMonitor = udev_monitor_new_from_netlink(udev, "udev");
80+ udevMonitor = udev_monitor_new_from_netlink(udevice, "udev");
7281 udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem.toLatin1(), NULL);
73− enumerate = udev_enumerate_new(udev);
82+ enumerate = udev_enumerate_new(udevice);
7483 udev_enumerate_add_match_subsystem(enumerate, subsystem.toLatin1());
7584
76−
7785 udev_monitor_enable_receiving(udevMonitor);
7886 notifierFd = udev_monitor_get_fd(udevMonitor);
7987
8088 notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read, this);
81− connect(notifier, &QSocketNotifier::activated, this, &QInputDeviceInfoPrivate::onUDevChanges);
82−
89+ connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
8390
8491 udev_enumerate_scan_devices(enumerate);
8592 devices = udev_enumerate_get_list_entry(enumerate);
@@ -88,78 +95,80 @@ void QInputDeviceInfoPrivate::init()
8895 const char *path;
8996 path = udev_list_entry_get_name(dev_list_entry);
9097
91− dev = udev_device_new_from_syspath(udev, path);
92−
93− QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
94−
98+ dev = udev_device_new_from_syspath(udevice, path);
9599 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
96−
97− if (eventPath.contains(QStringLiteral("event"))) {
98− eventPath.prepend(QStringLiteral("/dev/input/"));
99−
100− QInputDevice *iDevice = addDevice(eventPath);
101− if (!iDevice)
102− continue;
103−
104− iDevice->setTypes(getInputTypes(dev));
105−
106− if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
107− iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
108−
109− if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
110− iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
111−
112− deviceList.append(iDevice);
113− deviceMap.insert(eventPath,iDevice);
114− Q_EMIT newDevice(eventPath);
115−
100+ QInputDevice *iDevice = addDevice(dev);
101+ if (iDevice && !iDevice->devicePath().isEmpty()) {
102+ deviceMap.insert(iDevice->devicePath(),iDevice);
116103 }
117104 }
105+ udev_device_unref(dev);
118106 }
119107 udev_enumerate_unref(enumerate);
120108 }
109+ // udev_unref(udevice);
110+ Q_FOREACH (const QString &devicePath, deviceMap.keys()) {
111+ Q_EMIT deviceAdded(devicePath);
112+ }
121113 Q_EMIT ready();
122114}
123115
124−QInputDeviceInfo::InputTypes QInputDeviceInfoPrivate::getInputTypes( struct udev_device *dev)
116+QInputDevice::InputTypeFlags QInputDeviceManagerPrivate::getInputTypeFlags(struct udev_device *dev)
125117{
126− QInputDeviceInfo::InputTypes types = QInputDeviceInfo::Unknown;
127− if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"), "1") == 0 )
128− types |= QInputDeviceInfo::Keyboard;
129−
130− if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_MOUSE"), "1") == 0)
131− types |= QInputDeviceInfo::Mouse;
132−
133− if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"), "1") == 0)
134− types |= QInputDeviceInfo::TouchPad;
135−
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+ }
136128 if (qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN"), "1") == 0
137− || qstrcmp(udev_device_get_property_value(dev, "ID_INPUT_TABLET"), "1") == 0)
138− types |= QInputDeviceInfo::TouchScreen;
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+ }
139138
140− return types;
139+ return flags;
141140}
142141
143−QInputDevice *QInputDeviceInfoPrivate::addDevice(const QString &path)
142+QInputDevice *QInputDeviceManagerPrivate::addDevice(struct udev_device *udev)
144143{
145− QInputDevice *inputDevice = new QInputDevice(this);
146− inputDevice->setDevicePath(path);
144+ QString eventPath = QString::fromLatin1(udev_device_get_sysname(udev));
147145
146+ if (eventPath.contains(QStringLiteral("event")))
147+ eventPath.prepend(QStringLiteral("/dev/input/"));
148+
149+ if (deviceMap.contains(eventPath)) {
150+ return Q_NULLPTR;
151+ }
148152 struct libevdev *dev = NULL;
149153 int fd;
150154 int rc = 1;
151− fd = open(path.toLatin1(), O_RDONLY|O_NONBLOCK);
155+ QInputDevice *inputDevice;
156+ inputDevice = addUdevDevice(udev);
157+ if (!inputDevice) {
158+ return Q_NULLPTR;
159+ }
160+ eventPath = inputDevice->devicePath();
152161
162+ fd = open(eventPath.toLatin1(), O_RDONLY|O_NONBLOCK);
153163 if (fd == -1) {
154164 return inputDevice;
155165 }
156166 rc = libevdev_new_from_fd(fd, &dev);
157167 if (rc < 0) {
158168 qWarning() << "Failed to init libevdev ("<< strerror(-rc) << ")";
159− return inputDevice;
169+ return Q_NULLPTR;
160170 }
161171
162− inputDevice->setName(QString::fromLatin1(libevdev_get_name(dev)));
163172 for (int i = 0; i < EV_MAX; i++) {
164173 if (i == EV_KEY || i == EV_SW || i == EV_REL
165174 || i == EV_REL || i == EV_ABS) {
@@ -183,54 +192,93 @@ QInputDevice *QInputDeviceInfoPrivate::addDevice(const QString &path)
183192 }
184193 }
185194 }
195+
186196 return inputDevice;
187197}
188198
189−void QInputDeviceInfoPrivate::removeDevice(const QString &path)
199+void QInputDeviceManagerPrivate::addDetails(struct udev_device *)
200+{
201+}
202+
203+void QInputDeviceManagerPrivate::removeDevice(const QString &path)
190204{
191− for (int i = 0; i < deviceList.size(); ++i) {
192− if (deviceList.at(i)->devicePath() == path) {
193− delete deviceList.takeAt(i);
194− deviceMap.remove(path);
195− Q_EMIT deviceRemoved(path);
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);
196210 }
197211 }
198212}
199213
200−void QInputDeviceInfoPrivate::onUDevChanges()
214+QInputDevice *QInputDeviceManagerPrivate::addUdevDevice(struct udev_device *udev)
201215{
202− struct udev_device *dev = udev_monitor_receive_device(udevMonitor);
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+
203256 if (dev) {
204257 if (qstrcmp(udev_device_get_subsystem(dev), "input") == 0 ) {
205258 QString eventPath = QString::fromLatin1(udev_device_get_sysname(dev));
206259
207− if (eventPath.contains(QStringLiteral("input")))
208− return;
209−
210260 QString action = QString::fromStdString(udev_device_get_action(dev));
211261
212262 if (!eventPath.contains(QStringLiteral("/dev/input/")))
213263 eventPath.prepend(QStringLiteral("/dev/input/"));
214264
215265 if (action == QStringLiteral("add")) {
216−
217− QInputDevice *iDevice = addDevice(eventPath);
218− if (!iDevice)
219− return;
220−
221− iDevice->setTypes(getInputTypes(dev));
266+ if (deviceMap.contains(eventPath)){
222267 udev_device_unref(dev);
268+ return;
269+ }
223270
224− if (iDevice->switches().count() > 0 && iDevice->buttons().count() == 0)
225− iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Switch);
226−
227− if (iDevice->buttons().count() > 0 && iDevice->types() == QInputDeviceInfo::Unknown)
228− iDevice->setTypes(iDevice->types() | QInputDeviceInfo::Button);
271+ QInputDevice *iDevice = addDevice(dev);
272+ if (!iDevice) {
273+ delete iDevice;
274+ return;
275+ }
276+ iDevice->setType(getInputTypeFlags(dev));
277+ udev_device_unref(dev);
229278
230− deviceList.append(iDevice);
231279 deviceMap.insert(eventPath,iDevice);
232280
233− Q_EMIT newDevice(eventPath);
281+ Q_EMIT deviceAdded(eventPath);
234282
235283 } else if (action == QStringLiteral("remove")) {
236284 removeDevice(eventPath);
plugins/Unity/InputInfo/linux/qinputdeviceinfo_linux_p.h+13−6
@@ -58,30 +58,37 @@ public:
5858 QList <int> switches;
5959 QList <int> relativeAxis;
6060 QList <int> absoluteAxis;
61− QInputDeviceInfo::InputTypes types;
61+ QInputDevice::InputTypeFlags type;
6262};
6363
64−class QInputDeviceInfoPrivate : public QObject
64+class QInputDeviceManagerPrivate : public QObject
6565{
6666 Q_OBJECT
6767public:
68− explicit QInputDeviceInfoPrivate(QObject *parent = 0);
68+ explicit QInputDeviceManagerPrivate(QObject *parent = 0);
69+ ~QInputDeviceManagerPrivate();
6970 QVector <QInputDevice *> deviceList;
7071 QMap <QString, QInputDevice *> deviceMap;
72+ static QInputDeviceManagerPrivate * instance();
73+ QInputDevice::InputType currentFilter;
7174
7275Q_SIGNALS:
73− void newDevice(const QString &);
76+ void deviceAdded(const QString &);
7477 void deviceRemoved(const QString &);
7578 void ready();
7679
7780private:
78− struct udev *udev;
81+ QInputDevice *addDevice(struct udev_device *udev);
82+ QInputDevice *addUdevDevice(struct udev_device *);
83+
7984 QInputDevice *addDevice(const QString &path);
8085 void removeDevice(const QString &path);
8186 QSocketNotifier *notifier;
8287 int notifierFd;
8388 struct udev_monitor *udevMonitor;
84− QInputDeviceInfo::InputTypes getInputTypes( struct udev_device *);
89+ QInputDevice::InputTypeFlags getInputTypeFlags(struct udev_device *);
90+ struct udev *udevice;
91+ void addDetails(struct udev_device *);
8592
8693private Q_SLOTS:
8794 void onUDevChanges();
plugins/Unity/InputInfo/plugin.cpp+2−2
@@ -21,12 +21,12 @@
2121#include "plugin.h"
2222
2323// local
24−#include "qdeclarativeinputdeviceinfo_p.h"
24+#include "qdeclarativeinputdevicemodel_p.h"
2525
2626void InputInfoPlugin::registerTypes(const char *uri)
2727{
2828 int major = 0;
2929 int minor = 1;
30− qmlRegisterType<QDeclarativeInputDeviceInfo>(uri, major, minor, "InputDeviceInfo");
30+ qmlRegisterType<QDeclarativeInputDeviceModel>(uri, major, minor, "InputDeviceModel");
3131 qmlRegisterType<QInputDevice>(uri, major, minor, "InputInfo");
3232}
plugins/Unity/InputInfo/qdeclarativeinputdeviceinfo.cpp → plugins/Unity/InputInfo/qdeclarativeinputdevicemodel.cpprenamed+78−24
@@ -38,40 +38,64 @@
3838** $QT_END_LICENSE$
3939**
4040****************************************************************************/
41−#include "qdeclarativeinputdeviceinfo_p.h"
41+#include "qdeclarativeinputdevicemodel_p.h"
42+#include "qinputinfo.h"
4243
43−QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
44+QDeclarativeInputDeviceModel::QDeclarativeInputDeviceModel(QObject *parent) :
4445 QAbstractListModel(parent),
45− deviceInfo(new QInputDeviceInfo)
46+ deviceInfo(new QInputDeviceManager),
47+ currentFilter(QInputDevice::Unknown)
4648{
47− connect(deviceInfo, &QInputDeviceInfo::ready, this, &QDeclarativeInputDeviceInfo::updateDeviceList);
48− connect(deviceInfo, &QInputDeviceInfo::deviceAdded,this,&QDeclarativeInputDeviceInfo::addedDevice);
49− connect(deviceInfo, &QInputDeviceInfo::deviceRemoved,this,&QDeclarativeInputDeviceInfo::removedDevice);
49+ connect(deviceInfo,SIGNAL(ready()),this,SLOT(updateDeviceList()));
50+ connect(deviceInfo, &QInputDeviceManager::deviceAdded,this,&QDeclarativeInputDeviceModel::addedDevice);
51+ connect(deviceInfo, &QInputDeviceManager::deviceRemoved,this,&QDeclarativeInputDeviceModel::removedDevice);
5052}
5153
52−QDeclarativeInputDeviceInfo::~QDeclarativeInputDeviceInfo()
54+QDeclarativeInputDeviceModel::~QDeclarativeInputDeviceModel()
5355{
5456 delete deviceInfo;
5557}
5658
57−QVariant QDeclarativeInputDeviceInfo::data(const QModelIndex &index, int role) const
59+QVariant QDeclarativeInputDeviceModel::data(const QModelIndex &index, int role) const
5860{
5961 switch (role) {
6062 case ServiceRole:
6163 return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
62− }
64+ break;
65+ case NameRole:
66+ return QVariant::fromValue(static_cast<QString>(inputDevices.value(index.row())->name()));
67+ break;
68+ case DevicePathRole:
69+ return QVariant::fromValue(static_cast<QString>(inputDevices.value(index.row())->devicePath()));
70+ break;
71+ case ButtonsRole:
72+ return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->buttons()));
73+ break;
74+ case SwitchesRole:
75+ return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->switches()));
76+ break;
77+ case RelativeAxisRole:
78+ return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->relativeAxis()));
79+ break;
80+ case AbsoluteAxisRole:
81+ return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->absoluteAxis()));
82+ break;
83+ case TypesRole:
84+ return QVariant::fromValue(static_cast<int>(inputDevices.value(index.row())->type()));
85+ break;
86+ };
6387
6488 return QVariant();
6589}
6690
67−int QDeclarativeInputDeviceInfo::rowCount(const QModelIndex &parent) const
91+int QDeclarativeInputDeviceModel::rowCount(const QModelIndex &parent) const
6892{
6993 Q_UNUSED(parent);
7094
7195 return inputDevices.count();
7296}
7397
74−int QDeclarativeInputDeviceInfo::indexOf(const QString &devicePath) const
98+int QDeclarativeInputDeviceModel::indexOf(const QString &devicePath) const
7599{
76100 int idx(-1);
77101 Q_FOREACH (QInputDevice *device, inputDevices) {
@@ -82,26 +106,27 @@ int QDeclarativeInputDeviceInfo::indexOf(const QString &devicePath) const
82106 return -1;
83107}
84108
85−QInputDevice *QDeclarativeInputDeviceInfo::get(int index) const
109+QInputDevice *QDeclarativeInputDeviceModel::get(int index) const
86110{
87111 if (index < 0 || index > inputDevices.count())
88112 return 0;
89113 return inputDevices.value(index);
90114}
91115
92−void QDeclarativeInputDeviceInfo::updateDeviceList()
116+void QDeclarativeInputDeviceModel::updateDeviceList()
93117{
94− QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
118+ QVector <QInputDevice *> newDevices = deviceInfo->deviceListOfType(currentFilter);
95119
96120 int numNew = newDevices.count();
97121
98122 for (int i = 0; i < numNew; i++) {
99123 int j = inputDevices.indexOf(newDevices.value(i));
124+
100125 if (j == -1) {
101− // not found -> remove from list
102126 beginInsertRows(QModelIndex(), i, i);
103127 inputDevices.insert(i, newDevices.value(i));
104128 endInsertRows();
129+ Q_EMIT countChanged();
105130 } else if (i != j) {
106131 // changed its position -> move it
107132 QInputDevice* device = inputDevices.value(j);
@@ -109,10 +134,10 @@ void QDeclarativeInputDeviceInfo::updateDeviceList()
109134 inputDevices.remove(j);
110135 inputDevices.insert(i, device);
111136 endMoveRows();
112− } else {
113− QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
114− Q_EMIT dataChanged(changedIndex, changedIndex);
115− }
137+ Q_EMIT countChanged();
138+ } //else {
139+ QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
140+ Q_EMIT dataChanged(changedIndex, changedIndex);
116141 }
117142
118143 int numOld = inputDevices.count();
@@ -120,24 +145,53 @@ void QDeclarativeInputDeviceInfo::updateDeviceList()
120145 beginRemoveRows(QModelIndex(), numNew, numOld - 1);
121146 inputDevices.remove(numNew, numOld - numNew);
122147 endRemoveRows();
148+ Q_EMIT countChanged();
123149 }
124150}
125151
126−void QDeclarativeInputDeviceInfo::addedDevice(const QString &devicePath)
152+void QDeclarativeInputDeviceModel::addedDevice(const QString &devicePath)
127153{
128154 updateDeviceList();
129− Q_EMIT newDevice(devicePath);
155+ Q_EMIT deviceAdded(devicePath);
130156}
131157
132−void QDeclarativeInputDeviceInfo::removedDevice(const QString &devicePath)
158+void QDeclarativeInputDeviceModel::removedDevice(const QString &devicePath)
133159{
134160 updateDeviceList();
135161 Q_EMIT deviceRemoved(devicePath);
136162}
137163
138−QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames() const
164+QHash<int,QByteArray> QDeclarativeInputDeviceModel::roleNames() const
139165{
140166 QHash<int, QByteArray> roles;
141− roles.insert(ServiceRole, "service");
167+ roles[NameRole] = "name";
168+ roles[DevicePathRole] = "devicePath";
169+ roles[ButtonsRole] = "buttons";
170+ roles[SwitchesRole] = "switches";
171+ roles[RelativeAxisRole] = "rAxis";
172+ roles[AbsoluteAxisRole] = "aAxis";
173+ roles[TypesRole] = "types";
142174 return roles;
143175}
176+
177+/*
178+ * Returns the currently set device filter.
179+ * */
180+QInputDevice::InputType QDeclarativeInputDeviceModel::deviceFilter()
181+{
182+ return currentFilter;
183+}
184+
185+/*
186+ * Sets the current input device filter to filter.
187+ * */
188+void QDeclarativeInputDeviceModel::setDeviceFilter(QInputDevice::InputType filter)
189+{
190+ if (filter != currentFilter) {
191+ deviceInfo->setDeviceFilter(filter);
192+ currentFilter = filter;
193+ updateDeviceList();
194+ Q_EMIT deviceFilterChanged(filter);
195+ }
196+}
197+
plugins/Unity/InputInfo/qdeclarativeinputdeviceinfo_p.h → plugins/Unity/InputInfo/qdeclarativeinputdevicemodel_p.hrenamed+27−11
@@ -39,48 +39,64 @@
3939**
4040****************************************************************************/
4141
42−#ifndef QDECLARATIVEINPUTDEVICEINFO_H
43−#define QDECLARATIVEINPUTDEVICEINFO_H
42+#ifndef QDECLARATIVEINPUTDEVICEMODEL_H
43+#define QDECLARATIVEINPUTDEVICEMODEL_H
4444
4545#include <QObject>
4646#include <QAbstractListModel>
4747#include "qinputinfo.h"
4848
49−class QDeclarativeInputDeviceInfo : public QAbstractListModel
49+class QDeclarativeInputDeviceModel : public QAbstractListModel
5050{
5151 Q_OBJECT
52− Q_DISABLE_COPY(QDeclarativeInputDeviceInfo)
52+ Q_DISABLE_COPY(QDeclarativeInputDeviceModel)
53+ Q_PROPERTY(QInputDevice::InputType deviceFilter READ deviceFilter WRITE setDeviceFilter NOTIFY deviceFilterChanged)
54+
55+ Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
5356
5457public:
5558 enum ItemRoles {
56− ServiceRole = Qt::UserRole + 1
59+ ServiceRole = Qt::UserRole + 1,
60+ NameRole,
61+ DevicePathRole,
62+ ButtonsRole,
63+ SwitchesRole,
64+ RelativeAxisRole,
65+ AbsoluteAxisRole,
66+ TypesRole
5767 };
5868
59− explicit QDeclarativeInputDeviceInfo(QObject *parent = 0);
60− virtual ~QDeclarativeInputDeviceInfo();
69+ explicit QDeclarativeInputDeviceModel(QObject *parent = 0);
70+ virtual ~QDeclarativeInputDeviceModel();
6171
6272 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
6373 int rowCount(const QModelIndex &parent = QModelIndex()) const;
6474
65− QHash<int, QByteArray> roleNames() const;
75+ void setDeviceFilter(QInputDevice::InputType filter);
76+ QInputDevice::InputType deviceFilter();
6677
6778 Q_INVOKABLE int indexOf(const QString &devicePath) const;
6879
6980 Q_INVOKABLE QInputDevice *get(int index) const;
81+ QHash<int, QByteArray> roleNames() const;
7082
7183Q_SIGNALS:
72− void newDevice(const QString &devicePath);
84+ void deviceAdded(const QString &devicePath);
7385 void deviceRemoved(const QString &devicePath);
86+ void deviceFilterChanged(const QInputDevice::InputType filter);
87+ void countChanged();
7488
7589public Q_SLOTS:
7690 void updateDeviceList();
7791private:
78− QInputDeviceInfo *deviceInfo;
92+ QInputDeviceManager *deviceInfo;
7993 QVector<QInputDevice *> inputDevices;
94+ QInputDevice::InputType currentFilter;
95+
8096private Q_SLOTS:
8197 void addedDevice(const QString &);
8298 void removedDevice(const QString &path);
8399
84100};
85101
86−#endif // QDECLARATIVEINPUTDEVICEINFO_H
102+#endif // QDECLARATIVEINPUTDEVICEMODEL_H
plugins/Unity/InputInfo/qinputinfo.cpp+126−17
@@ -45,12 +45,19 @@
4545#include "linux/qinputdeviceinfo_linux_p.h"
4646#endif
4747
48+Q_GLOBAL_STATIC(QInputDeviceManagerPrivate, inputDeviceManagerPrivate)
49+
4850QT_BEGIN_NAMESPACE
4951
52+QInputDeviceManagerPrivate * QInputDeviceManagerPrivate::instance()
53+{
54+ QInputDeviceManagerPrivate *priv = inputDeviceManagerPrivate();
55+ return priv;
56+}
5057
5158QInputDevicePrivate::QInputDevicePrivate(QObject *parent) :
5259 QObject(parent),
53− types(QInputDeviceInfo::Unknown)
60+ type(QInputDevice::Unknown)
5461{
5562}
5663
@@ -60,100 +67,202 @@ QInputDevice::QInputDevice(QObject *parent) :
6067{
6168}
6269
70+/*
71+ * Returns the name of this input device.
72+ */
6373QString QInputDevice::name() const
6474{
6575 return d_ptr->name;
6676}
6777
78+/*
79+ * Sets the name of this input device to \b name.
80+ */
6881void QInputDevice::setName(const QString &name)
6982{
7083 d_ptr->name = name;
7184}
7285
86+/*
87+ * Returns the device path of this device.
88+ */
7389QString QInputDevice::devicePath() const
7490{
7591 return d_ptr->devicePath;
7692}
7793
94+/*
95+ * Sets the device ppath of this device to /b path.
96+ */
7897void QInputDevice::setDevicePath(const QString &path)
7998{
8099 d_ptr->devicePath = path;
81100}
82101
102+/*
103+ * Returns the number of buttons this device has.
104+ */
83105QList <int> QInputDevice::buttons() const
84106{
85107 return d_ptr->buttons;
86108}
87109
110+/*
111+ * Adds a button
112+ */
88113void QInputDevice::addButton(int buttonCode)
89114{
90115 d_ptr->buttons.append(buttonCode);
91116}
92117
118+/*
119+ * Returns the number of switch of this device.
120+ */
93121QList <int> QInputDevice::switches() const
94122{
95123 return d_ptr->switches;
96124}
97125
126+/*
127+ * Adds a switch
128+ */
98129void QInputDevice::addSwitch(int switchCode)
99130{
100131 d_ptr->switches.append(switchCode);
101132}
102133
134+/*
135+ * Returns a list of the relative axis of this device
136+ */
103137QList <int> QInputDevice::relativeAxis() const
104138{
105139 return d_ptr->relativeAxis;
106140}
107141
142+/*
143+ */
108144void QInputDevice::addRelativeAxis(int axisCode)
109145{
110146 d_ptr->relativeAxis.append(axisCode);
111147}
112148
149+/*
150+ * Returns a list of the absolute axis of this device
151+ */
113152QList <int> QInputDevice::absoluteAxis() const
114153{
115154 return d_ptr->absoluteAxis;
116155}
117156
157+/*
158+ */
118159void QInputDevice::addAbsoluteAxis(int axisCode)
119160{
120161 d_ptr->absoluteAxis.append(axisCode);
121162}
122163
123−QInputDeviceInfo::InputTypes QInputDevice::types()
164+/*
165+ * Returns a QInputDevice::InputTypeFlags of all the types of types.
166+ */
167+QInputDevice::InputTypeFlags QInputDevice::type() const
124168{
125− return d_ptr->types;
169+ return d_ptr->type;
126170}
127171
128−void QInputDevice::setTypes(QInputDeviceInfo::InputTypes types)
172+/*
173+ */
174+void QInputDevice::setType(QInputDevice::InputTypeFlags type) //? setTypes?
129175{
130− d_ptr->types = types;
176+ d_ptr->type = type;
131177}
132178
133−
134−QInputDeviceInfo::QInputDeviceInfo(QObject *parent) :
179+QInputDeviceManager::QInputDeviceManager(QObject *parent) :
135180 QObject(parent),
136− d_ptr(new QInputDeviceInfoPrivate(this))
181+ d_ptr(inputDeviceManagerPrivate)
137182{
138− connect(d_ptr, &QInputDeviceInfoPrivate::newDevice,this,&QInputDeviceInfo::addedDevice);
139− connect(d_ptr, &QInputDeviceInfoPrivate::deviceRemoved,this,&QInputDeviceInfo::deviceRemoved);
183+ connect(d_ptr, &QInputDeviceManagerPrivate::deviceAdded,this,&QInputDeviceManager::addedDevice);
184+ connect(d_ptr, &QInputDeviceManagerPrivate::deviceRemoved,this,&QInputDeviceManager::deviceRemoved);
140185
141− connect(d_ptr, &QInputDeviceInfoPrivate::ready, this, &QInputDeviceInfo::ready);
186+ connect(d_ptr,SIGNAL(ready()),this,SIGNAL(ready()));
142187}
143188
144−QVector <QInputDevice *> QInputDeviceInfo::deviceList()
189+/*
190+ * Returns a QMap of known input devices.
191+ */
192+QMap <QString, QInputDevice *> QInputDeviceManager::deviceMap()
145193{
146− return d_ptr->deviceList;
194+ return d_ptr->deviceMap;
147195}
148196
149−QMap <QString, QInputDevice *> QInputDeviceInfo::deviceMap()
197+/*
198+ */
199+void QInputDeviceManager::addedDevice(const QString & devicePath)
150200{
151− return d_ptr->deviceMap;
201+ Q_EMIT deviceAdded(devicePath);
152202}
153203
154−void QInputDeviceInfo::addedDevice(const QString & devicePath)
204+/*
205+ * Returns a QVector of InputDevices of type filter
206+ * */
207+QVector <QInputDevice *> QInputDeviceManager::deviceListOfType(QInputDevice::InputType filter)
155208{
156− Q_EMIT deviceAdded(devicePath);
209+ QVector <QInputDevice *> dList;
210+ QMapIterator<QString, QInputDevice *> i(d_ptr->deviceMap);
211+ while (i.hasNext()) {
212+ i.next();
213+ if (i.value()->type().testFlag(filter) || filter == QInputDevice::Unknown) {
214+ dList.append(i.value());
215+ }
216+ }
217+ return dList;
218+}
219+
220+/*
221+ * Returns the number of input devices with the currently set QInputDevice::InputType filter.
222+ * If no device filter has been set, returns number of all available input devices.
223+ * If filter has not been set, returns all available input devices
224+ */
225+int QInputDeviceManager::deviceCount() const
226+{
227+ return deviceCount(static_cast< QInputDevice::InputType >(d_ptr->currentFilter));
228+}
229+
230+/*
231+ * Returns the number of input devices of the type filter.
232+ */
233+int QInputDeviceManager::deviceCount(const QInputDevice::InputType filter) const
234+{
235+ int dList = 0;
236+ QMapIterator<QString, QInputDevice *> i(d_ptr->deviceMap);
237+ while (i.hasNext()) {
238+ i.next();
239+// qDebug() << i.value()->name() << i.value()->devicePath();
240+// qDebug() << i.value()->type() << i.value()->type().testFlag(filter);
241+
242+ if (i.value()->type().testFlag(filter)) {
243+ dList++;
244+ }
245+ }
246+ return dList;
247+}
248+
249+/*
250+ * Returns the currently set device filter.
251+ * */
252+QInputDevice::InputType QInputDeviceManager::deviceFilter()
253+{
254+ return d_ptr->currentFilter;
255+}
256+
257+/*
258+ * Sets the current input device filter to filter.
259+ * */
260+void QInputDeviceManager::setDeviceFilter(QInputDevice::InputType filter)
261+{
262+ if (filter != d_ptr->currentFilter) {
263+ d_ptr->currentFilter = filter;
264+ Q_EMIT deviceFilterChanged(filter);
265+ }
157266}
158267
159268QT_END_NAMESPACE
plugins/Unity/InputInfo/qinputinfo.h+57−56
@@ -42,21 +42,26 @@
4242#ifndef QINPUTINFO_H
4343#define QINPUTINFO_H
4444
45+#include <QtSystemInfo/qsysteminfoglobal.h>
4546#include <QObject>
4647#include <QVector>
4748#include <QMap>
4849#include <QSocketNotifier>
4950#include <QDebug>
5051
51−class QInputDeviceInfoPrivate;
52+class QInputDeviceManagerPrivate;
5253class QInputDevicePrivate;
5354class QInputDevice;
5455
55−class QInputDeviceInfoPrivate;
56−class QInputDeviceInfo : public QObject
56+class QInputDeviceManager;
57+
58+class Q_SYSTEMINFO_EXPORT QInputDevice : public QObject
5759{
5860 Q_OBJECT
59− Q_PROPERTY(int deviceCount READ deviceCount)
61+ Q_ENUMS(InputType)
62+ Q_FLAGS(InputType InputTypeFlags)
63+ friend class QInputDeviceManagerPrivate;
64+
6065public:
6166
6267 enum InputType {
@@ -69,67 +74,19 @@ public:
6974 Switch = 32
7075 };
7176 Q_ENUMS(InputType)
72− Q_FLAGS(InputTypes)
73− Q_DECLARE_FLAGS(InputTypes, InputType)
74−
75− explicit QInputDeviceInfo(QObject *parent = 0);
76−
77− Q_INVOKABLE QVector <QInputDevice *> deviceList();
78−
79− Q_INVOKABLE QMap <QString, QInputDevice *> deviceMap();
80− int deviceCount() { return deviceList().count(); }
81−Q_SIGNALS:
77+ Q_DECLARE_FLAGS(InputTypeFlags, InputType)
8278
83− void deviceAdded(const QString & devicePath);
84− void deviceRemoved(const QString & devicePath);
85−
86− void ready();
87−
88−public Q_SLOTS:
89− void addedDevice(const QString & devicePath);
90−
91−private:
92− Q_DISABLE_COPY(QInputDeviceInfo)
93−#if !defined(QT_SIMULATOR)
94− QInputDeviceInfoPrivate *const d_ptr;
95− Q_DECLARE_PRIVATE(QInputDeviceInfo)
96−#endif
97−};
98−
99−class QInputDevice : public QObject
100−{
101− friend class QInputDeviceInfoPrivate;
102− Q_OBJECT
103− Q_ENUMS(InputType)
104− Q_FLAGS(InputTypes)
105− Q_PROPERTY(QString name READ name NOTIFY nameChanged)
106− Q_PROPERTY(QString devicePath READ devicePath NOTIFY devicePathChanged)
107− Q_PROPERTY(QList <int> buttons READ buttons NOTIFY buttonsChanged)
108− Q_PROPERTY(QList <int> switches READ switches NOTIFY switchesChanged)
109− Q_PROPERTY(QList <int> relativeAxis READ relativeAxis NOTIFY relativeAxisChanged)
110− Q_PROPERTY(QList <int> absoluteAxis READ absoluteAxis NOTIFY absoluteAxisChanged)
111− Q_PROPERTY(QInputDeviceInfo::InputTypes types READ types NOTIFY typesChanged)
112−
113−public:
11479 explicit QInputDevice(QObject *parent = 0);
115−
11680 QString name() const;
11781 QString devicePath() const;
11882 QList <int> buttons() const; //keys event code
11983 QList <int> switches() const;
12084 QList <int> relativeAxis() const;
12185 QList <int> absoluteAxis() const;
86+ QInputDevice::InputTypeFlags type() const;
12287
123− QInputDeviceInfo::InputTypes types();
124−Q_SIGNALS:
125− void nameChanged();
126− void devicePathChanged();
127− void buttonsChanged();
128− void switchesChanged();
129− void relativeAxisChanged();
130− void absoluteAxisChanged();
131− void typesChanged();
13288private:
89+
13390 QInputDevicePrivate *d_ptr;
13491 void setName(const QString &);
13592 void setDevicePath(const QString &);
@@ -137,7 +94,51 @@ private:
13794 void addSwitch(int);
13895 void addRelativeAxis(int);
13996 void addAbsoluteAxis(int);
140− void setTypes(QInputDeviceInfo::InputTypes);
97+ void setType(QInputDevice::InputTypeFlags flags);
98+
99+};
100+
101+Q_DECLARE_METATYPE(QInputDevice::InputType)
102+Q_DECLARE_METATYPE(QInputDevice::InputTypeFlags)
103+
104+class QInputDeviceManagerPrivate;
105+
106+class Q_SYSTEMINFO_EXPORT QInputDeviceManager : public QObject
107+{
108+ Q_OBJECT
109+ Q_PROPERTY(int deviceCount READ deviceCount NOTIFY deviceCountChanged)
110+ Q_PROPERTY(QInputDevice::InputType deviceFilter READ deviceFilter WRITE setDeviceFilter NOTIFY deviceFilterChanged)
111+public:
112+
113+ explicit QInputDeviceManager(QObject *parent = 0);
114+
115+ int deviceCount() const;
116+ int deviceCount(const QInputDevice::InputType filter) const;
117+
118+ void setDeviceFilter(QInputDevice::InputType filter);
119+ QInputDevice::InputType deviceFilter();
120+
121+ QMap <QString, QInputDevice *> deviceMap();
122+ Q_INVOKABLE QVector <QInputDevice *> deviceListOfType(QInputDevice::InputType filter);
123+
124+Q_SIGNALS:
125+
126+ void deviceAdded(const QString & devicePath);
127+ void deviceRemoved(const QString & devicePath);
128+
129+ void ready();
130+ void deviceCountChanged(int count);
131+ void deviceFilterChanged(const QInputDevice::InputType filter);
132+
133+public Q_SLOTS:
134+ void addedDevice(const QString & devicePath);
135+
136+private:
137+ Q_DISABLE_COPY(QInputDeviceManager)
138+#if !defined(QT_SIMULATOR)
139+ QInputDeviceManagerPrivate *const d_ptr;
140+ Q_DECLARE_PRIVATE(QInputDeviceManager)
141+#endif
141142};
142143
143144#endif // QINPUTINFO_H
qml/Components/UnityInputInfo/UnityInputInfo.qml+8−64
@@ -23,71 +23,15 @@ import Ubuntu.Components 1.2
2323
2424Item {
2525 id: root
26− readonly property alias mice: priv.miceCount
27− readonly property alias keyboards: priv.keyboardCount
26+ readonly property alias mice: miceModel.count
27+ readonly property alias keyboards: keyboardModel.count
2828
29− property alias inputInfo: inputInfo
30−
31− QtObject {
32− id: priv
33−
34− property var mice: []
35− property var keyboards: []
36−
37− property int miceCount: 0
38− property int keyboardCount: 0
39−
40− function addMouse(devicePath) {
41− mice.push(devicePath);
42− miceCount++;
43− }
44−
45− function addKeyboard(devicePath) {
46− keyboards.push(devicePath);
47− keyboardCount++;
48− }
49−
50− function removeDevice(devicePath) {
51− for (var i = 0; i < priv.mice.length; i++) {
52− if (priv.mice[i] == devicePath) {
53− priv.mice.splice(i, 1);
54− priv.miceCount--;
55− }
56− }
57− for (var i = 0; i < priv.keyboards.length; i++) {
58− if (priv.keyboards[i] == devicePath) {
59− priv.keyboards.splice(i, 1);
60− priv.keyboardCount--;
61− }
62− }
63− }
29+ InputDeviceModel {
30+ id: miceModel
31+ deviceFilter: InputInfo.Mouse
6432 }
65−
66− InputDeviceInfo {
67− id: inputInfo
68− objectName: "inputDeviceInfo"
69−
70− onNewDevice: {
71− var device = inputInfo.get(inputInfo.indexOf(devicePath));
72− if (device === null) {
73− return;
74− }
75−
76− var hasMouse = (device.types & InputInfo.Mouse) == InputInfo.Mouse
77− var hasTouchpad = (device.types & InputInfo.TouchPad) == InputInfo.TouchPad
78− var hasKeyboard = (device.types & InputInfo.Keyboard) == InputInfo.Keyboard
79−
80− if (hasMouse || hasTouchpad) {
81− priv.addMouse(devicePath);
82− } else if (hasKeyboard) {
83− // Only accepting keyboards that do not claim to be a mouse too
84− // This will be a bit buggy for real hybrid devices, but doesn't
85− // fall for Microsoft mice that claim to be Keyboards too.
86− priv.addKeyboard(devicePath)
87− }
88− }
89− onDeviceRemoved: {
90− priv.removeDevice(devicePath)
91− }
33+ InputDeviceModel {
34+ id: keyboardModel
35+ deviceFilter: InputInfo.Keyboard
9236 }
9337}