blob: 10704fb95f1aa33ead49f271fd93e17932edc566 [file] [log] [blame]
Lianghu Sue1655222017-05-24 14:52:06 +08001/*
2 * Copyright (C) Amlogic
3 *
4 * author: peipeng.zhao@amlogic.com
5 */
6
7#include <errno.h>
8#include <fcntl.h>
9#include <stdint.h>
10#include <linux/input.h>
11#include <pthread.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/stat.h>
17#include <sys/time.h>
18#include <sys/types.h>
19#include <time.h>
20#include <unistd.h>
21#include <ctype.h>
22#include "events.h"
23#include "events_process.h"
24
25#define WAIT_KEY_TIMEOUT_SEC 120
26#define nullptr NULL
Lianghu Su53dd4072017-05-26 19:04:48 +080027#define KEY_EVENT_TIME_INTERVAL 20
Lianghu Sue1655222017-05-24 14:52:06 +080028
29EventsProcess::KeyMapItem_t g_default_keymap[] = {
30 { "select", KEY_POWER, {KEY_POWER,KEY_ENTER, KEY_BACK, -1, -1, -1} },
31 { "down", KEY_VOLUMEDOWN, {KEY_VOLUMEDOWN, KEY_DOWN,KEY_PAGEDOWN, -1, -1, -1} },
32 { "up", KEY_VOLUMEUP, {KEY_VOLUMEUP, KEY_UP, KEY_PAGEUP, -1, -1, -1} },
33 };
34
35EventsProcess:: CtrlInfo_t g_ctrlinfo[] = {
36 { "select", KEY_ENTER },
37 { "down", KEY_DOWN },
38 { "up", KEY_UP },
39 };
40
41EventsProcess::EventsProcess()
42 : key_queue_len(0),
43 key_last_down(-1),
44 key_long_press(false),
45 key_down_count(0),
46 consecutive_power_keys(0),
47 last_key(-1),
48 has_power_key(false),
49 has_up_key(false),
50 has_down_key(false),
51 num_keys(0),
52 keys_map(NULL) {
53 pthread_mutex_init(&key_queue_mutex, nullptr);
54 pthread_cond_init(&key_queue_cond, nullptr);
55 memset(key_pressed, 0, sizeof(key_pressed));
Lianghu Su53dd4072017-05-26 19:04:48 +080056 memset(&last_queue_time, 0, sizeof(last_queue_time));
Lianghu Sue1655222017-05-24 14:52:06 +080057 load_key_map();
58}
59
60void EventsProcess::OnKeyDetected(int key_code) {
61 if (key_code == KEY_POWER) {
62 has_power_key = true;
63 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
64 has_down_key = true;
65 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
66 has_up_key = true;
67 }
68}
69
70int EventsProcess::InputCallback(int fd, uint32_t epevents, void* data) {
71 return reinterpret_cast<EventsProcess*>(data)->OnInputEvent(fd, epevents);
72}
73
74// Reads input events, handles special hot keys, and adds to the key queue.
75static void* InputThreadLoop(void*) {
76 while (true) {
77 if (!ev_wait(-1)) {
78 ev_dispatch();
79 }
80 }
81 return nullptr;
82}
83
84void EventsProcess::Init() {
85 ev_init(InputCallback, this);
86
87 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
88}
89
90int EventsProcess::OnInputEvent(int fd, uint32_t epevents) {
91 struct input_event ev;
92
93 if (ev_get_input(fd, epevents, &ev) == -1) {
94 return -1;
95 }
96
97 if (ev.type == EV_SYN) {
98 return 0;
99 }
100
101 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
102 int code = getMapKey(ev.code);
103 if (code > 0) {
104 ProcessKey(code, ev.value);
105 } else {
106 ProcessKey(ev.code, ev.value);
107 }
108 }
109
110 return 0;
111}
112
113// Process a key-up or -down event. A key is "registered" when it is
114// pressed and then released, with no other keypresses or releases in
115// between. Registered keys are passed to CheckKey() to see if it
116// should trigger a visibility toggle, an immediate reboot, or be
117// queued to be processed next time the foreground thread wants a key
118// (eg, for the menu).
119//
120// We also keep track of which keys are currently down so that
121// CheckKey can call IsKeyPressed to see what other keys are held when
122// a key is registered.
123//
124// value == 1 for key down events; 0 for key up events;2 for key repeat events
125void EventsProcess::ProcessKey(int key_code, int value) {
126 bool register_key = false;
127 bool long_press = false;
128
129 pthread_mutex_lock(&key_queue_mutex);
130 key_pressed[key_code] = value;
131 if (value == 1) {
132 ++key_down_count;
133 key_last_down = key_code;
134 key_long_press = false;
135 key_timer_t* info = new key_timer_t;
136 info->ep = this;
137 info->key_code = key_code;
138 info->count = key_down_count;
139 pthread_t thread;
140 pthread_create(&thread, nullptr, &EventsProcess::time_key_helper, info);
141 pthread_detach(thread);
142 } else if(value == 2){
143 long_press = key_long_press = true;
144 printf("%s,get kernel report repeat event\n",__func__);
145 } else {
146 if (key_last_down == key_code) {
147 long_press = key_long_press;
148 register_key = true;
149 }
150 key_last_down = -1;
151 }
152 pthread_mutex_unlock(&key_queue_mutex);
153
154 if (register_key) {
155 switch (CheckKey(key_code, long_press)) {
156 case EventsProcess::IGNORE:
157 break;
158
159 case EventsProcess::LONGPRESS:
160 break;
161
162 case EventsProcess::TOGGLE:
163 break;
164
165 case EventsProcess::REBOOT:
166 break;
167
168 case EventsProcess::ENQUEUE:
169 EnqueueKey(key_code);
170 break;
171 }
172 }
173}
174
175void* EventsProcess::time_key_helper(void* cookie) {
176 key_timer_t* info = (key_timer_t*) cookie;
177 info->ep->time_key(info->key_code, info->count);
178 delete info;
179 return nullptr;
180}
181
182void EventsProcess::time_key(int key_code, int count) {
183 usleep(750000); // 750 ms == "long"
184 bool long_press = false;
185 pthread_mutex_lock(&key_queue_mutex);
186 if (key_last_down == key_code && key_down_count == count) {
187 long_press = key_long_press = true;
188 }
189 pthread_mutex_unlock(&key_queue_mutex);
190 if (long_press)
191 KeyLongPress(key_code);
192}
193
194int EventsProcess::getKey(char *key) {
195 if (key == NULL) return -1;
196
197 unsigned int i;
198 for (i = 0; i < NUM_CTRLINFO; i++) {
199 CtrlInfo_t *info = &g_ctrlinfo[i];
200 if (strcmp(info->type, key) == 0) {
201 return info->value;
202 }
203 }
204 return -1;
205}
206
207void EventsProcess::load_key_map() {
208 FILE* fstab = fopen("/etc/gpio_key.kl", "r");
209 if (fstab != NULL) {
210 printf("loaded /etc/gpio_key.kl\n");
211 int alloc = 2;
212 keys_map = (KeyMapItem_t*)malloc(alloc * sizeof(KeyMapItem_t));
213
214 keys_map[0].type = "down";
215 keys_map[0].value = KEY_DOWN;
216 keys_map[0].key[0] = -1;
217 keys_map[0].key[1] = -1;
218 keys_map[0].key[2] = -1;
219 keys_map[0].key[3] = -1;
220 keys_map[0].key[4] = -1;
221 keys_map[0].key[5] = -1;
222 num_keys = 0;
223
224 char buffer[1024];
225 int i;
226 int value = -1;
227 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
228 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
229
230 if (buffer[i] == '\0' || buffer[i] == '#') continue;
231
232 char* original = strdup(buffer);
233
234 char* type = strtok(original+i, " \t\n");
235 char* key1 = strtok(NULL, " \t\n");
236 char* key2 = strtok(NULL, " \t\n");
237 char* key3 = strtok(NULL, " \t\n");
238 char* key4 = strtok(NULL, " \t\n");
239 char* key5 = strtok(NULL, " \t\n");
240 char* key6 = strtok(NULL, " \t\n");
241
242 value = getKey(type);
243 if (type && key1 && (value > 0)) {
244 while (num_keys >= alloc) {
245 alloc *= 2;
246 keys_map = (KeyMapItem_t*)realloc(keys_map, alloc*sizeof(KeyMapItem_t));
247 }
248 keys_map[num_keys].type = strdup(type);
249 keys_map[num_keys].value = value;
250 keys_map[num_keys].key[0] = key1?atoi(key1):-1;
251 keys_map[num_keys].key[1] = key2?atoi(key2):-1;
252 keys_map[num_keys].key[2] = key3?atoi(key3):-1;
253 keys_map[num_keys].key[3] = key4?atoi(key4):-1;
254 keys_map[num_keys].key[4] = key5?atoi(key5):-1;
255 keys_map[num_keys].key[5] = key6?atoi(key6):-1;
256
257 ++num_keys;
258 } else {
259 printf("error: skipping malformed keyboard.lk line: %s\n", original);
260 }
261 free(original);
262 }
263
264 fclose(fstab);
265 } else {
266 printf("error: failed to open /etc/gpio_key.kl, use default map\n");
267 num_keys = NUM_DEFAULT_KEY_MAP;
268 keys_map = g_default_keymap;
269 }
270
271 printf("keyboard key map table:\n");
272 int i;
273 for (i = 0; i < num_keys; ++i) {
274 KeyMapItem_t* v = &keys_map[i];
275 printf(" %d type:%s value:%d key:%d %d %d %d %d %d\n", i, v->type, v->value,
276 v->key[0], v->key[1], v->key[2], v->key[3], v->key[4], v->key[5]);
277 }
278 printf("\n");
279}
280
281int EventsProcess::getMapKey(int key) {
282 int i,j;
283 for (i = 0; i < num_keys; i++) {
284 KeyMapItem_t* v = &keys_map[i];
285 for (j = 0; j < 6; j++) {
286 if (v->key[j] == key)
287 return v->value;
288 }
289 }
290
291 return -1;
292}
293
Lianghu Su53dd4072017-05-26 19:04:48 +0800294// retrun time interval in millisecond between two timeval.
295long get_time_diff(struct timeval before, struct timeval later) {
296 long before_sec = before.tv_sec;
297 long before_usec = before.tv_usec;
298 long later_sec = later.tv_sec;
299 long later_usec = later.tv_usec;
300
301 return (later_sec - before_sec) * 1000 + (later_usec - before_usec) / 1000;
302}
303
Lianghu Sue1655222017-05-24 14:52:06 +0800304void EventsProcess::EnqueueKey(int key_code) {
Lianghu Su53dd4072017-05-26 19:04:48 +0800305 struct timeval now;
306 gettimeofday(&now, nullptr);
307
Lianghu Sue1655222017-05-24 14:52:06 +0800308 pthread_mutex_lock(&key_queue_mutex);
309 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
310 if (key_queue_len < queue_max) {
Lianghu Su53dd4072017-05-26 19:04:48 +0800311 if (last_key != key_code || get_time_diff(last_queue_time, now) >= KEY_EVENT_TIME_INTERVAL) {
312 key_queue[key_queue_len++] = key_code;
313 last_queue_time = now;
314 }
Lianghu Sue1655222017-05-24 14:52:06 +0800315 pthread_cond_signal(&key_queue_cond);
316 }
317 pthread_mutex_unlock(&key_queue_mutex);
318}
319
320int EventsProcess::WaitKey() {
321 pthread_mutex_lock(&key_queue_mutex);
322
323 // Time out after WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
324 // plugged in.
325 do {
326 struct timeval now;
327 struct timespec timeout;
328 gettimeofday(&now, nullptr);
329 timeout.tv_sec = now.tv_sec;
330 timeout.tv_nsec = now.tv_usec * 1000;
331 timeout.tv_sec += WAIT_KEY_TIMEOUT_SEC;
332
333 int rc = 0;
334 while (key_queue_len == 0 && rc != ETIMEDOUT) {
335 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
336 }
337 } while (key_queue_len == 0);
338
339 int key = -1;
340 if (key_queue_len > 0) {
341 key = key_queue[0];
342 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
343 }
344 pthread_mutex_unlock(&key_queue_mutex);
345 return key;
346}
347
348bool EventsProcess::IsKeyPressed(int key) {
349 pthread_mutex_lock(&key_queue_mutex);
350 int pressed = key_pressed[key];
351 pthread_mutex_unlock(&key_queue_mutex);
352 return pressed;
353}
354
355bool EventsProcess::IsLongPress() {
356 pthread_mutex_lock(&key_queue_mutex);
357 bool result = key_long_press;
358 pthread_mutex_unlock(&key_queue_mutex);
359 return result;
360}
361
362bool EventsProcess::HasThreeButtons() {
363 return has_power_key && has_up_key && has_down_key;
364}
365
366void EventsProcess::FlushKeys() {
367 pthread_mutex_lock(&key_queue_mutex);
368 key_queue_len = 0;
369 pthread_mutex_unlock(&key_queue_mutex);
370}
371
372EventsProcess::KeyAction EventsProcess::CheckKey(int key, bool is_long_press) {
373 pthread_mutex_lock(&key_queue_mutex);
374 key_long_press = false;
375 pthread_mutex_unlock(&key_queue_mutex);
376
377 // If we have power and volume up keys, that chord is the signal to toggle the text display.
378 if (HasThreeButtons()) {
379 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
380 return TOGGLE;
381 }
382 }
383
384 if (is_long_press) {
385 return LONGPRESS;
386 }
387
388 last_key = key;
389 return ENQUEUE;
390}
391
392void EventsProcess::KeyLongPress(int) {
393 printf("bebond timer generate %s\n",__func__);
394 key_long_press = false;
395}
396