xref: /OK3568_Linux_fs/app/lvgl_demo/hal/key.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /**
2  * @file evdev.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "key.h"
10 #if USE_KEY != 0
11 
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <linux/input.h>
16 
17 /*********************
18  *      DEFINES
19  *********************/
20 
21 /**********************
22  *      TYPEDEFS
23  **********************/
24 
25 /**********************
26  *  STATIC PROTOTYPES
27  **********************/
28 
29 /**********************
30  *  STATIC VARIABLES
31  **********************/
32 static int key_fd;
33 static int key_button;
34 
35 static int key_val;
36 /**********************
37  *      MACROS
38  **********************/
39 
40 /**********************
41  *   GLOBAL FUNCTIONS
42  **********************/
43 
44 /**
45  * Initialize the evdev interface
46  */
key_init(void)47 void key_init(void)
48 {
49     key_fd = open(KEY_NAME, O_RDWR | O_NOCTTY | O_NDELAY);
50 
51     if(key_fd == -1) {
52         perror("unable open evdev interface:");
53         return;
54     }
55 
56     fcntl(key_fd, F_SETFL, O_ASYNC | O_NONBLOCK);
57 
58     key_val = 0;
59     key_button = LV_INDEV_STATE_REL;
60 }
61 
62 /**
63  * Get the current position and state of the evdev
64  * @param data store the evdev data here
65  * @return false: because the points are not buffered, so no more data to be read
66  */
key_read(lv_indev_drv_t * drv,lv_indev_data_t * data)67 void key_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
68 {
69     struct input_event in;
70 
71     while(read(key_fd, &in, sizeof(struct input_event)) > 0) {
72         if(in.type == EV_KEY) {
73             data->state = (in.value) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
74             switch(in.code) {
75             case 1:
76                 data->key = LV_KEY_ESC;
77                 break;
78             case 139:
79                 data->key = LV_KEY_ENTER;
80                 break;
81             //case KEY_UP:
82             //    data->key = LV_KEY_UP;
83             //    break;
84             case 105:
85                 data->key = LV_KEY_PREV;
86                 break;
87             case 106:
88                 data->key = LV_KEY_NEXT;
89                 break;
90             //case KEY_DOWN:
91             //    data->key = LV_KEY_DOWN;
92             //    break;
93             default:
94                 data->key = 0;
95                 break;
96             }
97             key_val = data->key;
98             key_button = data->state;
99             return ;
100         }
101     }
102 
103     if(drv->type == LV_INDEV_TYPE_KEYPAD) {
104         /* No data retrieved */
105         data->key = key_val;
106         data->state = key_button;
107         return ;
108     }
109 }
110 #endif
111