atari:gem: Generate a fake message to mark regularly when to stop processing events. SDL-1.2
authorPatrice Mandin <patmandin@gmail.com>
Thu, 11 Sep 2014 21:59:25 +0200
branchSDL-1.2
changeset 9132b37013ec80de
parent 9131 8b9217e06b02
child 9308 bda7de312fbd
atari:gem: Generate a fake message to mark regularly when to stop processing events.
src/video/gem/SDL_gemevents.c
     1.1 --- a/src/video/gem/SDL_gemevents.c	Thu Sep 11 21:53:06 2014 +0200
     1.2 +++ b/src/video/gem/SDL_gemevents.c	Thu Sep 11 21:59:25 2014 +0200
     1.3 @@ -47,6 +47,8 @@
     1.4  
     1.5  #define KEY_PRESS_DURATION 100
     1.6  
     1.7 +#define MSG_SDL_ID	(('S'<<8)|'D')
     1.8 +
     1.9  /* Variables */
    1.10  
    1.11  static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    1.12 @@ -54,10 +56,11 @@
    1.13  static Uint32 keyboard_ticks[ATARIBIOS_MAXKEYS];
    1.14  
    1.15  static short prevmx=0,prevmy=0,prevmb=0;
    1.16 +static short dummy_msgbuf[8] = {MSG_SDL_ID,0,0,0, 0,0,0,0};
    1.17  
    1.18  /* Functions prototypes */
    1.19  
    1.20 -static int do_messages(_THIS, short *message);
    1.21 +static int do_messages(_THIS, short *message, short latest_msg_id);
    1.22  static void do_keyboard(short kc, Uint32 tick);
    1.23  static void do_keyboard_special(short ks, Uint32 tick);
    1.24  static void do_mouse_motion(_THIS, short mx, short my);
    1.25 @@ -82,35 +85,52 @@
    1.26  void GEM_PumpEvents(_THIS)
    1.27  {
    1.28  	short prevkc=0, mousex, mousey, mouseb, kstate;
    1.29 -	int i;
    1.30 +	int i, quit = 0;
    1.31  	SDL_keysym keysym;
    1.32  	Uint32 cur_tick;
    1.33 +	static Uint32 prev_now = 0, prev_msg = 0;
    1.34 +	static short latest_msg_id = 0;
    1.35  
    1.36  	cur_tick = SDL_GetTicks();
    1.37 +	if (prev_now == cur_tick) {
    1.38 +		return;
    1.39 +	}
    1.40 +	prev_now = cur_tick;
    1.41 +
    1.42 +	SDL_AtariMint_BackgroundTasks();
    1.43  	clearKeyboardState(cur_tick);
    1.44  
    1.45 -	for (;;)
    1.46 -	{
    1.47 -		int quit, resultat;
    1.48 +	if (prev_msg) {
    1.49 +		/* Wait at least 20ms before each event processing loop */
    1.50 +		if (cur_tick-prev_msg < 20) {
    1.51 +			return;
    1.52 +		}
    1.53 +	}
    1.54 +	prev_msg = cur_tick;
    1.55 +
    1.56 +	dummy_msgbuf[1] = ++latest_msg_id;
    1.57 +	if (appl_write(GEM_ap_id, sizeof(dummy_msgbuf), dummy_msgbuf) == 0) {
    1.58 +		/* If it fails, wait for previous id */
    1.59 +		--latest_msg_id;
    1.60 +	}
    1.61 +
    1.62 +	while (!quit) {
    1.63 +		int resultat;
    1.64  		short buffer[8], kc, dummy;
    1.65  
    1.66 -		quit = 0;
    1.67 -
    1.68 -		SDL_AtariMint_BackgroundTasks();
    1.69 -
    1.70  		resultat = evnt_multi(
    1.71  			MU_MESAG|MU_TIMER|MU_KEYBD,
    1.72  			0,0,0,
    1.73  			0,0,0,0,0,
    1.74  			0,0,0,0,0,
    1.75  			buffer,
    1.76 -			10,
    1.77 +			1000,
    1.78  			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
    1.79  		);
    1.80  
    1.81  		/* Message event ? */
    1.82  		if (resultat & MU_MESAG)
    1.83 -			quit = do_messages(this, buffer);
    1.84 +			quit = do_messages(this, buffer, latest_msg_id);
    1.85  
    1.86  		/* Keyboard event ? */
    1.87  		if (resultat & MU_KEYBD) {
    1.88 @@ -118,15 +138,13 @@
    1.89  			if (prevkc != kc) {
    1.90  				do_keyboard(kc, cur_tick);
    1.91  				prevkc = kc;
    1.92 -			} else {
    1.93 -				/* Avoid looping, if repeating same key */
    1.94 -				quit = 1;
    1.95  			}
    1.96  		}
    1.97  
    1.98 -		/* Timer event ? */
    1.99 -		if ((resultat & MU_TIMER) || quit)
   1.100 -			break;
   1.101 +		/* Timer event ? Just used as a safeguard */
   1.102 +		if (resultat & MU_TIMER) {
   1.103 +			quit = 1;
   1.104 +		}
   1.105  	}
   1.106  
   1.107  	GEM_CheckMouseMode(this);
   1.108 @@ -167,13 +185,16 @@
   1.109  	}
   1.110  }
   1.111  
   1.112 -static int do_messages(_THIS, short *message)
   1.113 +static int do_messages(_THIS, short *message, short latest_msg_id)
   1.114  {
   1.115  	int quit;
   1.116  	short x2,y2,w2,h2;
   1.117  
   1.118  	quit = 0;
   1.119  	switch (message[0]) {
   1.120 +		case MSG_SDL_ID:
   1.121 +			quit=(message[1] == latest_msg_id);
   1.122 +			break;
   1.123  		case WM_CLOSED:
   1.124  		case AP_TERM:    
   1.125  			SDL_PrivateQuit();