blob: 8561120cf9b7010c7490478efafcce9e67ed99d6 [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 Marshallf03f8252019-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 Marshall4c80d0d2017-08-23 22:45:19 +000065 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080066 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070067 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
68 kDefaultTouchLowThreshold)),
69 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
70 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070071 key_interrupted_(false),
Alessandro Astone73961412020-10-04 18:11:40 +020072 event_queue_len(0),
Tao Bao736d59c2017-01-03 10:15:33 -080073 key_last_down(-1),
74 key_long_press(false),
75 key_down_count(0),
76 enable_reboot(true),
77 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080078 has_power_key(false),
79 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080080 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070081 has_touch_screen(false),
82 touch_slot_(0),
Alessandro Astone73961412020-10-04 18:11:40 +020083 touch_finger_down_(false),
84 touch_saw_x_(false),
85 touch_saw_y_(false),
86 touch_reported_(false),
Tao Bao046aae22017-07-31 23:15:09 -070087 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080088 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080089 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070090}
91
Tao Bao26ea9592018-05-09 16:32:02 -070092RecoveryUI::~RecoveryUI() {
93 ev_exit();
94 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070095 if (input_thread_.joinable()) {
96 input_thread_.join();
97 }
Tao Bao26ea9592018-05-09 16:32:02 -070098}
99
Tom Marshall4b921692017-08-24 13:50:01 +0000100void RecoveryUI::OnTouchDeviceDetected(int fd) {
101 char name[256];
102 char path[PATH_MAX];
103 char buf[4096];
104
105 memset(name, 0, sizeof(name));
106 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
107 return;
108 }
109 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
110 int vkfd = open(path, O_RDONLY);
111 if (vkfd < 0) {
112 LOG(INFO) << "vkeys: could not open " << path;
113 return;
114 }
115 ssize_t len = read(vkfd, buf, sizeof(buf));
116 close(vkfd);
117 if (len <= 0) {
118 LOG(ERROR) << "vkeys: could not read " << path;
119 return;
120 }
121 buf[len] = '\0';
122
123 char* p = buf;
124 char* endp;
125 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
126 int val[6];
127 int f;
128 for (f = 0; *p && f < 6; ++f) {
129 val[f] = strtol(p, &endp, 0);
130 if (p == endp) break;
131 p = endp + 1;
132 }
133 if (f != 6 || val[0] != 0x01) break;
134 vkey_t vk;
135 vk.keycode = val[1];
136 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
137 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
138 virtual_keys_.push_back(vk);
139 }
140}
141
Elliott Hughes642aaa72015-04-10 12:47:46 -0700142void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700143 if (key_code == KEY_POWER) {
144 has_power_key = true;
145 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
146 has_down_key = true;
147 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
148 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700149 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
150 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700151 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700152}
153
Tao Bao6278bdf2017-01-16 17:38:18 -0800154bool RecoveryUI::InitScreensaver() {
155 // Disabled.
156 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
157 return false;
158 }
kataoc35c1b02017-12-08 11:02:43 +0800159 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700160 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
161 brightness_file_ = BRIGHTNESS_FILE_SDM;
162 } else {
163 brightness_file_ = BRIGHTNESS_FILE_PWM;
164 }
kataoc35c1b02017-12-08 11:02:43 +0800165 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700166
kataoc35c1b02017-12-08 11:02:43 +0800167 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700168 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
169 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
170 } else {
171 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
172 }
kataoc35c1b02017-12-08 11:02:43 +0800173 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800174 // Set the initial brightness level based on the max brightness. Note that reading the initial
175 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
176 // we don't have a good way to query the default value.
177 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800178 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800179 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800180 return false;
181 }
182
183 unsigned int max_value;
184 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
185 LOG(WARNING) << "Failed to parse max brightness: " << content;
186 return false;
187 }
188
189 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
190 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
191 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800192 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800193 PLOG(WARNING) << "Failed to set brightness";
194 return false;
195 }
196
197 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
198 screensaver_state_ = ScreensaverState::NORMAL;
199 return true;
200}
201
Tao Baoefb49ad2017-01-31 23:03:10 -0800202bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700203 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
204 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700205
Tao Bao736d59c2017-01-03 10:15:33 -0800206 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
207
Tao Bao5f8dd992017-07-28 00:05:40 -0700208 if (touch_screen_allowed_) {
Tom Marshall4b921692017-08-24 13:50:01 +0000209 ev_iterate_touch_inputs(
210 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
211 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700212
213 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
214 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
215 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
216 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
217 // mode will be turned on automatically on debuggable builds, even without a swipe.
218 std::string cmdline;
219 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
220 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
221 } else {
222 // Non-fatal, and won't affect Init() result.
223 PLOG(WARNING) << "Failed to read /proc/cmdline";
224 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700225 }
226
Tao Bao6278bdf2017-01-16 17:38:18 -0800227 if (!InitScreensaver()) {
228 LOG(INFO) << "Screensaver disabled";
229 }
230
Tao Bao26ea9592018-05-09 16:32:02 -0700231 // Create a separate thread that handles input events.
232 input_thread_ = std::thread([this]() {
233 while (!this->input_thread_stopped_) {
234 if (!ev_wait(500)) {
235 ev_dispatch();
236 }
237 }
238 });
239
Tao Bao736d59c2017-01-03 10:15:33 -0800240 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700241}
242
Tom Marshall405bfd22019-08-06 16:30:02 +0200243void RecoveryUI::CalibrateTouch(int fd) {
244 struct input_absinfo info;
245 static bool calibrated = false;
246
247 if (calibrated) return;
248
249 memset(&info, 0, sizeof(info));
250 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
251 touch_min_.x(info.minimum);
252 touch_max_.x(info.maximum);
253 }
254 memset(&info, 0, sizeof(info));
255 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
256 touch_min_.y(info.minimum);
257 touch_max_.y(info.maximum);
258 }
259
260 calibrated = true;
261}
262
Alessandro Astone73961412020-10-04 18:11:40 +0200263void RecoveryUI::OnTouchPress() {
264 touch_start_ = touch_track_ = touch_pos_;
265}
Tao Bao5f8dd992017-07-28 00:05:40 -0700266
Alessandro Astone73961412020-10-04 18:11:40 +0200267void RecoveryUI::OnTouchTrack() {
Alessandro Astonea991dad2022-03-21 17:14:47 +0100268 if (touch_pos_.y() <= gr_fb_height_real()) {
Alessandro Astone73961412020-10-04 18:11:40 +0200269 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
270 int dy = touch_pos_.y() - touch_track_.y();
271 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
272 ProcessKey(key, 1); // press key
273 ProcessKey(key, 0); // and release it
274 int sgn = (dy > 0) - (dy < 0);
275 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
Tom Marshall4b921692017-08-24 13:50:01 +0000276 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700277 }
Alessandro Astone73961412020-10-04 18:11:40 +0200278}
Tao Bao5f8dd992017-07-28 00:05:40 -0700279
Alessandro Astone73961412020-10-04 18:11:40 +0200280void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700281 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
282 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
283 ShowText(true);
284 return;
285 }
286
Alessandro Astone73961412020-10-04 18:11:40 +0200287 // Check vkeys. Only report if touch both starts and ends in the vkey.
Alessandro Astonea991dad2022-03-21 17:14:47 +0100288 if (touch_start_.y() > gr_fb_height_real() && touch_pos_.y() > gr_fb_height_real()) {
Alessandro Astone73961412020-10-04 18:11:40 +0200289 for (const auto& vk : virtual_keys_) {
290 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
291 ProcessKey(vk.keycode, 1); // press key
292 ProcessKey(vk.keycode, 0); // and release it
293 }
294 }
295 return;
296 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700297
Alessandro Astone73961412020-10-04 18:11:40 +0200298 // If we tracked a vertical swipe, ignore the release
299 if (touch_track_ != touch_start_) {
300 return;
301 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700302
Alessandro Astone73961412020-10-04 18:11:40 +0200303 // Simple touch
304 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700305}
306
Elliott Hughes985022a2015-04-13 13:04:32 -0700307int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700308 struct input_event ev;
309 if (ev_get_input(fd, epevents, &ev) == -1) {
310 return -1;
311 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700312
Tao Bao5f8dd992017-07-28 00:05:40 -0700313 // Touch inputs handling.
314 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700315 // Per the doc Multi-touch Protocol at below, there are two protocols.
316 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
317 //
318 // The main difference between the stateless type A protocol and the stateful type B slot protocol
319 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
320 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
321 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
322 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
323 //
324 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
325 // ABS_MT_TRACKING_ID being -1.
326 //
327 // Touch input events will only be available if touch_screen_allowed_ is set.
328
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700329 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700330 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Alessandro Astone73961412020-10-04 18:11:40 +0200331 // There might be multiple SYN_REPORT events. Only report press/release once.
332 if (!touch_reported_ && touch_finger_down_) {
333 if (touch_saw_x_ && touch_saw_y_) {
334 OnTouchPress();
335 touch_reported_ = true;
336 touch_saw_x_ = touch_saw_y_ = false;
337 }
338 } else if (touch_reported_ && !touch_finger_down_) {
339 OnTouchRelease();
340 touch_reported_ = false;
341 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700342 }
343 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700344 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700345 }
346
347 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700348 if (ev.code == REL_Y) {
349 // accumulate the up or down motion reported by
350 // the trackball. When it exceeds a threshold
351 // (positive or negative), fake an up/down
352 // key event.
353 rel_sum += ev.value;
354 if (rel_sum > 3) {
355 ProcessKey(KEY_DOWN, 1); // press down key
356 ProcessKey(KEY_DOWN, 0); // and release it
357 rel_sum = 0;
358 } else if (rel_sum < -3) {
359 ProcessKey(KEY_UP, 1); // press up key
360 ProcessKey(KEY_UP, 0); // and release it
361 rel_sum = 0;
362 }
363 }
364 } else {
365 rel_sum = 0;
366 }
367
Tao Bao5f8dd992017-07-28 00:05:40 -0700368 if (touch_screen_allowed_ && ev.type == EV_ABS) {
Tom Marshall405bfd22019-08-06 16:30:02 +0200369 CalibrateTouch(fd);
Tao Bao5f8dd992017-07-28 00:05:40 -0700370 if (ev.code == ABS_MT_SLOT) {
371 touch_slot_ = ev.value;
372 }
373 // Ignore other fingers.
374 if (touch_slot_ > 0) return 0;
375
376 switch (ev.code) {
377 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700378 touch_finger_down_ = true;
Alessandro Astone73961412020-10-04 18:11:40 +0200379 touch_saw_x_ = true;
Alessandro Astonea991dad2022-03-21 17:14:47 +0100380 touch_pos_.x(ev.value * gr_fb_width_real() / (touch_max_.x() - touch_min_.x()));
Alessandro Astone73961412020-10-04 18:11:40 +0200381 if (touch_reported_ && touch_saw_y_) {
382 OnTouchTrack();
383 touch_saw_x_ = touch_saw_y_ = false;
384 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700385 break;
386
387 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700388 touch_finger_down_ = true;
Alessandro Astone73961412020-10-04 18:11:40 +0200389 touch_saw_y_ = true;
Alessandro Astonea991dad2022-03-21 17:14:47 +0100390 touch_pos_.y(ev.value * gr_fb_height_real() / (touch_max_.y() - touch_min_.y()));
Alessandro Astone73961412020-10-04 18:11:40 +0200391 if (touch_reported_ && touch_saw_x_) {
392 OnTouchTrack();
393 touch_saw_x_ = touch_saw_y_ = false;
394 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700395 break;
396
397 case ABS_MT_TRACKING_ID:
398 // Protocol B: -1 marks lifting the contact.
399 if (ev.value < 0) touch_finger_down_ = false;
400 break;
401 }
402 return 0;
403 }
404
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700405 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700406 if (touch_screen_allowed_) {
407 if (ev.code == BTN_TOUCH) {
408 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
409 // lifting the contact.
410 touch_finger_down_ = (ev.value == 1);
411 }
412
413 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
414 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
415 // KEY_POWER and KEY_UP as KEY_DOWN).
416 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
417 return 0;
418 }
419 }
420
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700421 ProcessKey(ev.code, ev.value);
422 }
423
fredchiouf2f5aa22022-02-11 16:39:53 +0800424 // For Lid switch handle
425 if (ev.type == EV_SW) {
426 SetSwCallback(ev.code, ev.value);
427 }
428
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700429 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700430}
431
Tao Bao26ea9592018-05-09 16:32:02 -0700432// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
433// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
434// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
435// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700436//
Tao Bao26ea9592018-05-09 16:32:02 -0700437// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
438// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700439//
440// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700441void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700442 bool register_key = false;
443 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800444
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700445 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700446 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700447 key_pressed[key_code] = updown;
448 if (updown) {
449 ++key_down_count;
450 key_last_down = key_code;
451 key_long_press = false;
452 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
453 time_key_thread.detach();
454 } else {
455 if (key_last_down == key_code) {
456 long_press = key_long_press;
457 register_key = true;
458 }
459 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700460 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700461 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700462
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700463 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700464 if (register_key) {
465 switch (CheckKey(key_code, long_press)) {
466 case RecoveryUI::IGNORE:
467 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800468
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700469 case RecoveryUI::TOGGLE:
470 ShowText(!IsTextVisible());
471 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700472
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700473 case RecoveryUI::REBOOT:
474 if (reboot_enabled) {
Tom Marshallf03f8252019-01-04 14:37:31 -0800475 android::volmgr::VolumeManager::Instance()->unmountAll();
Mark Salyzyn488cc052019-05-20 10:36:16 -0700476 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700477 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700478 break;
479
480 case RecoveryUI::ENQUEUE:
481 EnqueueKey(key_code);
482 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700483 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700484 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700485}
486
Tao Bao26ea9592018-05-09 16:32:02 -0700487void RecoveryUI::TimeKey(int key_code, int count) {
488 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700489 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700490 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700491 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700492 if (key_last_down == key_code && key_down_count == count) {
493 long_press = key_long_press = true;
494 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700495 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700496 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700497}
498
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800499void RecoveryUI::EnqueueKey(int key_code) {
Alessandro Astone73961412020-10-04 18:11:40 +0200500 std::lock_guard<std::mutex> lg(event_queue_mutex);
501 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
502 if (event_queue_len < queue_max) {
503 InputEvent event(key_code);
504 event_queue[event_queue_len++] = event;
505 event_queue_cond.notify_one();
506 }
507}
508
509void RecoveryUI::EnqueueTouch(const Point& pos) {
510 std::lock_guard<std::mutex> lg(event_queue_mutex);
511 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
512 if (event_queue_len < queue_max) {
513 InputEvent event(pos);
514 event_queue[event_queue_len++] = event;
515 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700516 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800517}
518
Jerry Zhangb76af932018-05-22 12:08:35 -0700519void RecoveryUI::SetScreensaverState(ScreensaverState state) {
520 switch (state) {
521 case ScreensaverState::NORMAL:
522 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
523 brightness_file_)) {
524 screensaver_state_ = ScreensaverState::NORMAL;
525 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
526 << "%)";
527 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800528 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700529 }
530 break;
531 case ScreensaverState::DIMMED:
532 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
533 brightness_file_)) {
534 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
535 << "%)";
536 screensaver_state_ = ScreensaverState::DIMMED;
537 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800538 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700539 }
540 break;
541 case ScreensaverState::OFF:
542 if (android::base::WriteStringToFile("0", brightness_file_)) {
543 LOG(INFO) << "Brightness: 0 (off)";
544 screensaver_state_ = ScreensaverState::OFF;
545 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800546 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700547 }
548 break;
549 default:
550 LOG(ERROR) << "Invalid screensaver state";
551 }
552}
553
Alessandro Astone73961412020-10-04 18:11:40 +0200554RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
555 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700556
Jerry Zhangb76af932018-05-22 12:08:35 -0700557 // Check for a saved key queue interruption.
558 if (key_interrupted_) {
559 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astone73961412020-10-04 18:11:40 +0200560 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700561 }
562
Tao Bao53be3322018-08-16 11:48:50 -0700563 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800564 do {
Alessandro Astone73961412020-10-04 18:11:40 +0200565 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
566 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700567 });
568 if (key_interrupted_) {
569 SetScreensaverState(ScreensaverState::NORMAL);
Alessandro Astone73961412020-10-04 18:11:40 +0200570 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700571 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800572 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700573 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700574 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800575 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700576 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800577 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700578 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800579 }
Tao Bao53be3322018-08-16 11:48:50 -0700580 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800581 // Drop the first key if it's changing from OFF to NORMAL.
582 if (screensaver_state_ == ScreensaverState::OFF) {
Alessandro Astone73961412020-10-04 18:11:40 +0200583 if (event_queue_len > 0) {
584 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800585 }
586 }
587
588 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700589 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800590 }
591 }
Alessandro Astone73961412020-10-04 18:11:40 +0200592 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800593
Alessandro Astone73961412020-10-04 18:11:40 +0200594 InputEvent event;
595 if (event_queue_len > 0) {
596 event = event_queue[0];
597 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800598 }
Alessandro Astone73961412020-10-04 18:11:40 +0200599 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700600}
601
Tom Marshallfdf50332018-12-17 15:57:44 -0800602void RecoveryUI::CancelWaitKey() {
603 EnqueueKey(KEY_AGAIN);
604}
605
Jerry Zhangb76af932018-05-22 12:08:35 -0700606void RecoveryUI::InterruptKey() {
607 {
Alessandro Astone73961412020-10-04 18:11:40 +0200608 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700609 key_interrupted_ = true;
610 }
Alessandro Astone73961412020-10-04 18:11:40 +0200611 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700612}
613
Elliott Hughes985022a2015-04-13 13:04:32 -0700614bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700615 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
616 if (fd < 0) {
617 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
618 return 0;
619 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700620
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700621 char buf;
622 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
623 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
624 if (close(fd) < 0) {
625 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
626 }
627 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700628}
629
Elliott Hughesec283402015-04-10 10:01:53 -0700630bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700631 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700632 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700633 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700634}
635
Elliott Hughes642aaa72015-04-10 12:47:46 -0700636bool RecoveryUI::IsLongPress() {
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 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700639 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700640}
641
Tianjie Xue5032212019-07-23 13:23:29 -0700642bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700643 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700644}
645
Tao Bao5f8dd992017-07-28 00:05:40 -0700646bool RecoveryUI::HasPowerKey() const {
647 return has_power_key;
648}
649
650bool RecoveryUI::HasTouchScreen() const {
651 return has_touch_screen;
652}
653
Doug Zongker32a0a472011-11-01 11:00:20 -0700654void RecoveryUI::FlushKeys() {
Alessandro Astone73961412020-10-04 18:11:40 +0200655 std::lock_guard<std::mutex> lg(event_queue_mutex);
656 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700657}
658
Elliott Hughes642aaa72015-04-10 12:47:46 -0700659RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700660 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700661 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700662 key_long_press = false;
663 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700664
665 // 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 -0700666 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
667 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700668 return TOGGLE;
669 }
670 } else {
671 // Otherwise long press of any button toggles to the text display,
672 // and there's no way to toggle back (but that's pretty useless anyway).
673 if (is_long_press && !IsTextVisible()) {
674 return TOGGLE;
675 }
676
677 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
678 if (is_long_press && IsTextVisible()) {
679 EnqueueKey(KEY_ENTER);
680 return IGNORE;
681 }
682 }
683
684 // Press power seven times in a row to reboot.
685 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700686 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700687
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700688 if (reboot_enabled) {
689 ++consecutive_power_keys;
690 if (consecutive_power_keys >= 7) {
691 return REBOOT;
692 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700693 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700694 } else {
695 consecutive_power_keys = 0;
696 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700697
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700698 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700699}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800700
Tianjie Xu8f397302018-08-20 13:40:47 -0700701void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700702
703void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700704 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700705 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700706}