gp2x+wiz binary support, wiz code wip
[picodrive.git] / platform / gp2x / in_gp2x.c
index 489bbf7..ab4ba17 100644 (file)
@@ -1,14 +1,25 @@
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 
-#include "../common/common.h"
 #include "../common/input.h"
 #include "in_gp2x.h"
+#include "soc.h"
 
 #define IN_PREFIX "gp2x:"
 #define IN_GP2X_NBUTTONS 32
 
-extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
+/* note: in_gp2x hadles combos (if 2 btns have the same bind,
+ * both must be pressed for action to happen) */
+static int in_gp2x_combo_keys = 0;
+static int in_gp2x_combo_acts = 0;
+static int gpiodev = -1;       /* Wiz only */
+
+static int (*in_gp2x_get_bits)(void);
 
 enum  { BTN_UP = 0,      BTN_LEFT = 2,      BTN_DOWN = 4,  BTN_RIGHT = 6,
         BTN_START = 8,   BTN_SELECT = 9,    BTN_L = 10,    BTN_R = 11,
@@ -25,10 +36,40 @@ static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = {
        [BTN_PUSH] = "PUSH"
 };
 
+static int in_gp2x_get_mmsp2_bits(void);
+static int in_gp2x_get_wiz_bits(void);
 
 static void in_gp2x_probe(void)
 {
-       in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1);
+       gp2x_soc_t soc;
+
+       soc = soc_detect();
+       switch (soc)
+       {
+       case SOCID_MMSP2:
+               in_gp2x_get_bits = in_gp2x_get_mmsp2_bits;
+               break;
+       case SOCID_POLLUX:
+               gpiodev = open("/dev/GPIO", O_RDONLY);
+               if (gpiodev < 0) {
+                       perror("in_gp2x: couldn't open /dev/GPIO");
+                       return;
+               }
+               in_gp2x_get_bits = in_gp2x_get_wiz_bits;
+               break;
+       default:
+               return;
+       }
+
+       in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1);
+}
+
+static void in_gp2x_free(void *drv_data)
+{
+       if (gpiodev >= 0) {
+               close(gpiodev);
+               gpiodev = -1;
+       }
 }
 
 static int in_gp2x_get_bind_count(void)
@@ -36,9 +77,10 @@ static int in_gp2x_get_bind_count(void)
        return IN_GP2X_NBUTTONS;
 }
 
-static int in_gp2x_get_gpio_bits(void)
+static int in_gp2x_get_mmsp2_bits(void)
 {
 #ifndef FAKE_IN_GP2X
+       extern volatile unsigned short *gp2x_memregs;
        int value;
        value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
        if (value == 0xFD) value = 0xFA;
@@ -56,17 +98,46 @@ static int in_gp2x_get_gpio_bits(void)
 #endif
 }
 
+static int in_gp2x_get_wiz_bits(void)
+{
+       int value = 0;
+       read(gpiodev, &value, 4);
+       if (value & 0x02)
+               value |= 0x05;
+       if (value & 0x08)
+               value |= 0x14;
+       if (value & 0x20)
+               value |= 0x50;
+       if (value & 0x80)
+               value |= 0x41;
+
+       /* convert to GP2X style */
+       value &= 0x7ff55;
+       if (value & (1 << 16))
+               value |= 1 << BTN_VOL_UP;
+       if (value & (1 << 17))
+               value |= 1 << BTN_VOL_DOWN;
+       if (value & (1 << 18))
+               value |= 1 << BTN_PUSH;
+       value &= ~0x70000;
+
+       return value;
+}
+
 /* returns bitfield of binds of pressed buttons */
 int in_gp2x_update(void *drv_data, int *binds)
 {
-       int i, value, ret = 0;
+       int i, keys, ret = 0;
+
+       keys = in_gp2x_get_bits();
 
-       value = in_gp2x_get_gpio_bits();
+       if (keys & in_gp2x_combo_keys)
+               return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts);
 
