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