From 609378be70983806bac14419070935789d10e92b Mon Sep 17 00:00:00 2001 From: raphkoster Date: Sun, 23 Jun 2019 23:03:59 -0700 Subject: [PATCH] Add support for four controllers. --- libretro/core-mapper.c | 51 ++++++++++++++++------------- libretro/libretro-core.c | 23 +++++++++---- libretro/platform.c | 71 +++++++++++++++++++++++++++++++++------- 3 files changed, 104 insertions(+), 41 deletions(-) diff --git a/libretro/core-mapper.c b/libretro/core-mapper.c index dc61f24..237711e 100644 --- a/libretro/core-mapper.c +++ b/libretro/core-mapper.c @@ -63,7 +63,7 @@ int touch=-1; // gui mouse btn //JOY int al[2][2];//left analog1 int ar[2][2];//right analog1 -unsigned char MXjoy[2]; // joy +unsigned char MXjoy[4]; // joy #define JOYRANGE_UP_VALUE -16384 /* Joystick ranges in XY */ #define JOYRANGE_DOWN_VALUE 16383 @@ -171,7 +171,7 @@ int RSTOPON=-1; int CTRLON=-1; extern unsigned short int bmp[400*300]; -extern unsigned atari_devices[ 2 ]; +extern unsigned atari_devices[ 4 ]; extern int UI_is_active; extern int CURRENT_TV; @@ -401,13 +401,13 @@ void Process_key() if(i==RETROK_LALT){ KBMOD=-KBMOD; - printf("Modifier alt pressed %d \n",KBMOD); + printf("Modifier alt pressed %d \n",KBMOD); continue; } //printf("press: %d \n",i); //retro_key_down(i); - - } + + } else if ( !Key_Sate[i] && Key_Sate[i]!=old_Key_Sate[i] ) { /* @@ -417,25 +417,25 @@ void Process_key() */ if(i==RETROK_RCTRL){ CTRLON=-CTRLON; - printf("Modifier crtl released %d \n",CTRLON); + printf("Modifier crtl released %d \n",CTRLON); continue; } if(i==RETROK_RSHIFT){ SHIFTON=-SHIFTON; - printf("Modifier shift released %d \n",SHIFTON); + printf("Modifier shift released %d \n",SHIFTON); continue; } if(i==RETROK_LALT){ KBMOD=-KBMOD; - printf("Modifier alt released %d \n",KBMOD); + printf("Modifier alt released %d \n",KBMOD); continue; } //printf("release: %d \n",i); //retro_key_up(i); - - } + + } memcpy(old_Key_Sate,Key_Sate , sizeof(Key_Sate) ); @@ -446,9 +446,11 @@ int Retro_PollEvent() // RETRO B Y SLT STA UP DWN LEFT RGT A X L R L2 R2 L3 R3 // INDEX 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - int SAVPAS=PAS; + int SAVPAS=PAS; int i,j; - static int vbt[2][16]={ + static int vbt[4][16]={ + {0x0,0x0,0x0,0x0,0x01,0x02,0x04,0x08,0x80,0x40,0x0,0x0,0x0,0x0,0x0,0x0}, + {0x0,0x0,0x0,0x0,0x01,0x02,0x04,0x08,0x80,0x40,0x0,0x0,0x0,0x0,0x0,0x0}, {0x0,0x0,0x0,0x0,0x01,0x02,0x04,0x08,0x80,0x40,0x0,0x0,0x0,0x0,0x0,0x0}, {0x0,0x0,0x0,0x0,0x01,0x02,0x04,0x08,0x80,0x40,0x0,0x0,0x0,0x0,0x0,0x0}, }; @@ -462,15 +464,16 @@ int Retro_PollEvent() if(SHOWKEY==-1 && pauseg==0)Process_key(); - - //Joy mode - for(j=0;j<2;j++){ + //Joy mode + for(j=0;j<4;j++){ for(i=4;i<10;i++) { - if( input_state_cb(j, RETRO_DEVICE_JOYPAD, 0, i)) - MXjoy[j] |= vbt[j][i]; // Joy press - else if( MXjoy[j]&vbt[j][i])MXjoy[j] &= ~vbt[j][i]; // Joy press + if( input_state_cb(j, RETRO_DEVICE_JOYPAD, 0, i)) { + MXjoy[j] |= vbt[j][i]; // Joy press + fprintf(stderr, "[Libretro-atari800]: MXjoy press controller %u slot %u.\n", j, i); + } + else if( MXjoy[j]&vbt[j][i])MXjoy[j] &= ~vbt[j][i]; // Joy press } } @@ -505,8 +508,10 @@ int Retro_PollEvent() // 0 1 2 3 for(i=0;i<4;i++){ - if ( input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) && mbt[i]==0 ) + if ( input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) && mbt[i]==0 ) { mbt[i]=1; + fprintf(stderr, "[Libretro-atari800]: atari_devices press slot %u.\n", i); + } else if ( mbt[i]==1 && ! input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) ) { mbt[i]=0; @@ -517,8 +522,10 @@ int Retro_PollEvent() // 10 11 12 13 14 15 for(i=10;i<16;i++){ - if ( input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) && mbt[i]==0 ) + if ( input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) && mbt[i]==0 ) { + fprintf(stderr, "[Libretro-atari800]: atari_devices press slot %u.\n", i); mbt[i]=1; + } else if ( mbt[i]==1 && ! input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) ) { mbt[i]=0; @@ -559,7 +566,7 @@ int Retro_PollEvent() if(mmbL==0 && mouse_l){ - mmbL=1; + mmbL=1; pushi=1; touch=1; @@ -572,7 +579,7 @@ int Retro_PollEvent() } if(mmbR==0 && mouse_r){ - mmbR=1; + mmbR=1; } else if(mmbR==1 && !mouse_r) { mmbR=0; diff --git a/libretro/libretro-core.c b/libretro/libretro-core.c index 040a49d..2992e78 100644 --- a/libretro/libretro-core.c +++ b/libretro/libretro-core.c @@ -20,7 +20,7 @@ int retroh=300; #define RETRO_DEVICE_ATARI_KEYBOARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_KEYBOARD, 0) #define RETRO_DEVICE_ATARI_JOYSTICK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 1) -unsigned atari_devices[ 2 ]; +unsigned atari_devices[ 4 ]; int keyboard_type=0; int autorun5200=0; @@ -72,11 +72,20 @@ void retro_set_environment(retro_environment_t cb) { "ATARI Joystick", RETRO_DEVICE_ATARI_JOYSTICK }, { "ATARI Keyboard", RETRO_DEVICE_ATARI_KEYBOARD }, }; - + static const struct retro_controller_description p3_controllers[] = { + { "ATARI Joystick", RETRO_DEVICE_ATARI_JOYSTICK }, + { "ATARI Keyboard", RETRO_DEVICE_ATARI_KEYBOARD }, + }; + static const struct retro_controller_description p4_controllers[] = { + { "ATARI Joystick", RETRO_DEVICE_ATARI_JOYSTICK }, + { "ATARI Keyboard", RETRO_DEVICE_ATARI_KEYBOARD }, + }; static const struct retro_controller_info ports[] = { { p1_controllers, 2 }, // port 1 { p2_controllers, 2 }, // port 2 + { p3_controllers, 2 }, // port 3 + { p4_controllers, 2 }, // port 4 { NULL, 0 } }; @@ -551,7 +560,7 @@ unsigned retro_api_version(void) void retro_set_controller_port_device( unsigned port, unsigned device ) { - if ( port < 2 ) + if ( port < 4 ) { atari_devices[ port ] = device; @@ -742,15 +751,15 @@ bool retro_unserialize(const void *data_, size_t size) void *retro_get_memory_data(unsigned id) { - if ( id == RETRO_MEMORY_SYSTEM_RAM ) - return MEMORY_mem; + if ( id == RETRO_MEMORY_SYSTEM_RAM ) + return MEMORY_mem; return NULL; } size_t retro_get_memory_size(unsigned id) { - if ( id == RETRO_MEMORY_SYSTEM_RAM ) - return 65536; + if ( id == RETRO_MEMORY_SYSTEM_RAM ) + return 65536; return 0; } diff --git a/libretro/platform.c b/libretro/platform.c index 3696f64..0735303 100644 --- a/libretro/platform.c +++ b/libretro/platform.c @@ -49,7 +49,9 @@ extern int UI_is_active; static int swap_joysticks = FALSE; int PLATFORM_kbd_joy_0_enabled = TRUE; /* enabled by default, doesn't hurt */ int PLATFORM_kbd_joy_1_enabled = TRUE;//FALSE; /* disabled, would steal normal keys */ -extern unsigned char MXjoy[2]; // joy +int PLATFORM_kbd_joy_2_enabled = TRUE;//FALSE; /* disabled, would steal normal keys */ +int PLATFORM_kbd_joy_3_enabled = TRUE;//FALSE; /* disabled, would steal normal keys */ +extern unsigned char MXjoy[4]; // joy extern int mbt[16]; extern int retro_sound_finalized; @@ -655,10 +657,10 @@ void PLATFORM_PaletteUpdate(void) retro_PaletteUpdate(); } -static void get_platform_PORT(unsigned char *s0, unsigned char *s1) +static void get_platform_PORT(unsigned char *s0, unsigned char *s1, unsigned char *s2, unsigned char *s3) { - int stick0, stick1; - stick0 = stick1 = INPUT_STICK_CENTRE; + int stick0, stick1, stick2, stick3; + stick0 = stick1 = stick2 = stick3 = INPUT_STICK_CENTRE; if (PLATFORM_kbd_joy_0_enabled) { if (MXjoy[0]&0x04) @@ -680,24 +682,47 @@ static void get_platform_PORT(unsigned char *s0, unsigned char *s1) stick1 &= INPUT_STICK_FORWARD; if (MXjoy[1]&0x02) stick1 &= INPUT_STICK_BACK; - + } + if (PLATFORM_kbd_joy_2_enabled) { + if (MXjoy[2]&0x04) + stick2 &= INPUT_STICK_LEFT; + if (MXjoy[2]&0x08) + stick2 &= INPUT_STICK_RIGHT; + if (MXjoy[2]&0x01) + stick2 &= INPUT_STICK_FORWARD; + if (MXjoy[2]&0x02) + stick2 &= INPUT_STICK_BACK; + } + if (PLATFORM_kbd_joy_3_enabled) { + if (MXjoy[3]&0x04) + stick3 &= INPUT_STICK_LEFT; + if (MXjoy[3]&0x08) + stick3 &= INPUT_STICK_RIGHT; + if (MXjoy[3]&0x01) + stick3 &= INPUT_STICK_FORWARD; + if (MXjoy[3]&0x02) + stick3 &= INPUT_STICK_BACK; } if (swap_joysticks) { *s1 = stick0; *s0 = stick1; + *s2 = stick2; + *s3 = stick3; } else { *s0 = stick0; *s1 = stick1; + *s2 = stick2; + *s3 = stick3; } } -static void get_platform_TRIG(unsigned char *t0, unsigned char *t1) +static void get_platform_TRIG(unsigned char *t0, unsigned char *t1, unsigned char *t2, unsigned char *t3) { - int trig0, trig1; - trig0 = trig1 = 1; + int trig0, trig1, trig2, trig3; + trig0 = trig1 = trig2 = trig3 = 1; if (PLATFORM_kbd_joy_0_enabled) { trig0 = MXjoy[0]&0x80?0:1; @@ -707,13 +732,25 @@ static void get_platform_TRIG(unsigned char *t0, unsigned char *t1) trig1 = MXjoy[1]&0x80?0:1; } + if (PLATFORM_kbd_joy_2_enabled) { + trig2 = MXjoy[2]&0x80?0:1; + } + + if (PLATFORM_kbd_joy_3_enabled) { + trig3 = MXjoy[3]&0x80?0:1; + } + if (swap_joysticks) { *t1 = trig0; *t0 = trig1; + *t2 = trig2; + *t3 = trig3; } else { *t0 = trig0; *t1 = trig1; + *t2 = trig2; + *t3 = trig3; } } @@ -721,25 +758,35 @@ static void get_platform_TRIG(unsigned char *t0, unsigned char *t1) int PLATFORM_PORT(int num) { if (num == 0) { - UBYTE a, b; - get_platform_PORT(&a, &b); + UBYTE a, b, c, d; + get_platform_PORT(&a, &b, &c, &d); return (b << 4) | (a & 0x0f); } + if (num == 1) { + UBYTE a, b, c, d; + get_platform_PORT(&a, &b, &c, &d); + + return (d << 4) | (c & 0x0f); + } return 0xff; } int PLATFORM_TRIG(int num) { - UBYTE a, b; - get_platform_TRIG(&a, &b); + UBYTE a, b, c, d; + get_platform_TRIG(&a, &b, &c, &d); switch (num) { case 0: return a; case 1: return b; + case 2: + return c; + case 3: + return d; default: break; }