QT5.6.0 利用UDEV的发现机制

分析下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()

{
    QStringList devices;
    if (!m_udev)
        return devices;
    udev_enumerate *ue = udev_enumerate_new(m_udev);
    udev_enumerate_add_match_subsystem(ue, "input");
    udev_enumerate_add_match_subsystem(ue, "drm");
    if (m_types & Device_Mouse)
        udev_enumerate_add_match_property(ue, "ID_INPUT_MOUSE", "1");
    if (m_types & Device_Touchpad)
        udev_enumerate_add_match_property(ue, "ID_INPUT_TOUCHPAD", "1");
    if (m_types & Device_Touchscreen)
        udev_enumerate_add_match_property(ue, "ID_INPUT_TOUCHSCREEN", "1");
    if (m_types & Device_Keyboard) {
        udev_enumerate_add_match_property(ue, "ID_INPUT_KEYBOARD", "1");
        udev_enumerate_add_match_property(ue, "ID_INPUT_KEY", "1");
    }
    if (m_types & Device_Tablet)
        udev_enumerate_add_match_property(ue, "ID_INPUT_TABLET", "1");
    if (m_types & Device_Joystick)
        udev_enumerate_add_match_property(ue, "ID_INPUT_JOYSTICK", "1");
    if (udev_enumerate_scan_devices(ue) != 0) {
        qWarning("Failed to scan devices");
        return devices;
    }
    udev_list_entry *entry;
    udev_list_entry_foreach (entry, udev_enumerate_get_list_entry(ue)) {
        const char *syspath = udev_list_entry_get_name(entry);
        udev_device *udevice = udev_device_new_from_syspath(m_udev, syspath);
        QString candidate = QString::fromUtf8(udev_device_get_devnode(udevice));
        if ((m_types & Device_InputMask) && candidate.startsWith(QLatin1String(QT_EVDEV_DEVICE)))
            devices << candidate;
        if ((m_types & Device_VideoMask) && candidate.startsWith(QLatin1String(QT_DRM_DEVICE))) {
            if (m_types & Device_DRM_PrimaryGPU) {
                udev_device *pci = udev_device_get_parent_with_subsystem_devtype(udevice, "pci", 0);
                if (pci) {
                    if (qstrcmp(udev_device_get_sysattr_value(pci, "boot_vga"), "1") == 0)
                        devices << candidate;
                }
            } else
                devices << candidate;
        }
        udev_device_unref(udevice);
    }
    udev_enumerate_unref(ue);
    qCDebug(lcDD) << "Found matching devices" << devices;
    return devices;
}

       扫描已连接的设备

      前面的三句话是去匹配udev规则,如果我们自己去使用udev,会使用一个udev.conf或者其他名字的配置文件,里面的内容就是udev规则。

      下面就是需要哪几个添加要匹配的,然后udev枚举扫描,获得信息。

enum QDeviceType {

        Device_Unknown = 0x00,
        Device_Mouse = 0x01,
        Device_Touchpad = 0x02,
        Device_Touchscreen = 0x04,
        Device_Keyboard = 0x08,
        Device_DRM = 0x10,
        Device_DRM_PrimaryGPU = 0x20,
        Device_Tablet = 0x40,
        Device_Joystick = 0x80,
        Device_InputMask = Device_Mouse | Device_Touchpad | Device_Touchscreen | Device_Keyboard | Device_Tablet | Device_Joystick,
        Device_VideoMask = Device_DRM
    };

      前面打印出的信息可看到有0x8、0x1、0x2、0x4,键盘、鼠标、触摸

 

 

      现在先不分析之后的代码,先不分析怎样才算found

 

      先看下由谁调用,有很多,不管键盘的、触摸的,只看鼠标

QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specification, QObject *parent)

    : QObject(parent), m_x(0), m_y(0), m_xoffset(0), m_yoffset(0)
{
    Q_UNUSED(key);
    QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_MOUSE_PARAMETERS"));
    if (spec.isEmpty())
        spec = specification;
    QStringList args = spec.split(QLatin1Char(':'));
    QStringList devices;
    foreach (const QString &arg, args) {
        if (arg.startsWith(QLatin1String("/dev/"))) {
            // if device is specified try to use it
            devices.append(arg);
            args.removeAll(arg);
        } else if (arg.startsWith(QLatin1String("xoffset="))) {
            m_xoffset = arg.mid(8).toInt();
        } else if (arg.startsWith(QLatin1String("yoffset="))) {
            m_yoffset = arg.mid(8).toInt();
        }
    }
    // build new specification without /dev/ elements
    m_spec = args.join(QLatin1Char(':'));
    // add all mice for devices specified in the argument list
    foreach (const QString &device, devices)
        addMouse(device);
    if (devices.isEmpty()) {
        qCDebug(qLcEvdevMouse) << "evdevmouse: Using device discovery";
        m_deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Mouse | QDeviceDiscovery::Device_Touchpad, this);
        if (m_deviceDiscovery) {
            // scan and add already connected keyboards
            QStringList devices = m_deviceDiscovery->scanConnectedDevices();
            foreach (const QString &device, devices) {
                addMouse(device);
            }
            connect(m_deviceDiscovery, SIGNAL(deviceDetected(QString)), this, SLOT(addMouse(QString)));
            connect(m_deviceDiscovery, SIGNAL(deviceRemoved(QString)), this, SLOT(removeMouse(QString)));
        }
    }
    connect(QGuiApplicationPrivate::inputDeviceManager(), SIGNAL(cursorPositionChangeRequested(QPoint)),
            this, SLOT(handleCursorPositionChange(QPoint)));
}

      去创建一个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/"

#define QT_EVDEV_DEVICE_PREFIX "event"
#define QT_EVDEV_DEVICE QT_EVDEV_DEVICE_PATH QT_EVDEV_DEVICE_PREFIX

      可以看到了/dev/input/event前缀,但是我现在用的开发板上,不知道原厂做了什么,event是在/dev下的,前缀就成了/dev/event,所以就没有匹配到了。

      这里做个通用法,两个前缀都匹配下就好了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值