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