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