blob: 91b3edb3be986efbb4c72e692b16b046da7ab91b [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 Xu8f397302018-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";
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -070051constexpr const char* BRIGHTNESS_FILE_PWM =
52 "/sys/class/backlight/pwm-backlight.0/brightness";
53constexpr const char* MAX_BRIGHTNESS_FILE_PWM =
54 "/sys/class/backlight/pwm-backlight.0/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070055
Tao Bao0bc88de2018-07-31 14:53:16 -070056constexpr int kDefaultTouchLowThreshold = 50;
57constexpr int kDefaultTouchHighThreshold = 90;
58
Elliott Hughesec283402015-04-10 10:01:53 -070059RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080060 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080061 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080062 brightness_file_(BRIGHTNESS_FILE),
63 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tom Marshall4c80d0d2017-08-23 22:45:19 +000064 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080065 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070066 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
67 kDefaultTouchLowThreshold)),
68 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
69 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070070 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080071 key_queue_len(0),
72 key_last_down(-1),
73 key_long_press(false),
74 key_down_count(0),
75 enable_reboot(true),
76 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080077 has_power_key(false),
78 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080079 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070080 has_touch_screen(false),
81 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070082 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080083 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080084 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070085}
86
Tao Bao26ea9592018-05-09 16:32:02 -070087RecoveryUI::~RecoveryUI() {
88 ev_exit();
89 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070090 if (input_thread_.joinable()) {
91 input_thread_.join();
92 }
Tao Bao26ea9592018-05-09 16:32:02 -070093}
94
Tom Marshall4b921692017-08-24 13:50:01 +000095void RecoveryUI::OnTouchDeviceDetected(int fd) {
96 char name[256];
97 char path[PATH_MAX];
98 char buf[4096];
99
100 memset(name, 0, sizeof(name));
101 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
102 return;
103 }
104 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
105 int vkfd = open(path, O_RDONLY);
106 if (vkfd < 0) {
107 LOG(INFO) << "vkeys: could not open " << path;
108 return;
109 }
110 ssize_t len = read(vkfd, buf, sizeof(buf));
111 close(vkfd);
112 if (len <= 0) {
113 LOG(ERROR) << "vkeys: could not read " << path;
114 return;
115 }
116 buf[len] = '\0';
117
118 char* p = buf;
119 char* endp;
120 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
121 int val[6];
122 int f;
123 for (f = 0; *p && f < 6; ++f) {
124 val[f] = strtol(p, &endp, 0);
125 if (p == endp) break;
126 p = endp + 1;
127 }
128 if (f != 6 || val[0] != 0x01) break;
129 vkey_t vk;
130 vk.keycode = val[1];
131 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
132 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
133 virtual_keys_.push_back(vk);
134 }
135}
136
Elliott Hughes642aaa72015-04-10 12:47:46 -0700137void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700138 if (key_code == KEY_POWER) {
139 has_power_key = true;
140 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
141 has_down_key = true;
142 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
143 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700144 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
145 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700146 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700147}
148
Tao Bao6278bdf2017-01-16 17:38:18 -0800149bool RecoveryUI::InitScreensaver() {
150 // Disabled.
151 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
152 return false;
153 }
kataoc35c1b02017-12-08 11:02:43 +0800154 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700155 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
156 brightness_file_ = BRIGHTNESS_FILE_SDM;
157 } else {
158 brightness_file_ = BRIGHTNESS_FILE_PWM;
159 }
kataoc35c1b02017-12-08 11:02:43 +0800160 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700161
kataoc35c1b02017-12-08 11:02:43 +0800162 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700163 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
164 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
165 } else {
166 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
167 }
kataoc35c1b02017-12-08 11:02:43 +0800168 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800169 // Set the initial brightness level based on the max brightness. Note that reading the initial
170 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
171 // we don't have a good way to query the default value.
172 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800173 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800174 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800175 return false;
176 }
177
178 unsigned int max_value;
179 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
180 LOG(WARNING) << "Failed to parse max brightness: " << content;
181 return false;
182 }
183
184 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
185 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
186 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800187 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800188 PLOG(WARNING) << "Failed to set brightness";
189 return false;
190 }
191
192 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
193 screensaver_state_ = ScreensaverState::NORMAL;
194 return true;
195}
196
Tao Baoefb49ad2017-01-31 23:03:10 -0800197bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700198 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
199 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700200
Tao Bao736d59c2017-01-03 10:15:33 -0800201 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
202
Tao Bao5f8dd992017-07-28 00:05:40 -0700203 if (touch_screen_allowed_) {
Tom Marshall4b921692017-08-24 13:50:01 +0000204 ev_iterate_touch_inputs(
205 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
206 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700207
208 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
209 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
210 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
211 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
212 // mode will be turned on automatically on debuggable builds, even without a swipe.
213 std::string cmdline;
214 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
215 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
216 } else {
217 // Non-fatal, and won't affect Init() result.
218 PLOG(WARNING) << "Failed to read /proc/cmdline";
219 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700220 }
221
Tao Bao6278bdf2017-01-16 17:38:18 -0800222 if (!InitScreensaver()) {
223 LOG(INFO) << "Screensaver disabled";
224 }
225
Tao Bao26ea9592018-05-09 16:32:02 -0700226 // Create a separate thread that handles input events.
227 input_thread_ = std::thread([this]() {
228 while (!this->input_thread_stopped_) {
229 if (!ev_wait(500)) {
230 ev_dispatch();
231 }
232 }
233 });
234
Tao Bao736d59c2017-01-03 10:15:33 -0800235 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700236}
237
Tom Marshall0aedc572017-08-24 12:57:27 +0000238void RecoveryUI::OnTouchEvent() {
239 Point delta = touch_pos_ - touch_start_;
Tao Bao5f8dd992017-07-28 00:05:40 -0700240 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
241
242 // We only consider a valid swipe if:
Tao Bao0bc88de2018-07-31 14:53:16 -0700243 // - the delta along one axis is below touch_low_threshold_;
244 // - and the delta along the other axis is beyond touch_high_threshold_.
Tom Marshall0aedc572017-08-24 12:57:27 +0000245 if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) {
246 direction = delta.x() < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
247 } else if (abs(delta.x()) < touch_low_threshold_ && abs(delta.y()) > touch_high_threshold_) {
248 direction = delta.y() < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
Tao Bao5f8dd992017-07-28 00:05:40 -0700249 } else {
Tom Marshall4b921692017-08-24 13:50:01 +0000250 for (const auto& vk : virtual_keys_) {
251 if (touch_start_.x() >= vk.min_.x() && touch_start_.x() < vk.max_.x() &&
252 touch_start_.y() >= vk.min_.y() && touch_start_.y() < vk.max_.y()) {
253 ProcessKey(vk.keycode, 1); // press key
254 ProcessKey(vk.keycode, 0); // and release it
255 return;
256 }
257 }
Tom Marshall0aedc572017-08-24 12:57:27 +0000258 LOG(DEBUG) << "Ignored " << delta.x() << " " << delta.y() << " (low: " << touch_low_threshold_
Tao Bao0bc88de2018-07-31 14:53:16 -0700259 << ", high: " << touch_high_threshold_ << ")";
Tao Bao5f8dd992017-07-28 00:05:40 -0700260 return;
261 }
262
Tao Bao046aae22017-07-31 23:15:09 -0700263 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
264 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
265 ShowText(true);
266 return;
267 }
268
Tao Bao5f8dd992017-07-28 00:05:40 -0700269 LOG(DEBUG) << "Swipe direction=" << direction;
270 switch (direction) {
271 case SwipeDirection::UP:
272 ProcessKey(KEY_UP, 1); // press up key
273 ProcessKey(KEY_UP, 0); // and release it
274 break;
275
276 case SwipeDirection::DOWN:
277 ProcessKey(KEY_DOWN, 1); // press down key
278 ProcessKey(KEY_DOWN, 0); // and release it
279 break;
280
281 case SwipeDirection::LEFT:
Tom Marshall4b921692017-08-24 13:50:01 +0000282 ProcessKey(KEY_BACK, 1); // press back key
283 ProcessKey(KEY_BACK, 0); // and release it
284 break;
Tao Bao5f8dd992017-07-28 00:05:40 -0700285 case SwipeDirection::RIGHT:
286 ProcessKey(KEY_POWER, 1); // press power key
287 ProcessKey(KEY_POWER, 0); // and release it
288 break;
289 };
290}
291
Elliott Hughes985022a2015-04-13 13:04:32 -0700292int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700293 struct input_event ev;
294 if (ev_get_input(fd, epevents, &ev) == -1) {
295 return -1;
296 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700297
Tao Bao5f8dd992017-07-28 00:05:40 -0700298 // Touch inputs handling.
299 //
300 // We handle the touch inputs by tracking the position changes between initial contacting and
301 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
302 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
303 //
304 // Per the doc Multi-touch Protocol at below, there are two protocols.
305 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
306 //
307 // The main difference between the stateless type A protocol and the stateful type B slot protocol
308 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
309 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
310 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
311 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
312 //
313 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
314 // ABS_MT_TRACKING_ID being -1.
315 //
316 // Touch input events will only be available if touch_screen_allowed_ is set.
317
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700318 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700319 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
320 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
321 // contact.
322 if (touch_finger_down_ && !touch_swiping_) {
Tom Marshall0aedc572017-08-24 12:57:27 +0000323 touch_start_ = touch_pos_;
Tao Bao5f8dd992017-07-28 00:05:40 -0700324 touch_swiping_ = true;
325 } else if (!touch_finger_down_ && touch_swiping_) {
326 touch_swiping_ = false;
Tom Marshall0aedc572017-08-24 12:57:27 +0000327 OnTouchEvent();
Tao Bao5f8dd992017-07-28 00:05:40 -0700328 }
329 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700330 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700331 }
332
333 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700334 if (ev.code == REL_Y) {
335 // accumulate the up or down motion reported by
336 // the trackball. When it exceeds a threshold
337 // (positive or negative), fake an up/down
338 // key event.
339 rel_sum += ev.value;
340 if (rel_sum > 3) {
341 ProcessKey(KEY_DOWN, 1); // press down key
342 ProcessKey(KEY_DOWN, 0); // and release it
343 rel_sum = 0;
344 } else if (rel_sum < -3) {
345 ProcessKey(KEY_UP, 1); // press up key
346 ProcessKey(KEY_UP, 0); // and release it
347 rel_sum = 0;
348 }
349 }
350 } else {
351 rel_sum = 0;
352 }
353
Tao Bao5f8dd992017-07-28 00:05:40 -0700354 if (touch_screen_allowed_ && ev.type == EV_ABS) {
355 if (ev.code == ABS_MT_SLOT) {
356 touch_slot_ = ev.value;
357 }
358 // Ignore other fingers.
359 if (touch_slot_ > 0) return 0;
360
361 switch (ev.code) {
362 case ABS_MT_POSITION_X:
Tom Marshall0aedc572017-08-24 12:57:27 +0000363 touch_pos_.x(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700364 touch_finger_down_ = true;
365 break;
366
367 case ABS_MT_POSITION_Y:
Tom Marshall0aedc572017-08-24 12:57:27 +0000368 touch_pos_.y(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700369 touch_finger_down_ = true;
370 break;
371
372 case ABS_MT_TRACKING_ID:
373 // Protocol B: -1 marks lifting the contact.
374 if (ev.value < 0) touch_finger_down_ = false;
375 break;
376 }
377 return 0;
378 }
379
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700380 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700381 if (touch_screen_allowed_) {
382 if (ev.code == BTN_TOUCH) {
383 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
384 // lifting the contact.
385 touch_finger_down_ = (ev.value == 1);
386 }
387
388 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
389 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
390 // KEY_POWER and KEY_UP as KEY_DOWN).
391 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
392 return 0;
393 }
394 }
395
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700396 ProcessKey(ev.code, ev.value);
397 }
398
fredchiouf2f5aa22022-02-11 16:39:53 +0800399 // For Lid switch handle
400 if (ev.type == EV_SW) {
401 SetSwCallback(ev.code, ev.value);
402 }
403
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700404 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700405}
406
Tao Bao26ea9592018-05-09 16:32:02 -0700407// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
408// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
409// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
410// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700411//
Tao Bao26ea9592018-05-09 16:32:02 -0700412// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
413// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700414//
415// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700416void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700417 bool register_key = false;
418 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800419
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700420 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700421 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700422 key_pressed[key_code] = updown;
423 if (updown) {
424 ++key_down_count;
425 key_last_down = key_code;
426 key_long_press = false;
427 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
428 time_key_thread.detach();
429 } else {
430 if (key_last_down == key_code) {
431 long_press = key_long_press;
432 register_key = true;
433 }
434 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700435 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700436 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700437
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700438 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700439 if (register_key) {
440 switch (CheckKey(key_code, long_press)) {
441 case RecoveryUI::IGNORE:
442 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800443
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700444 case RecoveryUI::TOGGLE:
445 ShowText(!IsTextVisible());
446 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700447
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700448 case RecoveryUI::REBOOT:
449 if (reboot_enabled) {
Mark Salyzyn488cc052019-05-20 10:36:16 -0700450 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700451 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700452 break;
453
454 case RecoveryUI::ENQUEUE:
455 EnqueueKey(key_code);
456 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700457 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700458 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700459}
460
Tao Bao26ea9592018-05-09 16:32:02 -0700461void RecoveryUI::TimeKey(int key_code, int count) {
462 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700463 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700464 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700465 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700466 if (key_last_down == key_code && key_down_count == count) {
467 long_press = key_long_press = true;
468 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700469 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700470 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700471}
472
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800473void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700474 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700475 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
476 if (key_queue_len < queue_max) {
477 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700478 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800480}
481
Jerry Zhangb76af932018-05-22 12:08:35 -0700482void RecoveryUI::SetScreensaverState(ScreensaverState state) {
483 switch (state) {
484 case ScreensaverState::NORMAL:
485 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
486 brightness_file_)) {
487 screensaver_state_ = ScreensaverState::NORMAL;
488 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
489 << "%)";
490 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800491 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700492 }
493 break;
494 case ScreensaverState::DIMMED:
495 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
496 brightness_file_)) {
497 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
498 << "%)";
499 screensaver_state_ = ScreensaverState::DIMMED;
500 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800501 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700502 }
503 break;
504 case ScreensaverState::OFF:
505 if (android::base::WriteStringToFile("0", brightness_file_)) {
506 LOG(INFO) << "Brightness: 0 (off)";
507 screensaver_state_ = ScreensaverState::OFF;
508 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800509 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700510 }
511 break;
512 default:
513 LOG(ERROR) << "Invalid screensaver state";
514 }
515}
516
Elliott Hughesec283402015-04-10 10:01:53 -0700517int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700518 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700519
Jerry Zhangb76af932018-05-22 12:08:35 -0700520 // Check for a saved key queue interruption.
521 if (key_interrupted_) {
522 SetScreensaverState(ScreensaverState::NORMAL);
523 return static_cast<int>(KeyError::INTERRUPTED);
524 }
525
Tao Bao53be3322018-08-16 11:48:50 -0700526 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800527 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700528 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
529 return this->key_queue_len != 0 || key_interrupted_;
530 });
531 if (key_interrupted_) {
532 SetScreensaverState(ScreensaverState::NORMAL);
533 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700534 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800535 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700536 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700537 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800538 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700539 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800540 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700541 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800542 }
Tao Bao53be3322018-08-16 11:48:50 -0700543 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800544 // Drop the first key if it's changing from OFF to NORMAL.
545 if (screensaver_state_ == ScreensaverState::OFF) {
546 if (key_queue_len > 0) {
547 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
548 }
549 }
550
551 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700552 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800553 }
554 }
555 } while (IsUsbConnected() && key_queue_len == 0);
556
Jerry Zhangb76af932018-05-22 12:08:35 -0700557 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800558 if (key_queue_len > 0) {
559 key = key_queue[0];
560 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
561 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800562 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700563}
564
Jerry Zhangb76af932018-05-22 12:08:35 -0700565void RecoveryUI::InterruptKey() {
566 {
567 std::lock_guard<std::mutex> lg(key_queue_mutex);
568 key_interrupted_ = true;
569 }
570 key_queue_cond.notify_one();
571}
572
Elliott Hughes985022a2015-04-13 13:04:32 -0700573bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700574 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
575 if (fd < 0) {
576 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
577 return 0;
578 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700579
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700580 char buf;
581 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
582 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
583 if (close(fd) < 0) {
584 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
585 }
586 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700587}
588
Elliott Hughesec283402015-04-10 10:01:53 -0700589bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700590 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700591 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700592 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700593}
594
Elliott Hughes642aaa72015-04-10 12:47:46 -0700595bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700596 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700597 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700598 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700599}
600
Tianjie Xue5032212019-07-23 13:23:29 -0700601bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700602 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700603}
604
Tao Bao5f8dd992017-07-28 00:05:40 -0700605bool RecoveryUI::HasPowerKey() const {
606 return has_power_key;
607}
608
609bool RecoveryUI::HasTouchScreen() const {
610 return has_touch_screen;
611}
612
Doug Zongker32a0a472011-11-01 11:00:20 -0700613void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700614 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700615 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700616}
617
Elliott Hughes642aaa72015-04-10 12:47:46 -0700618RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700619 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700620 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700621 key_long_press = false;
622 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700623
624 // 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 -0700625 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
626 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700627 return TOGGLE;
628 }
629 } else {
630 // Otherwise long press of any button toggles to the text display,
631 // and there's no way to toggle back (but that's pretty useless anyway).
632 if (is_long_press && !IsTextVisible()) {
633 return TOGGLE;
634 }
635
636 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
637 if (is_long_press && IsTextVisible()) {
638 EnqueueKey(KEY_ENTER);
639 return IGNORE;
640 }
641 }
642
643 // Press power seven times in a row to reboot.
644 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700645 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700646
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700647 if (reboot_enabled) {
648 ++consecutive_power_keys;
649 if (consecutive_power_keys >= 7) {
650 return REBOOT;
651 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700652 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700653 } else {
654 consecutive_power_keys = 0;
655 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700656
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700657 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700658}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800659
Tianjie Xu8f397302018-08-20 13:40:47 -0700660void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700661
662void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700663 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700664 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700665}