blob: 5d407fcfdbe5371aa309f6825a3eebc6cf77fbfd [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),
Alessandro Astonef8b86652020-10-04 18:11:40 +020077 event_queue_len(0),
Tao Bao736d59c2017-01-03 10:15:33 -080078 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),
Alessandro Astonef8b86652020-10-04 18:11:40 +020088 touch_finger_down_(false),
89 touch_saw_x_(false),
90 touch_saw_y_(false),
91 touch_reported_(false),
Tao Bao046aae22017-07-31 23:15:09 -070092 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080093 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080094 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070095}
96
Tao Bao26ea9592018-05-09 16:32:02 -070097RecoveryUI::~RecoveryUI() {
98 ev_exit();
99 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700100 if (input_thread_.joinable()) {
101 input_thread_.join();
102 }
Tao Bao26ea9592018-05-09 16:32:02 -0700103}
104
Tom Marshall7a393632017-08-24 13:50:01 +0000105void RecoveryUI::OnTouchDeviceDetected(int fd) {
106 char name[256];
107 char path[PATH_MAX];
108 char buf[4096];
109
110 memset(name, 0, sizeof(name));
111 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
112 return;
113 }
114 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
115 int vkfd = open(path, O_RDONLY);
116 if (vkfd < 0) {
117 LOG(INFO) << "vkeys: could not open " << path;
118 return;
119 }
120 ssize_t len = read(vkfd, buf, sizeof(buf));
121 close(vkfd);
122 if (len <= 0) {
123 LOG(ERROR) << "vkeys: could not read " << path;
124 return;
125 }
126 buf[len] = '\0';
127
128 char* p = buf;
129 char* endp;
130 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
131 int val[6];
132 int f;
133 for (f = 0; *p && f < 6; ++f) {
134 val[f] = strtol(p, &endp, 0);
135 if (p == endp) break;
136 p = endp + 1;
137 }
138 if (f != 6 || val[0] != 0x01) break;
139 vkey_t vk;
140 vk.keycode = val[1];
141 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
142 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
143 virtual_keys_.push_back(vk);
144 }
145}
146
Elliott Hughes642aaa72015-04-10 12:47:46 -0700147void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700148 if (key_code == KEY_POWER) {
149 has_power_key = true;
150 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
151 has_down_key = true;
152 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
153 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700154 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
155 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700156 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700157}
158
Tao Bao6278bdf2017-01-16 17:38:18 -0800159bool RecoveryUI::InitScreensaver() {
160 // Disabled.
161 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
162 return false;
163 }
kataoc35c1b02017-12-08 11:02:43 +0800164 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700165 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
166 brightness_file_ = BRIGHTNESS_FILE_SDM;
167 } else {
168 brightness_file_ = BRIGHTNESS_FILE_PWM;
169 }
kataoc35c1b02017-12-08 11:02:43 +0800170 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700171
kataoc35c1b02017-12-08 11:02:43 +0800172 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700173 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
174 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
175 } else {
176 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
177 }
kataoc35c1b02017-12-08 11:02:43 +0800178 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800179 // Set the initial brightness level based on the max brightness. Note that reading the initial
180 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
181 // we don't have a good way to query the default value.
182 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800183 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800184 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800185 return false;
186 }
187
188 unsigned int max_value;
189 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
190 LOG(WARNING) << "Failed to parse max brightness: " << content;
191 return false;
192 }
193
194 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
195 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
196 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800197 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800198 PLOG(WARNING) << "Failed to set brightness";
199 return false;
200 }
201
202 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
203 screensaver_state_ = ScreensaverState::NORMAL;
204 return true;
205}
206
Tao Baoefb49ad2017-01-31 23:03:10 -0800207bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700208 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
209 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700210
Tao Bao736d59c2017-01-03 10:15:33 -0800211 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
212
Tao Bao5f8dd992017-07-28 00:05:40 -0700213 if (touch_screen_allowed_) {
Tom Marshall7a393632017-08-24 13:50:01 +0000214 ev_iterate_touch_inputs(
215 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
216 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700217
218 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
219 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
220 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
221 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
222 // mode will be turned on automatically on debuggable builds, even without a swipe.
223 std::string cmdline;
224 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
225 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
226 } else {
227 // Non-fatal, and won't affect Init() result.
228 PLOG(WARNING) << "Failed to read /proc/cmdline";
229 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700230 }
231
Tao Bao6278bdf2017-01-16 17:38:18 -0800232 if (!InitScreensaver()) {
233 LOG(INFO) << "Screensaver disabled";
234 }
235
Tao Bao26ea9592018-05-09 16:32:02 -0700236 // Create a separate thread that handles input events.
237 input_thread_ = std::thread([this]() {
238 while (!this->input_thread_stopped_) {
239 if (!ev_wait(500)) {
240 ev_dispatch();
241 }
242 }
243 });
244
Tao Bao736d59c2017-01-03 10:15:33 -0800245 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700246}
247
Tom Marshalld3892ff2019-08-06 16:30:02 +0200248void RecoveryUI::CalibrateTouch(int fd) {
249 struct input_absinfo info;
250 static bool calibrated = false;
251
252 if (calibrated) return;
253
254 memset(&info, 0, sizeof(info));
255 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
256 touch_min_.x(info.minimum);
257 touch_max_.x(info.maximum);
258 }
259 memset(&info, 0, sizeof(info));
260 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
261 touch_min_.y(info.minimum);
262 touch_max_.y(info.maximum);
263 }
264
265 calibrated = true;
266}
267
Alessandro Astonef8b86652020-10-04 18:11:40 +0200268void RecoveryUI::OnTouchPress() {
269 touch_start_ = touch_track_ = touch_pos_;
270}
Ludvig Hansson510589f2022-11-07 12:39:20 +0100271
Alessandro Astonef8b86652020-10-04 18:11:40 +0200272void RecoveryUI::OnTouchTrack() {
273 if (touch_pos_.y() <= gr_fb_height()) {
274 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
275 int dy = touch_pos_.y() - touch_track_.y();
276 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
277 ProcessKey(key, 1); // press key
278 ProcessKey(key, 0); // and release it
279 int sgn = (dy > 0) - (dy < 0);
280 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
281 }
Ludvig Hansson510589f2022-11-07 12:39:20 +0100282 }
283}
284
Alessandro Astonef8b86652020-10-04 18:11:40 +0200285void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700286 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
287 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
288 ShowText(true);
289 return;
290 }
291
Alessandro Astonef8b86652020-10-04 18:11:40 +0200292 // Check vkeys. Only report if touch both starts and ends in the vkey.
293 if (touch_start_.y() > gr_fb_height() && touch_pos_.y() > gr_fb_height()) {
294 for (const auto& vk : virtual_keys_) {
295 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
296 ProcessKey(vk.keycode, 1); // press key
297 ProcessKey(vk.keycode, 0); // and release it
298 }
299 }
300 return;
Ludvig Hansson510589f2022-11-07 12:39:20 +0100301 }
302
Alessandro Astonef8b86652020-10-04 18:11:40 +0200303 // If we tracked a vertical swipe, ignore the release
304 if (touch_track_ != touch_start_) {
305 return;
306 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700307
Alessandro Astonef8b86652020-10-04 18:11:40 +0200308 // Simple touch
309 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700310}
311
Elliott Hughes985022a2015-04-13 13:04:32 -0700312int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700313 struct input_event ev;
314 if (ev_get_input(fd, epevents, &ev) == -1) {
315 return -1;
316 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700317
Tao Bao5f8dd992017-07-28 00:05:40 -0700318 // Touch inputs handling.
319 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700320 // Per the doc Multi-touch Protocol at below, there are two protocols.
321 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
322 //
323 // The main difference between the stateless type A protocol and the stateful type B slot protocol
324 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
325 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
326 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
327 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
328 //
329 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
330 // ABS_MT_TRACKING_ID being -1.
331 //
332 // Touch input events will only be available if touch_screen_allowed_ is set.
333
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700334 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700335 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200336 // There might be multiple SYN_REPORT events. Only report press/release once.
337 if (!touch_reported_ && touch_finger_down_) {
338 if (touch_saw_x_ && touch_saw_y_) {
339 OnTouchPress();
340 touch_reported_ = true;
341 touch_saw_x_ = touch_saw_y_ = false;
342 }
343 } else if (touch_reported_ && !touch_finger_down_) {
344 OnTouchRelease();
345 touch_reported_ = false;
346 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700347 }
348 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700349 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700350 }
351
352 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700353 if (ev.code == REL_Y) {
354 // accumulate the up or down motion reported by
355 // the trackball. When it exceeds a threshold
356 // (positive or negative), fake an up/down
357 // key event.
358 rel_sum += ev.value;
359 if (rel_sum > 3) {
360 ProcessKey(KEY_DOWN, 1); // press down key
361 ProcessKey(KEY_DOWN, 0); // and release it
362 rel_sum = 0;
363 } else if (rel_sum < -3) {
364 ProcessKey(KEY_UP, 1); // press up key
365 ProcessKey(KEY_UP, 0); // and release it
366 rel_sum = 0;
367 }
368 }
369 } else {
370 rel_sum = 0;
371 }
372
Tao Bao5f8dd992017-07-28 00:05:40 -0700373 if (touch_screen_allowed_ && ev.type == EV_ABS) {
Tom Marshalld3892ff2019-08-06 16:30:02 +0200374 CalibrateTouch(fd);
Tao Bao5f8dd992017-07-28 00:05:40 -0700375 if (ev.code == ABS_MT_SLOT) {
376 touch_slot_ = ev.value;
377 }
378 // Ignore other fingers.
379 if (touch_slot_ > 0) return 0;
380
381 switch (ev.code) {
382 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700383 touch_finger_down_ = true;
Alessandro Astonef8b86652020-10-04 18:11:40 +0200384 touch_saw_x_ = true;
Tom Marshalld3892ff2019-08-06 16:30:02 +0200385 touch_pos_.x(ev.value * gr_fb_width() / (touch_max_.x() - touch_min_.x()));
Alessandro Astonef8b86652020-10-04 18:11:40 +0200386 if (touch_reported_ && touch_saw_y_) {
387 OnTouchTrack();
388 touch_saw_x_ = touch_saw_y_ = false;
389 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700390 break;
391
392 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700393 touch_finger_down_ = true;
Alessandro Astonef8b86652020-10-04 18:11:40 +0200394 touch_saw_y_ = true;
Tom Marshalld3892ff2019-08-06 16:30:02 +0200395 touch_pos_.y(ev.value * gr_fb_height() / (touch_max_.y() - touch_min_.y()));
Alessandro Astonef8b86652020-10-04 18:11:40 +0200396 if (touch_reported_ && touch_saw_x_) {
397 OnTouchTrack();
398 touch_saw_x_ = touch_saw_y_ = false;
399 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700400 break;
401
402 case ABS_MT_TRACKING_ID:
403 // Protocol B: -1 marks lifting the contact.
404 if (ev.value < 0) touch_finger_down_ = false;
405 break;
406 }
407 return 0;
408 }
409
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700410 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700411 if (touch_screen_allowed_) {
412 if (ev.code == BTN_TOUCH) {
413 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
414 // lifting the contact.
415 touch_finger_down_ = (ev.value == 1);
416 }
417
418 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
419 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
420 // KEY_POWER and KEY_UP as KEY_DOWN).
421 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
422 return 0;
423 }
424 }
425
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700426 ProcessKey(ev.code, ev.value);
427 }
428
fredchioub44702a2022-02-11 16:39:53 +0800429 // For Lid switch handle
430 if (ev.type == EV_SW) {
431 SetSwCallback(ev.code, ev.value);
432 }
433
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700434 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700435}
436
Tao Bao26ea9592018-05-09 16:32:02 -0700437// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
438// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
439// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
440// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700441//
Tao Bao26ea9592018-05-09 16:32:02 -0700442// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
443// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700444//
445// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700446void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700447 bool register_key = false;
448 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800449
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700450 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700451 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700452 key_pressed[key_code] = updown;
453 if (updown) {
454 ++key_down_count;
455 key_last_down = key_code;
456 key_long_press = false;
457 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
458 time_key_thread.detach();
459 } else {
460 if (key_last_down == key_code) {
461 long_press = key_long_press;
462 register_key = true;
463 }
464 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700465 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700466 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700467
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700468 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700469 if (register_key) {
470 switch (CheckKey(key_code, long_press)) {
471 case RecoveryUI::IGNORE:
472 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800473
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700474 case RecoveryUI::TOGGLE:
475 ShowText(!IsTextVisible());
476 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700477
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700478 case RecoveryUI::REBOOT:
479 if (reboot_enabled) {
Mark Salyzyn488cc052019-05-20 10:36:16 -0700480 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700481 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700482 break;
483
484 case RecoveryUI::ENQUEUE:
485 EnqueueKey(key_code);
486 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700487 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700489}
490
Tao Bao26ea9592018-05-09 16:32:02 -0700491void RecoveryUI::TimeKey(int key_code, int count) {
492 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700493 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700494 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700495 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700496 if (key_last_down == key_code && key_down_count == count) {
497 long_press = key_long_press = true;
498 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700499 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700500 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700501}
502
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800503void RecoveryUI::EnqueueKey(int key_code) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200504 std::lock_guard<std::mutex> lg(event_queue_mutex);
505 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
506 if (event_queue_len < queue_max) {
507 InputEvent event(key_code);
508 event_queue[event_queue_len++] = event;
509 event_queue_cond.notify_one();
510 }
511}
512
513void RecoveryUI::EnqueueTouch(const Point& pos) {
514 std::lock_guard<std::mutex> lg(event_queue_mutex);
515 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
516 if (event_queue_len < queue_max) {
517 InputEvent event(pos);
518 event_queue[event_queue_len++] = event;
519 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700520 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800521}
522
Jerry Zhangb76af932018-05-22 12:08:35 -0700523void RecoveryUI::SetScreensaverState(ScreensaverState state) {
524 switch (state) {
525 case ScreensaverState::NORMAL:
526 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
527 brightness_file_)) {
528 screensaver_state_ = ScreensaverState::NORMAL;
529 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
530 << "%)";
531 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800532 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700533 }
534 break;
535 case ScreensaverState::DIMMED:
536 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
537 brightness_file_)) {
538 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
539 << "%)";
540 screensaver_state_ = ScreensaverState::DIMMED;
541 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800542 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700543 }
544 break;
545 case ScreensaverState::OFF:
546 if (android::base::WriteStringToFile("0", brightness_file_)) {
547 LOG(INFO) << "Brightness: 0 (off)";
548 screensaver_state_ = ScreensaverState::OFF;
549 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800550 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700551 }
552 break;
553 default:
554 LOG(ERROR) << "Invalid screensaver state";
555 }
556}
557
Alessandro Astonef8b86652020-10-04 18:11:40 +0200558RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
559 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700560
Jerry Zhangb76af932018-05-22 12:08:35 -0700561 // Check for a saved key queue interruption.
562 if (key_interrupted_) {
563 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astonef8b86652020-10-04 18:11:40 +0200564 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700565 }
566
Tao Bao53be3322018-08-16 11:48:50 -0700567 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800568 do {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200569 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
570 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700571 });
572 if (key_interrupted_) {
573 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astonef8b86652020-10-04 18:11:40 +0200574 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700575 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800576 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700577 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700578 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800579 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700580 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800581 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700582 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800583 }
Tao Bao53be3322018-08-16 11:48:50 -0700584 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800585 // Drop the first key if it's changing from OFF to NORMAL.
586 if (screensaver_state_ == ScreensaverState::OFF) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200587 if (event_queue_len > 0) {
588 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800589 }
590 }
591
592 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700593 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800594 }
595 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200596 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800597
Alessandro Astonef8b86652020-10-04 18:11:40 +0200598 InputEvent event;
599 if (event_queue_len > 0) {
600 event = event_queue[0];
601 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800602 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200603 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700604}
605
Tom Marshall98716e92018-12-17 15:57:44 -0800606void RecoveryUI::CancelWaitKey() {
607 EnqueueKey(KEY_AGAIN);
608}
609
Jerry Zhangb76af932018-05-22 12:08:35 -0700610void RecoveryUI::InterruptKey() {
611 {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200612 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700613 key_interrupted_ = true;
614 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200615 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700616}
617
Elliott Hughes985022a2015-04-13 13:04:32 -0700618bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700619 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
620 if (fd < 0) {
621 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
622 return 0;
623 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700624
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700625 char buf;
626 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
627 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
628 if (close(fd) < 0) {
629 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
630 }
631 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700632}
633
Elliott Hughesec283402015-04-10 10:01:53 -0700634bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700635 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700636 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700637 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700638}
639
Elliott Hughes642aaa72015-04-10 12:47:46 -0700640bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700641 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700642 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700643 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700644}
645
Tianjie Xue5032212019-07-23 13:23:29 -0700646bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700647 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700648}
649
Tao Bao5f8dd992017-07-28 00:05:40 -0700650bool RecoveryUI::HasPowerKey() const {
651 return has_power_key;
652}
653
654bool RecoveryUI::HasTouchScreen() const {
655 return has_touch_screen;
656}
657
Doug Zongker32a0a472011-11-01 11:00:20 -0700658void RecoveryUI::FlushKeys() {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200659 std::lock_guard<std::mutex> lg(event_queue_mutex);
660 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700661}
662
Elliott Hughes642aaa72015-04-10 12:47:46 -0700663RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700664 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700665 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700666 key_long_press = false;
667 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700668
669 // 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 -0700670 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
671 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700672 return TOGGLE;
673 }
674 } else {
675 // Otherwise long press of any button toggles to the text display,
676 // and there's no way to toggle back (but that's pretty useless anyway).
677 if (is_long_press && !IsTextVisible()) {
678 return TOGGLE;
679 }
680
681 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
682 if (is_long_press && IsTextVisible()) {
683 EnqueueKey(KEY_ENTER);
684 return IGNORE;
685 }
686 }
687
688 // Press power seven times in a row to reboot.
689 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700690 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700691
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700692 if (reboot_enabled) {
693 ++consecutive_power_keys;
694 if (consecutive_power_keys >= 7) {
695 return REBOOT;
696 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700697 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700698 } else {
699 consecutive_power_keys = 0;
700 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700701
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700702 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700703}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800704
Tianjie Xu8f397302018-08-20 13:40:47 -0700705void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700706
707void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700708 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700709 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700710}