frontend: input: pass default binds as argument
[pcsx_rearmed.git] / frontend / plat_pollux.c
index daf2f84..80e7a1b 100644 (file)
 #include <linux/fb.h>
 #include <sys/mman.h>
 #include <linux/soundcard.h>
+#include <linux/input.h>
 
 #include "common/input.h"
 #include "gp2x/in_gp2x.h"
+#include "linux/in_evdev.h"
 #include "common/menu.h"
 #include "warm/warm.h"
 #include "plugin_lib.h"
@@ -46,22 +48,30 @@ static unsigned int saved_video_regs[2][6];
 static unsigned short *psx_vram;
 static unsigned int psx_vram_padds[512];
 static int psx_step, psx_width, psx_height, psx_bpp;
-static int psx_offset_x, psx_offset_y;
+static int psx_offset_x, psx_offset_y, psx_src_width, psx_src_height;
 static int fb_offset_x, fb_offset_y;
 
-// TODO: get rid of this
-struct vout_fbdev;
-struct vout_fbdev *layer_fb;
-int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
-
-int omap_enable_layer(int enabled)
-{
-       return 0;
-}
-
 static void caanoo_init(void);
+static void wiz_init(void);
 
 
+static const struct in_default_bind in_evdev_defbinds[] = {
+       { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
+       { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
+       { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
+       { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
+       { BTN_TOP,      IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
+       { BTN_THUMB,    IN_BINDTYPE_PLAYER12, DKEY_CROSS },
+       { BTN_THUMB2,   IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
+       { BTN_TRIGGER,  IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
+       { BTN_BASE3,    IN_BINDTYPE_PLAYER12, DKEY_START },
+       { BTN_BASE4,    IN_BINDTYPE_PLAYER12, DKEY_SELECT },
+       { BTN_TOP2,     IN_BINDTYPE_PLAYER12, DKEY_L1 },
+       { BTN_PINKIE,   IN_BINDTYPE_PLAYER12, DKEY_R1 },
+       { BTN_BASE,     IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
+       { 0, 0, 0 },
+};
+
 static void *fb_flip(void)
 {
        memregl[0x406C>>2] = memregl[0x446C>>2] = fb_paddrs[fb_work_buf];
@@ -248,9 +258,6 @@ void plat_video_menu_leave(void)
 
        memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
        g_menuscreen_ptr = fb_flip();
-       memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
-
-       pollux_changemode(psx_bpp, 1);
 }
 
 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
@@ -304,7 +311,7 @@ static void raw_flip_dma(int x, int y)
        int spsx_line = y + psx_offset_y;
        int spsx_offset = (x + psx_offset_x) & 0x3f8;
        int dst_stride = 320 * psx_bpp / 8;
-       int len = psx_width * psx_bpp / 8;
+       int len = psx_src_width * psx_bpp / 8;
        int i;
 
        warm_cache_op_all(WOP_D_CLEAN);
@@ -322,7 +329,7 @@ static void raw_flip_dma(int x, int y)
                DMA_REG(0x24) = 1;
        }
 
-       for (i = psx_height; i > 0; i--, spsx_line += psx_step, dst += dst_stride) {
+       for (i = psx_src_height; i > 0; i--, spsx_line += psx_step, dst += dst_stride) {
                while ((DMA_REG(0x2c) & 0x0f) < 4)
                        spend_cycles(10);
 
@@ -336,11 +343,11 @@ static void raw_flip_dma(int x, int y)
 
        if (psx_bpp == 16) {
                pl_vout_buf = g_menuscreen_ptr;
-               pl_print_hud(320, fb_offset_y + psx_height, fb_offset_x);
+               pl_print_hud(fb_offset_x);
        }
 
        g_menuscreen_ptr = fb_flip();
-       pl_flip_cnt++;
+       pl_rearmed_cbs.flip_cnt++;
 
        pcnt_end(PCNT_BLIT);
 }
@@ -353,23 +360,23 @@ static void name(int x, int y)
                         (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8;                \
         unsigned int src = (y + psx_offset_y) * 1024 + x + psx_offset_x;                \
         int dst_stride = 320 * psx_bpp / 8;                                             \
-        int len = psx_width * psx_bpp / 8;                                              \
+        int len = psx_src_width * psx_bpp / 8;                                          \
         int i;                                                                          \
                                                                                         \
         pcnt_start(PCNT_BLIT);                                                          \
                                                                                         \
-        for (i = psx_height; i > 0; i--, src += psx_step * 1024, dst += dst_stride) {   \
+        for (i = psx_src_height; i > 0; i--, src += psx_step * 1024, dst += dst_stride) { \
                 src &= 1024*512-1;                                                      \
                 blitfunc(dst, vram + src, len);                                         \
         }                                                                               \
                                                                                         \
         if (psx_bpp == 16) {                                                            \
                 pl_vout_buf = g_menuscreen_ptr;                                         \
-                pl_print_hud(320, fb_offset_y + psx_height, fb_offset_x);               \
+                pl_print_hud(fb_offset_x);                                              \
         }                                                                               \
                                                                                         \
         g_menuscreen_ptr = fb_flip();                                                   \
-        pl_flip_cnt++;                                                                  \
+        pl_rearmed_cbs.flip_cnt++;                                                      \
                                                                                         \
         pcnt_end(PCNT_BLIT);                                                            \
 }
@@ -379,17 +386,20 @@ make_flip_func(raw_flip_soft_368, blit320_368)
 make_flip_func(raw_flip_soft_512, blit320_512)
 make_flip_func(raw_flip_soft_640, blit320_640)
 
-static void *pl_vout_set_mode(int w, int h, int bpp)
+void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
 {
-       static int old_w, old_h, old_bpp;
        int poff_w, poff_h, w_max;
+       int w = *w_, h = *h_, bpp = *bpp_;
 
-       if (!w || !h || !bpp || (w == old_w && h == old_h && bpp == old_bpp))
+       if (!w || !h || !bpp)
                return NULL;
 
        printf("psx mode: %dx%d@%d\n", w, h, bpp);
+       psx_width = w;
+       psx_height = h;
+       psx_bpp = bpp;
 
-       switch (w + (bpp != 16)) {
+       switch (w + (bpp != 16) + !soft_scaling) {
        case 640:
                pl_rearmed_cbs.pl_vout_raw_flip = raw_flip_soft_640;
                w_max = 640;
@@ -431,9 +441,8 @@ static void *pl_vout_set_mode(int w, int h, int bpp)
 
        psx_offset_x = poff_w;
        psx_offset_y = poff_h;
-       psx_width = w;
-       psx_height = h;
-       psx_bpp = bpp;
+       psx_src_width = w;
+       psx_src_height = h;
 
        if (fb_offset_x || fb_offset_y) {
                // not fullscreen, must clear borders
@@ -446,14 +455,27 @@ static void *pl_vout_set_mode(int w, int h, int bpp)
 
        pl_set_gun_rect(fb_offset_x, fb_offset_y, w > 320 ? 320 : w, h);
 
+       // adjust for hud
+       *w_ = 320;
+       *h_ = fb_offset_y + psx_src_height;
+
        return NULL;
 }
 
-static void *pl_vout_flip(void)
+/* not really used, we do raw_flip */
+void plat_gvideo_open(void)
+{
+}
+
+void *plat_gvideo_flip(void)
 {
        return NULL;
 }
 
+void plat_gvideo_close(void)
+{
+}
+
 static void save_multiple_regs(unsigned int *dest, int base, int count)
 {
        const volatile unsigned int *regs = memregl + base / 4;
@@ -521,17 +543,12 @@ void plat_init(void)
        }
        fb_vaddrs[1] = (char *)fb_vaddrs[0] + 320*240*4;
 
+       memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
        pollux_changemode(16, 0);
        g_menuscreen_w = 320;
        g_menuscreen_h = 240;
        g_menuscreen_ptr = fb_flip();
 
-       g_menubg_ptr = calloc(320*240*2, 1);
-       if (g_menubg_ptr == NULL) {
-               fprintf(stderr, "OOM\n");
-               exit(1);
-       }
-
        warm_ret = warm_init();
        have_warm = warm_ret == 0;
        warm_change_cb_upper(WCB_B_BIT, 1);
@@ -590,26 +607,28 @@ void plat_init(void)
        else {
                printf("detected Wiz\n");
                gp2x_dev_id = GP2X_DEV_WIZ;
-               in_gp2x_init();
        }
 
        in_tsbutton_init();
-       in_probe();
+       in_evdev_init(in_evdev_defbinds);
        if (gp2x_dev_id == GP2X_DEV_CAANOO)
                caanoo_init();
+       else
+               wiz_init();
 
        mixerdev = open("/dev/mixer", O_RDWR);
        if (mixerdev == -1)
                perror("open(/dev/mixer)");
 
-       pl_rearmed_cbs.pl_vout_flip = pl_vout_flip;
        pl_rearmed_cbs.pl_vout_raw_flip = have_warm ? raw_flip_dma : raw_flip_soft;
-       pl_rearmed_cbs.pl_vout_set_mode = pl_vout_set_mode;
        pl_rearmed_cbs.pl_vout_set_raw_vram = pl_vout_set_raw_vram;
 
-       psx_width = 320;
-       psx_height = 240;
+       psx_src_width = 320;
+       psx_src_height = 240;
        psx_bpp = 16;
+
+       pl_rearmed_cbs.screen_w = 320;
+       pl_rearmed_cbs.screen_h = 240;
 }
 
 void plat_finish(void)
@@ -634,25 +653,6 @@ void plat_finish(void)
 }
 
 /* Caanoo stuff, perhaps move later */
-#include <linux/input.h>
-
-struct in_default_bind in_evdev_defbinds[] = {
-       { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
-       { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
-       { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
-       { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
-       { BTN_TOP,      IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
-       { BTN_THUMB,    IN_BINDTYPE_PLAYER12, DKEY_CROSS },
-       { BTN_THUMB2,   IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
-       { BTN_TRIGGER,  IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
-       { BTN_BASE3,    IN_BINDTYPE_PLAYER12, DKEY_START },
-       { BTN_BASE4,    IN_BINDTYPE_PLAYER12, DKEY_SELECT },
-       { BTN_TOP2,     IN_BINDTYPE_PLAYER12, DKEY_L1 },
-       { BTN_PINKIE,   IN_BINDTYPE_PLAYER12, DKEY_R1 },
-       { BTN_BASE,     IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
-       { 0, 0, 0 },
-};
-
 static const char * const caanoo_keys[KEY_MAX + 1] = {
        [0 ... KEY_MAX] = NULL,
        [KEY_UP]        = "Up",
@@ -685,21 +685,22 @@ struct haptic_data {
 #define HAPTIC_SET_VIB_LEVEL   _IOW(HAPTIC_IOCTL_MAGIC, 9, unsigned int)
 
 static int hapticdev = -1;
-static struct haptic_data haptic_seq;
+static struct haptic_data haptic_seq[2];
 
-static int haptic_init(void)
+static int haptic_read(const char *fname, struct haptic_data *data)
 {
        int i, ret, v1, v2;
        char buf[128], *p;
        FILE *f;
 
-       f = fopen("haptic.txt", "r");
+       f = fopen(fname, "r");
        if (f == NULL) {
-               perror("fopen(haptic.txt)");
+               fprintf(stderr, "fopen(%s)", fname);
+               perror("");
                return -1;
        }
 
-       for (i = 0; i < sizeof(haptic_seq.actions) / sizeof(haptic_seq.actions[0]); ) {
+       for (i = 0; i < sizeof(data->actions) / sizeof(data->actions[0]); ) {
                p = fgets(buf, sizeof(buf), f);
                if (p == NULL)
                        break;
@@ -714,17 +715,31 @@ static int haptic_init(void)
                        continue;
                }
 
-               haptic_seq.actions[i].time = v1;
-               haptic_seq.actions[i].strength = v2;
+               data->actions[i].time = v1;
+               data->actions[i].strength = v2;
                i++;
        }
        fclose(f);
 
        if (i == 0) {
-               fprintf(stderr, "bad haptic.txt\n");
+               fprintf(stderr, "bad haptic file: %s\n", fname);
                return -1;
        }
-       haptic_seq.count = i;
+       data->count = i;
+
+       return 0;
+}
+
+static int haptic_init(void)
+{
+       int ret, i;
+
+       ret = haptic_read("haptic_w.cfg", &haptic_seq[0]);
+       if (ret != 0)
+               return -1;
+       ret = haptic_read("haptic_s.cfg", &haptic_seq[1]);
+       if (ret != 0)
+               return -1;
 
        hapticdev = open("/dev/isa1200", O_RDWR | O_NONBLOCK);
        if (hapticdev == -1) {
@@ -746,7 +761,7 @@ static int haptic_init(void)
        return 0;
 }
 
-void plat_trigger_vibrate(void)
+void plat_trigger_vibrate(int is_strong)
 {
        int ret;
 
@@ -760,11 +775,18 @@ void plat_trigger_vibrate(void)
                }
        }
 
-       ioctl(hapticdev, HAPTIC_PLAY_PATTERN, &haptic_seq);
+       ioctl(hapticdev, HAPTIC_PLAY_PATTERN, &haptic_seq[!!is_strong]);
+}
+
+static void caanoo_init(void)
+{
+       in_probe();
+       in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
+                     caanoo_keys, sizeof(caanoo_keys));
 }
 
 /* Wiz stuff */
-struct in_default_bind in_gp2x_defbinds[] =
+static const struct in_default_bind in_gp2x_defbinds[] =
 {
        /* MXYZ SACB RLDU */
        { GP2X_BTN_UP,          IN_BINDTYPE_PLAYER12, DKEY_UP },
@@ -811,8 +833,8 @@ void plat_step_volume(int is_up)
 // unused dummy for in_gp2x
 volatile unsigned short *gp2x_memregs;
 
-static void caanoo_init(void)
+static void wiz_init(void)
 {
-       in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
-                     caanoo_keys, sizeof(caanoo_keys));
+       in_gp2x_init(in_gp2x_defbinds);
+       in_probe();
 }