minui: add ability to synchronize current key state
If a key is down prior to the time of initialization, we would not get the
down event for the key, and thus think that the key is not pressed.
Add an interface that allows one to provide a callback to execute
on all keys that are currently down.
Change-Id: I2a4096c0cb4c7c7a9a80d207835f168a0b418413
Signed-off-by: Dima Zavin <dima@android.com>
diff --git a/minui/events.c b/minui/events.c
index c533a48..2918afa 100644
--- a/minui/events.c
+++ b/minui/events.c
@@ -27,7 +27,11 @@
#define MAX_DEVICES 16
#define MAX_MISC_FDS 16
-#define test_bit(bit, array) ((array)[(bit)/8] & (1<<((bit)%8)))
+#define BITS_PER_LONG (sizeof(unsigned long) * 8)
+#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
+
+#define test_bit(bit, array) \
+ ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
struct fd_info {
ev_callback cb;
@@ -50,7 +54,7 @@
dir = opendir("/dev/input");
if(dir != 0) {
while((de = readdir(dir))) {
- uint8_t ev_bits[(EV_MAX + 1) / 8];
+ unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
// fprintf(stderr,"/dev/input/%s\n", de->d_name);
if(strncmp(de->d_name,"event",5)) continue;
@@ -139,3 +143,33 @@
}
return -1;
}
+
+int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data)
+{
+ unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)];
+ unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
+ unsigned i;
+ int ret;
+
+ for (i = 0; i < ev_dev_count; i++) {
+ int code;
+
+ memset(key_bits, 0, sizeof(key_bits));
+ memset(ev_bits, 0, sizeof(ev_bits));
+
+ ret = ioctl(ev_fds[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
+ if (ret < 0 || !test_bit(EV_KEY, ev_bits))
+ continue;
+
+ ret = ioctl(ev_fds[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits);
+ if (ret < 0)
+ continue;
+
+ for (code = 0; code <= KEY_MAX; code++) {
+ if (test_bit(code, key_bits))
+ set_key_cb(code, 1, data);
+ }
+ }
+
+ return 0;
+}
diff --git a/minui/minui.h b/minui/minui.h
index cb1ed65..2e2f1f4 100644
--- a/minui/minui.h
+++ b/minui/minui.h
@@ -46,10 +46,12 @@
struct input_event;
typedef int (*ev_callback)(int fd, short revents, void *data);
+typedef int (*ev_set_key_callback)(int code, int value, void *data);
int ev_init(ev_callback input_cb, void *data);
void ev_exit(void);
int ev_add_fd(int fd, ev_callback cb, void *data);
+int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data);
/* timeout has the same semantics as for poll
* 0 : don't block