src/video/gem/SDL_gemevents.c
branchSDL-1.2
changeset 9042 89d54976ff74
parent 9018 4a9c67d11c9f
child 9130 347df449e563
equal deleted inserted replaced
9040:916c837d202f 9042:89d54976ff74
    30  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    30  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    31  */
    31  */
    32 
    32 
    33 #include <gem.h>
    33 #include <gem.h>
    34 
    34 
       
    35 #include "SDL_timer.h"
    35 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "../../events/SDL_events_c.h"
    37 #include "SDL_gemvideo.h"
    38 #include "SDL_gemvideo.h"
    38 #include "SDL_gemevents_c.h"
    39 #include "SDL_gemevents_c.h"
    39 #include "SDL_gemmouse_c.h"
    40 #include "SDL_gemmouse_c.h"
    40 #include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    41 #include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    41 #include "../ataricommon/SDL_atarievents_c.h"
    42 #include "../ataricommon/SDL_atarievents_c.h"
    42 #include "../ataricommon/SDL_xbiosevents_c.h"
    43 #include "../ataricommon/SDL_xbiosevents_c.h"
    43 #include "../ataricommon/SDL_ataridevmouse_c.h"
    44 #include "../ataricommon/SDL_ataridevmouse_c.h"
    44 
    45 
       
    46 /* Duration after which we consider key released */
       
    47 
       
    48 #define KEY_PRESS_DURATION 100
       
    49 
    45 /* Variables */
    50 /* Variables */
    46 
    51 
    47 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    52 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    48 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    53 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
       
    54 static Uint32 keyboard_ticks[ATARIBIOS_MAXKEYS];
    49 
    55 
    50 static short prevmx=0,prevmy=0,prevmb=0;
    56 static short prevmx=0,prevmy=0,prevmb=0;
    51 
    57 
    52 /* Functions prototypes */
    58 /* Functions prototypes */
    53 
    59 
    54 static int do_messages(_THIS, short *message);
    60 static int do_messages(_THIS, short *message);
    55 static void do_keyboard(short kc);
    61 static void do_keyboard(short kc, Uint32 tick);
    56 static void do_keyboard_special(short ks);
    62 static void do_keyboard_special(short ks, Uint32 tick);
    57 static void do_mouse_motion(_THIS, short mx, short my);
    63 static void do_mouse_motion(_THIS, short mx, short my);
    58 static void do_mouse_buttons(_THIS, short mb);
    64 static void do_mouse_buttons(_THIS, short mb);
    59 static int mouse_in_work_area(int winhandle, short mx, short my);
    65 static int mouse_in_work_area(int winhandle, short mx, short my);
       
    66 static void clearKeyboardState(Uint32 tick);
    60 
    67 
    61 /* Functions */
    68 /* Functions */
    62 
    69 
    63 void GEM_InitOSKeymap(_THIS)
    70 void GEM_InitOSKeymap(_THIS)
    64 {
    71 {
    65 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    72 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    66 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    73 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
       
    74 	SDL_memset(keyboard_ticks, 0, sizeof(keyboard_ticks));
    67 
    75 
    68 	/* Mouse init */
    76 	/* Mouse init */
    69 	GEM_mouse_relative = SDL_FALSE;
    77 	GEM_mouse_relative = SDL_FALSE;
    70 
    78 
    71 	SDL_Atari_InitInternalKeymap(this);
    79 	SDL_Atari_InitInternalKeymap(this);
    74 void GEM_PumpEvents(_THIS)
    82 void GEM_PumpEvents(_THIS)
    75 {
    83 {
    76 	short prevkc=0, mousex, mousey, mouseb, kstate;
    84 	short prevkc=0, mousex, mousey, mouseb, kstate;
    77 	int i;
    85 	int i;
    78 	SDL_keysym keysym;
    86 	SDL_keysym keysym;
    79 
    87 	Uint32 cur_tick;
    80 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    88 
       
    89 	cur_tick = SDL_GetTicks();
       
    90 	clearKeyboardState(cur_tick);
    81 
    91 
    82 	for (;;)
    92 	for (;;)
    83 	{
    93 	{
    84 		int quit, resultat;
    94 		int quit, resultat;
    85 		short buffer[8], kc, dummy;
    95 		short buffer[8], kc, dummy;
   102 		if (resultat & MU_MESAG)
   112 		if (resultat & MU_MESAG)
   103 			quit = do_messages(this, buffer);
   113 			quit = do_messages(this, buffer);
   104 
   114 
   105 		/* Keyboard event ? */
   115 		/* Keyboard event ? */
   106 		if (resultat & MU_KEYBD) {
   116 		if (resultat & MU_KEYBD) {
   107 			do_keyboard_special(kstate);
   117 			do_keyboard_special(kstate, cur_tick);
   108 			if (prevkc != kc) {
   118 			if (prevkc != kc) {
   109 				do_keyboard(kc);
   119 				do_keyboard(kc, cur_tick);
   110 				prevkc = kc;
   120 				prevkc = kc;
   111 			} else {
   121 			} else {
   112 				/* Avoid looping, if repeating same key */
   122 				/* Avoid looping, if repeating same key */
   113 				quit = 1;
   123 				quit = 1;
   114 			}
   124 			}
   119 			break;
   129 			break;
   120 	}
   130 	}
   121 
   131 
   122 	/* Update mouse state */
   132 	/* Update mouse state */
   123 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   133 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   124 	do_keyboard_special(kstate);
   134 	do_keyboard_special(kstate, cur_tick);
   125 	do_mouse_motion(this, mousex, mousey);
   135 	do_mouse_motion(this, mousex, mousey);
   126 	do_mouse_buttons(this, mouseb);
   136 	do_mouse_buttons(this, mouseb);
   127 
   137 
   128 	/* Now generate keyboard events */
   138 	/* Now generate keyboard events */
   129 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   139 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   257 	}
   267 	}
   258 	
   268 	
   259 	return quit;
   269 	return quit;
   260 }
   270 }
   261 
   271 
   262 static void do_keyboard(short kc)
   272 static void do_keyboard(short kc, Uint32 tick)
   263 {
   273 {
   264 	int scancode;
   274 	int scancode;
   265 
   275 
   266 	if (kc) {
   276 	if (kc) {
   267 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   277 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   268 		gem_currentkeyboard[scancode]=0xFF;
   278 		gem_currentkeyboard[scancode]=0xFF;
   269 	}
   279 		keyboard_ticks[scancode]=tick;
   270 }
   280 	}
   271 
   281 }
   272 static void do_keyboard_special(short ks)
   282 
   273 {
   283 static void do_keyboard_special(short ks, Uint32 tick)
       
   284 {
       
   285 	int scancode=0;
       
   286 
   274 	/* Read special keys */
   287 	/* Read special keys */
   275 	if (ks & K_RSHIFT)
   288 	if (ks & K_RSHIFT)
   276 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   289 		scancode=SCANCODE_RIGHTSHIFT;
   277 	if (ks & K_LSHIFT)
   290 	if (ks & K_LSHIFT)
   278 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   291 		scancode=SCANCODE_LEFTSHIFT;
   279 	if (ks & K_CTRL)
   292 	if (ks & K_CTRL)
   280 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   293 		scancode=SCANCODE_LEFTCONTROL;
   281 	if (ks & K_ALT)
   294 	if (ks & K_ALT)
   282 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   295 		scancode=SCANCODE_LEFTALT;
       
   296 
       
   297 	if (scancode) {
       
   298 		gem_currentkeyboard[scancode]=0xFF;
       
   299 		keyboard_ticks[scancode]=tick;
       
   300 	}
   283 }
   301 }
   284 
   302 
   285 static void do_mouse_motion(_THIS, short mx, short my)
   303 static void do_mouse_motion(_THIS, short mx, short my)
   286 {
   304 {
   287 	short x2, y2, w2, h2;
   305 	short x2, y2, w2, h2;
   385 
   403 
   386 	}
   404 	}
   387 
   405 
   388 	return 0;
   406 	return 0;
   389 }
   407 }
       
   408 
       
   409 /* Clear key state for which we did not receive events for a while */
       
   410 
       
   411 static void clearKeyboardState(Uint32 tick)
       
   412 {
       
   413 	int i;
       
   414 
       
   415 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
       
   416 		if (keyboard_ticks[i]) {
       
   417 			if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
       
   418 				gem_currentkeyboard[i]=0;
       
   419 				keyboard_ticks[i]=0;
       
   420 			}
       
   421 		}
       
   422 	}
       
   423 }