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