[hatari-devel] Fix for joy button 2 space key emulation "auto-fire"

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Hi,

Currently (default) joy button 2 space key emulation works like auto-fire.

I do not see how this could be the expected behavior, but in most situation user probably does not notice it, because games read only the first space key press.

However, in some other games it is a real problem:
https://www.atari-forum.com/viewtopic.php?t=42586&sid=90e0b24ab726818cfcb83d15e34ee91f

Attached patch fixes that, and actually simplifies the code.

Comments?  Potential problems from this?



	- Eero
From 2d642d325b075a29eae499f7e887620a56f1bc31 Mon Sep 17 00:00:00 2001
From: Eero Tamminen <oak@xxxxxxxxxxxxxx>
Date: Mon, 6 Mar 2023 20:50:10 +0200
Subject: [PATCH] Fix joy button 2 space key emulation

Earlier it worked in rapid autofire mode making it impossible to use
for doing just a single space key press.

This commit changes it to work like normal key presses.

Simulated space key is released when the button is released, which
among other things meaning also that key repeat works as expected.
---
 src/ikbd.c         | 21 ++++++----------
 src/includes/joy.h |  1 +
 src/joy.c          | 62 +++++++++++++++++++++++++++++++++-------------
 3 files changed, 54 insertions(+), 30 deletions(-)

diff --git a/src/ikbd.c b/src/ikbd.c
index 1464562e..685d2feb 100644
--- a/src/ikbd.c
+++ b/src/ikbd.c
@@ -1711,21 +1711,16 @@ static void IKBD_SendAutoKeyboardCommands(void)
 	Keyboard.bOldRButtonDown = Keyboard.bRButtonDown;
 
 	/* Send joystick button '2' as 'Space bar' key - MUST do here so does not get mixed up in middle of joystick packets! */
-	if (JoystickSpaceBar)
+	if (JoystickSpaceBar==JOYSTICK_SPACE_DOWN)
 	{
-		/* As we simulating space bar? */
-		if (JoystickSpaceBar==JOYSTICK_SPACE_DOWN)
-		{
-			IKBD_PressSTKey(57, true);         /* Press */
-			JoystickSpaceBar = JOYSTICK_SPACE_UP;
-		}
-		else   //if (JoystickSpaceBar==JOYSTICK_SPACE_UP) {
-		{
-			IKBD_PressSTKey(57, false);       /* Release */
-			JoystickSpaceBar = false;         /* Complete */
-		}
+		IKBD_PressSTKey(57, true);                /* Press */
+		JoystickSpaceBar = JOYSTICK_SPACE_DOWNED; /* Pressed */
+	}
+	else if (JoystickSpaceBar==JOYSTICK_SPACE_UP)
+	{
+		IKBD_PressSTKey(57, false);               /* Release */
+		JoystickSpaceBar = JOYSTICK_SPACE_NULL;   /* Complete */
 	}
-
 
 	/* If we're executing a custom IKBD program, call it to process the key/mouse/joystick event */
 	if ( IKBD_ExeMode && pIKBD_CustomCodeHandler_Read )
diff --git a/src/includes/joy.h b/src/includes/joy.h
index 8a460192..9fceaad2 100644
--- a/src/includes/joy.h
+++ b/src/includes/joy.h
@@ -12,6 +12,7 @@ enum
 {
 	JOYSTICK_SPACE_NULL,          /* Not up/down */
 	JOYSTICK_SPACE_DOWN,
+	JOYSTICK_SPACE_DOWNED,
 	JOYSTICK_SPACE_UP
 };
 
diff --git a/src/joy.c b/src/joy.c
index 4d485375..34efa7f2 100644
--- a/src/joy.c
+++ b/src/joy.c
@@ -61,7 +61,7 @@ static bool bJoystickWorking[ JOYSTICK_COUNT ] =		/* Is joystick plugged in and
 	false, false, false, false, false, false
 };
 
-int JoystickSpaceBar = false;           /* State of space-bar on joystick button 2 */
+int JoystickSpaceBar = JOYSTICK_SPACE_NULL;   /* State of space-bar on joystick button 2 */
 static uint8_t nJoyKeyEmu[ JOYSTICK_COUNT ];
 static uint16_t nSteJoySelect;
 
