编写Linux下Input设备的检测程序

最近玩了下Android-x86的eclair,主题风格相对之前的1.6好看了很多,而且还加了不少之前没有的功能。但是触摸屏不能用,在终端下用getevent命令查看事件信息,发现得到的绝对坐标有很大出入(4096*4096)。于是想自己试着去解决这个问题。同时在Ubuntu下测试,发现居然没有getevent命令,当然可以用cat /dev/inpu/eventX查看事件信息,但是显示出来的是一堆乱码,怎么办?那就自己编写个Linux输入设备的测试小程序来检测问题所在吧,最后测试结果发现和在Android下一样的问题-没有校正,总算也小有成就。接下来应该再dig触摸屏的校正问题:-)

现在把写的测试程序放上来供大家参考:
代码中宏定义KEY_DEV、MOU_DEV、TOUS_DEV、TOUP_DEV是Linux下键盘,鼠标,触摸屏和触摸板的设备文件路径,对于不同的系统和机器可能有所不同,大家可以用cat /proc/bus/input/devices查看自己机器的设备文件。

用gcc -o outfilename filename编译生成可执行文件,然后./outfilename运行文件,注意修改生成的可执行文件权限,不想修改权限的用sudo ./outfilename。目前还没加入测试过程中切换测试设备的功能,只能用CTRL+Z结束程序,再重新执行文件。有时间可以再摸索下加个这个功能,呵呵~~

#include
#include
#include
#include
#include
#define KEY_DEV "/dev/input/event5"
#define MOU_DEV "/dev/input/event6"
#define TOUS_DEV "/dev/input/event7"
#define TOUP_DEV "/dev/input/event11"

static int ts_fd = -1;
static struct input_event data;

static int init_device(char *TS_DEV)
{
    if((ts_fd = open(TS_DEV, O_RDONLY)) < 0)
    {
        printf("Error open %s\n\n", TS_DEV);
        return -1;
    }
    return ts_fd;
}

static int test_key()
{
    if(init_device(KEY_DEV) < 0)
        return -1;
    while(1)
    {
        read(ts_fd, &data, sizeof(data));
        if (data.type == EV_KEY)
            printf(" type: EV_KEY, event = %d, value = %d\n",data.code, data.value);
    }
    return 0;
}

static int test_mouse()
{
    if(init_device(MOU_DEV) < 0)
        return -1;
    while(1)
    {
        read(ts_fd, &data, sizeof(data));
        if (data.type == EV_KEY)
        {
            printf(" type = EV_KEY, code = %s, value = %d\n",
                data.code == BTN_LEFT ? "MOUSE_LEFT" :
                data.code == BTN_RIGHT ? "MOUSE_RIGHT" :
                data.code == BTN_MIDDLE ? "MOUSE_MIDDLE" :
                data.code == BTN_SIDE ? "MOUSE_SIDE" :
                "Unkonw", data.value);
        }
        else if(data.type == EV_REL)
        {
            printf(" type = EV_ABS, code = %s, value = %d\n",
                data.code == REL_X ? "ABS_X" :
                data.code == REL_Y ? "ABS_Y" :
                data.code == ABS_WHEEL ? "MOUSE_WHEEL" :
                data.code == ABS_PRESSURE ? "ABS_PRESSURE" :
                "Unkown", data.value);
        }
    }
    return 0;
}

static int test_touch_screen()
{
    if(init_device(TOUS_DEV) < 0)
        return -1;
    while(1)
    {
        read(ts_fd, &data, sizeof(data));
        if (data.type == EV_KEY)
        {
            printf(" type: EV_KEY, event = %s, value = %d\n\n",
                data.code == BTN_TOUCH ? "BTN_TOUCH" : "Unkown", data.value);
        }
        else if(data.type == EV_ABS)
        {
            printf(" type: EV_ABS, event = %s, value = %d\n\n",
                data.code == ABS_X ? "ABS_X" :
                data.code == ABS_Y ? "ABS_Y" :
                data.code == ABS_PRESSURE ? "ABS_PRESSURE" :
                "Unkown", data.value);
        }
    }
    return 0;
}

static int test_touch_pancel()
{
    if(init_device(TOUP_DEV) < 0)
        return -1;
    while(1)
    {
        read(ts_fd, &data, sizeof(data));
        if (data.type == EV_KEY)
        {
            printf(" type = EV_KEY, code = %s, value = %d\n",
                data.code == BTN_LEFT ? "MOUSE_LEFT" :
                data.code == BTN_RIGHT ? "MOUSE_RIGHT" :
                data.code == BTN_MIDDLE ? "MOUSE_MIDDLE" :
                data.code == BTN_SIDE ? "MOUSE_SIDE" :
                "Unkonw", data.value);
        }
        else if(data.type == EV_REL)
        {
            printf(" type = EV_ABS, code = %s, value = %d\n",
                data.code == REL_X ? "ABS_X" :
                data.code == REL_Y ? "ABS_Y" :
                data.code == ABS_WHEEL ? "MOUSE_WHEEL" :
                data.code == ABS_PRESSURE ? "ABS_PRESSURE" :
                "Unkown", data.value);
        }
    }
    return 0;
}

int main()
{
    static int i;
select:    printf("Please select device:\n0.KeyBoard\n1.Mouse\n2.TouchScreen\n3.TouchPancel\n");
    scanf("%d",&i);
    switch(i){
        case 0:
            test_key();
            break;
        case 1:
            test_mouse();
            break;
        case 2:
            test_touch_screen();
            break;
        case 3:
            test_touch_pancel();
            break;
        default:
            printf("Wrong device, Please select again!\n\n");
            break;       
    }
    goto select;
        return 0;
}

相关推荐