#include #include #include #include #include #include #include #include #include #include #include #include "keyboard.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "confile.h" #include "Util.h" //#define KEY_EVENT_DEV0_NAME "/dev/input/event0" #define KEY_EVENT_DEV0_NAME "/dev/input/event1" // 长按阈值(毫秒) #define LONG_PRESS_THRESHOLD 1000 // 屏幕坐标范围定义 #define SCREEN_WIDTH 4096 #define SCREEN_HEIGHT 4096 //int switch_video; unsigned char key_value; typedef struct { int min_x; int max_x; int min_y; int max_y; } TouchArea; // 触摸区域定义 static const TouchArea AREA1 = {0, 1024, 2048, 4096}; static const TouchArea AREA2 = {1024, 3072, 0, 1365}; static const TouchArea AREA3 = {1024, 3072, 1365, 4096}; static const TouchArea AREA4 = {3072, 4096, 2048, 4096}; static const TouchArea FULL_SCREEN = {0, SCREEN_WIDTH, 0, SCREEN_HEIGHT}; /** * 检查坐标是否在指定区域内 */ static int is_point_in_area(int x, int y, const TouchArea *area) { return (x >= area->min_x && x < area->max_x && y >= area->min_y && y < area->max_y); } int *switch_memory_open(void) { char buf[128]; int fd; sprintf(buf, "/video_switch"); fd = shm_open(buf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if(fd < 0) { perror("shm_open"); exit(1); } if (ftruncate(fd, 4096) == -1) { perror("ftruncate"); exit(1); } int *retval = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); return retval; } int main() { // 打开触摸屏设备节点 int fd = open(KEY_EVENT_DEV0_NAME, O_RDONLY); if (fd < 0) { perror("打开event0失败"); return -1; } int *switch_mode; // 初始化输入事件结构体 struct input_event key_event = {0}; // 长按判定所需变量 long press_sec = 0, press_usec = 0; // 按下时间戳 int x_coord = 0, y_coord = 0; // 触摸坐标 int is_press = 0; // 触摸按下标记 switch_mode = switch_memory_open(); //printf("switch %d \n",switch_mode[0]); while (1) { // 读取1个input_event(16字节),阻塞等待触摸事件 ssize_t len = read(fd, &key_event, sizeof(struct input_event)); if (len != sizeof(struct input_event)) { perror("读取事件失败"); close(fd); return -1; } //提取坐标(EV_ABS) if (key_event.type == EV_ABS) { if (key_event.code == ABS_X) { x_coord = key_event.value; // X轴坐标(无需转小端,内核已处理) } else if (key_event.code == ABS_Y) { y_coord = key_event.value; // Y轴坐标 } } // 解析触摸按下/松开(EV_KEY + BTN_TOUCH) else if (key_event.type == EV_KEY && key_event.code == BTN_TOUCH) { // 触摸按下 if (key_event.value == 1) { press_sec = key_event.time.tv_sec; press_usec = key_event.time.tv_usec; is_press = 1; } // 触摸松开(判定长按) else if (key_event.value == 0 && is_press) { // 计算时间差(毫秒) long diff_sec = key_event.time.tv_sec - press_sec; long diff_usec = key_event.time.tv_usec - press_usec; long diff_ms = diff_sec * 1000 + diff_usec / 1000; // 判定长按/短按 if (diff_ms >= LONG_PRESS_THRESHOLD) { //printf("长按判定 | 持续%ldms | 坐标:X=%d, Y=%d\n", diff_ms, x_coord, y_coord); if(switch_mode[0]==5 ||switch_mode[0]==6){ if(is_point_in_area(x_coord, y_coord, &AREA1)){ if(switch_mode[0]==5) switch_mode[0]=1; else switch_mode[0]=2; }else if(is_point_in_area(x_coord, y_coord, &AREA2)){ if(switch_mode[0]==5) switch_mode[0]=2; else switch_mode[0]=1; }else if(is_point_in_area(x_coord, y_coord, &AREA3)){ switch_mode[0]=3; }else if(is_point_in_area(x_coord, y_coord, &AREA4)){ switch_mode[0]=4; } }else if(switch_mode[0]==1 ||switch_mode[0]==2 ||switch_mode[0]==3||switch_mode[0]==4 ){ if(is_point_in_area(x_coord, y_coord, &FULL_SCREEN)) switch_mode[0]=5; } } else { //printf("短按 | 持续%ldms | 坐标:X=%d, Y=%d\n", diff_ms, x_coord, y_coord); } // 重置状态 is_press = 0; x_coord = 0; y_coord = 0; } } // 同步事件(EV_SYN,无实际值,仅标记一组事件结束) //else if (key_event.type == EV_SYN) { //} } close(fd); return 0; }