blob: dbe5c51640c210dfa4681572231f7d05d6cf411e [file] [log] [blame]
Doug Zongker32a0a472011-11-01 11:00:20 -07001/*
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
17#include <errno.h>
18#include <fcntl.h>
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <cutils/android_reboot.h>
32
33#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070034#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070035#include "device.h"
36#include "minui/minui.h"
37#include "screen_ui.h"
38#include "ui.h"
39
40#define UI_WAIT_KEY_TIMEOUT_SEC 120
41
42// There's only (at most) one of these objects, and global callbacks
43// (for pthread_create, and the input event system) need to find it,
44// so use a global variable.
45static RecoveryUI* self = NULL;
46
Elliott Hughesec283402015-04-10 10:01:53 -070047RecoveryUI::RecoveryUI()
48 : key_queue_len(0),
49 key_last_down(-1),
50 key_long_press(false),
51 key_down_count(0),
52 enable_reboot(true),
53 consecutive_power_keys(0),
Elliott Hughes642aaa72015-04-10 12:47:46 -070054 last_key(-1),
55 has_power_key(false),
56 has_up_key(false),
57 has_down_key(false) {
Doug Zongker32a0a472011-11-01 11:00:20 -070058 pthread_mutex_init(&key_queue_mutex, NULL);
59 pthread_cond_init(&key_queue_cond, NULL);
60 self = this;
Mihai Serban187d6262014-06-06 15:23:20 +030061 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070062}
63
Elliott Hughes642aaa72015-04-10 12:47:46 -070064void RecoveryUI::OnKeyDetected(int key_code) {
65 if (key_code == KEY_POWER) {
66 has_power_key = true;
67 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
68 has_down_key = true;
69 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
70 has_up_key = true;
71 }
72}
73
Doug Zongker32a0a472011-11-01 11:00:20 -070074void RecoveryUI::Init() {
75 ev_init(input_callback, NULL);
Elliott Hughes642aaa72015-04-10 12:47:46 -070076
77 using namespace std::placeholders;
78 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, _1));
79
Doug Zongker32a0a472011-11-01 11:00:20 -070080 pthread_create(&input_t, NULL, input_thread, NULL);
81}
82
Elliott Hughesec283402015-04-10 10:01:53 -070083int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) {
Doug Zongker32a0a472011-11-01 11:00:20 -070084 struct input_event ev;
Elliott Hughes642aaa72015-04-10 12:47:46 -070085 if (ev_get_input(fd, epevents, &ev) == -1) {
Doug Zongker32a0a472011-11-01 11:00:20 -070086 return -1;
Elliott Hughes642aaa72015-04-10 12:47:46 -070087 }
Doug Zongker32a0a472011-11-01 11:00:20 -070088
89 if (ev.type == EV_SYN) {
90 return 0;
91 } else if (ev.type == EV_REL) {
92 if (ev.code == REL_Y) {
93 // accumulate the up or down motion reported by
94 // the trackball. When it exceeds a threshold
95 // (positive or negative), fake an up/down
96 // key event.
97 self->rel_sum += ev.value;
98 if (self->rel_sum > 3) {
99 self->process_key(KEY_DOWN, 1); // press down key
100 self->process_key(KEY_DOWN, 0); // and release it
101 self->rel_sum = 0;
102 } else if (self->rel_sum < -3) {
103 self->process_key(KEY_UP, 1); // press up key
104 self->process_key(KEY_UP, 0); // and release it
105 self->rel_sum = 0;
106 }
107 }
108 } else {
109 self->rel_sum = 0;
110 }
111
Elliott Hughes642aaa72015-04-10 12:47:46 -0700112 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700113 self->process_key(ev.code, ev.value);
Elliott Hughes642aaa72015-04-10 12:47:46 -0700114 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700115
116 return 0;
117}
118
119// Process a key-up or -down event. A key is "registered" when it is
120// pressed and then released, with no other keypresses or releases in
121// between. Registered keys are passed to CheckKey() to see if it
122// should trigger a visibility toggle, an immediate reboot, or be
123// queued to be processed next time the foreground thread wants a key
124// (eg, for the menu).
125//
126// We also keep track of which keys are currently down so that
127// CheckKey can call IsKeyPressed to see what other keys are held when
128// a key is registered.
129//
130// updown == 1 for key down events; 0 for key up events
131void RecoveryUI::process_key(int key_code, int updown) {
132 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800133 bool long_press = false;
Doug Zongkerc704e062014-05-23 08:40:35 -0700134 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800135
Doug Zongker32a0a472011-11-01 11:00:20 -0700136 pthread_mutex_lock(&key_queue_mutex);
137 key_pressed[key_code] = updown;
138 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700139 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700140 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700141 key_long_press = false;
142 pthread_t th;
143 key_timer_t* info = new key_timer_t;
144 info->ui = this;
145 info->key_code = key_code;
146 info->count = key_down_count;
147 pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info);
148 pthread_detach(th);
Doug Zongker32a0a472011-11-01 11:00:20 -0700149 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800150 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700151 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700152 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800153 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700154 key_last_down = -1;
155 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700156 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700157 pthread_mutex_unlock(&key_queue_mutex);
158
159 if (register_key) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700160 switch (CheckKey(key_code, long_press)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800161 case RecoveryUI::IGNORE:
162 break;
163
Doug Zongker32a0a472011-11-01 11:00:20 -0700164 case RecoveryUI::TOGGLE:
165 ShowText(!IsTextVisible());
166 break;
167
168 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700169 if (reboot_enabled) {
170 android_reboot(ANDROID_RB_RESTART, 0, 0);
171 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700172 break;
173
174 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800175 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700176 break;
177 }
178 }
179}
180
Doug Zongkerc0441d12013-07-31 11:28:24 -0700181void* RecoveryUI::time_key_helper(void* cookie) {
182 key_timer_t* info = (key_timer_t*) cookie;
183 info->ui->time_key(info->key_code, info->count);
184 delete info;
185 return NULL;
186}
187
188void RecoveryUI::time_key(int key_code, int count) {
189 usleep(750000); // 750 ms == "long"
190 bool long_press = false;
191 pthread_mutex_lock(&key_queue_mutex);
192 if (key_last_down == key_code && key_down_count == count) {
193 long_press = key_long_press = true;
194 }
195 pthread_mutex_unlock(&key_queue_mutex);
196 if (long_press) KeyLongPress(key_code);
197}
198
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800199void RecoveryUI::EnqueueKey(int key_code) {
200 pthread_mutex_lock(&key_queue_mutex);
201 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
202 if (key_queue_len < queue_max) {
203 key_queue[key_queue_len++] = key_code;
204 pthread_cond_signal(&key_queue_cond);
205 }
206 pthread_mutex_unlock(&key_queue_mutex);
207}
208
209
Doug Zongker32a0a472011-11-01 11:00:20 -0700210// Reads input events, handles special hot keys, and adds to the key queue.
Elliott Hughesec283402015-04-10 10:01:53 -0700211void* RecoveryUI::input_thread(void* cookie) {
212 while (true) {
213 if (!ev_wait(-1)) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700214 ev_dispatch();
Elliott Hughesec283402015-04-10 10:01:53 -0700215 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700216 }
217 return NULL;
218}
219
Elliott Hughesec283402015-04-10 10:01:53 -0700220int RecoveryUI::WaitKey() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700221 pthread_mutex_lock(&key_queue_mutex);
222
223 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
224 // plugged in.
225 do {
226 struct timeval now;
227 struct timespec timeout;
228 gettimeofday(&now, NULL);
229 timeout.tv_sec = now.tv_sec;
230 timeout.tv_nsec = now.tv_usec * 1000;
231 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
232
233 int rc = 0;
234 while (key_queue_len == 0 && rc != ETIMEDOUT) {
Elliott Hughesec283402015-04-10 10:01:53 -0700235 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700236 }
237 } while (usb_connected() && key_queue_len == 0);
238
239 int key = -1;
240 if (key_queue_len > 0) {
241 key = key_queue[0];
242 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
243 }
244 pthread_mutex_unlock(&key_queue_mutex);
245 return key;
246}
247
248// Return true if USB is connected.
249bool RecoveryUI::usb_connected() {
250 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
251 if (fd < 0) {
252 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
253 strerror(errno));
254 return 0;
255 }
256
257 char buf;
258 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
259 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
260 if (close(fd) < 0) {
261 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
262 strerror(errno));
263 }
264 return connected;
265}
266
Elliott Hughesec283402015-04-10 10:01:53 -0700267bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700268 pthread_mutex_lock(&key_queue_mutex);
269 int pressed = key_pressed[key];
270 pthread_mutex_unlock(&key_queue_mutex);
271 return pressed;
272}
273
Elliott Hughes642aaa72015-04-10 12:47:46 -0700274bool RecoveryUI::IsLongPress() {
275 pthread_mutex_lock(&key_queue_mutex);
276 bool result = key_long_press;
277 pthread_mutex_unlock(&key_queue_mutex);
278 return result;
279}
280
Elliott Hughes4af215b2015-04-10 15:00:34 -0700281bool RecoveryUI::HasThreeButtons() {
282 return has_power_key && has_up_key && has_down_key;
283}
284
Doug Zongker32a0a472011-11-01 11:00:20 -0700285void RecoveryUI::FlushKeys() {
286 pthread_mutex_lock(&key_queue_mutex);
287 key_queue_len = 0;
288 pthread_mutex_unlock(&key_queue_mutex);
289}
290
Elliott Hughes642aaa72015-04-10 12:47:46 -0700291RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
292 pthread_mutex_lock(&key_queue_mutex);
293 key_long_press = false;
294 pthread_mutex_unlock(&key_queue_mutex);
295
296 // If we have power and volume up keys, that chord is the signal to toggle the text display.
Elliott Hughes4af215b2015-04-10 15:00:34 -0700297 if (HasThreeButtons()) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700298 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
299 return TOGGLE;
300 }
301 } else {
302 // Otherwise long press of any button toggles to the text display,
303 // and there's no way to toggle back (but that's pretty useless anyway).
304 if (is_long_press && !IsTextVisible()) {
305 return TOGGLE;
306 }
307
308 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
309 if (is_long_press && IsTextVisible()) {
310 EnqueueKey(KEY_ENTER);
311 return IGNORE;
312 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700313 }
314
Elliott Hughes642aaa72015-04-10 12:47:46 -0700315 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700316 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700317 pthread_mutex_lock(&key_queue_mutex);
318 bool reboot_enabled = enable_reboot;
319 pthread_mutex_unlock(&key_queue_mutex);
320
321 if (reboot_enabled) {
322 ++consecutive_power_keys;
323 if (consecutive_power_keys >= 7) {
324 return REBOOT;
325 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700326 }
327 } else {
328 consecutive_power_keys = 0;
329 }
330
Doug Zongker9e805d62013-09-04 13:44:38 -0700331 last_key = key;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700332 return IsTextVisible() ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700333}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800334
Elliott Hughes642aaa72015-04-10 12:47:46 -0700335void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700336}
Doug Zongkerc704e062014-05-23 08:40:35 -0700337
338void RecoveryUI::SetEnableReboot(bool enabled) {
339 pthread_mutex_lock(&key_queue_mutex);
340 enable_reboot = enabled;
341 pthread_mutex_unlock(&key_queue_mutex);
342}