blob: baf6d1080312c79209c96d3ad071654e5f7e9b58 [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>
21#include <linux/input.h>
22#include <pthread.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
Tao Bao9468fc02017-03-17 00:57:37 -070033#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080034#include <string>
35
Tao Bao6278bdf2017-01-16 17:38:18 -080036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070039#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080040#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070041#include <cutils/android_reboot.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080042#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070043
44#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070045#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070046#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070047
Tao Bao6278bdf2017-01-16 17:38:18 -080048static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
49static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
50static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Elliott Hughesec283402015-04-10 10:01:53 -070052RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080053 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080054 brightness_dimmed_(25),
Tao Bao5f8dd992017-07-28 00:05:40 -070055 touch_screen_allowed_(false),
56 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
57 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Tao Bao736d59c2017-01-03 10:15:33 -080058 key_queue_len(0),
59 key_last_down(-1),
60 key_long_press(false),
61 key_down_count(0),
62 enable_reboot(true),
63 consecutive_power_keys(0),
64 last_key(-1),
65 has_power_key(false),
66 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080067 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070068 has_touch_screen(false),
69 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070070 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080071 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080072 pthread_mutex_init(&key_queue_mutex, nullptr);
73 pthread_cond_init(&key_queue_cond, nullptr);
74 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070075}
76
Elliott Hughes642aaa72015-04-10 12:47:46 -070077void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070078 if (key_code == KEY_POWER) {
79 has_power_key = true;
80 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
81 has_down_key = true;
82 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
83 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070084 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
85 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070086 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070087}
88
Elliott Hughes985022a2015-04-13 13:04:32 -070089// Reads input events, handles special hot keys, and adds to the key queue.
90static void* InputThreadLoop(void*) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070091 while (true) {
92 if (!ev_wait(-1)) {
93 ev_dispatch();
Elliott Hughes985022a2015-04-13 13:04:32 -070094 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095 }
96 return nullptr;
Elliott Hughes985022a2015-04-13 13:04:32 -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 }
104
105 // Set the initial brightness level based on the max brightness. Note that reading the initial
106 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
107 // we don't have a good way to query the default value.
108 std::string content;
109 if (!android::base::ReadFileToString(MAX_BRIGHTNESS_FILE, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800110 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800111 return false;
112 }
113
114 unsigned int max_value;
115 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
116 LOG(WARNING) << "Failed to parse max brightness: " << content;
117 return false;
118 }
119
120 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
121 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
122 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
123 BRIGHTNESS_FILE)) {
124 PLOG(WARNING) << "Failed to set brightness";
125 return false;
126 }
127
128 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
129 screensaver_state_ = ScreensaverState::NORMAL;
130 return true;
131}
132
Tao Baoefb49ad2017-01-31 23:03:10 -0800133bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700134 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
135 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700136
Tao Bao736d59c2017-01-03 10:15:33 -0800137 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
138
Tao Bao5f8dd992017-07-28 00:05:40 -0700139 if (touch_screen_allowed_) {
140 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700141
142 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
143 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
144 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
145 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
146 // mode will be turned on automatically on debuggable builds, even without a swipe.
147 std::string cmdline;
148 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
149 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
150 } else {
151 // Non-fatal, and won't affect Init() result.
152 PLOG(WARNING) << "Failed to read /proc/cmdline";
153 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700154 }
155
Tao Bao6278bdf2017-01-16 17:38:18 -0800156 if (!InitScreensaver()) {
157 LOG(INFO) << "Screensaver disabled";
158 }
159
Tao Bao736d59c2017-01-03 10:15:33 -0800160 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
161 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700162}
163
Tao Bao5f8dd992017-07-28 00:05:40 -0700164void RecoveryUI::OnTouchDetected(int dx, int dy) {
165 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
166
167 // We only consider a valid swipe if:
168 // - the delta along one axis is below kTouchLowThreshold;
169 // - and the delta along the other axis is beyond kTouchHighThreshold.
170 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
171 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
172 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
173 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
174 } else {
175 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
176 << ", high: " << kTouchHighThreshold << ")";
177 return;
178 }
179
Tao Bao046aae22017-07-31 23:15:09 -0700180 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
181 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
182 ShowText(true);
183 return;
184 }
185
Tao Bao5f8dd992017-07-28 00:05:40 -0700186 LOG(DEBUG) << "Swipe direction=" << direction;
187 switch (direction) {
188 case SwipeDirection::UP:
189 ProcessKey(KEY_UP, 1); // press up key
190 ProcessKey(KEY_UP, 0); // and release it
191 break;
192
193 case SwipeDirection::DOWN:
194 ProcessKey(KEY_DOWN, 1); // press down key
195 ProcessKey(KEY_DOWN, 0); // and release it
196 break;
197
198 case SwipeDirection::LEFT:
199 case SwipeDirection::RIGHT:
200 ProcessKey(KEY_POWER, 1); // press power key
201 ProcessKey(KEY_POWER, 0); // and release it
202 break;
203 };
204}
205
Elliott Hughes985022a2015-04-13 13:04:32 -0700206int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700207 struct input_event ev;
208 if (ev_get_input(fd, epevents, &ev) == -1) {
209 return -1;
210 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700211
Tao Bao5f8dd992017-07-28 00:05:40 -0700212 // Touch inputs handling.
213 //
214 // We handle the touch inputs by tracking the position changes between initial contacting and
215 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
216 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
217 //
218 // Per the doc Multi-touch Protocol at below, there are two protocols.
219 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
220 //
221 // The main difference between the stateless type A protocol and the stateful type B slot protocol
222 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
223 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
224 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
225 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
226 //
227 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
228 // ABS_MT_TRACKING_ID being -1.
229 //
230 // Touch input events will only be available if touch_screen_allowed_ is set.
231
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700232 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700233 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
234 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
235 // contact.
236 if (touch_finger_down_ && !touch_swiping_) {
237 touch_start_X_ = touch_X_;
238 touch_start_Y_ = touch_Y_;
239 touch_swiping_ = true;
240 } else if (!touch_finger_down_ && touch_swiping_) {
241 touch_swiping_ = false;
242 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
243 }
244 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700245 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700246 }
247
248 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700249 if (ev.code == REL_Y) {
250 // accumulate the up or down motion reported by
251 // the trackball. When it exceeds a threshold
252 // (positive or negative), fake an up/down
253 // key event.
254 rel_sum += ev.value;
255 if (rel_sum > 3) {
256 ProcessKey(KEY_DOWN, 1); // press down key
257 ProcessKey(KEY_DOWN, 0); // and release it
258 rel_sum = 0;
259 } else if (rel_sum < -3) {
260 ProcessKey(KEY_UP, 1); // press up key
261 ProcessKey(KEY_UP, 0); // and release it
262 rel_sum = 0;
263 }
264 }
265 } else {
266 rel_sum = 0;
267 }
268
Tao Bao5f8dd992017-07-28 00:05:40 -0700269 if (touch_screen_allowed_ && ev.type == EV_ABS) {
270 if (ev.code == ABS_MT_SLOT) {
271 touch_slot_ = ev.value;
272 }
273 // Ignore other fingers.
274 if (touch_slot_ > 0) return 0;
275
276 switch (ev.code) {
277 case ABS_MT_POSITION_X:
278 touch_X_ = ev.value;
279 touch_finger_down_ = true;
280 break;
281
282 case ABS_MT_POSITION_Y:
283 touch_Y_ = ev.value;
284 touch_finger_down_ = true;
285 break;
286
287 case ABS_MT_TRACKING_ID:
288 // Protocol B: -1 marks lifting the contact.
289 if (ev.value < 0) touch_finger_down_ = false;
290 break;
291 }
292 return 0;
293 }
294
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700295 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700296 if (touch_screen_allowed_) {
297 if (ev.code == BTN_TOUCH) {
298 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
299 // lifting the contact.
300 touch_finger_down_ = (ev.value == 1);
301 }
302
303 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
304 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
305 // KEY_POWER and KEY_UP as KEY_DOWN).
306 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
307 return 0;
308 }
309 }
310
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700311 ProcessKey(ev.code, ev.value);
312 }
313
314 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700315}
316
317// Process a key-up or -down event. A key is "registered" when it is
318// pressed and then released, with no other keypresses or releases in
319// between. Registered keys are passed to CheckKey() to see if it
320// should trigger a visibility toggle, an immediate reboot, or be
321// queued to be processed next time the foreground thread wants a key
322// (eg, for the menu).
323//
324// We also keep track of which keys are currently down so that
325// CheckKey can call IsKeyPressed to see what other keys are held when
326// a key is registered.
327//
328// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700329void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700330 bool register_key = false;
331 bool long_press = false;
332 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800333
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700334 pthread_mutex_lock(&key_queue_mutex);
335 key_pressed[key_code] = updown;
336 if (updown) {
337 ++key_down_count;
338 key_last_down = key_code;
339 key_long_press = false;
340 key_timer_t* info = new key_timer_t;
341 info->ui = this;
342 info->key_code = key_code;
343 info->count = key_down_count;
344 pthread_t thread;
345 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
346 pthread_detach(thread);
347 } else {
348 if (key_last_down == key_code) {
349 long_press = key_long_press;
350 register_key = true;
Doug Zongker32a0a472011-11-01 11:00:20 -0700351 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700352 key_last_down = -1;
353 }
354 reboot_enabled = enable_reboot;
355 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700356
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700357 if (register_key) {
358 switch (CheckKey(key_code, long_press)) {
359 case RecoveryUI::IGNORE:
360 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800361
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700362 case RecoveryUI::TOGGLE:
363 ShowText(!IsTextVisible());
364 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700365
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700366 case RecoveryUI::REBOOT:
367 if (reboot_enabled) {
368 reboot("reboot,");
369 while (true) {
370 pause();
371 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700372 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700373 break;
374
375 case RecoveryUI::ENQUEUE:
376 EnqueueKey(key_code);
377 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700378 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700379 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700380}
381
Doug Zongkerc0441d12013-07-31 11:28:24 -0700382void* RecoveryUI::time_key_helper(void* cookie) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700383 key_timer_t* info = static_cast<key_timer_t*>(cookie);
384 info->ui->time_key(info->key_code, info->count);
385 delete info;
386 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700387}
388
389void RecoveryUI::time_key(int key_code, int count) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700390 usleep(750000); // 750 ms == "long"
391 bool long_press = false;
392 pthread_mutex_lock(&key_queue_mutex);
393 if (key_last_down == key_code && key_down_count == count) {
394 long_press = key_long_press = true;
395 }
396 pthread_mutex_unlock(&key_queue_mutex);
397 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700398}
399
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800400void RecoveryUI::EnqueueKey(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700401 pthread_mutex_lock(&key_queue_mutex);
402 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
403 if (key_queue_len < queue_max) {
404 key_queue[key_queue_len++] = key_code;
405 pthread_cond_signal(&key_queue_cond);
406 }
407 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800408}
409
Elliott Hughesec283402015-04-10 10:01:53 -0700410int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800411 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700412
Tao Bao6278bdf2017-01-16 17:38:18 -0800413 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
414 // plugged in.
415 do {
416 struct timeval now;
417 struct timespec timeout;
418 gettimeofday(&now, nullptr);
419 timeout.tv_sec = now.tv_sec;
420 timeout.tv_nsec = now.tv_usec * 1000;
421 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700422
Tao Bao6278bdf2017-01-16 17:38:18 -0800423 int rc = 0;
424 while (key_queue_len == 0 && rc != ETIMEDOUT) {
425 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700426 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800427
428 if (screensaver_state_ != ScreensaverState::DISABLED) {
429 if (rc == ETIMEDOUT) {
430 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
431 if (screensaver_state_ == ScreensaverState::NORMAL) {
432 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
433 BRIGHTNESS_FILE)) {
434 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
435 << "%)";
436 screensaver_state_ = ScreensaverState::DIMMED;
437 }
438 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
439 if (android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) {
440 LOG(INFO) << "Brightness: 0 (off)";
441 screensaver_state_ = ScreensaverState::OFF;
442 }
443 }
444 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
445 // Drop the first key if it's changing from OFF to NORMAL.
446 if (screensaver_state_ == ScreensaverState::OFF) {
447 if (key_queue_len > 0) {
448 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
449 }
450 }
451
452 // Reset the brightness to normal.
453 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
454 BRIGHTNESS_FILE)) {
455 screensaver_state_ = ScreensaverState::NORMAL;
456 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
457 << "%)";
458 }
459 }
460 }
461 } while (IsUsbConnected() && key_queue_len == 0);
462
463 int key = -1;
464 if (key_queue_len > 0) {
465 key = key_queue[0];
466 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
467 }
468 pthread_mutex_unlock(&key_queue_mutex);
469 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700470}
471
Elliott Hughes985022a2015-04-13 13:04:32 -0700472bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700473 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
474 if (fd < 0) {
475 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
476 return 0;
477 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700478
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 char buf;
480 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
481 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
482 if (close(fd) < 0) {
483 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
484 }
485 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700486}
487
Elliott Hughesec283402015-04-10 10:01:53 -0700488bool RecoveryUI::IsKeyPressed(int key) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700489 pthread_mutex_lock(&key_queue_mutex);
490 int pressed = key_pressed[key];
491 pthread_mutex_unlock(&key_queue_mutex);
492 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700493}
494
Elliott Hughes642aaa72015-04-10 12:47:46 -0700495bool RecoveryUI::IsLongPress() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700496 pthread_mutex_lock(&key_queue_mutex);
497 bool result = key_long_press;
498 pthread_mutex_unlock(&key_queue_mutex);
499 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700500}
501
Elliott Hughes4af215b2015-04-10 15:00:34 -0700502bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700503 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700504}
505
Tao Bao5f8dd992017-07-28 00:05:40 -0700506bool RecoveryUI::HasPowerKey() const {
507 return has_power_key;
508}
509
510bool RecoveryUI::HasTouchScreen() const {
511 return has_touch_screen;
512}
513
Doug Zongker32a0a472011-11-01 11:00:20 -0700514void RecoveryUI::FlushKeys() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700515 pthread_mutex_lock(&key_queue_mutex);
516 key_queue_len = 0;
517 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700518}
519
Elliott Hughes642aaa72015-04-10 12:47:46 -0700520RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700521 pthread_mutex_lock(&key_queue_mutex);
522 key_long_press = false;
523 pthread_mutex_unlock(&key_queue_mutex);
524
525 // 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 -0700526 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
527 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700528 return TOGGLE;
529 }
530 } else {
531 // Otherwise long press of any button toggles to the text display,
532 // and there's no way to toggle back (but that's pretty useless anyway).
533 if (is_long_press && !IsTextVisible()) {
534 return TOGGLE;
535 }
536
537 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
538 if (is_long_press && IsTextVisible()) {
539 EnqueueKey(KEY_ENTER);
540 return IGNORE;
541 }
542 }
543
544 // Press power seven times in a row to reboot.
545 if (key == KEY_POWER) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700546 pthread_mutex_lock(&key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700547 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700548 pthread_mutex_unlock(&key_queue_mutex);
549
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700550 if (reboot_enabled) {
551 ++consecutive_power_keys;
552 if (consecutive_power_keys >= 7) {
553 return REBOOT;
554 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700555 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700556 } else {
557 consecutive_power_keys = 0;
558 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700559
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700560 last_key = key;
561 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700562}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800563
Elliott Hughes642aaa72015-04-10 12:47:46 -0700564void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700565}
Doug Zongkerc704e062014-05-23 08:40:35 -0700566
567void RecoveryUI::SetEnableReboot(bool enabled) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700568 pthread_mutex_lock(&key_queue_mutex);
569 enable_reboot = enabled;
570 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerc704e062014-05-23 08:40:35 -0700571}