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