src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Mon, 28 Jul 2014 11:44:39 +0200
branchSDL-1.2
changeset 9018 4a9c67d11c9f
parent 6649 1aab0b09a0e3
child 9042 89d54976ff74
permissions -rw-r--r--
atari: Do not use system interrupt for handling timer. Do it from userspace, like RISCOS backend.
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
slouken@281
    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
slouken@1361
    35
#include "../../events/SDL_sysevents.h"
slouken@1361
    36
#include "../../events/SDL_events_c.h"
slouken@281
    37
#include "SDL_gemvideo.h"
slouken@281
    38
#include "SDL_gemevents_c.h"
patmandin@5649
    39
#include "SDL_gemmouse_c.h"
patmandin@1412
    40
#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
patmandin@1412
    41
#include "../ataricommon/SDL_atarievents_c.h"
patmandin@1412
    42
#include "../ataricommon/SDL_xbiosevents_c.h"
patmandin@1420
    43
#include "../ataricommon/SDL_ataridevmouse_c.h"
slouken@281
    44
slouken@281
    45
/* Variables */
slouken@281
    46
slouken@281
    47
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    48
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    49
patmandin@6647
    50
static short prevmx=0,prevmy=0,prevmb=0;
patmandin@6647
    51
slouken@281
    52
/* Functions prototypes */
slouken@281
    53
slouken@281
    54
static int do_messages(_THIS, short *message);
patmandin@6582
    55
static void do_keyboard(short kc);
patmandin@6582
    56
static void do_keyboard_special(short ks);
patmandin@6583
    57
static void do_mouse_motion(_THIS, short mx, short my);
patmandin@6583
    58
static void do_mouse_buttons(_THIS, short mb);
patmandin@6585
    59
static int mouse_in_work_area(int winhandle, short mx, short my);
slouken@281
    60
slouken@281
    61
/* Functions */
slouken@281
    62
slouken@281
    63
void GEM_InitOSKeymap(_THIS)
slouken@281
    64
{
slouken@1336
    65
	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
slouken@1336
    66
	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
slouken@281
    67
slouken@281
    68
	/* Mouse init */
slouken@281
    69
	GEM_mouse_relative = SDL_FALSE;
patmandin@3861
    70
patmandin@3861
    71
	SDL_Atari_InitInternalKeymap(this);
slouken@281
    72
}
slouken@281
    73
slouken@281
    74
void GEM_PumpEvents(_THIS)
slouken@281
    75
{
patmandin@6647
    76
	short prevkc=0, mousex, mousey, mouseb, kstate;
slouken@281
    77
	int i;
patmandin@6647
    78
	SDL_keysym keysym;
slouken@281
    79
slouken@1336
    80
	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
patmandin@4564
    81
slouken@281
    82
	for (;;)
slouken@281
    83
	{
patmandin@6585
    84
		int quit, resultat;
patmandin@6647
    85
		short buffer[8], kc, dummy;
slouken@281
    86
patmandin@6585
    87
		quit = 0;
patmandin@1067
    88
patmandin@9018
    89
		SDL_AtariMint_BackgroundTasks();
patmandin@9018
    90
slouken@281
    91
		resultat = evnt_multi(
patmandin@6649
    92
			MU_MESAG|MU_TIMER|MU_KEYBD,
patmandin@6649
    93
			0,0,0,
patmandin@6649
    94
			0,0,0,0,0,
patmandin@1089
    95
			0,0,0,0,0,
slouken@281
    96
			buffer,
slouken@281
    97
			10,
patmandin@6649
    98
			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
slouken@281
    99
		);
slouken@281
   100
slouken@281
   101
		/* Message event ? */
slouken@281
   102
		if (resultat & MU_MESAG)
slouken@281
   103
			quit = do_messages(this, buffer);
slouken@281
   104
slouken@281
   105
		/* Keyboard event ? */
slouken@319
   106
		if (resultat & MU_KEYBD) {
patmandin@6649
   107
			do_keyboard_special(kstate);
patmandin@6582
   108
			if (prevkc != kc) {
patmandin@6582
   109
				do_keyboard(kc);
patmandin@6572
   110
				prevkc = kc;
slouken@319
   111
			} else {
slouken@319
   112
				/* Avoid looping, if repeating same key */
patmandin@6582
   113
				quit = 1;
slouken@319
   114
			}
slouken@319
   115
		}
slouken@281
   116
slouken@281
   117
		/* Timer event ? */
slouken@281
   118
		if ((resultat & MU_TIMER) || quit)
slouken@281
   119
			break;
slouken@281
   120
	}
slouken@281
   121
patmandin@6649
   122
	/* Update mouse state */
patmandin@6649
   123
	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
patmandin@6649
   124
	do_keyboard_special(kstate);
patmandin@6649
   125
	do_mouse_motion(this, mousex, mousey);
patmandin@6649
   126
	do_mouse_buttons(this, mouseb);
patmandin@6649
   127
slouken@319
   128
	/* Now generate keyboard events */
slouken@281
   129
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   130
		/* Key pressed ? */
slouken@281
   131
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
patmandin@1209
   132
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@3861
   133
				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
slouken@281
   134
			
slouken@281
   135
		/* Key unpressed ? */
slouken@281
   136
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
patmandin@1209
   137
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@3861
   138
				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
slouken@281
   139
	}
