SDL-1.2: atari:events:ikbd: add check for unprocessed packets

From 8f05ce5cdc2bec93671fe3fa188ef1ff6b7945af Mon Sep 17 00:00:00 2001
From: Miro Kropacek <[EMAIL REDACTED]>
Date: Sat, 22 Aug 2026 21:14:18 +1000
Subject: [PATCH] atari:events:ikbd: add check for unprocessed packets

Do what xbios does (to make sure that all packets were processed before
the switch to our 0x118). When exiting, just make sure that 0x118 points
to the original "ikbd" address and not one of the mouse/joystick
routines.
---
 src/video/ataricommon/SDL_ikbdinterrupt.S | 38 ++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/video/ataricommon/SDL_ikbdinterrupt.S b/src/video/ataricommon/SDL_ikbdinterrupt.S
index a9ee8c74..6c1329a3 100644
--- a/src/video/ataricommon/SDL_ikbdinterrupt.S
+++ b/src/video/ataricommon/SDL_ikbdinterrupt.S
@@ -46,11 +46,40 @@
 /*--- Install our IKBD vector ---*/
 
 SYM(SDL_AtariIkbd_Install):
-	| Disable interrupts
+	| KBDVBASE *Kbdvbase(void);
+
+#if defined(__mcoldfire__)
+	lea	sp@(-8),sp
+	moveml	d2/a2,sp@
+#else
+	moveml	d2/a2,sp@-
+#endif
+	movew	#34,sp@-
+	trap	#14
+	addql	#2,sp
+#if defined(__mcoldfire__)
+	moveml	sp@,d2/a2
+	lea	sp@(8),sp
+#else
+	moveml	sp@+,d2/a2
+#endif
+	movel	d0,a1
+
+processing_ikbd_packet_install:
+
+	| Stop interrupts
 
 	movew	sr,d1
 	movew	#0x2700,sr
 
+	| Test kbstate
+
+	tstb	a1@(36)
+	beqs	no_ikbd_packet_install
+	movew	d1,sr
+	bras	processing_ikbd_packet_install
+no_ikbd_packet_install:
+
 	| Save MFP registers used for keyboard
 
 	lea	0xfffffa00:w,a0
@@ -138,6 +167,13 @@ ikbd_restoreierb:
 	bset	#6,a0@(0x15)
 ikbd_restoreimrb:
 
+#if defined(__mcoldfire__)
+	movel	#ikbd,d0
+	movel	d0,0x118:w
+#else
+	movel	#ikbd,0x118:w
+#endif
+
 	movew	d1,sp@-
 
 	pea	ikbd(pc)