Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <cutils/android_reboot.h> |
| 32 | |
| 33 | #include "common.h" |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 34 | #include "roots.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 35 | #include "device.h" |
| 36 | #include "minui/minui.h" |
| 37 | #include "screen_ui.h" |
| 38 | #include "ui.h" |
| 39 | |
| 40 | #define UI_WAIT_KEY_TIMEOUT_SEC 120 |
| 41 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 42 | RecoveryUI::RecoveryUI() |
| 43 | : key_queue_len(0), |
| 44 | key_last_down(-1), |
| 45 | key_long_press(false), |
| 46 | key_down_count(0), |
| 47 | enable_reboot(true), |
| 48 | consecutive_power_keys(0), |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 49 | last_key(-1), |
| 50 | has_power_key(false), |
| 51 | has_up_key(false), |
| 52 | has_down_key(false) { |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 53 | pthread_mutex_init(&key_queue_mutex, nullptr); |
| 54 | pthread_cond_init(&key_queue_cond, nullptr); |
Mihai Serban | 187d626 | 2014-06-06 15:23:20 +0300 | [diff] [blame] | 55 | memset(key_pressed, 0, sizeof(key_pressed)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 58 | void RecoveryUI::OnKeyDetected(int key_code) { |
| 59 | if (key_code == KEY_POWER) { |
| 60 | has_power_key = true; |
| 61 | } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { |
| 62 | has_down_key = true; |
| 63 | } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { |
| 64 | has_up_key = true; |
| 65 | } |
| 66 | } |
| 67 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 68 | int RecoveryUI::InputCallback(int fd, uint32_t epevents, void* data) { |
| 69 | return reinterpret_cast<RecoveryUI*>(data)->OnInputEvent(fd, epevents); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 72 | // Reads input events, handles special hot keys, and adds to the key queue. |
| 73 | static void* InputThreadLoop(void*) { |
| 74 | while (true) { |
| 75 | if (!ev_wait(-1)) { |
| 76 | ev_dispatch(); |
| 77 | } |
| 78 | } |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | void RecoveryUI::Init() { |
| 83 | ev_init(InputCallback, this); |
| 84 | |
| 85 | ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
| 86 | |
| 87 | pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr); |
| 88 | } |
| 89 | |
| 90 | int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 91 | struct input_event ev; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 92 | if (ev_get_input(fd, epevents, &ev) == -1) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 93 | return -1; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 94 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 95 | |
| 96 | if (ev.type == EV_SYN) { |
| 97 | return 0; |
| 98 | } else if (ev.type == EV_REL) { |
| 99 | if (ev.code == REL_Y) { |
| 100 | // accumulate the up or down motion reported by |
| 101 | // the trackball. When it exceeds a threshold |
| 102 | // (positive or negative), fake an up/down |
| 103 | // key event. |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 104 | rel_sum += ev.value; |
| 105 | if (rel_sum > 3) { |
| 106 | ProcessKey(KEY_DOWN, 1); // press down key |
| 107 | ProcessKey(KEY_DOWN, 0); // and release it |
| 108 | rel_sum = 0; |
| 109 | } else if (rel_sum < -3) { |
| 110 | ProcessKey(KEY_UP, 1); // press up key |
| 111 | ProcessKey(KEY_UP, 0); // and release it |
| 112 | rel_sum = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } else { |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 116 | rel_sum = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 119 | if (ev.type == EV_KEY && ev.code <= KEY_MAX) { |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 120 | ProcessKey(ev.code, ev.value); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 121 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | // Process a key-up or -down event. A key is "registered" when it is |
| 127 | // pressed and then released, with no other keypresses or releases in |
| 128 | // between. Registered keys are passed to CheckKey() to see if it |
| 129 | // should trigger a visibility toggle, an immediate reboot, or be |
| 130 | // queued to be processed next time the foreground thread wants a key |
| 131 | // (eg, for the menu). |
| 132 | // |
| 133 | // We also keep track of which keys are currently down so that |
| 134 | // CheckKey can call IsKeyPressed to see what other keys are held when |
| 135 | // a key is registered. |
| 136 | // |
| 137 | // updown == 1 for key down events; 0 for key up events |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 138 | void RecoveryUI::ProcessKey(int key_code, int updown) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 139 | bool register_key = false; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 140 | bool long_press = false; |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 141 | bool reboot_enabled; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 142 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 143 | pthread_mutex_lock(&key_queue_mutex); |
| 144 | key_pressed[key_code] = updown; |
| 145 | if (updown) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 146 | ++key_down_count; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 147 | key_last_down = key_code; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 148 | key_long_press = false; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 149 | key_timer_t* info = new key_timer_t; |
| 150 | info->ui = this; |
| 151 | info->key_code = key_code; |
| 152 | info->count = key_down_count; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 153 | pthread_t thread; |
| 154 | pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info); |
| 155 | pthread_detach(thread); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 156 | } else { |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 157 | if (key_last_down == key_code) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 158 | long_press = key_long_press; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 159 | register_key = true; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 160 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 161 | key_last_down = -1; |
| 162 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 163 | reboot_enabled = enable_reboot; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 164 | pthread_mutex_unlock(&key_queue_mutex); |
| 165 | |
| 166 | if (register_key) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 167 | switch (CheckKey(key_code, long_press)) { |
Doug Zongker | 48b5b07 | 2012-01-18 13:46:26 -0800 | [diff] [blame] | 168 | case RecoveryUI::IGNORE: |
| 169 | break; |
| 170 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 171 | case RecoveryUI::TOGGLE: |
| 172 | ShowText(!IsTextVisible()); |
| 173 | break; |
| 174 | |
| 175 | case RecoveryUI::REBOOT: |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 176 | if (reboot_enabled) { |
| 177 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 178 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case RecoveryUI::ENQUEUE: |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 182 | EnqueueKey(key_code); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 188 | void* RecoveryUI::time_key_helper(void* cookie) { |
| 189 | key_timer_t* info = (key_timer_t*) cookie; |
| 190 | info->ui->time_key(info->key_code, info->count); |
| 191 | delete info; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 192 | return nullptr; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void RecoveryUI::time_key(int key_code, int count) { |
| 196 | usleep(750000); // 750 ms == "long" |
| 197 | bool long_press = false; |
| 198 | pthread_mutex_lock(&key_queue_mutex); |
| 199 | if (key_last_down == key_code && key_down_count == count) { |
| 200 | long_press = key_long_press = true; |
| 201 | } |
| 202 | pthread_mutex_unlock(&key_queue_mutex); |
| 203 | if (long_press) KeyLongPress(key_code); |
| 204 | } |
| 205 | |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 206 | void RecoveryUI::EnqueueKey(int key_code) { |
| 207 | pthread_mutex_lock(&key_queue_mutex); |
| 208 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); |
| 209 | if (key_queue_len < queue_max) { |
| 210 | key_queue[key_queue_len++] = key_code; |
| 211 | pthread_cond_signal(&key_queue_cond); |
| 212 | } |
| 213 | pthread_mutex_unlock(&key_queue_mutex); |
| 214 | } |
| 215 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 216 | int RecoveryUI::WaitKey() { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 217 | pthread_mutex_lock(&key_queue_mutex); |
| 218 | |
| 219 | // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is |
| 220 | // plugged in. |
| 221 | do { |
| 222 | struct timeval now; |
| 223 | struct timespec timeout; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 224 | gettimeofday(&now, nullptr); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 225 | timeout.tv_sec = now.tv_sec; |
| 226 | timeout.tv_nsec = now.tv_usec * 1000; |
| 227 | timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC; |
| 228 | |
| 229 | int rc = 0; |
| 230 | while (key_queue_len == 0 && rc != ETIMEDOUT) { |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 231 | rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 232 | } |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 233 | } while (IsUsbConnected() && key_queue_len == 0); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 234 | |
| 235 | int key = -1; |
| 236 | if (key_queue_len > 0) { |
| 237 | key = key_queue[0]; |
| 238 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 239 | } |
| 240 | pthread_mutex_unlock(&key_queue_mutex); |
| 241 | return key; |
| 242 | } |
| 243 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 244 | bool RecoveryUI::IsUsbConnected() { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 245 | int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); |
| 246 | if (fd < 0) { |
| 247 | printf("failed to open /sys/class/android_usb/android0/state: %s\n", |
| 248 | strerror(errno)); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | char buf; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 253 | // USB is connected if android_usb state is CONNECTED or CONFIGURED. |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 254 | int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 255 | if (close(fd) < 0) { |
| 256 | printf("failed to close /sys/class/android_usb/android0/state: %s\n", |
| 257 | strerror(errno)); |
| 258 | } |
| 259 | return connected; |
| 260 | } |
| 261 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 262 | bool RecoveryUI::IsKeyPressed(int key) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 263 | pthread_mutex_lock(&key_queue_mutex); |
| 264 | int pressed = key_pressed[key]; |
| 265 | pthread_mutex_unlock(&key_queue_mutex); |
| 266 | return pressed; |
| 267 | } |
| 268 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 269 | bool RecoveryUI::IsLongPress() { |
| 270 | pthread_mutex_lock(&key_queue_mutex); |
| 271 | bool result = key_long_press; |
| 272 | pthread_mutex_unlock(&key_queue_mutex); |
| 273 | return result; |
| 274 | } |
| 275 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 276 | bool RecoveryUI::HasThreeButtons() { |
| 277 | return has_power_key && has_up_key && has_down_key; |
| 278 | } |
| 279 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 280 | void RecoveryUI::FlushKeys() { |
| 281 | pthread_mutex_lock(&key_queue_mutex); |
| 282 | key_queue_len = 0; |
| 283 | pthread_mutex_unlock(&key_queue_mutex); |
| 284 | } |
| 285 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 286 | RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { |
| 287 | pthread_mutex_lock(&key_queue_mutex); |
| 288 | key_long_press = false; |
| 289 | pthread_mutex_unlock(&key_queue_mutex); |
| 290 | |
| 291 | // If we have power and volume up keys, that chord is the signal to toggle the text display. |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 292 | if (HasThreeButtons()) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 293 | if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) { |
| 294 | return TOGGLE; |
| 295 | } |
| 296 | } else { |
| 297 | // Otherwise long press of any button toggles to the text display, |
| 298 | // and there's no way to toggle back (but that's pretty useless anyway). |
| 299 | if (is_long_press && !IsTextVisible()) { |
| 300 | return TOGGLE; |
| 301 | } |
| 302 | |
| 303 | // Also, for button-limited devices, a long press is translated to KEY_ENTER. |
| 304 | if (is_long_press && IsTextVisible()) { |
| 305 | EnqueueKey(KEY_ENTER); |
| 306 | return IGNORE; |
| 307 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 310 | // Press power seven times in a row to reboot. |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 311 | if (key == KEY_POWER) { |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 312 | pthread_mutex_lock(&key_queue_mutex); |
| 313 | bool reboot_enabled = enable_reboot; |
| 314 | pthread_mutex_unlock(&key_queue_mutex); |
| 315 | |
| 316 | if (reboot_enabled) { |
| 317 | ++consecutive_power_keys; |
| 318 | if (consecutive_power_keys >= 7) { |
| 319 | return REBOOT; |
| 320 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 321 | } |
| 322 | } else { |
| 323 | consecutive_power_keys = 0; |
| 324 | } |
| 325 | |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 326 | last_key = key; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 327 | return IsTextVisible() ? ENQUEUE : IGNORE; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 328 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 329 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 330 | void RecoveryUI::KeyLongPress(int) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 331 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 332 | |
| 333 | void RecoveryUI::SetEnableReboot(bool enabled) { |
| 334 | pthread_mutex_lock(&key_queue_mutex); |
| 335 | enable_reboot = enabled; |
| 336 | pthread_mutex_unlock(&key_queue_mutex); |
| 337 | } |