src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Sat, 30 Sep 2017 22:06:30 +0200
branchSDL-1.2
changeset 11559 a330adcd0490
parent 11558 560392acace0
child 11560 c267eabdc97f
permissions -rw-r--r--
atari:gem: Align window content on 16 pixels boundary (faster for bitplanes modes). Make GEM_clear_rect public function to clear the visible extra space.
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@281
     4
slouken@281
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@281
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@281
     9
slouken@281
    10
    This library is distributed in the hope that it will be useful,
slouken@281
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@281
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@281
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@281
    18
slouken@281
    19
    Sam Lantinga
slouken@281
    20
    slouken@libsdl.org
slouken@281
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@281
    23
slouken@281
    24
/*
slouken@281
    25
 * GEM SDL video driver implementation
slouken@281
    26
 * inspired from the Dummy SDL driver
patmandin@11554
    27
 *
slouken@281
    28
 * Patrice Mandin
slouken@281
    29
 * and work from
slouken@281
    30
 * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
slouken@281
    31
 */
slouken@281
    32
slouken@281
    33
#include <gem.h>
slouken@281
    34
patmandin@9042
    35
#include "SDL_timer.h"
slouken@1361
    36
#include "../../events/SDL_sysevents.h"
slouken@1361
    37
#include "../../events/SDL_events_c.h"
slouken@281
    38
#include "SDL_gemvideo.h"
slouken@281
    39
#include "SDL_gemevents_c.h"
patmandin@5649
    40
#include "SDL_gemmouse_c.h"
patmandin@1412
    41
#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
patmandin@1412
    42
#include "../ataricommon/SDL_atarievents_c.h"
patmandin@1412
    43
#include "../ataricommon/SDL_xbiosevents_c.h"
patmandin@1420
    44
#include "../ataricommon/SDL_ataridevmouse_c.h"
slouken@281
    45
patmandin@9042
    46
/* Duration after which we consider key released */
patmandin@9042
    47
patmandin@9042
    48
#define KEY_PRESS_DURATION 100
patmandin@9042
    49
patmandin@9132
    50
#define MSG_SDL_ID	(('S'<<8)|'D')
patmandin@9132
    51
slouken@281
    52
/* Variables */
slouken@281
    53
slouken@281
    54
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    55
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
patmandin@9042
    56
static Uint32 keyboard_ticks[ATARIBIOS_MAXKEYS];
slouken@281
    57
patmandin@6647
    58
static short prevmx=0,prevmy=0,prevmb=0;
patmandin@9132
    59
static short dummy_msgbuf[8] = {MSG_SDL_ID,0,0,0, 0,0,0,0};
patmandin@6647
    60
slouken@281
    61
/* Functions prototypes */
slouken@281
    62
patmandin@9132
    63
static int do_messages(_THIS, short *message, short latest_msg_id);
patmandin@9042
    64
static void do_keyboard(short kc, Uint32 tick);
patmandin@9042
    65
static void do_keyboard_special(short ks, Uint32 tick);
patmandin@6583
    66
static void do_mouse_motion(_THIS, short mx, short my);
patmandin@6583
    67
static void do_mouse_buttons(_THIS, short mb);
patmandin@6585
    68
static int mouse_in_work_area(int winhandle, short mx, short my);
patmandin@9042
    69
static void clearKeyboardState(Uint32 tick);
slouken@281
    70
slouken@281
    71
/* Functions */
slouken@281
    72
slouken@281
    73
void GEM_InitOSKeymap(_THIS)
slouken@281
    74
{
slouken@1336
    75
	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
slouken@1336
    76
	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
patmandin@9042
    77
	SDL_memset(keyboard_ticks, 0, sizeof(keyboard_ticks));
slouken@281
    78
slouken@281
    79
	/* Mouse init */
slouken@281
    80
	GEM_mouse_relative = SDL_FALSE;
patmandin@3861
    81
patmandin@3861
    82
	SDL_Atari_InitInternalKeymap(this);
slouken@281
    83
}
slouken@281
    84
