Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 17 | #include "recovery_ui/ui.h" |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 18 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 24 | #include <sys/time.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 29 | #include <chrono> |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 30 | #include <functional> |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 31 | #include <string> |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 32 | #include <thread> |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 33 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 34 | #include <android-base/file.h> |
| 35 | #include <android-base/logging.h> |
| 36 | #include <android-base/parseint.h> |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 37 | #include <android-base/properties.h> |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 39 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 40 | #include "minui/minui.h" |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 41 | #include "otautil/sysutil.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 42 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 43 | using namespace std::chrono_literals; |
| 44 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 45 | constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; |
| 46 | constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; |
| 47 | constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness"; |
| 48 | constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness"; |
| 49 | constexpr const char* MAX_BRIGHTNESS_FILE_SDM = |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 50 | "/sys/class/backlight/panel0-backlight/max_brightness"; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 51 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 52 | constexpr int kDefaultTouchLowThreshold = 50; |
| 53 | constexpr int kDefaultTouchHighThreshold = 90; |
| 54 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 55 | RecoveryUI::RecoveryUI() |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 56 | : brightness_normal_(50), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 57 | brightness_dimmed_(25), |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 58 | brightness_file_(BRIGHTNESS_FILE), |
| 59 | max_brightness_file_(MAX_BRIGHTNESS_FILE), |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 60 | touch_screen_allowed_(false), |
David Anderson | 983e2d5 | 2019-01-02 11:35:38 -0800 | [diff] [blame] | 61 | fastbootd_logo_enabled_(false), |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 62 | 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 Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 66 | key_interrupted_(false), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 67 | key_queue_len(0), |
| 68 | 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 Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 76 | has_down_key(false), |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 77 | has_touch_screen(false), |
| 78 | touch_slot_(0), |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 79 | is_bootreason_recovery_ui_(false), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 80 | screensaver_state_(ScreensaverState::DISABLED) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 81 | memset(key_pressed, 0, sizeof(key_pressed)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 84 | RecoveryUI::~RecoveryUI() { |
| 85 | ev_exit(); |
| 86 | input_thread_stopped_ = true; |
Tao Bao | 94371fd | 2018-06-06 07:38:54 -0700 | [diff] [blame] | 87 | if (input_thread_.joinable()) { |
| 88 | input_thread_.join(); |
| 89 | } |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 92 | void RecoveryUI::OnKeyDetected(int key_code) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 93 | if (key_code == KEY_POWER) { |
| 94 | has_power_key = true; |
| 95 | } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { |
| 96 | has_down_key = true; |
| 97 | } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { |
| 98 | has_up_key = true; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 99 | } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) { |
| 100 | has_touch_screen = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 101 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 104 | bool RecoveryUI::InitScreensaver() { |
| 105 | // Disabled. |
| 106 | if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) { |
| 107 | return false; |
| 108 | } |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 109 | if (access(brightness_file_.c_str(), R_OK | W_OK)) { |
| 110 | brightness_file_ = BRIGHTNESS_FILE_SDM; |
| 111 | } |
| 112 | if (access(max_brightness_file_.c_str(), R_OK)) { |
| 113 | max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM; |
| 114 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 115 | // Set the initial brightness level based on the max brightness. Note that reading the initial |
| 116 | // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so |
| 117 | // we don't have a good way to query the default value. |
| 118 | std::string content; |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 119 | if (!android::base::ReadFileToString(max_brightness_file_, &content)) { |
Tao Bao | 8eec373 | 2017-01-31 21:24:16 -0800 | [diff] [blame] | 120 | PLOG(WARNING) << "Failed to read max brightness"; |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
| 124 | unsigned int max_value; |
| 125 | if (!android::base::ParseUint(android::base::Trim(content), &max_value)) { |
| 126 | LOG(WARNING) << "Failed to parse max brightness: " << content; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | brightness_normal_value_ = max_value * brightness_normal_ / 100.0; |
| 131 | brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0; |
| 132 | if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 133 | brightness_file_)) { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 134 | PLOG(WARNING) << "Failed to set brightness"; |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)"; |
| 139 | screensaver_state_ = ScreensaverState::NORMAL; |
| 140 | return true; |
| 141 | } |
| 142 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 143 | bool RecoveryUI::Init(const std::string& /* locale */) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 144 | ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2), |
| 145 | touch_screen_allowed_); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 146 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 147 | ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
| 148 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 149 | if (touch_screen_allowed_) { |
| 150 | ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 151 | |
| 152 | // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of |
| 153 | // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way |
| 154 | // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or |
| 155 | // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text |
| 156 | // mode will be turned on automatically on debuggable builds, even without a swipe. |
| 157 | std::string cmdline; |
| 158 | if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) { |
| 159 | is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos; |
| 160 | } else { |
| 161 | // Non-fatal, and won't affect Init() result. |
| 162 | PLOG(WARNING) << "Failed to read /proc/cmdline"; |
| 163 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 166 | if (!InitScreensaver()) { |
| 167 | LOG(INFO) << "Screensaver disabled"; |
| 168 | } |
| 169 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 170 | // Create a separate thread that handles input events. |
| 171 | input_thread_ = std::thread([this]() { |
| 172 | while (!this->input_thread_stopped_) { |
| 173 | if (!ev_wait(500)) { |
| 174 | ev_dispatch(); |
| 175 | } |
| 176 | } |
| 177 | }); |
| 178 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 179 | return true; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 182 | void RecoveryUI::OnTouchDetected(int dx, int dy) { |
| 183 | enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction; |
| 184 | |
| 185 | // We only consider a valid swipe if: |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 186 | // - the delta along one axis is below touch_low_threshold_; |
| 187 | // - and the delta along the other axis is beyond touch_high_threshold_. |
| 188 | if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 189 | direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 190 | } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 191 | direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN; |
| 192 | } else { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 193 | LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_ |
| 194 | << ", high: " << touch_high_threshold_ << ")"; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 198 | // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui. |
| 199 | if (is_bootreason_recovery_ui_ && !IsTextVisible()) { |
| 200 | ShowText(true); |
| 201 | return; |
| 202 | } |
| 203 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 204 | LOG(DEBUG) << "Swipe direction=" << direction; |
| 205 | switch (direction) { |
| 206 | case SwipeDirection::UP: |
| 207 | ProcessKey(KEY_UP, 1); // press up key |
| 208 | ProcessKey(KEY_UP, 0); // and release it |
| 209 | break; |
| 210 | |
| 211 | case SwipeDirection::DOWN: |
| 212 | ProcessKey(KEY_DOWN, 1); // press down key |
| 213 | ProcessKey(KEY_DOWN, 0); // and release it |
| 214 | break; |
| 215 | |
| 216 | case SwipeDirection::LEFT: |
| 217 | case SwipeDirection::RIGHT: |
| 218 | ProcessKey(KEY_POWER, 1); // press power key |
| 219 | ProcessKey(KEY_POWER, 0); // and release it |
| 220 | break; |
| 221 | }; |
| 222 | } |
| 223 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 224 | int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 225 | struct input_event ev; |
| 226 | if (ev_get_input(fd, epevents, &ev) == -1) { |
| 227 | return -1; |
| 228 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 229 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 230 | // Touch inputs handling. |
| 231 | // |
| 232 | // We handle the touch inputs by tracking the position changes between initial contacting and |
| 233 | // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon |
| 234 | // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes. |
| 235 | // |
| 236 | // Per the doc Multi-touch Protocol at below, there are two protocols. |
| 237 | // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt |
| 238 | // |
| 239 | // The main difference between the stateless type A protocol and the stateful type B slot protocol |
| 240 | // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The |
| 241 | // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and |
| 242 | // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send |
| 243 | // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event. |
| 244 | // |
| 245 | // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for |
| 246 | // ABS_MT_TRACKING_ID being -1. |
| 247 | // |
| 248 | // Touch input events will only be available if touch_screen_allowed_ is set. |
| 249 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 250 | if (ev.type == EV_SYN) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 251 | if (touch_screen_allowed_ && ev.code == SYN_REPORT) { |
| 252 | // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the |
| 253 | // contact. |
| 254 | if (touch_finger_down_ && !touch_swiping_) { |
| 255 | touch_start_X_ = touch_X_; |
| 256 | touch_start_Y_ = touch_Y_; |
| 257 | touch_swiping_ = true; |
| 258 | } else if (!touch_finger_down_ && touch_swiping_) { |
| 259 | touch_swiping_ = false; |
| 260 | OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_); |
| 261 | } |
| 262 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 263 | return 0; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (ev.type == EV_REL) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 267 | if (ev.code == REL_Y) { |
| 268 | // accumulate the up or down motion reported by |
| 269 | // the trackball. When it exceeds a threshold |
| 270 | // (positive or negative), fake an up/down |
| 271 | // key event. |
| 272 | rel_sum += ev.value; |
| 273 | if (rel_sum > 3) { |
| 274 | ProcessKey(KEY_DOWN, 1); // press down key |
| 275 | ProcessKey(KEY_DOWN, 0); // and release it |
| 276 | rel_sum = 0; |
| 277 | } else if (rel_sum < -3) { |
| 278 | ProcessKey(KEY_UP, 1); // press up key |
| 279 | ProcessKey(KEY_UP, 0); // and release it |
| 280 | rel_sum = 0; |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | rel_sum = 0; |
| 285 | } |
| 286 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 287 | if (touch_screen_allowed_ && ev.type == EV_ABS) { |
| 288 | if (ev.code == ABS_MT_SLOT) { |
| 289 | touch_slot_ = ev.value; |
| 290 | } |
| 291 | // Ignore other fingers. |
| 292 | if (touch_slot_ > 0) return 0; |
| 293 | |
| 294 | switch (ev.code) { |
| 295 | case ABS_MT_POSITION_X: |
| 296 | touch_X_ = ev.value; |
| 297 | touch_finger_down_ = true; |
| 298 | break; |
| 299 | |
| 300 | case ABS_MT_POSITION_Y: |
| 301 | touch_Y_ = ev.value; |
| 302 | touch_finger_down_ = true; |
| 303 | break; |
| 304 | |
| 305 | case ABS_MT_TRACKING_ID: |
| 306 | // Protocol B: -1 marks lifting the contact. |
| 307 | if (ev.value < 0) touch_finger_down_ = false; |
| 308 | break; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 313 | if (ev.type == EV_KEY && ev.code <= KEY_MAX) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 314 | if (touch_screen_allowed_) { |
| 315 | if (ev.code == BTN_TOUCH) { |
| 316 | // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means |
| 317 | // lifting the contact. |
| 318 | touch_finger_down_ = (ev.value == 1); |
| 319 | } |
| 320 | |
| 321 | // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger |
| 322 | // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than |
| 323 | // KEY_POWER and KEY_UP as KEY_DOWN). |
| 324 | if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) { |
| 325 | return 0; |
| 326 | } |
| 327 | } |
| 328 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 329 | ProcessKey(ev.code, ev.value); |
| 330 | } |
| 331 | |
| 332 | return 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 335 | // Processes a key-up or -down event. A key is "registered" when it is pressed and then released, |
| 336 | // with no other keypresses or releases in between. Registered keys are passed to CheckKey() to |
| 337 | // see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed |
| 338 | // next time the foreground thread wants a key (eg, for the menu). |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 339 | // |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 340 | // We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed() |
| 341 | // to see what other keys are held when a key is registered. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 342 | // |
| 343 | // updown == 1 for key down events; 0 for key up events |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 344 | void RecoveryUI::ProcessKey(int key_code, int updown) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 345 | bool register_key = false; |
| 346 | bool long_press = false; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 347 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 348 | { |
| 349 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
| 350 | key_pressed[key_code] = updown; |
| 351 | if (updown) { |
| 352 | ++key_down_count; |
| 353 | key_last_down = key_code; |
| 354 | key_long_press = false; |
| 355 | std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count); |
| 356 | time_key_thread.detach(); |
| 357 | } else { |
| 358 | if (key_last_down == key_code) { |
| 359 | long_press = key_long_press; |
| 360 | register_key = true; |
| 361 | } |
| 362 | key_last_down = -1; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 363 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 364 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 365 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 366 | bool reboot_enabled = enable_reboot; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 367 | if (register_key) { |
| 368 | switch (CheckKey(key_code, long_press)) { |
| 369 | case RecoveryUI::IGNORE: |
| 370 | break; |
Doug Zongker | 48b5b07 | 2012-01-18 13:46:26 -0800 | [diff] [blame] | 371 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 372 | case RecoveryUI::TOGGLE: |
| 373 | ShowText(!IsTextVisible()); |
| 374 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 375 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 376 | case RecoveryUI::REBOOT: |
| 377 | if (reboot_enabled) { |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame^] | 378 | Reboot(""); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 379 | while (true) { |
| 380 | pause(); |
| 381 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 382 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case RecoveryUI::ENQUEUE: |
| 386 | EnqueueKey(key_code); |
| 387 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 388 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 389 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 392 | void RecoveryUI::TimeKey(int key_code, int count) { |
| 393 | std::this_thread::sleep_for(750ms); // 750 ms == "long" |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 394 | bool long_press = false; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 395 | { |
| 396 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
| 397 | if (key_last_down == key_code && key_down_count == count) { |
| 398 | long_press = key_long_press = true; |
| 399 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 400 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 401 | if (long_press) KeyLongPress(key_code); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 404 | void RecoveryUI::EnqueueKey(int key_code) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 405 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 406 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); |
| 407 | if (key_queue_len < queue_max) { |
| 408 | key_queue[key_queue_len++] = key_code; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 409 | key_queue_cond.notify_one(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 410 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 413 | void RecoveryUI::SetScreensaverState(ScreensaverState state) { |
| 414 | switch (state) { |
| 415 | case ScreensaverState::NORMAL: |
| 416 | if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
| 417 | brightness_file_)) { |
| 418 | screensaver_state_ = ScreensaverState::NORMAL; |
| 419 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ |
| 420 | << "%)"; |
| 421 | } else { |
| 422 | LOG(ERROR) << "Unable to set brightness to normal"; |
| 423 | } |
| 424 | break; |
| 425 | case ScreensaverState::DIMMED: |
| 426 | if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_), |
| 427 | brightness_file_)) { |
| 428 | LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_ |
| 429 | << "%)"; |
| 430 | screensaver_state_ = ScreensaverState::DIMMED; |
| 431 | } else { |
| 432 | LOG(ERROR) << "Unable to set brightness to dim"; |
| 433 | } |
| 434 | break; |
| 435 | case ScreensaverState::OFF: |
| 436 | if (android::base::WriteStringToFile("0", brightness_file_)) { |
| 437 | LOG(INFO) << "Brightness: 0 (off)"; |
| 438 | screensaver_state_ = ScreensaverState::OFF; |
| 439 | } else { |
| 440 | LOG(ERROR) << "Unable to set brightness to off"; |
| 441 | } |
| 442 | break; |
| 443 | default: |
| 444 | LOG(ERROR) << "Invalid screensaver state"; |
| 445 | } |
| 446 | } |
| 447 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 448 | int RecoveryUI::WaitKey() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 449 | std::unique_lock<std::mutex> lk(key_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 450 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 451 | // Check for a saved key queue interruption. |
| 452 | if (key_interrupted_) { |
| 453 | SetScreensaverState(ScreensaverState::NORMAL); |
| 454 | return static_cast<int>(KeyError::INTERRUPTED); |
| 455 | } |
| 456 | |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 457 | // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in. |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 458 | do { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 459 | bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] { |
| 460 | return this->key_queue_len != 0 || key_interrupted_; |
| 461 | }); |
| 462 | if (key_interrupted_) { |
| 463 | SetScreensaverState(ScreensaverState::NORMAL); |
| 464 | return static_cast<int>(KeyError::INTERRUPTED); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 465 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 466 | if (screensaver_state_ != ScreensaverState::DISABLED) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 467 | if (!rc) { |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 468 | // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 469 | if (screensaver_state_ == ScreensaverState::NORMAL) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 470 | SetScreensaverState(ScreensaverState::DIMMED); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 471 | } else if (screensaver_state_ == ScreensaverState::DIMMED) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 472 | SetScreensaverState(ScreensaverState::OFF); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 473 | } |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 474 | } else if (screensaver_state_ != ScreensaverState::NORMAL) { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 475 | // Drop the first key if it's changing from OFF to NORMAL. |
| 476 | if (screensaver_state_ == ScreensaverState::OFF) { |
| 477 | if (key_queue_len > 0) { |
| 478 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // Reset the brightness to normal. |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 483 | SetScreensaverState(ScreensaverState::NORMAL); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } while (IsUsbConnected() && key_queue_len == 0); |
| 487 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 488 | int key = static_cast<int>(KeyError::TIMED_OUT); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 489 | if (key_queue_len > 0) { |
| 490 | key = key_queue[0]; |
| 491 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 492 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 493 | return key; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 496 | void RecoveryUI::InterruptKey() { |
| 497 | { |
| 498 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
| 499 | key_interrupted_ = true; |
| 500 | } |
| 501 | key_queue_cond.notify_one(); |
| 502 | } |
| 503 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 504 | bool RecoveryUI::IsUsbConnected() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 505 | int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); |
| 506 | if (fd < 0) { |
| 507 | printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno)); |
| 508 | return 0; |
| 509 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 510 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 511 | char buf; |
| 512 | // USB is connected if android_usb state is CONNECTED or CONFIGURED. |
| 513 | int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); |
| 514 | if (close(fd) < 0) { |
| 515 | printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno)); |
| 516 | } |
| 517 | return connected; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 520 | bool RecoveryUI::IsKeyPressed(int key) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 521 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 522 | int pressed = key_pressed[key]; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 523 | return pressed; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 526 | bool RecoveryUI::IsLongPress() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 527 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 528 | bool result = key_long_press; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 529 | return result; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 532 | bool RecoveryUI::HasThreeButtons() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 533 | return has_power_key && has_up_key && has_down_key; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 536 | bool RecoveryUI::HasPowerKey() const { |
| 537 | return has_power_key; |
| 538 | } |
| 539 | |
| 540 | bool RecoveryUI::HasTouchScreen() const { |
| 541 | return has_touch_screen; |
| 542 | } |
| 543 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 544 | void RecoveryUI::FlushKeys() { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 545 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 546 | key_queue_len = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 549 | RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 550 | { |
| 551 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
| 552 | key_long_press = false; |
| 553 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 554 | |
| 555 | // If we have power and volume up keys, that chord is the signal to toggle the text display. |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 556 | if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) { |
| 557 | if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 558 | return TOGGLE; |
| 559 | } |
| 560 | } else { |
| 561 | // Otherwise long press of any button toggles to the text display, |
| 562 | // and there's no way to toggle back (but that's pretty useless anyway). |
| 563 | if (is_long_press && !IsTextVisible()) { |
| 564 | return TOGGLE; |
| 565 | } |
| 566 | |
| 567 | // Also, for button-limited devices, a long press is translated to KEY_ENTER. |
| 568 | if (is_long_press && IsTextVisible()) { |
| 569 | EnqueueKey(KEY_ENTER); |
| 570 | return IGNORE; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // Press power seven times in a row to reboot. |
| 575 | if (key == KEY_POWER) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 576 | bool reboot_enabled = enable_reboot; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 577 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 578 | if (reboot_enabled) { |
| 579 | ++consecutive_power_keys; |
| 580 | if (consecutive_power_keys >= 7) { |
| 581 | return REBOOT; |
| 582 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 583 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 584 | } else { |
| 585 | consecutive_power_keys = 0; |
| 586 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 587 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 588 | last_key = key; |
| 589 | return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 590 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 591 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 592 | void RecoveryUI::KeyLongPress(int) {} |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 593 | |
| 594 | void RecoveryUI::SetEnableReboot(bool enabled) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 595 | std::lock_guard<std::mutex> lg(key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 596 | enable_reboot = enabled; |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 597 | } |