在基于linux的hmi屏幕中使用uinput触摸事件模拟不起作用?

时间:2016-01-21 12:15:07

标签: c linux embedded uinput

以下是我尝试将触摸事件从用户空间发送到我们的hmi的代码。显式检查所有ioctl调用和写入是否成功但未注入事件。未正确输入供应商ID和产品ID设备名称。不确定它是否与输出相关。 (事件注入的所有三种方法都已逐一尝试)。它需要供应商ID产品ID和设备名称,然后我们将从基于Linux的嵌入式系统中获取这些数据。

#include <linux/input.h>
#include <linux/uinput.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

void injectEvent(int, int, int, int);

int main()
{
    struct uinput_user_dev dev;
    int i;
    int err;
    int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if(fd < 0) {
        //perror("Failed to open: /dev/uinput");
        printf("Failed to open: /dev/uinput\n");
        return fd;
    }

    err = ioctl(fd, UI_SET_EVBIT, EV_KEY);
    if (err)
        goto err;

    err = ioctl(fd, UI_SET_EVBIT, EV_ABS);
    if (err)
        goto err;

    err = ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
    if (err)
        goto err;

    err = ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
    if (err)
        goto err;

    memset(&dev, 0, sizeof(dev));
    snprintf(dev.name, sizeof(dev.name), "Test device");
    printf("devname = %s\n",dev.name);
    dev.id.bustype = BUS_SPI;

    /* 10 touch inputs */
    dev.absmax[ABS_MT_SLOT] = 10;

    dev.absmax[ABS_X] = 4096;
    dev.absmin[ABS_X] = 0;

    dev.absmax[ABS_Y] = 4096;
    dev.absmin[ABS_Y] = 0;

    dev.absmax[ABS_PRESSURE] = 0xff;
    dev.absmin[ABS_PRESSURE] = 0;

    dev.absmax[ABS_MT_TOUCH_MAJOR] = 0xff;
    dev.absmin[ABS_MT_TOUCH_MAJOR] = 0;

    dev.absmax[ABS_MT_POSITION_X] = 4096;
    dev.absmin[ABS_MT_POSITION_X] = 0;

    dev.absmax[ABS_MT_POSITION_Y] = 4096;
    dev.absmin[ABS_MT_POSITION_Y] = 0;

    dev.absmax[ABS_MT_PRESSURE] = 4096;
    dev.absmin[ABS_MT_PRESSURE] = 0;

    err = write(fd, &dev, sizeof(dev));
    if (err < 0)
        goto err;

    err = ioctl(fd, UI_DEV_CREATE);
    if (err < 0)
        goto err;

    /* Event should be injected here.... */

    /********************First Method***********************/
#if 1
    struct input_event ev[2];

    memset(ev, 0, sizeof(ev));

    ev[0].type = EV_ABS;
    ev[0].code = ABS_X;
    ev[0].value = 1001;
    ev[1].type = EV_ABS;
    ev[1].code = ABS_Y;
    ev[1].value = 2002;

    if(write(fd, &ev, sizeof(ev)) < 0)
        printf("Error::event injection failed\n");

#endif

    /********************Second Method***********************/
#if 0
    injectEvent( fd, EV_ABS, ABS_MT_TRACKING_ID, 0  );
    injectEvent( fd, EV_ABS, ABS_MT_POSITION_X, 1001  );
    injectEvent( fd, EV_ABS, ABS_MT_POSITION_Y, 2002  );
    injectEvent( fd, EV_ABS, ABS_MT_TOUCH_MAJOR,    111 );
    injectEvent( fd, EV_ABS, ABS_MT_PRESSURE,   3003 );
    injectEvent( fd, EV_SYN, SYN_MT_REPORT, 0   );
    injectEvent( fd, EV_SYN, SYN_REPORT, 0  );
    injectEvent( fd, EV_SYN, SYN_MT_REPORT, 0   );
    injectEvent( fd, EV_SYN, SYN_REPORT, 0  );

    void injectEvent(int fd_ev,int type, int code, int value)
    {
        printf("(%s)==>> (%d,%d,%d,%d)\n",__func__, fd_ev, type, code, value);

        struct uinput_event event;
        int len;

        if (fd_ev <= fileno(stderr))
            return;

        memset(&event, 0, sizeof(event));
        event.type = type;
        event.code = code;
        event.value = value;

        len = write( fd_ev, &event, sizeof(event) );

        printf("(%s) done:%d\n",__func__,len);
    }
#endif

    /********************Third Method***********************/
#if 0
    struct uinput_event event;
    for (int i=0; i<30; i++)
    {
        memset(&event, 0, sizeof(event));

        event.type=EV_ABS;
        event.code=ABS_X;
        event.value=1005;

        if(write(fd, &event, sizeof(event)) < 0)
            printf("Error::event injection failed\n");

        sleep (5);
    }
#endif

    sleep (180);
    /* start cleanup ... */
    err = ioctl(fd, UI_DEV_DESTROY);
    if (err < 0)
        goto err;

    close(fd);
    return 0;
err:
    //perror("Failed to initialise");
    printf("(%s) Failed to initialise\n",__func__);
    close(fd);
    return err;
}

1 个答案:

答案 0 :(得分:0)

我不知道这是否是你的解决方案,但ABS_X和ABS_Y不能与ABS_MT_POSITION_X和* _Y一起设置,它们不能共存,你必须激活一个或另一个。你可以使用&#34; xinput list-props + device number&#34;来检查这个。在linux中。希望它能帮到你;)