continuing input framework integration
[libpicofe.git] / common / input.c
index 980115e..8262e21 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "common.h"
 #include "input.h"
 #include "../linux/in_evdev.h"
 
@@ -11,7 +12,6 @@ typedef struct
        void *drv_data;
        char *name;
        int *binds;
-       int prev_result;
        int probed:1;
 } in_dev_t;
 
@@ -156,18 +156,11 @@ int in_update(void)
 
        for (i = 0; i < in_dev_count; i++) {
                in_dev_t *dev = &in_devices[i];
-               int ret;
                if (dev->probed && dev->binds != NULL) {
                        switch (dev->drv_id) {
 #ifdef IN_EVDEV
                        case IN_DRVID_EVDEV:
-                               ret = in_evdev_update(dev->drv_data, dev->binds);
-                               if (ret == -1) /* no change */
-                                       result |= dev->prev_result;
-                               else {
-                                       dev->prev_result = ret;
-                                       result |= ret;
-                               }
+                               result |= in_evdev_update(dev->drv_data, dev->binds);
                                break;
 #endif
                        }
@@ -199,12 +192,11 @@ void in_set_blocking(int is_blocking)
        for (i = 0; i < in_dev_count; i++) {
                if (in_devices[i].probed)
                        DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
-               in_devices[i].prev_result = 0;
        }
 
        menu_key_state = 0;
        /* flush events */
-       in_update_keycode(NULL, NULL, 1);
+       in_update_keycode(NULL, NULL, 0);
 }
 
 /* 
@@ -213,7 +205,7 @@ void in_set_blocking(int is_blocking)
  */
 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
 {
-       int result = 0, dev_id, is_down, result_menu;
+       int result = 0, dev_id = 0, is_down, result_menu;
 #ifdef IN_EVDEV
        void **data;
        int i, id = 0, count = 0;
@@ -238,7 +230,7 @@ int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
 #endif
 
        /* keep track of menu key state, to allow mixing
-        * in_update_keycode() and in_update_menu() calls */
+        * in_update_keycode() and in_menu_wait_any() calls */
        result_menu = DRV(in_devices[dev_id].drv_id).menu_translate(result);
        if (result_menu != 0) {
                if (is_down)
@@ -254,10 +246,8 @@ int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
        return result;
 }
 
-/* 
- * same as above, only return bitfield of BTN_*
- */
-int in_update_menu(int timeout_ms)
+/* same as above, only return bitfield of PBTN_*  */
+int in_menu_wait_any(int timeout_ms)
 {
        int keys_old = menu_key_state;
 
@@ -268,7 +258,7 @@ int in_update_menu(int timeout_ms)
                code = in_update_keycode(&dev_id, &is_down, timeout_ms);
                code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
 
-               if (timeout_ms != 0)
+               if (timeout_ms >= 0)
                        break;
                if (code == 0)
                        continue;
@@ -279,6 +269,45 @@ int in_update_menu(int timeout_ms)
        return menu_key_state;
 }
 
+/* wait for menu input, do autorepeat */
+int in_menu_wait(int interesting)
+{
+       static int inp_prev = 0;
+       static int repeats = 0, wait = 20;
+       int ret = 0, release = 0, i;
+
+       if      (repeats == 2) wait = 3;
+       else if (repeats == 4) wait = 2;
+       else if (repeats == 6) wait = 1;
+
+       for (i = 0; i < wait; i++) {
+               ret = in_menu_wait_any(30);
+               if (ret != inp_prev) break;
+               if (i == 0) repeats++;
+       }
+
+       while (!(ret & interesting)) {
+               ret = in_menu_wait_any(-1);
+               release = 1;
+       }
+
+       if (release || ret != inp_prev) {
+               repeats = 0;
+               wait = 20;
+       }
+       if (wait > 6 && (ret & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)))
+               wait = 6;
+       inp_prev = ret;
+
+       // we don't need diagonals in menus
+       if ((ret & PBTN_UP)   && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
+       if ((ret & PBTN_UP)   && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
+       if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
+       if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
+
+       return ret;
+}
+
 const int *in_get_dev_binds(int dev_id)
 {
        if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
@@ -306,11 +335,13 @@ int in_get_dev_bind_count(int dev_id)
        return in_bind_count(in_devices[dev_id].drv_id);
 }
 
-const char *in_get_dev_name(int dev_id)
+const char *in_get_dev_name(int dev_id, int must_be_active)
 {
        if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
                return NULL;
 
+       if (must_be_active && !in_devices[dev_id].probed)
+               return NULL;
        return in_devices[dev_id].name;
 }
 
@@ -589,7 +620,7 @@ int main(void)
        }
 #else
        while (1) {
-               ret = in_update_menu();
+               ret = in_menu_wait_any();
                printf("%08x\n", ret);
        }
 #endif