程式語言 - GNU - C/C++ - Touch Event



main.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
 
int main(int argc, char **argv)
{
    int fd = -1;
    int valid = 0;
    struct input_event ev = {0};
 
    fd = open(argv[1], O_RDONLY);
    if (fd > 0) {
        while (read(fd, &ev, sizeof(struct input_event))) {
            //printf("type:%d, code:%d, value:%d\n", ev.type, ev.code, ev.value);

            if (ev.type == EV_ABS) {
                if (ev.code == ABS_MT_TRACKING_ID) {
                    valid = 1;
                    printf("ID=%d, ", ev.value);
                }
                else if (ev.code == ABS_MT_POSITION_X) {
                    valid = 1;
                    printf("X=%d, ", ev.value);
                }
                else if (ev.code == ABS_MT_POSITION_Y) {
                    valid = 1;
                    printf("Y=%d, ", ev.value);
                }
                else if (ev.code == ABS_MT_PRESSURE) {
                    valid = 1;
                    printf("Pressure=%d, ", ev.value);
                }
            }
            else if (ev.type == EV_SYN) {
                if ((ev.code == ABS_Z) && (ev.value == 0)) {
                    if (valid) {
                        valid = 0;
                        printf("\n");
                    }
                    else {
                        printf("Released\n");
                    }
                }
            }
        }
        close(fd);
    }
    return 0;
}