19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * GEM SDL video driver implementation
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * inspired from the Dummy SDL driver
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Patrice Mandin
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * and work from
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <gem.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gemvideo.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gemevents_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gemmouse_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../ataricommon/SDL_atarievents_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../ataricommon/SDL_xbiosevents_c.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../ataricommon/SDL_ataridevmouse_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Variables */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions prototypes */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int do_messages(_THIS, short *message);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void do_keyboard(short kc, short ks);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void do_mouse(_THIS, short mx, short my, short mb, short ks);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GEM_InitOSKeymap(_THIS)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mouse init */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GEM_mouse_relative = SDL_FALSE;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Atari_InitInternalKeymap(this);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GEM_PumpEvents(_THIS)
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short prevkc, prevks;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static short maskmouseb=0;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_keysym	keysym;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prevkc = prevks = 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (;;)
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int quit, resultat, event_mask, mouse_event;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		short buffer[8], kc;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		short x2,y2,w2,h2;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		short mousex, mousey, mouseb, dummy;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		short kstate;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		quit =
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_event =
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x2=y2=w2=h2 = 0;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!GEM_fullscreen && (GEM_handle>=0)) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			event_mask |= MU_M1;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_event = ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				== SDL_APPMOUSEFOCUS) ? MO_LEAVE : MO_ENTER;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		resultat = evnt_multi(
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			event_mask,
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			0x101,7,maskmouseb,
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_event,x2,y2,w2,h2,
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			0,0,0,0,0,
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			buffer,
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			10,
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Message event ? */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (resultat & MU_MESAG)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			quit = do_messages(this, buffer);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Keyboard event ? */
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (resultat & MU_KEYBD) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((prevkc != kc) || (prevks != kstate)) {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				do_keyboard(kc,kstate);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Avoid looping, if repeating same key */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Mouse entering/leaving window */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (resultat & MU_M1) {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (this->input_grab == SDL_GRAB_OFF) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Switch mouse focus state */
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateAppActive((mouse_event == MO_ENTER),
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_APPMOUSEFOCUS);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GEM_CheckMouseMode(this);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Mouse button event ? */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (resultat & MU_BUTTON) {
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			do_mouse(this, mousex, mousey, mouseb, kstate);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			maskmouseb = mouseb & 7;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Timer event ? */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((resultat & MU_TIMER) || quit)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Now generate keyboard events */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Key pressed ? */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateKeyboard(SDL_PRESSED,
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Key unpressed ? */
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateKeyboard(SDL_RELEASED,
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Refresh window name ? */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (GEM_refresh_name) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *window_name =
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(SDL_GetAppState() & SDL_APPACTIVE)
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			? GEM_title_name : GEM_icon_name;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (window_name) {
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(GEM_handle,WF_NAME,
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(short)(((unsigned long)window_name)>>16),
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(short)(((unsigned long)window_name) & 0xffff),
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0,0);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GEM_refresh_name = SDL_FALSE;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int do_messages(_THIS, short *message)
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int quit, check_mouse_mode;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short x2,y2,w2,h2;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	quit = check_mouse_mode = 0;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (message[0]) {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_CLOSED:
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case AP_TERM:
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateQuit();
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			quit=1;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_MOVED:
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_TOPPED:
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(message[3],WF_TOP,message[4],0,0,0);
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Continue with TOP event processing */
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_ONTOP:
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (VDI_setpalette) {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				VDI_setpalette(this, VDI_curpalette);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			check_mouse_mode = 1;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_REDRAW:
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (!GEM_lock_redraw) {
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				GEM_wind_redraw(this, message[3],&message[4]);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_ICONIFY:
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_ALLICONIFY:
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If we're active, make ourselves inactive */
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Send an internal deactivate event */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateAppActive(0, SDL_APPACTIVE);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Update window title */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (GEM_refresh_name && GEM_icon_name) {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				GEM_refresh_name = SDL_FALSE;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			check_mouse_mode = 1;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_UNICONIFY:
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If we're not active, make ourselves active */
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Send an internal activate event */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateAppActive(1, SDL_APPACTIVE);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (GEM_refresh_name && GEM_title_name) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				GEM_refresh_name = SDL_FALSE;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			check_mouse_mode = 1;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_SIZED:
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateResize(w2, h2);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_FULLED:
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				short x,y,w,h;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (GEM_win_fulled) {
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GEM_win_fulled = SDL_FALSE;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					x = GEM_desk_x;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					y = GEM_desk_y;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					w = GEM_desk_w;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					h = GEM_desk_h;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GEM_win_fulled = SDL_TRUE;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateResize(w2, h2);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_BOTTOMED:
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wind_set(message[3],WF_BOTTOM,0,0,0,0);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Continue with BOTTOM event processing */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_UNTOPPED:
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (VDI_setpalette) {
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				VDI_setpalette(this, VDI_oldpalette);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			check_mouse_mode = 1;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (check_mouse_mode) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GEM_CheckMouseMode(this);
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return quit;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void do_keyboard(short kc, short ks)
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int scancode;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (kc) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[scancode]=0xFF;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read special keys */
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_RSHIFT)
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_LSHIFT)
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_CTRL)
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_ALT)
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void do_mouse(_THIS, short mx, short my, short mb, short ks)
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static short prevmousex=0, prevmousey=0, prevmouseb=0;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short x2, y2, w2, h2;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't return mouse events if out of window */
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Retrieve window coords, and generate mouse events accordingly */
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	x2 = y2 = 0;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	w2 = VDI_w;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	h2 = VDI_h;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Do not generate mouse button event if out of window working area */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mb=prevmouseb;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mouse motion ? */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (GEM_mouse_relative) {
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (GEM_usedevmouse) {
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((prevmousex!=mx) || (prevmousey!=my)) {
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int posx, posy;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Give mouse position relative to window position */
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posx = mx - x2;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (posx<0) posx = 0;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (posx>w2) posx = w2-1;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posy = my - y2;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (posy<0) posy = 0;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (posy>h2) posy = h2-1;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateMouseMotion(0, 0, posx, posy);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		prevmousex = mx;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		prevmousey = my;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mouse button ? */
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (prevmouseb!=mb) {
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int i;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (i=0;i<3;i++) {
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int curbutton, prevbutton;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			curbutton = mb & (1<<i);
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			prevbutton = prevmouseb & (1<<i);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (curbutton && !prevbutton) {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (!curbutton && prevbutton) {
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		prevmouseb = mb;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read special keys */
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_RSHIFT)
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_LSHIFT)
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_CTRL)
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ks & K_ALT)
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
376