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