Application Programming in Linux Environment (8): Input System

One: input system

Input system introduction and driver reference: https://blog.csdn.net/qq_34968572/article/details/89875957

Reading and analysis of input system events:

1. Get device information instructions related to the event:

cat /proc/bus/input/devices

The corresponding meaning of the parameters:

I: Device ID

struct input_id {
	__u16 bustype; //总线类型
	__u16 vendor;  //与厂商相关的ID
	__u16 product; //与产品相关的ID
	__u16 version; //版本号
};

N: device name

P: The physical path of the device in the system hierarchy

S: The path located in the sys file system

U: the unique identification code of the device

H: List of input handles associated with the device

B: bitmap

PROP:设备属性。
EV:设备支持的事件类型。
KEY:此设备具有的键/按钮。
MSC:设备支持的其他事件。
LED:设备上的指示灯。

2. View event commands:

hexdump /dev/input/event1

First look at the event event type structure in the kernel, and then analyze a mouse event according to the hexdump command:

struct timeval {
	__kernel_time_t		tv_sec;		/* seconds */
	__kernel_suseconds_t	tv_usec;	/* microseconds */
};

struct input_event {
	struct timeval time;
	__u16 type;
	__u16 code;
	__s32 value;
};

Column 1: hexdump serial number

2~3 columns: seconds

Columns 4~5: subtle

6 columns: event type, relative event, absolute event, etc.

/*
 * Event types
 */

#define EV_SYN			0x00  同步事件
#define EV_KEY			0x01  按键事件
#define EV_REL			0x02  相对事件
#define EV_ABS			0x03  绝对事件
#define EV_MSC			0x04
#define EV_SW			0x05
#define EV_LED			0x11
#define EV_SND			0x12
#define EV_REP			0x14
#define EV_FF			0x15
#define EV_PWR			0x16
#define EV_FF_STATUS		0x17
#define EV_MAX			0x1f
#define EV_CNT			(EV_MAX+1)

Column 7: Event value. For relative events such as the mouse, the code represents the x or y coordinate relative to the current position of the mouse

/*
 * Keys and buttons
 *
 * Most of the keys/buttons are modeled after USB HUT 1.12
 * (see http://www.usb.org/developers/hidpage).
 * Abbreviations in the comments:
 * AC - Application Control
 * AL - Application Launch Button
 * SC - System Control
 */

#define KEY_RESERVED		0
#define KEY_ESC			1
#define KEY_1			2
#define KEY_2			3
#define KEY_3			4
#define KEY_4			5
... ...

Column 8: The meaning of the corresponding value of the specific device, which is expressed as the offset relative to the current position

Two: Application

struct input_event event_mouse ;
int fd    = -1 ;

fd = open("/dev/input/event2", O_RDONLY);
read(fd, &event_mouse, sizeof(event_mouse));
if(EV_ABS == event_mouse.type || EV_REL == event_mouse.type)
{
//code表示相对位移X或者Y,当判断是X时,打印X的相对位移value
//当判断是Y时,打印Y的相对位移value
if(event_mouse.code == REL_X)
{
    printf("event_mouse.code_X:%d\n", event_mouse.code);
    printf("event_mouse.value_X:%d\n", event_mouse.value);
}
else if(event_mouse.code == REL_Y)
{
    printf("event_mouse.code_Y:%d\n", event_mouse.code);
    printf("event_mouse.value_Y:%d\n", event_mouse.value);
}

 

Guess you like

Origin blog.csdn.net/qq_34968572/article/details/107403611