slouken@281
    85
void GEM_PumpEvents(_THIS)
slouken@281
    86
{
patmandin@6647
    87
	short prevkc=0, mousex, mousey, mouseb, kstate;
patmandin@9132
    88
	int i, quit = 0;
patmandin@6647
    89
	SDL_keysym keysym;
patmandin@9042
    90
	Uint32 cur_tick;
patmandin@9132
    91
	static Uint32 prev_now = 0, prev_msg = 0;
patmandin@9132
    92
	static short latest_msg_id = 0;
slouken@281
    93
patmandin@9042
    94
	cur_tick = SDL_GetTicks();
patmandin@9132
    95
	if (prev_now == cur_tick) {
patmandin@9132
    96
		return;
patmandin@9132
    97
	}
patmandin@9132
    98
	prev_now = cur_tick;
patmandin@9132
    99
patmandin@9132
   100
	SDL_AtariMint_BackgroundTasks();
patmandin@9042
   101
	clearKeyboardState(cur_tick);
patmandin@4564
   102
patmandin@9132
   103
	if (prev_msg) {
patmandin@9132
   104
		/* Wait at least 20ms before each event processing loop */
patmandin@9132
   105
		if (cur_tick-prev_msg < 20) {
patmandin@9132
   106
			return;
patmandin@9132
   107
		}
patmandin@9132
   108
	}
patmandin@9132
   109
	prev_msg = cur_tick;
patmandin@9132
   110
patmandin@9132
   111
	dummy_msgbuf[1] = ++latest_msg_id;
patmandin@9132
   112
	if (appl_write(GEM_ap_id, sizeof(dummy_msgbuf), dummy_msgbuf) == 0) {
patmandin@9132
   113
		/* If it fails, wait for previous id */
patmandin@9132
   114
		--latest_msg_id;
patmandin@9132
   115
	}
patmandin@9132
   116
patmandin@9132
   117
	while (!quit) {
patmandin@9132
   118
		int resultat;
patmandin@6647
   119
		short buffer[8], kc, dummy;
slouken@281
   120
slouken@281
   121
		resultat = evnt_multi(
patmandin@6649
   122
			MU_MESAG|MU_TIMER|MU_KEYBD,
patmandin@6649
   123
			0,0,0,
patmandin@6649
   124
			0,0,0,0,0,
patmandin@1089
   125
			0,0,0,0,0,
slouken@281
   126
			buffer,
patmandin@9132
   127
			1000,
patmandin@6649
   128
			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
slouken@281
   129
		);
slouken@281
   130
slouken@281
   131
		/* Message event ? */
slouken@281
   132
		if (resultat & MU_MESAG)
patmandin@9132
   133
			quit = do_messages(this, buffer, latest_msg_id);
slouken@281
   134
slouken@281
   135
		/* Keyboard event ? */
slouken@319
   136
		if (resultat & MU_KEYBD) {
patmandin@9042
   137
			do_keyboard_special(kstate, cur_tick);
patmandin@6582
   138
			if (prevkc != kc) {
patmandin@9042
   139
				do_keyboard(kc, cur_tick);
patmandin@6572
   140
				prevkc = kc;
slouken@319
   141
			}
slouken@319
   142
		}
slouken@281
   143
patmandin@9132
   144
		/* Timer event ? Just used as a safeguard */
patmandin@9132
   145
		if (resultat & MU_TIMER) {
patmandin@9132
   146
			quit = 1;
patmandin@9132
   147
		}
slouken@281
   148
	}
slouken@281
   149
patmandin@9130
   150
	GEM_CheckMouseMode(this);
patmandin@9130
   151
patmandin@6649
   152
	/* Update mouse state */
patmandin@6649
   153
	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
patmandin@9042
   154
	do_keyboard_special(kstate, cur_tick);
patmandin@6649
   155
	do_mouse_motion(this, mousex, mousey);
patmandin@6649
   156
	do_mouse_buttons(this, mouseb);
patmandin@6649
   157
slouken@319
   158
	/* Now generate keyboard events */
slouken@281
   159
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   160
		/* Key pressed ? */
slouken@281
   161
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
patmandin@1209
   162
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@3861
   163
				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
patmandin@11554
   164
slouken@281
   165
		/* Key unpressed ? */
slouken@281
   166
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
patmandin@1209
   167
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@3861
   168
				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
slouken@281
   169
	}
