Avoid generating multiple key press/release messages for the same key
authorPatrice Mandin <patmandin@gmail.com>
Wed, 29 Jun 2005 20:32:46 +0000
changeset 1082 48436ffdf677
parent 1081 369dcdb52d70
child 1083 9f8aa754622b
Avoid generating multiple key press/release messages for the same key
src/video/ataricommon/SDL_ikbdevents.c
src/video/ataricommon/SDL_ikbdinterrupt.S
--- a/src/video/ataricommon/SDL_ikbdevents.c	Wed Jun 29 16:18:46 2005 +0000
+++ b/src/video/ataricommon/SDL_ikbdevents.c	Wed Jun 29 20:32:46 2005 +0000
@@ -54,11 +54,11 @@
 	K_INSERT
 };
 
-/* To save state of keyboard */
 #define ATARIBIOS_MAXKEYS 128
 
-static unsigned char ikbd_previouskeyboard[ATARIBIOS_MAXKEYS];
-static Uint16 atari_prevmouseb;	/* buttons */
+#define KEY_PRESSED		0xff
+#define KEY_UNDEFINED	0x80
+#define KEY_RELEASED	0x00
 
 /* The translation tables from a console scancode to a SDL keysym */
 #define KT_NOCHANGE -1
@@ -69,7 +69,8 @@
 	KT_CAPS=2
 };
 
-static int caps_state;
+static Uint16 atari_prevmouseb;	/* save state of mouse buttons */
+static int caps_state;			/* caps lock state */
 _KEYTAB *curtables;
 static unsigned char *tab_unshift, *tab_shift, *tab_caps;
 static SDLKey keymap[ATARIBIOS_MAXKEYS];
@@ -80,8 +81,7 @@
 {
 	int i;
 
-	memset(SDL_AtariIkbd_keyboard, 0, ATARIBIOS_MAXKEYS);
-	memset(ikbd_previouskeyboard, 0, ATARIBIOS_MAXKEYS);
+	memset(SDL_AtariIkbd_keyboard, KEY_UNDEFINED, ATARIBIOS_MAXKEYS);
 
 	/* Initialize keymap */
 	for ( i=0; i<sizeof(keymap); i++ )
@@ -152,30 +152,36 @@
 	/*--- Send keyboard events ---*/
 
 	/* Update caps lock state */
-	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK] && !ikbd_previouskeyboard[SCANCODE_CAPSLOCK])
+	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK]==KEY_PRESSED) {
 		caps_state ^= 1;
+	}
 
 	/* Choose the translation table */
 	specialkeys=KT_UNSHIFT;
-	if (SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT] || SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT])
+	if ((SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT]==KEY_PRESSED)
+		|| (SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT]==KEY_PRESSED))
+	{
 		specialkeys = KT_SHIFT;
-	if (caps_state)
+	}
+	if (caps_state) {
 		specialkeys = KT_CAPS;
+	}
 
 	/* Now generate events */
 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
 		/* Key pressed ? */
-		if (SDL_AtariIkbd_keyboard[i] && !ikbd_previouskeyboard[i])
+		if (SDL_AtariIkbd_keyboard[i]==KEY_PRESSED) {
 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, specialkeys, &keysym));
+			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
+		}
 			
-		/* Key unpressed ? */
-		if (ikbd_previouskeyboard[i] && !SDL_AtariIkbd_keyboard[i])
+		/* Key released ? */
+		if (SDL_AtariIkbd_keyboard[i]==KEY_RELEASED) {
 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, specialkeys, &keysym));
+			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
+		}
 	}
 
-	/* Will be previous table */
-	memcpy(ikbd_previouskeyboard, SDL_AtariIkbd_keyboard, ATARIBIOS_MAXKEYS);
-
 	/*--- Send mouse events ---*/
 
 	/* Mouse motion ? */
@@ -238,4 +244,3 @@
 {
 	Supexec(SDL_AtariIkbdUninstall);
 }
-
--- a/src/video/ataricommon/SDL_ikbdinterrupt.S	Wed Jun 29 16:18:46 2005 +0000
+++ b/src/video/ataricommon/SDL_ikbdinterrupt.S	Wed Jun 29 20:32:46 2005 +0000
@@ -49,7 +49,7 @@
 _SDL_AtariIkbdInstall:
 	moveml	d0-d1/a0-a1,sp@-
 
