Home | History | Annotate | Download | only in recovery
      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 
     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.
     44 static RecoveryUI* self = NULL;
     45 
     46 RecoveryUI::RecoveryUI() :
     47     key_queue_len(0),
     48     key_last_down(-1),
     49     key_long_press(false),
     50     key_down_count(0) {
     51     pthread_mutex_init(&key_queue_mutex, NULL);
     52     pthread_cond_init(&key_queue_cond, NULL);
     53     self = this;
     54 }
     55 
     56 void RecoveryUI::Init() {
     57     ev_init(input_callback, NULL);
     58     pthread_create(&input_t, NULL, input_thread, NULL);
     59 }
     60 
     61 
     62 int 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
    112 void RecoveryUI::process_key(int key_code, int updown) {
    113     bool register_key = false;
    114     bool long_press = false;
    115 
    116     pthread_mutex_lock(&key_queue_mutex);
    117     key_pressed[key_code] = updown;
    118     if (updown) {
    119         ++key_down_count;
    120         key_last_down = key_code;
    121         key_long_press = false;
    122         pthread_t th;
    123         key_timer_t* info = new key_timer_t;
    124         info->ui = this;
    125         info->key_code = key_code;
    126         info->count = key_down_count;
    127         pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info);
    128         pthread_detach(th);
    129     } else {
    130         if (key_last_down == key_code) {
    131             long_press = key_long_press;
    132             register_key = true;
    133         }
    134         key_last_down = -1;
    135     }
    136     pthread_mutex_unlock(&key_queue_mutex);
    137 
    138     if (register_key) {
    139         NextCheckKeyIsLong(long_press);
    140         switch (CheckKey(key_code)) {
    141           case RecoveryUI::IGNORE:
    142             break;
    143 
    144           case RecoveryUI::TOGGLE:
    145             ShowText(!IsTextVisible());
    146             break;
    147 
    148           case RecoveryUI::REBOOT:
    149             android_reboot(ANDROID_RB_RESTART, 0, 0);
    150             break;
    151 
    152           case RecoveryUI::ENQUEUE:
    153             EnqueueKey(key_code);
    154             break;
    155         }
    156     }
    157 }
    158 
    159 void* RecoveryUI::time_key_helper(void* cookie) {
    160     key_timer_t* info = (key_timer_t*) cookie;
    161     info->ui->time_key(info->key_code, info->count);
    162     delete info;
    163     return NULL;
    164 }
    165 
    166 void RecoveryUI::time_key(int key_code, int count) {
    167     usleep(750000);  // 750 ms == "long"
    168     bool long_press = false;
    169     pthread_mutex_lock(&key_queue_mutex);
    170     if (key_last_down == key_code && key_down_count == count) {
    171         long_press = key_long_press = true;
    172     }
    173     pthread_mutex_unlock(&key_queue_mutex);
    174     if (long_press) KeyLongPress(key_code);
    175 }
    176 
    177 void RecoveryUI::EnqueueKey(int key_code) {
    178     pthread_mutex_lock(&key_queue_mutex);
    179     const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
    180     if (key_queue_len < queue_max) {
    181         key_queue[key_queue_len++] = key_code;
    182         pthread_cond_signal(&key_queue_cond);
    183     }
    184     pthread_mutex_unlock(&key_queue_mutex);
    185 }
    186 
    187 
    188 // Reads input events, handles special hot keys, and adds to the key queue.
    189 void* RecoveryUI::input_thread(void *cookie)
    190 {
    191     for (;;) {
    192         if (!ev_wait(-1))
    193             ev_dispatch();
    194     }
    195     return NULL;
    196 }
    197 
    198 int RecoveryUI::WaitKey()
    199 {
    200     pthread_mutex_lock(&key_queue_mutex);
    201 
    202     // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
    203     // plugged in.
    204     do {
    205         struct timeval now;
    206         struct timespec timeout;
    207         gettimeofday(&now, NULL);
    208         timeout.tv_sec = now.tv_sec;
    209         timeout.tv_nsec = now.tv_usec * 1000;
    210         timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
    211 
    212         int rc = 0;
    213         while (key_queue_len == 0 && rc != ETIMEDOUT) {
    214             rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex,
    215                                         &timeout);
    216         }
    217     } while (usb_connected() && key_queue_len == 0);
    218 
    219     int key = -1;
    220     if (key_queue_len > 0) {
    221         key = key_queue[0];
    222         memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
    223     }
    224     pthread_mutex_unlock(&key_queue_mutex);
    225     return key;
    226 }
    227 
    228 // Return true if USB is connected.
    229 bool RecoveryUI::usb_connected() {
    230     int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
    231     if (fd < 0) {
    232         printf("failed to open /sys/class/android_usb/android0/state: %s\n",
    233                strerror(errno));
    234         return 0;
    235     }
    236 
    237     char buf;
    238     /* USB is connected if android_usb state is CONNECTED or CONFIGURED */
    239     int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
    240     if (close(fd) < 0) {
    241         printf("failed to close /sys/class/android_usb/android0/state: %s\n",
    242                strerror(errno));
    243     }
    244     return connected;
    245 }
    246 
    247 bool RecoveryUI::IsKeyPressed(int key)
    248 {
    249     pthread_mutex_lock(&key_queue_mutex);
    250     int pressed = key_pressed[key];
    251     pthread_mutex_unlock(&key_queue_mutex);
    252     return pressed;
    253 }
    254 
    255 void RecoveryUI::FlushKeys() {
    256     pthread_mutex_lock(&key_queue_mutex);
    257     key_queue_len = 0;
    258     pthread_mutex_unlock(&key_queue_mutex);
    259 }
    260 
    261 RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) {
    262     return RecoveryUI::ENQUEUE;
    263 }
    264 
    265 void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) {
    266 }
    267 
    268 void RecoveryUI::KeyLongPress(int key) {
    269 }
    270