slouken@281
   170
slouken@1336
   171
	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   172
patmandin@996
   173
	/* Refresh window name ? */
patmandin@996
   174
	if (GEM_refresh_name) {
patmandin@4059
   175
		const char *window_name =
patmandin@4059
   176
			(SDL_GetAppState() & SDL_APPACTIVE)
patmandin@4059
   177
			? GEM_title_name : GEM_icon_name;
patmandin@4059
   178
		if (window_name) {
patmandin@4059
   179
			wind_set(GEM_handle,WF_NAME,
patmandin@4059
   180
				(short)(((unsigned long)window_name)>>16),
patmandin@4059
   181
				(short)(((unsigned long)window_name) & 0xffff),
patmandin@4059
   182
				0,0);
patmandin@996
   183
		}
patmandin@996
   184
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   185
	}
slouken@281
   186
}
slouken@281
   187
patmandin@9132
   188
static int do_messages(_THIS, short *message, short latest_msg_id)
slouken@281
   189
{
patmandin@11559
   190
	int quit, update_work_area, align_work_area, sdl_resize;
slouken@281
   191
patmandin@11559
   192
	quit = update_work_area = align_work_area = sdl_resize = 0;
slouken@281
   193
	switch (message[0]) {
patmandin@9132
   194
		case MSG_SDL_ID:
patmandin@9132
   195
			quit=(message[1] == latest_msg_id);
patmandin@9132
   196
			break;
slouken@281
   197
		case WM_CLOSED:
patmandin@11554
   198
		case AP_TERM:
patmandin@5867
   199
			SDL_PrivateQuit();
slouken@281
   200
			quit=1;
slouken@281
   201
			break;
slouken@281
   202
		case WM_MOVED:
slouken@281
   203
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
patmandin@11559
   204
			update_work_area = align_work_area = 1;
slouken@281
   205
			break;
slouken@281
   206
		case WM_TOPPED:
slouken@281
   207
			wind_set(message[3],WF_TOP,message[4],0,0,0);
patmandin@1091
   208
			/* Continue with TOP event processing */
patmandin@1091
   209
		case WM_ONTOP:
slouken@281
   210
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
patmandin@1074
   211
			if (VDI_setpalette) {
patmandin@1074
   212
				VDI_setpalette(this, VDI_curpalette);
patmandin@1074
   213
			}
slouken@281
   214
			break;
slouken@281
   215
		case WM_REDRAW:
patmandin@964
   216
			if (!GEM_lock_redraw) {
patmandin@964
   217
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   218
			}
slouken@281
   219
			break;
slouken@281
   220
		case WM_ICONIFY:
slouken@281
   221
		case WM_ALLICONIFY:
patmandin@11555
   222
			wind_set (message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   223
			/* If we're active, make ourselves inactive */
slouken@281
   224
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   225
				/* Send an internal deactivate event */
patmandin@1088
   226
				SDL_PrivateAppActive(0, SDL_APPACTIVE);
slouken@281
   227
			}
patmandin@736
   228
			/* Update window title */
patmandin@736
   229
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@11554
   230
				wind_set(GEM_handle,WF_NAME,
patmandin@11554
   231
					(short)(((unsigned long)GEM_icon_name)>>16),
patmandin@11554
   232
					(short)(((unsigned long)GEM_icon_name) & 0xffff),
patmandin@11554
   233
					0,0);
patmandin@736
   234
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   235
			}
patmandin@11557
   236
			update_work_area = 1;
slouken@281
   237
			break;
slouken@281
   238
		case WM_UNICONIFY:
patmandin@11555
   239
			wind_set (message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   240
			/* If we're not active, make ourselves active */
slouken@281
   241
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   242
				/* Send an internal activate event */
slouken@281
   243
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   244
			}
patmandin@736
   245
			if (GEM_refresh_name && GEM_title_name) {
patmandin@11554
   246
				wind_set(GEM_handle,WF_NAME,
patmandin@11554
   247
					(short)(((unsigned long)GEM_title_name)>>16),
patmandin@11554
   248
					(short)(((unsigned long)GEM_title_name) & 0xffff),
patmandin@11554
   249
					0,0);
patmandin@736
   250
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   251
			}
patmandin@11559
   252
			update_work_area = align_work_area = 1;
slouken@281
   253
			break;
slouken@281
   254
		case WM_SIZED:
slouken@281
   255
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
patmandin@11559
   256
			update_work_area = align_work_area = sdl_resize = 1;
slouken@281
   257
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
patmandin@964
   258
			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
slouken@281
   259
			break;
slouken@281
   260
		case WM_FULLED:
slouken@281
   261
			{
slouken@281
   262
				short x,y,w,h;
slouken@281
   263
slouken@281
   264
				if (GEM_win_fulled) {
slouken@281
   265
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   266
					GEM_win_fulled = SDL_FALSE;
slouken@281
   267
				} else {
slouken@281
   268
					x = GEM_desk_x;
slouken@281
   269
					y = GEM_desk_y;
slouken@281
   270
					w = GEM_desk_w;
slouken@281
   271
					h = GEM_desk_h;
slouken@281
   272
					GEM_win_fulled = SDL_TRUE;
slouken@281
   273
				}
slouken@281
   274
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
patmandin@11559
   275
				update_work_area = align_work_area = sdl_resize = 1;
patmandin@964
   276
				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
slouken@281
   277
			}
slouken@281
   278
			break;
slouken@281
   279
		case WM_BOTTOMED:
patmandin@1091
   280
			wind_set(message[3],WF_BOTTOM,0,0,0,0);
patmandin@1091
   281
			/* Continue with BOTTOM event processing */
slouken@281
   282
		case WM_UNTOPPED:
slouken@281
   283
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
patmandin@1074
   284
			if (VDI_setpalette) {
patmandin@1074
   285
				VDI_setpalette(this, VDI_oldpalette);
patmandin@1074
   286
			}
slouken@281
   287
			break;
slouken@281
   288
	}
