Skip to content

Commit

Permalink
Update joystick.c
Browse files Browse the repository at this point in the history
  • Loading branch information
negativeExponent committed Aug 16, 2024
1 parent 8815117 commit 193dee7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 91 deletions.
169 changes: 90 additions & 79 deletions x11/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,101 +98,117 @@ static int get_px68k_input(int port)
return res;
}

#define PAD_2BUTTON 0
#define PAD_CPSF_MD 1
#define PAD_CPSF_SFC 2

void FASTCALL Joystick_Update(int is_menu, int key, int port)
{
static uint8_t pre_ret0 = 0xff;

uint8_t ret0 = 0xff;
uint8_t ret1 = 0xff;
uint8_t temp = 0;
int res = 0;
int input = 0;

if (libretro_supports_input_bitmasks)
res = get_px68k_input_bitmasks(port);
input = get_px68k_input_bitmasks(port);
else
res = get_px68k_input(port);
input = get_px68k_input(port);

/* D-Pad */
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
temp |= JOY_RIGHT;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
temp |= JOY_LEFT;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
temp |= JOY_UP;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
temp |= JOY_DOWN;

/* disallow invalid input */
if ((temp & (JOY_LEFT | JOY_RIGHT)) == (JOY_LEFT | JOY_RIGHT))
temp &= ~(JOY_LEFT | JOY_RIGHT);
if ((temp & (JOY_UP | JOY_DOWN)) == (JOY_UP | JOY_DOWN))
temp &= ~(JOY_UP | JOY_DOWN);

ret0 ^= temp;
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
ret0 &= ~JOY_RIGHT;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
ret0 &= ~JOY_LEFT;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
ret0 &= ~JOY_UP;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
ret0 &= ~JOY_DOWN;

/* disallow invalid button combinations */
{
uint8_t mask = (JOY_LEFT | JOY_RIGHT);
if ((ret0 & ~mask) == mask)
ret0 &= ~mask;
mask = (JOY_UP | JOY_DOWN);
if ((ret0 & ~mask) == mask)
ret0 &= ~mask;
}

/* Buttons */
switch (Config.joyType[port])
{
case PAD_2BUTTON:
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 ^= (Config.VbtnSwap ? JOY_TRG1 : JOY_TRG2);
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 ^= (Config.VbtnSwap ? JOY_TRG2 : JOY_TRG1);
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret0 ^= (Config.VbtnSwap ? JOY_TRG2 : JOY_TRG1);
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret0 ^= (Config.VbtnSwap ? JOY_TRG1 : JOY_TRG2);

if (res & (1 << RETRO_DEVICE_ID_JOYPAD_START))
ret0 &= ~(JOY_UP | JOY_DOWN);
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 &= ~(Config.VbtnSwap ? JOY_TRG1 : JOY_TRG2);

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 &= ~(Config.VbtnSwap ? JOY_TRG2 : JOY_TRG1);

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret0 &= ~(Config.VbtnSwap ? JOY_TRG2 : JOY_TRG1);

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret0 &= ~(Config.VbtnSwap ? JOY_TRG1 : JOY_TRG2);

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_START))
ret0 &= ~JOY_START;

if (!Config.P1SelectMap)
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
ret0 &= ~(JOY_LEFT | JOY_RIGHT);
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
ret0 &= ~JOY_SELECT;
break;

case PAD_CPSF_MD:
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 ^= JOY_TRG1;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 ^= JOY_TRG2;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret1 ^= JOY_TRG4;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret1 ^= JOY_TRG3;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_L))
ret1 ^= JOY_TRG5;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_R))
ret1 ^= JOY_TRG8;

if (res & (1 << RETRO_DEVICE_ID_JOYPAD_START))
ret1 ^= JOY_TRG6;
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 &= ~JOY_TRG1;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 &= ~JOY_TRG2;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret1 &= ~JOY_TRG4;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret1 &= ~JOY_TRG3;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_L))
ret1 &= ~JOY_TRG5;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_R))
ret1 &= ~JOY_TRG8;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_START))
ret1 &= ~JOY_TRG6;

if (!Config.P1SelectMap)
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
ret1 ^= JOY_TRG7;
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
ret1 &= ~JOY_TRG7;
break;

