atari:gem: Simulate key released after X ms elapsed since last key pressed. SDL-1.2
authorPatrice Mandin <patmandin@gmail.com>
Sat, 02 Aug 2014 11:16:16 +0200
branchSDL-1.2
changeset 904289d54976ff74
parent 9040 916c837d202f
child 9043 1a6ca0c7977e
atari:gem: Simulate key released after X ms elapsed since last key pressed.
src/video/gem/SDL_gemevents.c
     1.1 --- a/src/video/gem/SDL_gemevents.c	Thu Jul 31 15:57:38 2014 +0200
     1.2 +++ b/src/video/gem/SDL_gemevents.c	Sat Aug 02 11:16:16 2014 +0200
     1.3 @@ -32,6 +32,7 @@
     1.4  
     1.5  #include <gem.h>
     1.6  
     1.7 +#include "SDL_timer.h"
     1.8  #include "../../events/SDL_sysevents.h"
     1.9  #include "../../events/SDL_events_c.h"
    1.10  #include "SDL_gemvideo.h"
    1.11 @@ -42,21 +43,27 @@
    1.12  #include "../ataricommon/SDL_xbiosevents_c.h"
    1.13  #include "../ataricommon/SDL_ataridevmouse_c.h"
    1.14  
    1.15 +/* Duration after which we consider key released */
    1.16 +
    1.17 +#define KEY_PRESS_DURATION 100
    1.18 +
    1.19  /* Variables */
    1.20  
    1.21  static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    1.22  static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    1.23 +static Uint32 keyboard_ticks[ATARIBIOS_MAXKEYS];
    1.24  
    1.25  static short prevmx=0,prevmy=0,prevmb=0;
    1.26  
    1.27  /* Functions prototypes */
    1.28  
    1.29  static int do_messages(_THIS, short *message);
    1.30 -static void do_keyboard(short kc);
    1.31 -static void do_keyboard_special(short ks);
    1.32 +static void do_keyboard(short kc, Uint32 tick);
    1.33 +static void do_keyboard_special(short ks, Uint32 tick);
    1.34  static void do_mouse_motion(_THIS, short mx, short my);
    1.35  static void do_mouse_buttons(_THIS, short mb);
    1.36  static int mouse_in_work_area(int winhandle, short mx, short my);
    1.37 +static void clearKeyboardState(Uint32 tick);
    1.38  
    1.39  /* Functions */
    1.40  
    1.41 @@ -64,6 +71,7 @@
    1.42  {
    1.43  	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    1.44  	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    1.45 +	SDL_memset(keyboard_ticks, 0, sizeof(keyboard_ticks));
    1.46  
    1.47  	/* Mouse init */
    1.48  	GEM_mouse_relative = SDL_FALSE;
    1.49 @@ -76,8 +84,10 @@
    1.50  	short prevkc=0, mousex, mousey, mouseb, kstate;
    1.51  	int i;
    1.52  	SDL_keysym keysym;
    1.53 +	Uint32 cur_tick;
    1.54  
    1.55 -	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    1.56 +	cur_tick = SDL_GetTicks();
    1.57 +	clearKeyboardState(cur_tick);
    1.58  
    1.59  	for (;;)
    1.60  	{
    1.61 @@ -104,9 +114,9 @@
    1.62  
    1.63  		/* Keyboard event ? */
    1.64  		if (resultat & MU_KEYBD) {
    1.65 -			do_keyboard_special(kstate);
    1.66 +			do_keyboard_special(kstate, cur_tick);
    1.67  			if (prevkc != kc) {
    1.68 -				do_keyboard(kc);
    1.69 +				do_keyboard(kc, cur_tick);
    1.70  				prevkc = kc;
    1.71  			} else {
    1.72  				/* Avoid looping, if repeating same key */
    1.73 @@ -121,7 +131,7 @@
    1.74  
    1.75  	/* Update mouse state */
    1.76  	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
    1.77 -	do_keyboard_special(kstate);
    1.78 +	do_keyboard_special(kstate, cur_tick);
    1.79  	do_mouse_motion(this, mousex, mousey);
    1.80  	do_mouse_buttons(this, mouseb);
    1.81  
    1.82 @@ -259,27 +269,35 @@
    1.83  	return quit;
    1.84  }
    1.85  
    1.86 -static void do_keyboard(short kc)
    1.87 +static void do_keyboard(short kc, Uint32 tick)
    1.88  {
    1.89  	int scancode;
    1.90  
    1.91  	if (kc) {
    1.92  		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
    1.93  		gem_currentkeyboard[scancode]=0xFF;
    1.94 +		keyboard_ticks[scancode]=tick;
    1.95  	}
    1.96  }
    1.97  
    1.98 -static void do_keyboard_special(short ks)
    1.99 +static void do_keyboard_special(short ks, Uint32 tick)
   1.100  {
   1.101 +	int scancode=0;
   1.102 +
   1.103  	/* Read special keys */
   1.104  	if (ks & K_RSHIFT)
   1.105 -		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   1.106 +		scancode=SCANCODE_RIGHTSHIFT;
   1.107  	if (ks & K_LSHIFT)
   1.108 -		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   1.109 +		scancode=SCANCODE_LEFTSHIFT;
   1.110  	if (ks & K_CTRL)
   1.111 -		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   1.112 +		scancode=SCANCODE_LEFTCONTROL;
   1.113  	if (ks & K_ALT)
   1.114 -		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   1.115 +		scancode=SCANCODE_LEFTALT;
   1.116 +
   1.117 +	if (scancode) {
   1.118 +		gem_currentkeyboard[scancode]=0xFF;
   1.119 +		keyboard_ticks[scancode]=tick;
   1.120 +	}
   1.121  }
   1.122  
   1.123  static void do_mouse_motion(_THIS, short mx, short my)
   1.124 @@ -387,3 +405,19 @@
   1.125  
   1.126  	return 0;
   1.127  }
   1.128 +
   1.129 +/* Clear key state for which we did not receive events for a while */
   1.130 +
   1.131 +static void clearKeyboardState(Uint32 tick)
   1.132 +{
   1.133 +	int i;
   1.134 +
   1.135 +	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   1.136 +		if (keyboard_ticks[i]) {
   1.137 +			if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
   1.138 +				gem_currentkeyboard[i]=0;
   1.139 +				keyboard_ticks[i]=0;
   1.140 +			}
   1.141 +		}
   1.142 +	}
   1.143 +}