patmandin@11554
   289
patmandin@11557
   290
	if (update_work_area) {
patmandin@11557
   291
		wind_get (message[3], WF_WORKXYWH, &GEM_work_x, &GEM_work_y, &GEM_work_w, &GEM_work_h);
patmandin@11559
   292
patmandin@11559
   293
		/* Align work area on 16 pixels boundary (faster for bitplanes modes) */
patmandin@11559
   294
		if (align_work_area) {
patmandin@11559
   295
			int aligned_x;
patmandin@11559
   296
			short pxy[4];
patmandin@11559
   297
patmandin@11559
   298
			aligned_x = GEM_work_x;
patmandin@11559
   299
			if (aligned_x & 15) {
patmandin@11559
   300
				aligned_x = (aligned_x|15)+1;
patmandin@11559
   301
patmandin@11559
   302
				pxy[0] = GEM_work_x;
patmandin@11559
   303
				pxy[1] = GEM_work_y;
patmandin@11559
   304
				pxy[2] = aligned_x - 1;
patmandin@11559
   305
				pxy[3] = pxy[1] + GEM_work_h - 1;
patmandin@11559
   306
				GEM_clear_rect(this, pxy);
patmandin@11559
   307
			}
patmandin@11559
   308
			GEM_work_w -= (aligned_x - GEM_work_x);
patmandin@11559
   309
			GEM_work_x = aligned_x;
patmandin@11559
   310
		}
patmandin@11559
   311
patmandin@11557
   312
		if (sdl_resize) {
patmandin@11557
   313
			SDL_PrivateResize(GEM_work_w, GEM_work_h);
patmandin@11557
   314
		}
patmandin@11557
   315
	}
