blob: 3ecd6d125ce89a5c5778c7d66cf21005c005232b [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
Tao Bao6278bdf2017-01-16 17:38:18 -080035#include <android-base/file.h>
36#include <android-base/logging.h>
37#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070038#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080039#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070040#include <cutils/android_reboot.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080041#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070042
43#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070044#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070045#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070046
Tao Bao6278bdf2017-01-16 17:38:18 -080047static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
48static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
49static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070050
Elliott Hughesec283402015-04-10 10:01:53 -070051RecoveryUI::RecoveryUI()
Tao Bao736d59c2017-01-03 10:15:33 -080052 : locale_(""),
53 rtl_locale_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080054 brightness_normal_(50),
55 brightness_dimmed_(25),
Tao Bao736d59c2017-01-03 10:15:33 -080056 key_queue_len(0),
57 key_last_down(-1),
58 key_long_press(false),
59 key_down_count(0),
60 enable_reboot(true),
61 consecutive_power_keys(0),
62 last_key(-1),
63 has_power_key(false),
64 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080065 has_down_key(false),
66 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080067 pthread_mutex_init(&key_queue_mutex, nullptr);
68 pthread_cond_init(&key_queue_cond, nullptr);
69 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070070}
71
Elliott Hughes642aaa72015-04-10 12:47:46 -070072void RecoveryUI::OnKeyDetected(int key_code) {
73 if (key_code == KEY_POWER) {
74 has_power_key = true;
75 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
76 has_down_key = true;
77 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
78 has_up_key = true;
79 }
80}
81
Elliott Hughes985022a2015-04-13 13:04:32 -070082// Reads input events, handles special hot keys, and adds to the key queue.
83static void* InputThreadLoop(void*) {
84 while (true) {
85 if (!ev_wait(-1)) {
86 ev_dispatch();
87 }
88 }
89 return nullptr;
90}
91
Tao Bao6278bdf2017-01-16 17:38:18 -080092bool RecoveryUI::InitScreensaver() {
93 // Disabled.
94 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
95 return false;
96 }
97
98 // Set the initial brightness level based on the max brightness. Note that reading the initial
99 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
100 // we don't have a good way to query the default value.
101 std::string content;
102 if (!android::base::ReadFileToString(MAX_BRIGHTNESS_FILE, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800103 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800104 return false;
105 }
106
107 unsigned int max_value;
108 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
109 LOG(WARNING) << "Failed to parse max brightness: " << content;
110 return false;
111 }
112
113 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
114 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
115 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
116 BRIGHTNESS_FILE)) {
117 PLOG(WARNING) << "Failed to set brightness";
118 return false;
119 }
120
121 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
122 screensaver_state_ = ScreensaverState::NORMAL;
123 return true;
124}
125
Tao Bao736d59c2017-01-03 10:15:33 -0800126bool RecoveryUI::Init(const std::string& locale) {
127 // Set up the locale info.
128 SetLocale(locale);
Elliott Hughes985022a2015-04-13 13:04:32 -0700129
Tao Bao0b1118d2017-01-14 07:46:10 -0800130 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2));
Elliott Hughes985022a2015-04-13 13:04:32 -0700131
Tao Bao736d59c2017-01-03 10:15:33 -0800132 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
133
Tao Bao6278bdf2017-01-16 17:38:18 -0800134 if (!InitScreensaver()) {
135 LOG(INFO) << "Screensaver disabled";
136 }
137
Tao Bao736d59c2017-01-03 10:15:33 -0800138 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
139 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700140}
141
142int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700143 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700144 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700145 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700146 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700147
148 if (ev.type == EV_SYN) {
149 return 0;
150 } else if (ev.type == EV_REL) {
151 if (ev.code == REL_Y) {
152 // accumulate the up or down motion reported by
153 // the trackball. When it exceeds a threshold
154 // (positive or negative), fake an up/down
155 // key event.
Elliott Hughes985022a2015-04-13 13:04:32 -0700156 rel_sum += ev.value;
157 if (rel_sum > 3) {
158 ProcessKey(KEY_DOWN, 1); // press down key
159 ProcessKey(KEY_DOWN, 0); // and release it
160 rel_sum = 0;
161 } else if (rel_sum < -3) {
162 ProcessKey(KEY_UP, 1); // press up key
163 ProcessKey(KEY_UP, 0); // and release it
164 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700165 }
166 }
167 } else {
Elliott Hughes985022a2015-04-13 13:04:32 -0700168 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700169 }
170
Elliott Hughes642aaa72015-04-10 12:47:46 -0700171 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Elliott Hughes985022a2015-04-13 13:04:32 -0700172 ProcessKey(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700173 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700174
175 return 0;
176}
177
178// Process a key-up or -down event. A key is "registered" when it is
179// pressed and then released, with no other keypresses or releases in
180// between. Registered keys are passed to CheckKey() to see if it
181// should trigger a visibility toggle, an immediate reboot, or be
182// queued to be processed next time the foreground thread wants a key
183// (eg, for the menu).
184//
185// We also keep track of which keys are currently down so that
186// CheckKey can call IsKeyPressed to see what other keys are held when
187// a key is registered.
188//
189// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700190void RecoveryUI::ProcessKey(int key_code, int updown) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700191 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800192 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700193 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800194
Doug Zongker32a0a472011-11-01 11:00:20 -0700195 pthread_mutex_lock(&key_queue_mutex);
196 key_pressed[key_code] = updown;
197 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700198 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700199 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700200 key_long_press = false;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700201 key_timer_t* info = new key_timer_t;
202 info->ui = this;
203 info->key_code = key_code;
204 info->count = key_down_count;
Elliott Hughes985022a2015-04-13 13:04:32 -0700205 pthread_t thread;
206 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
207 pthread_detach(thread);
Doug Zongker32a0a472011-11-01 11:00:20 -0700208 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800209 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700210 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700211 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800212 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700213 key_last_down = -1;
214 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700215 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700216 pthread_mutex_unlock(&key_queue_mutex);
217
218 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700219 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800220 case RecoveryUI::IGNORE:
221 break;
222
Doug Zongker32a0a472011-11-01 11:00:20 -0700223 case RecoveryUI::TOGGLE:
224 ShowText(!IsTextVisible());
225 break;
226
227 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700228 if (reboot_enabled) {
Elliott Hughescb220402016-09-23 15:30:55 -0700229 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Tao Bao75238632015-05-27 14:46:17 -0700230 while (true) { pause(); }
Doug Zongkerc704e062014-05-23 08:40:35 -0700231 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700232 break;
233
234 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800235 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700236 break;
237 }
238 }
239}
240
Doug Zongkerc0441d12013-07-31 11:28:24 -0700241void* RecoveryUI::time_key_helper(void* cookie) {
242 key_timer_t* info = (key_timer_t*) cookie;
243 info->ui->time_key(info->key_code, info->count);
244 delete info;
Elliott Hughes985022a2015-04-13 13:04:32 -0700245 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700246}
247
248void RecoveryUI::time_key(int key_code, int count) {
249 usleep(750000); // 750 ms == "long"
250 bool long_press = false;
251 pthread_mutex_lock(&key_queue_mutex);
252 if (key_last_down == key_code && key_down_count == count) {
253 long_press = key_long_press = true;
254 }
255 pthread_mutex_unlock(&key_queue_mutex);
256 if (long_press) KeyLongPress(key_code);
257}
258
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800259void RecoveryUI::EnqueueKey(int key_code) {
260 pthread_mutex_lock(&key_queue_mutex);
261 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
262 if (key_queue_len < queue_max) {
263 key_queue[key_queue_len++] = key_code;
264 pthread_cond_signal(&key_queue_cond);
265 }
266 pthread_mutex_unlock(&key_queue_mutex);
267}
268
Elliott Hughesec283402015-04-10 10:01:53 -0700269int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800270 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700271
Tao Bao6278bdf2017-01-16 17:38:18 -0800272 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
273 // plugged in.
274 do {
275 struct timeval now;
276 struct timespec timeout;
277 gettimeofday(&now, nullptr);
278 timeout.tv_sec = now.tv_sec;
279 timeout.tv_nsec = now.tv_usec * 1000;
280 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700281
Tao Bao6278bdf2017-01-16 17:38:18 -0800282 int rc = 0;
283 while (key_queue_len == 0 && rc != ETIMEDOUT) {
284 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700285 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800286
287 if (screensaver_state_ != ScreensaverState::DISABLED) {
288 if (rc == ETIMEDOUT) {
289 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
290 if (screensaver_state_ == ScreensaverState::NORMAL) {
291 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
292 BRIGHTNESS_FILE)) {
293 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
294 << "%)";
295 screensaver_state_ = ScreensaverState::DIMMED;
296 }
297 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
298 if (android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) {
299 LOG(INFO) << "Brightness: 0 (off)";
300 screensaver_state_ = ScreensaverState::OFF;
301 }
302 }
303 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
304 // Drop the first key if it's changing from OFF to NORMAL.
305 if (screensaver_state_ == ScreensaverState::OFF) {
306 if (key_queue_len > 0) {
307 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
308 }
309 }
310
311 // Reset the brightness to normal.
312 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
313 BRIGHTNESS_FILE)) {
314 screensaver_state_ = ScreensaverState::NORMAL;
315 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
316 << "%)";
317 }
318 }
319 }
320 } while (IsUsbConnected() && key_queue_len == 0);
321
322 int key = -1;
323 if (key_queue_len > 0) {
324 key = key_queue[0];
325 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
326 }
327 pthread_mutex_unlock(&key_queue_mutex);
328 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700329}
330
Elliott Hughes985022a2015-04-13 13:04:32 -0700331bool RecoveryUI::IsUsbConnected() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700332 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
333 if (fd < 0) {
334 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
335 strerror(errno));
336 return 0;
337 }
338
339 char buf;
Elliott Hughes985022a2015-04-13 13:04:32 -0700340 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700341 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
Doug Zongker32a0a472011-11-01 11:00:20 -0700342 if (close(fd) < 0) {
343 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
344 strerror(errno));
345 }
346 return connected;
347}
348
Elliott Hughesec283402015-04-10 10:01:53 -0700349bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700350 pthread_mutex_lock(&key_queue_mutex);
351 int pressed = key_pressed[key];
352 pthread_mutex_unlock(&key_queue_mutex);
353 return pressed;
354}
355
Elliott Hughes642aaa72015-04-10 12:47:46 -0700356bool RecoveryUI::IsLongPress() {
357 pthread_mutex_lock(&key_queue_mutex);
358 bool result = key_long_press;
359 pthread_mutex_unlock(&key_queue_mutex);
360 return result;
361}
362
Elliott Hughes4af215b2015-04-10 15:00:34 -0700363bool RecoveryUI::HasThreeButtons() {
364 return has_power_key && has_up_key && has_down_key;
365}
366
Doug Zongker32a0a472011-11-01 11:00:20 -0700367void RecoveryUI::FlushKeys() {
368 pthread_mutex_lock(&key_queue_mutex);
369 key_queue_len = 0;
370 pthread_mutex_unlock(&key_queue_mutex);
371}
372
Elliott Hughes642aaa72015-04-10 12:47:46 -0700373RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
374 pthread_mutex_lock(&key_queue_mutex);
375 key_long_press = false;
376 pthread_mutex_unlock(&key_queue_mutex);
377
378 // 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 -0700379 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700380 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
381 return TOGGLE;
382 }
383 } else {
384 // Otherwise long press of any button toggles to the text display,
385 // and there's no way to toggle back (but that's pretty useless anyway).
386 if (is_long_press && !IsTextVisible()) {
387 return TOGGLE;
388 }
389
390 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
391 if (is_long_press && IsTextVisible()) {
392 EnqueueKey(KEY_ENTER);
393 return IGNORE;
394 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700395 }
396
Elliott Hughes642aaa72015-04-10 12:47:46 -0700397 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700398 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700399 pthread_mutex_lock(&key_queue_mutex);
400 bool reboot_enabled = enable_reboot;
401 pthread_mutex_unlock(&key_queue_mutex);
402
403 if (reboot_enabled) {
404 ++consecutive_power_keys;
405 if (consecutive_power_keys >= 7) {
406 return REBOOT;
407 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700408 }
409 } else {
410 consecutive_power_keys = 0;
411 }
412
Doug Zongker9e805d62013-09-04 13:44:38 -0700413 last_key = key;
Tao Bao6278bdf2017-01-16 17:38:18 -0800414 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700415}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800416
Elliott Hughes642aaa72015-04-10 12:47:46 -0700417void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700418}
Doug Zongkerc704e062014-05-23 08:40:35 -0700419
420void RecoveryUI::SetEnableReboot(bool enabled) {
421 pthread_mutex_lock(&key_queue_mutex);
422 enable_reboot = enabled;
423 pthread_mutex_unlock(&key_queue_mutex);
424}
Tao Bao736d59c2017-01-03 10:15:33 -0800425
426void RecoveryUI::SetLocale(const std::string& new_locale) {
427 this->locale_ = new_locale;
428 this->rtl_locale_ = false;
429
430 if (!new_locale.empty()) {
431 size_t underscore = new_locale.find('_');
432 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
433 std::string lang = new_locale.substr(0, underscore);
434
435 // A bit cheesy: keep an explicit list of supported RTL languages.
436 if (lang == "ar" || // Arabic
437 lang == "fa" || // Persian (Farsi)
438 lang == "he" || // Hebrew (new language code)
439 lang == "iw" || // Hebrew (old language code)
440 lang == "ur") { // Urdu
441 rtl_locale_ = true;
442 }
443 }
444}