blob: 064890ea4ef5f1a00f05603f36d9191ae20afc39 [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
Doug Zongker32a0a472011-11-01 11:00:20 -0700281void RecoveryUI::FlushKeys() {
282 pthread_mutex_lock(&key_queue_mutex);
283 key_queue_len = 0;
284 pthread_mutex_unlock(&key_queue_mutex);
285}
286
Elliott Hughes642aaa72015-04-10 12:47:46 -0700287RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
288 pthread_mutex_lock(&key_queue_mutex);
289 key_long_press = false;
290 pthread_mutex_unlock(&key_queue_mutex);
291
292 // If we have power and volume up keys, that chord is the signal to toggle the text display.
293 if (has_power_key && has_up_key) {
294 if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) {
295 return TOGGLE;
296 }
297 } else {
298 // Otherwise long press of any button toggles to the text display,
299 // and there's no way to toggle back (but that's pretty useless anyway).
300 if (is_long_press && !IsTextVisible()) {
301 return TOGGLE;
302 }
303
304 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
305 if (is_long_press && IsTextVisible()) {
306 EnqueueKey(KEY_ENTER);
307 return IGNORE;
308 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700309 }
310
Elliott Hughes642aaa72015-04-10 12:47:46 -0700311 // Press power seven times in a row to reboot.
Doug Zongker9e805d62013-09-04 13:44:38 -0700312 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700313 pthread_mutex_lock(&key_queue_mutex);
314 bool reboot_enabled = enable_reboot;
315 pthread_mutex_unlock(&key_queue_mutex);
316
317 if (reboot_enabled) {
318 ++consecutive_power_keys;
319 if (consecutive_power_keys >= 7) {
320 return REBOOT;
321 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700322 }
323 } else {
324 consecutive_power_keys = 0;
325 }
326
Doug Zongker9e805d62013-09-04 13:44:38 -0700327 last_key = key;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700328 return IsTextVisible() ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700329}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800330
Elliott Hughes642aaa72015-04-10 12:47:46 -0700331void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700332}
Doug Zongkerc704e062014-05-23 08:40:35 -0700333
334void RecoveryUI::SetEnableReboot(bool enabled) {
335 pthread_mutex_lock(&key_queue_mutex);
336 enable_reboot = enabled;
337 pthread_mutex_unlock(&key_queue_mutex);
338}