patmandin@11557
   316
slouken@281
   317
	return quit;
slouken@281
   318
}
slouken@281
   319
patmandin@9042
   320
static void do_keyboard(short kc, Uint32 tick)
slouken@281
   321
{
patmandin@3861
   322
	int scancode;
slouken@281
   323
slouken@281
   324
	if (kc) {
patmandin@3861
   325
		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
slouken@281
   326
		gem_currentkeyboard[scancode]=0xFF;
patmandin@9042
   327
		keyboard_ticks[scancode]=tick;
slouken@281
   328
	}
patmandin@6582
   329
}
slouken@281
   330
patmandin@9042
   331
static void do_keyboard_special(short ks, Uint32 tick)
patmandin@6582
   332
{
patmandin@9042
   333
	int scancode=0;
patmandin@9042
   334
slouken@281
   335
	/* Read special keys */
slouken@281
   336
	if (ks & K_RSHIFT)
patmandin@9042
   337
		scancode=SCANCODE_RIGHTSHIFT;
slouken@281
   338
	if (ks & K_LSHIFT)
patmandin@9042
   339
		scancode=SCANCODE_LEFTSHIFT;
slouken@281
   340
	if (ks & K_CTRL)
patmandin@9042
   341
		scancode=SCANCODE_LEFTCONTROL;
slouken@281
   342
	if (ks & K_ALT)
patmandin@9042
   343
		scancode=SCANCODE_LEFTALT;
patmandin@9042
   344
patmandin@9042
   345
	if (scancode) {
patmandin@9042
   346
		gem_currentkeyboard[scancode]=0xFF;
patmandin@9042
   347
		keyboard_ticks[scancode]=tick;
patmandin@9042
   348
	}
slouken@281
   349
}
slouken@281
   350
patmandin@6583
   351
static void do_mouse_motion(_THIS, short mx, short my)
slouken@281
   352
{
patmandin@926
   353
	short x2, y2, w2, h2;
patmandin@926
   354
patmandin@6648
   355
	if (this->input_grab == SDL_GRAB_OFF) {
patmandin@6648
   356
		/* Switch mouse focus state */
patmandin@6648
   357
		if (!GEM_fullscreen && (GEM_handle>=0)) {
patmandin@6648
   358
			SDL_PrivateAppActive(
patmandin@6648
   359
				mouse_in_work_area(GEM_handle, mx,my),
patmandin@6648
   360
				SDL_APPMOUSEFOCUS);
patmandin@6648
   361
		}
patmandin@6648
   362
	}
patmandin@6648
   363
	GEM_CheckMouseMode(this);
patmandin@6648
   364
patmandin@1067
   365
	/* Don't return mouse events if out of window */
patmandin@1067
   366
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
patmandin@1067
   367
		return;
patmandin@1067
   368
	}
patmandin@1067
   369
patmandin@6584
   370
	/* Relative mouse motion ? */
patmandin@6584
   371
	if (GEM_mouse_relative) {
patmandin@6584
   372
		if (GEM_usedevmouse) {
patmandin@6584
   373
			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   374
		} else {
patmandin@6584
   375
			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   376
		}
patmandin@6584
   377
		return;
patmandin@6584
   378
	}
patmandin@6584
   379
patmandin@926
   380
	/* Retrieve window coords, and generate mouse events accordingly */
patmandin@926
   381
	x2 = y2 = 0;
patmandin@928
   382
	w2 = VDI_w;
patmandin@928
   383
	h2 = VDI_h;
patmandin@926
   384
	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
patmandin@11558
   385
		x2 = GEM_work_x;
patmandin@11558
   386
		y2 = GEM_work_y;
patmandin@11558
   387
		w2 = GEM_work_w;
patmandin@11558
   388
		h2 = GEM_work_h;
