#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" //event0 // 长按阈值(毫秒) #define LONG_PRESS_THRESHOLD 1000 // 屏幕坐标范围定义 #define SCREEN_WIDTH 4096 #define SCREEN_HEIGHT 4096 #define DEFAULT_MD_LEN 4096 static pthread_t pkts_thread; int socket_descriptor; struct sockaddr_in address; int *switch_mode; //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 = {1024, 2048, 0, 682}; //ahd1 7 static const TouchArea AREA2 = {2048, 3072, 0, 682}; //ahd2 8 static const TouchArea AREA3 = {1024, 3072, 683, 2578}; //sdi1 3 static const TouchArea AREA4 = {0, 1024, 1800, 4096};//sdi2 4 static const TouchArea AREA5 = {3072, 4096, 180, 4096};//sdi3 5 static const TouchArea AREA6 = {1024, 3072, 2580, 4096};//sdi4 6 static const TouchArea AREA3_2 = {1024, 3072, 2275, 4096}; //sdi1 3 static const TouchArea AREA4_2 = {3072, 4096, 2276, 4096};//sdi2 4 static const TouchArea AREA5_2 = {0, 1024, 2276, 4096};//sdi3 5 static const TouchArea AREA6_2 = {1024, 3072, 0, 2274};//sdi4 6 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; } void* pkts_worker(void *arg) { printf("pkts_worker...\n"); int anjian_switch=0; uint32_t ulOld; int read_key; void* pMem = NULL; while(1) { //anjian_switch =count; //if(anjian_switch ==(switch_video+1)){ // anjian_switch =1; //} anjian_switch = switch_mode[0]; //printf("-------------%x\n",anjian_switch); sendto(socket_descriptor,&anjian_switch,4,0,(struct sockaddr *)&address,sizeof(address)); usleep(500000); } } int main(int argc, char **argv) { int fd; struct input_event touch_event = {0}; int len,count,port; pthread_attr_t attr; long press_sec = 0, press_usec = 0; // 按下时间戳 int x_coord = 0, y_coord = 0; // 触摸坐标 int is_press = 0; // 触摸按下标记 bzero(&address,sizeof(address)); address.sin_family=AF_INET; address.sin_addr.s_addr=inet_addr(argv[1]); port = atoi(argv[2]); address.sin_port=htons(port); socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);//IPV4 // 打开触摸屏设备节点 fd = open(KEY_EVENT_DEV0_NAME, O_RDONLY); if (fd < 0) { perror("open /dev/input/event0 device(touch) error! ---\n"); return -1; } pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); len = pthread_create(&pkts_thread, &attr, pkts_worker, NULL); pthread_attr_destroy(&attr); switch_mode = switch_memory_open(); switch_mode[0] = 1; count = switch_mode[0]; printf("switch %d \n",switch_mode[0]); while (1) { // 读取1个input_event(16字节),阻塞等待触摸事件 ssize_t len = read(fd, &touch_event,sizeof(struct input_event)); count=switch_mode[0]; if (len != sizeof(struct input_event)) { perror("读取事件失败"); close(fd); return -1; }else{ //提取坐标(EV_ABS) if (touch_event.type == EV_ABS) { if (touch_event.code == ABS_X) { x_coord = touch_event.value; // X轴坐标(无需转小端,内核已处理) } else if (touch_event.code == ABS_Y) { y_coord = touch_event.value; // Y轴坐标 } //printf("判定 | 坐标:X=%d, Y=%d\n", x_coord, y_coord); } // 解析触摸按下/松开(EV_KEY + BTN_TOUCH) else if (touch_event.type == EV_KEY && touch_event.code == BTN_TOUCH) { //printf("长按判定 | 坐标:X=%d, Y=%d\n", x_coord, y_coord); // 触摸按下 if (touch_event.value == 1){ //记录按下时间(秒) press_sec = touch_event.time.tv_sec; press_usec = touch_event.time.tv_usec; is_press = 1; } // 触摸松开(判定长按) else if (touch_event.value == 0 && is_press) { // 计算时间差(毫秒) long diff_sec = touch_event.time.tv_sec - press_sec; long diff_usec = touch_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]==1 ){ if(is_point_in_area(x_coord, y_coord, &AREA1)){ switch_mode[0]=7; }else if(is_point_in_area(x_coord, y_coord, &AREA2)){ switch_mode[0]=8; }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(is_point_in_area(x_coord, y_coord, &AREA5)){ switch_mode[0]=5; }else if(is_point_in_area(x_coord, y_coord, &AREA6)){ switch_mode[0]=6; } count = switch_mode[0]; sendto(socket_descriptor,&count,4,0,(struct sockaddr *)&address,sizeof(address)); printf("count %x\n",switch_mode[0]); }else if(switch_mode[0]==2 ){ if(is_point_in_area(x_coord, y_coord, &AREA3_2)){ switch_mode[0]=3; }else if(is_point_in_area(x_coord, y_coord, &AREA4_2)){ switch_mode[0]=4; }else if(is_point_in_area(x_coord, y_coord, &AREA5_2)){ switch_mode[0]=5; }else if(is_point_in_area(x_coord, y_coord, &AREA6_2)){ switch_mode[0]=6; } count = switch_mode[0]; sendto(socket_descriptor,&count,4,0,(struct sockaddr *)&address,sizeof(address)); printf("switch_mode[0] %x\n",switch_mode[0]); }else{ //else if(switch_mode[0]==3 ||switch_mode[0]==4 ||switch_mode[0]==5||switch_mode[0]==6||switch_mode[0]==7||switch_mode[0]==8 ){ if(is_point_in_area(x_coord, y_coord, &FULL_SCREEN)){ switch_mode[0]=1; count = switch_mode[0]; sendto(socket_descriptor,&count,4,0,(struct sockaddr *)&address,sizeof(address)); printf("switch_mode %x\n",switch_mode[0]); } } } 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; }