blob: 87162452090b61bf416052f046af8713eb02957f [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),
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
Elliott Hughesec283402015-04-10 10:01:53 -070067int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) {
Doug Zongker32a0a472011-11-01 11:00:20 -070068 struct input_event ev;
69 int ret;
70
Todd Poynore7265df2013-09-10 16:53:12 -070071 ret = ev_get_input(fd, epevents, &ev);
Doug Zongker32a0a472011-11-01 11:00:20 -070072 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;
Doug Zongkerc704e062014-05-23 08:40:35 -0700119 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800120
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 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700141 reboot_enabled = enable_reboot;
Doug Zongker32a0a472011-11-01 11:00:20 -0700142 pthread_mutex_unlock(&key_queue_mutex);
143
144 if (register_key) {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800145 NextCheckKeyIsLong(long_press);
Doug Zongker32a0a472011-11-01 11:00:20 -0700146 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800147 case RecoveryUI::IGNORE:
148 break;
149
Doug Zongker32a0a472011-11-01 11:00:20 -0700150 case RecoveryUI::TOGGLE:
151 ShowText(!IsTextVisible());
152 break;
153
154 case RecoveryUI::REBOOT:
Doug Zongkerc704e062014-05-23 08:40:35 -0700155 if (reboot_enabled) {
156 android_reboot(ANDROID_RB_RESTART, 0, 0);
157 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700158 break;
159
160 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800161 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700162 break;
163 }
164 }
165}
166
Doug Zongkerc0441d12013-07-31 11:28:24 -0700167void* RecoveryUI::time_key_helper(void* cookie) {
168 key_timer_t* info = (key_timer_t*) cookie;
169 info->ui->time_key(info->key_code, info->count);
170 delete info;
171 return NULL;
172}
173
174void RecoveryUI::time_key(int key_code, int count) {
175 usleep(750000); // 750 ms == "long"
176 bool long_press = false;
177 pthread_mutex_lock(&key_queue_mutex);
178 if (key_last_down == key_code && key_down_count == count) {
179 long_press = key_long_press = true;
180 }
181 pthread_mutex_unlock(&key_queue_mutex);
182 if (long_press) KeyLongPress(key_code);
183}
184
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800185void RecoveryUI::EnqueueKey(int key_code) {
186 pthread_mutex_lock(&key_queue_mutex);
187 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
188 if (key_queue_len < queue_max) {
189 key_queue[key_queue_len++] = key_code;
190 pthread_cond_signal(&key_queue_cond);
191 }
192 pthread_mutex_unlock(&key_queue_mutex);
193}
194
195
Doug Zongker32a0a472011-11-01 11:00:20 -0700196// Reads input events, handles special hot keys, and adds to the key queue.
Elliott Hughesec283402015-04-10 10:01:53 -0700197void* RecoveryUI::input_thread(void* cookie) {
198 while (true) {
199 if (!ev_wait(-1)) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700200 ev_dispatch();
Elliott Hughesec283402015-04-10 10:01:53 -0700201 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700202 }
203 return NULL;
204}
205
Elliott Hughesec283402015-04-10 10:01:53 -0700206int RecoveryUI::WaitKey() {
Doug Zongker32a0a472011-11-01 11:00:20 -0700207 pthread_mutex_lock(&key_queue_mutex);
208
209 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
210 // plugged in.
211 do {
212 struct timeval now;
213 struct timespec timeout;
214 gettimeofday(&now, NULL);
215 timeout.tv_sec = now.tv_sec;
216 timeout.tv_nsec = now.tv_usec * 1000;
217 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
218
219 int rc = 0;
220 while (key_queue_len == 0 && rc != ETIMEDOUT) {
Elliott Hughesec283402015-04-10 10:01:53 -0700221 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700222 }
223 } while (usb_connected() && key_queue_len == 0);
224
225 int key = -1;
226 if (key_queue_len > 0) {
227 key = key_queue[0];
228 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
229 }
230 pthread_mutex_unlock(&key_queue_mutex);
231 return key;
232}
233
234// Return true if USB is connected.
235bool RecoveryUI::usb_connected() {
236 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
237 if (fd < 0) {
238 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
239 strerror(errno));
240 return 0;
241 }
242
243 char buf;
244 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
245 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
246 if (close(fd) < 0) {
247 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
248 strerror(errno));
249 }
250 return connected;
251}
252
Elliott Hughesec283402015-04-10 10:01:53 -0700253bool RecoveryUI::IsKeyPressed(int key) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700254 pthread_mutex_lock(&key_queue_mutex);
255 int pressed = key_pressed[key];
256 pthread_mutex_unlock(&key_queue_mutex);
257 return pressed;
258}
259
260void RecoveryUI::FlushKeys() {
261 pthread_mutex_lock(&key_queue_mutex);
262 key_queue_len = 0;
263 pthread_mutex_unlock(&key_queue_mutex);
264}
265
Doug Zongker9e805d62013-09-04 13:44:38 -0700266// The default CheckKey implementation assumes the device has power,
267// volume up, and volume down keys.
268//
269// - Hold power and press vol-up to toggle display.
270// - Press power seven times in a row to reboot.
271// - Alternate vol-up and vol-down seven times to mount /system.
Doug Zongker32a0a472011-11-01 11:00:20 -0700272RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
Doug Zongker02abde52014-04-01 09:45:24 -0700273 if ((IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) || key == KEY_HOME) {
Doug Zongker9e805d62013-09-04 13:44:38 -0700274 return TOGGLE;
275 }
276
277 if (key == KEY_POWER) {
Doug Zongkerc704e062014-05-23 08:40:35 -0700278 pthread_mutex_lock(&key_queue_mutex);
279 bool reboot_enabled = enable_reboot;
280 pthread_mutex_unlock(&key_queue_mutex);
281
282 if (reboot_enabled) {
283 ++consecutive_power_keys;
284 if (consecutive_power_keys >= 7) {
285 return REBOOT;
286 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700287 }
288 } else {
289 consecutive_power_keys = 0;
290 }
291
Doug Zongker9e805d62013-09-04 13:44:38 -0700292 last_key = key;
293
294 return ENQUEUE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700295}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800296
297void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
298}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700299
300void RecoveryUI::KeyLongPress(int key) {
301}
Doug Zongkerc704e062014-05-23 08:40:35 -0700302
303void RecoveryUI::SetEnableReboot(bool enabled) {
304 pthread_mutex_lock(&key_queue_mutex);
305 enable_reboot = enabled;
306 pthread_mutex_unlock(&key_queue_mutex);
307}