Struct input under Linux_ Detailed explanation of event structure (refer to Guangdong embedded data)

4.4 touch screen application interface

4.4.1 introduction to input subsystem

There can be more than one input device connected to the operating system, perhaps a standard PS/2 keyboard, perhaps a USB mouse, or a touch screen, or even a game console joystick. When dealing with these complex and different input devices, Linux still uses the middle layer to shield various details. Please see the following figure:

In the Linux kernel, the use of input devices is actually managed by three blocks: the so-called input device driver layer, the core layer of input subsystem, and the event trigger layer. Their respective jobs are:

1. Input device driver layer:

Each device has its specific driver, which is properly loaded into the device model framework of the operating system, encapsulates the functions provided by the hardware and provides the specified interface upward.

2. Core layer:

Here, the data sent by the device driver layer will be collected and an event will be triggered after integration.
3. Event trigger layer:
This layer is what we need to pay attention to. We can learn a certain action of a device by reading the node file of the corresponding device in the user space. On the event trigger layer closest to the application, all kinds of input events known to the kernel, such as the keyboard being pressed, the touch screen being slid, etc., will be uniformly encapsulated in one called input_ The structure of event is defined as follows:

vincent@ubuntu:/usr/inlucde/linux/$ cat input.h -n
1 #ifndef _INPUT_H
2 #define _INPUT_H
21 struct input_event {
22 struct timeval time;
23 __u16 type;
24 __u16 code;
25 __s32 value;
26 };

The structure has four members, which have the following meanings:
1, Time: enter the timestamp of the event, accurate to microseconds. The time structure is defined as follows:

struct timeval
{ __time_t tv_sec; // second
long int tv_usec; // Microseconds (1 microsecond = 10-3 milliseconds = 10-6 seconds)

2, Type: enter the type of event. For example:
EV_SYN: the division mark between events. Some events may continue in time and space, such as continuously pressing and holding a key. To better manage these ongoing events, EV_SYN is used to divide them into small packets.
EV_KEY: used to describe the state change of keyboard, key or similar keyboard equipment.
EV_REL: relative displacement, such as mouse movement, roller rotation, etc.
EV_ABS: absolute displacement, such as the coordinate value on the touch screen.
EV_MSC: cannot match the existing type, which is equivalent to an event that is not recognized at present. For example, pressing the "one click antivirus" button in the keyboard for Windows system in Linux system will generate this event.
EV_LED: the switch used to control the LED lights on the equipment. For example, pressing the capital lock key of the keyboard will generate "EV" at the same time_ "Key" and "EV"_ Led "two events.

3, Code: this "event code" is used to further describe the type of event. For example, when EV occurs_ In case of key event, the keyboard may be pressed, so which key is pressed? Check the code at this time. When EV occurs_ In rel event, maybe the mouse moves or the scroll wheel moves. This can be distinguished by the value of code.

4, Value: when the code is not enough to distinguish the nature of the event, you can use value to confirm. For example, by EV_REL and REL_WHEEL confirms that the action of the mouse wheel has occurred, but does it roll up or down?
Another example is by EV_KEY and KEY_F confirms the action of the F key on the keyboard, but is it pressed or bounced? At this time, you can use the value value to further judge.
The following code shows how to read data from the touch screen device node / dev/event0 and display the real-time original data of the current touch screen:

vincent@ubuntu:~/ch04/4.5$ cat ts_raw.c -```
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <strings.h>
7 #include <errno.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <Linux/input.h>
14 int main(int argc, char **argv)
15 {
16 int ts = open("/dev/event0", O_RDONLY);
18 struct input_event buf;
19 bzero(&buf, sizeof(buf));
21 while(1)
22 {
23 read(ts, &buf, sizeof(buf));
25 switch(buf.type)
26 {
27 case EV_SYN:
28 printf("--------------------- SYN ------------------\n");
29 break;
30 case EV_ABS:
31 printf("time: %u.%u\ttype: EV_ABS\t", 32 buf.time.tv_sec, buf.time.tv_usec);
33 switch(buf.code)
34 {
35 case ABS_X:
36 printf("X:%u\n", buf.value);
37 break;
38 case ABS_Y:
39 printf("Y:%u\n", buf.value);
40 break;
42 printf("pressure:%u\n", buf.value);
43 }
44 }
45 }
46 return 0;
47 }

Note that the above code prints raw data directly read from the touch screen without any correction, filtering, de chattering and denoising. Therefore, these data can not be directly used by the application layer program, but don't worry. These cumbersome and complex tasks have been supported by a mature library, but there is a TSLIB Library, The application library based on the touch screen provides a more convenient interface for the application of the upper layer, such as touch screen filtering and de-noising.

Keywords: Linux Embedded system

Added by justinwhite93 on Sun, 20 Feb 2022 13:13:07 +0200