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