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