Files
hisi_SS524V100_SDK/mpp/sample/sdi_cut/touch_sdi.c
T

258 lines
7.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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" //event0
// 长按阈值(毫秒)
#define LONG_PRESS_THRESHOLD 1500
// 屏幕坐标范围定义
#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 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 = {2048, 2048, 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);
}
// 长按切换
static void handle_long_press(int x, int y)
{
int count;
if(switch_mode[0]==1 ){
if(is_point_in_area(x, y, &AREA1)){
switch_mode[0]=2;
}else if(is_point_in_area(x, y, &AREA2)){
switch_mode[0]=3;
}else if(is_point_in_area(x, y, &AREA3)){
switch_mode[0]=4;
}else if(is_point_in_area(x, y, &AREA4)){
switch_mode[0]=5;
}
count = switch_mode[0];
sendto(socket_descriptor,&count,4,0,(struct sockaddr *)&address,sizeof(address));
printf("count %x\n",switch_mode[0]);
}else{
if(is_point_in_area(x, y, &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]);
}
}
}
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 get_parameter(struct decoce_param *pparam)
{
if(confile_init()) {
printf("config file parse filed!\n");
return -1;
}
pparam->sdi_switch = get_conf_int("sdi_switch", 0);
pparam->osd_on = get_conf_int("osd_on", 0);
//pparam->light = get_conf_int("light", 0);
printf("sdi %d osd %d ",pparam->sdi_switch,pparam->osd_on);
return 0;
}
int main(int argc, char **argv) {
int fd;
struct input_event touch_event = {0};
int len,count,port;
pthread_attr_t attr;
struct decoce_param param;
long press_sec = 0, press_usec = 0; // 按下时间戳
int x_coord = 0, y_coord = 0; // 触摸坐标
int is_press = 0; // 触摸按下标记
int long_triggered = 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();
get_parameter(&param);
//printf("---\n");
//switch_mode[0] = 0x01;
//count=1;
switch_mode[0] = param.sdi_switch;
//count = param.sdi_switch;
switch_mode[1] = param.osd_on;
//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;
long_triggered = 0;
printf("触摸按下\n");
}
else if (touch_event.value == 0 && is_press) {
is_press = 0;
long_triggered = 0;
x_coord = 0;
y_coord = 0;
printf("触摸抬下\n");
}
}
}
// 触摸松开(判定长按)
if (is_press && !long_triggered) {
// 计算时间差(毫秒)
struct timeval now;
gettimeofday(&now, NULL);
long diff_sec = now.tv_sec - press_sec;
long diff_usec = now.tv_usec - press_usec;
long diff_ms = diff_sec * 1000 + diff_usec / 1000;
// printf("time %d \n",diff_ms);
// 判定长按/短按
if (diff_ms >= LONG_PRESS_THRESHOLD) {
//printf("长按判定 | 持续%ldms | 坐标:X=%d, Y=%d\n", diff_ms, x_coord, y_coord);
handle_long_press(x_coord,y_coord);
count=switch_mode[0];
//set_conf_int("sdi_switch",count);
//set_conf_int("light",switch_mode[3]);
//save_confile("/root/decode.cfg");
long_triggered = 1;
}
}
usleep(500);
}
close(fd);
return 0;
}