blob: b8b4f6c44025e298603a9625ea87504c197ed450 [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 Xub5108c32018-08-20 13:40:47 -070017#include "recovery_ui/ui.h"
Tao Bao736d59c2017-01-03 10:15:33 -080018
Doug Zongker32a0a472011-11-01 11:00:20 -070019#include <errno.h>
20#include <fcntl.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070024#include <sys/time.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
Tao Bao26ea9592018-05-09 16:32:02 -070029#include <chrono>
Tao Bao9468fc02017-03-17 00:57:37 -070030#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080031#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070032#include <thread>
Tao Bao736d59c2017-01-03 10:15:33 -080033
Tao Bao6278bdf2017-01-16 17:38:18 -080034#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/parseint.h>
Tao Bao0bc88de2018-07-31 14:53:16 -070037#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080038#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070039
Tao Bao26ea9592018-05-09 16:32:02 -070040#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070041#include "otautil/sysutil.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
Tao Bao26ea9592018-05-09 16:32:02 -070043using namespace std::chrono_literals;
44
Tao Bao0bc88de2018-07-31 14:53:16 -070045constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
46constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
48constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness";
49constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
kataoc35c1b02017-12-08 11:02:43 +080050 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Tao Bao0bc88de2018-07-31 14:53:16 -070052constexpr int kDefaultTouchLowThreshold = 50;
53constexpr int kDefaultTouchHighThreshold = 90;
54
Elliott Hughesec283402015-04-10 10:01:53 -070055RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080056 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080057 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080058 brightness_file_(BRIGHTNESS_FILE),
59 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tom Marshall2ec11982017-08-23 22:45:19 +000060 touch_screen_allowed_(true),
David Anderson983e2d52019-01-02 11:35:38 -080061 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070062 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
63 kDefaultTouchLowThreshold)),
64 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
65 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070066 key_interrupted_(false),
Tom Marshall7e2c6002020-03-10 20:17:49 +010067 event_queue_len(0),
Tao Bao736d59c2017-01-03 10:15:33 -080068 key_last_down(-1),
69 key_long_press(false),
70 key_down_count(0),
71 enable_reboot(true),
72 consecutive_power_keys(0),
73 last_key(-1),
74 has_power_key(false),
75 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080076 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070077 has_touch_screen(false),
78 touch_slot_(0),
Tom Marshall7e2c6002020-03-10 20:17:49 +010079 touch_finger_down_(false),
80 touch_saw_x_(false),
81 touch_saw_y_(false),
82 touch_reported_(false),
Tao Bao046aae22017-07-31 23:15:09 -070083 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080084 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080085 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070086}
87
Tao Bao26ea9592018-05-09 16:32:02 -070088RecoveryUI::~RecoveryUI() {
89 ev_exit();
90 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070091 if (input_thread_.joinable()) {
92 input_thread_.join();
93 }
Tao Bao26ea9592018-05-09 16:32:02 -070094}
95
Tom Marshalld23b5102017-08-24 13:50:01 +000096void RecoveryUI::OnTouchDeviceDetected(int fd) {
97 char name[256];
98 char path[PATH_MAX];
99 char buf[4096];
100
101 memset(name, 0, sizeof(name));
102 if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
103 return;
104 }
105 sprintf(path, "/sys/board_properties/virtualkeys.%s", name);
106 int vkfd = open(path, O_RDONLY);
107 if (vkfd < 0) {
108 LOG(INFO) << "vkeys: could not open " << path;
109 return;
110 }
111 ssize_t len = read(vkfd, buf, sizeof(buf));
112 close(vkfd);
113 if (len <= 0) {
114 LOG(ERROR) << "vkeys: could not read " << path;
115 return;
116 }
117 buf[len] = '\0';
118
119 char* p = buf;
120 char* endp;
121 for (size_t n = 0; p < buf + len && *p == '0'; ++n) {
122 int val[6];
123 int f;
124 for (f = 0; *p && f < 6; ++f) {
125 val[f] = strtol(p, &endp, 0);
126 if (p == endp) break;
127 p = endp + 1;
128 }
129 if (f != 6 || val[0] != 0x01) break;
130 vkey_t vk;
131 vk.keycode = val[1];
132 vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2);
133 vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2);
134 virtual_keys_.push_back(vk);
135 }
136}
137
Elliott Hughes642aaa72015-04-10 12:47:46 -0700138void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700139 if (key_code == KEY_POWER) {
140 has_power_key = true;
141 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
142 has_down_key = true;
143 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
144 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700145 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
146 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700147 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700148}
149
Tao Bao6278bdf2017-01-16 17:38:18 -0800150bool RecoveryUI::InitScreensaver() {
151 // Disabled.
152 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
153 return false;
154 }
kataoc35c1b02017-12-08 11:02:43 +0800155 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
156 brightness_file_ = BRIGHTNESS_FILE_SDM;
157 }
158 if (access(max_brightness_file_.c_str(), R_OK)) {
159 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
160 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800161 // Set the initial brightness level based on the max brightness. Note that reading the initial
162 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
163 // we don't have a good way to query the default value.
164 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800165 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800166 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800167 return false;
168 }
169
170 unsigned int max_value;
171 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
172 LOG(WARNING) << "Failed to parse max brightness: " << content;
173 return false;
174 }
175
176 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
177 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
178 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800179 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800180 PLOG(WARNING) << "Failed to set brightness";
181 return false;
182 }
183
184 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
185 screensaver_state_ = ScreensaverState::NORMAL;
186 return true;
187}
188
Tao Baoefb49ad2017-01-31 23:03:10 -0800189bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700190 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
191 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700192
Tao Bao736d59c2017-01-03 10:15:33 -0800193 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
194
Tao Bao5f8dd992017-07-28 00:05:40 -0700195 if (touch_screen_allowed_) {
Tom Marshalld23b5102017-08-24 13:50:01 +0000196 ev_iterate_touch_inputs(
197 std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1),
198 std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700199
200 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
201 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
202 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
203 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
204 // mode will be turned on automatically on debuggable builds, even without a swipe.
205 std::string cmdline;
206 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
207 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
208 } else {
209 // Non-fatal, and won't affect Init() result.
210 PLOG(WARNING) << "Failed to read /proc/cmdline";
211 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700212 }
213
Tao Bao6278bdf2017-01-16 17:38:18 -0800214 if (!InitScreensaver()) {
215 LOG(INFO) << "Screensaver disabled";
216 }
217
Tao Bao26ea9592018-05-09 16:32:02 -0700218 // Create a separate thread that handles input events.
219 input_thread_ = std::thread([this]() {
220 while (!this->input_thread_stopped_) {
221 if (!ev_wait(500)) {
222 ev_dispatch();
223 }
224 }
225 });
226
Tao Bao736d59c2017-01-03 10:15:33 -0800227 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700228}
229
Tom Marshall0037a5e2019-08-06 16:30:02 +0200230void RecoveryUI::CalibrateTouch(int fd) {
231 struct input_absinfo info;
232 static bool calibrated = false;
233
234 if (calibrated) return;
235
236 memset(&info, 0, sizeof(info));
237 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
238 touch_min_.x(info.minimum);
239 touch_max_.x(info.maximum);
240 }
241 memset(&info, 0, sizeof(info));
242 if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
243 touch_min_.y(info.minimum);
244 touch_max_.y(info.maximum);
245 }
246
247 calibrated = true;
248}
249
Tom Marshall7e2c6002020-03-10 20:17:49 +0100250void RecoveryUI::OnTouchPress() {
251 touch_start_ = touch_track_ = touch_pos_;
252}
Tao Bao5f8dd992017-07-28 00:05:40 -0700253
Tom Marshall7e2c6002020-03-10 20:17:49 +0100254void RecoveryUI::OnTouchTrack() {
255 if (touch_pos_.y() <= gr_fb_height()) {
256 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
257 int dy = touch_pos_.y() - touch_track_.y();
258 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
259 ProcessKey(key, 1); // press key
260 ProcessKey(key, 0); // and release it
261 int sgn = (dy > 0) - (dy < 0);
262 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
Tom Marshalld23b5102017-08-24 13:50:01 +0000263 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700264 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100265}
Tao Bao5f8dd992017-07-28 00:05:40 -0700266
Tom Marshall7e2c6002020-03-10 20:17:49 +0100267void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700268 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
269 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
270 ShowText(true);
271 return;
272 }
273
Tom Marshall7e2c6002020-03-10 20:17:49 +0100274 // Check vkeys. Only report if touch both starts and ends in the vkey.
275 if (touch_start_.y() > gr_fb_height() && touch_pos_.y() > gr_fb_height()) {
276 for (const auto& vk : virtual_keys_) {
277 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
278 ProcessKey(vk.keycode, 1); // press key
279 ProcessKey(vk.keycode, 0); // and release it
280 }
281 }
282 return;
283 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700284
Tom Marshall7e2c6002020-03-10 20:17:49 +0100285 // If we tracked a vertical swipe, ignore the release
286 if (touch_track_ != touch_start_) {
287 return;
288 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700289
Tom Marshall7e2c6002020-03-10 20:17:49 +0100290 // Check for horizontal swipe
291 Point delta = touch_pos_ - touch_start_;
292 if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) {
293 int key = (delta.x() < 0) ? KEY_BACK : KEY_POWER;
294 ProcessKey(key, 1); // press key
295 ProcessKey(key, 0); // and release it
296 return;
297 }
298
299 // Simple touch
300 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700301}
302
Elliott Hughes985022a2015-04-13 13:04:32 -0700303int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700304 struct input_event ev;
305 if (ev_get_input(fd, epevents, &ev) == -1) {
306 return -1;
307 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700308
Tao Bao5f8dd992017-07-28 00:05:40 -0700309 // Touch inputs handling.
310 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700311 // Per the doc Multi-touch Protocol at below, there are two protocols.
312 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
313 //
314 // The main difference between the stateless type A protocol and the stateful type B slot protocol
315 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
316 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
317 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
318 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
319 //
320 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
321 // ABS_MT_TRACKING_ID being -1.
322 //
323 // Touch input events will only be available if touch_screen_allowed_ is set.
324
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700325 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700326 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100327 // There might be multiple SYN_REPORT events. Only report press/release once.
328 if (!touch_reported_ && touch_finger_down_) {
329 if (touch_saw_x_ && touch_saw_y_) {
330 OnTouchPress();
331 touch_reported_ = true;
332 touch_saw_x_ = touch_saw_y_ = false;
333 }
334 } else if (touch_reported_ && !touch_finger_down_) {
335 OnTouchRelease();
336 touch_reported_ = false;
337 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700338 }
339 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700340 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700341 }
342
343 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 if (ev.code == REL_Y) {
345 // accumulate the up or down motion reported by
346 // the trackball. When it exceeds a threshold
347 // (positive or negative), fake an up/down
348 // key event.
349 rel_sum += ev.value;
350 if (rel_sum > 3) {
351 ProcessKey(KEY_DOWN, 1); // press down key
352 ProcessKey(KEY_DOWN, 0); // and release it
353 rel_sum = 0;
354 } else if (rel_sum < -3) {
355 ProcessKey(KEY_UP, 1); // press up key
356 ProcessKey(KEY_UP, 0); // and release it
357 rel_sum = 0;
358 }
359 }
360 } else {
361 rel_sum = 0;
362 }
363
Tao Bao5f8dd992017-07-28 00:05:40 -0700364 if (touch_screen_allowed_ && ev.type == EV_ABS) {
Tom Marshall0037a5e2019-08-06 16:30:02 +0200365 CalibrateTouch(fd);
Tao Bao5f8dd992017-07-28 00:05:40 -0700366 if (ev.code == ABS_MT_SLOT) {
367 touch_slot_ = ev.value;
368 }
369 // Ignore other fingers.
370 if (touch_slot_ > 0) return 0;
371
372 switch (ev.code) {
373 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700374 touch_finger_down_ = true;
Tom Marshall7e2c6002020-03-10 20:17:49 +0100375 touch_saw_x_ = true;
Tom Marshall0037a5e2019-08-06 16:30:02 +0200376 touch_pos_.x(ev.value * gr_fb_width() / (touch_max_.x() - touch_min_.x()));
Tom Marshall7e2c6002020-03-10 20:17:49 +0100377 if (touch_reported_ && touch_saw_y_) {
378 OnTouchTrack();
379 touch_saw_x_ = touch_saw_y_ = false;
380 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700381 break;
382
383 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700384 touch_finger_down_ = true;
Tom Marshall7e2c6002020-03-10 20:17:49 +0100385 touch_saw_y_ = true;
Tom Marshall0037a5e2019-08-06 16:30:02 +0200386 touch_pos_.y(ev.value * gr_fb_height() / (touch_max_.y() - touch_min_.y()));
Tom Marshall7e2c6002020-03-10 20:17:49 +0100387 if (touch_reported_ && touch_saw_x_) {
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_TRACKING_ID:
394 // Protocol B: -1 marks lifting the contact.
395 if (ev.value < 0) touch_finger_down_ = false;
396 break;
397 }
398 return 0;
399 }
400
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700401 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700402 if (touch_screen_allowed_) {
403 if (ev.code == BTN_TOUCH) {
404 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
405 // lifting the contact.
406 touch_finger_down_ = (ev.value == 1);
407 }
408
409 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
410 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
411 // KEY_POWER and KEY_UP as KEY_DOWN).
412 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
413 return 0;
414 }
415 }
416
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700417 ProcessKey(ev.code, ev.value);
418 }
419
420 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700421}
422
Tao Bao26ea9592018-05-09 16:32:02 -0700423// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
424// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
425// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
426// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700427//
Tao Bao26ea9592018-05-09 16:32:02 -0700428// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
429// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700430//
431// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700432void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700433 bool register_key = false;
434 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800435
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700436 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100437 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700438 key_pressed[key_code] = updown;
439 if (updown) {
440 ++key_down_count;
441 key_last_down = key_code;
442 key_long_press = false;
443 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
444 time_key_thread.detach();
445 } else {
446 if (key_last_down == key_code) {
447 long_press = key_long_press;
448 register_key = true;
449 }
450 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700451 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700452 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700453
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700454 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700455 if (register_key) {
456 switch (CheckKey(key_code, long_press)) {
457 case RecoveryUI::IGNORE:
458 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800459
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700460 case RecoveryUI::TOGGLE:
461 ShowText(!IsTextVisible());
462 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700463
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700464 case RecoveryUI::REBOOT:
465 if (reboot_enabled) {
466 reboot("reboot,");
467 while (true) {
468 pause();
469 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700470 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700471 break;
472
473 case RecoveryUI::ENQUEUE:
474 EnqueueKey(key_code);
475 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700476 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700477 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700478}
479
Tao Bao26ea9592018-05-09 16:32:02 -0700480void RecoveryUI::TimeKey(int key_code, int count) {
481 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700482 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700483 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100484 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700485 if (key_last_down == key_code && key_down_count == count) {
486 long_press = key_long_press = true;
487 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700489 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700490}
491
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800492void RecoveryUI::EnqueueKey(int key_code) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100493 std::lock_guard<std::mutex> lg(event_queue_mutex);
494 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
495 if (event_queue_len < queue_max) {
496 InputEvent event(key_code);
497 event_queue[event_queue_len++] = event;
498 event_queue_cond.notify_one();
499 }
500}
501
502void RecoveryUI::EnqueueTouch(const Point& pos) {
503 std::lock_guard<std::mutex> lg(event_queue_mutex);
504 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
505 if (event_queue_len < queue_max) {
506 InputEvent event(pos);
507 event_queue[event_queue_len++] = event;
508 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700509 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800510}
511
Jerry Zhangb76af932018-05-22 12:08:35 -0700512void RecoveryUI::SetScreensaverState(ScreensaverState state) {
513 switch (state) {
514 case ScreensaverState::NORMAL:
515 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
516 brightness_file_)) {
517 screensaver_state_ = ScreensaverState::NORMAL;
518 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
519 << "%)";
520 } else {
521 LOG(ERROR) << "Unable to set brightness to normal";
522 }
523 break;
524 case ScreensaverState::DIMMED:
525 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
526 brightness_file_)) {
527 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
528 << "%)";
529 screensaver_state_ = ScreensaverState::DIMMED;
530 } else {
531 LOG(ERROR) << "Unable to set brightness to dim";
532 }
533 break;
534 case ScreensaverState::OFF:
535 if (android::base::WriteStringToFile("0", brightness_file_)) {
536 LOG(INFO) << "Brightness: 0 (off)";
537 screensaver_state_ = ScreensaverState::OFF;
538 } else {
539 LOG(ERROR) << "Unable to set brightness to off";
540 }
541 break;
542 default:
543 LOG(ERROR) << "Invalid screensaver state";
544 }
545}
546
Tom Marshall7e2c6002020-03-10 20:17:49 +0100547RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
548 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700549
Jerry Zhangb76af932018-05-22 12:08:35 -0700550 // Check for a saved key queue interruption.
551 if (key_interrupted_) {
552 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall7e2c6002020-03-10 20:17:49 +0100553 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700554 }
555
Tao Bao53be3322018-08-16 11:48:50 -0700556 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800557 do {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100558 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
559 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700560 });
561 if (key_interrupted_) {
562 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall7e2c6002020-03-10 20:17:49 +0100563 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700564 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800565 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700566 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700567 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800568 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700569 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800570 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700571 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800572 }
Tao Bao53be3322018-08-16 11:48:50 -0700573 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800574 // Drop the first key if it's changing from OFF to NORMAL.
575 if (screensaver_state_ == ScreensaverState::OFF) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100576 if (event_queue_len > 0) {
577 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800578 }
579 }
580
581 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700582 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800583 }
584 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100585 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800586
Tom Marshall7e2c6002020-03-10 20:17:49 +0100587 InputEvent event;
588 if (event_queue_len > 0) {
589 event = event_queue[0];
590 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800591 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100592 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700593}
594
Tom Marshalla9ac9552018-12-17 15:57:44 -0800595void RecoveryUI::CancelWaitKey() {
596 EnqueueKey(KEY_REFRESH);
597}
598
Jerry Zhangb76af932018-05-22 12:08:35 -0700599void RecoveryUI::InterruptKey() {
600 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100601 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700602 key_interrupted_ = true;
603 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100604 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700605}
606
Elliott Hughes985022a2015-04-13 13:04:32 -0700607bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700608 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
609 if (fd < 0) {
610 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
611 return 0;
612 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700613
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700614 char buf;
615 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
616 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
617 if (close(fd) < 0) {
618 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
619 }
620 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700621}
622
Elliott Hughesec283402015-04-10 10:01:53 -0700623bool RecoveryUI::IsKeyPressed(int key) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100624 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700625 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700626 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700627}
628
Elliott Hughes642aaa72015-04-10 12:47:46 -0700629bool RecoveryUI::IsLongPress() {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100630 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700631 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700632 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700633}
634
Elliott Hughes4af215b2015-04-10 15:00:34 -0700635bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700636 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700637}
638
Tao Bao5f8dd992017-07-28 00:05:40 -0700639bool RecoveryUI::HasPowerKey() const {
640 return has_power_key;
641}
642
643bool RecoveryUI::HasTouchScreen() const {
644 return has_touch_screen;
645}
646
Doug Zongker32a0a472011-11-01 11:00:20 -0700647void RecoveryUI::FlushKeys() {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100648 std::lock_guard<std::mutex> lg(event_queue_mutex);
649 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700650}
651
Elliott Hughes642aaa72015-04-10 12:47:46 -0700652RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700653 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100654 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700655 key_long_press = false;
656 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700657
658 // 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 -0700659 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
660 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700661 return TOGGLE;
662 }
663 } else {
664 // Otherwise long press of any button toggles to the text display,
665 // and there's no way to toggle back (but that's pretty useless anyway).
666 if (is_long_press && !IsTextVisible()) {
667 return TOGGLE;
668 }
669
670 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
671 if (is_long_press && IsTextVisible()) {
672 EnqueueKey(KEY_ENTER);
673 return IGNORE;
674 }
675 }
676
677 // Press power seven times in a row to reboot.
678 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700679 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700680
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700681 if (reboot_enabled) {
682 ++consecutive_power_keys;
683 if (consecutive_power_keys >= 7) {
684 return REBOOT;
685 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700686 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700687 } else {
688 consecutive_power_keys = 0;
689 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700690
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700691 last_key = key;
692 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700693}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800694
Tianjie Xub5108c32018-08-20 13:40:47 -0700695void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700696
697void RecoveryUI::SetEnableReboot(bool enabled) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100698 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700699 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700700}