[PATCH] Fix inconsistencies in keymap table sizes

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


IKBD_PressSTKey() is used to pass KeyStates[] array values maintained
by keymap.c, to ScanCodeState[] array maintained by ikbd.

However, former was 14 items (128-0x72) items shorter than latter.

And LoadedKeymap[] used by keymap.c to allow users to map the keys,
was one item shorter than that.

Thorsten Otto reported that the sym one gets from the SDL for eg. an
"y" on a german keyboard is 0x79, with is just the ascii code of y.
But it is larger than KBD_MAX_SCANCODE (0x72) and thus cannot be
remapped.

This commit makes all these arrays to be of same size (128 items),
which should fix that bug.
---
 src/ikbd.c          |  9 ++++-----
 src/includes/ikbd.h |  4 ++--
 src/keymap.c        | 14 +++++++-------
 3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/ikbd.c b/src/ikbd.c
index 1f162af6..f071d5aa 100644
--- a/src/ikbd.c
+++ b/src/ikbd.c
@@ -379,7 +379,7 @@ static void	(*pIKBD_CustomCodeHandler_Read) ( void );
 static void	(*pIKBD_CustomCodeHandler_Write) ( Uint8 );
 static bool	IKBD_ExeMode = false;
 
-static Uint8	ScanCodeState[ 128 ];			/* state of each key : 0=released 1=pressed */
+static Uint8	ScanCodeState[KBD_SCANCODES];		/* state of each key : 0=released 1=pressed */
 
 /* This array contains all known custom 6301 programs, with their CRC */
 static const struct
@@ -569,9 +569,8 @@ static void	IKBD_Boot_ROM ( bool ClearAllRAM )
 
 	KeyboardProcessor.Joy.PrevJoyData[0] = KeyboardProcessor.Joy.PrevJoyData[1] = 0;
 
-	for ( i=0 ; i<128 ; i++ )
-		ScanCodeState[ i ] = 0;				/* key is released */
-
+	/* keys are released */
+	memset(ScanCodeState, 0, sizeof(ScanCodeState));
 
 	/* Reset our keyboard states and clear key state table */
 	Keyboard.BufferHead = Keyboard.BufferTail = 0;
@@ -1757,7 +1756,7 @@ static int IKBD_CheckPressedKey(void)
 {
 	unsigned int	i;
 
-	for (i=0 ; i<sizeof(ScanCodeState) ; i++ )
+	for (i=0 ; i<ARRAY_SIZE(ScanCodeState) ; i++ )
 		if ( ScanCodeState[ i ] )
 			return i;
 
diff --git a/src/includes/ikbd.h b/src/includes/ikbd.h
index e4bf6898..4a3c5d66 100644
--- a/src/includes/ikbd.h
+++ b/src/includes/ikbd.h
@@ -40,12 +40,12 @@ typedef struct {
 } KEYBOARD_PROCESSOR;
 
 /* Keyboard state */
-#define KBD_MAX_SCANCODE          0x72
+#define KBD_SCANCODES             128     /* size of ikbd::ScanCodeState[] & keymap::KeyStates[] */
 #define SIZE_KEYBOARD_BUFFER      1024    /* Allow this many bytes to be stored in buffer (waiting to send to ACIA) */
 #define KEYBOARD_BUFFER_MASK      (SIZE_KEYBOARD_BUFFER-1)
 #define SIZE_KEYBOARDINPUT_BUFFER 8
 typedef struct {
-  Uint8 KeyStates[KBD_MAX_SCANCODE + 1];	/* State of ST keys, TRUE is down */
+  Uint8 KeyStates[KBD_SCANCODES];		/* State of ST keys, TRUE is down */
 
   Uint8 Buffer[SIZE_KEYBOARD_BUFFER];		/* Keyboard output buffer */
   int BufferHead,BufferTail;			/* Pointers into above buffer */
diff --git a/src/keymap.c b/src/keymap.c
index 7089aab5..2a2f3ebb 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -23,7 +23,7 @@ const char Keymap_fileid[] = "Hatari keymap.c";
 
 
 /* Table for loaded keys: */
-static int LoadedKeymap[KBD_MAX_SCANCODE][2];
+static int LoadedKeymap[KBD_SCANCODES][2];
 
 /* List of ST scan codes to NOT de-bounce when running in maximum speed */
 static const char DebounceExtendedKeys[] =
@@ -406,7 +406,7 @@ static char Keymap_RemapKeyToSTScanCode(SDL_keysym* pKeySym)
 	if (ConfigureParams.Keyboard.nKeymapType == KEYMAP_LOADED)
 	{
 		int i;
-		for (i = 0; i < KBD_MAX_SCANCODE && LoadedKeymap[i][1] != 0; i++)
+		for (i = 0; i < KBD_SCANCODES && LoadedKeymap[i][1] != 0; i++)
 		{
 			if (pKeySym->sym == (SDLKey)LoadedKeymap[i][0])
 				return LoadedKeymap[i][1];
@@ -449,7 +449,7 @@ void Keymap_LoadRemapFile(char *pszFileName)
 		return;
 	}
 
-	while (!feof(in) && idx < KBD_MAX_SCANCODE)
+	while (!feof(in) && idx < KBD_SCANCODES)
 	{
 		/* Read line from file */
 		if (fgets(szString, sizeof(szString), in) == NULL)
@@ -484,7 +484,7 @@ void Keymap_LoadRemapFile(char *pszFileName)
 				continue;
 			STScanCode = atoi(p);
 			/* Store into remap table, check both value within range */
-			if (STScanCode > 0 && STScanCode <= KBD_MAX_SCANCODE
+			if (STScanCode > 0 && STScanCode < KBD_SCANCODES
 			    && PCKeyCode >= 8)
 			{
 				LOG_TRACE(TRACE_KEYMAP,
@@ -497,8 +497,8 @@ void Keymap_LoadRemapFile(char *pszFileName)
 			else
 			{
 				Log_Printf(LOG_WARN, "Could not parse keymap file:"
-				           " '%s' (%d >= 8), '%s' (0 > %d <= %d)\n",
-					   szString, PCKeyCode, p, STScanCode, KBD_MAX_SCANCODE);
+				           " '%s' (%d >= 8), '%s' (0 > %d < %d)\n",
+					   szString, PCKeyCode, p, STScanCode, KBD_SCANCODES);
 			}
 		}
 	}
@@ -556,7 +556,7 @@ void Keymap_DebounceAllKeys(void)
 	}
 
 	/* Now run through each key looking for ones held down */
-	for (nScanCode = 1; nScanCode <= KBD_MAX_SCANCODE; nScanCode++)
+	for (nScanCode = 1; nScanCode < KBD_SCANCODES; nScanCode++)
 	{
 		/* Is key held? */
 		if (Keyboard.KeyStates[nScanCode])
-- 
2.30.2


--------------3845FE7660BCFA6A026301C9--



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