blob: 9194ae3df60b45b5c6a835dae30be0751a363853 [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 Bao9468fc02017-03-17 00:57:37 -070033#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080034#include <string>
35
Tao Bao6278bdf2017-01-16 17:38:18 -080036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070039#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080040#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070041#include <cutils/android_reboot.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080042#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070043
44#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070045#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070046#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070047
Tao Bao6278bdf2017-01-16 17:38:18 -080048static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
49static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
50static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Elliott Hughesec283402015-04-10 10:01:53 -070052RecoveryUI::RecoveryUI()
Tao Bao736d59c2017-01-03 10:15:33 -080053 : locale_(""),
54 rtl_locale_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080055 brightness_normal_(50),
56 brightness_dimmed_(25),
Tao Bao736d59c2017-01-03 10:15:33 -080057 key_queue_len(0),
58 key_last_down(-1),
59 key_long_press(false),
60 key_down_count(0),
61 enable_reboot(true),
62 consecutive_power_keys(0),
63 last_key(-1),
64 has_power_key(false),
65 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080066 has_down_key(false),
67 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080068 pthread_mutex_init(&key_queue_mutex, nullptr);
69 pthread_cond_init(&key_queue_cond, nullptr);
70 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070071}
72
Elliott Hughes642aaa72015-04-10 12:47:46 -070073void RecoveryUI::OnKeyDetected(int key_code) {
74 if (key_code == KEY_POWER) {
75 has_power_key = true;
76 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
77 has_down_key = true;
78 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
79 has_up_key = true;
80 }
81}
82
Elliott Hughes985022a2015-04-13 13:04:32 -070083// Reads input events, handles special hot keys, and adds to the key queue.
84static void* InputThreadLoop(void*) {
85 while (true) {
86 if (!ev_wait(-1)) {
87 ev_dispatch();
88 }
89 }
90 return nullptr;
91}
92
Tao Bao6278bdf2017-01-16 17:38:18 -080093bool RecoveryUI::InitScreensaver() {
94 // Disabled.
95 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
96 return false;
97 }
98
99 // Set the initial brightness level based on the max brightness. Note that reading the initial
100 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
101 // we don't have a good way to query the default value.
102 std::string content;
103 if (!android::base::ReadFileToString(MAX_BRIGHTNESS_FILE, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800104 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800105 return false;
106 }
107
108 unsigned int max_value;
109 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
110 LOG(WARNING) << "Failed to parse max brightness: " << content;
111 return false;
112 }
113
114 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
115 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
116 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
117 BRIGHTNESS_FILE)) {
118 PLOG(WARNING) << "Failed to set brightness";
119 return false;
120 }
121
122 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
123 screensaver_state_ = ScreensaverState::NORMAL;
124 return true;
125}
126
Tao Bao736d59c2017-01-03 10:15:33 -0800127bool RecoveryUI::Init(const std::string& locale) {
128 // Set up the locale info.
129 SetLocale(locale);
Elliott Hughes985022a2015-04-13 13:04:32 -0700130
Tao Bao0b1118d2017-01-14 07:46:10 -0800131 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2));
Elliott Hughes985022a2015-04-13 13:04:32 -0700132
Tao Bao736d59c2017-01-03 10:15:33 -0800133 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
134
Tao Bao6278bdf2017-01-16 17:38:18 -0800135 if (!InitScreensaver()) {
136 LOG(INFO) << "Screensaver disabled";
137 }
138
Tao Bao736d59c2017-01-03 10:15:33 -0800139 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
140 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700141}
142
143int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700144 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700145 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700146 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700147 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700148
149 if (ev.type == EV_SYN) {
150 return 0;
151 } else if (ev.type == EV_REL) {
152 if (ev.code == REL_Y) {
153 // accumulate the up or down motion reported by
154 // the trackball. When it exceeds a threshold
155 // (positive or negative), fake an up/down
156 // key event.
Elliott Hughes985022a2015-04-13 13:04:32 -0700157 rel_sum += ev.value;
158 if (rel_sum > 3) {
159 ProcessKey(KEY_DOWN, 1); // press down key
160 ProcessKey(KEY_DOWN, 0); // and release it
161 rel_sum = 0;
162 } else if (rel_sum < -3) {
163 ProcessKey(KEY_UP, 1); // press up key
164 ProcessKey(KEY_UP, 0); // and release it
165 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700166 }
167 }
168 } else {
Elliott Hughes985022a2015-04-13 13:04:32 -0700169 rel_sum = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700170 }
171
Elliott Hughes642aaa72015-04-10 12:47:46 -0700172 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Elliott Hughes985022a2015-04-13 13:04:32 -0700173 ProcessKey(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700174 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700175
176 return 0;
177}
178
179// Process a key-up or -down event. A key is "registered" when it is
180// pressed and then released, with no other keypresses or releases in
181// between. Registered keys are passed to CheckKey() to see if it
182// should trigger a visibility toggle, an immediate reboot, or be
183// queued to be processed next time the foreground thread wants a key
184// (eg, for the menu).
185//
186// We also keep track of which keys are currently down so that
187// CheckKey can call IsKeyPressed to see what other keys are held when
188// a key is registered.
189//
190// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700191void RecoveryUI::ProcessKey(int key_code, int updown) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700192 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800193 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700194 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800195
Doug Zongker32a0a472011-11-01 11:00:20 -0700196 pthread_mutex_lock(&key_queue_mutex);
197 key_pressed[key_code] = updown;
198 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700199 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700200 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700201 key_long_press = false;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700202 key_timer_t* info = new key_timer_t;
203 info->ui = this;
204 info->key_code = key_code;
205 info->count = key_down_count;
Elliott Hughes985022a2015-04-13 13:04:32 -0700206 pthread_t thread;
207 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
208 pthread_detach(thread);
Doug Zongker32a0a472011-11-01 11:00:20 -0700209 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800210 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700211 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700212 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800213 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700214 key_last_down = -1;
215 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700216 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700217 pthread_mutex_unlock(&key_queue_mutex);
218
219 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700220 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800221 case RecoveryUI::IGNORE:
222 break;
223
Doug Zongker32a0a472011-11-01 11:00:20 -0700224 case RecoveryUI::TOGGLE:
225 ShowText(!IsTextVisible());
226 break;
227
228 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700229 if (reboot_enabled) {
Elliott Hughescb220402016-09-23 15:30:55 -0700230 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Tao Bao75238632015-05-27 14:46:17 -0700231 while (true) { pause(); }
Doug Zongkerc704e062014-05-23 08:40:35 -0700232 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700233 break;
234
235 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800236 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700237 break;
238 }
239 }
240}
241
Doug Zongkerc0441d12013-07-31 11:28:24 -0700242void* RecoveryUI::time_key_helper(void* cookie) {
Mikhail Lappo20791bd2017-03-23 21:30:36 +0100243 key_timer_t* info = static_cast<key_timer_t*>(cookie);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700244 info->ui->time_key(info->key_code, info->count);
245 delete info;
Elliott Hughes985022a2015-04-13 13:04:32 -0700246 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700247}
248
249void RecoveryUI::time_key(int key_code, int count) {
250 usleep(750000); // 750 ms == "long"
251 bool long_press = false;
252 pthread_mutex_lock(&key_queue_mutex);
253 if (key_last_down == key_code && key_down_count == count) {
254 long_press = key_long_press = true;
255 }
256 pthread_mutex_unlock(&key_queue_mutex);
257 if (long_press) KeyLongPress(key_code);
258}
259
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800260void RecoveryUI::EnqueueKey(int key_code) {
261 pthread_mutex_lock(&key_queue_mutex);
262 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
263 if (key_queue_len < queue_max) {
264 key_queue[key_queue_len++] = key_code;
265 pthread_cond_signal(&key_queue_cond);
266 }
267 pthread_mutex_unlock(&key_queue_mutex);
268}
269
Elliott Hughesec283402015-04-10 10:01:53 -0700270int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800271 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700272
Tao Bao6278bdf2017-01-16 17:38:18 -0800273 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
274 // plugged in.
275 do {
276 struct timeval now;
277 struct timespec timeout;
278 gettimeofday(&now, nullptr);
279 timeout.tv_sec = now.tv_sec;
280 timeout.tv_nsec = now.tv_usec * 1000;
281 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700282
Tao Bao6278bdf2017-01-16 17:38:18 -0800283 int rc = 0;
284 while (key_queue_len == 0 && rc != ETIMEDOUT) {
285 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700286 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800287
288 if (screensaver_state_ != ScreensaverState::DISABLED) {
289 if (rc == ETIMEDOUT) {
290 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
291 if (screensaver_state_ == ScreensaverState::NORMAL) {
292 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
293 BRIGHTNESS_FILE)) {
294 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
295 << "%)";
296 screensaver_state_ = ScreensaverState::DIMMED;
297 }
298 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
299 if (android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) {
300 LOG(INFO) << "Brightness: 0 (off)";
301 screensaver_state_ = ScreensaverState::OFF;
302 }
303 }
304 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
305 // Drop the first key if it's changing from OFF to NORMAL.
306 if (screensaver_state_ == ScreensaverState::OFF) {
307 if (key_queue_len > 0) {
308 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
309 }
310 }
311
312 // Reset the brightness to normal.
313 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
314 BRIGHTNESS_FILE)) {
315 screensaver_state_ = ScreensaverState::NORMAL;
316 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
317 << "%)";
318 }
319 }
320 }
321 } while (IsUsbConnected() && key_queue_len == 0);
322
323 int key = -1;
324 if (key_queue_len > 0) {
325 key = key_queue[0];
326 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
327 }
328 pthread_mutex_unlock(&key_queue_mutex);
329 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700330}
331
Elliott Hughes985022a2015-04-13 13:04:32 -0700332bool RecoveryUI::IsUsbConnected() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700333 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
334 if (fd < 0) {
335 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
336 strerror(errno));
337 return 0;
338 }
339
340 char buf;
Elliott Hughes985022a2015-04-13 13:04:32 -0700341 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700342 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
Doug Zongker32a0a472011-11-01 11:00:20 -0700343 if (close(fd) < 0) {
344 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
345 strerror(errno));
346 }
347 return connected;
348}
349
Elliott Hughesec283402015-04-10 10:01:53 -0700350bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700351 pthread_mutex_lock(&key_queue_mutex);
352 int pressed = key_pressed[key];
353 pthread_mutex_unlock(&key_queue_mutex);
354 return pressed;
355}
356
Elliott Hughes642aaa72015-04-10 12:47:46 -0700357bool RecoveryUI::IsLongPress() {
358 pthread_mutex_lock(&key_queue_mutex);
359 bool result = key_long_press;
360 pthread_mutex_unlock(&key_queue_mutex);
361 return result;
362}
363
Elliott Hughes4af215b2015-04-10 15:00:34 -0700364bool RecoveryUI::HasThreeButtons() {
365 return has_power_key && has_up_key && has_down_key;
366}
367
Doug Zongker32a0a472011-11-01 11:00:20 -0700368void RecoveryUI::FlushKeys() {
369 pthread_mutex_lock(&key_queue_mutex);
370 key_queue_len = 0;
371 pthread_mutex_unlock(&key_queue_mutex);
372}
373
Elliott Hughes642aaa72015-04-10 12:47:46 -0700374RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
375 pthread_mutex_lock(&key_queue_mutex);
376 key_long_press = false;
377 pthread_mutex_unlock(&key_queue_mutex);
378
379 // 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 -0700380 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700381 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
382 return TOGGLE;
383 }
384 } else {
385 // Otherwise long press of any button toggles to the text display,
386 // and there's no way to toggle back (but that's pretty useless anyway).
387 if (is_long_press && !IsTextVisible()) {
388 return TOGGLE;
389 }
390
391 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
392 if (is_long_press && IsTextVisible()) {
393 EnqueueKey(KEY_ENTER);
394 return IGNORE;
395 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700396 }
397
Elliott Hughes642aaa72015-04-10 12:47:46 -0700398 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700399 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700400 pthread_mutex_lock(&key_queue_mutex);
401 bool reboot_enabled = enable_reboot;
402 pthread_mutex_unlock(&key_queue_mutex);
403
404 if (reboot_enabled) {
405 ++consecutive_power_keys;
406 if (consecutive_power_keys >= 7) {
407 return REBOOT;
408 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700409 }
410 } else {
411 consecutive_power_keys = 0;
412 }
413
Doug Zongker9e805d62013-09-04 13:44:38 -0700414 last_key = key;
Tao Bao6278bdf2017-01-16 17:38:18 -0800415 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700416}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800417
Elliott Hughes642aaa72015-04-10 12:47:46 -0700418void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700419}
Doug Zongkerc704e062014-05-23 08:40:35 -0700420
421void RecoveryUI::SetEnableReboot(bool enabled) {
422 pthread_mutex_lock(&key_queue_mutex);
423 enable_reboot = enabled;
424 pthread_mutex_unlock(&key_queue_mutex);
425}
Tao Bao736d59c2017-01-03 10:15:33 -0800426
427void RecoveryUI::SetLocale(const std::string& new_locale) {
428 this->locale_ = new_locale;
429 this->rtl_locale_ = false;
430
431 if (!new_locale.empty()) {
432 size_t underscore = new_locale.find('_');
433 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
434 std::string lang = new_locale.substr(0, underscore);
435
436 // A bit cheesy: keep an explicit list of supported RTL languages.
437 if (lang == "ar" || // Arabic
438 lang == "fa" || // Persian (Farsi)
439 lang == "he" || // Hebrew (new language code)
440 lang == "iw" || // Hebrew (old language code)
441 lang == "ur") { // Urdu
442 rtl_locale_ = true;
443 }
444 }
445}