SDL-1.2: atari:events: use xbios driver wherever possible

From e46487ef7556917870b767c8f03da84999e72adb Mon Sep 17 00:00:00 2001
From: Miro Kropacek <[EMAIL REDACTED]>
Date: Fri, 28 Jun 2024 22:49:55 +0200
Subject: [PATCH] atari:events: use xbios driver wherever possible

IKBD driver serves only as a fallback option for TOS 1.x machines.
---
 README.MiNT                             | 44 ++++++++++---------------
 src/video/ataricommon/SDL_atarievents.c | 44 ++++++-------------------
 src/video/ataricommon/SDL_xbiosevents.c | 26 +++++----------
 3 files changed, 37 insertions(+), 77 deletions(-)

diff --git a/README.MiNT b/README.MiNT
index 45482673..c8bfa22f 100644
--- a/README.MiNT
+++ b/README.MiNT
@@ -49,23 +49,17 @@ III.  Enjoy! :)
 ==============================================================================
 IV.  What is supported:
 
-Keyboard (XBIOS, GEM, Ikbd)
-Mouse (XBIOS, GEM, Ikbd)
+Keyboard (XBIOS, GEM(1), Ikbd)
+Mouse (XBIOS, GEM(2), Ikbd)
 Video (XBIOS (Fullscreen), GEM (Windowed and Fullscreen))
 Timer (Timer C interrupt, GNU pth library)
