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 *	Atari keyboard events manager, using Gemdos
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *	Patrice Mandin
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Mint includes */
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/osbind.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/cookie.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_atarikeys.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_atarievents_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_xbiosevents_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ataridevmouse_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* To save state of keyboard */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char gemdos_currentkeyboard[ATARIBIOS_MAXKEYS];
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char gemdos_previouskeyboard[ATARIBIOS_MAXKEYS];
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_bool use_dev_mouse = SDL_FALSE;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UpdateSpecialKeys(int special_keys_state);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid AtariGemdos_InitOSKeymap(_THIS)
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int vectors_mask;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*	unsigned long dummy;*/
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gemdos_currentkeyboard, 0, sizeof(gemdos_currentkeyboard));
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gemdos_previouskeyboard, 0, sizeof(gemdos_previouskeyboard));
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	use_dev_mouse = (SDL_AtariDevMouse_Open()!=0) ? SDL_TRUE : SDL_FALSE;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vectors_mask = ATARI_XBIOS_JOYSTICKEVENTS;	/* XBIOS joystick events */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!use_dev_mouse) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vectors_mask |= ATARI_XBIOS_MOUSEEVENTS;	/* XBIOS mouse events */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vectors_mask = 0;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}*/
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AtariXbios_InstallVectors(vectors_mask);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid AtariGemdos_PumpEvents(_THIS)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_keysym keysym;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update pressed keys */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(gemdos_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (Cconis()!=DEV_BUSY) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned long key_pressed;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		key_pressed=Cnecin();
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gemdos_currentkeyboard[(key_pressed>>16)&(ATARIBIOS_MAXKEYS-1)]=0xFF;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read special keys */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UpdateSpecialKeys(Kbshift(-1));
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Now generate events */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Key pressed ? */
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (gemdos_currentkeyboard[i] && !gemdos_previouskeyboard[i])
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateKeyboard(SDL_PRESSED,
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Key unpressed ? */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (gemdos_previouskeyboard[i] && !gemdos_currentkeyboard[i])
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateKeyboard(SDL_RELEASED,
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (use_dev_mouse) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_AtariDevMouse_PostMouseEvents(this, SDL_TRUE);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Will be previous table */
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(gemdos_previouskeyboard, gemdos_currentkeyboard, sizeof(gemdos_previouskeyboard));
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UpdateSpecialKeys(int special_keys_state)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define UPDATE_SPECIAL_KEYS(numbit,scancode) \
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{	\
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (special_keys_state & (1<<(numbit))) { \
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gemdos_currentkeyboard[scancode]=0xFF; \
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}	\
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid AtariGemdos_ShutdownEvents(void)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AtariXbios_RestoreVectors();
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (use_dev_mouse) {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_AtariDevMouse_Close();
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
133