slouken@281
   140
slouken@1336
   141
	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   142
patmandin@996
   143
	/* Refresh window name ? */
patmandin@996
   144
	if (GEM_refresh_name) {
patmandin@4059
   145
		const char *window_name =
patmandin@4059
   146
			(SDL_GetAppState() & SDL_APPACTIVE)
patmandin@4059
   147
			? GEM_title_name : GEM_icon_name;
patmandin@4059
   148
		if (window_name) {
patmandin@4059
   149
			wind_set(GEM_handle,WF_NAME,
patmandin@4059
   150
				(short)(((unsigned long)window_name)>>16),
patmandin@4059
   151
				(short)(((unsigned long)window_name) & 0xffff),
patmandin@4059
   152
				0,0);
patmandin@996
   153
		}
patmandin@996
   154
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   155
	}
slouken@281
   156
}
slouken@281
   157
slouken@281
   158
static int do_messages(_THIS, short *message)
slouken@281
   159
{
patmandin@5867
   160
	int quit, check_mouse_mode;
patmandin@922
   161
	short x2,y2,w2,h2;
slouken@281
   162
patmandin@4060
   163
	quit = check_mouse_mode = 0;
slouken@281
   164
	switch (message[0]) {
slouken@281
   165
		case WM_CLOSED:
slouken@281
   166
		case AP_TERM:    
patmandin@5867
   167
			SDL_PrivateQuit();
slouken@281
   168
			quit=1;
slouken@281
   169
			break;
slouken@281
   170
		case WM_MOVED:
slouken@281
   171
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   172
			break;
slouken@281
   173
		case WM_TOPPED:
slouken@281
   174
			wind_set(message[3],WF_TOP,message[4],0,0,0);
patmandin@1091
   175
			/* Continue with TOP event processing */
patmandin@1091
   176
		case WM_ONTOP:
slouken@281
   177
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
patmandin@1074
   178
			if (VDI_setpalette) {
patmandin@1074
   179
				VDI_setpalette(this, VDI_curpalette);
patmandin@1074
   180
			}
patmandin@4060
   181
			check_mouse_mode = 1;
slouken@281
   182
			break;
slouken@281
   183
		case WM_REDRAW:
patmandin@964
   184
			if (!GEM_lock_redraw) {
patmandin@964
   185
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   186
			}
slouken@281
   187
			break;
slouken@281
   188
		case WM_ICONIFY:
slouken@281
   189
		case WM_ALLICONIFY:
slouken@281
   190
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   191
			/* If we're active, make ourselves inactive */
slouken@281
   192
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   193
				/* Send an internal deactivate event */
patmandin@1088
   194
				SDL_PrivateAppActive(0, SDL_APPACTIVE);
slouken@281
   195
			}
patmandin@736
   196
			/* Update window title */
patmandin@736
   197
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@736
   198
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@736
   199
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   200
			}
patmandin@4060
   201
			check_mouse_mode = 1;
slouken@281
   202
			break;
