blob: aced5b09567ef7e9096448c3225c9dcbe6e08af4 [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
Tianjie Xub5108c32018-08-20 13:40:47 -070017#include "recovery_ui/ui.h"
Tao Bao736d59c2017-01-03 10:15:33 -080018
Doug Zongker32a0a472011-11-01 11:00:20 -070019#include <errno.h>
20#include <fcntl.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070024#include <sys/time.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
Tao Bao26ea9592018-05-09 16:32:02 -070029#include <chrono>
Tao Bao9468fc02017-03-17 00:57:37 -070030#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080031#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070032#include <thread>
Tao Bao736d59c2017-01-03 10:15:33 -080033
Tao Bao6278bdf2017-01-16 17:38:18 -080034#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/parseint.h>
Tao Bao0bc88de2018-07-31 14:53:16 -070037#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080038#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070039
Tao Bao26ea9592018-05-09 16:32:02 -070040#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070041#include "otautil/sysutil.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
Tao Bao26ea9592018-05-09 16:32:02 -070043using namespace std::chrono_literals;
44
Tao Bao0bc88de2018-07-31 14:53:16 -070045constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
46constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
48constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness";
49constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
kataoc35c1b02017-12-08 11:02:43 +080050 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Tao Bao0bc88de2018-07-31 14:53:16 -070052constexpr int kDefaultTouchLowThreshold = 50;
53constexpr int kDefaultTouchHighThreshold = 90;
54
Elliott Hughesec283402015-04-10 10:01:53 -070055RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080056 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080057 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080058 brightness_file_(BRIGHTNESS_FILE),
59 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tom Marshall2ec11982017-08-23 22:45:19 +000060 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080061 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070062 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
63 kDefaultTouchLowThreshold)),
64 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
65 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070066 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080067 key_queue_len(0),
68 key_last_down(-1),
69 key_long_press(false),
70 key_down_count(0),
71 enable_reboot(true),
72 consecutive_power_keys(0),
73 last_key(-1),
74 has_power_key(false),
75 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080076 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070077 has_touch_screen(false),
78 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070079 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080080 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080081 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070082}
83
Tao Bao26ea9592018-05-09 16:32:02 -070084RecoveryUI::~RecoveryUI() {
85 ev_exit();
86 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070087 if (input_thread_.joinable()) {
88 input_thread_.join();
89 }
Tao Bao26ea9592018-05-09 16:32:02 -070090}
91
Tom Marshalld23b5102017-08-24 13:50:01 +000092void RecoveryUI::OnTouchDeviceDetected(int fd) {
93 char name[256];
94 char path[PATH_MAX];
95 char buf[4096];
96
97 memset(name, 0, sizeof(name));
98 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
99 return;
100 }
101 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
102 int vkfd = open(path, O_RDONLY);
103 if (vkfd < 0) {
104 LOG(INFO) << "vkeys: could not open " << path;
105 return;
106 }
107 ssize_t len = read(vkfd, buf, sizeof(buf));
108 close(vkfd);
109 if (len <= 0) {
110 LOG(ERROR) << "vkeys: could not read " << path;
111 return;
112 }
113 buf[len] = '\0';
114
115 char* p = buf;
116 char* endp;
117 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
118 int val[6];
119 int f;
120 for (f = 0; *p && f < 6; ++f) {
121 val[f] = strtol(p, &endp, 0);
122 if (p == endp) break;
123 p = endp + 1;
124 }
125 if (f != 6 || val[0] != 0x01) break;
126 vkey_t vk;
127 vk.keycode = val[1];
128 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
129 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
130 virtual_keys_.push_back(vk);
131 }
132}
133
Elliott Hughes642aaa72015-04-10 12:47:46 -0700134void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700135 if (key_code == KEY_POWER) {
136 has_power_key = true;
137 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
138 has_down_key = true;
139 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
140 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700141 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
142 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700143 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700144}
145
Tao Bao6278bdf2017-01-16 17:38:18 -0800146bool RecoveryUI::InitScreensaver() {
147 // Disabled.
148 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
149 return false;
150 }
kataoc35c1b02017-12-08 11:02:43 +0800151 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
152 brightness_file_ = BRIGHTNESS_FILE_SDM;
153 }
154 if (access(max_brightness_file_.c_str(), R_OK)) {
155 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
156 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800157 // Set the initial brightness level based on the max brightness. Note that reading the initial
158 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
159 // we don't have a good way to query the default value.
160 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800161 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800162 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800163 return false;
164 }
165
166 unsigned int max_value;
167 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
168 LOG(WARNING) << "Failed to parse max brightness: " << content;
169 return false;
170 }
171
172 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
173 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
174 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800175 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800176 PLOG(WARNING) << "Failed to set brightness";
177 return false;
178 }
179
180 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
181 screensaver_state_ = ScreensaverState::NORMAL;
182 return true;
183}
184
Tao Baoefb49ad2017-01-31 23:03:10 -0800185bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700186 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
187 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700188
Tao Bao736d59c2017-01-03 10:15:33 -0800189 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
190
Tao Bao5f8dd992017-07-28 00:05:40 -0700191 if (touch_screen_allowed_) {
Tom Marshalld23b5102017-08-24 13:50:01 +0000192 ev_iterate_touch_inputs(
193 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
194 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700195
196 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
197 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
198 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
199 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
200 // mode will be turned on automatically on debuggable builds, even without a swipe.
201 std::string cmdline;
202 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
203 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
204 } else {
205 // Non-fatal, and won't affect Init() result.
206 PLOG(WARNING) << "Failed to read /proc/cmdline";
207 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700208 }
209
Tao Bao6278bdf2017-01-16 17:38:18 -0800210 if (!InitScreensaver()) {
211 LOG(INFO) << "Screensaver disabled";
212 }
213
Tao Bao26ea9592018-05-09 16:32:02 -0700214 // Create a separate thread that handles input events.
215 input_thread_ = std::thread([this]() {
216 while (!this->input_thread_stopped_) {
217 if (!ev_wait(500)) {
218 ev_dispatch();
219 }
220 }
221 });
222
Tao Bao736d59c2017-01-03 10:15:33 -0800223 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700224}
225
Tom Marshall778cbae2017-09-08 19:48:46 +0000226void RecoveryUI::Stop() {
227 if (!android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) {
228 PLOG(WARNING) << "Failed to write brightness file";
229 }
230}
231
Tom Marshall0441a6c2017-08-24 12:57:27 +0000232void RecoveryUI::OnTouchEvent() {
233 Point delta = touch_pos_ - touch_start_;
Tao Bao5f8dd992017-07-28 00:05:40 -0700234 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
235
236 // We only consider a valid swipe if:
Tao Bao0bc88de2018-07-31 14:53:16 -0700237 // - the delta along one axis is below touch_low_threshold_;
238 // - and the delta along the other axis is beyond touch_high_threshold_.
Tom Marshall0441a6c2017-08-24 12:57:27 +0000239 if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) {
240 direction = delta.x() < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
241 } else if (abs(delta.x()) < touch_low_threshold_ && abs(delta.y()) > touch_high_threshold_) {
242 direction = delta.y() < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
Tao Bao5f8dd992017-07-28 00:05:40 -0700243 } else {
Tom Marshalld23b5102017-08-24 13:50:01 +0000244 for (const auto& vk : virtual_keys_) {
245 if (touch_start_.x() >= vk.min_.x() && touch_start_.x() < vk.max_.x() &&
246 touch_start_.y() >= vk.min_.y() && touch_start_.y() < vk.max_.y()) {
247 ProcessKey(vk.keycode, 1); // press key
248 ProcessKey(vk.keycode, 0); // and release it
249 return;
250 }
251 }
Tom Marshall0441a6c2017-08-24 12:57:27 +0000252 LOG(DEBUG) << "Ignored " << delta.x() << " " << delta.y() << " (low: " << touch_low_threshold_
Tao Bao0bc88de2018-07-31 14:53:16 -0700253 << ", high: " << touch_high_threshold_ << ")";
Tao Bao5f8dd992017-07-28 00:05:40 -0700254 return;
255 }
256
Tao Bao046aae22017-07-31 23:15:09 -0700257 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
258 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
259 ShowText(true);
260 return;
261 }
262
Tao Bao5f8dd992017-07-28 00:05:40 -0700263 LOG(DEBUG) << "Swipe direction=" << direction;
264 switch (direction) {
265 case SwipeDirection::UP:
266 ProcessKey(KEY_UP, 1); // press up key
267 ProcessKey(KEY_UP, 0); // and release it
268 break;
269
270 case SwipeDirection::DOWN:
271 ProcessKey(KEY_DOWN, 1); // press down key
272 ProcessKey(KEY_DOWN, 0); // and release it
273 break;
274
275 case SwipeDirection::LEFT:
Tom Marshalld23b5102017-08-24 13:50:01 +0000276 ProcessKey(KEY_BACK, 1); // press back key
277 ProcessKey(KEY_BACK, 0); // and release it
278 break;
Tao Bao5f8dd992017-07-28 00:05:40 -0700279 case SwipeDirection::RIGHT:
280 ProcessKey(KEY_POWER, 1); // press power key
281 ProcessKey(KEY_POWER, 0); // and release it
282 break;
283 };
284}
285
Elliott Hughes985022a2015-04-13 13:04:32 -0700286int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700287 struct input_event ev;
288 if (ev_get_input(fd, epevents, &ev) == -1) {
289 return -1;
290 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700291
Tao Bao5f8dd992017-07-28 00:05:40 -0700292 // Touch inputs handling.
293 //
294 // We handle the touch inputs by tracking the position changes between initial contacting and
295 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
296 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
297 //
298 // Per the doc Multi-touch Protocol at below, there are two protocols.
299 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
300 //
301 // The main difference between the stateless type A protocol and the stateful type B slot protocol
302 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
303 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
304 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
305 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
306 //
307 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
308 // ABS_MT_TRACKING_ID being -1.
309 //
310 // Touch input events will only be available if touch_screen_allowed_ is set.
311
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700312 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700313 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
314 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
315 // contact.
316 if (touch_finger_down_ && !touch_swiping_) {
Tom Marshall0441a6c2017-08-24 12:57:27 +0000317 touch_start_ = touch_pos_;
Tao Bao5f8dd992017-07-28 00:05:40 -0700318 touch_swiping_ = true;
319 } else if (!touch_finger_down_ && touch_swiping_) {
320 touch_swiping_ = false;
Tom Marshall0441a6c2017-08-24 12:57:27 +0000321 OnTouchEvent();
Tao Bao5f8dd992017-07-28 00:05:40 -0700322 }
323 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700324 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700325 }
326
327 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700328 if (ev.code == REL_Y) {
329 // accumulate the up or down motion reported by
330 // the trackball. When it exceeds a threshold
331 // (positive or negative), fake an up/down
332 // key event.
333 rel_sum += ev.value;
334 if (rel_sum > 3) {
335 ProcessKey(KEY_DOWN, 1); // press down key
336 ProcessKey(KEY_DOWN, 0); // and release it
337 rel_sum = 0;
338 } else if (rel_sum < -3) {
339 ProcessKey(KEY_UP, 1); // press up key
340 ProcessKey(KEY_UP, 0); // and release it
341 rel_sum = 0;
342 }
343 }
344 } else {
345 rel_sum = 0;
346 }
347
Tao Bao5f8dd992017-07-28 00:05:40 -0700348 if (touch_screen_allowed_ && ev.type == EV_ABS) {
349 if (ev.code == ABS_MT_SLOT) {
350 touch_slot_ = ev.value;
351 }
352 // Ignore other fingers.
353 if (touch_slot_ > 0) return 0;
354
355 switch (ev.code) {
356 case ABS_MT_POSITION_X:
Tom Marshall0441a6c2017-08-24 12:57:27 +0000357 touch_pos_.x(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700358 touch_finger_down_ = true;
359 break;
360
361 case ABS_MT_POSITION_Y:
Tom Marshall0441a6c2017-08-24 12:57:27 +0000362 touch_pos_.y(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700363 touch_finger_down_ = true;
364 break;
365
366 case ABS_MT_TRACKING_ID:
367 // Protocol B: -1 marks lifting the contact.
368 if (ev.value < 0) touch_finger_down_ = false;
369 break;
370 }
371 return 0;
372 }
373
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700374 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700375 if (touch_screen_allowed_) {
376 if (ev.code == BTN_TOUCH) {
377 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
378 // lifting the contact.
379 touch_finger_down_ = (ev.value == 1);
380 }
381
382 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
383 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
384 // KEY_POWER and KEY_UP as KEY_DOWN).
385 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
386 return 0;
387 }
388 }
389
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700390 ProcessKey(ev.code, ev.value);
391 }
392
393 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700394}
395
Tao Bao26ea9592018-05-09 16:32:02 -0700396// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
397// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
398// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
399// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700400//
Tao Bao26ea9592018-05-09 16:32:02 -0700401// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
402// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700403//
404// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700405void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700406 bool register_key = false;
407 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800408
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700409 {
410 std::lock_guard<std::mutex> lg(key_queue_mutex);
411 key_pressed[key_code] = updown;
412 if (updown) {
413 ++key_down_count;
414 key_last_down = key_code;
415 key_long_press = false;
416 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
417 time_key_thread.detach();
418 } else {
419 if (key_last_down == key_code) {
420 long_press = key_long_press;
421 register_key = true;
422 }
423 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700424 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700425 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700426
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700427 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700428 if (register_key) {
429 switch (CheckKey(key_code, long_press)) {
430 case RecoveryUI::IGNORE:
431 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800432
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700433 case RecoveryUI::TOGGLE:
434 ShowText(!IsTextVisible());
435 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700436
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700437 case RecoveryUI::REBOOT:
438 if (reboot_enabled) {
439 reboot("reboot,");
440 while (true) {
441 pause();
442 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700443 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700444 break;
445
446 case RecoveryUI::ENQUEUE:
447 EnqueueKey(key_code);
448 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700449 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700450 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700451}
452
Tao Bao26ea9592018-05-09 16:32:02 -0700453void RecoveryUI::TimeKey(int key_code, int count) {
454 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700455 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700456 {
457 std::lock_guard<std::mutex> lg(key_queue_mutex);
458 if (key_last_down == key_code && key_down_count == count) {
459 long_press = key_long_press = true;
460 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700461 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700462 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700463}
464
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800465void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700466 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700467 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
468 if (key_queue_len < queue_max) {
469 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700470 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700471 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800472}
473
Jerry Zhangb76af932018-05-22 12:08:35 -0700474void RecoveryUI::SetScreensaverState(ScreensaverState state) {
475 switch (state) {
476 case ScreensaverState::NORMAL:
477 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
478 brightness_file_)) {
479 screensaver_state_ = ScreensaverState::NORMAL;
480 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
481 << "%)";
482 } else {
483 LOG(ERROR) << "Unable to set brightness to normal";
484 }
485 break;
486 case ScreensaverState::DIMMED:
487 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
488 brightness_file_)) {
489 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
490 << "%)";
491 screensaver_state_ = ScreensaverState::DIMMED;
492 } else {
493 LOG(ERROR) << "Unable to set brightness to dim";
494 }
495 break;
496 case ScreensaverState::OFF:
497 if (android::base::WriteStringToFile("0", brightness_file_)) {
498 LOG(INFO) << "Brightness: 0 (off)";
499 screensaver_state_ = ScreensaverState::OFF;
500 } else {
501 LOG(ERROR) << "Unable to set brightness to off";
502 }
503 break;
504 default:
505 LOG(ERROR) << "Invalid screensaver state";
506 }
507}
508
Elliott Hughesec283402015-04-10 10:01:53 -0700509int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700510 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700511
Jerry Zhangb76af932018-05-22 12:08:35 -0700512 // Check for a saved key queue interruption.
513 if (key_interrupted_) {
514 SetScreensaverState(ScreensaverState::NORMAL);
515 return static_cast<int>(KeyError::INTERRUPTED);
516 }
517
Tao Bao53be3322018-08-16 11:48:50 -0700518 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800519 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700520 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
521 return this->key_queue_len != 0 || key_interrupted_;
522 });
523 if (key_interrupted_) {
524 SetScreensaverState(ScreensaverState::NORMAL);
525 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700526 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800527 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700528 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700529 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800530 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700531 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800532 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700533 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800534 }
Tao Bao53be3322018-08-16 11:48:50 -0700535 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800536 // Drop the first key if it's changing from OFF to NORMAL.
537 if (screensaver_state_ == ScreensaverState::OFF) {
538 if (key_queue_len > 0) {
539 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
540 }
541 }
542
543 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700544 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800545 }
546 }
547 } while (IsUsbConnected() && key_queue_len == 0);
548
Jerry Zhangb76af932018-05-22 12:08:35 -0700549 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800550 if (key_queue_len > 0) {
551 key = key_queue[0];
552 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
553 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800554 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700555}
556
Jerry Zhangb76af932018-05-22 12:08:35 -0700557void RecoveryUI::InterruptKey() {
558 {
559 std::lock_guard<std::mutex> lg(key_queue_mutex);
560 key_interrupted_ = true;
561 }
562 key_queue_cond.notify_one();
563}
564
Elliott Hughes985022a2015-04-13 13:04:32 -0700565bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700566 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
567 if (fd < 0) {
568 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
569 return 0;
570 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700571
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700572 char buf;
573 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
574 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
575 if (close(fd) < 0) {
576 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
577 }
578 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700579}
580
Elliott Hughesec283402015-04-10 10:01:53 -0700581bool RecoveryUI::IsKeyPressed(int key) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700582 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700583 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700584 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700585}
586
Elliott Hughes642aaa72015-04-10 12:47:46 -0700587bool RecoveryUI::IsLongPress() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700588 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700589 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700590 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700591}
592
Elliott Hughes4af215b2015-04-10 15:00:34 -0700593bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700594 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700595}
596
Tao Bao5f8dd992017-07-28 00:05:40 -0700597bool RecoveryUI::HasPowerKey() const {
598 return has_power_key;
599}
600
601bool RecoveryUI::HasTouchScreen() const {
602 return has_touch_screen;
603}
604
Doug Zongker32a0a472011-11-01 11:00:20 -0700605void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700606 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700607 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700608}
609
Elliott Hughes642aaa72015-04-10 12:47:46 -0700610RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700611 {
612 std::lock_guard<std::mutex> lg(key_queue_mutex);
613 key_long_press = false;
614 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700615
616 // If we have power and volume up keys, that chord is the signal to toggle the text display.
Tao Bao5f8dd992017-07-28 00:05:40 -0700617 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
618 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700619 return TOGGLE;
620 }
621 } else {
622 // Otherwise long press of any button toggles to the text display,
623 // and there's no way to toggle back (but that's pretty useless anyway).
624 if (is_long_press && !IsTextVisible()) {
625 return TOGGLE;
626 }
627
628 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
629 if (is_long_press && IsTextVisible()) {
630 EnqueueKey(KEY_ENTER);
631 return IGNORE;
632 }
633 }
634
635 // Press power seven times in a row to reboot.
636 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700637 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700638
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700639 if (reboot_enabled) {
640 ++consecutive_power_keys;
641 if (consecutive_power_keys >= 7) {
642 return REBOOT;
643 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700644 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700645 } else {
646 consecutive_power_keys = 0;
647 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700648
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700649 last_key = key;
650 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700651}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800652
Tianjie Xub5108c32018-08-20 13:40:47 -0700653void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700654
655void RecoveryUI::SetEnableReboot(bool enabled) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700656 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700657 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700658}