blob: a7c8bea48ad1b920e2e680e07dd9f3dbae54ea7a [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 Zongker4db31d22013-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
47RecoveryUI::RecoveryUI() :
48 key_queue_len(0),
Doug Zongkerbb01d0c2012-12-17 10:52:58 -080049 key_last_down(-1),
Doug Zongkerc0441d12013-07-31 11:28:24 -070050 key_long_press(false),
Doug Zongker4db31d22013-09-04 13:44:38 -070051 key_down_count(0),
52 consecutive_power_keys(0),
53 consecutive_alternate_keys(0),
54 last_key(-1) {
Doug Zongker32a0a472011-11-01 11:00:20 -070055 pthread_mutex_init(&key_queue_mutex, NULL);
56 pthread_cond_init(&key_queue_cond, NULL);
57 self = this;
Mihai Serban187d6262014-06-06 15:23:20 +030058 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070059}
60
61void RecoveryUI::Init() {
62 ev_init(input_callback, NULL);
63 pthread_create(&input_t, NULL, input_thread, NULL);
64}
65
66
67int RecoveryUI::input_callback(int fd, short revents, void* data)
68{
69 struct input_event ev;
70 int ret;
71
72 ret = ev_get_input(fd, revents, &ev);
73 if (ret)
74 return -1;
75
76 if (ev.type == EV_SYN) {
77 return 0;
78 } else if (ev.type == EV_REL) {
79 if (ev.code == REL_Y) {
80 // accumulate the up or down motion reported by
81 // the trackball. When it exceeds a threshold
82 // (positive or negative), fake an up/down
83 // key event.
84 self->rel_sum += ev.value;
85 if (self->rel_sum > 3) {
86 self->process_key(KEY_DOWN, 1); // press down key
87 self->process_key(KEY_DOWN, 0); // and release it
88 self->rel_sum = 0;
89 } else if (self->rel_sum < -3) {
90 self->process_key(KEY_UP, 1); // press up key
91 self->process_key(KEY_UP, 0); // and release it
92 self->rel_sum = 0;
93 }
94 }
95 } else {
96 self->rel_sum = 0;
97 }
98
99 if (ev.type == EV_KEY && ev.code <= KEY_MAX)
100 self->process_key(ev.code, ev.value);
101
102 return 0;
103}
104
105// Process a key-up or -down event. A key is "registered" when it is
106// pressed and then released, with no other keypresses or releases in
107// between. Registered keys are passed to CheckKey() to see if it
108// should trigger a visibility toggle, an immediate reboot, or be
109// queued to be processed next time the foreground thread wants a key
110// (eg, for the menu).
111//
112// We also keep track of which keys are currently down so that
113// CheckKey can call IsKeyPressed to see what other keys are held when
114// a key is registered.
115//
116// updown == 1 for key down events; 0 for key up events
117void RecoveryUI::process_key(int key_code, int updown) {
118 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800119 bool long_press = false;
120
Doug Zongker32a0a472011-11-01 11:00:20 -0700121 pthread_mutex_lock(&key_queue_mutex);
122 key_pressed[key_code] = updown;
123 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700124 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700125 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700126 key_long_press = false;
127 pthread_t th;
128 key_timer_t* info = new key_timer_t;
129 info->ui = this;
130 info->key_code = key_code;
131 info->count = key_down_count;
132 pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info);
133 pthread_detach(th);
Doug Zongker32a0a472011-11-01 11:00:20 -0700134 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800135 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700136 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700137 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800138 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700139 key_last_down = -1;
140 }
141 pthread_mutex_unlock(&key_queue_mutex);
142
143 if (register_key) {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800144 NextCheckKeyIsLong(long_press);
Doug Zongker32a0a472011-11-01 11:00:20 -0700145 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800146 case RecoveryUI::IGNORE:
147 break;
148
Doug Zongker32a0a472011-11-01 11:00:20 -0700149 case RecoveryUI::TOGGLE:
150 ShowText(!IsTextVisible());
151 break;
152
153 case RecoveryUI::REBOOT:
154 android_reboot(ANDROID_RB_RESTART, 0, 0);
155 break;
156
157 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800158 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700159 break;
Doug Zongker4db31d22013-09-04 13:44:38 -0700160
161 case RecoveryUI::MOUNT_SYSTEM:
162#ifndef NO_RECOVERY_MOUNT
163 ensure_path_mounted("/system");
164 Print("Mounted /system.");
165#endif
166 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700167 }
168 }
169}
170
Doug Zongkerc0441d12013-07-31 11:28:24 -0700171void* RecoveryUI::time_key_helper(void* cookie) {
172 key_timer_t* info = (key_timer_t*) cookie;
173 info->ui->time_key(info->key_code, info->count);
174 delete info;
175 return NULL;
176}
177
178void RecoveryUI::time_key(int key_code, int count) {
179 usleep(750000); // 750 ms == "long"
180 bool long_press = false;
181 pthread_mutex_lock(&key_queue_mutex);
182 if (key_last_down == key_code && key_down_count == count) {
183 long_press = key_long_press = true;
184 }
185 pthread_mutex_unlock(&key_queue_mutex);
186 if (long_press) KeyLongPress(key_code);
187}
188
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800189void RecoveryUI::EnqueueKey(int key_code) {
190 pthread_mutex_lock(&key_queue_mutex);
191 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
192 if (key_queue_len < queue_max) {
193 key_queue[key_queue_len++] = key_code;
194 pthread_cond_signal(&key_queue_cond);
195 }
196 pthread_mutex_unlock(&key_queue_mutex);
197}
198
199
Doug Zongker32a0a472011-11-01 11:00:20 -0700200// Reads input events, handles special hot keys, and adds to the key queue.
201void* RecoveryUI::input_thread(void *cookie)
202{
203 for (;;) {
204 if (!ev_wait(-1))
205 ev_dispatch();
206 }
207 return NULL;
208}
209
210int RecoveryUI::WaitKey()
211{
212 pthread_mutex_lock(&key_queue_mutex);
213
214 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
215 // plugged in.
216 do {
217 struct timeval now;
218 struct timespec timeout;
219 gettimeofday(&now, NULL);
220 timeout.tv_sec = now.tv_sec;
221 timeout.tv_nsec = now.tv_usec * 1000;
222 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
223
224 int rc = 0;
225 while (key_queue_len == 0 && rc != ETIMEDOUT) {
226 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
227 &timeout);
228 }
229 } while (usb_connected() && key_queue_len == 0);
230
231 int key = -1;
232 if (key_queue_len > 0) {
233 key = key_queue[0];
234 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
235 }
236 pthread_mutex_unlock(&key_queue_mutex);
237 return key;
238}
239
240// Return true if USB is connected.
241bool RecoveryUI::usb_connected() {
242 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
243 if (fd < 0) {
244 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
245 strerror(errno));
246 return 0;
247 }
248
249 char buf;
250 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
251 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
252 if (close(fd) < 0) {
253 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
254 strerror(errno));
255 }
256 return connected;
257}
258
259bool RecoveryUI::IsKeyPressed(int key)
260{
261 pthread_mutex_lock(&key_queue_mutex);
262 int pressed = key_pressed[key];
263 pthread_mutex_unlock(&key_queue_mutex);
264 return pressed;
265}
266
267void RecoveryUI::FlushKeys() {
268 pthread_mutex_lock(&key_queue_mutex);
269 key_queue_len = 0;
270 pthread_mutex_unlock(&key_queue_mutex);
271}
272
Doug Zongker4db31d22013-09-04 13:44:38 -0700273// The default CheckKey implementation assumes the device has power,
274// volume up, and volume down keys.
275//
276// - Hold power and press vol-up to toggle display.
277// - Press power seven times in a row to reboot.
278// - Alternate vol-up and vol-down seven times to mount /system.
Doug Zongker32a0a472011-11-01 11:00:20 -0700279RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
Doug Zongker4db31d22013-09-04 13:44:38 -0700280 if (IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) {
281 return TOGGLE;
282 }
283
284 if (key == KEY_POWER) {
285 ++consecutive_power_keys;
286 if (consecutive_power_keys >= 7) {
287 return REBOOT;
288 }
289 } else {
290 consecutive_power_keys = 0;
291 }
292
293 if ((key == KEY_VOLUMEUP &&
294 (last_key == KEY_VOLUMEDOWN || last_key == -1)) ||
295 (key == KEY_VOLUMEDOWN &&
296 (last_key == KEY_VOLUMEUP || last_key == -1))) {
297 ++consecutive_alternate_keys;
298 if (consecutive_alternate_keys >= 7) {
299 consecutive_alternate_keys = 0;
300 return MOUNT_SYSTEM;
301 }
302 } else {
303 consecutive_alternate_keys = 0;
304 }
305 last_key = key;
306
307 return ENQUEUE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700308}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800309
310void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
311}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700312
313void RecoveryUI::KeyLongPress(int key) {
314}