-Joysticks and joypads (Ikbd, Hardware)
+Joysticks (XBIOS, Ikbd) and joypads (Hardware)
 Audio (Hardware, XBIOS, GSXB, MCSN, STFA, /dev/audio if threads enabled)
 Threads (Multitasking OS only via GNU pth library)
 Shared object loader (using LDG library from http://ldg.atari.org/)
 Audio CD (MetaDOS)
 OpenGL (using Mesa offscreen rendering driver)
 
-- Dependent driver combinations:
-Video   Kbd     Mouse   Joysticks
-xbios   ikbd    ikbd    ikbd
-xbios   xbios   xbios   xbios
-gem     gem     gem(1)  xbios
-
 Audio   O/S     Misc
 dma8    All     Uses MFP Timer A interrupt
 xbios	TOS     Uses MFP Timer A interrupt
@@ -80,15 +74,17 @@ gsxb    All     Uses GSXB callback
 Joypad driver always uses hardware access.
 OpenGL driver always uses OSMesa.
 
-(1) GEM does not report relative mouse motion, so xbios mouse driver is used
+(1) GEM does not report AltrGr and CAPSLOCK key states, so Kbshift() (a BIOS
+function) is used to report those key events.
+(2) GEM does not report relative mouse motion, so xbios mouse driver is used
 to report this type event.
 
 ==============================================================================
 V.  Environment variables:
 
 SDL_VIDEODRIVER:
-	Set to 'xbios' to force xbios video driver
-	Set to 'gem' to force gem video driver
+	Set to 'xbios' to force xbios video driver. Uses XBIOS/Ikbd events driver.
+	Set to 'gem' to force gem video driver. Uses an internal events driver.
 
 SDL_VIDEO_GL_DRIVER:
 	Set to filename to load as OpenGL library, if you use SDL_GL_LoadLibrary()
@@ -107,15 +103,16 @@ SDL_AUDIODRIVER:
 	Set to 'disk' to force disk-writing audio driver
 
 SDL_ATARI_EVENTSDRIVER
-	Set to 'ikbd' to force IKBD 6301 keyboard driver
-	Set to 'xbios' to force xbios keyboard driver
+	Set to 'ikbd' to force IKBD 6301 events driver
+	Set to 'xbios' to force XBIOS events driver
+	GEM events driver is used implicitly if 'gem' video driver is used.
 
 SDL_JOYSTICK_ATARI:
 	Use any of these strings in the environment variable to enable or
 	disable a joystick:
 
 	'ikbd-joy1-[on|off]' for IKBD joystick on port 1 (hardware access)
-	'xbios-joy1-[on|off]' for IKBD joystick on port 1 (xbios access)
+	'xbios-joy1-[on|off]' for IKBD joystick on port 1 (XBIOS access)
 	'porta-pad-[on|off]' for joypad and/or teamtap on port A
 	'porta-joy0-[on|off]' for joystick 0 on port A
 	'porta-joy1-[on|off]' for joystick 1 on port A
@@ -197,18 +194,13 @@ Gem video:
 	All machines:
 		Only the current resolution, if 8 bits or higher depth.
 
-IKBD keyboard, mouse and joystick driver:
-	Available if _MCH cookie is ST, Mega ST, STE, Mega STE, TT, Falcon or
-	ARAnyM.
-
-	Hades has an IKBD, but xbios is not available for video, so IKBD driver is
-	disabled.
-
-XBIOS keyboard driver:
-	Available on all machines with TOS >= 2.x. XBIOS driver is default for non-Atari machines.
+Keyboard, mouse and joystick driver:
+	XBIOS driver is automatically used in TOS >= 2.x and/or in MagiC. Otherwise
+	IKBD driver is used.
 
-Mouse and joystick xbios driver:
-	Available on all machines (I think).
+	As XBIOS video driver is not available on Hades, it is forced to use the
+	implicit GEM driver despite the fact that it has both IKBD and XBIOS
+	features available.
 
 Joypad driver:
 	Available if _MCH cookie is STE or Falcon. Supports teamtap.
diff --git a/src/video/ataricommon/SDL_atarievents.c b/src/video/ataricommon/SDL_atarievents.c
index b29f2e03..a6946910 100644
--- a/src/video/ataricommon/SDL_atarievents.c
+++ b/src/video/ataricommon/SDL_atarievents.c
@@ -29,7 +29,6 @@
  *	This routines choose what the final event manager will be
  */
 
-#include <mint/cookie.h>
 #include <mint/osbind.h>
 
 #include "../../events/SDL_sysevents.h"
@@ -48,15 +47,6 @@ void SDL_AtariMint_UpdateAudio(void);
 void SDL_AtariMint_CheckTimer(void);
 #endif
 
-enum {
-	MCH_ST=0,
-	MCH_STE,
-	MCH_TT,
-	MCH_F30,
-	MCH_CLONE,
-	MCH_ARANYM
-};
-
 /* The translation tables from a console scancode to a SDL keysym */
 static SDLKey keymap[ATARIBIOS_MAXKEYS];
 static const char *keytab_normal;
@@ -67,35 +57,21 @@ void (*Atari_ShutdownEvents)(void);
 static void Atari_InitializeEvents(_THIS)
 {
 	const char *envr;
-	long cookie_mch;
 
-	/* Test if we are on an Atari machine or not */
-	if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
-		cookie_mch = 0;
-	}
-	cookie_mch >>= 16;
-
-	/* Default is Ikbd, the faster except for clones */
-	switch(cookie_mch) {
-		case MCH_ST:
-		case MCH_STE:
-		case MCH_TT:
-		case MCH_F30:
-		case MCH_ARANYM:
-			this->InitOSKeymap=AtariIkbd_InitOSKeymap;
-			this->PumpEvents=AtariIkbd_PumpEvents;
-			Atari_ShutdownEvents=AtariIkbd_ShutdownEvents;
-			break;
-		default:
-			this->InitOSKeymap=AtariXbios_InitOSKeymap;
-			this->PumpEvents=AtariXbios_PumpEvents;
-			Atari_ShutdownEvents=AtariXbios_ShutdownEvents;
-			break;
+	if (!SDL_AtariXbios_IsKeyboardVectorSupported()) {
+		/* Fall back to hardware (TOS 1.x) */
+		this->InitOSKeymap=AtariIkbd_InitOSKeymap;
+		this->PumpEvents=AtariIkbd_PumpEvents;
+		Atari_ShutdownEvents=AtariIkbd_ShutdownEvents;
+	} else {
+		this->InitOSKeymap=AtariXbios_InitOSKeymap;
+		this->PumpEvents=AtariXbios_PumpEvents;
+		Atari_ShutdownEvents=AtariXbios_ShutdownEvents;
 	}
 
 	envr = SDL_getenv("SDL_ATARI_EVENTSDRIVER");
 
- 	if (!envr) {
+	if (!envr) {
 		return;
 	}
 
diff --git a/src/video/ataricommon/SDL_xbiosevents.c b/src/video/ataricommon/SDL_xbiosevents.c
index 0708cf76..5ea9693b 100644
--- a/src/video/ataricommon/SDL_xbiosevents.c
+++ b/src/video/ataricommon/SDL_xbiosevents.c
@@ -27,7 +27,9 @@
  *	Patrice Mandin
  */
 
+#include <mint/cookie.h>
 #include <mint/osbind.h>
+#include <mint/sysvars.h>
 
 #include "../../events/SDL_sysevents.h"
 #include "../../events/SDL_events_c.h"
@@ -52,16 +54,12 @@ static Uint16 atari_prevmouseb;	/* buttons */
 
 /* Functions */
 
-SDL_bool SDL_AtariXbios_IsKeyboardVectorSupported_Super()
-{
-	/* TODO: FreeMiNT (MagiC, Geneva?) */
-	uint16_t* oshdr = *((uint16_t**)(0x4f2));
-	return (oshdr[1] >= 0x0200) ? SDL_TRUE : SDL_FALSE;
-}
-
 SDL_bool SDL_AtariXbios_IsKeyboardVectorSupported()
 {
-	return Supexec(SDL_AtariXbios_IsKeyboardVectorSupported_Super);
+	OSHEADER *tos_header = (OSHEADER *)get_sysvar(_sysbase);
+
+	/* Available only in TOS >= 2.x or in MagiC */
+	return tos_header->os_version >= 0x0200 || Getcookie(C_MagX, NULL) == C_FOUND;
 }
 
 void AtariXbios_InitOSKeymap(_THIS)
@@ -69,12 +67,7 @@ void AtariXbios_InitOSKeymap(_THIS)
 	int vectors_mask;
 	vectors_mask  = ATARI_XBIOS_JOYSTICKEVENTS;	/* XBIOS joystick events */
 	vectors_mask |= ATARI_XBIOS_MOUSEEVENTS;	/* XBIOS mouse events */
-	if (SDL_AtariXbios_IsKeyboardVectorSupported()) {
-		vectors_mask |= ATARI_XBIOS_KEYBOARDEVENTS;	/* XBIOS keyboard events */
-	} else {
-		SDL_SetError("Xbios keyboard vector is not supported on TOS 1.x");
-        return;
-	}
+	vectors_mask |= ATARI_XBIOS_KEYBOARDEVENTS;	/* XBIOS keyboard events */
 
 	SDL_AtariXbios_InstallVectors(vectors_mask);
 }
@@ -131,8 +124,7 @@ void SDL_AtariXbios_RestoreVectors(void)
 
 static int atari_GetButton(int button)
 {
-	switch(button)
-	{
+	switch(button) {
 		case 0:
 			return SDL_BUTTON_RIGHT;
 			break;
@@ -211,7 +203,7 @@ void SDL_AtariXbios_PostKeyboardEvents(_THIS)
 			}
 			SDL_AtariXbios_keyboard[i]=KEY_UNDEFINED;
 		}
-			
+
 		/* Key released ? */
 		if (SDL_AtariXbios_keyboard[i]==KEY_RELEASED) {
 			if (i == SCANCODE_CAPSLOCK) {