ref : /bootable/recovery/events.c
/bootable/recovery/ui.cpp
/bootable/recovery/recovery.cpp
/bootable/recovery/roots.cpp
与UI相关的地方,一般都会要与event (key ,touch)的发生关系
UI初始化:
void RecoveryUI::Init() {
ev_init(input_callback, NULL);
pthread_create(&input_t, NULL, input_thread, NULL);
}
#define MAX_DEVICES 16
// 获得/dev/input目录下所有文件的描述符,但是最多不能超过16个
// 这个与factory mode 中的miniui很类似
int ev_init(ev_callback input_cb, void *data)
{
DIR *dir;
struct dirent *de;
int fd;
dir = opendir("/dev/input");
if(dir != 0) {
while((de = readdir(dir))) {
unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
if(strncmp(de->d_name,"event",5)) continue;
fd = openat(dirfd(dir), de->d_name, O_RDONLY);
if(fd < 0) continue;
/* read the evbits of the input device */
if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
close(fd);
continue;
}
/* TODO: add ability to specify event masks. For now, just assume
* that only EV_KEY and EV_REL event types are ever needed. */
if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) {
close(fd);
continue;
}
ev_fds[ev_count].fd = fd;
ev_fds[ev_count].events = POLLIN;
ev_fdinfo[ev_count].cb = input_cb;
ev_fdinfo[ev_count].data = data;
ev_count++;
ev_dev_count++;
if(ev_dev_count == MAX_DEVICES) break;
}
}
return 0;
}
// UI初始化的时候,会启动一个新的线程,来轮询event
pthread_create(&input_t, NULL, input_thread, NULL);
// Reads input events, handles special hot keys, and adds to the key queue.
void* RecoveryUI::input_thread(void *cookie)
{
for (;;) {
if (!ev_wait(-1))
ev_dispatch();
}
return NULL;
}
//分发时间,cb是回调函数,ev_init的时候作为参数传入的
// 此时不代表,所有FD描述符都有事件发生,只能有至少有个一个有时间产生
void ev_dispatch(void)
{
unsigned n;
int ret;
for (n = 0; n < ev_count; n++) {
ev_callback cb = ev_fdinfo[n].cb;
if (cb && (ev_fds[n].revents & ev_fds[n].events))
cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data);
}
}
// 读取文件描述符,读取时间内容发送给UI主线程
// 没有事件的话,就直接返回
int RecoveryUI::input_callback(int fd, short revents, void* data)
{
struct input_event ev;
int ret;
ret = ev_get_input(fd, revents, &ev);
if (ret)
return -1;
.......
if (ev.type == EV_KEY && ev.code <= KEY_MAX)
self->process_key(ev.code, ev.value);
return 0;
}