blob: a848f71281eab2f692d9c82586b9d4e1c0f8a232 [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;
Chuong Hoang0b6c0172023-06-30 04:38:53 +000046constexpr const char* DEFAULT_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47constexpr const char* DEFAULT_MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Tao Bao0bc88de2018-07-31 14:53:16 -070048constexpr 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";
Chuong Hoang0b6c0172023-06-30 04:38:53 +000051constexpr const char* BRIGHTNESS_FILE_PWM = "/sys/class/backlight/pwm-backlight.0/brightness";
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -070052constexpr const char* MAX_BRIGHTNESS_FILE_PWM =
53 "/sys/class/backlight/pwm-backlight.0/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070054
Tao Bao0bc88de2018-07-31 14:53:16 -070055constexpr int kDefaultTouchLowThreshold = 50;
56constexpr int kDefaultTouchHighThreshold = 90;
57
Chuong Hoang0b6c0172023-06-30 04:38:53 +000058constexpr int kDefaultNormalBrightnessPercent = 50;
59constexpr int kDefaultDimmedBrightnessPercent = 25;
60
Elliott Hughesec283402015-04-10 10:01:53 -070061RecoveryUI::RecoveryUI()
Chuong Hoang0b6c0172023-06-30 04:38:53 +000062 : brightness_normal_(android::base::GetIntProperty("ro.recovery.ui.brightness_normal_percent",
63 kDefaultNormalBrightnessPercent)),
64 brightness_dimmed_(android::base::GetIntProperty("ro.recovery.ui.brightness_dimmed_percent",
65 kDefaultDimmedBrightnessPercent)),
66 brightness_file_(
67 android::base::GetProperty("ro.recovery.ui.brightness_file", DEFAULT_BRIGHTNESS_FILE)),
68 max_brightness_file_(android::base::GetProperty("ro.recovery.ui.max_brightness_file",
69 DEFAULT_MAX_BRIGHTNESS_FILE)),
Tom Marshallc8475da2017-08-23 22:45:19 +000070 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080071 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070072 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
73 kDefaultTouchLowThreshold)),
74 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
75 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070076 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080077 key_queue_len(0),
78 key_last_down(-1),
79 key_long_press(false),
80 key_down_count(0),
81 enable_reboot(true),
82 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080083 has_power_key(false),
84 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080085 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070086 has_touch_screen(false),
87 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070088 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080089 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080090 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070091}
92
Tao Bao26ea9592018-05-09 16:32:02 -070093RecoveryUI::~RecoveryUI() {
94 ev_exit();
95 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070096 if (input_thread_.joinable()) {
97 input_thread_.join();
98 }
Tao Bao26ea9592018-05-09 16:32:02 -070099}
100
Tom Marshall7a393632017-08-24 13:50:01 +0000101void RecoveryUI::OnTouchDeviceDetected(int fd) {
102 char name[256];
103 char path[PATH_MAX];
104 char buf[4096];
105
106 memset(name, 0, sizeof(name));
107 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
108 return;
109 }
110 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
111 int vkfd = open(path, O_RDONLY);
112 if (vkfd < 0) {
113 LOG(INFO) << "vkeys: could not open " << path;
114 return;
115 }
116 ssize_t len = read(vkfd, buf, sizeof(buf));
117 close(vkfd);
118 if (len <= 0) {
119 LOG(ERROR) << "vkeys: could not read " << path;
120 return;
121 }
122 buf[len] = '\0';
123
124 char* p = buf;
125 char* endp;
126 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
127 int val[6];
128 int f;
129 for (f = 0; *p && f < 6; ++f) {
130 val[f] = strtol(p, &endp, 0);
131 if (p == endp) break;
132 p = endp + 1;
133 }
134 if (f != 6 || val[0] != 0x01) break;
135 vkey_t vk;
136 vk.keycode = val[1];
137 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
138 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
139 virtual_keys_.push_back(vk);
140 }
141}
142
Elliott Hughes642aaa72015-04-10 12:47:46 -0700143void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700144 if (key_code == KEY_POWER) {
145 has_power_key = true;
146 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
147 has_down_key = true;
148 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
149 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700150 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
151 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700152 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700153}
154
Tao Bao6278bdf2017-01-16 17:38:18 -0800155bool RecoveryUI::InitScreensaver() {
156 // Disabled.
157 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
158 return false;
159 }
kataoc35c1b02017-12-08 11:02:43 +0800160 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700161 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
162 brightness_file_ = BRIGHTNESS_FILE_SDM;
163 } else {
164 brightness_file_ = BRIGHTNESS_FILE_PWM;
165 }
kataoc35c1b02017-12-08 11:02:43 +0800166 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700167
kataoc35c1b02017-12-08 11:02:43 +0800168 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700169 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
170 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
171 } else {
172 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
173 }
kataoc35c1b02017-12-08 11:02:43 +0800174 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800175 // Set the initial brightness level based on the max brightness. Note that reading the initial
176 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
177 // we don't have a good way to query the default value.
178 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800179 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800180 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800181 return false;
182 }
183
184 unsigned int max_value;
185 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
186 LOG(WARNING) << "Failed to parse max brightness: " << content;
187 return false;
188 }
189
190 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
191 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
192 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800193 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800194 PLOG(WARNING) << "Failed to set brightness";
195 return false;
196 }
197
198 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
199 screensaver_state_ = ScreensaverState::NORMAL;
200 return true;
201}
202
Tao Baoefb49ad2017-01-31 23:03:10 -0800203bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700204 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
205 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700206
Tao Bao736d59c2017-01-03 10:15:33 -0800207 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
208
Tao Bao5f8dd992017-07-28 00:05:40 -0700209 if (touch_screen_allowed_) {
Tom Marshall7a393632017-08-24 13:50:01 +0000210 ev_iterate_touch_inputs(
211 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
212 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700213
214 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
215 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
216 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
217 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
218 // mode will be turned on automatically on debuggable builds, even without a swipe.
219 std::string cmdline;
220 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
221 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
222 } else {
223 // Non-fatal, and won't affect Init() result.
224 PLOG(WARNING) << "Failed to read /proc/cmdline";
225 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700226 }
227
Tao Bao6278bdf2017-01-16 17:38:18 -0800228 if (!InitScreensaver()) {
229 LOG(INFO) << "Screensaver disabled";
230 }
231
Tao Bao26ea9592018-05-09 16:32:02 -0700232 // Create a separate thread that handles input events.
233 input_thread_ = std::thread([this]() {
234 while (!this->input_thread_stopped_) {
235 if (!ev_wait(500)) {
236 ev_dispatch();
237 }
238 }
239 });
240
Tao Bao736d59c2017-01-03 10:15:33 -0800241 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700242}
243
Ludvig Hansson510589f2022-11-07 12:39:20 +0100244enum SwipeDirection { UP, DOWN, RIGHT, LEFT };
245
246static SwipeDirection FlipSwipeDirection(SwipeDirection direction) {
247 switch (direction) {
248 case UP:
249 return SwipeDirection::DOWN;
250 case DOWN:
251 return SwipeDirection::UP;
252 case RIGHT:
253 return SwipeDirection::LEFT;
254 case LEFT:
255 return SwipeDirection::RIGHT;
256 }
257}
258
Tom Marshall893a3092017-08-24 12:57:27 +0000259void RecoveryUI::OnTouchEvent() {
260 Point delta = touch_pos_ - touch_start_;
Ludvig Hansson510589f2022-11-07 12:39:20 +0100261 SwipeDirection direction;
Tao Bao5f8dd992017-07-28 00:05:40 -0700262
263 // We only consider a valid swipe if:
Tao Bao0bc88de2018-07-31 14:53:16 -0700264 // - the delta along one axis is below touch_low_threshold_;
265 // - and the delta along the other axis is beyond touch_high_threshold_.
Tom Marshall893a3092017-08-24 12:57:27 +0000266 if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) {
267 direction = delta.x() < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
268 } else if (abs(delta.x()) < touch_low_threshold_ && abs(delta.y()) > touch_high_threshold_) {
269 direction = delta.y() < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
Tao Bao5f8dd992017-07-28 00:05:40 -0700270 } else {
Tom Marshall7a393632017-08-24 13:50:01 +0000271 for (const auto& vk : virtual_keys_) {
272 if (touch_start_.x() >= vk.min_.x() && touch_start_.x() < vk.max_.x() &&
273 touch_start_.y() >= vk.min_.y() && touch_start_.y() < vk.max_.y()) {
274 ProcessKey(vk.keycode, 1); // press key
275 ProcessKey(vk.keycode, 0); // and release it
276 return;
277 }
278 }
Tom Marshall893a3092017-08-24 12:57:27 +0000279 LOG(DEBUG) << "Ignored " << delta.x() << " " << delta.y() << " (low: " << touch_low_threshold_
Tao Bao0bc88de2018-07-31 14:53:16 -0700280 << ", high: " << touch_high_threshold_ << ")";
Tao Bao5f8dd992017-07-28 00:05:40 -0700281 return;
282 }
283
Tao Bao046aae22017-07-31 23:15:09 -0700284 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
285 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
286 ShowText(true);
287 return;
288 }
289
Ludvig Hansson510589f2022-11-07 12:39:20 +0100290 // Flip swipe direction if screen is rotated upside down
291 if (gr_get_rotation() == GRRotation::DOWN) {
292 direction = FlipSwipeDirection(direction);
293 }
294
Tao Bao5f8dd992017-07-28 00:05:40 -0700295 LOG(DEBUG) << "Swipe direction=" << direction;
296 switch (direction) {
297 case SwipeDirection::UP:
298 ProcessKey(KEY_UP, 1); // press up key
299 ProcessKey(KEY_UP, 0); // and release it
300 break;
301
302 case SwipeDirection::DOWN:
303 ProcessKey(KEY_DOWN, 1); // press down key
304 ProcessKey(KEY_DOWN, 0); // and release it
305 break;
306
307 case SwipeDirection::LEFT:
Tom Marshall7a393632017-08-24 13:50:01 +0000308 ProcessKey(KEY_BACK, 1); // press back key
309 ProcessKey(KEY_BACK, 0); // and release it
310 break;
Tao Bao5f8dd992017-07-28 00:05:40 -0700311 case SwipeDirection::RIGHT:
312 ProcessKey(KEY_POWER, 1); // press power key
313 ProcessKey(KEY_POWER, 0); // and release it
314 break;
315 };
316}
317
Elliott Hughes985022a2015-04-13 13:04:32 -0700318int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700319 struct input_event ev;
320 if (ev_get_input(fd, epevents, &ev) == -1) {
321 return -1;
322 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700323
Tao Bao5f8dd992017-07-28 00:05:40 -0700324 // Touch inputs handling.
325 //
326 // We handle the touch inputs by tracking the position changes between initial contacting and
327 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
328 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
329 //
330 // Per the doc Multi-touch Protocol at below, there are two protocols.
331 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
332 //
333 // The main difference between the stateless type A protocol and the stateful type B slot protocol
334 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
335 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
336 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
337 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
338 //
339 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
340 // ABS_MT_TRACKING_ID being -1.
341 //
342 // Touch input events will only be available if touch_screen_allowed_ is set.
343
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700345 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
346 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
347 // contact.
348 if (touch_finger_down_ && !touch_swiping_) {
Tom Marshall893a3092017-08-24 12:57:27 +0000349 touch_start_ = touch_pos_;
Tao Bao5f8dd992017-07-28 00:05:40 -0700350 touch_swiping_ = true;
351 } else if (!touch_finger_down_ && touch_swiping_) {
352 touch_swiping_ = false;
Tom Marshall893a3092017-08-24 12:57:27 +0000353 OnTouchEvent();
Tao Bao5f8dd992017-07-28 00:05:40 -0700354 }
355 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700356 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700357 }
358
359 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700360 if (ev.code == REL_Y) {
361 // accumulate the up or down motion reported by
362 // the trackball. When it exceeds a threshold
363 // (positive or negative), fake an up/down
364 // key event.
365 rel_sum += ev.value;
366 if (rel_sum > 3) {
367 ProcessKey(KEY_DOWN, 1); // press down key
368 ProcessKey(KEY_DOWN, 0); // and release it
369 rel_sum = 0;
370 } else if (rel_sum < -3) {
371 ProcessKey(KEY_UP, 1); // press up key
372 ProcessKey(KEY_UP, 0); // and release it
373 rel_sum = 0;
374 }
375 }
376 } else {
377 rel_sum = 0;
378 }
379
Tao Bao5f8dd992017-07-28 00:05:40 -0700380 if (touch_screen_allowed_ && ev.type == EV_ABS) {
381 if (ev.code == ABS_MT_SLOT) {
382 touch_slot_ = ev.value;
383 }
384 // Ignore other fingers.
385 if (touch_slot_ > 0) return 0;
386
387 switch (ev.code) {
388 case ABS_MT_POSITION_X:
Tom Marshall893a3092017-08-24 12:57:27 +0000389 touch_pos_.x(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700390 touch_finger_down_ = true;
391 break;
392
393 case ABS_MT_POSITION_Y:
Tom Marshall893a3092017-08-24 12:57:27 +0000394 touch_pos_.y(ev.value);
Tao Bao5f8dd992017-07-28 00:05:40 -0700395 touch_finger_down_ = true;
396 break;
397
398 case ABS_MT_TRACKING_ID:
399 // Protocol B: -1 marks lifting the contact.
400 if (ev.value < 0) touch_finger_down_ = false;
401 break;
402 }
403 return 0;
404 }
405
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700406 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700407 if (touch_screen_allowed_) {
408 if (ev.code == BTN_TOUCH) {
409 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
410 // lifting the contact.
411 touch_finger_down_ = (ev.value == 1);
412 }
413
414 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
415 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
416 // KEY_POWER and KEY_UP as KEY_DOWN).
417 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
418 return 0;
419 }
420 }
421
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700422 ProcessKey(ev.code, ev.value);
423 }
424
fredchioub44702a2022-02-11 16:39:53 +0800425 // For Lid switch handle
426 if (ev.type == EV_SW) {
427 SetSwCallback(ev.code, ev.value);
428 }
429
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700430 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700431}
432
Tao Bao26ea9592018-05-09 16:32:02 -0700433// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
434// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
435// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
436// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700437//
Tao Bao26ea9592018-05-09 16:32:02 -0700438// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
439// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700440//
441// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700442void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700443 bool register_key = false;
444 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800445
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700446 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700447 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700448 key_pressed[key_code] = updown;
449 if (updown) {
450 ++key_down_count;
451 key_last_down = key_code;
452 key_long_press = false;
453 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
454 time_key_thread.detach();
455 } else {
456 if (key_last_down == key_code) {
457 long_press = key_long_press;
458 register_key = true;
459 }
460 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700461 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700462 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700463
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700464 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700465 if (register_key) {
466 switch (CheckKey(key_code, long_press)) {
467 case RecoveryUI::IGNORE:
468 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800469
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700470 case RecoveryUI::TOGGLE:
471 ShowText(!IsTextVisible());
472 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700473
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700474 case RecoveryUI::REBOOT:
475 if (reboot_enabled) {
Mark Salyzyn488cc052019-05-20 10:36:16 -0700476 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700477 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700478 break;
479
480 case RecoveryUI::ENQUEUE:
481 EnqueueKey(key_code);
482 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700483 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700484 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700485}
486
Tao Bao26ea9592018-05-09 16:32:02 -0700487void RecoveryUI::TimeKey(int key_code, int count) {
488 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700489 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700490 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700491 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700492 if (key_last_down == key_code && key_down_count == count) {
493 long_press = key_long_press = true;
494 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700495 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700496 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700497}
498
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800499void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700500 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700501 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
502 if (key_queue_len < queue_max) {
503 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700504 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700505 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800506}
507
Jerry Zhangb76af932018-05-22 12:08:35 -0700508void RecoveryUI::SetScreensaverState(ScreensaverState state) {
509 switch (state) {
510 case ScreensaverState::NORMAL:
511 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
512 brightness_file_)) {
513 screensaver_state_ = ScreensaverState::NORMAL;
514 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
515 << "%)";
516 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800517 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700518 }
519 break;
520 case ScreensaverState::DIMMED:
521 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
522 brightness_file_)) {
523 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
524 << "%)";
525 screensaver_state_ = ScreensaverState::DIMMED;
526 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800527 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700528 }
529 break;
530 case ScreensaverState::OFF:
531 if (android::base::WriteStringToFile("0", brightness_file_)) {
532 LOG(INFO) << "Brightness: 0 (off)";
533 screensaver_state_ = ScreensaverState::OFF;
534 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800535 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700536 }
537 break;
538 default:
539 LOG(ERROR) << "Invalid screensaver state";
540 }
541}
542
Elliott Hughesec283402015-04-10 10:01:53 -0700543int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700544 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700545
Jerry Zhangb76af932018-05-22 12:08:35 -0700546 // Check for a saved key queue interruption.
547 if (key_interrupted_) {
548 SetScreensaverState(ScreensaverState::NORMAL);
549 return static_cast<int>(KeyError::INTERRUPTED);
550 }
551
Tao Bao53be3322018-08-16 11:48:50 -0700552 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800553 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700554 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
555 return this->key_queue_len != 0 || key_interrupted_;
556 });
557 if (key_interrupted_) {
558 SetScreensaverState(ScreensaverState::NORMAL);
559 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700560 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800561 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700562 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700563 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800564 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700565 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800566 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700567 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800568 }
Tao Bao53be3322018-08-16 11:48:50 -0700569 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800570 // Drop the first key if it's changing from OFF to NORMAL.
571 if (screensaver_state_ == ScreensaverState::OFF) {
572 if (key_queue_len > 0) {
573 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
574 }
575 }
576
577 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700578 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800579 }
580 }
581 } while (IsUsbConnected() && key_queue_len == 0);
582
Jerry Zhangb76af932018-05-22 12:08:35 -0700583 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800584 if (key_queue_len > 0) {
585 key = key_queue[0];
586 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
587 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800588 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700589}
590
Jerry Zhangb76af932018-05-22 12:08:35 -0700591void RecoveryUI::InterruptKey() {
592 {
593 std::lock_guard<std::mutex> lg(key_queue_mutex);
594 key_interrupted_ = true;
595 }
596 key_queue_cond.notify_one();
597}
598
Elliott Hughes985022a2015-04-13 13:04:32 -0700599bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700600 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
601 if (fd < 0) {
602 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
603 return 0;
604 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700605
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700606 char buf;
607 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
608 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
609 if (close(fd) < 0) {
610 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
611 }
612 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700613}
614
Elliott Hughesec283402015-04-10 10:01:53 -0700615bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700616 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700617 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700618 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700619}
620
Elliott Hughes642aaa72015-04-10 12:47:46 -0700621bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700622 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700623 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700624 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700625}
626
Tianjie Xue5032212019-07-23 13:23:29 -0700627bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700628 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700629}
630
Tao Bao5f8dd992017-07-28 00:05:40 -0700631bool RecoveryUI::HasPowerKey() const {
632 return has_power_key;
633}
634
635bool RecoveryUI::HasTouchScreen() const {
636 return has_touch_screen;
637}
638
Doug Zongker32a0a472011-11-01 11:00:20 -0700639void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700640 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700641 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700642}
643
Elliott Hughes642aaa72015-04-10 12:47:46 -0700644RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700645 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700646 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700647 key_long_press = false;
648 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700649
650 // 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 -0700651 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
652 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700653 return TOGGLE;
654 }
655 } else {
656 // Otherwise long press of any button toggles to the text display,
657 // and there's no way to toggle back (but that's pretty useless anyway).
658 if (is_long_press && !IsTextVisible()) {
659 return TOGGLE;
660 }
661
662 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
663 if (is_long_press && IsTextVisible()) {
664 EnqueueKey(KEY_ENTER);
665 return IGNORE;
666 }
667 }
668
669 // Press power seven times in a row to reboot.
670 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700671 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700672
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700673 if (reboot_enabled) {
674 ++consecutive_power_keys;
675 if (consecutive_power_keys >= 7) {
676 return REBOOT;
677 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700678 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700679 } else {
680 consecutive_power_keys = 0;
681 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700682
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700683 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700684}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800685
Tianjie Xu8f397302018-08-20 13:40:47 -0700686void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700687
688void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700689 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700690 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700691}