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> |
| 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 Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 33 | #include <functional> |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 34 | #include <string> |
| 35 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 39 | #include <android-base/properties.h> |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 40 | #include <android-base/strings.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 41 | #include <cutils/android_reboot.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 42 | #include <minui/minui.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 43 | |
| 44 | #include "common.h" |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 45 | #include "roots.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 46 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 47 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 48 | static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; |
| 49 | static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; |
| 50 | static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness"; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 52 | RecoveryUI::RecoveryUI() |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 53 | : brightness_normal_(50), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 54 | brightness_dimmed_(25), |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 55 | touch_screen_allowed_(false), |
| 56 | kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD), |
| 57 | kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 58 | 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 Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 67 | has_down_key(false), |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 68 | has_touch_screen(false), |
| 69 | touch_slot_(0), |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 70 | is_bootreason_recovery_ui_(false), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 71 | screensaver_state_(ScreensaverState::DISABLED) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 72 | pthread_mutex_init(&key_queue_mutex, nullptr); |
| 73 | pthread_cond_init(&key_queue_cond, nullptr); |
| 74 | memset(key_pressed, 0, sizeof(key_pressed)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 77 | void RecoveryUI::OnKeyDetected(int key_code) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 78 | 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 Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 84 | } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) { |
| 85 | has_touch_screen = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 89 | // Reads input events, handles special hot keys, and adds to the key queue. |
| 90 | static void* InputThreadLoop(void*) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 91 | while (true) { |
| 92 | if (!ev_wait(-1)) { |
| 93 | ev_dispatch(); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 94 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 95 | } |
| 96 | return nullptr; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 99 | bool 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 Bao | 8eec373 | 2017-01-31 21:24:16 -0800 | [diff] [blame] | 110 | PLOG(WARNING) << "Failed to read max brightness"; |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 111 | 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 Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 133 | bool RecoveryUI::Init(const std::string& /* locale */) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 134 | ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2), |
| 135 | touch_screen_allowed_); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 136 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 137 | ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
| 138 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 139 | if (touch_screen_allowed_) { |
| 140 | ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 141 | |
| 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 Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 156 | if (!InitScreensaver()) { |
| 157 | LOG(INFO) << "Screensaver disabled"; |
| 158 | } |
| 159 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 160 | pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr); |
| 161 | return true; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 164 | void 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 Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 180 | // 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 Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 186 | 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 Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 206 | int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 207 | struct input_event ev; |
| 208 | if (ev_get_input(fd, epevents, &ev) == -1) { |
| 209 | return -1; |
| 210 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 211 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 212 | // 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 232 | if (ev.type == EV_SYN) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 233 | 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 245 | return 0; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (ev.type == EV_REL) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 249 | 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 Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 269 | 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 295 | if (ev.type == EV_KEY && ev.code <= KEY_MAX) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 296 | 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 311 | ProcessKey(ev.code, ev.value); |
| 312 | } |
| 313 | |
| 314 | return 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 315 | } |
| 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 Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 329 | void RecoveryUI::ProcessKey(int key_code, int updown) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 330 | bool register_key = false; |
| 331 | bool long_press = false; |
| 332 | bool reboot_enabled; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 333 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 334 | 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 351 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 352 | key_last_down = -1; |
| 353 | } |
| 354 | reboot_enabled = enable_reboot; |
| 355 | pthread_mutex_unlock(&key_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 356 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 357 | if (register_key) { |
| 358 | switch (CheckKey(key_code, long_press)) { |
| 359 | case RecoveryUI::IGNORE: |
| 360 | break; |
Doug Zongker | 48b5b07 | 2012-01-18 13:46:26 -0800 | [diff] [blame] | 361 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 362 | case RecoveryUI::TOGGLE: |
| 363 | ShowText(!IsTextVisible()); |
| 364 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 365 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 366 | case RecoveryUI::REBOOT: |
| 367 | if (reboot_enabled) { |
| 368 | reboot("reboot,"); |
| 369 | while (true) { |
| 370 | pause(); |
| 371 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 372 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | case RecoveryUI::ENQUEUE: |
| 376 | EnqueueKey(key_code); |
| 377 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 378 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 379 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 382 | void* RecoveryUI::time_key_helper(void* cookie) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 383 | 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 Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void RecoveryUI::time_key(int key_code, int count) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 390 | 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 Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 400 | void RecoveryUI::EnqueueKey(int key_code) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 401 | 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 Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 410 | int RecoveryUI::WaitKey() { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 411 | pthread_mutex_lock(&key_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 412 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 413 | // 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 422 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 423 | int rc = 0; |
| 424 | while (key_queue_len == 0 && rc != ETIMEDOUT) { |
| 425 | rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 426 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 427 | |
| 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 472 | bool RecoveryUI::IsUsbConnected() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 473 | 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 478 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 479 | 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 Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 488 | bool RecoveryUI::IsKeyPressed(int key) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 489 | pthread_mutex_lock(&key_queue_mutex); |
| 490 | int pressed = key_pressed[key]; |
| 491 | pthread_mutex_unlock(&key_queue_mutex); |
| 492 | return pressed; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 495 | bool RecoveryUI::IsLongPress() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 496 | pthread_mutex_lock(&key_queue_mutex); |
| 497 | bool result = key_long_press; |
| 498 | pthread_mutex_unlock(&key_queue_mutex); |
| 499 | return result; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 502 | bool RecoveryUI::HasThreeButtons() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 503 | return has_power_key && has_up_key && has_down_key; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 506 | bool RecoveryUI::HasPowerKey() const { |
| 507 | return has_power_key; |
| 508 | } |
| 509 | |
| 510 | bool RecoveryUI::HasTouchScreen() const { |
| 511 | return has_touch_screen; |
| 512 | } |
| 513 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 514 | void RecoveryUI::FlushKeys() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 515 | pthread_mutex_lock(&key_queue_mutex); |
| 516 | key_queue_len = 0; |
| 517 | pthread_mutex_unlock(&key_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 520 | RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 521 | 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 Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 526 | if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) { |
| 527 | if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 528 | 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 Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 546 | pthread_mutex_lock(&key_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 547 | bool reboot_enabled = enable_reboot; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 548 | pthread_mutex_unlock(&key_queue_mutex); |
| 549 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 550 | if (reboot_enabled) { |
| 551 | ++consecutive_power_keys; |
| 552 | if (consecutive_power_keys >= 7) { |
| 553 | return REBOOT; |
| 554 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 555 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 556 | } else { |
| 557 | consecutive_power_keys = 0; |
| 558 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 559 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 560 | last_key = key; |
| 561 | return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 562 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 563 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 564 | void RecoveryUI::KeyLongPress(int) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 565 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 566 | |
| 567 | void RecoveryUI::SetEnableReboot(bool enabled) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 568 | pthread_mutex_lock(&key_queue_mutex); |
| 569 | enable_reboot = enabled; |
| 570 | pthread_mutex_unlock(&key_queue_mutex); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 571 | } |