case PAD_CPSF_SFC:
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 ^= JOY_TRG2;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 ^= JOY_TRG1;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret1 ^= JOY_TRG3;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret1 ^= JOY_TRG4;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_L))
ret1 ^= JOY_TRG8;
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_R))
ret1 ^= JOY_TRG5;

if (res & (1 << RETRO_DEVICE_ID_JOYPAD_START))
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_A))
ret0 &= ~JOY_TRG2;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_B))
ret0 &= ~JOY_TRG1;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_X))
ret1 &= ~JOY_TRG3;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_Y))
ret1 &= ~JOY_TRG4;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_L))
ret1 &= ~JOY_TRG8;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_R))
ret1 &= ~JOY_TRG5;

if (input & (1 << RETRO_DEVICE_ID_JOYPAD_START))
ret1 ^= JOY_TRG6;

if (!Config.P1SelectMap)
if (res & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
if (input & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT))
ret1 ^= JOY_TRG7;
break;
}
Expand All @@ -201,12 +217,10 @@ void FASTCALL Joystick_Update(int is_menu, int key, int port)
JoyUpState0 = (ret0 ^ pre_ret0) & ret0;
pre_ret0 = ret0;

if (!is_menu)
{
JoyState0[port] = ret0;
JoyState1[port] = ret1;
}
else
JoyState0[port] = ret0;
JoyState1[port] = ret1;

if (is_menu)
{
/* input overrides section during Menu mode for faster menu browsing
* by pressing and holding key or button aka turbo mode */
Expand All @@ -221,9 +235,6 @@ void FASTCALL Joystick_Update(int is_menu, int key, int port)
if ((inbuf & (JOY_UP | JOY_DOWN)) == (JOY_UP | JOY_DOWN))
inbuf &= ~(JOY_UP | JOY_DOWN);

for (i = 0; i < 4; i++)
speedup_joy[1 << i] = 0;

if (last_inbuf != inbuf)
{
last_inbuf = inbuf;
Expand All @@ -247,7 +258,7 @@ void FASTCALL Joystick_Update(int is_menu, int key, int port)
/* which direction? UP/DOWN/LEFT/RIGHT */
uint8_t tmp = (1 << i);
if ((inbuf & tmp) == tmp)
speedup_joy[tmp] = 1;
JoyDownState0 &= ~tmp;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions x11/joystick.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef _X68K_JOY_H
#define _X68K_JOY_H

#define PAD_2BUTTON 0
#define PAD_CPSF_MD 1
#define PAD_CPSF_SFC 2

#define JOY_UP 0x01
#define JOY_DOWN 0x02
#define JOY_LEFT 0x04
Expand All @@ -15,6 +19,9 @@
#define JOY_TRG8 0x20
#define JOY_TRG6 0x40

#define JOY_SELECT (JOY_UP | JOY_DOWN)
#define JOY_START (JOY_LEFT | JOY_RIGHT)

void Joystick_Init(void);
void Joystick_Cleanup(void);
uint8_t FASTCALL Joystick_Read(uint8_t num);
Expand Down
11 changes: 0 additions & 11 deletions x11/winui.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,6 @@ static void shortcut_dir(int drv)
}
}

int speedup_joy[0xff] = { 0 };

int WinUI_Menu(int first)
{
int cursor0;
Expand All @@ -429,15 +427,6 @@ int WinUI_Menu(int first)
joy = get_joy_downstate();
reset_joy_downstate();

if (speedup_joy[JOY_RIGHT])
joy &= ~JOY_RIGHT;
if (speedup_joy[JOY_LEFT])
joy &= ~JOY_LEFT;
if (speedup_joy[JOY_UP])
joy &= ~JOY_UP;
if (speedup_joy[JOY_DOWN])
joy &= ~JOY_DOWN;

if (!(joy & JOY_UP))
{
switch (menu_state)
Expand Down
1 change: 0 additions & 1 deletion x11/winui.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern uint8_t Debug_Text, Debug_Grp, Debug_Sp;

extern char cur_dir_str[];
extern int cur_dir_slen;
extern int speedup_joy[0xff];

void WinUI_Init(void);
int WinUI_Menu(int first);
Expand Down

0 comments on commit 193dee7

Please sign in to comment.