分析下QT5.6.0的QPA机制下利用udev的devicediscovery和edevxxxmanage。
官网上介绍的
Debugging Input Devices
It is possible to print some information to the debug output by enabling theqt.qpa.input
logging rule, for example by setting theQT_LOGGING_RULES
environment variable toqt.qpa.input=true
. This is useful for detecting which device is being used, or to troubleshoot device discovery issues.
可以调试,添加export QT_LOGGING_RULES=qt.qpa.input=true
这样再运行程序就会打印出东西:
[root@GK sdcard]# ./start_qt5.sh
qt.qpa.input: evdevkeyboard: Using device discovery
qt.qpa.input: udev device discovery for type QFlags(0x8)
qt.qpa.input: Found matching devices ()
qt.qpa.input: evdevmouse: Using device discovery
qt.qpa.input: udev device discovery for type QFlags(0x1|0x2)
qt.qpa.input: Found matching devices ()
qt.qpa.input: evdevtouch: Using device discovery
qt.qpa.input: udev device discovery for type QFlags(0x2|0x4)
我接的是USB键鼠套件的接收头,所以会有键盘和鼠标。
好了,下面来看看为什么会没有找到匹配的设备:
Found matching devices ()
首先搜索这个信息,有_static、_udev两个文件,现在是udev的,应该是_udev的这个。
QStringList QDeviceDiscoveryUDev::scanConnectedDevices()
1{
1
2QStringList devices;
1
2if (!m_udev)
1
2return devices;
1
2udev_enumerate *ue = udev_enumerate_new(m_udev);
1
2udev_enumerate_add_match_subsystem(ue, "input");
1
2udev_enumerate_add_match_subsystem(ue, "drm");
1
2if (m_types & Device_Mouse)
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_MOUSE", "1");
1
2if (m_types & Device_Touchpad)
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_TOUCHPAD", "1");
1
2if (m_types & Device_Touchscreen)
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_TOUCHSCREEN", "1");
1
2if (m_types & Device_Keyboard) {
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_KEYBOARD", "1");
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_KEY", "1");
1
2}
1
2if (m_types & Device_Tablet)
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_TABLET", "1");
1
2if (m_types & Device_Joystick)
1
2udev_enumerate_add_match_property(ue, "ID_INPUT_JOYSTICK", "1");
1
2if (udev_enumerate_scan_devices(ue) != 0) {
1
2qWarning("Failed to scan devices");
1
2return devices;
1
2}
1
2udev_list_entry *entry;
1
2udev_list_entry_foreach (entry, udev_enumerate_get_list_entry(ue)) {
1
2const char *syspath = udev_list_entry_get_name(entry);
1
2udev_device *udevice = udev_device_new_from_syspath(m_udev, syspath);
1
2QString candidate = QString::fromUtf8(udev_device_get_devnode(udevice));
1
2if ((m_types & Device_InputMask) && candidate.startsWith(QLatin1String(QT_EVDEV_DEVICE)))
1
2devices << candidate;
1
2if ((m_types & Device_VideoMask) && candidate.startsWith(QLatin1String(QT_DRM_DEVICE))) {
1
2if (m_types & Device_DRM_PrimaryGPU) {
1
2udev_device *pci = udev_device_get_parent_with_subsystem_devtype(udevice, "pci", 0);
1
2if (pci) {
1
2if (qstrcmp(udev_device_get_sysattr_value(pci, "boot_vga"), "1") == 0)
1
2devices << candidate;
1
2}
1
2} else
1
2devices << candidate;
1
2}
1
2udev_device_unref(udevice);
1
2}
1
2udev_enumerate_unref(ue);
1
2qCDebug(lcDD) << "Found matching devices" << devices;
1
2return devices;
1}
扫描已连接的设备
前面的三句话是去匹配udev规则,如果我们自己去使用udev,会使用一个udev.conf或者其他名字的配置文件,里面的内容就是udev规则。
下面就是需要哪几个添加要匹配的,然后udev枚举扫描,获得信息。
enum QDeviceType {
1
2Device_Unknown = 0x00,
1
2Device_Mouse = 0x01,
1
2Device_Touchpad = 0x02,
1
2Device_Touchscreen = 0x04,
1
2Device_Keyboard = 0x08,
1
2Device_DRM = 0x10,
1
2Device_DRM_PrimaryGPU = 0x20,
1
2Device_Tablet = 0x40,
1
2Device_Joystick = 0x80,
1
2Device_InputMask = Device_Mouse | Device_Touchpad | Device_Touchscreen | Device_Keyboard | Device_Tablet | Device_Joystick,
1
2Device_VideoMask = Device_DRM
1
2};
前面打印出的信息可看到有0x8、0x1、0x2、0x4,键盘、鼠标、触摸
现在先不分析之后的代码,先不分析怎样才算found
先看下由谁调用,有很多,不管键盘的、触摸的,只看鼠标
QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specification, QObject *parent)
1
2: QObject(parent), m_x(0), m_y(0), m_xoffset(0), m_yoffset(0)
1{
1
2Q_UNUSED(key);
1
2QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_MOUSE_PARAMETERS"));
1
2if (spec.isEmpty())
1
2spec = specification;
1
2QStringList args = spec.split(QLatin1Char(':'));
1
2QStringList devices;
1
2foreach (const QString &arg, args) {
1
2if (arg.startsWith(QLatin1String("/dev/"))) {
1
2// if device is specified try to use it
1
2devices.append(arg);
1
2args.removeAll(arg);
1
2} else if (arg.startsWith(QLatin1String("xoffset="))) {
1
2m_xoffset = arg.mid(8).toInt();
1
2} else if (arg.startsWith(QLatin1String("yoffset="))) {
1
2m_yoffset = arg.mid(8).toInt();
1
2}
1
2}
1
2// build new specification without /dev/ elements
1
2m_spec = args.join(QLatin1Char(':'));
1
2// add all mice for devices specified in the argument list
1
2foreach (const QString &device, devices)
1
2addMouse(device);
1
2if (devices.isEmpty()) {
1
2qCDebug(qLcEvdevMouse) << "evdevmouse: Using device discovery";
1
2m_deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Mouse | QDeviceDiscovery::Device_Touchpad, this);
1
2if (m_deviceDiscovery) {
1
2// scan and add already connected keyboards
1
2QStringList devices = m_deviceDiscovery->scanConnectedDevices();
1
2foreach (const QString &device, devices) {
1
2addMouse(device);
1
2}
1
2connect(m_deviceDiscovery, SIGNAL(deviceDetected(QString)), this, SLOT(addMouse(QString)));
1
2connect(m_deviceDiscovery, SIGNAL(deviceRemoved(QString)), this, SLOT(removeMouse(QString)));
1
2}
1
2}
1
2connect(QGuiApplicationPrivate::inputDeviceManager(), SIGNAL(cursorPositionChangeRequested(QPoint)),
1
2this, SLOT(handleCursorPositionChange(QPoint)));
1}
去创建一个edevmouse管理者的对象时会调用到。
首先去获得环境变量QT_QPA_EVDEV_MOUSE_PARAMETERS,以':'划分,如果有设置/dev/节点、xoffset=、yoffset=就使用它。如果没有,"evdevmouse: Using device discovery",然后创建设备发现对象,扫描已连接的设备。把扫描到的结果添加进去。然后就是管理处理插拔事件。
看这很简单,如果以后这种机制被QT移除了,自己也可以参考写一个。同时,在其他方面,也可以参考这个,做一个设备自动发现并添加的程序。这就是我选择分析它的另一个目的。
好了,现在再去分析那个扫描,怎样才算找到了。可以注意到,evdevmouse:xxx这句在前面的信息中有打印,所以后面如果要加调试信息,也可以这样:
qCDebug(qLcEvdevMouse) << "evdevmouse: Using device discovery";
当然也可以这样:
qCDebug(lcDD) << "Found matching devices" << devices;
简单分析一边之后,不用调试也知道了,后面一部分是循环判断扫描到的结果。
foreach里前面三句获得相应内容,后面可以看到有Device_InputMask和Device_VideoMask ,找Mouse,选择input,所以只看第一个if
Device_InputMask = Device_Mouse | Device_Touchpad | Device_Touchscreen | Device_Keyboard | Device_Tablet | Device_Joystick,
Mouse在里面,startsWith(QLatin1String(QT_EVDEV_DEVICE)),
#define QT_EVDEV_DEVICE_PATH "/dev/input/"
1#define QT_EVDEV_DEVICE_PREFIX "event"
1#define QT_EVDEV_DEVICE QT_EVDEV_DEVICE_PATH QT_EVDEV_DEVICE_PREFIX
可以看到了/dev/input/event前缀,但是我现在用的开发板上,不知道原厂做了什么,event是在/dev下的,前缀就成了/dev/event,所以就没有匹配到了。
这里做个通用法,两个前缀都匹配下就好了。
最后
以上就是兴奋水壶最近收集整理的关于QT5.6.0 利用UDEV的发现机制的全部内容,更多相关QT5.6.0内容请搜索靠谱客的其他文章。
发表评论 取消回复