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 | b5108c3 | 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), |
Tom Marshall | 2ec1198 | 2017-08-23 22:45:19 +0000 | [diff] [blame] | 60 | touch_screen_allowed_(true), |
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), |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 67 | event_queue_len(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 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), |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 79 | touch_finger_down_(false), |
| 80 | touch_saw_x_(false), |
| 81 | touch_saw_y_(false), |
| 82 | touch_reported_(false), |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 83 | is_bootreason_recovery_ui_(false), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 84 | screensaver_state_(ScreensaverState::DISABLED) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 85 | memset(key_pressed, 0, sizeof(key_pressed)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 88 | RecoveryUI::~RecoveryUI() { |
| 89 | ev_exit(); |
| 90 | input_thread_stopped_ = true; |
Tao Bao | 94371fd | 2018-06-06 07:38:54 -0700 | [diff] [blame] | 91 | if (input_thread_.joinable()) { |
| 92 | input_thread_.join(); |
| 93 | } |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 96 | void RecoveryUI::OnTouchDeviceDetected(int fd) { |
| 97 | char name[256]; |
| 98 | char path[PATH_MAX]; |
| 99 | char buf[4096]; |
| 100 | |
| 101 | memset(name, 0, sizeof(name)); |
| 102 | if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) { |
| 103 | return; |
| 104 | } |
| 105 | sprintf(path, "/sys/board_properties/virtualkeys.%s", name); |
| 106 | int vkfd = open(path, O_RDONLY); |
| 107 | if (vkfd < 0) { |
| 108 | LOG(INFO) << "vkeys: could not open " << path; |
| 109 | return; |
| 110 | } |
| 111 | ssize_t len = read(vkfd, buf, sizeof(buf)); |
| 112 | close(vkfd); |
| 113 | if (len <= 0) { |
| 114 | LOG(ERROR) << "vkeys: could not read " << path; |
| 115 | return; |
| 116 | } |
| 117 | buf[len] = '\0'; |
| 118 | |
| 119 | char* p = buf; |
| 120 | char* endp; |
| 121 | for (size_t n = 0; p < buf + len && *p == '0'; ++n) { |
| 122 | int val[6]; |
| 123 | int f; |
| 124 | for (f = 0; *p && f < 6; ++f) { |
| 125 | val[f] = strtol(p, &endp, 0); |
| 126 | if (p == endp) break; |
| 127 | p = endp + 1; |
| 128 | } |
| 129 | if (f != 6 || val[0] != 0x01) break; |
| 130 | vkey_t vk; |
| 131 | vk.keycode = val[1]; |
| 132 | vk.min_ = Point(val[2] - val[4] / 2, val[3] - val[5] / 2); |
| 133 | vk.max_ = Point(val[2] + val[4] / 2, val[3] + val[5] / 2); |
| 134 | virtual_keys_.push_back(vk); |
| 135 | } |
| 136 | } |
| 137 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 138 | void RecoveryUI::OnKeyDetected(int key_code) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 139 | if (key_code == KEY_POWER) { |
| 140 | has_power_key = true; |
| 141 | } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { |
| 142 | has_down_key = true; |
| 143 | } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { |
| 144 | has_up_key = true; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 145 | } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) { |
| 146 | has_touch_screen = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 147 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 150 | bool RecoveryUI::InitScreensaver() { |
| 151 | // Disabled. |
| 152 | if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) { |
| 153 | return false; |
| 154 | } |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 155 | if (access(brightness_file_.c_str(), R_OK | W_OK)) { |
| 156 | brightness_file_ = BRIGHTNESS_FILE_SDM; |
| 157 | } |
| 158 | if (access(max_brightness_file_.c_str(), R_OK)) { |
| 159 | max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM; |
| 160 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 161 | // Set the initial brightness level based on the max brightness. Note that reading the initial |
| 162 | // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so |
| 163 | // we don't have a good way to query the default value. |
| 164 | std::string content; |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 165 | if (!android::base::ReadFileToString(max_brightness_file_, &content)) { |
Tao Bao | 8eec373 | 2017-01-31 21:24:16 -0800 | [diff] [blame] | 166 | PLOG(WARNING) << "Failed to read max brightness"; |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
| 170 | unsigned int max_value; |
| 171 | if (!android::base::ParseUint(android::base::Trim(content), &max_value)) { |
| 172 | LOG(WARNING) << "Failed to parse max brightness: " << content; |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | brightness_normal_value_ = max_value * brightness_normal_ / 100.0; |
| 177 | brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0; |
| 178 | if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
katao | c35c1b0 | 2017-12-08 11:02:43 +0800 | [diff] [blame] | 179 | brightness_file_)) { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 180 | PLOG(WARNING) << "Failed to set brightness"; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)"; |
| 185 | screensaver_state_ = ScreensaverState::NORMAL; |
| 186 | return true; |
| 187 | } |
| 188 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 189 | bool RecoveryUI::Init(const std::string& /* locale */) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 190 | ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2), |
| 191 | touch_screen_allowed_); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 192 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 193 | ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
| 194 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 195 | if (touch_screen_allowed_) { |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 196 | ev_iterate_touch_inputs( |
| 197 | std::bind(&RecoveryUI::OnTouchDeviceDetected, this, std::placeholders::_1), |
| 198 | std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 199 | |
| 200 | // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of |
| 201 | // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way |
| 202 | // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or |
| 203 | // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text |
| 204 | // mode will be turned on automatically on debuggable builds, even without a swipe. |
| 205 | std::string cmdline; |
| 206 | if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) { |
| 207 | is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos; |
| 208 | } else { |
| 209 | // Non-fatal, and won't affect Init() result. |
| 210 | PLOG(WARNING) << "Failed to read /proc/cmdline"; |
| 211 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 214 | if (!InitScreensaver()) { |
| 215 | LOG(INFO) << "Screensaver disabled"; |
| 216 | } |
| 217 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 218 | // Create a separate thread that handles input events. |
| 219 | input_thread_ = std::thread([this]() { |
| 220 | while (!this->input_thread_stopped_) { |
| 221 | if (!ev_wait(500)) { |
| 222 | ev_dispatch(); |
| 223 | } |
| 224 | } |
| 225 | }); |
| 226 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 227 | return true; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Tom Marshall | 0037a5e | 2019-08-06 16:30:02 +0200 | [diff] [blame^] | 230 | void RecoveryUI::CalibrateTouch(int fd) { |
| 231 | struct input_absinfo info; |
| 232 | static bool calibrated = false; |
| 233 | |
| 234 | if (calibrated) return; |
| 235 | |
| 236 | memset(&info, 0, sizeof(info)); |
| 237 | if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) { |
| 238 | touch_min_.x(info.minimum); |
| 239 | touch_max_.x(info.maximum); |
| 240 | } |
| 241 | memset(&info, 0, sizeof(info)); |
| 242 | if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) { |
| 243 | touch_min_.y(info.minimum); |
| 244 | touch_max_.y(info.maximum); |
| 245 | } |
| 246 | |
| 247 | calibrated = true; |
| 248 | } |
| 249 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 250 | void RecoveryUI::OnTouchPress() { |
| 251 | touch_start_ = touch_track_ = touch_pos_; |
| 252 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 253 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 254 | void RecoveryUI::OnTouchTrack() { |
| 255 | if (touch_pos_.y() <= gr_fb_height()) { |
| 256 | while (abs(touch_pos_.y() - touch_track_.y()) >= MenuItemHeight()) { |
| 257 | int dy = touch_pos_.y() - touch_track_.y(); |
| 258 | int key = (dy < 0) ? KEY_SCROLLDOWN : KEY_SCROLLUP; |
| 259 | ProcessKey(key, 1); // press key |
| 260 | ProcessKey(key, 0); // and release it |
| 261 | int sgn = (dy > 0) - (dy < 0); |
| 262 | touch_track_.y(touch_track_.y() + sgn * MenuItemHeight()); |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 263 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 264 | } |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 265 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 266 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 267 | void RecoveryUI::OnTouchRelease() { |
Tao Bao | 046aae2 | 2017-07-31 23:15:09 -0700 | [diff] [blame] | 268 | // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui. |
| 269 | if (is_bootreason_recovery_ui_ && !IsTextVisible()) { |
| 270 | ShowText(true); |
| 271 | return; |
| 272 | } |
| 273 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 274 | // Check vkeys. Only report if touch both starts and ends in the vkey. |
| 275 | if (touch_start_.y() > gr_fb_height() && touch_pos_.y() > gr_fb_height()) { |
| 276 | for (const auto& vk : virtual_keys_) { |
| 277 | if (vk.inside(touch_start_) && vk.inside(touch_pos_)) { |
| 278 | ProcessKey(vk.keycode, 1); // press key |
| 279 | ProcessKey(vk.keycode, 0); // and release it |
| 280 | } |
| 281 | } |
| 282 | return; |
| 283 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 284 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 285 | // If we tracked a vertical swipe, ignore the release |
| 286 | if (touch_track_ != touch_start_) { |
| 287 | return; |
| 288 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 289 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 290 | // Check for horizontal swipe |
| 291 | Point delta = touch_pos_ - touch_start_; |
| 292 | if (abs(delta.y()) < touch_low_threshold_ && abs(delta.x()) > touch_high_threshold_) { |
| 293 | int key = (delta.x() < 0) ? KEY_BACK : KEY_POWER; |
| 294 | ProcessKey(key, 1); // press key |
| 295 | ProcessKey(key, 0); // and release it |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // Simple touch |
| 300 | EnqueueTouch(touch_pos_); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 303 | int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 304 | struct input_event ev; |
| 305 | if (ev_get_input(fd, epevents, &ev) == -1) { |
| 306 | return -1; |
| 307 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 308 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 309 | // Touch inputs handling. |
| 310 | // |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 311 | // Per the doc Multi-touch Protocol at below, there are two protocols. |
| 312 | // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt |
| 313 | // |
| 314 | // The main difference between the stateless type A protocol and the stateful type B slot protocol |
| 315 | // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The |
| 316 | // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and |
| 317 | // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send |
| 318 | // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event. |
| 319 | // |
| 320 | // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for |
| 321 | // ABS_MT_TRACKING_ID being -1. |
| 322 | // |
| 323 | // Touch input events will only be available if touch_screen_allowed_ is set. |
| 324 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 325 | if (ev.type == EV_SYN) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 326 | if (touch_screen_allowed_ && ev.code == SYN_REPORT) { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 327 | // There might be multiple SYN_REPORT events. Only report press/release once. |
| 328 | if (!touch_reported_ && touch_finger_down_) { |
| 329 | if (touch_saw_x_ && touch_saw_y_) { |
| 330 | OnTouchPress(); |
| 331 | touch_reported_ = true; |
| 332 | touch_saw_x_ = touch_saw_y_ = false; |
| 333 | } |
| 334 | } else if (touch_reported_ && !touch_finger_down_) { |
| 335 | OnTouchRelease(); |
| 336 | touch_reported_ = false; |
| 337 | touch_saw_x_ = touch_saw_y_ = false; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 340 | return 0; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (ev.type == EV_REL) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 344 | if (ev.code == REL_Y) { |
| 345 | // accumulate the up or down motion reported by |
| 346 | // the trackball. When it exceeds a threshold |
| 347 | // (positive or negative), fake an up/down |
| 348 | // key event. |
| 349 | rel_sum += ev.value; |
| 350 | if (rel_sum > 3) { |
| 351 | ProcessKey(KEY_DOWN, 1); // press down key |
| 352 | ProcessKey(KEY_DOWN, 0); // and release it |
| 353 | rel_sum = 0; |
| 354 | } else if (rel_sum < -3) { |
| 355 | ProcessKey(KEY_UP, 1); // press up key |
| 356 | ProcessKey(KEY_UP, 0); // and release it |
| 357 | rel_sum = 0; |
| 358 | } |
| 359 | } |
| 360 | } else { |
| 361 | rel_sum = 0; |
| 362 | } |
| 363 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 364 | if (touch_screen_allowed_ && ev.type == EV_ABS) { |
Tom Marshall | 0037a5e | 2019-08-06 16:30:02 +0200 | [diff] [blame^] | 365 | CalibrateTouch(fd); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 366 | if (ev.code == ABS_MT_SLOT) { |
| 367 | touch_slot_ = ev.value; |
| 368 | } |
| 369 | // Ignore other fingers. |
| 370 | if (touch_slot_ > 0) return 0; |
| 371 | |
| 372 | switch (ev.code) { |
| 373 | case ABS_MT_POSITION_X: |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 374 | touch_finger_down_ = true; |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 375 | touch_saw_x_ = true; |
Tom Marshall | 0037a5e | 2019-08-06 16:30:02 +0200 | [diff] [blame^] | 376 | touch_pos_.x(ev.value * gr_fb_width() / (touch_max_.x() - touch_min_.x())); |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 377 | if (touch_reported_ && touch_saw_y_) { |
| 378 | OnTouchTrack(); |
| 379 | touch_saw_x_ = touch_saw_y_ = false; |
| 380 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | case ABS_MT_POSITION_Y: |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 384 | touch_finger_down_ = true; |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 385 | touch_saw_y_ = true; |
Tom Marshall | 0037a5e | 2019-08-06 16:30:02 +0200 | [diff] [blame^] | 386 | touch_pos_.y(ev.value * gr_fb_height() / (touch_max_.y() - touch_min_.y())); |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 387 | if (touch_reported_ && touch_saw_x_) { |
| 388 | OnTouchTrack(); |
| 389 | touch_saw_x_ = touch_saw_y_ = false; |
| 390 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 391 | break; |
| 392 | |
| 393 | case ABS_MT_TRACKING_ID: |
| 394 | // Protocol B: -1 marks lifting the contact. |
| 395 | if (ev.value < 0) touch_finger_down_ = false; |
| 396 | break; |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 401 | if (ev.type == EV_KEY && ev.code <= KEY_MAX) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 402 | if (touch_screen_allowed_) { |
| 403 | if (ev.code == BTN_TOUCH) { |
| 404 | // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means |
| 405 | // lifting the contact. |
| 406 | touch_finger_down_ = (ev.value == 1); |
| 407 | } |
| 408 | |
| 409 | // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger |
| 410 | // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than |
| 411 | // KEY_POWER and KEY_UP as KEY_DOWN). |
| 412 | if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) { |
| 413 | return 0; |
| 414 | } |
| 415 | } |
| 416 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 417 | ProcessKey(ev.code, ev.value); |
| 418 | } |
| 419 | |
| 420 | return 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 423 | // Processes a key-up or -down event. A key is "registered" when it is pressed and then released, |
| 424 | // with no other keypresses or releases in between. Registered keys are passed to CheckKey() to |
| 425 | // see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed |
| 426 | // next time the foreground thread wants a key (eg, for the menu). |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 427 | // |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 428 | // We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed() |
| 429 | // to see what other keys are held when a key is registered. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 430 | // |
| 431 | // updown == 1 for key down events; 0 for key up events |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 432 | void RecoveryUI::ProcessKey(int key_code, int updown) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 433 | bool register_key = false; |
| 434 | bool long_press = false; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 435 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 436 | { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 437 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 438 | key_pressed[key_code] = updown; |
| 439 | if (updown) { |
| 440 | ++key_down_count; |
| 441 | key_last_down = key_code; |
| 442 | key_long_press = false; |
| 443 | std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count); |
| 444 | time_key_thread.detach(); |
| 445 | } else { |
| 446 | if (key_last_down == key_code) { |
| 447 | long_press = key_long_press; |
| 448 | register_key = true; |
| 449 | } |
| 450 | key_last_down = -1; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 451 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 452 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 453 | |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 454 | bool reboot_enabled = enable_reboot; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 455 | if (register_key) { |
| 456 | switch (CheckKey(key_code, long_press)) { |
| 457 | case RecoveryUI::IGNORE: |
| 458 | break; |
Doug Zongker | 48b5b07 | 2012-01-18 13:46:26 -0800 | [diff] [blame] | 459 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 460 | case RecoveryUI::TOGGLE: |
| 461 | ShowText(!IsTextVisible()); |
| 462 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 463 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 464 | case RecoveryUI::REBOOT: |
| 465 | if (reboot_enabled) { |
| 466 | reboot("reboot,"); |
| 467 | while (true) { |
| 468 | pause(); |
| 469 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 470 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | case RecoveryUI::ENQUEUE: |
| 474 | EnqueueKey(key_code); |
| 475 | break; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 476 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 477 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 480 | void RecoveryUI::TimeKey(int key_code, int count) { |
| 481 | std::this_thread::sleep_for(750ms); // 750 ms == "long" |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 482 | bool long_press = false; |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 483 | { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 484 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 485 | if (key_last_down == key_code && key_down_count == count) { |
| 486 | long_press = key_long_press = true; |
| 487 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 488 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 489 | if (long_press) KeyLongPress(key_code); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 492 | void RecoveryUI::EnqueueKey(int key_code) { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 493 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
| 494 | const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]); |
| 495 | if (event_queue_len < queue_max) { |
| 496 | InputEvent event(key_code); |
| 497 | event_queue[event_queue_len++] = event; |
| 498 | event_queue_cond.notify_one(); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void RecoveryUI::EnqueueTouch(const Point& pos) { |
| 503 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
| 504 | const int queue_max = sizeof(event_queue) / sizeof(event_queue[0]); |
| 505 | if (event_queue_len < queue_max) { |
| 506 | InputEvent event(pos); |
| 507 | event_queue[event_queue_len++] = event; |
| 508 | event_queue_cond.notify_one(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 509 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 512 | void RecoveryUI::SetScreensaverState(ScreensaverState state) { |
| 513 | switch (state) { |
| 514 | case ScreensaverState::NORMAL: |
| 515 | if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
| 516 | brightness_file_)) { |
| 517 | screensaver_state_ = ScreensaverState::NORMAL; |
| 518 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ |
| 519 | << "%)"; |
| 520 | } else { |
| 521 | LOG(ERROR) << "Unable to set brightness to normal"; |
| 522 | } |
| 523 | break; |
| 524 | case ScreensaverState::DIMMED: |
| 525 | if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_), |
| 526 | brightness_file_)) { |
| 527 | LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_ |
| 528 | << "%)"; |
| 529 | screensaver_state_ = ScreensaverState::DIMMED; |
| 530 | } else { |
| 531 | LOG(ERROR) << "Unable to set brightness to dim"; |
| 532 | } |
| 533 | break; |
| 534 | case ScreensaverState::OFF: |
| 535 | if (android::base::WriteStringToFile("0", brightness_file_)) { |
| 536 | LOG(INFO) << "Brightness: 0 (off)"; |
| 537 | screensaver_state_ = ScreensaverState::OFF; |
| 538 | } else { |
| 539 | LOG(ERROR) << "Unable to set brightness to off"; |
| 540 | } |
| 541 | break; |
| 542 | default: |
| 543 | LOG(ERROR) << "Invalid screensaver state"; |
| 544 | } |
| 545 | } |
| 546 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 547 | RecoveryUI::InputEvent RecoveryUI::WaitInputEvent() { |
| 548 | std::unique_lock<std::mutex> lk(event_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 549 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 550 | // Check for a saved key queue interruption. |
| 551 | if (key_interrupted_) { |
| 552 | SetScreensaverState(ScreensaverState::NORMAL); |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 553 | return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 556 | // 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] | 557 | do { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 558 | bool rc = event_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] { |
| 559 | return this->event_queue_len != 0 || key_interrupted_; |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 560 | }); |
| 561 | if (key_interrupted_) { |
| 562 | SetScreensaverState(ScreensaverState::NORMAL); |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 563 | return InputEvent(EventType::EXTRA, KeyError::INTERRUPTED); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 564 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 565 | if (screensaver_state_ != ScreensaverState::DISABLED) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 566 | if (!rc) { |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 567 | // 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] | 568 | if (screensaver_state_ == ScreensaverState::NORMAL) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 569 | SetScreensaverState(ScreensaverState::DIMMED); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 570 | } else if (screensaver_state_ == ScreensaverState::DIMMED) { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 571 | SetScreensaverState(ScreensaverState::OFF); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 572 | } |
Tao Bao | 53be332 | 2018-08-16 11:48:50 -0700 | [diff] [blame] | 573 | } else if (screensaver_state_ != ScreensaverState::NORMAL) { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 574 | // Drop the first key if it's changing from OFF to NORMAL. |
| 575 | if (screensaver_state_ == ScreensaverState::OFF) { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 576 | if (event_queue_len > 0) { |
| 577 | memcpy(&event_queue[0], &event_queue[1], sizeof(int) * --event_queue_len); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | // Reset the brightness to normal. |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 582 | SetScreensaverState(ScreensaverState::NORMAL); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 583 | } |
| 584 | } |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 585 | } while (IsUsbConnected() && event_queue_len == 0); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 586 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 587 | InputEvent event; |
| 588 | if (event_queue_len > 0) { |
| 589 | event = event_queue[0]; |
| 590 | memcpy(&event_queue[0], &event_queue[1], sizeof(InputEvent) * --event_queue_len); |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 591 | } |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 592 | return event; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Tom Marshall | a9ac955 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 595 | void RecoveryUI::CancelWaitKey() { |
| 596 | EnqueueKey(KEY_REFRESH); |
| 597 | } |
| 598 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 599 | void RecoveryUI::InterruptKey() { |
| 600 | { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 601 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 602 | key_interrupted_ = true; |
| 603 | } |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 604 | event_queue_cond.notify_one(); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 607 | bool RecoveryUI::IsUsbConnected() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 608 | int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); |
| 609 | if (fd < 0) { |
| 610 | printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno)); |
| 611 | return 0; |
| 612 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 613 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 614 | char buf; |
| 615 | // USB is connected if android_usb state is CONNECTED or CONFIGURED. |
| 616 | int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); |
| 617 | if (close(fd) < 0) { |
| 618 | printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno)); |
| 619 | } |
| 620 | return connected; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 623 | bool RecoveryUI::IsKeyPressed(int key) { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 624 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 625 | int pressed = key_pressed[key]; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 626 | return pressed; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 629 | bool RecoveryUI::IsLongPress() { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 630 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 631 | bool result = key_long_press; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 632 | return result; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 635 | bool RecoveryUI::HasThreeButtons() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 636 | return has_power_key && has_up_key && has_down_key; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 639 | bool RecoveryUI::HasPowerKey() const { |
| 640 | return has_power_key; |
| 641 | } |
| 642 | |
| 643 | bool RecoveryUI::HasTouchScreen() const { |
| 644 | return has_touch_screen; |
| 645 | } |
| 646 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 647 | void RecoveryUI::FlushKeys() { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 648 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
| 649 | event_queue_len = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 652 | RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 653 | { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 654 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Jerry Zhang | b31f9ce | 2018-05-21 16:04:57 -0700 | [diff] [blame] | 655 | key_long_press = false; |
| 656 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 657 | |
| 658 | // 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] | 659 | if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) { |
| 660 | if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 661 | return TOGGLE; |
| 662 | } |
| 663 | } else { |
| 664 | // Otherwise long press of any button toggles to the text display, |
| 665 | // and there's no way to toggle back (but that's pretty useless anyway). |
| 666 | if (is_long_press && !IsTextVisible()) { |
| 667 | return TOGGLE; |
| 668 | } |
| 669 | |
| 670 | // Also, for button-limited devices, a long press is translated to KEY_ENTER. |
| 671 | if (is_long_press && IsTextVisible()) { |
| 672 | EnqueueKey(KEY_ENTER); |
| 673 | return IGNORE; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // Press power seven times in a row to reboot. |
| 678 | if (key == KEY_POWER) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 679 | bool reboot_enabled = enable_reboot; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 680 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 681 | if (reboot_enabled) { |
| 682 | ++consecutive_power_keys; |
| 683 | if (consecutive_power_keys >= 7) { |
| 684 | return REBOOT; |
| 685 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 686 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 687 | } else { |
| 688 | consecutive_power_keys = 0; |
| 689 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 690 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 691 | last_key = key; |
| 692 | return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 693 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 694 | |
Tianjie Xu | b5108c3 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 695 | void RecoveryUI::KeyLongPress(int) {} |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 696 | |
| 697 | void RecoveryUI::SetEnableReboot(bool enabled) { |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 698 | std::lock_guard<std::mutex> lg(event_queue_mutex); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 699 | enable_reboot = enabled; |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 700 | } |