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