blob: eb87f52617a059fff9efac36a3beda1e0be37f99 [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>
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";
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -070051constexpr const char* BRIGHTNESS_FILE_PWM =
52 "/sys/class/backlight/pwm-backlight.0/brightness";
53constexpr const char* MAX_BRIGHTNESS_FILE_PWM =
54 "/sys/class/backlight/pwm-backlight.0/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070055
Tao Bao0bc88de2018-07-31 14:53:16 -070056constexpr int kDefaultTouchLowThreshold = 50;
57constexpr int kDefaultTouchHighThreshold = 90;
58
Elliott Hughesec283402015-04-10 10:01:53 -070059RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080060 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080061 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080062 brightness_file_(BRIGHTNESS_FILE),
63 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tao Bao5f8dd992017-07-28 00:05:40 -070064 touch_screen_allowed_(false),
David Anderson983e2d52019-01-02 11:35:38 -080065 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070066 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
67 kDefaultTouchLowThreshold)),
68 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
69 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070070 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080071 key_queue_len(0),
72 key_last_down(-1),
73 key_long_press(false),
74 key_down_count(0),
75 enable_reboot(true),
76 consecutive_power_keys(0),
Tao Bao736d59c2017-01-03 10:15:33 -080077 has_power_key(false),
78 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080079 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070080 has_touch_screen(false),
81 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070082 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080083 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080084 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070085}
86
Tao Bao26ea9592018-05-09 16:32:02 -070087RecoveryUI::~RecoveryUI() {
88 ev_exit();
89 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070090 if (input_thread_.joinable()) {
91 input_thread_.join();
92 }
Tao Bao26ea9592018-05-09 16:32:02 -070093}
94
Elliott Hughes642aaa72015-04-10 12:47:46 -070095void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070096 if (key_code == KEY_POWER) {
97 has_power_key = true;
98 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
99 has_down_key = true;
100 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
101 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -0700102 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
103 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700104 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700105}
106
Tao Bao6278bdf2017-01-16 17:38:18 -0800107bool RecoveryUI::InitScreensaver() {
108 // Disabled.
109 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
110 return false;
111 }
kataoc35c1b02017-12-08 11:02:43 +0800112 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700113 if (!access(BRIGHTNESS_FILE_SDM, R_OK | W_OK)) {
114 brightness_file_ = BRIGHTNESS_FILE_SDM;
115 } else {
116 brightness_file_ = BRIGHTNESS_FILE_PWM;
117 }
kataoc35c1b02017-12-08 11:02:43 +0800118 }
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700119
kataoc35c1b02017-12-08 11:02:43 +0800120 if (access(max_brightness_file_.c_str(), R_OK)) {
Lincoln Atkinsonfcf4b6b2020-03-09 15:22:29 -0700121 if (!access(MAX_BRIGHTNESS_FILE_SDM, R_OK)) {
122 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
123 } else {
124 max_brightness_file_ = MAX_BRIGHTNESS_FILE_PWM;
125 }
kataoc35c1b02017-12-08 11:02:43 +0800126 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800127 // Set the initial brightness level based on the max brightness. Note that reading the initial
128 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
129 // we don't have a good way to query the default value.
130 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800131 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800132 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800133 return false;
134 }
135
136 unsigned int max_value;
137 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
138 LOG(WARNING) << "Failed to parse max brightness: " << content;
139 return false;
140 }
141
142 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
143 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
144 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800145 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800146 PLOG(WARNING) << "Failed to set brightness";
147 return false;
148 }
149
150 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
151 screensaver_state_ = ScreensaverState::NORMAL;
152 return true;
153}
154
Tao Baoefb49ad2017-01-31 23:03:10 -0800155bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700156 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
157 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700158
Tao Bao736d59c2017-01-03 10:15:33 -0800159 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
160
Tao Bao5f8dd992017-07-28 00:05:40 -0700161 if (touch_screen_allowed_) {
162 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700163
164 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
165 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
166 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
167 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
168 // mode will be turned on automatically on debuggable builds, even without a swipe.
169 std::string cmdline;
170 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
171 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
172 } else {
173 // Non-fatal, and won't affect Init() result.
174 PLOG(WARNING) << "Failed to read /proc/cmdline";
175 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700176 }
177
Tao Bao6278bdf2017-01-16 17:38:18 -0800178 if (!InitScreensaver()) {
179 LOG(INFO) << "Screensaver disabled";
180 }
181
Tao Bao26ea9592018-05-09 16:32:02 -0700182 // Create a separate thread that handles input events.
183 input_thread_ = std::thread([this]() {
184 while (!this->input_thread_stopped_) {
185 if (!ev_wait(500)) {
186 ev_dispatch();
187 }
188 }
189 });
190
Tao Bao736d59c2017-01-03 10:15:33 -0800191 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700192}
193
Tao Bao5f8dd992017-07-28 00:05:40 -0700194void RecoveryUI::OnTouchDetected(int dx, int dy) {
195 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
196
197 // We only consider a valid swipe if:
Tao Bao0bc88de2018-07-31 14:53:16 -0700198 // - the delta along one axis is below touch_low_threshold_;
199 // - and the delta along the other axis is beyond touch_high_threshold_.
200 if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700201 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
Tao Bao0bc88de2018-07-31 14:53:16 -0700202 } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700203 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
204 } else {
Tao Bao0bc88de2018-07-31 14:53:16 -0700205 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_
206 << ", high: " << touch_high_threshold_ << ")";
Tao Bao5f8dd992017-07-28 00:05:40 -0700207 return;
208 }
209
Tao Bao046aae22017-07-31 23:15:09 -0700210 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
211 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
212 ShowText(true);
213 return;
214 }
215
Tao Bao5f8dd992017-07-28 00:05:40 -0700216 LOG(DEBUG) << "Swipe direction=" << direction;
217 switch (direction) {
218 case SwipeDirection::UP:
219 ProcessKey(KEY_UP, 1); // press up key
220 ProcessKey(KEY_UP, 0); // and release it
221 break;
222
223 case SwipeDirection::DOWN:
224 ProcessKey(KEY_DOWN, 1); // press down key
225 ProcessKey(KEY_DOWN, 0); // and release it
226 break;
227
228 case SwipeDirection::LEFT:
229 case SwipeDirection::RIGHT:
230 ProcessKey(KEY_POWER, 1); // press power key
231 ProcessKey(KEY_POWER, 0); // and release it
232 break;
233 };
234}
235
Elliott Hughes985022a2015-04-13 13:04:32 -0700236int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700237 struct input_event ev;
238 if (ev_get_input(fd, epevents, &ev) == -1) {
239 return -1;
240 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700241
Tao Bao5f8dd992017-07-28 00:05:40 -0700242 // Touch inputs handling.
243 //
244 // We handle the touch inputs by tracking the position changes between initial contacting and
245 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
246 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
247 //
248 // Per the doc Multi-touch Protocol at below, there are two protocols.
249 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
250 //
251 // The main difference between the stateless type A protocol and the stateful type B slot protocol
252 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
253 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
254 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
255 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
256 //
257 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
258 // ABS_MT_TRACKING_ID being -1.
259 //
260 // Touch input events will only be available if touch_screen_allowed_ is set.
261
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700262 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700263 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
264 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
265 // contact.
266 if (touch_finger_down_ && !touch_swiping_) {
267 touch_start_X_ = touch_X_;
268 touch_start_Y_ = touch_Y_;
269 touch_swiping_ = true;
270 } else if (!touch_finger_down_ && touch_swiping_) {
271 touch_swiping_ = false;
272 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
273 }
274 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700275 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700276 }
277
278 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700279 if (ev.code == REL_Y) {
280 // accumulate the up or down motion reported by
281 // the trackball. When it exceeds a threshold
282 // (positive or negative), fake an up/down
283 // key event.
284 rel_sum += ev.value;
285 if (rel_sum > 3) {
286 ProcessKey(KEY_DOWN, 1); // press down key
287 ProcessKey(KEY_DOWN, 0); // and release it
288 rel_sum = 0;
289 } else if (rel_sum < -3) {
290 ProcessKey(KEY_UP, 1); // press up key
291 ProcessKey(KEY_UP, 0); // and release it
292 rel_sum = 0;
293 }
294 }
295 } else {
296 rel_sum = 0;
297 }
298
Tao Bao5f8dd992017-07-28 00:05:40 -0700299 if (touch_screen_allowed_ && ev.type == EV_ABS) {
300 if (ev.code == ABS_MT_SLOT) {
301 touch_slot_ = ev.value;
302 }
303 // Ignore other fingers.
304 if (touch_slot_ > 0) return 0;
305
306 switch (ev.code) {
307 case ABS_MT_POSITION_X:
308 touch_X_ = ev.value;
309 touch_finger_down_ = true;
310 break;
311
312 case ABS_MT_POSITION_Y:
313 touch_Y_ = ev.value;
314 touch_finger_down_ = true;
315 break;
316
317 case ABS_MT_TRACKING_ID:
318 // Protocol B: -1 marks lifting the contact.
319 if (ev.value < 0) touch_finger_down_ = false;
320 break;
321 }
322 return 0;
323 }
324
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700325 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700326 if (touch_screen_allowed_) {
327 if (ev.code == BTN_TOUCH) {
328 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
329 // lifting the contact.
330 touch_finger_down_ = (ev.value == 1);
331 }
332
333 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
334 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
335 // KEY_POWER and KEY_UP as KEY_DOWN).
336 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
337 return 0;
338 }
339 }
340
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700341 ProcessKey(ev.code, ev.value);
342 }
343
fredchiouf2f5aa22022-02-11 16:39:53 +0800344 // For Lid switch handle
345 if (ev.type == EV_SW) {
346 SetSwCallback(ev.code, ev.value);
347 }
348
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700349 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700350}
351
Tao Bao26ea9592018-05-09 16:32:02 -0700352// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
353// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
354// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
355// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700356//
Tao Bao26ea9592018-05-09 16:32:02 -0700357// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
358// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700359//
360// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700361void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700362 bool register_key = false;
363 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800364
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700365 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700366 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700367 key_pressed[key_code] = updown;
368 if (updown) {
369 ++key_down_count;
370 key_last_down = key_code;
371 key_long_press = false;
372 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
373 time_key_thread.detach();
374 } else {
375 if (key_last_down == key_code) {
376 long_press = key_long_press;
377 register_key = true;
378 }
379 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700380 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700381 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700382
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700383 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700384 if (register_key) {
385 switch (CheckKey(key_code, long_press)) {
386 case RecoveryUI::IGNORE:
387 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800388
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700389 case RecoveryUI::TOGGLE:
390 ShowText(!IsTextVisible());
391 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700392
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700393 case RecoveryUI::REBOOT:
394 if (reboot_enabled) {
Mark Salyzyn488cc052019-05-20 10:36:16 -0700395 Reboot("userrequested,recovery,ui");
Doug Zongker32a0a472011-11-01 11:00:20 -0700396 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700397 break;
398
399 case RecoveryUI::ENQUEUE:
400 EnqueueKey(key_code);
401 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700402 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700403 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700404}
405
Tao Bao26ea9592018-05-09 16:32:02 -0700406void RecoveryUI::TimeKey(int key_code, int count) {
407 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700408 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700409 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700410 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700411 if (key_last_down == key_code && key_down_count == count) {
412 long_press = key_long_press = true;
413 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700414 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700415 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700416}
417
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800418void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700419 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700420 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
421 if (key_queue_len < queue_max) {
422 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700423 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700424 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800425}
426
Jerry Zhangb76af932018-05-22 12:08:35 -0700427void RecoveryUI::SetScreensaverState(ScreensaverState state) {
428 switch (state) {
429 case ScreensaverState::NORMAL:
430 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
431 brightness_file_)) {
432 screensaver_state_ = ScreensaverState::NORMAL;
433 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
434 << "%)";
435 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800436 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700437 }
438 break;
439 case ScreensaverState::DIMMED:
440 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
441 brightness_file_)) {
442 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
443 << "%)";
444 screensaver_state_ = ScreensaverState::DIMMED;
445 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800446 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700447 }
448 break;
449 case ScreensaverState::OFF:
450 if (android::base::WriteStringToFile("0", brightness_file_)) {
451 LOG(INFO) << "Brightness: 0 (off)";
452 screensaver_state_ = ScreensaverState::OFF;
453 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800454 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700455 }
456 break;
457 default:
458 LOG(ERROR) << "Invalid screensaver state";
459 }
460}
461
Elliott Hughesec283402015-04-10 10:01:53 -0700462int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700463 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700464
Jerry Zhangb76af932018-05-22 12:08:35 -0700465 // Check for a saved key queue interruption.
466 if (key_interrupted_) {
467 SetScreensaverState(ScreensaverState::NORMAL);
468 return static_cast<int>(KeyError::INTERRUPTED);
469 }
470
Tao Bao53be3322018-08-16 11:48:50 -0700471 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800472 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700473 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
474 return this->key_queue_len != 0 || key_interrupted_;
475 });
476 if (key_interrupted_) {
477 SetScreensaverState(ScreensaverState::NORMAL);
478 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700479 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800480 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700481 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700482 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800483 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700484 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800485 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700486 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800487 }
Tao Bao53be3322018-08-16 11:48:50 -0700488 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800489 // Drop the first key if it's changing from OFF to NORMAL.
490 if (screensaver_state_ == ScreensaverState::OFF) {
491 if (key_queue_len > 0) {
492 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
493 }
494 }
495
496 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700497 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800498 }
499 }
500 } while (IsUsbConnected() && key_queue_len == 0);
501
Jerry Zhangb76af932018-05-22 12:08:35 -0700502 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800503 if (key_queue_len > 0) {
504 key = key_queue[0];
505 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
506 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800507 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700508}
509
Jerry Zhangb76af932018-05-22 12:08:35 -0700510void RecoveryUI::InterruptKey() {
511 {
512 std::lock_guard<std::mutex> lg(key_queue_mutex);
513 key_interrupted_ = true;
514 }
515 key_queue_cond.notify_one();
516}
517
Elliott Hughes985022a2015-04-13 13:04:32 -0700518bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700519 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
520 if (fd < 0) {
521 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
522 return 0;
523 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700524
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700525 char buf;
526 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
527 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
528 if (close(fd) < 0) {
529 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
530 }
531 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700532}
533
Elliott Hughesec283402015-04-10 10:01:53 -0700534bool RecoveryUI::IsKeyPressed(int key) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700535 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700536 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700537 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700538}
539
Elliott Hughes642aaa72015-04-10 12:47:46 -0700540bool RecoveryUI::IsLongPress() {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700541 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700542 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700543 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700544}
545
Tianjie Xue5032212019-07-23 13:23:29 -0700546bool RecoveryUI::HasThreeButtons() const {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700547 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700548}
549
Tao Bao5f8dd992017-07-28 00:05:40 -0700550bool RecoveryUI::HasPowerKey() const {
551 return has_power_key;
552}
553
554bool RecoveryUI::HasTouchScreen() const {
555 return has_touch_screen;
556}
557
Doug Zongker32a0a472011-11-01 11:00:20 -0700558void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700559 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700560 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700561}
562
Elliott Hughes642aaa72015-04-10 12:47:46 -0700563RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700564 {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700565 std::lock_guard<std::mutex> lg(key_press_mutex);
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700566 key_long_press = false;
567 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700568
569 // 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 -0700570 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
571 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700572 return TOGGLE;
573 }
574 } else {
575 // Otherwise long press of any button toggles to the text display,
576 // and there's no way to toggle back (but that's pretty useless anyway).
577 if (is_long_press && !IsTextVisible()) {
578 return TOGGLE;
579 }
580
581 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
582 if (is_long_press && IsTextVisible()) {
583 EnqueueKey(KEY_ENTER);
584 return IGNORE;
585 }
586 }
587
588 // Press power seven times in a row to reboot.
589 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700590 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700591
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700592 if (reboot_enabled) {
593 ++consecutive_power_keys;
594 if (consecutive_power_keys >= 7) {
595 return REBOOT;
596 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700597 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700598 } else {
599 consecutive_power_keys = 0;
600 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700601
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700602 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700603}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800604
Tianjie Xu8f397302018-08-20 13:40:47 -0700605void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700606
607void RecoveryUI::SetEnableReboot(bool enabled) {
Tianjie Xub8a959b2019-06-14 15:35:31 -0700608 std::lock_guard<std::mutex> lg(key_press_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700609 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700610}