2011-11-01 19:00:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
#include "ui.h"
|
|
|
|
|
2011-11-01 19:00:20 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
#include <string>
|
|
|
|
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <android-base/properties.h>
|
2011-11-01 19:00:20 +01:00
|
|
|
#include <cutils/android_reboot.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2013-09-04 22:44:38 +02:00
|
|
|
#include "roots.h"
|
2011-11-01 19:00:20 +01:00
|
|
|
#include "device.h"
|
|
|
|
#include "minui/minui.h"
|
|
|
|
|
|
|
|
#define UI_WAIT_KEY_TIMEOUT_SEC 120
|
|
|
|
|
2015-04-10 19:01:53 +02:00
|
|
|
RecoveryUI::RecoveryUI()
|
2017-01-03 19:15:33 +01:00
|
|
|
: locale_(""),
|
|
|
|
rtl_locale_(false),
|
|
|
|
key_queue_len(0),
|
|
|
|
key_last_down(-1),
|
|
|
|
key_long_press(false),
|
|
|
|
key_down_count(0),
|
|
|
|
enable_reboot(true),
|
|
|
|
consecutive_power_keys(0),
|
|
|
|
last_key(-1),
|
|
|
|
has_power_key(false),
|
|
|
|
has_up_key(false),
|
|
|
|
has_down_key(false) {
|
|
|
|
pthread_mutex_init(&key_queue_mutex, nullptr);
|
|
|
|
pthread_cond_init(&key_queue_cond, nullptr);
|
|
|
|
memset(key_pressed, 0, sizeof(key_pressed));
|
2011-11-01 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
void RecoveryUI::OnKeyDetected(int key_code) {
|
|
|
|
if (key_code == KEY_POWER) {
|
|
|
|
has_power_key = true;
|
|
|
|
} else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
|
|
|
|
has_down_key = true;
|
|
|
|
} else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
|
|
|
|
has_up_key = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:04:32 +02:00
|
|
|
int RecoveryUI::InputCallback(int fd, uint32_t epevents, void* data) {
|
|
|
|
return reinterpret_cast<RecoveryUI*>(data)->OnInputEvent(fd, epevents);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads input events, handles special hot keys, and adds to the key queue.
|
|
|
|
static void* InputThreadLoop(void*) {
|
|
|
|
while (true) {
|
|
|
|
if (!ev_wait(-1)) {
|
|
|
|
ev_dispatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
bool RecoveryUI::Init(const std::string& locale) {
|
|
|
|
// Set up the locale info.
|
|
|
|
SetLocale(locale);
|
2015-04-10 21:47:46 +02:00
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
ev_init(InputCallback, this);
|
2015-04-10 21:47:46 +02:00
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
|
|
|
|
|
|
|
|
pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
|
|
|
|
return true;
|
2011-11-01 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:04:32 +02:00
|
|
|
int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
|
2011-11-01 19:00:20 +01:00
|
|
|
struct input_event ev;
|
2015-04-10 21:47:46 +02:00
|
|
|
if (ev_get_input(fd, epevents, &ev) == -1) {
|
2011-11-01 19:00:20 +01:00
|
|
|
return -1;
|
2015-04-10 21:47:46 +02:00
|
|
|
}
|
2011-11-01 19:00:20 +01:00
|
|
|
|
|
|
|
if (ev.type == EV_SYN) {
|
|
|
|
return 0;
|
|
|
|
} else if (ev.type == EV_REL) {
|
|
|
|
if (ev.code == REL_Y) {
|
|
|
|
// accumulate the up or down motion reported by
|
|
|
|
// the trackball. When it exceeds a threshold
|
|
|
|
// (positive or negative), fake an up/down
|
|
|
|
// key event.
|
2015-04-13 22:04:32 +02:00
|
|
|
rel_sum += ev.value;
|
|
|
|
if (rel_sum > 3) {
|
|
|
|
ProcessKey(KEY_DOWN, 1); // press down key
|
|
|
|
ProcessKey(KEY_DOWN, 0); // and release it
|
|
|
|
rel_sum = 0;
|
|
|
|
} else if (rel_sum < -3) {
|
|
|
|
ProcessKey(KEY_UP, 1); // press up key
|
|
|
|
ProcessKey(KEY_UP, 0); // and release it
|
|
|
|
rel_sum = 0;
|
2011-11-01 19:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-13 22:04:32 +02:00
|
|
|
rel_sum = 0;
|
2011-11-01 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
|
2015-04-13 22:04:32 +02:00
|
|
|
ProcessKey(ev.code, ev.value);
|
2015-04-10 21:47:46 +02:00
|
|
|
}
|
2011-11-01 19:00:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process a key-up or -down event. A key is "registered" when it is
|
|
|
|
// pressed and then released, with no other keypresses or releases in
|
|
|
|
// between. Registered keys are passed to CheckKey() to see if it
|
|
|
|
// should trigger a visibility toggle, an immediate reboot, or be
|
|
|
|
// queued to be processed next time the foreground thread wants a key
|
|
|
|
// (eg, for the menu).
|
|
|
|
//
|
|
|
|
// We also keep track of which keys are currently down so that
|
|
|
|
// CheckKey can call IsKeyPressed to see what other keys are held when
|
|
|
|
// a key is registered.
|
|
|
|
//
|
|
|
|
// updown == 1 for key down events; 0 for key up events
|
2015-04-13 22:04:32 +02:00
|
|
|
void RecoveryUI::ProcessKey(int key_code, int updown) {
|
2011-11-01 19:00:20 +01:00
|
|
|
bool register_key = false;
|
2012-12-17 19:52:58 +01:00
|
|
|
bool long_press = false;
|
2014-05-23 17:40:35 +02:00
|
|
|
bool reboot_enabled;
|
2012-12-17 19:52:58 +01:00
|
|
|
|
2011-11-01 19:00:20 +01:00
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
key_pressed[key_code] = updown;
|
|
|
|
if (updown) {
|
2013-07-31 20:28:24 +02:00
|
|
|
++key_down_count;
|
2011-11-01 19:00:20 +01:00
|
|
|
key_last_down = key_code;
|
2013-07-31 20:28:24 +02:00
|
|
|
key_long_press = false;
|
|
|
|
key_timer_t* info = new key_timer_t;
|
|
|
|
info->ui = this;
|
|
|
|
info->key_code = key_code;
|
|
|
|
info->count = key_down_count;
|
2015-04-13 22:04:32 +02:00
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
|
|
|
|
pthread_detach(thread);
|
2011-11-01 19:00:20 +01:00
|
|
|
} else {
|
2012-12-17 19:52:58 +01:00
|
|
|
if (key_last_down == key_code) {
|
2013-07-31 20:28:24 +02:00
|
|
|
long_press = key_long_press;
|
2011-11-01 19:00:20 +01:00
|
|
|
register_key = true;
|
2012-12-17 19:52:58 +01:00
|
|
|
}
|
2011-11-01 19:00:20 +01:00
|
|
|
key_last_down = -1;
|
|
|
|
}
|
2014-05-23 17:40:35 +02:00
|
|
|
reboot_enabled = enable_reboot;
|
2011-11-01 19:00:20 +01:00
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
|
|
|
|
if (register_key) {
|
2015-04-10 21:47:46 +02:00
|
|
|
switch (CheckKey(key_code, long_press)) {
|
2012-01-18 22:46:26 +01:00
|
|
|
case RecoveryUI::IGNORE:
|
|
|
|
break;
|
|
|
|
|
2011-11-01 19:00:20 +01:00
|
|
|
case RecoveryUI::TOGGLE:
|
|
|
|
ShowText(!IsTextVisible());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecoveryUI::REBOOT:
|
2014-05-23 17:40:35 +02:00
|
|
|
if (reboot_enabled) {
|
2016-09-24 00:30:55 +02:00
|
|
|
android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
|
2015-05-27 23:46:17 +02:00
|
|
|
while (true) { pause(); }
|
2014-05-23 17:40:35 +02:00
|
|
|
}
|
2011-11-01 19:00:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RecoveryUI::ENQUEUE:
|
2012-12-17 19:52:58 +01:00
|
|
|
EnqueueKey(key_code);
|
2011-11-01 19:00:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 20:28:24 +02:00
|
|
|
void* RecoveryUI::time_key_helper(void* cookie) {
|
|
|
|
key_timer_t* info = (key_timer_t*) cookie;
|
|
|
|
info->ui->time_key(info->key_code, info->count);
|
|
|
|
delete info;
|
2015-04-13 22:04:32 +02:00
|
|
|
return nullptr;
|
2013-07-31 20:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecoveryUI::time_key(int key_code, int count) {
|
|
|
|
usleep(750000); // 750 ms == "long"
|
|
|
|
bool long_press = false;
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
if (key_last_down == key_code && key_down_count == count) {
|
|
|
|
long_press = key_long_press = true;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
if (long_press) KeyLongPress(key_code);
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:52:58 +01:00
|
|
|
void RecoveryUI::EnqueueKey(int key_code) {
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
|
|
|
|
if (key_queue_len < queue_max) {
|
|
|
|
key_queue[key_queue_len++] = key_code;
|
|
|
|
pthread_cond_signal(&key_queue_cond);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:01:53 +02:00
|
|
|
int RecoveryUI::WaitKey() {
|
2011-11-01 19:00:20 +01:00
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
|
|
|
|
// Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
|
|
|
|
// plugged in.
|
|
|
|
do {
|
|
|
|
struct timeval now;
|
|
|
|
struct timespec timeout;
|
2015-04-13 22:04:32 +02:00
|
|
|
gettimeofday(&now, nullptr);
|
2011-11-01 19:00:20 +01:00
|
|
|
timeout.tv_sec = now.tv_sec;
|
|
|
|
timeout.tv_nsec = now.tv_usec * 1000;
|
|
|
|
timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
while (key_queue_len == 0 && rc != ETIMEDOUT) {
|
2015-04-10 19:01:53 +02:00
|
|
|
rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
|
2011-11-01 19:00:20 +01:00
|
|
|
}
|
2015-04-13 22:04:32 +02:00
|
|
|
} while (IsUsbConnected() && key_queue_len == 0);
|
2011-11-01 19:00:20 +01:00
|
|
|
|
|
|
|
int key = -1;
|
|
|
|
if (key_queue_len > 0) {
|
|
|
|
key = key_queue[0];
|
|
|
|
memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:04:32 +02:00
|
|
|
bool RecoveryUI::IsUsbConnected() {
|
2011-11-01 19:00:20 +01:00
|
|
|
int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("failed to open /sys/class/android_usb/android0/state: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf;
|
2015-04-13 22:04:32 +02:00
|
|
|
// USB is connected if android_usb state is CONNECTED or CONFIGURED.
|
2015-04-29 02:24:24 +02:00
|
|
|
int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
|
2011-11-01 19:00:20 +01:00
|
|
|
if (close(fd) < 0) {
|
|
|
|
printf("failed to close /sys/class/android_usb/android0/state: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:01:53 +02:00
|
|
|
bool RecoveryUI::IsKeyPressed(int key) {
|
2011-11-01 19:00:20 +01:00
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
int pressed = key_pressed[key];
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
return pressed;
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
bool RecoveryUI::IsLongPress() {
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
bool result = key_long_press;
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:00:34 +02:00
|
|
|
bool RecoveryUI::HasThreeButtons() {
|
|
|
|
return has_power_key && has_up_key && has_down_key;
|
|
|
|
}
|
|
|
|
|
2011-11-01 19:00:20 +01:00
|
|
|
void RecoveryUI::FlushKeys() {
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
key_queue_len = 0;
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
key_long_press = false;
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
|
|
|
|
// If we have power and volume up keys, that chord is the signal to toggle the text display.
|
2015-04-11 00:00:34 +02:00
|
|
|
if (HasThreeButtons()) {
|
2015-04-10 21:47:46 +02:00
|
|
|
if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
|
|
|
|
return TOGGLE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise long press of any button toggles to the text display,
|
|
|
|
// and there's no way to toggle back (but that's pretty useless anyway).
|
|
|
|
if (is_long_press && !IsTextVisible()) {
|
|
|
|
return TOGGLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also, for button-limited devices, a long press is translated to KEY_ENTER.
|
|
|
|
if (is_long_press && IsTextVisible()) {
|
|
|
|
EnqueueKey(KEY_ENTER);
|
|
|
|
return IGNORE;
|
|
|
|
}
|
2013-09-04 22:44:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
// Press power seven times in a row to reboot.
|
2013-09-04 22:44:38 +02:00
|
|
|
if (key == KEY_POWER) {
|
2014-05-23 17:40:35 +02:00
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
bool reboot_enabled = enable_reboot;
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
|
|
|
|
if (reboot_enabled) {
|
|
|
|
++consecutive_power_keys;
|
|
|
|
if (consecutive_power_keys >= 7) {
|
|
|
|
return REBOOT;
|
|
|
|
}
|
2013-09-04 22:44:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
consecutive_power_keys = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_key = key;
|
2015-04-10 21:47:46 +02:00
|
|
|
return IsTextVisible() ? ENQUEUE : IGNORE;
|
2012-12-17 19:52:58 +01:00
|
|
|
}
|
2013-07-31 20:28:24 +02:00
|
|
|
|
2015-04-10 21:47:46 +02:00
|
|
|
void RecoveryUI::KeyLongPress(int) {
|
2013-07-31 20:28:24 +02:00
|
|
|
}
|
2014-05-23 17:40:35 +02:00
|
|
|
|
|
|
|
void RecoveryUI::SetEnableReboot(bool enabled) {
|
|
|
|
pthread_mutex_lock(&key_queue_mutex);
|
|
|
|
enable_reboot = enabled;
|
|
|
|
pthread_mutex_unlock(&key_queue_mutex);
|
|
|
|
}
|
2017-01-03 19:15:33 +01:00
|
|
|
|
|
|
|
void RecoveryUI::SetLocale(const std::string& new_locale) {
|
|
|
|
this->locale_ = new_locale;
|
|
|
|
this->rtl_locale_ = false;
|
|
|
|
|
|
|
|
if (!new_locale.empty()) {
|
|
|
|
size_t underscore = new_locale.find('_');
|
|
|
|
// lang has the language prefix prior to '_', or full string if '_' doesn't exist.
|
|
|
|
std::string lang = new_locale.substr(0, underscore);
|
|
|
|
|
|
|
|
// A bit cheesy: keep an explicit list of supported RTL languages.
|
|
|
|
if (lang == "ar" || // Arabic
|
|
|
|
lang == "fa" || // Persian (Farsi)
|
|
|
|
lang == "he" || // Hebrew (new language code)
|
|
|
|
lang == "iw" || // Hebrew (old language code)
|
|
|
|
lang == "ur") { // Urdu
|
|
|
|
rtl_locale_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|