slouken@281
   203
		case WM_UNICONIFY:
slouken@281
   204
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   205
			/* If we're not active, make ourselves active */
slouken@281
   206
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   207
				/* Send an internal activate event */
slouken@281
   208
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   209
			}
patmandin@736
   210
			if (GEM_refresh_name && GEM_title_name) {
patmandin@736
   211
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@736
   212
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   213
			}
patmandin@4060
   214
			check_mouse_mode = 1;
slouken@281
   215
			break;
slouken@281
   216
		case WM_SIZED:
slouken@281
   217
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
patmandin@964
   218
			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
slouken@281
   219
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
patmandin@964
   220
			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   221
			SDL_PrivateResize(w2, h2);
slouken@281
   222
			break;
slouken@281
   223
		case WM_FULLED:
slouken@281
   224
			{
slouken@281
   225
				short x,y,w,h;
slouken@281
   226
slouken@281
   227
				if (GEM_win_fulled) {
slouken@281
   228
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   229
					GEM_win_fulled = SDL_FALSE;
slouken@281
   230
				} else {
slouken@281
   231
					x = GEM_desk_x;
slouken@281
   232
					y = GEM_desk_y;
slouken@281
   233
					w = GEM_desk_w;
slouken@281
   234
					h = GEM_desk_h;
slouken@281
   235
					GEM_win_fulled = SDL_TRUE;
slouken@281
   236
				}
slouken@281
   237
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
patmandin@922
   238
				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@964
   239
				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   240
				SDL_PrivateResize(w2, h2);
slouken@281
   241
			}
slouken@281
   242
			break;
slouken@281
   243
		case WM_BOTTOMED:
patmandin@1091
   244
			wind_set(message[3],WF_BOTTOM,0,0,0,0);
patmandin@1091
   245
			/* Continue with BOTTOM event processing */
slouken@281
   246
		case WM_UNTOPPED:
slouken@281
   247
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
patmandin@1074
   248
			if (VDI_setpalette) {
patmandin@1074
   249
				VDI_setpalette(this, VDI_oldpalette);
patmandin@1074
   250
			}
patmandin@4060
   251
			check_mouse_mode = 1;
slouken@281
   252
			break;
slouken@281
   253
	}
patmandin@4060
   254
patmandin@4060
   255
	if (check_mouse_mode) {
patmandin@4060
   256
		GEM_CheckMouseMode(this);
patmandin@4060
   257
	}
slouken@281
   258
	
slouken@281
   259
	return quit;
slouken@281
   260
}
slouken@281
   261
patmandin@6582
   262
static void do_keyboard(short kc)
slouken@281
   263
{
patmandin@3861
   264
	int scancode;
slouken@281
   265
slouken@281
   266
	if (kc) {
patmandin@3861
   267
		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
slouken@281
   268
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   269
	}
patmandin@6582
   270
}
slouken@281
   271
patmandin@6582
   272
static void do_keyboard_special(short ks)
patmandin@6582
   273
{
slouken@281
   274
	/* Read special keys */
slouken@281
   275
	if (ks & K_RSHIFT)
slouken@281
   276
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   277
	if (ks & K_LSHIFT)
slouken@281
   278
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   279
	if (ks & K_CTRL)
slouken@281
   280
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   281
	if (ks & K_ALT)
slouken@281
   282
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   283
}
slouken@281
   284
patmandin@6583
   285
static void do_mouse_motion(_THIS, short mx, short my)
slouken@281
   286
{
patmandin@926
   287
	short x2, y2, w2, h2;
patmandin@926
   288
patmandin@6648
   289
	if (this->input_grab == SDL_GRAB_OFF) {
patmandin@6648
   290
		/* Switch mouse focus state */
patmandin@6648
   291
		if (!GEM_fullscreen && (GEM_handle>=0)) {
patmandin@6648
   292
			SDL_PrivateAppActive(
patmandin@6648
   293
				mouse_in_work_area(GEM_handle, mx,my),
patmandin@6648
   294
				SDL_APPMOUSEFOCUS);
patmandin@6648
   295
		}
patmandin@6648
   296
	}
patmandin@6648
   297
	GEM_CheckMouseMode(this);
patmandin@6648
   298
patmandin@1067
   299
	/* Don't return mouse events if out of window */
patmandin@1067
   300
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
patmandin@1067
   301
		return;
patmandin@1067
   302
	}
