+#include <sys/time.h>
+#include <time.h>
+
+static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
+{
+ struct timeval start, now;
+ int i, is_down, result;
+
+ gettimeofday(&start, NULL);
+
+ while (1)
+ {
+ for (i = 0; i < in_dev_count; i++) {
+ in_dev_t *d = &in_devices[i];
+ if (!d->probed)
+ continue;
+
+ result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
+ if (result == -1)
+ continue;
+
+ if (dev_id_out)
+ *dev_id_out = i;
+ if (is_down_out)
+ *is_down_out = is_down;
+ return result;
+ }
+
+ if (timeout_ms >= 0) {
+ gettimeofday(&now, NULL);
+ if ((now.tv_sec - start.tv_sec) * 1000 +
+ (now.tv_usec - start.tv_usec) / 1000 > timeout_ms)
+ break;
+ }
+
+ usleep(10000);
+ }
+
+ return -1;
+}