patmandin@926
   389
	}
slouken@319
   390
patmandin@6647
   391
	if ((prevmx!=mx) || (prevmy!=my)) {
patmandin@6584
   392
		int posx, posy;
patmandin@926
   393
patmandin@6584
   394
		/* Give mouse position relative to window position */
patmandin@6584
   395
		posx = mx - x2;
patmandin@6584
   396
		if (posx<0) posx = 0;
patmandin@6584
   397
		if (posx>w2) posx = w2-1;
patmandin@6584
   398
		posy = my - y2;
patmandin@6584
   399
		if (posy<0) posy = 0;
patmandin@6584
   400
		if (posy>h2) posy = h2-1;
patmandin@926
   401
patmandin@6584
   402
		SDL_PrivateMouseMotion(0, 0, posx, posy);
slouken@281
   403
	}
patmandin@6647
   404
patmandin@6647
   405
	prevmx = mx;
patmandin@6647
   406
	prevmy = my;
patmandin@6583
   407
}
slouken@281
   408
patmandin@6583
   409
static void do_mouse_buttons(_THIS, short mb)
patmandin@6583
   410
{
patmandin@6583
   411
	int i;
slouken@281
   412
patmandin@6583
   413
	/* Don't return mouse events if out of window */
patmandin@6583
   414
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
patmandin@6583
   415
		return;
patmandin@6583
   416
patmandin@6647
   417
	if (prevmb==mb)
patmandin@6583
   418
		return;
patmandin@6583
   419
patmandin@6583
   420
	for (i=0;i<3;i++) {
patmandin@6583
   421
		int curbutton, prevbutton;
patmandin@11554
   422
patmandin@6583
   423
		curbutton = mb & (1<<i);
patmandin@6647
   424
		prevbutton = prevmb & (1<<i);
patmandin@11554
   425
patmandin@6583
   426
		if (curbutton && !prevbutton) {
patmandin@6583
   427
			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
slouken@281
   428
		}
patmandin@6583
   429
		if (!curbutton && prevbutton) {
patmandin@6583
   430
			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
patmandin@6583
   431
		}
slouken@281
   432
	}
patmandin@6583
   433
patmandin@6647
   434
	prevmb = mb;
slouken@281
   435
}
patmandin@6585
   436
patmandin@6585
   437
/* Check if mouse in visible area of the window */
patmandin@6585
   438
static int mouse_in_work_area(int winhandle, short mx, short my)
patmandin@6585
   439
{
patmandin@6585
   440
	short todo[4];
patmandin@6585
   441
	short inside[4] = {mx,my,1,1};
patmandin@6585
   442
patmandin@6585
   443
	/* Browse the rectangle list */
patmandin@6585
   444
	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
patmandin@6585
   445
		while (todo[2] && todo[3]) {
patmandin@6585
   446
			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
patmandin@6585
   447
				return 1;
patmandin@6585
   448
			}
patmandin@6585
   449
patmandin@6585
   450
			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
patmandin@6585
   451
				break;
patmandin@6585
   452
			}
patmandin@6585
   453
		}
patmandin@6585
   454
patmandin@6585
   455
	}
patmandin@6585
   456
patmandin@6585
   457
	return 0;
patmandin@6585
   458
}
patmandin@9042
   459
patmandin@9042
   460
/* Clear key state for which we did not receive events for a while */
patmandin@9042
   461
patmandin@9042
   462
static void clearKeyboardState(Uint32 tick)
patmandin@9042
   463
{
patmandin@9042
   464
	int i;
patmandin@9042
   465
patmandin@9042
   466
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
patmandin@9042
   467
		if (keyboard_ticks[i]) {
patmandin@9042
   468
			if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
patmandin@9042
   469
				gem_currentkeyboard[i]=0;
patmandin@9042
   470
				keyboard_ticks[i]=0;
patmandin@9042
   471
			}
patmandin@9042
   472
		}
patmandin@9042
   473
	}
patmandin@9042
   474
}