blob: 98442539b116dd22025442bc57c03153c632c3a8 [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
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 Zongker9e805d62013-09-04 13:44:38 -070051 key_down_count(0),
Doug Zongkerc704e062014-05-23 08:40:35 -070052 enable_reboot(true),
Doug Zongker9e805d62013-09-04 13:44:38 -070053 consecutive_power_keys(0),
54 consecutive_alternate_keys(0),
55 last_key(-1) {
Doug Zongker32a0a472011-11-01 11:00:20 -070056 pthread_mutex_init(&key_queue_mutex, NULL);
57 pthread_cond_init(&key_queue_cond, NULL);
58 self = this;
59}
60
61void RecoveryUI::Init() {
62 ev_init(input_callback, NULL);
63 pthread_create(&input_t, NULL, input_thread, NULL);
64}
65
66
Todd Poynora5ef19f2013-09-17 13:39:10 -070067int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data)
Doug Zongker32a0a472011-11-01 11:00:20 -070068{
69 struct input_event ev;
70 int ret;
71
Todd Poynore7265df2013-09-10 16:53:12 -070072 ret = ev_get_input(fd, epevents, &ev);
Doug Zongker32a0a472011-11-01 11:00:20 -070073 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;
Doug Zongkerc704e062014-05-23 08:40:35 -0700120 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800121
Doug Zongker32a0a472011-11-01 11:00:20 -0700122 pthread_mutex_lock(&key_queue_mutex);
123 key_pressed[key_code] = updown;
124 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700125 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700126 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700127 key_long_press = false;
128 pthread_t th;
129 key_timer_t* info = new key_timer_t;
130 info->ui = this;
131 info->key_code = key_code;
132 info->count = key_down_count;
133 pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info);
134 pthread_detach(th);
Doug Zongker32a0a472011-11-01 11:00:20 -0700135 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800136 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700137 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700138 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800139 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700140 key_last_down = -1;
141 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700142 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700143 pthread_mutex_unlock(&key_queue_mutex);
144
145 if (register_key) {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800146 NextCheckKeyIsLong(long_press);
Doug Zongker32a0a472011-11-01 11:00:20 -0700147 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800148 case RecoveryUI::IGNORE:
149 break;
150
Doug Zongker32a0a472011-11-01 11:00:20 -0700151 case RecoveryUI::TOGGLE:
152 ShowText(!IsTextVisible());
153 break;
154
155 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700156 if (reboot_enabled) {
157 android_reboot(ANDROID_RB_RESTART, 0, 0);
158 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700159 break;
160
161 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800162 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700163 break;
Doug Zongker9e805d62013-09-04 13:44:38 -0700164
165 case RecoveryUI::MOUNT_SYSTEM:
166#ifndef NO_RECOVERY_MOUNT
167 ensure_path_mounted("/system");
168 Print("Mounted /system.");
169#endif
170 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700171 }
172 }
173}
174
Doug Zongkerc0441d12013-07-31 11:28:24 -0700175void* RecoveryUI::time_key_helper(void* cookie) {
176 key_timer_t* info = (key_timer_t*) cookie;
177 info->ui->time_key(info->key_code, info->count);
178 delete info;
179 return NULL;
180}
181
182void RecoveryUI::time_key(int key_code, int count) {
183 usleep(750000); // 750 ms == "long"
184 bool long_press = false;
185 pthread_mutex_lock(&key_queue_mutex);
186 if (key_last_down == key_code && key_down_count == count) {
187 long_press = key_long_press = true;
188 }
189 pthread_mutex_unlock(&key_queue_mutex);
190 if (long_press) KeyLongPress(key_code);
191}
192
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800193void RecoveryUI::EnqueueKey(int key_code) {
194 pthread_mutex_lock(&key_queue_mutex);
195 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
196 if (key_queue_len < queue_max) {
197 key_queue[key_queue_len++] = key_code;
198 pthread_cond_signal(&key_queue_cond);
199 }
200 pthread_mutex_unlock(&key_queue_mutex);
201}
202
203
Doug Zongker32a0a472011-11-01 11:00:20 -0700204// Reads input events, handles special hot keys, and adds to the key queue.
205void* RecoveryUI::input_thread(void *cookie)
206{
207 for (;;) {
208 if (!ev_wait(-1))
209 ev_dispatch();
210 }
211 return NULL;
212}
213
214int RecoveryUI::WaitKey()
215{
216 pthread_mutex_lock(&key_queue_mutex);
217
218 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
219 // plugged in.
220 do {
221 struct timeval now;
222 struct timespec timeout;
223 gettimeofday(&now, NULL);
224 timeout.tv_sec = now.tv_sec;
225 timeout.tv_nsec = now.tv_usec * 1000;
226 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
227
228 int rc = 0;
229 while (key_queue_len == 0 && rc != ETIMEDOUT) {
230 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
231 &timeout);
232 }
233 } while (usb_connected() && key_queue_len == 0);
234
235 int key = -1;
236 if (key_queue_len > 0) {
237 key = key_queue[0];
238 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
239 }
240 pthread_mutex_unlock(&key_queue_mutex);
241 return key;
242}
243
244// Return true if USB is connected.
245bool RecoveryUI::usb_connected() {
246 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
247 if (fd < 0) {
248 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
249 strerror(errno));
250 return 0;
251 }
252
253 char buf;
254 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
255 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
256 if (close(fd) < 0) {
257 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
258 strerror(errno));
259 }
260 return connected;
261}
262
263bool RecoveryUI::IsKeyPressed(int key)
264{
265 pthread_mutex_lock(&key_queue_mutex);
266 int pressed = key_pressed[key];
267 pthread_mutex_unlock(&key_queue_mutex);
268 return pressed;
269}
270
271void RecoveryUI::FlushKeys() {
272 pthread_mutex_lock(&key_queue_mutex);
273 key_queue_len = 0;
274 pthread_mutex_unlock(&key_queue_mutex);
275}
276
Doug Zongker9e805d62013-09-04 13:44:38 -0700277// The default CheckKey implementation assumes the device has power,
278// volume up, and volume down keys.
279//
280// - Hold power and press vol-up to toggle display.
281// - Press power seven times in a row to reboot.
282// - Alternate vol-up and vol-down seven times to mount /system.
Doug Zongker32a0a472011-11-01 11:00:20 -0700283RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
Doug Zongker02abde52014-04-01 09:45:24 -0700284 if ((IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) || key == KEY_HOME) {
Doug Zongker9e805d62013-09-04 13:44:38 -0700285 return TOGGLE;
286 }
287
288 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700289 pthread_mutex_lock(&key_queue_mutex);
290 bool reboot_enabled = enable_reboot;
291 pthread_mutex_unlock(&key_queue_mutex);
292
293 if (reboot_enabled) {
294 ++consecutive_power_keys;
295 if (consecutive_power_keys >= 7) {
296 return REBOOT;
297 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700298 }
299 } else {
300 consecutive_power_keys = 0;
301 }
302
303 if ((key == KEY_VOLUMEUP &&
304 (last_key == KEY_VOLUMEDOWN || last_key == -1)) ||
305 (key == KEY_VOLUMEDOWN &&
306 (last_key == KEY_VOLUMEUP || last_key == -1))) {
307 ++consecutive_alternate_keys;
308 if (consecutive_alternate_keys >= 7) {
309 consecutive_alternate_keys = 0;
310 return MOUNT_SYSTEM;
311 }
312 } else {
313 consecutive_alternate_keys = 0;
314 }
315 last_key = key;
316
317 return ENQUEUE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700318}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800319
320void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
321}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700322
323void RecoveryUI::KeyLongPress(int key) {
324}
Doug Zongkerc704e062014-05-23 08:40:35 -0700325
326void RecoveryUI::SetEnableReboot(bool enabled) {
327 pthread_mutex_lock(&key_queue_mutex);
328 enable_reboot = enabled;
329 pthread_mutex_unlock(&key_queue_mutex);
330}