blob: 2d80c382f414e8eaee580f1262b467165662cef5 [file] [log] [blame]
Doug Zongker32a0a472011-11-01 11:00:20 -07001/*
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
Elliott Hughescb220402016-09-23 15:30:55 -070031#include <android-base/properties.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070032#include <cutils/android_reboot.h>
33
34#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070035#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070036#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 Hughesec283402015-04-10 10:01:53 -070043RecoveryUI::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 Hughes642aaa72015-04-10 12:47:46 -070050 last_key(-1),
51 has_power_key(false),
52 has_up_key(false),
53 has_down_key(false) {
Elliott Hughes985022a2015-04-13 13:04:32 -070054 pthread_mutex_init(&key_queue_mutex, nullptr);
55 pthread_cond_init(&key_queue_cond, nullptr);
Mihai Serban187d6262014-06-06 15:23:20 +030056 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070057}
58
Elliott Hughes642aaa72015-04-10 12:47:46 -070059void 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 Hughes985022a2015-04-13 13:04:32 -070069int RecoveryUI::InputCallback(int fd, uint32_t epevents, void* data) {
70 return reinterpret_cast<RecoveryUI*>(data)->OnInputEvent(fd, epevents);
Doug Zongker32a0a472011-11-01 11:00:20 -070071}
72
Elliott Hughes985022a2015-04-13 13:04:32 -070073// Reads input events, handles special hot keys, and adds to the key queue.
74static void* InputThreadLoop(void*) {
75 while (true) {
76 if (!ev_wait(-1)) {
77 ev_dispatch();
78 }
79 }
80 return nullptr;
81}
82
Sen Jiangd5304492016-12-09 16:20:49 -080083bool RecoveryUI::Init() {
Elliott Hughes985022a2015-04-13 13:04:32 -070084 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);
Sen Jiangd5304492016-12-09 16:20:49 -080089 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -070090}
91
92int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Doug Zongker32a0a472011-11-01 11:00:20 -070093 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -070094 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -070095 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -070096 }
Doug Zongker32a0a472011-11-01 11:00:20 -070097
98 if (ev.type == EV_SYN) {
99 return 0;
100 } else if (ev.type == EV_REL) {
101 if (ev.code == REL_Y) {
102 // accumulate the up or down motion reported by
103 // the trackball. When it exceeds a threshold
104 // (positive or negative), fake an up/down
105 // key event.
Elliott Hughes985022a2015-04-13 13:04:32 -0700106 rel_sum += ev.value;
107 if (rel_sum > 3) {
108 ProcessKey(KEY_DOWN, 1); // press down key
109 ProcessKey(KEY_DOWN, 0); // and release it
110 rel_sum = 0;
111 } else if (rel_sum < -3) {
112 ProcessKey(KEY_UP, 1); // press up key
113 ProcessKey(KEY_UP, 0); // and release it
114 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700115 }
116 }
117 } else {
Elliott Hughes985022a2015-04-13 13:04:32 -0700118 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700119 }
120
Elliott Hughes642aaa72015-04-10 12:47:46 -0700121 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Elliott Hughes985022a2015-04-13 13:04:32 -0700122 ProcessKey(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700123 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700124
125 return 0;
126}
127
128// Process a key-up or -down event. A key is "registered" when it is
129// pressed and then released, with no other keypresses or releases in
130// between. Registered keys are passed to CheckKey() to see if it
131// should trigger a visibility toggle, an immediate reboot, or be
132// queued to be processed next time the foreground thread wants a key
133// (eg, for the menu).
134//
135// We also keep track of which keys are currently down so that
136// CheckKey can call IsKeyPressed to see what other keys are held when
137// a key is registered.
138//
139// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700140void RecoveryUI::ProcessKey(int key_code, int updown) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700141 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800142 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700143 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800144
Doug Zongker32a0a472011-11-01 11:00:20 -0700145 pthread_mutex_lock(&key_queue_mutex);
146 key_pressed[key_code] = updown;
147 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700148 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700149 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700150 key_long_press = false;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700151 key_timer_t* info = new key_timer_t;
152 info->ui = this;
153 info->key_code = key_code;
154 info->count = key_down_count;
Elliott Hughes985022a2015-04-13 13:04:32 -0700155 pthread_t thread;
156 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
157 pthread_detach(thread);
Doug Zongker32a0a472011-11-01 11:00:20 -0700158 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800159 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700160 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700161 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800162 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700163 key_last_down = -1;
164 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700165 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700166 pthread_mutex_unlock(&key_queue_mutex);
167
168 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700169 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800170 case RecoveryUI::IGNORE:
171 break;
172
Doug Zongker32a0a472011-11-01 11:00:20 -0700173 case RecoveryUI::TOGGLE:
174 ShowText(!IsTextVisible());
175 break;
176
177 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700178 if (reboot_enabled) {
Elliott Hughescb220402016-09-23 15:30:55 -0700179 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Tao Bao75238632015-05-27 14:46:17 -0700180 while (true) { pause(); }
Doug Zongkerc704e062014-05-23 08:40:35 -0700181 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700182 break;
183
184 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800185 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700186 break;
187 }
188 }
189}
190
Doug Zongkerc0441d12013-07-31 11:28:24 -0700191void* RecoveryUI::time_key_helper(void* cookie) {
192 key_timer_t* info = (key_timer_t*) cookie;
193 info->ui->time_key(info->key_code, info->count);
194 delete info;
Elliott Hughes985022a2015-04-13 13:04:32 -0700195 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700196}
197
198void RecoveryUI::time_key(int key_code, int count) {
199 usleep(750000); // 750 ms == "long"
200 bool long_press = false;
201 pthread_mutex_lock(&key_queue_mutex);
202 if (key_last_down == key_code && key_down_count == count) {
203 long_press = key_long_press = true;
204 }
205 pthread_mutex_unlock(&key_queue_mutex);
206 if (long_press) KeyLongPress(key_code);
207}
208
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800209void RecoveryUI::EnqueueKey(int key_code) {
210 pthread_mutex_lock(&key_queue_mutex);
211 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
212 if (key_queue_len < queue_max) {
213 key_queue[key_queue_len++] = key_code;
214 pthread_cond_signal(&key_queue_cond);
215 }
216 pthread_mutex_unlock(&key_queue_mutex);
217}
218
Elliott Hughesec283402015-04-10 10:01:53 -0700219int RecoveryUI::WaitKey() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700220 pthread_mutex_lock(&key_queue_mutex);
221
222 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
223 // plugged in.
224 do {
225 struct timeval now;
226 struct timespec timeout;
Elliott Hughes985022a2015-04-13 13:04:32 -0700227 gettimeofday(&now, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700228 timeout.tv_sec = now.tv_sec;
229 timeout.tv_nsec = now.tv_usec * 1000;
230 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
231
232 int rc = 0;
233 while (key_queue_len == 0 && rc != ETIMEDOUT) {
Elliott Hughesec283402015-04-10 10:01:53 -0700234 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700235 }
Elliott Hughes985022a2015-04-13 13:04:32 -0700236 } while (IsUsbConnected() && key_queue_len == 0);
Doug Zongker32a0a472011-11-01 11:00:20 -0700237
238 int key = -1;
239 if (key_queue_len > 0) {
240 key = key_queue[0];
241 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
242 }
243 pthread_mutex_unlock(&key_queue_mutex);
244 return key;
245}
246
Elliott Hughes985022a2015-04-13 13:04:32 -0700247bool RecoveryUI::IsUsbConnected() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700248 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
249 if (fd < 0) {
250 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
251 strerror(errno));
252 return 0;
253 }
254
255 char buf;
Elliott Hughes985022a2015-04-13 13:04:32 -0700256 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700257 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
Doug Zongker32a0a472011-11-01 11:00:20 -0700258 if (close(fd) < 0) {
259 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
260 strerror(errno));
261 }
262 return connected;
263}
264
Elliott Hughesec283402015-04-10 10:01:53 -0700265bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700266 pthread_mutex_lock(&key_queue_mutex);
267 int pressed = key_pressed[key];
268 pthread_mutex_unlock(&key_queue_mutex);
269 return pressed;
270}
271
Elliott Hughes642aaa72015-04-10 12:47:46 -0700272bool RecoveryUI::IsLongPress() {
273 pthread_mutex_lock(&key_queue_mutex);
274 bool result = key_long_press;
275 pthread_mutex_unlock(&key_queue_mutex);
276 return result;
277}
278
Elliott Hughes4af215b2015-04-10 15:00:34 -0700279bool RecoveryUI::HasThreeButtons() {
280 return has_power_key && has_up_key && has_down_key;
281}
282
Doug Zongker32a0a472011-11-01 11:00:20 -0700283void RecoveryUI::FlushKeys() {
284 pthread_mutex_lock(&key_queue_mutex);
285 key_queue_len = 0;
286 pthread_mutex_unlock(&key_queue_mutex);
287}
288
Elliott Hughes642aaa72015-04-10 12:47:46 -0700289RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
290 pthread_mutex_lock(&key_queue_mutex);
291 key_long_press = false;
292 pthread_mutex_unlock(&key_queue_mutex);
293
294 // If we have power and volume up keys, that chord is the signal to toggle the text display.
Elliott Hughes4af215b2015-04-10 15:00:34 -0700295 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700296 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
297 return TOGGLE;
298 }
299 } else {
300 // Otherwise long press of any button toggles to the text display,
301 // and there's no way to toggle back (but that's pretty useless anyway).
302 if (is_long_press && !IsTextVisible()) {
303 return TOGGLE;
304 }
305
306 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
307 if (is_long_press && IsTextVisible()) {
308 EnqueueKey(KEY_ENTER);
309 return IGNORE;
310 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700311 }
312
Elliott Hughes642aaa72015-04-10 12:47:46 -0700313 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700314 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700315 pthread_mutex_lock(&key_queue_mutex);
316 bool reboot_enabled = enable_reboot;
317 pthread_mutex_unlock(&key_queue_mutex);
318
319 if (reboot_enabled) {
320 ++consecutive_power_keys;
321 if (consecutive_power_keys >= 7) {
322 return REBOOT;
323 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700324 }
325 } else {
326 consecutive_power_keys = 0;
327 }
328
Doug Zongker9e805d62013-09-04 13:44:38 -0700329 last_key = key;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700330 return IsTextVisible() ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700331}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800332
Elliott Hughes642aaa72015-04-10 12:47:46 -0700333void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700334}
Doug Zongkerc704e062014-05-23 08:40:35 -0700335
336void RecoveryUI::SetEnableReboot(bool enabled) {
337 pthread_mutex_lock(&key_queue_mutex);
338 enable_reboot = enabled;
339 pthread_mutex_unlock(&key_queue_mutex);
340}