patmandin@1067
   303
patmandin@6584
   304
	/* Relative mouse motion ? */
patmandin@6584
   305
	if (GEM_mouse_relative) {
patmandin@6584
   306
		if (GEM_usedevmouse) {
patmandin@6584
   307
			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   308
		} else {
patmandin@6584
   309
			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   310
		}
patmandin@6584
   311
		return;
patmandin@6584
   312
	}
patmandin@6584
   313
patmandin@926
   314
	/* Retrieve window coords, and generate mouse events accordingly */
patmandin@926
   315
	x2 = y2 = 0;
patmandin@928
   316
	w2 = VDI_w;
patmandin@928
   317
	h2 = VDI_h;
patmandin@926
   318
	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
patmandin@926
   319
		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@926
   320
	}
slouken@319
   321
patmandin@6647
   322
	if ((prevmx!=mx) || (prevmy!=my)) {
patmandin@6584
   323
		int posx, posy;
patmandin@926
   324
patmandin@6584
   325
		/* Give mouse position relative to window position */
patmandin@6584
   326
		posx = mx - x2;
patmandin@6584
   327
		if (posx<0) posx = 0;
patmandin@6584
   328
		if (posx>w2) posx = w2-1;
patmandin@6584
   329
		posy = my - y2;
patmandin@6584
   330
		if (posy<0) posy = 0;
patmandin@6584
   331
		if (posy>h2) posy = h2-1;
patmandin@926
   332
patmandin@6584
   333
		SDL_PrivateMouseMotion(0, 0, posx, posy);
slouken@281
   334
	}
patmandin@6647
   335
patmandin@6647
   336
	prevmx = mx;
patmandin@6647
   337
	prevmy = my;
patmandin@6583
   338
}
slouken@281
   339
patmandin@6583
   340
static void do_mouse_buttons(_THIS, short mb)
patmandin@6583
   341
{
patmandin@6583
   342
	int i;
slouken@281
   343
patmandin@6583
   344
	/* Don't return mouse events if out of window */
patmandin@6583
   345
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
patmandin@6583
   346
		return;
patmandin@6583
   347
patmandin@6647
   348
	if (prevmb==mb)
patmandin@6583
   349
		return;
patmandin@6583
   350
patmandin@6583
   351
	for (i=0;i<3;i++) {
patmandin@6583
   352
		int curbutton, prevbutton;
slouken@281
   353
		
patmandin@6583
   354
		curbutton = mb & (1<<i);
patmandin@6647
   355
		prevbutton = prevmb & (1<<i);
patmandin@6583
   356
	
patmandin@6583
   357
		if (curbutton && !prevbutton) {
patmandin@6583
   358
			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
slouken@281
   359
		}
patmandin@6583
   360
		if (!curbutton && prevbutton) {
patmandin@6583
   361
			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
patmandin@6583
   362
		}
slouken@281
   363
	}
patmandin@6583
   364
patmandin@6647
   365
	prevmb = mb;
slouken@281
   366
}
patmandin@6585
   367
patmandin@6585
   368
/* Check if mouse in visible area of the window */
patmandin@6585
   369
static int mouse_in_work_area(int winhandle, short mx, short my)
patmandin@6585
   370
{
patmandin@6585
   371
	short todo[4];
patmandin@6585
   372
	short inside[4] = {mx,my,1,1};
patmandin@6585
   373
patmandin@6585
   374
	/* Browse the rectangle list */
patmandin@6585
   375
	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
patmandin@6585
   376
		while (todo[2] && todo[3]) {
patmandin@6585
   377
			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
patmandin@6585
   378
				return 1;
patmandin@6585
   379
			}
patmandin@6585
   380
patmandin@6585
   381
			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
patmandin@6585
   382
				break;
patmandin@6585
   383
			}
patmandin@6585
   384
		}
patmandin@6585
   385
patmandin@6585
   386
	}
patmandin@6585
   387
patmandin@6585
   388
	return 0;
patmandin@6585
   389
}