Commit a4f69b9f1e1672e914e6f1f469538a7dc758fe2b
- Diff rendering mode:
- inline
- side by side
daemon/mousedevicesmonitor.cpp
(60 / 6)
|   | |||
| 47 | 47 | ||
| 48 | 48 | virtual ~MouseDevicesMonitorPrivate() {}; | |
| 49 | 49 | ||
| 50 | /** | ||
| 51 | * Slot for monitorNotifier signals. Reads the available device, | ||
| 52 | * and emits a MouseDevicesMonitor signals, if the device is an | ||
| 53 | * added or removed mouse device. Also updates the serial number | ||
| 54 | * and product name caches. | ||
| 55 | */ | ||
| 50 | 56 | void _k_udevDataAvailable(); | |
| 51 | 57 | ||
| 58 | /** | ||
| 59 | * Find the device object for the given @p serial number. | ||
| 60 | * | ||
| 61 | * Device lookup is cached using serialCache. UDev is only queried | ||
| 62 | * on cache misses. | ||
| 63 | * | ||
| 64 | * @param serial the serial number | ||
| 65 | * @return the device or 0, if there is no device for @p serial | ||
| 66 | */ | ||
| 52 | 67 | QUDevDevicePtr findDeviceBySerial(const QByteArray &serial) const; | |
| 53 | 68 | ||
| 54 | // maps serials to device paths to speed up mapping from serial | ||
| 55 | // numbers to device paths | ||
| 69 | /** | ||
| 70 | * Map serial numbers to device paths to speed up device look up by | ||
| 71 | * serial number | ||
| 72 | */ | ||
| 56 | 73 | mutable QHash<QByteArray, QByteArray> serialCache; | |
| 57 | // map serials to product names to speed up | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Map serial numbers to the product name of the corresponding | ||
| 77 | * device. | ||
| 78 | */ | ||
| 58 | 79 | mutable QHash<QByteArray, QByteArray> productNameCache; | |
| 80 | |||
| 81 | /** | ||
| 82 | * UDev library context, which is required to access the whole | ||
| 83 | * library. | ||
| 84 | */ | ||
| 59 | 85 | QUDevContextPtr context; | |
| 86 | |||
| 87 | /** | ||
| 88 | * UDev monitor object. Provides a file descriptor to read added or | ||
| 89 | * removed devices from. | ||
| 90 | */ | ||
| 60 | 91 | QUDevMonitorPtr monitor; | |
| 92 | |||
| 93 | /** | ||
| 94 | * Notifier object for the monitor. Monitors the monitor's file | ||
| 95 | * descriptor for added or removed devices. | ||
| 96 | */ | ||
| 61 | 97 | QSocketNotifier *monitorNotifier; | |
| 98 | |||
| 62 | 99 | MouseDevicesMonitor *q_ptr; | |
| 63 | 100 | }; | |
| 64 | 101 | } | |
| 65 | 102 | ||
| 103 | /** | ||
| 104 | * This code uses the serial number of input devices to clearly identify | ||
| 105 | * mouse device. The path is sysfs can't be used, because it depends on the | ||
| 106 | * port, into which the device is plugged: a single mouse device has | ||
| 107 | * different sys paths if plugged into different ports. | ||
| 108 | * | ||
| 109 | * However, udev does not allow for direct property queries. To get a | ||
| 110 | * device object for a given serial number, all input devices are enumerated | ||
| 111 | * and filtered by the given serial number. Results of such lookups are | ||
| 112 | * cached. | ||
| 113 | */ | ||
| 66 | 114 | ||
| 67 | 115 | MouseDevicesMonitorPrivate::MouseDevicesMonitorPrivate( | |
| 68 | 116 | MouseDevicesMonitor *qq): q_ptr(qq) { | |
| … | … | ||
| 121 | 121 | udev_monitor_get_fd(this->monitor), QSocketNotifier::Read, q); | |
| 122 | 122 | q->connect(this->monitorNotifier, SIGNAL(activated(int)), | |
| 123 | 123 | SLOT(_k_udevDataAvailable())); | |
| 124 | // we are only interested in events on input devices | ||
| 124 | 125 | udev_monitor_filter_add_match_subsystem_devtype( | |
| 125 | 126 | this->monitor, "input", 0); | |
| 126 | 127 | udev_monitor_enable_receiving(this->monitor); | |
| … | … | ||
| 132 | 132 | * otherwise | |
| 133 | 133 | */ | |
| 134 | 134 | inline bool isMouse(const QUDevDevicePtr &device) { | |
| 135 | // check the sys name here, because input devices may be represented by | ||
| 136 | // more than one device object (e.g. a raw event device and a mouse | ||
| 137 | // device). We need to make sure, that we really got the mouse device | ||
| 138 | // object | ||
| 135 | 139 | QByteArray sysName = udev_device_get_sysname(device); | |
| 136 | // check, if the device really is a mouse | ||
| 137 | 140 | QByteArray isMouse = udev_device_get_property_value( | |
| 138 | 141 | device, "ID_INPUT_MOUSE"); | |
| 139 | 142 | QByteArray isTouchpad = udev_device_get_property_value( | |
| … | … | ||
| 264 | 264 | // cache miss, query udev for the product name | |
| 265 | 265 | QUDevDevicePtr device = d->findDeviceBySerial(id.toAscii()); | |
| 266 | 266 | if (device) { | |
| 267 | // no need for a guarded pointer here, the parent device is freed | ||
| 268 | // together with the child | ||
| 267 | // the parent object is attached to the device object, and is not | ||
| 268 | // referenced itself. It is destroyed, when the device object is | ||
| 269 | // destroyed, therefore no guarded pointer must be used to avoid | ||
| 270 | // double frees. | ||
| 269 | 271 | udev_device *parent = udev_device_get_parent(device); | |
| 270 | 272 | if (parent) { | |
| 271 | 273 | kDebug() << "found" << name << "for serial" << id; |

