blob: d3323d728d91fbc005b562fccb531b959d95309e [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 Marshall2823cd72019-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;
Chuong Hoang0b6c0172023-06-30 04:38:53 +000047constexpr const char* DEFAULT_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
48constexpr const char* DEFAULT_MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Tao Bao0bc88de2018-07-31 14:53:16 -070049constexpr 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";
Chuong Hoang0b6c0172023-06-30 04:38:53 +000052constexpr const char* BRIGHTNESS_FILE_PWM = "/sys/class/backlight/pwm-backlight.0/brightness";
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -070053constexpr const char* MAX_BRIGHTNESS_FILE_PWM =
54 "/sys/class/backlight/pwm-backlight.0/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070055
Tao Bao0bc88de2018-07-31 14:53:16 -070056constexpr int kDefaultTouchLowThreshold = 50;
57constexpr int kDefaultTouchHighThreshold = 90;
58
Chuong Hoang0b6c0172023-06-30 04:38:53 +000059constexpr int kDefaultNormalBrightnessPercent = 50;
60constexpr int kDefaultDimmedBrightnessPercent = 25;
61
Elliott Hughesec283402015-04-10 10:01:53 -070062RecoveryUI::RecoveryUI()
Chuong Hoang0b6c0172023-06-30 04:38:53 +000063 : brightness_normal_(android::base::GetIntProperty("ro.recovery.ui.brightness_normal_percent",
64 kDefaultNormalBrightnessPercent)),
65 brightness_dimmed_(android::base::GetIntProperty("ro.recovery.ui.brightness_dimmed_percent",
66 kDefaultDimmedBrightnessPercent)),
67 brightness_file_(
68 android::base::GetProperty("ro.recovery.ui.brightness_file", DEFAULT_BRIGHTNESS_FILE)),
69 max_brightness_file_(android::base::GetProperty("ro.recovery.ui.max_brightness_file",
70 DEFAULT_MAX_BRIGHTNESS_FILE)),
Tom Marshallc8475da2017-08-23 22:45:19 +000071 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080072 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070073 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
74 kDefaultTouchLowThreshold)),
75 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
76 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070077 key_interrupted_(false),
Alessandro Astonef8b86652020-10-04 18:11:40 +020078 event_queue_len(0),
Tao Bao736d59c2017-01-03 10:15:33 -080079 key_last_down(-1),
80 key_long_press(false),
81 key_down_count(0),
82 enable_reboot(true),
83 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080084 has_power_key(false),
85 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080086 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070087 has_touch_screen(false),
88 touch_slot_(0),
Alessandro Astonef8b86652020-10-04 18:11:40 +020089 touch_finger_down_(false),
90 touch_saw_x_(false),
91 touch_saw_y_(false),
92 touch_reported_(false),
Tao Bao046aae22017-07-31 23:15:09 -070093 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080094 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080095 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070096}
97
Tao Bao26ea9592018-05-09 16:32:02 -070098RecoveryUI::~RecoveryUI() {
99 ev_exit();
100 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -0700101 if (input_thread_.joinable()) {
102 input_thread_.join();
103 }
Tao Bao26ea9592018-05-09 16:32:02 -0700104}
105
Tom Marshall7a393632017-08-24 13:50:01 +0000106void RecoveryUI::OnTouchDeviceDetected(int fd) {
107 char name[256];
108 char path[PATH_MAX];
109 char buf[4096];
110
111 memset(name, 0, sizeof(name));
112 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
113 return;
114 }
115 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
116 int vkfd = open(path, O_RDONLY);
117 if (vkfd < 0) {
118 LOG(INFO) << "vkeys: could not open " << path;
119 return;
120 }
121 ssize_t len = read(vkfd, buf, sizeof(buf));
122 close(vkfd);
123 if (len <= 0) {
124 LOG(ERROR) << "vkeys: could not read " << path;
125 return;
126 }
127 buf[len] = '\0';
128
129 char* p = buf;
130 char* endp;
131 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
132 int val[6];
133 int f;
134 for (f = 0; *p && f < 6; ++f) {
135 val[f] = strtol(p, &endp, 0);
136 if (p == endp) break;
137 p = endp + 1;
138 }
139 if (f != 6 || val[0] != 0x01) break;
140 vkey_t vk;
141 vk.keycode = val[1];
142 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
143 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
144 virtual_keys_.push_back(vk);
145 }
146}
147
Elliott Hughes642aaa72015-04-10 12:47:46 -0700148void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700149 if (key_code == KEY_POWER) {
150 has_power_key = true;
151 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
152 has_down_key = true;
153 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
154 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700155 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
156 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700157 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700158}
159
Tao Bao6278bdf2017-01-16 17:38:18 -0800160bool RecoveryUI::InitScreensaver() {
161 // Disabled.
162 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
163 return false;
164 }
kataoc35c1b02017-12-08 11:02:43 +0800165 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700166 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
167 brightness_file_ = BRIGHTNESS_FILE_SDM;
168 } else {
169 brightness_file_ = BRIGHTNESS_FILE_PWM;
170 }
kataoc35c1b02017-12-08 11:02:43 +0800171 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700172
kataoc35c1b02017-12-08 11:02:43 +0800173 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700174 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
175 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
176 } else {
177 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
178 }
kataoc35c1b02017-12-08 11:02:43 +0800179 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800180 // Set the initial brightness level based on the max brightness. Note that reading the initial
181 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
182 // we don't have a good way to query the default value.
183 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800184 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800185 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800186 return false;
187 }
188
189 unsigned int max_value;
190 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
191 LOG(WARNING) << "Failed to parse max brightness: " << content;
192 return false;
193 }
194
195 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
196 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
197 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800198 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800199 PLOG(WARNING) << "Failed to set brightness";
200 return false;
201 }
202
203 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
204 screensaver_state_ = ScreensaverState::NORMAL;
205 return true;
206}
207
Tao Baoefb49ad2017-01-31 23:03:10 -0800208bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700209 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
210 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700211
Tao Bao736d59c2017-01-03 10:15:33 -0800212 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
213
Tao Bao5f8dd992017-07-28 00:05:40 -0700214 if (touch_screen_allowed_) {
Tom Marshall7a393632017-08-24 13:50:01 +0000215 ev_iterate_touch_inputs(
216 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
217 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700218
219 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
220 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
221 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
222 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
223 // mode will be turned on automatically on debuggable builds, even without a swipe.
224 std::string cmdline;
225 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
226 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
227 } else {
228 // Non-fatal, and won't affect Init() result.
229 PLOG(WARNING) << "Failed to read /proc/cmdline";
230 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700231 }
232
Tao Bao6278bdf2017-01-16 17:38:18 -0800233 if (!InitScreensaver()) {
234 LOG(INFO) << "Screensaver disabled";
235 }
236
Tao Bao26ea9592018-05-09 16:32:02 -0700237 // Create a separate thread that handles input events.
238 input_thread_ = std::thread([this]() {
239 while (!this->input_thread_stopped_) {
240 if (!ev_wait(500)) {
241 ev_dispatch();
242 }
243 }
244 });
245
Tao Bao736d59c2017-01-03 10:15:33 -0800246 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700247}
248
Tom Marshalld3892ff2019-08-06 16:30:02 +0200249void RecoveryUI::CalibrateTouch(int fd) {
250 struct input_absinfo info;
251 static bool calibrated = false;
252
253 if (calibrated) return;
254
255 memset(&info, 0, sizeof(info));
256 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
257 touch_min_.x(info.minimum);
258 touch_max_.x(info.maximum);
259 }
260 memset(&info, 0, sizeof(info));
261 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
262 touch_min_.y(info.minimum);
263 touch_max_.y(info.maximum);
264 }
265
266 calibrated = true;
267}
268
Alessandro Astonef8b86652020-10-04 18:11:40 +0200269void RecoveryUI::OnTouchPress() {
270 touch_start_ = touch_track_ = touch_pos_;
271}
Ludvig Hansson510589f2022-11-07 12:39:20 +0100272
Alessandro Astonef8b86652020-10-04 18:11:40 +0200273void RecoveryUI::OnTouchTrack() {
274 if (touch_pos_.y() <= gr_fb_height()) {
275 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
276 int dy = touch_pos_.y() - touch_track_.y();
277 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
278 ProcessKey(key, 1); // press key
279 ProcessKey(key, 0); // and release it
280 int sgn = (dy > 0) - (dy < 0);
281 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
282 }
Ludvig Hansson510589f2022-11-07 12:39:20 +0100283 }
284}
285
Alessandro Astonef8b86652020-10-04 18:11:40 +0200286void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700287 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
288 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
289 ShowText(true);
290 return;
291 }
292
Alessandro Astonef8b86652020-10-04 18:11:40 +0200293 // Check vkeys. Only report if touch both starts and ends in the vkey.
294 if (touch_start_.y() > gr_fb_height() && touch_pos_.y() > gr_fb_height()) {
295 for (const auto& vk : virtual_keys_) {
296 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
297 ProcessKey(vk.keycode, 1); // press key
298 ProcessKey(vk.keycode, 0); // and release it
299 }
300 }
301 return;
Ludvig Hansson510589f2022-11-07 12:39:20 +0100302 }
303
Alessandro Astonef8b86652020-10-04 18:11:40 +0200304 // If we tracked a vertical swipe, ignore the release
305 if (touch_track_ != touch_start_) {
306 return;
307 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700308
Alessandro Astonef8b86652020-10-04 18:11:40 +0200309 // Simple touch
310 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700311}
312
Elliott Hughes985022a2015-04-13 13:04:32 -0700313int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700314 struct input_event ev;
315 if (ev_get_input(fd, epevents, &ev) == -1) {
316 return -1;
317 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700318
Tao Bao5f8dd992017-07-28 00:05:40 -0700319 // Touch inputs handling.
320 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700321 // Per the doc Multi-touch Protocol at below, there are two protocols.
322 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
323 //
324 // The main difference between the stateless type A protocol and the stateful type B slot protocol
325 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
326 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
327 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
328 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
329 //
330 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
331 // ABS_MT_TRACKING_ID being -1.
332 //
333 // Touch input events will only be available if touch_screen_allowed_ is set.
334
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700335 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700336 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200337 // There might be multiple SYN_REPORT events. Only report press/release once.
338 if (!touch_reported_ && touch_finger_down_) {
339 if (touch_saw_x_ && touch_saw_y_) {
340 OnTouchPress();
341 touch_reported_ = true;
342 touch_saw_x_ = touch_saw_y_ = false;
343 }
344 } else if (touch_reported_ && !touch_finger_down_) {
345 OnTouchRelease();
346 touch_reported_ = false;
347 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700348 }
349 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700350 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700351 }
352
353 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700354 if (ev.code == REL_Y) {
355 // accumulate the up or down motion reported by
356 // the trackball. When it exceeds a threshold
357 // (positive or negative), fake an up/down
358 // key event.
359 rel_sum += ev.value;
360 if (rel_sum > 3) {
361 ProcessKey(KEY_DOWN, 1); // press down key
362 ProcessKey(KEY_DOWN, 0); // and release it
363 rel_sum = 0;
364 } else if (rel_sum < -3) {
365 ProcessKey(KEY_UP, 1); // press up key
366 ProcessKey(KEY_UP, 0); // and release it
367 rel_sum = 0;
368 }
369 }
370 } else {
371 rel_sum = 0;
372 }
373
Tao Bao5f8dd992017-07-28 00:05:40 -0700374 if (touch_screen_allowed_ && ev.type == EV_ABS) {
Tom Marshalld3892ff2019-08-06 16:30:02 +0200375 CalibrateTouch(fd);
Tao Bao5f8dd992017-07-28 00:05:40 -0700376 if (ev.code == ABS_MT_SLOT) {
377 touch_slot_ = ev.value;
378 }
379 // Ignore other fingers.
380 if (touch_slot_ > 0) return 0;
381
382 switch (ev.code) {
383 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700384 touch_finger_down_ = true;
Alessandro Astonef8b86652020-10-04 18:11:40 +0200385 touch_saw_x_ = true;
Tom Marshalld3892ff2019-08-06 16:30:02 +0200386 touch_pos_.x(ev.value * gr_fb_width() / (touch_max_.x() - touch_min_.x()));
Alessandro Astonef8b86652020-10-04 18:11:40 +0200387 if (touch_reported_ && touch_saw_y_) {
388 OnTouchTrack();
389 touch_saw_x_ = touch_saw_y_ = false;
390 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700391 break;
392
393 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700394 touch_finger_down_ = true;
Alessandro Astonef8b86652020-10-04 18:11:40 +0200395 touch_saw_y_ = true;
Tom Marshalld3892ff2019-08-06 16:30:02 +0200396 touch_pos_.y(ev.value * gr_fb_height() / (touch_max_.y() - touch_min_.y()));
Alessandro Astonef8b86652020-10-04 18:11:40 +0200397 if (touch_reported_ && touch_saw_x_) {
398 OnTouchTrack();
399 touch_saw_x_ = touch_saw_y_ = false;
400 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700401 break;
402
403 case ABS_MT_TRACKING_ID:
404 // Protocol B: -1 marks lifting the contact.
405 if (ev.value < 0) touch_finger_down_ = false;
406 break;
407 }
408 return 0;
409 }
410
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700411 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700412 if (touch_screen_allowed_) {
413 if (ev.code == BTN_TOUCH) {
414 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
415 // lifting the contact.
416 touch_finger_down_ = (ev.value == 1);
417 }
418
419 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
420 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
421 // KEY_POWER and KEY_UP as KEY_DOWN).
422 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
423 return 0;
424 }
425 }
426
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700427 ProcessKey(ev.code, ev.value);
428 }
429
fredchioub44702a2022-02-11 16:39:53 +0800430 // For Lid switch handle
431 if (ev.type == EV_SW) {
432 SetSwCallback(ev.code, ev.value);
433 }
434
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700435 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700436}
437
Tao Bao26ea9592018-05-09 16:32:02 -0700438// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
439// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
440// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
441// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700442//
Tao Bao26ea9592018-05-09 16:32:02 -0700443// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
444// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700445//
446// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700447void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700448 bool register_key = false;
449 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800450
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700451 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700452 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700453 key_pressed[key_code] = updown;
454 if (updown) {
455 ++key_down_count;
456 key_last_down = key_code;
457 key_long_press = false;
458 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
459 time_key_thread.detach();
460 } else {
461 if (key_last_down == key_code) {
462 long_press = key_long_press;
463 register_key = true;
464 }
465 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700466 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700467 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700468
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700469 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700470 if (register_key) {
471 switch (CheckKey(key_code, long_press)) {
472 case RecoveryUI::IGNORE:
473 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800474
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700475 case RecoveryUI::TOGGLE:
476 ShowText(!IsTextVisible());
477 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700478
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 case RecoveryUI::REBOOT:
480 if (reboot_enabled) {
Tom Marshall2823cd72019-01-04 14:37:31 -0800481 android::volmgr::VolumeManager::Instance()->unmountAll();
Mark Salyzyn488cc052019-05-20 10:36:16 -0700482 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700483 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700484 break;
485
486 case RecoveryUI::ENQUEUE:
487 EnqueueKey(key_code);
488 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700489 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700490 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700491}
492
Tao Bao26ea9592018-05-09 16:32:02 -0700493void RecoveryUI::TimeKey(int key_code, int count) {
494 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700495 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700496 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700497 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700498 if (key_last_down == key_code && key_down_count == count) {
499 long_press = key_long_press = true;
500 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700501 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700502 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700503}
504
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800505void RecoveryUI::EnqueueKey(int key_code) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200506 std::lock_guard<std::mutex> lg(event_queue_mutex);
507 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
508 if (event_queue_len < queue_max) {
509 InputEvent event(key_code);
510 event_queue[event_queue_len++] = event;
511 event_queue_cond.notify_one();
512 }
513}
514
515void RecoveryUI::EnqueueTouch(const Point& pos) {
516 std::lock_guard<std::mutex> lg(event_queue_mutex);
517 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
518 if (event_queue_len < queue_max) {
519 InputEvent event(pos);
520 event_queue[event_queue_len++] = event;
521 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700522 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800523}
524
Jerry Zhangb76af932018-05-22 12:08:35 -0700525void RecoveryUI::SetScreensaverState(ScreensaverState state) {
526 switch (state) {
527 case ScreensaverState::NORMAL:
528 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
529 brightness_file_)) {
530 screensaver_state_ = ScreensaverState::NORMAL;
531 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
532 << "%)";
533 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800534 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700535 }
536 break;
537 case ScreensaverState::DIMMED:
538 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
539 brightness_file_)) {
540 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
541 << "%)";
542 screensaver_state_ = ScreensaverState::DIMMED;
543 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800544 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700545 }
546 break;
547 case ScreensaverState::OFF:
548 if (android::base::WriteStringToFile("0", brightness_file_)) {
549 LOG(INFO) << "Brightness: 0 (off)";
550 screensaver_state_ = ScreensaverState::OFF;
551 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800552 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700553 }
554 break;
555 default:
556 LOG(ERROR) << "Invalid screensaver state";
557 }
558}
559
Alessandro Astonef8b86652020-10-04 18:11:40 +0200560RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
561 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700562
Jerry Zhangb76af932018-05-22 12:08:35 -0700563 // Check for a saved key queue interruption.
564 if (key_interrupted_) {
565 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astonef8b86652020-10-04 18:11:40 +0200566 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700567 }
568
Tao Bao53be3322018-08-16 11:48:50 -0700569 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800570 do {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200571 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
572 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700573 });
574 if (key_interrupted_) {
575 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astonef8b86652020-10-04 18:11:40 +0200576 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700577 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800578 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700579 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700580 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800581 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700582 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800583 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700584 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800585 }
Tao Bao53be3322018-08-16 11:48:50 -0700586 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800587 // Drop the first key if it's changing from OFF to NORMAL.
588 if (screensaver_state_ == ScreensaverState::OFF) {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200589 if (event_queue_len > 0) {
590 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800591 }
592 }
593
594 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700595 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800596 }
597 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200598 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800599
Alessandro Astonef8b86652020-10-04 18:11:40 +0200600 InputEvent event;
601 if (event_queue_len > 0) {
602 event = event_queue[0];
603 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800604 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200605 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700606}
607
Tom Marshall98716e92018-12-17 15:57:44 -0800608void RecoveryUI::CancelWaitKey() {
609 EnqueueKey(KEY_AGAIN);
610}
611
Jerry Zhangb76af932018-05-22 12:08:35 -0700612void RecoveryUI::InterruptKey() {
613 {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200614 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700615 key_interrupted_ = true;
616 }
Alessandro Astonef8b86652020-10-04 18:11:40 +0200617 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700618}
619
Elliott Hughes985022a2015-04-13 13:04:32 -0700620bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700621 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
622 if (fd < 0) {
623 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
624 return 0;
625 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700626
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700627 char buf;
628 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
629 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
630 if (close(fd) < 0) {
631 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
632 }
633 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700634}
635
Elliott Hughesec283402015-04-10 10:01:53 -0700636bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700637 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700638 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700639 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700640}
641
Elliott Hughes642aaa72015-04-10 12:47:46 -0700642bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700643 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700644 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700645 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700646}
647
Tianjie Xue5032212019-07-23 13:23:29 -0700648bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700649 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700650}
651
Tao Bao5f8dd992017-07-28 00:05:40 -0700652bool RecoveryUI::HasPowerKey() const {
653 return has_power_key;
654}
655
656bool RecoveryUI::HasTouchScreen() const {
657 return has_touch_screen;
658}
659
Doug Zongker32a0a472011-11-01 11:00:20 -0700660void RecoveryUI::FlushKeys() {
Alessandro Astonef8b86652020-10-04 18:11:40 +0200661 std::lock_guard<std::mutex> lg(event_queue_mutex);
662 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700663}
664
Elliott Hughes642aaa72015-04-10 12:47:46 -0700665RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700666 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700667 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700668 key_long_press = false;
669 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700670
671 // 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 -0700672 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
673 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700674 return TOGGLE;
675 }
676 } else {
677 // Otherwise long press of any button toggles to the text display,
678 // and there's no way to toggle back (but that's pretty useless anyway).
679 if (is_long_press && !IsTextVisible()) {
680 return TOGGLE;
681 }
682
683 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
684 if (is_long_press && IsTextVisible()) {
685 EnqueueKey(KEY_ENTER);
686 return IGNORE;
687 }
688 }
689
690 // Press power seven times in a row to reboot.
691 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700692 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700693
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700694 if (reboot_enabled) {
695 ++consecutive_power_keys;
696 if (consecutive_power_keys >= 7) {
697 return REBOOT;
698 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700699 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700700 } else {
701 consecutive_power_keys = 0;
702 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700703
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700704 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700705}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800706
Tianjie Xu8f397302018-08-20 13:40:47 -0700707void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700708
709void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700710 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700711 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700712}