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