blob: 60df90197dc1053feee195b940483a9b7a2abfdc [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>
Tom Marshall292bd232019-01-04 14:37:31 -080039#include <volume_manager/VolumeManager.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070040
Tao Bao26ea9592018-05-09 16:32:02 -070041#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070042#include "otautil/sysutil.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070043
Tao Bao26ea9592018-05-09 16:32:02 -070044using namespace std::chrono_literals;
45
Tao Bao0bc88de2018-07-31 14:53:16 -070046constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
47constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
48constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
49constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness";
50constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
kataoc35c1b02017-12-08 11:02:43 +080051 "/sys/class/backlight/panel0-backlight/max_brightness";
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -070052constexpr const char* BRIGHTNESS_FILE_PWM =
53 "/sys/class/backlight/pwm-backlight.0/brightness";
54constexpr const char* MAX_BRIGHTNESS_FILE_PWM =
55 "/sys/class/backlight/pwm-backlight.0/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070056
Tao Bao0bc88de2018-07-31 14:53:16 -070057constexpr int kDefaultTouchLowThreshold = 50;
58constexpr int kDefaultTouchHighThreshold = 90;
59
Elliott Hughesec283402015-04-10 10:01:53 -070060RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080061 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080062 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080063 brightness_file_(BRIGHTNESS_FILE),
64 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tom Marshall6faecb32017-08-23 22:45:19 +000065 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080066 fastbootd_logo_enabled_(false),
Joseph Annareddye5c3f122023-08-16 12:04:56 -040067 sideload_auto_reboot_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070068 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
69 kDefaultTouchLowThreshold)),
70 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
71 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070072 key_interrupted_(false),
Tom Marshall85f19a52020-03-10 20:17:49 +010073 event_queue_len(0),
Tao Bao736d59c2017-01-03 10:15:33 -080074 key_last_down(-1),
75 key_long_press(false),
76 key_down_count(0),
77 enable_reboot(true),
78 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080079 has_power_key(false),
80 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080081 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070082 has_touch_screen(false),
83 touch_slot_(0),
Tom Marshall85f19a52020-03-10 20:17:49 +010084 touch_finger_down_(false),
85 touch_saw_x_(false),
86 touch_saw_y_(false),
87 touch_reported_(false),
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 Marshallaf3d5002017-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 Marshallaf3d5002017-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
Tom Marshall06624c32019-08-06 16:30:02 +0200244void RecoveryUI::CalibrateTouch(int fd) {
245 struct input_absinfo info;
246 static bool calibrated = false;
247
248 if (calibrated) return;
249
250 memset(&info, 0, sizeof(info));
251 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
252 touch_min_.x(info.minimum);
253 touch_max_.x(info.maximum);
254 }
255 memset(&info, 0, sizeof(info));
256 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
257 touch_min_.y(info.minimum);
258 touch_max_.y(info.maximum);
259 }
260
261 calibrated = true;
262}
263
Tom Marshall85f19a52020-03-10 20:17:49 +0100264void RecoveryUI::OnTouchPress() {
265 touch_start_ = touch_track_ = touch_pos_;
266}
Tao Bao5f8dd992017-07-28 00:05:40 -0700267
Tom Marshall85f19a52020-03-10 20:17:49 +0100268void RecoveryUI::OnTouchTrack() {
Alessandro Astoneac2d3412022-03-21 17:14:47 +0100269 if (touch_pos_.y() <= gr_fb_height_real()) {
Tom Marshall85f19a52020-03-10 20:17:49 +0100270 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
271 int dy = touch_pos_.y() - touch_track_.y();
272 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
273 ProcessKey(key, 1); // press key
274 ProcessKey(key, 0); // and release it
275 int sgn = (dy > 0) - (dy < 0);
276 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
Tom Marshallaf3d5002017-08-24 13:50:01 +0000277 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700278 }
Tom Marshall85f19a52020-03-10 20:17:49 +0100279}
Tao Bao5f8dd992017-07-28 00:05:40 -0700280
Tom Marshall85f19a52020-03-10 20:17:49 +0100281void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700282 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
283 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
284 ShowText(true);
285 return;
286 }
287
Tom Marshall85f19a52020-03-10 20:17:49 +0100288 // Check vkeys. Only report if touch both starts and ends in the vkey.
Alessandro Astoneac2d3412022-03-21 17:14:47 +0100289 if (touch_start_.y() > gr_fb_height_real() && touch_pos_.y() > gr_fb_height_real()) {
Tom Marshall85f19a52020-03-10 20:17:49 +0100290 for (const auto& vk : virtual_keys_) {
291 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
292 ProcessKey(vk.keycode, 1); // press key
293 ProcessKey(vk.keycode, 0); // and release it
294 }
295 }
296 return;
297 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700298
Tom Marshall85f19a52020-03-10 20:17:49 +0100299 // If we tracked a vertical swipe, ignore the release
300 if (touch_track_ != touch_start_) {
301 return;
302 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700303
Tom Marshall85f19a52020-03-10 20:17:49 +0100304 // Simple touch
305 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700306}
307
Elliott Hughes985022a2015-04-13 13:04:32 -0700308int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700309 struct input_event ev;
310 if (ev_get_input(fd, epevents, &ev) == -1) {
311 return -1;
312 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700313
Tao Bao5f8dd992017-07-28 00:05:40 -0700314 // Touch inputs handling.
315 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700316 // Per the doc Multi-touch Protocol at below, there are two protocols.
317 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
318 //
319 // The main difference between the stateless type A protocol and the stateful type B slot protocol
320 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
321 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
322 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
323 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
324 //
325 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
326 // ABS_MT_TRACKING_ID being -1.
327 //
328 // Touch input events will only be available if touch_screen_allowed_ is set.
329
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700330 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700331 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Tom Marshall85f19a52020-03-10 20:17:49 +0100332 // There might be multiple SYN_REPORT events. Only report press/release once.
333 if (!touch_reported_ && touch_finger_down_) {
334 if (touch_saw_x_ && touch_saw_y_) {
335 OnTouchPress();
336 touch_reported_ = true;
337 touch_saw_x_ = touch_saw_y_ = false;
338 }
339 } else if (touch_reported_ && !touch_finger_down_) {
340 OnTouchRelease();
341 touch_reported_ = false;
342 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700343 }
344 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700345 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700346 }
347
348 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700349 if (ev.code == REL_Y) {
350 // accumulate the up or down motion reported by
351 // the trackball. When it exceeds a threshold
352 // (positive or negative), fake an up/down
353 // key event.
354 rel_sum += ev.value;
355 if (rel_sum > 3) {
356 ProcessKey(KEY_DOWN, 1); // press down key
357 ProcessKey(KEY_DOWN, 0); // and release it
358 rel_sum = 0;
359 } else if (rel_sum < -3) {
360 ProcessKey(KEY_UP, 1); // press up key
361 ProcessKey(KEY_UP, 0); // and release it
362 rel_sum = 0;
363 }
364 }
365 } else {
366 rel_sum = 0;
367 }
368
Tao Bao5f8dd992017-07-28 00:05:40 -0700369 if (touch_screen_allowed_ && ev.type == EV_ABS) {
Tom Marshall06624c32019-08-06 16:30:02 +0200370 CalibrateTouch(fd);
Tao Bao5f8dd992017-07-28 00:05:40 -0700371 if (ev.code == ABS_MT_SLOT) {
372 touch_slot_ = ev.value;
373 }
374 // Ignore other fingers.
375 if (touch_slot_ > 0) return 0;
376
377 switch (ev.code) {
378 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700379 touch_finger_down_ = true;
Tom Marshall85f19a52020-03-10 20:17:49 +0100380 touch_saw_x_ = true;
Alessandro Astoneac2d3412022-03-21 17:14:47 +0100381 touch_pos_.x(ev.value * gr_fb_width_real() / (touch_max_.x() - touch_min_.x()));
Tom Marshall85f19a52020-03-10 20:17:49 +0100382 if (touch_reported_ && touch_saw_y_) {
383 OnTouchTrack();
384 touch_saw_x_ = touch_saw_y_ = false;
385 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700386 break;
387
388 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700389 touch_finger_down_ = true;
Tom Marshall85f19a52020-03-10 20:17:49 +0100390 touch_saw_y_ = true;
Alessandro Astoneac2d3412022-03-21 17:14:47 +0100391 touch_pos_.y(ev.value * gr_fb_height_real() / (touch_max_.y() - touch_min_.y()));
Tom Marshall85f19a52020-03-10 20:17:49 +0100392 if (touch_reported_ && touch_saw_x_) {
393 OnTouchTrack();
394 touch_saw_x_ = touch_saw_y_ = false;
395 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700396 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
fredchiouf2f5aa22022-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) {
Tom Marshall292bd232019-01-04 14:37:31 -0800476 android::volmgr::VolumeManager::Instance()->unmountAll();
Mark Salyzyn488cc052019-05-20 10:36:16 -0700477 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700478 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 break;
480
481 case RecoveryUI::ENQUEUE:
482 EnqueueKey(key_code);
483 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700484 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700485 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700486}
487
Tao Bao26ea9592018-05-09 16:32:02 -0700488void RecoveryUI::TimeKey(int key_code, int count) {
489 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700490 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700491 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700492 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700493 if (key_last_down == key_code && key_down_count == count) {
494 long_press = key_long_press = true;
495 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700496 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700497 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700498}
499
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800500void RecoveryUI::EnqueueKey(int key_code) {
Tom Marshall85f19a52020-03-10 20:17:49 +0100501 std::lock_guard<std::mutex> lg(event_queue_mutex);
502 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
503 if (event_queue_len < queue_max) {
504 InputEvent event(key_code);
505 event_queue[event_queue_len++] = event;
506 event_queue_cond.notify_one();
507 }
508}
509
510void RecoveryUI::EnqueueTouch(const Point& pos) {
511 std::lock_guard<std::mutex> lg(event_queue_mutex);
512 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
513 if (event_queue_len < queue_max) {
514 InputEvent event(pos);
515 event_queue[event_queue_len++] = event;
516 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700517 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800518}
519
Jerry Zhangb76af932018-05-22 12:08:35 -0700520void RecoveryUI::SetScreensaverState(ScreensaverState state) {
521 switch (state) {
522 case ScreensaverState::NORMAL:
523 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
524 brightness_file_)) {
525 screensaver_state_ = ScreensaverState::NORMAL;
526 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
527 << "%)";
528 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800529 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700530 }
531 break;
532 case ScreensaverState::DIMMED:
533 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
534 brightness_file_)) {
535 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
536 << "%)";
537 screensaver_state_ = ScreensaverState::DIMMED;
538 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800539 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700540 }
541 break;
542 case ScreensaverState::OFF:
543 if (android::base::WriteStringToFile("0", brightness_file_)) {
544 LOG(INFO) << "Brightness: 0 (off)";
545 screensaver_state_ = ScreensaverState::OFF;
546 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800547 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700548 }
549 break;
550 default:
551 LOG(ERROR) << "Invalid screensaver state";
552 }
553}
554
Tom Marshall85f19a52020-03-10 20:17:49 +0100555RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
556 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700557
Jerry Zhangb76af932018-05-22 12:08:35 -0700558 // Check for a saved key queue interruption.
559 if (key_interrupted_) {
560 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall85f19a52020-03-10 20:17:49 +0100561 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700562 }
563
Tao Bao53be3322018-08-16 11:48:50 -0700564 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800565 do {
Tom Marshall85f19a52020-03-10 20:17:49 +0100566 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
567 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700568 });
569 if (key_interrupted_) {
570 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall85f19a52020-03-10 20:17:49 +0100571 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700572 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800573 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700574 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700575 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800576 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700577 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800578 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700579 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800580 }
Tao Bao53be3322018-08-16 11:48:50 -0700581 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800582 // Drop the first key if it's changing from OFF to NORMAL.
583 if (screensaver_state_ == ScreensaverState::OFF) {
Tom Marshall85f19a52020-03-10 20:17:49 +0100584 if (event_queue_len > 0) {
585 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800586 }
587 }
588
589 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700590 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800591 }
592 }
Tom Marshall85f19a52020-03-10 20:17:49 +0100593 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800594
Tom Marshall85f19a52020-03-10 20:17:49 +0100595 InputEvent event;
596 if (event_queue_len > 0) {
597 event = event_queue[0];
598 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800599 }
Tom Marshall85f19a52020-03-10 20:17:49 +0100600 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700601}
602
Tom Marshall8f4e0102018-12-17 15:57:44 -0800603void RecoveryUI::CancelWaitKey() {
604 EnqueueKey(KEY_AGAIN);
605}
606
Jerry Zhangb76af932018-05-22 12:08:35 -0700607void RecoveryUI::InterruptKey() {
608 {
Tom Marshall85f19a52020-03-10 20:17:49 +0100609 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700610 key_interrupted_ = true;
611 }
Tom Marshall85f19a52020-03-10 20:17:49 +0100612 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700613}
614
Elliott Hughes985022a2015-04-13 13:04:32 -0700615bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700616 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
617 if (fd < 0) {
618 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
619 return 0;
620 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700621
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700622 char buf;
623 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
624 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
625 if (close(fd) < 0) {
626 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
627 }
628 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700629}
630
Elliott Hughesec283402015-04-10 10:01:53 -0700631bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700632 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700633 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700634 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700635}
636
Elliott Hughes642aaa72015-04-10 12:47:46 -0700637bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700638 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700639 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700640 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700641}
642
Tianjie Xue5032212019-07-23 13:23:29 -0700643bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700644 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700645}
646
Tao Bao5f8dd992017-07-28 00:05:40 -0700647bool RecoveryUI::HasPowerKey() const {
648 return has_power_key;
649}
650
651bool RecoveryUI::HasTouchScreen() const {
652 return has_touch_screen;
653}
654
Doug Zongker32a0a472011-11-01 11:00:20 -0700655void RecoveryUI::FlushKeys() {
Tom Marshall85f19a52020-03-10 20:17:49 +0100656 std::lock_guard<std::mutex> lg(event_queue_mutex);
657 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700658}
659
Elliott Hughes642aaa72015-04-10 12:47:46 -0700660RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700661 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700662 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700663 key_long_press = false;
664 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700665
666 // 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 -0700667 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
668 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700669 return TOGGLE;
670 }
671 } else {
672 // Otherwise long press of any button toggles to the text display,
673 // and there's no way to toggle back (but that's pretty useless anyway).
674 if (is_long_press && !IsTextVisible()) {
675 return TOGGLE;
676 }
677
678 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
679 if (is_long_press && IsTextVisible()) {
680 EnqueueKey(KEY_ENTER);
681 return IGNORE;
682 }
683 }
684
685 // Press power seven times in a row to reboot.
686 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700687 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700688
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700689 if (reboot_enabled) {
690 ++consecutive_power_keys;
691 if (consecutive_power_keys >= 7) {
692 return REBOOT;
693 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700694 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700695 } else {
696 consecutive_power_keys = 0;
697 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700698
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700699 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700700}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800701
Tianjie Xu8f397302018-08-20 13:40:47 -0700702void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700703
704void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700705 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700706 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700707}