Commit 4a464098d54e54d5e2608d140905941791354d47
- Diff rendering mode:
- inline
- side by side
daemon/mousedevicesmonitor.cpp
(49 / 13)
|   | |||
| 51 | 51 | ||
| 52 | 52 | QUDevDevicePtr findDeviceBySerial(const QByteArray &serial) const; | |
| 53 | 53 | ||
| 54 | QSet<QString> mouseDevices; | ||
| 54 | // maps serials to device paths to speed up mapping from serial | ||
| 55 | // numbers to device paths | ||
| 56 | mutable QHash<QByteArray, QByteArray> serialCache; | ||
| 57 | // map serials to product names to speed up | ||
| 58 | mutable QHash<QByteArray, QByteArray> productNameCache; | ||
| 55 | 59 | QUDevContextPtr context; | |
| 56 | 60 | QUDevMonitorPtr monitor; | |
| 57 | 61 | QSocketNotifier *monitorNotifier; | |
| … | … | ||
| 109 | 109 | QByteArray serial = getSerial(device); | |
| 110 | 110 | if (action == "add") { | |
| 111 | 111 | kDebug() << "mouse added:" << serial; | |
| 112 | QByteArray sysPath = udev_device_get_syspath(device); | ||
| 113 | this->serialCache.insert(serial, sysPath); | ||
| 112 | 114 | emit q->mousePlugged(QString::fromAscii(serial)); | |
| 113 | 115 | } else if (action == "remove") { | |
| 114 | 116 | kDebug() << "mouse removed:" << serial; | |
| 117 | // remove device from all caches | ||
| 118 | this->serialCache.remove(serial); | ||
| 119 | this->productNameCache.remove(serial); | ||
| 115 | 120 | emit q->mouseUnplugged(QString::fromAscii(serial)); | |
| 116 | 121 | } | |
| 117 | 122 | } | |
| … | … | ||
| 125 | 125 | QUDevDevicePtr MouseDevicesMonitorPrivate::findDeviceBySerial( | |
| 126 | 126 | const QByteArray &serial) const { | |
| 127 | 127 | kDebug() << "searching device for serial" << serial; | |
| 128 | QUDevDevicePtr device; | ||
| 128 | // try to get the device from cache | ||
| 129 | QByteArray sysPath = this->serialCache.value(serial); | ||
| 130 | if (!sysPath.isEmpty()) { | ||
| 131 | QUDevDevicePtr device = | ||
| 132 | udev_device_new_from_syspath(this->context, sysPath); | ||
| 133 | if (device && isMouse(device)) { | ||
| 134 | kDebug() << "found device for serial" << serial << "in cache"; | ||
| 135 | return device; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | // cache miss, query udev for a device with the given serial number | ||
| 129 | 139 | QUDevEnumeratePtr enumerator = udev_enumerate_new(this->context); | |
| 130 | 140 | Q_ASSERT(enumerator); | |
| 131 | 141 | // filter for the given serial number | |
| … | … | ||
| 147 | 147 | udev_enumerate_get_list_entry(enumerator); | |
| 148 | 148 | udev_list_entry *current_entry = 0; | |
| 149 | 149 | udev_list_entry_foreach(current_entry, device_entries) { | |
| 150 | device = udev_device_new_from_syspath( | ||
| 151 | this->context, udev_list_entry_get_name(current_entry)); | ||
| 150 | QByteArray sysPath = udev_list_entry_get_name(current_entry); | ||
| 151 | QUDevDevicePtr device = udev_device_new_from_syspath( | ||
| 152 | this->context, sysPath); | ||
| 152 | 153 | // we have a matching mouse device | |
| 153 | 154 | if (device && isMouse(device)) { | |
| 154 | 155 | kDebug() << "found device for serial:" << serial; | |
| 155 | break; | ||
| 156 | // cache the result of the udev query | ||
| 157 | this->serialCache.insert(serial, sysPath); | ||
| 158 | return device; | ||
| 156 | 159 | } | |
| 157 | 160 | } | |
| 158 | return device; | ||
| 161 | return 0; | ||
| 159 | 162 | } | |
| 160 | 163 | ||
| 161 | 164 | MouseDevicesMonitor::MouseDevicesMonitor(QObject *parent): | |
| … | … | ||
| 185 | 185 | udev_list_entry_foreach(current_entry, device_entries) { | |
| 186 | 186 | QUDevDevicePtr device = udev_device_new_from_syspath( | |
| 187 | 187 | d->context, udev_list_entry_get_name(current_entry)); | |
| 188 | // a true mouse device | ||
| 188 | // a real mouse device | ||
| 189 | 189 | if (device && isMouse(device)) { | |
| 190 | 190 | QByteArray serial = getSerial(device); | |
| 191 | kDebug() << "found device with serial" << serial; | ||
| 191 | // update the serial cache | ||
| 192 | QByteArray sysPath = udev_device_get_syspath(device); | ||
| 193 | d->serialCache.insert(serial, sysPath); | ||
| 194 | kDebug() << "device with serial" << serial; | ||
| 192 | 195 | devices << QString::fromAscii(serial); | |
| 193 | 196 | } | |
| 194 | 197 | } | |
| … | … | ||
| 201 | 201 | QString MouseDevicesMonitor::productName(const QString &id) const { | |
| 202 | 202 | Q_D(const MouseDevicesMonitor); | |
| 203 | 203 | kDebug() << "querying product name for serial" << id; | |
| 204 | QString name; | ||
| 204 | QByteArray serial = id.toAscii(); | ||
| 205 | // try to get product name from cache | ||
| 206 | QByteArray name = d->productNameCache.value(serial); | ||
| 207 | if (!name.isEmpty()) { | ||
| 208 | kDebug() << "found product name" << name | ||
| 209 | << "for serial" << serial << "in cache"; | ||
| 210 | return QString::fromAscii(name); | ||
| 211 | } | ||
| 212 | // cache miss, query udev for the product name | ||
| 205 | 213 | QUDevDevicePtr device = d->findDeviceBySerial(id.toAscii()); | |
| 206 | 214 | if (device) { | |
| 207 | 215 | // no need for a guarded pointer here, the parent device is freed | |
| 208 | 216 | // together with the child | |
| 209 | 217 | udev_device *parent = udev_device_get_parent(device); | |
| 210 | 218 | if (parent) { | |
| 211 | kDebug() << "got" << name << "for serial" << id; | ||
| 212 | name = QString::fromAscii( | ||
| 213 | udev_device_get_property_value(parent, "NAME")); | ||
| 219 | kDebug() << "found" << name << "for serial" << id; | ||
| 220 | QByteArray name = udev_device_get_property_value( | ||
| 221 | parent, "NAME"); | ||
| 222 | // cache the name | ||
| 223 | d->productNameCache.insert(serial, name); | ||
| 224 | return QString::fromAscii(name); | ||
| 214 | 225 | } | |
| 215 | 226 | } | |
| 216 | return name; | ||
| 227 | return QString(); | ||
| 217 | 228 | } | |
| 218 | 229 | ||
| 219 | 230 | #include "moc_mousedevicesmonitor.cpp" |

