149 lines
4.2 KiB
C
149 lines
4.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <linux/input.h>
|
|
#include "evdev.h"
|
|
#include <errno.h>
|
|
|
|
static int fd_Mouse;
|
|
|
|
/*------------------
|
|
* Mouse
|
|
* -----------------*/
|
|
|
|
/*Initialize your mouse*/
|
|
void mouse_init(void)
|
|
{
|
|
fd_Mouse = open("/dev/input/event6", O_RDONLY);
|
|
if (fd_Mouse == -1) {
|
|
perror("Error opening device");
|
|
return;
|
|
}
|
|
// 设置为非阻塞模式
|
|
int flags = fcntl(fd_Mouse, F_GETFL, 0);
|
|
fcntl(fd_Mouse, F_SETFL, flags | O_NONBLOCK);
|
|
// printf("Listening for mouse events on /dev/input/event6:\n");
|
|
}
|
|
|
|
static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y)
|
|
{
|
|
/*Your code comes here*/
|
|
struct input_event ie;
|
|
ssize_t bytes = read(fd_Mouse, &ie, sizeof(struct input_event));
|
|
if (bytes < 0) {
|
|
if (errno == EAGAIN) {
|
|
// 没有可用数据,继续循环
|
|
} else {
|
|
perror("Error reading");
|
|
close(fd_Mouse);
|
|
}
|
|
}
|
|
// 只处理鼠标事件
|
|
if (ie.type == EV_REL) {
|
|
if (ie.code == REL_X) {
|
|
// printf("Mouse moved horizontally: %d\n", ie.value);
|
|
(*x) = abs(ie.value);
|
|
} else if (ie.code == REL_Y) {
|
|
(*y) = abs(ie.value);
|
|
// printf("Mouse moved vertically: %d\n", ie.value);
|
|
}
|
|
} else if (ie.type == EV_KEY) {
|
|
if (ie.code == BTN_LEFT) {
|
|
// printf("Left button %s\n", ie.value ? "pressed" : "released");
|
|
} else if (ie.code == BTN_RIGHT) {
|
|
// printf("Right button %s\n", ie.value ? "pressed" : "released");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/*Return true is the mouse button is pressed*/
|
|
static bool mouse_is_pressed(void)
|
|
{
|
|
|
|
return false;
|
|
}
|
|
|
|
/*Will be called by the library to read the mouse*/
|
|
void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
|
|
{
|
|
/*Get the current x and y coordinates*/
|
|
// mouse_get_xy(&data->point.x, &data->point.y);
|
|
|
|
struct input_event ie;
|
|
ssize_t bytes = read(fd_Mouse, &ie, sizeof(struct input_event));
|
|
if (bytes < 0) {
|
|
if (errno == EAGAIN) {
|
|
// 没有可用数据,继续循环
|
|
} else {
|
|
perror("Error reading");
|
|
close(fd_Mouse);
|
|
}
|
|
}
|
|
// 只处理鼠标事件
|
|
if (ie.type == EV_REL) {
|
|
if (ie.code == REL_X) {
|
|
// printf("Mouse moved horizontally: %d\n", ie.value);
|
|
data->point.x = 240;//abs(ie.value);
|
|
} else if (ie.code == REL_Y) {
|
|
data->point.y= 120;//abs(ie.value);
|
|
// printf("Mouse moved vertically: %d\n", ie.value);
|
|
}
|
|
} else if (ie.type == EV_KEY) {
|
|
if (ie.code == BTN_LEFT) {
|
|
data->state = LV_INDEV_STATE_PR;
|
|
//printf("Left button %s\n", ie.value ? "pressed" : "released");
|
|
} else if (ie.code == BTN_RIGHT) {
|
|
//printf("Right button %s\n", ie.value ? "pressed" : "released");
|
|
}else{
|
|
data->state = LV_INDEV_STATE_REL;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
int mouse_test() {
|
|
int fd;
|
|
struct input_event ie;
|
|
|
|
// 打开鼠标设备
|
|
fd = open("/dev/input/event6", O_RDONLY);
|
|
if (fd == -1) {
|
|
perror("Error opening device");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
printf("Listening for mouse events on /dev/input/event6:\n");
|
|
|
|
// 读取鼠标事件
|
|
while (1) {
|
|
if (read(fd, &ie, sizeof(struct input_event)) < (int)sizeof(struct input_event)) {
|
|
perror("Error reading");
|
|
close(fd);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// 只处理鼠标事件
|
|
if (ie.type == EV_REL) {
|
|
if (ie.code == REL_X) {
|
|
printf("Mouse moved horizontally: %d\n", ie.value);
|
|
} else if (ie.code == REL_Y) {
|
|
printf("Mouse moved vertically: %d\n", ie.value);
|
|
}
|
|
} else if (ie.type == EV_KEY) {
|
|
if (ie.code == BTN_LEFT) {
|
|
printf("Left button %s\n", ie.value ? "pressed" : "released");
|
|
} else if (ie.code == BTN_RIGHT) {
|
|
printf("Right button %s\n", ie.value ? "pressed" : "released");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 关闭设备
|
|
close(fd);
|
|
return EXIT_SUCCESS;
|
|
} |