blob: 27ecc280508b9ee0f0f3a0d9149954734ff4be4c [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),
50 key_last_down(-1) {
51 pthread_mutex_init(&key_queue_mutex, NULL);
52 pthread_cond_init(&key_queue_cond, NULL);
53 self = this;
54}
55
56void RecoveryUI::Init() {
57 ev_init(input_callback, NULL);
58 pthread_create(&input_t, NULL, input_thread, NULL);
59}
60
61
62int RecoveryUI::input_callback(int fd, short revents, void* data)
63{
64 struct input_event ev;
65 int ret;
66
67 ret = ev_get_input(fd, revents, &ev);
68 if (ret)
69 return -1;
70
71 if (ev.type == EV_SYN) {
72 return 0;
73 } else if (ev.type == EV_REL) {
74 if (ev.code == REL_Y) {
75 // accumulate the up or down motion reported by
76 // the trackball. When it exceeds a threshold
77 // (positive or negative), fake an up/down
78 // key event.
79 self->rel_sum += ev.value;
80 if (self->rel_sum > 3) {
81 self->process_key(KEY_DOWN, 1); // press down key
82 self->process_key(KEY_DOWN, 0); // and release it
83 self->rel_sum = 0;
84 } else if (self->rel_sum < -3) {
85 self->process_key(KEY_UP, 1); // press up key
86 self->process_key(KEY_UP, 0); // and release it
87 self->rel_sum = 0;
88 }
89 }
90 } else {
91 self->rel_sum = 0;
92 }
93
94 if (ev.type == EV_KEY && ev.code <= KEY_MAX)
95 self->process_key(ev.code, ev.value);
96
97 return 0;
98}
99
100// Process a key-up or -down event. A key is "registered" when it is
101// pressed and then released, with no other keypresses or releases in
102// between. Registered keys are passed to CheckKey() to see if it
103// should trigger a visibility toggle, an immediate reboot, or be
104// queued to be processed next time the foreground thread wants a key
105// (eg, for the menu).
106//
107// We also keep track of which keys are currently down so that
108// CheckKey can call IsKeyPressed to see what other keys are held when
109// a key is registered.
110//
111// updown == 1 for key down events; 0 for key up events
112void RecoveryUI::process_key(int key_code, int updown) {
113 bool register_key = false;
114
115 pthread_mutex_lock(&key_queue_mutex);
116 key_pressed[key_code] = updown;
117 if (updown) {
118 key_last_down = key_code;
119 } else {
120 if (key_last_down == key_code)
121 register_key = true;
122 key_last_down = -1;
123 }
124 pthread_mutex_unlock(&key_queue_mutex);
125
126 if (register_key) {
127 switch (CheckKey(key_code)) {
Doug Zongker48b5b072012-01-18 13:46:26 -0800128 case RecoveryUI::IGNORE:
129 break;
130
Doug Zongker32a0a472011-11-01 11:00:20 -0700131 case RecoveryUI::TOGGLE:
132 ShowText(!IsTextVisible());
133 break;
134
135 case RecoveryUI::REBOOT:
Dees_Troy38bd7602012-09-14 13:33:53 -0400136#ifdef ANDROID_RB_RESTART
Doug Zongker32a0a472011-11-01 11:00:20 -0700137 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -0400138#endif
Doug Zongker32a0a472011-11-01 11:00:20 -0700139 break;
140
141 case RecoveryUI::ENQUEUE:
142 pthread_mutex_lock(&key_queue_mutex);
143 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
144 if (key_queue_len < queue_max) {
145 key_queue[key_queue_len++] = key_code;
146 pthread_cond_signal(&key_queue_cond);
147 }
148 pthread_mutex_unlock(&key_queue_mutex);
149 break;
150 }
151 }
152}
153
154// Reads input events, handles special hot keys, and adds to the key queue.
155void* RecoveryUI::input_thread(void *cookie)
156{
157 for (;;) {
158 if (!ev_wait(-1))
159 ev_dispatch();
160 }
161 return NULL;
162}
163
164int RecoveryUI::WaitKey()
165{
166 pthread_mutex_lock(&key_queue_mutex);
167
168 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
169 // plugged in.
170 do {
171 struct timeval now;
172 struct timespec timeout;
173 gettimeofday(&now, NULL);
174 timeout.tv_sec = now.tv_sec;
175 timeout.tv_nsec = now.tv_usec * 1000;
176 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
177
178 int rc = 0;
179 while (key_queue_len == 0 && rc != ETIMEDOUT) {
180 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
181 &timeout);
182 }
183 } while (usb_connected() && key_queue_len == 0);
184
185 int key = -1;
186 if (key_queue_len > 0) {
187 key = key_queue[0];
188 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
189 }
190 pthread_mutex_unlock(&key_queue_mutex);
191 return key;
192}
193
194// Return true if USB is connected.
195bool RecoveryUI::usb_connected() {
196 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
197 if (fd < 0) {
198 printf("failed to open /sys/class/android_usb/android0/state: %s\n",
199 strerror(errno));
200 return 0;
201 }
202
203 char buf;
204 /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
205 int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
206 if (close(fd) < 0) {
207 printf("failed to close /sys/class/android_usb/android0/state: %s\n",
208 strerror(errno));
209 }
210 return connected;
211}
212
213bool RecoveryUI::IsKeyPressed(int key)
214{
215 pthread_mutex_lock(&key_queue_mutex);
216 int pressed = key_pressed[key];
217 pthread_mutex_unlock(&key_queue_mutex);
218 return pressed;
219}
220
221void RecoveryUI::FlushKeys() {
222 pthread_mutex_lock(&key_queue_mutex);
223 key_queue_len = 0;
224 pthread_mutex_unlock(&key_queue_mutex);
225}
226
227RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
228 return RecoveryUI::ENQUEUE;
229}