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 | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 33 | #include <string> |
| 34 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 35 | #include <android-base/file.h> |
| 36 | #include <android-base/logging.h> |
| 37 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 38 | #include <android-base/properties.h> |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 40 | #include <cutils/android_reboot.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 41 | #include <minui/minui.h> |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 42 | |
| 43 | #include "common.h" |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 44 | #include "roots.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 45 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 46 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 47 | static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; |
| 48 | static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; |
| 49 | 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] | 50 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 51 | RecoveryUI::RecoveryUI() |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 52 | : locale_(""), |
| 53 | rtl_locale_(false), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 54 | brightness_normal_(50), |
| 55 | brightness_dimmed_(25), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 56 | key_queue_len(0), |
| 57 | key_last_down(-1), |
| 58 | key_long_press(false), |
| 59 | key_down_count(0), |
| 60 | enable_reboot(true), |
| 61 | consecutive_power_keys(0), |
| 62 | last_key(-1), |
| 63 | has_power_key(false), |
| 64 | has_up_key(false), |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 65 | has_down_key(false), |
| 66 | screensaver_state_(ScreensaverState::DISABLED) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 67 | pthread_mutex_init(&key_queue_mutex, nullptr); |
| 68 | pthread_cond_init(&key_queue_cond, nullptr); |
| 69 | memset(key_pressed, 0, sizeof(key_pressed)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 72 | void RecoveryUI::OnKeyDetected(int key_code) { |
| 73 | if (key_code == KEY_POWER) { |
| 74 | has_power_key = true; |
| 75 | } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { |
| 76 | has_down_key = true; |
| 77 | } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { |
| 78 | has_up_key = true; |
| 79 | } |
| 80 | } |
| 81 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 82 | // Reads input events, handles special hot keys, and adds to the key queue. |
| 83 | static void* InputThreadLoop(void*) { |
| 84 | while (true) { |
| 85 | if (!ev_wait(-1)) { |
| 86 | ev_dispatch(); |
| 87 | } |
| 88 | } |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 92 | bool RecoveryUI::InitScreensaver() { |
| 93 | // Disabled. |
| 94 | if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // Set the initial brightness level based on the max brightness. Note that reading the initial |
| 99 | // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so |
| 100 | // we don't have a good way to query the default value. |
| 101 | std::string content; |
| 102 | if (!android::base::ReadFileToString(MAX_BRIGHTNESS_FILE, &content)) { |
Tao Bao | 8eec373 | 2017-01-31 21:24:16 -0800 | [diff] [blame] | 103 | PLOG(WARNING) << "Failed to read max brightness"; |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | unsigned int max_value; |
| 108 | if (!android::base::ParseUint(android::base::Trim(content), &max_value)) { |
| 109 | LOG(WARNING) << "Failed to parse max brightness: " << content; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | brightness_normal_value_ = max_value * brightness_normal_ / 100.0; |
| 114 | brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0; |
| 115 | if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
| 116 | BRIGHTNESS_FILE)) { |
| 117 | PLOG(WARNING) << "Failed to set brightness"; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)"; |
| 122 | screensaver_state_ = ScreensaverState::NORMAL; |
| 123 | return true; |
| 124 | } |
| 125 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 126 | bool RecoveryUI::Init(const std::string& locale) { |
| 127 | // Set up the locale info. |
| 128 | SetLocale(locale); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 129 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 130 | ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2)); |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 131 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 132 | ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); |
| 133 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 134 | if (!InitScreensaver()) { |
| 135 | LOG(INFO) << "Screensaver disabled"; |
| 136 | } |
| 137 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 138 | pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr); |
| 139 | return true; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 143 | struct input_event ev; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 144 | if (ev_get_input(fd, epevents, &ev) == -1) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 145 | return -1; |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 146 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 147 | |
| 148 | if (ev.type == EV_SYN) { |
| 149 | return 0; |
| 150 | } else if (ev.type == EV_REL) { |
| 151 | if (ev.code == REL_Y) { |
| 152 | // accumulate the up or down motion reported by |
| 153 | // the trackball. When it exceeds a threshold |
| 154 | // (positive or negative), fake an up/down |
| 155 | // key event. |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 156 | rel_sum += ev.value; |
| 157 | if (rel_sum > 3) { |
| 158 | ProcessKey(KEY_DOWN, 1); // press down key |
| 159 | ProcessKey(KEY_DOWN, 0); // and release it |
| 160 | rel_sum = 0; |
| 161 | } else if (rel_sum < -3) { |
| 162 | ProcessKey(KEY_UP, 1); // press up key |
| 163 | ProcessKey(KEY_UP, 0); // and release it |
| 164 | rel_sum = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } else { |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 168 | rel_sum = 0; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 171 | if (ev.type == EV_KEY && ev.code <= KEY_MAX) { |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 172 | ProcessKey(ev.code, ev.value); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 173 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | // Process a key-up or -down event. A key is "registered" when it is |
| 179 | // pressed and then released, with no other keypresses or releases in |
| 180 | // between. Registered keys are passed to CheckKey() to see if it |
| 181 | // should trigger a visibility toggle, an immediate reboot, or be |
| 182 | // queued to be processed next time the foreground thread wants a key |
| 183 | // (eg, for the menu). |
| 184 | // |
| 185 | // We also keep track of which keys are currently down so that |
| 186 | // CheckKey can call IsKeyPressed to see what other keys are held when |
| 187 | // a key is registered. |
| 188 | // |
| 189 | // updown == 1 for key down events; 0 for key up events |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 190 | void RecoveryUI::ProcessKey(int key_code, int updown) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 191 | bool register_key = false; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 192 | bool long_press = false; |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 193 | bool reboot_enabled; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 194 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 195 | pthread_mutex_lock(&key_queue_mutex); |
| 196 | key_pressed[key_code] = updown; |
| 197 | if (updown) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 198 | ++key_down_count; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 199 | key_last_down = key_code; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 200 | key_long_press = false; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 201 | key_timer_t* info = new key_timer_t; |
| 202 | info->ui = this; |
| 203 | info->key_code = key_code; |
| 204 | info->count = key_down_count; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 205 | pthread_t thread; |
| 206 | pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info); |
| 207 | pthread_detach(thread); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 208 | } else { |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 209 | if (key_last_down == key_code) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 210 | long_press = key_long_press; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 211 | register_key = true; |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 212 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 213 | key_last_down = -1; |
| 214 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 215 | reboot_enabled = enable_reboot; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 216 | pthread_mutex_unlock(&key_queue_mutex); |
| 217 | |
| 218 | if (register_key) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 219 | switch (CheckKey(key_code, long_press)) { |
Doug Zongker | 48b5b07 | 2012-01-18 13:46:26 -0800 | [diff] [blame] | 220 | case RecoveryUI::IGNORE: |
| 221 | break; |
| 222 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 223 | case RecoveryUI::TOGGLE: |
| 224 | ShowText(!IsTextVisible()); |
| 225 | break; |
| 226 | |
| 227 | case RecoveryUI::REBOOT: |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 228 | if (reboot_enabled) { |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 229 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,"); |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 230 | while (true) { pause(); } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 231 | } |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 232 | break; |
| 233 | |
| 234 | case RecoveryUI::ENQUEUE: |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 235 | EnqueueKey(key_code); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 241 | void* RecoveryUI::time_key_helper(void* cookie) { |
| 242 | key_timer_t* info = (key_timer_t*) cookie; |
| 243 | info->ui->time_key(info->key_code, info->count); |
| 244 | delete info; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 245 | return nullptr; |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void RecoveryUI::time_key(int key_code, int count) { |
| 249 | usleep(750000); // 750 ms == "long" |
| 250 | bool long_press = false; |
| 251 | pthread_mutex_lock(&key_queue_mutex); |
| 252 | if (key_last_down == key_code && key_down_count == count) { |
| 253 | long_press = key_long_press = true; |
| 254 | } |
| 255 | pthread_mutex_unlock(&key_queue_mutex); |
| 256 | if (long_press) KeyLongPress(key_code); |
| 257 | } |
| 258 | |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 259 | void RecoveryUI::EnqueueKey(int key_code) { |
| 260 | pthread_mutex_lock(&key_queue_mutex); |
| 261 | const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); |
| 262 | if (key_queue_len < queue_max) { |
| 263 | key_queue[key_queue_len++] = key_code; |
| 264 | pthread_cond_signal(&key_queue_cond); |
| 265 | } |
| 266 | pthread_mutex_unlock(&key_queue_mutex); |
| 267 | } |
| 268 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 269 | int RecoveryUI::WaitKey() { |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 270 | pthread_mutex_lock(&key_queue_mutex); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 271 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 272 | // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is |
| 273 | // plugged in. |
| 274 | do { |
| 275 | struct timeval now; |
| 276 | struct timespec timeout; |
| 277 | gettimeofday(&now, nullptr); |
| 278 | timeout.tv_sec = now.tv_sec; |
| 279 | timeout.tv_nsec = now.tv_usec * 1000; |
| 280 | timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 281 | |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 282 | int rc = 0; |
| 283 | while (key_queue_len == 0 && rc != ETIMEDOUT) { |
| 284 | rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 285 | } |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 286 | |
| 287 | if (screensaver_state_ != ScreensaverState::DISABLED) { |
| 288 | if (rc == ETIMEDOUT) { |
| 289 | // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. |
| 290 | if (screensaver_state_ == ScreensaverState::NORMAL) { |
| 291 | if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_), |
| 292 | BRIGHTNESS_FILE)) { |
| 293 | LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_ |
| 294 | << "%)"; |
| 295 | screensaver_state_ = ScreensaverState::DIMMED; |
| 296 | } |
| 297 | } else if (screensaver_state_ == ScreensaverState::DIMMED) { |
| 298 | if (android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) { |
| 299 | LOG(INFO) << "Brightness: 0 (off)"; |
| 300 | screensaver_state_ = ScreensaverState::OFF; |
| 301 | } |
| 302 | } |
| 303 | } else if (screensaver_state_ != ScreensaverState::NORMAL) { |
| 304 | // Drop the first key if it's changing from OFF to NORMAL. |
| 305 | if (screensaver_state_ == ScreensaverState::OFF) { |
| 306 | if (key_queue_len > 0) { |
| 307 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Reset the brightness to normal. |
| 312 | if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_), |
| 313 | BRIGHTNESS_FILE)) { |
| 314 | screensaver_state_ = ScreensaverState::NORMAL; |
| 315 | LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ |
| 316 | << "%)"; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } while (IsUsbConnected() && key_queue_len == 0); |
| 321 | |
| 322 | int key = -1; |
| 323 | if (key_queue_len > 0) { |
| 324 | key = key_queue[0]; |
| 325 | memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); |
| 326 | } |
| 327 | pthread_mutex_unlock(&key_queue_mutex); |
| 328 | return key; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 331 | bool RecoveryUI::IsUsbConnected() { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 332 | int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); |
| 333 | if (fd < 0) { |
| 334 | printf("failed to open /sys/class/android_usb/android0/state: %s\n", |
| 335 | strerror(errno)); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | char buf; |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 340 | // USB is connected if android_usb state is CONNECTED or CONFIGURED. |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 341 | int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 342 | if (close(fd) < 0) { |
| 343 | printf("failed to close /sys/class/android_usb/android0/state: %s\n", |
| 344 | strerror(errno)); |
| 345 | } |
| 346 | return connected; |
| 347 | } |
| 348 | |
Elliott Hughes | ec28340 | 2015-04-10 10:01:53 -0700 | [diff] [blame] | 349 | bool RecoveryUI::IsKeyPressed(int key) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 350 | pthread_mutex_lock(&key_queue_mutex); |
| 351 | int pressed = key_pressed[key]; |
| 352 | pthread_mutex_unlock(&key_queue_mutex); |
| 353 | return pressed; |
| 354 | } |
| 355 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 356 | bool RecoveryUI::IsLongPress() { |
| 357 | pthread_mutex_lock(&key_queue_mutex); |
| 358 | bool result = key_long_press; |
| 359 | pthread_mutex_unlock(&key_queue_mutex); |
| 360 | return result; |
| 361 | } |
| 362 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 363 | bool RecoveryUI::HasThreeButtons() { |
| 364 | return has_power_key && has_up_key && has_down_key; |
| 365 | } |
| 366 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 367 | void RecoveryUI::FlushKeys() { |
| 368 | pthread_mutex_lock(&key_queue_mutex); |
| 369 | key_queue_len = 0; |
| 370 | pthread_mutex_unlock(&key_queue_mutex); |
| 371 | } |
| 372 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 373 | RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { |
| 374 | pthread_mutex_lock(&key_queue_mutex); |
| 375 | key_long_press = false; |
| 376 | pthread_mutex_unlock(&key_queue_mutex); |
| 377 | |
| 378 | // If we have power and volume up keys, that chord is the signal to toggle the text display. |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 379 | if (HasThreeButtons()) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 380 | if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) { |
| 381 | return TOGGLE; |
| 382 | } |
| 383 | } else { |
| 384 | // Otherwise long press of any button toggles to the text display, |
| 385 | // and there's no way to toggle back (but that's pretty useless anyway). |
| 386 | if (is_long_press && !IsTextVisible()) { |
| 387 | return TOGGLE; |
| 388 | } |
| 389 | |
| 390 | // Also, for button-limited devices, a long press is translated to KEY_ENTER. |
| 391 | if (is_long_press && IsTextVisible()) { |
| 392 | EnqueueKey(KEY_ENTER); |
| 393 | return IGNORE; |
| 394 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 397 | // Press power seven times in a row to reboot. |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 398 | if (key == KEY_POWER) { |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 399 | pthread_mutex_lock(&key_queue_mutex); |
| 400 | bool reboot_enabled = enable_reboot; |
| 401 | pthread_mutex_unlock(&key_queue_mutex); |
| 402 | |
| 403 | if (reboot_enabled) { |
| 404 | ++consecutive_power_keys; |
| 405 | if (consecutive_power_keys >= 7) { |
| 406 | return REBOOT; |
| 407 | } |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 408 | } |
| 409 | } else { |
| 410 | consecutive_power_keys = 0; |
| 411 | } |
| 412 | |
Doug Zongker | 9e805d6 | 2013-09-04 13:44:38 -0700 | [diff] [blame] | 413 | last_key = key; |
Tao Bao | 6278bdf | 2017-01-16 17:38:18 -0800 | [diff] [blame] | 414 | return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 415 | } |
Doug Zongker | bb01d0c | 2012-12-17 10:52:58 -0800 | [diff] [blame] | 416 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 417 | void RecoveryUI::KeyLongPress(int) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 418 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 419 | |
| 420 | void RecoveryUI::SetEnableReboot(bool enabled) { |
| 421 | pthread_mutex_lock(&key_queue_mutex); |
| 422 | enable_reboot = enabled; |
| 423 | pthread_mutex_unlock(&key_queue_mutex); |
| 424 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 425 | |
| 426 | void RecoveryUI::SetLocale(const std::string& new_locale) { |
| 427 | this->locale_ = new_locale; |
| 428 | this->rtl_locale_ = false; |
| 429 | |
| 430 | if (!new_locale.empty()) { |
| 431 | size_t underscore = new_locale.find('_'); |
| 432 | // lang has the language prefix prior to '_', or full string if '_' doesn't exist. |
| 433 | std::string lang = new_locale.substr(0, underscore); |
| 434 | |
| 435 | // A bit cheesy: keep an explicit list of supported RTL languages. |
| 436 | if (lang == "ar" || // Arabic |
| 437 | lang == "fa" || // Persian (Farsi) |
| 438 | lang == "he" || // Hebrew (new language code) |
| 439 | lang == "iw" || // Hebrew (old language code) |
| 440 | lang == "ur") { // Urdu |
| 441 | rtl_locale_ = true; |
| 442 | } |
| 443 | } |
| 444 | } |