more input wip
[libpicofe.git] / common / input.c
index 77d9f3a..48e3c0a 100644 (file)
@@ -23,8 +23,11 @@ static int in_dev_count = 0;
 static int in_bind_count(int drv_id)
 {
        int count = 0;
-       if (drv_id == IN_DRVID_EVDEV)
+       switch (drv_id) {
+       case IN_DRVID_EVDEV:
                count = in_evdev_bind_count();
+               break;
+       }
        if (count <= 0)
                printf("input: failed to get bind count for drv %d\n", drv_id);
 
@@ -48,8 +51,11 @@ static int *in_alloc_binds(int drv_id)
 static void in_free(in_dev_t *dev)
 {
        if (dev->probed) {
-               if (dev->drv_id == IN_DRVID_EVDEV)
+               switch (dev->drv_id) {
+               case IN_DRVID_EVDEV:
                        in_evdev_free(dev->drv_data);
+                       break;
+               }
        }
        dev->probed = 0;
        dev->drv_data = NULL;
@@ -157,37 +163,73 @@ int in_update(void)
 
        for (i = 0; i < in_dev_count; i++) {
                if (in_devices[i].probed && in_devices[i].binds != NULL) {
-                       if (in_devices[i].drv_id == IN_DRVID_EVDEV)
+                       switch (in_devices[i].drv_id) {
+                       case IN_DRVID_EVDEV:
                                result |= in_evdev_update(in_devices[i].drv_data, in_devices[i].binds);
+                               break;
+                       }
                }
        }
 
        return result;
 }
 
+static void **in_collect_drvdata(int drv_id, int *count)
+{
+       static void *data[IN_MAX_DEVS];
+       int i;
+
+       for (*count = i = 0; i < in_dev_count; i++) {
+               if (in_devices[i].drv_id == drv_id && in_devices[i].probed)
+                       data[(*count)++] = in_devices[i].drv_data;
+       }
+
+       return data;
+}
+
+void in_set_blocking(int is_blocking)
+{
+       int i;
+
+       for (i = 0; i < in_dev_count; i++) {
+               if (in_devices[i].probed) {
+                       switch (in_devices[i].drv_id) {
+                       case IN_DRVID_EVDEV:
+                               in_evdev_set_blocking(in_devices[i].drv_data, is_blocking);
+                               break;
+                       }
+               }
+       }
+}
+
 /* 
- * update with wait for a press, return bitfield of BTN_*
+ * update with wait for a press, return keycode
  * only can use 1 drv here..
  */
-int in_update_menu(void)
+int in_update_keycode(int *dev_id, int *is_down)
 {
        int result = 0;
 #ifdef IN_EVDEV
-       void *data[IN_MAX_DEVS];
-       int i, count = 0;
-
-       for (i = 0; i < in_dev_count; i++) {
-               if (in_devices[i].probed)
-                       data[count++] = in_devices[i].drv_data;
-       }
+       void **data;
+       int i, id = 0, count = 0;
 
+       data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
        if (count == 0) {
                /* don't deadlock, fail */
                printf("input: failed to find devices to read\n");
                exit(1);
        }
 
-       result = in_evdev_update_menu(data, count);
+       result = in_evdev_update_keycode(data, count, &id, is_down);
+
+       if (dev_id != NULL) {
+               for (i = id; i < in_dev_count; i++) {
+                       if (in_devices[i].drv_data == data[id]) {
+                               *dev_id = i;
+                               break;
+                       }
+               }
+       }
 #else
 #error no menu read handlers
 #endif
@@ -195,6 +237,47 @@ int in_update_menu(void)
        return result;
 }
 
+/* 
+ * same as above, only return bitfield of BTN_*
+ */
+int in_update_menu(void)
+{
+       static int keys_active = 0;
+       int keys_old = keys_active;
+
+       while (1)
+       {
+               int code, is_down = 0;
+               code = in_update_keycode(NULL, &is_down);
+#ifdef IN_EVDEV
+               code = in_evdev_menu_translate(code);
+#endif
+               if (code == 0) continue;
+
+               if (is_down)
+                       keys_active |=  code;
+               else
+                       keys_active &= ~code;
+
+               if (keys_old != keys_active)
+                       break;
+       }
+
+       return keys_active;
+}
+
+const char *in_get_key_name(int dev_id, int keycode)
+{
+       if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
+               return "Unkn0";
+       switch (in_devices[dev_id].drv_id) {
+       case IN_DRVID_EVDEV:
+               return in_evdev_get_key_name(keycode);
+       }
+
+       return "Unkn1";
+}
+
 void in_init(void)
 {
        memset(in_devices, 0, sizeof(in_devices));
@@ -208,11 +291,20 @@ int main(void)
        in_init();
        in_probe();
 
+       in_set_blocking(1);
+
+#if 1
+       while (1) {
+               int dev = 0, down;
+               ret = in_update_keycode(&dev, &down);
+               printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
+       }
+#else
        while (1) {
                ret = in_update_menu();
                printf("%08x\n", ret);
-               sleep(1);
        }
+#endif
 
        return 0;
 }