blob: a0f741e5774115af4eed72d806df3772936f1599 [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>
Tao Bao0b1118d2017-01-14 07:46:10 -080037#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070038
39#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070040#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070041#include "device.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 -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
Tao Bao736d59c2017-01-03 10:15:33 -080083bool RecoveryUI::Init(const std::string& locale) {
84 // Set up the locale info.
85 SetLocale(locale);
Elliott Hughes985022a2015-04-13 13:04:32 -070086
Tao Bao0b1118d2017-01-14 07:46:10 -080087 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2));
Elliott Hughes985022a2015-04-13 13:04:32 -070088
Tao Bao736d59c2017-01-03 10:15:33 -080089 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
90
91 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
92 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -070093}
94
95int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Doug Zongker32a0a472011-11-01 11:00:20 -070096 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -070097 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -070098 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -070099 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700100
101 if (ev.type == EV_SYN) {
102 return 0;
103 } else if (ev.type == EV_REL) {
104 if (ev.code == REL_Y) {
105 // accumulate the up or down motion reported by
106 // the trackball. When it exceeds a threshold
107 // (positive or negative), fake an up/down
108 // key event.
Elliott Hughes985022a2015-04-13 13:04:32 -0700109 rel_sum += ev.value;
110 if (rel_sum > 3) {
111 ProcessKey(KEY_DOWN, 1); // press down key
112 ProcessKey(KEY_DOWN, 0); // and release it
113 rel_sum = 0;
114 } else if (rel_sum < -3) {
115 ProcessKey(KEY_UP, 1); // press up key
116 ProcessKey(KEY_UP, 0); // and release it
117 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700118 }
119 }
120 } else {
Elliott Hughes985022a2015-04-13 13:04:32 -0700121 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700122 }
123
Elliott Hughes642aaa72015-04-10 12:47:46 -0700124 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Elliott Hughes985022a2015-04-13 13:04:32 -0700125 ProcessKey(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700126 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700127
128 return 0;
129}
130
131// Process a key-up or -down event. A key is "registered" when it is
132// pressed and then released, with no other keypresses or releases in
133// between. Registered keys are passed to CheckKey() to see if it
134// should trigger a visibility toggle, an immediate reboot, or be
135// queued to be processed next time the foreground thread wants a key
136// (eg, for the menu).
137//
138// We also keep track of which keys are currently down so that
139// CheckKey can call IsKeyPressed to see what other keys are held when
140// a key is registered.
141//
142// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700143void RecoveryUI::ProcessKey(int key_code, int updown) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700144 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800145 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700146 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800147
Doug Zongker32a0a472011-11-01 11:00:20 -0700148 pthread_mutex_lock(&key_queue_mutex);
149 key_pressed[key_code] = updown;
150 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700151 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700152 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700153 key_long_press = false;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700154 key_timer_t* info = new key_timer_t;
155 info->ui = this;
156 info->key_code = key_code;
157 info->count = key_down_count;
Elliott Hughes985022a2015-04-13 13:04:32 -0700158 pthread_t thread;
159 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
160 pthread_detach(thread);
Doug Zongker32a0a472011-11-01 11:00:20 -0700161 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800162 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700163 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700164 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800165 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700166 key_last_down = -1;
167 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700168 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700169 pthread_mutex_unlock(&key_queue_mutex);
170
171 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700172 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800173 case RecoveryUI::IGNORE:
174 break;
175
Doug Zongker32a0a472011-11-01 11:00:20 -0700176 case RecoveryUI::TOGGLE:
177 ShowText(!IsTextVisible());
178 break;
179
180 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700181 if (reboot_enabled) {
Elliott Hughescb220402016-09-23 15:30:55 -0700182 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Tao Bao75238632015-05-27 14:46:17 -0700183 while (true) { pause(); }
Doug Zongkerc704e062014-05-23 08:40:35 -0700184 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700185 break;
186
187 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800188 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700189 break;
190 }
191 }
192}
193
Doug Zongkerc0441d12013-07-31 11:28:24 -0700194void* RecoveryUI::time_key_helper(void* cookie) {
195 key_timer_t* info = (key_timer_t*) cookie;
196 info->ui->time_key(info->key_code, info->count);
197 delete info;
Elliott Hughes985022a2015-04-13 13:04:32 -0700198 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700199}
200
201void RecoveryUI::time_key(int key_code, int count) {
202 usleep(750000); // 750 ms == "long"
203 bool long_press = false;
204 pthread_mutex_lock(&key_queue_mutex);
205 if (key_last_down == key_code && key_down_count == count) {
206 long_press = key_long_press = true;
207 }
208 pthread_mutex_unlock(&key_queue_mutex);
209 if (long_press) KeyLongPress(key_code);
210}
211
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800212void RecoveryUI::EnqueueKey(int key_code) {
213 pthread_mutex_lock(&key_queue_mutex);
214 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
215 if (key_queue_len < queue_max) {
216 key_queue[key_queue_len++] = key_code;
217 pthread_cond_signal(&key_queue_cond);
218 }
219 pthread_mutex_unlock(&key_queue_mutex);
220}
221
Elliott Hughesec283402015-04-10 10:01:53 -0700222int RecoveryUI::WaitKey() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700223 pthread_mutex_lock(&key_queue_mutex);
224
225 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
226 // plugged in.
227 do {
228 struct timeval now;
229 struct timespec timeout;
Elliott Hughes985022a2015-04-13 13:04:32 -0700230 gettimeofday(&now, nullptr);
Doug Zongker32a0a472011-11-01 11:00:20 -0700231 timeout.tv_sec = now.tv_sec;
232 timeout.tv_nsec = now.tv_usec * 1000;
233 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
234
235 int rc = 0;
236 while (key_queue_len == 0 && rc != ETIMEDOUT) {
Elliott Hughesec283402015-04-10 10:01:53 -0700237 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700238 }
Elliott Hughes985022a2015-04-13 13:04:32 -0700239 } while (IsUsbConnected() && key_queue_len == 0);
Doug Zongker32a0a472011-11-01 11:00:20 -0700240
241 int key = -1;
242 if (key_queue_len > 0) {
243 key = key_queue[0];
244 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
245 }
246 pthread_mutex_unlock(&key_queue_mutex);
247 return key;
248}
249
Elliott Hughes985022a2015-04-13 13:04:32 -0700250bool RecoveryUI::IsUsbConnected() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700251 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
252 if (fd < 0) {
253 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
254 strerror(errno));
255 return 0;
256 }
257
258 char buf;
Elliott Hughes985022a2015-04-13 13:04:32 -0700259 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700260 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
Doug Zongker32a0a472011-11-01 11:00:20 -0700261 if (close(fd) < 0) {
262 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
263 strerror(errno));
264 }
265 return connected;
266}
267
Elliott Hughesec283402015-04-10 10:01:53 -0700268bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700269 pthread_mutex_lock(&key_queue_mutex);
270 int pressed = key_pressed[key];
271 pthread_mutex_unlock(&key_queue_mutex);
272 return pressed;
273}
274
Elliott Hughes642aaa72015-04-10 12:47:46 -0700275bool RecoveryUI::IsLongPress() {
276 pthread_mutex_lock(&key_queue_mutex);
277 bool result = key_long_press;
278 pthread_mutex_unlock(&key_queue_mutex);
279 return result;
280}
281
Elliott Hughes4af215b2015-04-10 15:00:34 -0700282bool RecoveryUI::HasThreeButtons() {
283 return has_power_key && has_up_key && has_down_key;
284}
285
Doug Zongker32a0a472011-11-01 11:00:20 -0700286void RecoveryUI::FlushKeys() {
287 pthread_mutex_lock(&key_queue_mutex);
288 key_queue_len = 0;
289 pthread_mutex_unlock(&key_queue_mutex);
290}
291
Elliott Hughes642aaa72015-04-10 12:47:46 -0700292RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
293 pthread_mutex_lock(&key_queue_mutex);
294 key_long_press = false;
295 pthread_mutex_unlock(&key_queue_mutex);
296
297 // 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 -0700298 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700299 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
300 return TOGGLE;
301 }
302 } else {
303 // Otherwise long press of any button toggles to the text display,
304 // and there's no way to toggle back (but that's pretty useless anyway).
305 if (is_long_press && !IsTextVisible()) {
306 return TOGGLE;
307 }
308
309 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
310 if (is_long_press && IsTextVisible()) {
311 EnqueueKey(KEY_ENTER);
312 return IGNORE;
313 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700314 }
315
Elliott Hughes642aaa72015-04-10 12:47:46 -0700316 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700317 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700318 pthread_mutex_lock(&key_queue_mutex);
319 bool reboot_enabled = enable_reboot;
320 pthread_mutex_unlock(&key_queue_mutex);
321
322 if (reboot_enabled) {
323 ++consecutive_power_keys;
324 if (consecutive_power_keys >= 7) {
325 return REBOOT;
326 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700327 }
328 } else {
329 consecutive_power_keys = 0;
330 }
331
Doug Zongker9e805d62013-09-04 13:44:38 -0700332 last_key = key;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700333 return IsTextVisible() ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700334}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800335
Elliott Hughes642aaa72015-04-10 12:47:46 -0700336void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700337}
Doug Zongkerc704e062014-05-23 08:40:35 -0700338
339void RecoveryUI::SetEnableReboot(bool enabled) {
340 pthread_mutex_lock(&key_queue_mutex);
341 enable_reboot = enabled;
342 pthread_mutex_unlock(&key_queue_mutex);
343}
Tao Bao736d59c2017-01-03 10:15:33 -0800344
345void RecoveryUI::SetLocale(const std::string& new_locale) {
346 this->locale_ = new_locale;
347 this->rtl_locale_ = false;
348
349 if (!new_locale.empty()) {
350 size_t underscore = new_locale.find('_');
351 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
352 std::string lang = new_locale.substr(0, underscore);
353
354 // A bit cheesy: keep an explicit list of supported RTL languages.
355 if (lang == "ar" || // Arabic
356 lang == "fa" || // Persian (Farsi)
357 lang == "he" || // Hebrew (new language code)
358 lang == "iw" || // Hebrew (old language code)
359 lang == "ur") { // Urdu
360 rtl_locale_ = true;
361 }
362 }
363}