183 lines
5.7 KiB
C
183 lines
5.7 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <fcntl.h>
|
||
#include <unistd.h>
|
||
#include <linux/input.h>
|
||
#include <sys/time.h>
|
||
#include <linux/input.h>
|
||
#include <fcntl.h>
|
||
#include <stdio.h>
|
||
#include <stdint.h>
|
||
#include <pthread.h>
|
||
#include "keyboard.h"
|
||
#include <unistd.h>
|
||
#include <stdlib.h>
|
||
#include <sys/stat.h>
|
||
#include <string.h>
|
||
#include <sys/socket.h>
|
||
#include <netinet/in.h>
|
||
#include <arpa/inet.h>
|
||
#include <netdb.h>
|
||
#include <unistd.h>
|
||
#include <sys/types.h>
|
||
#include <sys/time.h>
|
||
#include <fcntl.h>
|
||
#include <sys/mman.h>
|
||
#include <arpa/inet.h>
|
||
#include <netdb.h>
|
||
#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 min_y;
|
||
int max_x;
|
||
int max_y;
|
||
} TouchArea;
|
||
|
||
// 触摸区域定义
|
||
static const TouchArea AREA1 = {0, 0, 2048, 2048};
|
||
static const TouchArea AREA2 = {2048, 0, 4096, 2048};
|
||
static const TouchArea AREA3 = {0, 2048, 2048, 4096};
|
||
static const TouchArea AREA4 = {0, 2048, 4096, 4096}; //{2048, 2048, 4096, 4096}
|
||
//static const TouchArea AREA1 = {0, 0, 1024, 4096};
|
||
//static const TouchArea AREA2 = {1024, 0, 3072, 2730};
|
||
//static const TouchArea AREA3 = {1024, 2730, 3072, 4096};
|
||
//static const TouchArea AREA4 = {3072, 0, 4096, 4096};
|
||
static const TouchArea FULL_SCREEN = {0,0, SCREEN_WIDTH, 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");
|
||
//sprintf(buf, "/switch_mode");
|
||
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 count;
|
||
// 打开触摸屏设备节点
|
||
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]);
|
||
count = switch_mode[0];
|
||
while (1) {
|
||
count =switch_mode[0];
|
||
//printf("count %d \n",count);
|
||
// 读取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 ) {
|
||
// 计算时间差(毫秒)
|
||
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(count==1){
|
||
//if(switch_mode[0]==1){
|
||
if(is_point_in_area(x_coord, y_coord, &AREA1)){
|
||
count=2;
|
||
switch_mode[0]=count;
|
||
printf("--count %d \n",count);
|
||
}else if(is_point_in_area(x_coord, y_coord, &AREA2)){
|
||
count=3;
|
||
switch_mode[0]=count;
|
||
printf("--count %d \n",count);
|
||
|
||
}else if(is_point_in_area(x_coord, y_coord, &AREA3)){
|
||
count=4;
|
||
switch_mode[0]=count;
|
||
printf("--count %d \n",count);
|
||
}else if(is_point_in_area(x_coord, y_coord, &AREA4)){
|
||
count=5;
|
||
switch_mode[0]=count;
|
||
printf("--count %d \n",count);
|
||
}
|
||
|
||
}else if(count==2 ||count==3 ||count==4 ||count==5 ){
|
||
if(is_point_in_area(x_coord, y_coord, &FULL_SCREEN))
|
||
{ count=1;
|
||
switch_mode[0]=count;
|
||
printf("--count %d \n",count);
|
||
}
|
||
}
|
||
|
||
} 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;
|
||
}
|