blob: f31660db46b361c0b8f80793d2495d1320887eb6 [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
Tao Bao736d59c2017-01-03 10:15:33 -080017#include "ui.h"
18
Doug Zongker32a0a472011-11-01 11:00:20 -070019#include <errno.h>
20#include <fcntl.h>
21#include <linux/input.h>
22#include <pthread.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
Tao Bao736d59c2017-01-03 10:15:33 -080033#include <string>
34
Elliott Hughescb220402016-09-23 15:30:55 -070035#include <android-base/properties.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070036#include <cutils/android_reboot.h>
37
38#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070039#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070040#include "device.h"
41#include "minui/minui.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
43#define UI_WAIT_KEY_TIMEOUT_SEC 120
44
Elliott Hughesec283402015-04-10 10:01:53 -070045RecoveryUI::RecoveryUI()
Tao Bao736d59c2017-01-03 10:15:33 -080046 : locale_(""),
47 rtl_locale_(false),
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),
54 last_key(-1),
55 has_power_key(false),
56 has_up_key(false),
57 has_down_key(false) {
58 pthread_mutex_init(&key_queue_mutex, nullptr);
59 pthread_cond_init(&key_queue_cond, nullptr);
60 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070061}
62
Elliott Hughes642aaa72015-04-10 12:47:46 -070063void RecoveryUI::OnKeyDetected(int key_code) {
64 if (key_code == KEY_POWER) {
65 has_power_key = true;
66 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
67 has_down_key = true;
68 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
69 has_up_key = true;
70 }
71}
72
Elliott Hughes985022a2015-04-13 13:04:32 -070073int RecoveryUI::InputCallback(int fd, uint32_t epevents, void* data) {
74 return reinterpret_cast<RecoveryUI*>(data)->OnInputEvent(fd, epevents);
Doug Zongker32a0a472011-11-01 11:00:20 -070075}
76
Elliott Hughes985022a2015-04-13 13:04:32 -070077// Reads input events, handles special hot keys, and adds to the key queue.
78static void* InputThreadLoop(void*) {
79 while (true) {
80 if (!ev_wait(-1)) {
81 ev_dispatch();
82 }
83 }
84 return nullptr;
85}
86
Tao Bao736d59c2017-01-03 10:15:33 -080087bool RecoveryUI::Init(const std::string& locale) {
88 // Set up the locale info.
89 SetLocale(locale);
Elliott Hughes985022a2015-04-13 13:04:32 -070090
Tao Bao736d59c2017-01-03 10:15:33 -080091 ev_init(InputCallback, this);
Elliott Hughes985022a2015-04-13 13:04:32 -070092
Tao Bao736d59c2017-01-03 10:15:33 -080093 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
94
95 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
96 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -070097}
98
99int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700100 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700101 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700102 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700103 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700104
105 if (ev.type == EV_SYN) {
106 return 0;
107 } else if (ev.type == EV_REL) {
108 if (ev.code == REL_Y) {
109 // accumulate the up or down motion reported by
110 // the trackball. When it exceeds a threshold
111 // (positive or negative), fake an up/down
112 // key event.
Elliott Hughes985022a2015-04-13 13:04:32 -0700113 rel_sum += ev.value;
114 if (rel_sum > 3) {
115 ProcessKey(KEY_DOWN, 1); // press down key
116 ProcessKey(KEY_DOWN, 0); // and release it
117 rel_sum = 0;
118 } else if (rel_sum < -3) {
119 ProcessKey(KEY_UP, 1); // press up key
120 ProcessKey(KEY_UP, 0); // and release it
121 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700122 }
123 }
124 } else {
Elliott Hughes985022a2015-04-13 13:04:32 -0700125 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700126 }
127
Elliott Hughes642aaa72015-04-10 12:47:46 -0700128 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Elliott Hughes985022a2015-04-13 13:04:32 -0700129 ProcessKey(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700130 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700131
132 return 0;
133}
134
135// Process a key-up or -down event. A key is "registered" when it is
136// pressed and then released, with no other keypresses or releases in
137// between. Registered keys are passed to CheckKey() to see if it
138// should trigger a visibility toggle, an immediate reboot, or be
139// queued to be processed next time the foreground thread wants a key
140// (eg, for the menu).
141//
142// We also keep track of which keys are currently down so that
143// CheckKey can call IsKeyPressed to see what other keys are held when
144// a key is registered.
145//
146// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700147void RecoveryUI::ProcessKey(int key_code, int updown) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700148 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800149 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700150 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800151
Doug Zongker32a0a472011-11-01 11:00:20 -0700152 pthread_mutex_lock(&key_queue_mutex);
153 key_pressed[key_code] = updown;
154 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700155 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700156 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700157 key_long_press = false;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700158 key_timer_t* info = new key_timer_t;
159 info->ui = this;
160 info->key_code = key_code;
161 info->count = key_down_count;
Elliott Hughes985022a2015-04-13 13:04:32 -0700162 pthread_t thread;
163 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
164 pthread_detach(thread);
Doug Zongker32a0a472011-11-01 11:00:20 -0700165 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800166 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700167 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700168 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800169 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700170 key_last_down = -1;
171 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700172 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700173 pthread_mutex_unlock(&key_queue_mutex);
174
175 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700176 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800177 case RecoveryUI::IGNORE:
178 break;
179
Doug Zongker32a0a472011-11-01 11:00:20 -0700180 case RecoveryUI::TOGGLE:
181 ShowText(!IsTextVisible());
182 break;
183
184 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700185 if (reboot_enabled) {
Elliott Hughescb220402016-09-23 15:30:55 -0700186 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Tao Bao75238632015-05-27 14:46:17 -0700187 while (true) { pause(); }
Doug Zongkerc704e062014-05-23 08:40:35 -0700188 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700189 break;
190
191 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800192 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700193 break;
194 }
195 }
196}
197
Doug Zongkerc0441d12013-07-31 11:28:24 -0700198void* RecoveryUI::time_key_helper(void* cookie) {
199 key_timer_t* info = (key_timer_t*) cookie;
200 info->ui->time_key(info->key_code, info->count);
201 delete info;
Elliott Hughes985022a2015-04-13 13:04:32 -0700202 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700203}
204
205void RecoveryUI::time_key(int key_code, int count) {
206 usleep(750000); // 750 ms == "long"
207 bool long_press = false;
208 pthread_mutex_lock(&key_queue_mutex);
209 if (key_last_down == key_code && key_down_count == count) {
210 long_press = key_long_press = true;
211 }
212 pthread_mutex_unlock(&key_queue_mutex);
213 if (long_press) KeyLongPress(key_code);
214}
215
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800216void RecoveryUI::EnqueueKey(int key_code) {
217 pthread_mutex_lock(&key_queue_mutex);
218 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
219 if (key_queue_len < queue_max) {
220 key_queue[key_queue_len++] = key_code;
221 pthread_cond_signal(&key_queue_cond);
222 }
223 pthread_mutex_unlock(&key_queue_mutex);
224}
225
Elliott Hughesec283402015-04-10 10:01:53 -0700226int RecoveryUI::WaitKey() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700227 pthread_mutex_lock(&key_queue_mutex);
228
229 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
230 // plugged in.
231 do {
232 struct timeval now;
233 struct timespec timeout;
Elliott Hughes985022a2015-04-13 13:04:32 -0700234 gettimeofday(&now, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700235 timeout.tv_sec = now.tv_sec;
236 timeout.tv_nsec = now.tv_usec * 1000;
237 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
238
239 int rc = 0;
240 while (key_queue_len == 0 && rc != ETIMEDOUT) {
Elliott Hughesec283402015-04-10 10:01:53 -0700241 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700242 }
Elliott Hughes985022a2015-04-13 13:04:32 -0700243 } while (IsUsbConnected() && key_queue_len == 0);
Doug Zongker32a0a472011-11-01 11:00:20 -0700244
245 int key = -1;
246 if (key_queue_len > 0) {
247 key = key_queue[0];
248 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
249 }
250 pthread_mutex_unlock(&key_queue_mutex);
251 return key;
252}
253
Elliott Hughes985022a2015-04-13 13:04:32 -0700254bool RecoveryUI::IsUsbConnected() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700255 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
256 if (fd < 0) {
257 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
258 strerror(errno));
259 return 0;
260 }
261
262 char buf;
Elliott Hughes985022a2015-04-13 13:04:32 -0700263 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700264 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
Doug Zongker32a0a472011-11-01 11:00:20 -0700265 if (close(fd) < 0) {
266 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
267 strerror(errno));
268 }
269 return connected;
270}
271
Elliott Hughesec283402015-04-10 10:01:53 -0700272bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700273 pthread_mutex_lock(&key_queue_mutex);
274 int pressed = key_pressed[key];
275 pthread_mutex_unlock(&key_queue_mutex);
276 return pressed;
277}
278
Elliott Hughes642aaa72015-04-10 12:47:46 -0700279bool RecoveryUI::IsLongPress() {
280 pthread_mutex_lock(&key_queue_mutex);
281 bool result = key_long_press;
282 pthread_mutex_unlock(&key_queue_mutex);
283 return result;
284}
285
Elliott Hughes4af215b2015-04-10 15:00:34 -0700286bool RecoveryUI::HasThreeButtons() {
287 return has_power_key && has_up_key && has_down_key;
288}
289
Doug Zongker32a0a472011-11-01 11:00:20 -0700290void RecoveryUI::FlushKeys() {
291 pthread_mutex_lock(&key_queue_mutex);
292 key_queue_len = 0;
293 pthread_mutex_unlock(&key_queue_mutex);
294}
295
Elliott Hughes642aaa72015-04-10 12:47:46 -0700296RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
297 pthread_mutex_lock(&key_queue_mutex);
298 key_long_press = false;
299 pthread_mutex_unlock(&key_queue_mutex);
300
301 // 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 -0700302 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700303 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
304 return TOGGLE;
305 }
306 } else {
307 // Otherwise long press of any button toggles to the text display,
308 // and there's no way to toggle back (but that's pretty useless anyway).
309 if (is_long_press && !IsTextVisible()) {
310 return TOGGLE;
311 }
312
313 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
314 if (is_long_press && IsTextVisible()) {
315 EnqueueKey(KEY_ENTER);
316 return IGNORE;
317 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700318 }
319
Elliott Hughes642aaa72015-04-10 12:47:46 -0700320 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700321 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700322 pthread_mutex_lock(&key_queue_mutex);
323 bool reboot_enabled = enable_reboot;
324 pthread_mutex_unlock(&key_queue_mutex);
325
326 if (reboot_enabled) {
327 ++consecutive_power_keys;
328 if (consecutive_power_keys >= 7) {
329 return REBOOT;
330 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700331 }
332 } else {
333 consecutive_power_keys = 0;
334 }
335
Doug Zongker9e805d62013-09-04 13:44:38 -0700336 last_key = key;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700337 return IsTextVisible() ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700338}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800339
Elliott Hughes642aaa72015-04-10 12:47:46 -0700340void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700341}
Doug Zongkerc704e062014-05-23 08:40:35 -0700342
343void RecoveryUI::SetEnableReboot(bool enabled) {
344 pthread_mutex_lock(&key_queue_mutex);
345 enable_reboot = enabled;
346 pthread_mutex_unlock(&key_queue_mutex);
347}
Tao Bao736d59c2017-01-03 10:15:33 -0800348
349void RecoveryUI::SetLocale(const std::string& new_locale) {
350 this->locale_ = new_locale;
351 this->rtl_locale_ = false;
352
353 if (!new_locale.empty()) {
354 size_t underscore = new_locale.find('_');
355 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
356 std::string lang = new_locale.substr(0, underscore);
357
358 // A bit cheesy: keep an explicit list of supported RTL languages.
359 if (lang == "ar" || // Arabic
360 lang == "fa" || // Persian (Farsi)
361 lang == "he" || // Hebrew (new language code)
362 lang == "iw" || // Hebrew (old language code)
363 lang == "ur") { // Urdu
364 rtl_locale_ = true;
365 }
366 }
367}