blob: 65f4028210b378dc5a148b11a828f9c3dba8f9f9 [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"
34#include "device.h"
35#include "minui/minui.h"
36#include "screen_ui.h"
37#include "ui.h"
38
39#define UI_WAIT_KEY_TIMEOUT_SEC 120
40
41// There's only (at most) one of these objects, and global callbacks
42// (for pthread_create, and the input event system) need to find it,
43// so use a global variable.
44static RecoveryUI* self = NULL;
45
46RecoveryUI::RecoveryUI() :
47 key_queue_len(0),
Doug Zongkerbb01d0c2012-12-17 10:52:58 -080048 key_last_down(-1),
49 key_down_time(0) {
Doug Zongker32a0a472011-11-01 11:00:20 -070050 pthread_mutex_init(&key_queue_mutex, NULL);
51 pthread_cond_init(&key_queue_cond, NULL);
52 self = this;
53}
54
55void RecoveryUI::Init() {
56 ev_init(input_callback, NULL);
57 pthread_create(&input_t, NULL, input_thread, NULL);
58}
59
60
61int RecoveryUI::input_callback(int fd, short revents, void* data)
62{
63 struct input_event ev;
64 int ret;
65
66 ret = ev_get_input(fd, revents, &ev);
67 if (ret)
68 return -1;
69
70 if (ev.type == EV_SYN) {
71 return 0;
72 } else if (ev.type == EV_REL) {
73 if (ev.code == REL_Y) {
74 // accumulate the up or down motion reported by
75 // the trackball. When it exceeds a threshold
76 // (positive or negative), fake an up/down
77 // key event.
78 self->rel_sum += ev.value;
79 if (self->rel_sum > 3) {
80 self->process_key(KEY_DOWN, 1); // press down key
81 self->process_key(KEY_DOWN, 0); // and release it
82 self->rel_sum = 0;
83 } else if (self->rel_sum < -3) {
84 self->process_key(KEY_UP, 1); // press up key
85 self->process_key(KEY_UP, 0); // and release it
86 self->rel_sum = 0;
87 }
88 }
89 } else {
90 self->rel_sum = 0;
91 }
92
93 if (ev.type == EV_KEY && ev.code <= KEY_MAX)
94 self->process_key(ev.code, ev.value);
95
96 return 0;
97}
98
99// Process a key-up or -down event. A key is "registered" when it is
100// pressed and then released, with no other keypresses or releases in
101// between. Registered keys are passed to CheckKey() to see if it
102// should trigger a visibility toggle, an immediate reboot, or be
103// queued to be processed next time the foreground thread wants a key
104// (eg, for the menu).
105//
106// We also keep track of which keys are currently down so that
107// CheckKey can call IsKeyPressed to see what other keys are held when
108// a key is registered.
109//
110// updown == 1 for key down events; 0 for key up events
111void RecoveryUI::process_key(int key_code, int updown) {
112 bool register_key = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800113 bool long_press = false;
114
115 const long long_threshold = CLOCKS_PER_SEC * 750 / 1000;
Doug Zongker32a0a472011-11-01 11:00:20 -0700116
117 pthread_mutex_lock(&key_queue_mutex);
118 key_pressed[key_code] = updown;
119 if (updown) {
120 key_last_down = key_code;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800121 key_down_time = clock();
Doug Zongker32a0a472011-11-01 11:00:20 -0700122 } else {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800123 if (key_last_down == key_code) {
124 long duration = clock() - key_down_time;
125 if (duration > long_threshold) {
126 long_press = true;
127 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700128 register_key = true;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800129 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700130 key_last_down = -1;
131 }
132 pthread_mutex_unlock(&key_queue_mutex);
133
134 if (register_key) {
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800135 NextCheckKeyIsLong(long_press);
Doug Zongker32a0a472011-11-01 11:00:20 -0700136 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800137 case RecoveryUI::IGNORE:
138 break;
139
Doug Zongker32a0a472011-11-01 11:00:20 -0700140 case RecoveryUI::TOGGLE:
141 ShowText(!IsTextVisible());
142 break;
143
144 case RecoveryUI::REBOOT:
145 android_reboot(ANDROID_RB_RESTART, 0, 0);
146 break;
147
148 case RecoveryUI::ENQUEUE:
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800149 EnqueueKey(key_code);
Doug Zongker32a0a472011-11-01 11:00:20 -0700150 break;
151 }
152 }
153}
154
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800155void RecoveryUI::EnqueueKey(int key_code) {
156 pthread_mutex_lock(&key_queue_mutex);
157 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
158 if (key_queue_len < queue_max) {
159 key_queue[key_queue_len++] = key_code;
160 pthread_cond_signal(&key_queue_cond);
161 }
162 pthread_mutex_unlock(&key_queue_mutex);
163}
164
165
Doug Zongker32a0a472011-11-01 11:00:20 -0700166// Reads input events, handles special hot keys, and adds to the key queue.
167void* RecoveryUI::input_thread(void *cookie)
168{
169 for (;;) {
170 if (!ev_wait(-1))
171 ev_dispatch();
172 }
173 return NULL;
174}
175
176int RecoveryUI::WaitKey()
177{
178 pthread_mutex_lock(&key_queue_mutex);
179
180 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
181 // plugged in.
182 do {
183 struct timeval now;
184 struct timespec timeout;
185 gettimeofday(&now, NULL);
186 timeout.tv_sec = now.tv_sec;
187 timeout.tv_nsec = now.tv_usec * 1000;
188 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
189
190 int rc = 0;
191 while (key_queue_len == 0 && rc != ETIMEDOUT) {
192 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
193 &timeout);
194 }
195 } while (usb_connected() && key_queue_len == 0);
196
197 int key = -1;
198 if (key_queue_len > 0) {
199 key = key_queue[0];
200 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
201 }
202 pthread_mutex_unlock(&key_queue_mutex);
203 return key;
204}
205
206// Return true if USB is connected.
207bool RecoveryUI::usb_connected() {
208 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
209 if (fd < 0) {
210 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
211 strerror(errno));
212 return 0;
213 }
214
215 char buf;
216 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
217 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
218 if (close(fd) < 0) {
219 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
220 strerror(errno));
221 }
222 return connected;
223}
224
225bool RecoveryUI::IsKeyPressed(int key)
226{
227 pthread_mutex_lock(&key_queue_mutex);
228 int pressed = key_pressed[key];
229 pthread_mutex_unlock(&key_queue_mutex);
230 return pressed;
231}
232
233void RecoveryUI::FlushKeys() {
234 pthread_mutex_lock(&key_queue_mutex);
235 key_queue_len = 0;
236 pthread_mutex_unlock(&key_queue_mutex);
237}
238
239RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
240 return RecoveryUI::ENQUEUE;
241}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800242
243void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
244}