@@ -165,7 +165,7 @@ void Joy_Init(void)
 	for (i = 0; i < JOYSTICK_COUNT ; i++)
 		Joy_ValidateJoyId(i);
 
-	JoystickSpaceBar = false;
+	JoystickSpaceBar = JOYSTICK_SPACE_NULL;
 }
 
 
@@ -223,6 +223,44 @@ static bool Joy_ReadJoystick(int nSdlJoyID, JOYREADING *pJoyReading)
 }
 
 
+/*-----------------------------------------------------------------------*/
+/**
+ * Enable PC Joystick button press to mimic space bar
+ * (For XenonII, Flying Shark etc...) or joystick up (jump)
+ *
+ * Return up bit or zero
+ */
+static uint8_t Joy_ButtonSpaceJump(int press, bool jump)
+{
+	/* If "Jump on Button" is enabled, button acts as "ST Joystick up" */
+	if (jump)
+	{
+		if (press)
+			return ATARIJOY_BITMASK_UP;
+		return 0;
+	}
+	/* Otherwise, it acts as pressing SPACE on the ST keyboard
+	 *
+	 * "JoystickSpaceBar" goes through following transitions:
+	 * - JOYSTICK_SPACE_NULL   (init)
+	 * - JOYSTICK_SPACE_DOWN   (joy.c:  button pressed)
+	 * - JOYSTICK_SPACE_DOWNED (ikbd.c: space  pressed)
+	 * - JOYSTICK_SPACE_UP     (joy.c:  button released)
+	 * - JOYSTICK_SPACE_NULL   (ikbd.c: space  released)
+	 */
+	if (JoystickSpaceBar == JOYSTICK_SPACE_NULL)
+	{
+		if (press)
+			JoystickSpaceBar = JOYSTICK_SPACE_DOWN;
+	}
+	else if (JoystickSpaceBar == JOYSTICK_SPACE_DOWNED)
+	{
+		if (!press)
+			JoystickSpaceBar = JOYSTICK_SPACE_UP;
+	}
+	return 0;
+}
+
 /*-----------------------------------------------------------------------*/
 /**
  * Read PC joystick and return ST format byte, i.e. lower 4 bits direction
@@ -291,22 +329,12 @@ uint8_t Joy_GetStickData(int nStJoyId)
 		if (JoyReading.Buttons & JOYREADING_BUTTON1)
 			nData |= ATARIJOY_BITMASK_FIRE;
 
-		/* Enable PC Joystick button 2 to mimic space bar (For XenonII, Flying Shark etc...) */
-		if (nStJoyId == JOYID_JOYSTICK1 && (JoyReading.Buttons & JOYREADING_BUTTON2))
+		/* Enable PC Joystick button 2 to mimic space bar or jump */
+		if (nStJoyId == JOYID_JOYSTICK1)
 		{
-			if (ConfigureParams.Joysticks.Joy[nStJoyId].bEnableJumpOnFire2)
-			{
-				/* If "Jump on Button 2" is enabled, PC Joystick button 2 acts as "ST Joystick up" */
-				nData |= ATARIJOY_BITMASK_UP;
-			} else {
-				/* If "Jump on Button 2" is not enabled, PC Joystick button 2 acts as pressing SPACE on the ST keyboard */
-				/* Only press 'space bar' if not in NULL state */
-				if (!JoystickSpaceBar)
-				{
-					/* Press, ikbd will send packets and de-press */
-					JoystickSpaceBar = JOYSTICK_SPACE_DOWN;
-				}
-			}
+			const bool press = JoyReading.Buttons & JOYREADING_BUTTON2;
+			const bool jump = ConfigureParams.Joysticks.Joy[nStJoyId].bEnableJumpOnFire2;
+			nData |= Joy_ButtonSpaceJump(press, jump);
 		}
 
 		/* PC Joystick button 3 is autofire button for ST joystick button */
-- 
2.30.2



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/