blob: a2c160f7a7ff06326b0f5fe33b69c769daae3fa7 [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
Tao Bao736d59c2017-01-03 10:15:33 -080017#include "ui.h"
18
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 Bao6278bdf2017-01-16 17:38:18 -080037#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070038
Tao Bao26ea9592018-05-09 16:32:02 -070039#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070040#include "otautil/sysutil.h"
41#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
Tao Bao26ea9592018-05-09 16:32:02 -070043using namespace std::chrono_literals;
44
Tao Bao6278bdf2017-01-16 17:38:18 -080045static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
46static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
kataoc35c1b02017-12-08 11:02:43 +080048static constexpr const char* BRIGHTNESS_FILE_SDM =
49 "/sys/class/backlight/panel0-backlight/brightness";
50static constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
51 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070052
Elliott Hughesec283402015-04-10 10:01:53 -070053RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080054 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080055 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080056 brightness_file_(BRIGHTNESS_FILE),
57 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tao Bao5f8dd992017-07-28 00:05:40 -070058 touch_screen_allowed_(false),
59 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
60 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Jerry Zhangb76af932018-05-22 12:08:35 -070061 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080062 key_queue_len(0),
63 key_last_down(-1),
64 key_long_press(false),
65 key_down_count(0),
66 enable_reboot(true),
67 consecutive_power_keys(0),
68 last_key(-1),
69 has_power_key(false),
70 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080071 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070072 has_touch_screen(false),
73 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070074 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080075 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080076 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070077}
78
Tao Bao26ea9592018-05-09 16:32:02 -070079RecoveryUI::~RecoveryUI() {
80 ev_exit();
81 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070082 if (input_thread_.joinable()) {
83 input_thread_.join();
84 }
Tao Bao26ea9592018-05-09 16:32:02 -070085}
86
Elliott Hughes642aaa72015-04-10 12:47:46 -070087void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070088 if (key_code == KEY_POWER) {
89 has_power_key = true;
90 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
91 has_down_key = true;
92 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
93 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070094 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
95 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070096 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070097}
98
Tao Bao6278bdf2017-01-16 17:38:18 -080099bool RecoveryUI::InitScreensaver() {
100 // Disabled.
101 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
102 return false;
103 }
kataoc35c1b02017-12-08 11:02:43 +0800104 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
105 brightness_file_ = BRIGHTNESS_FILE_SDM;
106 }
107 if (access(max_brightness_file_.c_str(), R_OK)) {
108 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
109 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800110 // Set the initial brightness level based on the max brightness. Note that reading the initial
111 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
112 // we don't have a good way to query the default value.
113 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800114 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800115 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800116 return false;
117 }
118
119 unsigned int max_value;
120 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
121 LOG(WARNING) << "Failed to parse max brightness: " << content;
122 return false;
123 }
124
125 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
126 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
127 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800128 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800129 PLOG(WARNING) << "Failed to set brightness";
130 return false;
131 }
132
133 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
134 screensaver_state_ = ScreensaverState::NORMAL;
135 return true;
136}
137
Tao Baoefb49ad2017-01-31 23:03:10 -0800138bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700139 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
140 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700141
Tao Bao736d59c2017-01-03 10:15:33 -0800142 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
143
Tao Bao5f8dd992017-07-28 00:05:40 -0700144 if (touch_screen_allowed_) {
145 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700146
147 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
148 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
149 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
150 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
151 // mode will be turned on automatically on debuggable builds, even without a swipe.
152 std::string cmdline;
153 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
154 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
155 } else {
156 // Non-fatal, and won't affect Init() result.
157 PLOG(WARNING) << "Failed to read /proc/cmdline";
158 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700159 }
160
Tao Bao6278bdf2017-01-16 17:38:18 -0800161 if (!InitScreensaver()) {
162 LOG(INFO) << "Screensaver disabled";
163 }
164
Tao Bao26ea9592018-05-09 16:32:02 -0700165 // Create a separate thread that handles input events.
166 input_thread_ = std::thread([this]() {
167 while (!this->input_thread_stopped_) {
168 if (!ev_wait(500)) {
169 ev_dispatch();
170 }
171 }
172 });
173
Tao Bao736d59c2017-01-03 10:15:33 -0800174 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700175}
176
Tao Bao5f8dd992017-07-28 00:05:40 -0700177void RecoveryUI::OnTouchDetected(int dx, int dy) {
178 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
179
180 // We only consider a valid swipe if:
181 // - the delta along one axis is below kTouchLowThreshold;
182 // - and the delta along the other axis is beyond kTouchHighThreshold.
183 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
184 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
185 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
186 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
187 } else {
188 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
189 << ", high: " << kTouchHighThreshold << ")";
190 return;
191 }
192
Tao Bao046aae22017-07-31 23:15:09 -0700193 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
194 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
195 ShowText(true);
196 return;
197 }
198
Tao Bao5f8dd992017-07-28 00:05:40 -0700199 LOG(DEBUG) << "Swipe direction=" << direction;
200 switch (direction) {
201 case SwipeDirection::UP:
202 ProcessKey(KEY_UP, 1); // press up key
203 ProcessKey(KEY_UP, 0); // and release it
204 break;
205
206 case SwipeDirection::DOWN:
207 ProcessKey(KEY_DOWN, 1); // press down key
208 ProcessKey(KEY_DOWN, 0); // and release it
209 break;
210
211 case SwipeDirection::LEFT:
212 case SwipeDirection::RIGHT:
213 ProcessKey(KEY_POWER, 1); // press power key
214 ProcessKey(KEY_POWER, 0); // and release it
215 break;
216 };
217}
218
Elliott Hughes985022a2015-04-13 13:04:32 -0700219int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700220 struct input_event ev;
221 if (ev_get_input(fd, epevents, &ev) == -1) {
222 return -1;
223 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700224
Tao Bao5f8dd992017-07-28 00:05:40 -0700225 // Touch inputs handling.
226 //
227 // We handle the touch inputs by tracking the position changes between initial contacting and
228 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
229 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
230 //
231 // Per the doc Multi-touch Protocol at below, there are two protocols.
232 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
233 //
234 // The main difference between the stateless type A protocol and the stateful type B slot protocol
235 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
236 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
237 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
238 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
239 //
240 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
241 // ABS_MT_TRACKING_ID being -1.
242 //
243 // Touch input events will only be available if touch_screen_allowed_ is set.
244
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700246 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
247 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
248 // contact.
249 if (touch_finger_down_ && !touch_swiping_) {
250 touch_start_X_ = touch_X_;
251 touch_start_Y_ = touch_Y_;
252 touch_swiping_ = true;
253 } else if (!touch_finger_down_ && touch_swiping_) {
254 touch_swiping_ = false;
255 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
256 }
257 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700258 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700259 }
260
261 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700262 if (ev.code == REL_Y) {
263 // accumulate the up or down motion reported by
264 // the trackball. When it exceeds a threshold
265 // (positive or negative), fake an up/down
266 // key event.
267 rel_sum += ev.value;
268 if (rel_sum > 3) {
269 ProcessKey(KEY_DOWN, 1); // press down key
270 ProcessKey(KEY_DOWN, 0); // and release it
271 rel_sum = 0;
272 } else if (rel_sum < -3) {
273 ProcessKey(KEY_UP, 1); // press up key
274 ProcessKey(KEY_UP, 0); // and release it
275 rel_sum = 0;
276 }
277 }
278 } else {
279 rel_sum = 0;
280 }
281
Tao Bao5f8dd992017-07-28 00:05:40 -0700282 if (touch_screen_allowed_ && ev.type == EV_ABS) {
283 if (ev.code == ABS_MT_SLOT) {
284 touch_slot_ = ev.value;
285 }
286 // Ignore other fingers.
287 if (touch_slot_ > 0) return 0;
288
289 switch (ev.code) {
290 case ABS_MT_POSITION_X:
291 touch_X_ = ev.value;
292 touch_finger_down_ = true;
293 break;
294
295 case ABS_MT_POSITION_Y:
296 touch_Y_ = ev.value;
297 touch_finger_down_ = true;
298 break;
299
300 case ABS_MT_TRACKING_ID:
301 // Protocol B: -1 marks lifting the contact.
302 if (ev.value < 0) touch_finger_down_ = false;
303 break;
304 }
305 return 0;
306 }
307
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700308 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700309 if (touch_screen_allowed_) {
310 if (ev.code == BTN_TOUCH) {
311 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
312 // lifting the contact.
313 touch_finger_down_ = (ev.value == 1);
314 }
315
316 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
317 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
318 // KEY_POWER and KEY_UP as KEY_DOWN).
319 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
320 return 0;
321 }
322 }
323
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700324 ProcessKey(ev.code, ev.value);
325 }
326
327 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700328}
329
Tao Bao26ea9592018-05-09 16:32:02 -0700330// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
331// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
332// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
333// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700334//
Tao Bao26ea9592018-05-09 16:32:02 -0700335// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
336// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700337//
338// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700339void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700340 bool register_key = false;
341 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800342
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700343 {
344 std::lock_guard<std::mutex> lg(key_queue_mutex);
345 key_pressed[key_code] = updown;
346 if (updown) {
347 ++key_down_count;
348 key_last_down = key_code;
349 key_long_press = false;
350 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
351 time_key_thread.detach();
352 } else {
353 if (key_last_down == key_code) {
354 long_press = key_long_press;
355 register_key = true;
356 }
357 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700358 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700359 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700360
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700361 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700362 if (register_key) {
363 switch (CheckKey(key_code, long_press)) {
364 case RecoveryUI::IGNORE:
365 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800366
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700367 case RecoveryUI::TOGGLE:
368 ShowText(!IsTextVisible());
369 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700370
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700371 case RecoveryUI::REBOOT:
372 if (reboot_enabled) {
373 reboot("reboot,");
374 while (true) {
375 pause();
376 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700377 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700378 break;
379
380 case RecoveryUI::ENQUEUE:
381 EnqueueKey(key_code);
382 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700383 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700384 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700385}
386
Tao Bao26ea9592018-05-09 16:32:02 -0700387void RecoveryUI::TimeKey(int key_code, int count) {
388 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700389 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700390 {
391 std::lock_guard<std::mutex> lg(key_queue_mutex);
392 if (key_last_down == key_code && key_down_count == count) {
393 long_press = key_long_press = true;
394 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700395 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700396 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700397}
398
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800399void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700400 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700401 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
402 if (key_queue_len < queue_max) {
403 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700404 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700405 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800406}
407
Jerry Zhangb76af932018-05-22 12:08:35 -0700408void RecoveryUI::SetScreensaverState(ScreensaverState state) {
409 switch (state) {
410 case ScreensaverState::NORMAL:
411 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
412 brightness_file_)) {
413 screensaver_state_ = ScreensaverState::NORMAL;
414 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
415 << "%)";
416 } else {
417 LOG(ERROR) << "Unable to set brightness to normal";
418 }
419 break;
420 case ScreensaverState::DIMMED:
421 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
422 brightness_file_)) {
423 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
424 << "%)";
425 screensaver_state_ = ScreensaverState::DIMMED;
426 } else {
427 LOG(ERROR) << "Unable to set brightness to dim";
428 }
429 break;
430 case ScreensaverState::OFF:
431 if (android::base::WriteStringToFile("0", brightness_file_)) {
432 LOG(INFO) << "Brightness: 0 (off)";
433 screensaver_state_ = ScreensaverState::OFF;
434 } else {
435 LOG(ERROR) << "Unable to set brightness to off";
436 }
437 break;
438 default:
439 LOG(ERROR) << "Invalid screensaver state";
440 }
441}
442
Elliott Hughesec283402015-04-10 10:01:53 -0700443int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700444 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700445
Jerry Zhangb76af932018-05-22 12:08:35 -0700446 // Check for a saved key queue interruption.
447 if (key_interrupted_) {
448 SetScreensaverState(ScreensaverState::NORMAL);
449 return static_cast<int>(KeyError::INTERRUPTED);
450 }
451
Tao Bao6278bdf2017-01-16 17:38:18 -0800452 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
453 // plugged in.
454 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700455 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
456 return this->key_queue_len != 0 || key_interrupted_;
457 });
458 if (key_interrupted_) {
459 SetScreensaverState(ScreensaverState::NORMAL);
460 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700461 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800462 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700463 if (!rc) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800464 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
465 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700466 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800467 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700468 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800469 }
Jerry Zhangb76af932018-05-22 12:08:35 -0700470 } else {
Tao Bao6278bdf2017-01-16 17:38:18 -0800471 // Drop the first key if it's changing from OFF to NORMAL.
472 if (screensaver_state_ == ScreensaverState::OFF) {
473 if (key_queue_len > 0) {
474 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
475 }
476 }
477
478 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700479 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800480 }
481 }
482 } while (IsUsbConnected() && key_queue_len == 0);
483
Jerry Zhangb76af932018-05-22 12:08:35 -0700484 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800485 if (key_queue_len > 0) {
486 key = key_queue[0];
487 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
488 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800489 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700490}
491
Jerry Zhangb76af932018-05-22 12:08:35 -0700492void RecoveryUI::InterruptKey() {
493 {
494 std::lock_guard<std::mutex> lg(key_queue_mutex);
495 key_interrupted_ = true;
496 }
497 key_queue_cond.notify_one();
498}
499
Elliott Hughes985022a2015-04-13 13:04:32 -0700500bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700501 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
502 if (fd < 0) {
503 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
504 return 0;
505 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700506
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700507 char buf;
508 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
509 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
510 if (close(fd) < 0) {
511 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
512 }
513 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700514}
515
Elliott Hughesec283402015-04-10 10:01:53 -0700516bool RecoveryUI::IsKeyPressed(int key) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700517 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700518 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700519 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700520}
521
Elliott Hughes642aaa72015-04-10 12:47:46 -0700522bool RecoveryUI::IsLongPress() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700523 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700524 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700525 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700526}
527
Elliott Hughes4af215b2015-04-10 15:00:34 -0700528bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700529 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700530}
531
Tao Bao5f8dd992017-07-28 00:05:40 -0700532bool RecoveryUI::HasPowerKey() const {
533 return has_power_key;
534}
535
536bool RecoveryUI::HasTouchScreen() const {
537 return has_touch_screen;
538}
539
Doug Zongker32a0a472011-11-01 11:00:20 -0700540void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700541 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700542 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700543}
544
Elliott Hughes642aaa72015-04-10 12:47:46 -0700545RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700546 {
547 std::lock_guard<std::mutex> lg(key_queue_mutex);
548 key_long_press = false;
549 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700550
551 // 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 -0700552 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
553 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700554 return TOGGLE;
555 }
556 } else {
557 // Otherwise long press of any button toggles to the text display,
558 // and there's no way to toggle back (but that's pretty useless anyway).
559 if (is_long_press && !IsTextVisible()) {
560 return TOGGLE;
561 }
562
563 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
564 if (is_long_press && IsTextVisible()) {
565 EnqueueKey(KEY_ENTER);
566 return IGNORE;
567 }
568 }
569
570 // Press power seven times in a row to reboot.
571 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700572 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700573
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700574 if (reboot_enabled) {
575 ++consecutive_power_keys;
576 if (consecutive_power_keys >= 7) {
577 return REBOOT;
578 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700579 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700580 } else {
581 consecutive_power_keys = 0;
582 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700583
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700584 last_key = key;
585 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700586}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800587
Elliott Hughes642aaa72015-04-10 12:47:46 -0700588void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700589}
Doug Zongkerc704e062014-05-23 08:40:35 -0700590
591void RecoveryUI::SetEnableReboot(bool enabled) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700592 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700593 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700594}