blob: 45d747b94dc80c6398fdaac719fd71d521f07875 [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 Marshall7e2c6002020-03-10 20:17:49 +0100230void RecoveryUI::OnTouchPress() {
231 touch_start_ = touch_track_ = touch_pos_;
232}
Tao Bao5f8dd992017-07-28 00:05:40 -0700233
Tom Marshall7e2c6002020-03-10 20:17:49 +0100234void RecoveryUI::OnTouchTrack() {
235 if (touch_pos_.y() <= gr_fb_height()) {
236 while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) {
237 int dy = touch_pos_.y() - touch_track_.y();
238 int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP;
239 ProcessKey(key, 1); // press key
240 ProcessKey(key, 0); // and release it
241 int sgn = (dy > 0) - (dy < 0);
242 touch_track_.y(touch_track_.y() + sgn * MenuItemHeight());
Tom Marshalld23b5102017-08-24 13:50:01 +0000243 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700244 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100245}
Tao Bao5f8dd992017-07-28 00:05:40 -0700246
Tom Marshall7e2c6002020-03-10 20:17:49 +0100247void RecoveryUI::OnTouchRelease() {
Tao Bao046aae22017-07-31 23:15:09 -0700248 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
249 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
250 ShowText(true);
251 return;
252 }
253
Tom Marshall7e2c6002020-03-10 20:17:49 +0100254 // Check vkeys. Only report if touch both starts and ends in the vkey.
255 if (touch_start_.y() > gr_fb_height() && touch_pos_.y() > gr_fb_height()) {
256 for (const auto& vk : virtual_keys_) {
257 if (vk.inside(touch_start_) && vk.inside(touch_pos_)) {
258 ProcessKey(vk.keycode, 1); // press key
259 ProcessKey(vk.keycode, 0); // and release it
260 }
261 }
262 return;
263 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700264
Tom Marshall7e2c6002020-03-10 20:17:49 +0100265 // If we tracked a vertical swipe, ignore the release
266 if (touch_track_ != touch_start_) {
267 return;
268 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700269
Tom Marshall7e2c6002020-03-10 20:17:49 +0100270 // Check for horizontal swipe
271 Point delta = touch_pos_ - touch_start_;
272 if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) {
273 int key = (delta.x() < 0) ? KEY_BACK : KEY_POWER;
274 ProcessKey(key, 1); // press key
275 ProcessKey(key, 0); // and release it
276 return;
277 }
278
279 // Simple touch
280 EnqueueTouch(touch_pos_);
Tao Bao5f8dd992017-07-28 00:05:40 -0700281}
282
Elliott Hughes985022a2015-04-13 13:04:32 -0700283int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700284 struct input_event ev;
285 if (ev_get_input(fd, epevents, &ev) == -1) {
286 return -1;
287 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700288
Tao Bao5f8dd992017-07-28 00:05:40 -0700289 // Touch inputs handling.
290 //
Tao Bao5f8dd992017-07-28 00:05:40 -0700291 // Per the doc Multi-touch Protocol at below, there are two protocols.
292 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
293 //
294 // The main difference between the stateless type A protocol and the stateful type B slot protocol
295 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
296 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
297 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
298 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
299 //
300 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
301 // ABS_MT_TRACKING_ID being -1.
302 //
303 // Touch input events will only be available if touch_screen_allowed_ is set.
304
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700305 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700306 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100307 // There might be multiple SYN_REPORT events. Only report press/release once.
308 if (!touch_reported_ && touch_finger_down_) {
309 if (touch_saw_x_ && touch_saw_y_) {
310 OnTouchPress();
311 touch_reported_ = true;
312 touch_saw_x_ = touch_saw_y_ = false;
313 }
314 } else if (touch_reported_ && !touch_finger_down_) {
315 OnTouchRelease();
316 touch_reported_ = false;
317 touch_saw_x_ = touch_saw_y_ = false;
Tao Bao5f8dd992017-07-28 00:05:40 -0700318 }
319 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700320 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700321 }
322
323 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700324 if (ev.code == REL_Y) {
325 // accumulate the up or down motion reported by
326 // the trackball. When it exceeds a threshold
327 // (positive or negative), fake an up/down
328 // key event.
329 rel_sum += ev.value;
330 if (rel_sum > 3) {
331 ProcessKey(KEY_DOWN, 1); // press down key
332 ProcessKey(KEY_DOWN, 0); // and release it
333 rel_sum = 0;
334 } else if (rel_sum < -3) {
335 ProcessKey(KEY_UP, 1); // press up key
336 ProcessKey(KEY_UP, 0); // and release it
337 rel_sum = 0;
338 }
339 }
340 } else {
341 rel_sum = 0;
342 }
343
Tao Bao5f8dd992017-07-28 00:05:40 -0700344 if (touch_screen_allowed_ && ev.type == EV_ABS) {
345 if (ev.code == ABS_MT_SLOT) {
346 touch_slot_ = ev.value;
347 }
348 // Ignore other fingers.
349 if (touch_slot_ > 0) return 0;
350
351 switch (ev.code) {
352 case ABS_MT_POSITION_X:
Tao Bao5f8dd992017-07-28 00:05:40 -0700353 touch_finger_down_ = true;
Tom Marshall7e2c6002020-03-10 20:17:49 +0100354 touch_saw_x_ = true;
355 touch_pos_.x(ev.value);
356 if (touch_reported_ && touch_saw_y_) {
357 OnTouchTrack();
358 touch_saw_x_ = touch_saw_y_ = false;
359 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700360 break;
361
362 case ABS_MT_POSITION_Y:
Tao Bao5f8dd992017-07-28 00:05:40 -0700363 touch_finger_down_ = true;
Tom Marshall7e2c6002020-03-10 20:17:49 +0100364 touch_saw_y_ = true;
365 touch_pos_.y(ev.value);
366 if (touch_reported_ && touch_saw_x_) {
367 OnTouchTrack();
368 touch_saw_x_ = touch_saw_y_ = false;
369 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700370 break;
371
372 case ABS_MT_TRACKING_ID:
373 // Protocol B: -1 marks lifting the contact.
374 if (ev.value < 0) touch_finger_down_ = false;
375 break;
376 }
377 return 0;
378 }
379
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700380 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700381 if (touch_screen_allowed_) {
382 if (ev.code == BTN_TOUCH) {
383 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
384 // lifting the contact.
385 touch_finger_down_ = (ev.value == 1);
386 }
387
388 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
389 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
390 // KEY_POWER and KEY_UP as KEY_DOWN).
391 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
392 return 0;
393 }
394 }
395
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700396 ProcessKey(ev.code, ev.value);
397 }
398
399 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700400}
401
Tao Bao26ea9592018-05-09 16:32:02 -0700402// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
403// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
404// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
405// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700406//
Tao Bao26ea9592018-05-09 16:32:02 -0700407// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
408// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700409//
410// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700411void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700412 bool register_key = false;
413 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800414
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700415 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100416 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700417 key_pressed[key_code] = updown;
418 if (updown) {
419 ++key_down_count;
420 key_last_down = key_code;
421 key_long_press = false;
422 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
423 time_key_thread.detach();
424 } else {
425 if (key_last_down == key_code) {
426 long_press = key_long_press;
427 register_key = true;
428 }
429 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700430 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700431 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700432
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700433 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700434 if (register_key) {
435 switch (CheckKey(key_code, long_press)) {
436 case RecoveryUI::IGNORE:
437 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800438
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700439 case RecoveryUI::TOGGLE:
440 ShowText(!IsTextVisible());
441 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700442
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700443 case RecoveryUI::REBOOT:
444 if (reboot_enabled) {
445 reboot("reboot,");
446 while (true) {
447 pause();
448 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700449 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700450 break;
451
452 case RecoveryUI::ENQUEUE:
453 EnqueueKey(key_code);
454 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700455 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700456 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700457}
458
Tao Bao26ea9592018-05-09 16:32:02 -0700459void RecoveryUI::TimeKey(int key_code, int count) {
460 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700461 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700462 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100463 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700464 if (key_last_down == key_code && key_down_count == count) {
465 long_press = key_long_press = true;
466 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700467 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700468 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700469}
470
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800471void RecoveryUI::EnqueueKey(int key_code) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100472 std::lock_guard<std::mutex> lg(event_queue_mutex);
473 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
474 if (event_queue_len < queue_max) {
475 InputEvent event(key_code);
476 event_queue[event_queue_len++] = event;
477 event_queue_cond.notify_one();
478 }
479}
480
481void RecoveryUI::EnqueueTouch(const Point& pos) {
482 std::lock_guard<std::mutex> lg(event_queue_mutex);
483 const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]);
484 if (event_queue_len < queue_max) {
485 InputEvent event(pos);
486 event_queue[event_queue_len++] = event;
487 event_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800489}
490
Jerry Zhangb76af932018-05-22 12:08:35 -0700491void RecoveryUI::SetScreensaverState(ScreensaverState state) {
492 switch (state) {
493 case ScreensaverState::NORMAL:
494 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
495 brightness_file_)) {
496 screensaver_state_ = ScreensaverState::NORMAL;
497 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
498 << "%)";
499 } else {
500 LOG(ERROR) << "Unable to set brightness to normal";
501 }
502 break;
503 case ScreensaverState::DIMMED:
504 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
505 brightness_file_)) {
506 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
507 << "%)";
508 screensaver_state_ = ScreensaverState::DIMMED;
509 } else {
510 LOG(ERROR) << "Unable to set brightness to dim";
511 }
512 break;
513 case ScreensaverState::OFF:
514 if (android::base::WriteStringToFile("0", brightness_file_)) {
515 LOG(INFO) << "Brightness: 0 (off)";
516 screensaver_state_ = ScreensaverState::OFF;
517 } else {
518 LOG(ERROR) << "Unable to set brightness to off";
519 }
520 break;
521 default:
522 LOG(ERROR) << "Invalid screensaver state";
523 }
524}
525
Tom Marshall7e2c6002020-03-10 20:17:49 +0100526RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() {
527 std::unique_lock<std::mutex> lk(event_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700528
Jerry Zhangb76af932018-05-22 12:08:35 -0700529 // Check for a saved key queue interruption.
530 if (key_interrupted_) {
531 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall7e2c6002020-03-10 20:17:49 +0100532 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Jerry Zhangb76af932018-05-22 12:08:35 -0700533 }
534
Tao Bao53be3322018-08-16 11:48:50 -0700535 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800536 do {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100537 bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
538 return this->event_queue_len != 0 || key_interrupted_;
Jerry Zhangb76af932018-05-22 12:08:35 -0700539 });
540 if (key_interrupted_) {
541 SetScreensaverState(ScreensaverState::NORMAL);
Tom Marshall7e2c6002020-03-10 20:17:49 +0100542 return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700543 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800544 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700545 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700546 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800547 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700548 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800549 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700550 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800551 }
Tao Bao53be3322018-08-16 11:48:50 -0700552 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800553 // Drop the first key if it's changing from OFF to NORMAL.
554 if (screensaver_state_ == ScreensaverState::OFF) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100555 if (event_queue_len > 0) {
556 memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800557 }
558 }
559
560 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700561 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800562 }
563 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100564 } while (IsUsbConnected() && event_queue_len == 0);
Tao Bao6278bdf2017-01-16 17:38:18 -0800565
Tom Marshall7e2c6002020-03-10 20:17:49 +0100566 InputEvent event;
567 if (event_queue_len > 0) {
568 event = event_queue[0];
569 memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len);
Tao Bao6278bdf2017-01-16 17:38:18 -0800570 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100571 return event;
Doug Zongker32a0a472011-11-01 11:00:20 -0700572}
573
Tom Marshalla9ac9552018-12-17 15:57:44 -0800574void RecoveryUI::CancelWaitKey() {
575 EnqueueKey(KEY_REFRESH);
576}
577
Jerry Zhangb76af932018-05-22 12:08:35 -0700578void RecoveryUI::InterruptKey() {
579 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100580 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb76af932018-05-22 12:08:35 -0700581 key_interrupted_ = true;
582 }
Tom Marshall7e2c6002020-03-10 20:17:49 +0100583 event_queue_cond.notify_one();
Jerry Zhangb76af932018-05-22 12:08:35 -0700584}
585
Elliott Hughes985022a2015-04-13 13:04:32 -0700586bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700587 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
588 if (fd < 0) {
589 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
590 return 0;
591 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700592
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700593 char buf;
594 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
595 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
596 if (close(fd) < 0) {
597 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
598 }
599 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700600}
601
Elliott Hughesec283402015-04-10 10:01:53 -0700602bool RecoveryUI::IsKeyPressed(int key) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100603 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700604 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700605 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700606}
607
Elliott Hughes642aaa72015-04-10 12:47:46 -0700608bool RecoveryUI::IsLongPress() {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100609 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700610 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700611 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700612}
613
Elliott Hughes4af215b2015-04-10 15:00:34 -0700614bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700615 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700616}
617
Tao Bao5f8dd992017-07-28 00:05:40 -0700618bool RecoveryUI::HasPowerKey() const {
619 return has_power_key;
620}
621
622bool RecoveryUI::HasTouchScreen() const {
623 return has_touch_screen;
624}
625
Doug Zongker32a0a472011-11-01 11:00:20 -0700626void RecoveryUI::FlushKeys() {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100627 std::lock_guard<std::mutex> lg(event_queue_mutex);
628 event_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700629}
630
Elliott Hughes642aaa72015-04-10 12:47:46 -0700631RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700632 {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100633 std::lock_guard<std::mutex> lg(event_queue_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700634 key_long_press = false;
635 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700636
637 // 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 -0700638 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
639 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700640 return TOGGLE;
641 }
642 } else {
643 // Otherwise long press of any button toggles to the text display,
644 // and there's no way to toggle back (but that's pretty useless anyway).
645 if (is_long_press && !IsTextVisible()) {
646 return TOGGLE;
647 }
648
649 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
650 if (is_long_press && IsTextVisible()) {
651 EnqueueKey(KEY_ENTER);
652 return IGNORE;
653 }
654 }
655
656 // Press power seven times in a row to reboot.
657 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700658 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700659
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700660 if (reboot_enabled) {
661 ++consecutive_power_keys;
662 if (consecutive_power_keys >= 7) {
663 return REBOOT;
664 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700665 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700666 } else {
667 consecutive_power_keys = 0;
668 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700669
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700670 last_key = key;
671 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700672}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800673
Tianjie Xub5108c32018-08-20 13:40:47 -0700674void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700675
676void RecoveryUI::SetEnableReboot(bool enabled) {
Tom Marshall7e2c6002020-03-10 20:17:49 +0100677 std::lock_guard<std::mutex> lg(event_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700678 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700679}