-	| Init interrupts
+	| Disable interrupts
 
 	movew	#0x2700,sr
 
@@ -57,9 +57,9 @@
 
 	lea	0xfffffa00:w,a0
 	btst	#6,a0@(0x09)
-	sne	ikbd_ierb
+	sne		ikbd_ierb
 	btst	#6,a0@(0x15)
-	sne	ikbd_imrb
+	sne		ikbd_imrb
 
 	| Set our routine
 
@@ -68,8 +68,12 @@
 	bset	#6,0xfffffa09:w	| IERB
 	bset	#6,0xfffffa15:w	| IMRB
 
+	| Set mouse relative mode
+
 	moveb	#8,0xfffffc02:w
 
+	| Reenable interrupts
+
 	movew	#0x2300,sr
 
 	| Interrupts done
@@ -84,7 +88,7 @@
 _SDL_AtariIkbdUninstall:
 	movel	a0,sp@-
 
-	| Stop interrupt
+	| Disable interrupts
 
 	movew	#0x2700,sr
 
@@ -94,13 +98,13 @@
 
 	bclr	#6,a0@(0x09)
 	tstb	ikbd_ierb
-	beq	ikbd_restoreierb
+	beqs	ikbd_restoreierb
 	bset	#6,a0@(0x09)
 ikbd_restoreierb:
 
 	bclr	#6,a0@(0x15)
 	tstb	ikbd_imrb
-	beq	ikbd_restoreimrb
+	beqs	ikbd_restoreimrb
 	bset	#6,a0@(0x15)
 ikbd_restoreimrb:
 
@@ -111,11 +115,13 @@
 	lea	0xfffffc00:w,a0
 ikbd_videbuffer:
 	btst	#0,a0@
-	beq	ikbd_finbuffer
+	beqs	ikbd_finbuffer
 	tstb	a0@(0x02)
-	bra	ikbd_videbuffer
+	bras	ikbd_videbuffer
 ikbd_finbuffer:
 
+	| Reenable interrupts
+
 	movew	#0x2300,sr
 
 	movel	sp@+,a0
@@ -136,7 +142,6 @@
 	.comm	old_ikbd,4*1
 ikbd:
 	| Check if source is IKBD or MIDI
-	
 	btst	#0,0xfffffc00.w
 	beqs	ikbd_oldmidi
 
@@ -155,6 +160,8 @@
 	cmpb	#0xfc,d0
 	bpls	ikbd_no_mouse
 
+	| Mouse packet, byte #1
+
 ikbd_yes_mouse:
 	andw	#3,d0
 	movew	d0,_SDL_AtariIkbd_mouseb
@@ -162,16 +169,20 @@
 	movel	#ikbd_mousex,0x118:w
 	bras	ikbd_endit_stack
 
+	| Joystick packet, byte #1
+
 ikbd_yes_joystick:
 	movel	#ikbd_joystick,0x118:w
 	bras	ikbd_endit_stack
 
+	| Keyboard press/release
+
 ikbd_no_mouse:
 	moveb	d0,d1
-	lea	_SDL_AtariIkbd_keyboard,a0
-	andl	#0x7f,d1
-	tas	d0
-	spl	a0@(0,d1:w)
+	lea		_SDL_AtariIkbd_keyboard,a0
+	andw	#0x7f,d1
+	tas		d0
+	spl		a0@(0,d1:w)
 
 	| End of interrupt
 
@@ -187,9 +198,11 @@
 	movel	old_ikbd,sp@-
 	rts
 
+	| Mouse packet, byte #2
+
 ikbd_mousex:
+
 	| Check if source is IKBD or MIDI
-	
 	btst	#0,0xfffffc00.w
 	beqs	ikbd_oldmidi
 
@@ -204,9 +217,11 @@
 	movel	#ikbd_mousey,0x118:w
 	bras	ikbd_endit
 
+	| Mouse packet, byte #3
+
 ikbd_mousey:
+
 	| Check if source is IKBD or MIDI
-	
 	btst	#0,0xfffffc00.w
 	beqs	ikbd_oldmidi
 
@@ -221,9 +236,11 @@
 	movel	#ikbd,0x118:w
 	bras	ikbd_endit
 
+	| Joystick packet, byte #2
+
 ikbd_joystick:
+
 	| Check if source is IKBD or MIDI
-	
 	btst	#0,0xfffffc00.w
 	beqs	ikbd_oldmidi