blob: 44656d10f8f72da2bb6d89fda7c3fe8773889d1f [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
Dees_Troy38bd7602012-09-14 13:33:53 -040031#ifdef ANDROID_RB_RESTART
Doug Zongker32a0a472011-11-01 11:00:20 -070032#include <cutils/android_reboot.h>
Dees_Troy38bd7602012-09-14 13:33:53 -040033#endif
Doug Zongker32a0a472011-11-01 11:00:20 -070034
35#include "common.h"
36#include "device.h"
37#include "minui/minui.h"
38#include "screen_ui.h"
39#include "ui.h"
40
41#define UI_WAIT_KEY_TIMEOUT_SEC 120
42
43// There's only (at most) one of these objects, and global callbacks
44// (for pthread_create, and the input event system) need to find it,
45// so use a global variable.
46static RecoveryUI* self = NULL;
47
48RecoveryUI::RecoveryUI() :
49 key_queue_len(0),
Doug Zongkerbb01d0c2012-12-17 10:52:58 -080050 key_last_down(-1),
Doug Zongkerc0441d12013-07-31 11:28:24 -070051 key_long_press(false),
52 key_down_count(0) {
Doug Zongker32a0a472011-11-01 11:00:20 -070053 pthread_mutex_init(&key_queue_mutex, NULL);
54 pthread_cond_init(&key_queue_cond, NULL);
55 self = this;
56}
57
58void RecoveryUI::Init() {
59 ev_init(input_callback, NULL);
60 pthread_create(&input_t, NULL, input_thread, NULL);
61}
62
63
64int RecoveryUI::input_callback(int fd, short revents, void* data)
65{
66 struct input_event ev;
67 int ret;
68
69 ret = ev_get_input(fd, revents, &ev);
70 if (ret)
71 return -1;
72
73 if (ev.type == EV_SYN) {
74 return 0;
75 } else if (ev.type == EV_REL) {
76 if (ev.code == REL_Y) {
77 // accumulate the up or down motion reported by
78 // the trackball. When it exceeds a threshold
79 // (positive or negative), fake an up/down
80 // key event.
81 self->rel_sum += ev.value;
82 if (self->rel_sum > 3) {
83 self->process_key(KEY_DOWN, 1); // press down key
84 self->process_key(KEY_DOWN, 0); // and release it
85 self->rel_sum = 0;
86 } else if (self->rel_sum < -3) {
87 self->process_key(KEY_UP, 1); // press up key
88 self->process_key(KEY_UP, 0); // and release it
89 self->rel_sum = 0;
90 }
91 }
92 } else {
93 self->rel_sum = 0;
94 }
95
96 if (ev.type == EV_KEY && ev.code <= KEY_MAX)
97 self->process_key(ev.code, ev.value);
98
99 return 0;
100}
101
102// Process a key-up or -down event. A key is "registered" when it is
103// pressed and then released, with no other keypresses or releases in
104// between. Registered keys are passed to CheckKey() to see if it
105// should trigger a visibility toggle, an immediate reboot, or be
106// queued to be processed next time the foreground thread wants a key
107// (eg, for the menu).
108//
109// We also keep track of which keys are currently down so that
110// CheckKey can call IsKeyPressed to see what other keys are held when
111// a key is registered.
112//
113// updown == 1 for key down events; 0 for key up events
114void RecoveryUI::process_key(int key_code, int updown) {
115 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800116 bool long_press = false;
117
Doug Zongker32a0a472011-11-01 11:00:20 -0700118 pthread_mutex_lock(&key_queue_mutex);
119 key_pressed[key_code] = updown;
120 if (updown) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700121 ++key_down_count;
Doug Zongker32a0a472011-11-01 11:00:20 -0700122 key_last_down = key_code;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700123 key_long_press = false;
124 pthread_t th;
125 key_timer_t* info = new key_timer_t;
126 info->ui = this;
127 info->key_code = key_code;
128 info->count = key_down_count;
129 pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info);
130 pthread_detach(th);
Doug Zongker32a0a472011-11-01 11:00:20 -0700131 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800132 if (key_last_down == key_code) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700133 long_press = key_long_press;
Doug Zongker32a0a472011-11-01 11:00:20 -0700134 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800135 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700136 key_last_down = -1;
137 }
138 pthread_mutex_unlock(&key_queue_mutex);
139
140 if (register_key) {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800141 NextCheckKeyIsLong(long_press);
Doug Zongker32a0a472011-11-01 11:00:20 -0700142 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800143 case RecoveryUI::IGNORE:
144 break;
145
Doug Zongker32a0a472011-11-01 11:00:20 -0700146 case RecoveryUI::TOGGLE:
147 ShowText(!IsTextVisible());
148 break;
149
150 case RecoveryUI::REBOOT:
Dees_Troy38bd7602012-09-14 13:33:53 -0400151#ifdef ANDROID_RB_RESTART
Doug Zongker32a0a472011-11-01 11:00:20 -0700152 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -0400153#endif
Doug Zongker32a0a472011-11-01 11:00:20 -0700154 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;
159 }
160 }
161}
162
Doug Zongkerc0441d12013-07-31 11:28:24 -0700163void* RecoveryUI::time_key_helper(void* cookie) {
164 key_timer_t* info = (key_timer_t*) cookie;
165 info->ui->time_key(info->key_code, info->count);
166 delete info;
167 return NULL;
168}
169
170void RecoveryUI::time_key(int key_code, int count) {
171 usleep(750000); // 750 ms == "long"
172 bool long_press = false;
173 pthread_mutex_lock(&key_queue_mutex);
174 if (key_last_down == key_code && key_down_count == count) {
175 long_press = key_long_press = true;
176 }
177 pthread_mutex_unlock(&key_queue_mutex);
178 if (long_press) KeyLongPress(key_code);
179}
180
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800181void RecoveryUI::EnqueueKey(int key_code) {
182 pthread_mutex_lock(&key_queue_mutex);
183 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
184 if (key_queue_len < queue_max) {
185 key_queue[key_queue_len++] = key_code;
186 pthread_cond_signal(&key_queue_cond);
187 }
188 pthread_mutex_unlock(&key_queue_mutex);
189}
190
191
Doug Zongker32a0a472011-11-01 11:00:20 -0700192// Reads input events, handles special hot keys, and adds to the key queue.
193void* RecoveryUI::input_thread(void *cookie)
194{
195 for (;;) {
196 if (!ev_wait(-1))
197 ev_dispatch();
198 }
199 return NULL;
200}
201
202int RecoveryUI::WaitKey()
203{
204 pthread_mutex_lock(&key_queue_mutex);
205
206 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
207 // plugged in.
208 do {
209 struct timeval now;
210 struct timespec timeout;
211 gettimeofday(&now, NULL);
212 timeout.tv_sec = now.tv_sec;
213 timeout.tv_nsec = now.tv_usec * 1000;
214 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
215
216 int rc = 0;
217 while (key_queue_len == 0 && rc != ETIMEDOUT) {
218 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
219 &timeout);
220 }
221 } while (usb_connected() && key_queue_len == 0);
222
223 int key = -1;
224 if (key_queue_len > 0) {
225 key = key_queue[0];
226 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
227 }
228 pthread_mutex_unlock(&key_queue_mutex);
229 return key;
230}
231
232// Return true if USB is connected.
233bool RecoveryUI::usb_connected() {
234 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
235 if (fd < 0) {
236 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
237 strerror(errno));
238 return 0;
239 }
240
241 char buf;
242 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
243 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
244 if (close(fd) < 0) {
245 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
246 strerror(errno));
247 }
248 return connected;
249}
250
251bool RecoveryUI::IsKeyPressed(int key)
252{
253 pthread_mutex_lock(&key_queue_mutex);
254 int pressed = key_pressed[key];
255 pthread_mutex_unlock(&key_queue_mutex);
256 return pressed;
257}
258
259void RecoveryUI::FlushKeys() {
260 pthread_mutex_lock(&key_queue_mutex);
261 key_queue_len = 0;
262 pthread_mutex_unlock(&key_queue_mutex);
263}
264
265RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
266 return RecoveryUI::ENQUEUE;
267}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800268
269void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
270}
Doug Zongkerc0441d12013-07-31 11:28:24 -0700271
272void RecoveryUI::KeyLongPress(int key) {
273}