-       for (i = 0; value; i++) {
-               if (value & 1)
+       for (i = 0; keys; i++) {
+               if (keys & 1)
                        ret |= binds[i];
-               value >>= 1;
+               keys >>= 1;
        }
 
        return ret;
@@ -77,7 +148,7 @@ int in_gp2x_update_keycode(void *data, int *is_down)
        static int old_val = 0;
        int val, diff, i;
 
-       val = in_gp2x_get_gpio_bits();
+       val = in_gp2x_get_bits();
        diff = val ^ old_val;
        if (diff == 0)
                return -1;
@@ -94,18 +165,45 @@ int in_gp2x_update_keycode(void *data, int *is_down)
        return i;
 }
 
+static const struct {
+       short key;
+       short pbtn;
+} key_pbtn_map[] =
+{
+       { BTN_UP,       PBTN_UP },
+       { BTN_DOWN,     PBTN_DOWN },
+       { BTN_LEFT,     PBTN_LEFT },
+       { BTN_RIGHT,    PBTN_RIGHT },
+       { BTN_B,        PBTN_MOK },
+       { BTN_X,        PBTN_MBACK },
+       { BTN_A,        PBTN_MA2 },
+       { BTN_Y,        PBTN_MA3 },
+       { BTN_L,        PBTN_L },
+       { BTN_R,        PBTN_R },
+       { BTN_SELECT,   PBTN_MENU },
+};
+
+#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
+
 static int in_gp2x_menu_translate(int keycode)
 {
-       switch (keycode) {
-               case BTN_UP:    return PBTN_UP;
-               case BTN_LEFT:  return PBTN_LEFT;
-               case BTN_DOWN:  return PBTN_DOWN;
-               case BTN_RIGHT: return PBTN_RIGHT;
-               case BTN_B:     return PBTN_MOK;
-               case BTN_X:     return PBTN_MBACK;
-               case BTN_START: return PBTN_MENU;
-               default:        return 0;
+       int i;
+       if (keycode < 0)
+       {
+               /* menu -> kc */
+               keycode = -keycode;
+               for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
+                       if (key_pbtn_map[i].pbtn == keycode)
+                               return key_pbtn_map[i].key;
+       }
+       else
+       {
+               for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
+                       if (key_pbtn_map[i].key == keycode)
+                               return key_pbtn_map[i].pbtn;
        }
+
+       return 0;
 }
 
 static int in_gp2x_get_key_code(const char *key_name)
@@ -146,6 +244,12 @@ static const struct {
        { BTN_B,        5 },    /* C */
        { BTN_A,        6 },    /* A */
        { BTN_START,    7 },
+       { BTN_SELECT,   23 },   /* menu */
+       { BTN_Y,        26 },   /* switch rend */
+       { BTN_L,        27 },   /* save state */
+       { BTN_R,        28 },   /* load state */
+       { BTN_VOL_UP,   29 },   /* vol up */
+       { BTN_VOL_DOWN, 30 },   /* vol down */
 };
 
 #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
@@ -161,15 +265,31 @@ static void in_gp2x_get_def_binds(int *binds)
 /* remove binds of missing keys, count remaining ones */
 static int in_gp2x_clean_binds(void *drv_data, int *binds)
 {
-       int i, count = 0;
+       int i, count = 0, have_vol = 0, have_menu = 0;
 
        for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
                if (in_gp2x_keys[i] == NULL)
                        binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0;
-               if (binds[i])
+               if (binds[i]) {
                        count++;
+                       if (binds[i] & ((1 << 29)|(1 << 30)))
+                               have_vol = 1;
+                       if (binds[i] & (1 << 23))
+                               have_menu = 1;
+               }
+       }
+
+       /* autobind some important keys, if they are unbound */
+       if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) {
+               binds[BTN_VOL_UP]   = 1 << 29;
+               binds[BTN_VOL_DOWN] = 1 << 30;
        }
 
+       if (!have_menu && binds[BTN_SELECT] == 0)
+               binds[BTN_SELECT] = 1 << 23;
+
+       in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
+
        return count;
 
 }
@@ -178,8 +298,11 @@ void in_gp2x_init(void *vdrv)
 {
        in_drv_t *drv = vdrv;
 
+       in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
+
        drv->prefix = in_gp2x_prefix;
        drv->probe = in_gp2x_probe;
+       drv->free = in_gp2x_free;
        drv->get_bind_count = in_gp2x_get_bind_count;
        drv->get_def_binds = in_gp2x_get_def_binds;
        drv->clean_binds = in_gp2x_clean_binds;