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#ifdef SDL_JOYSTICK_RISCOS
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   RISC OS - Joystick support by Alan Buckley (alan_baa@hotmail.com) - 10 April 2003
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   Note: Currently assumes joystick is present if joystick module is loaded
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   and that there is one joystick with four buttons.
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the system specific header for the SDL joystick API */
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_joystick.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysjoystick.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_joystick_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "kernel.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define JOYSTICK_READ 0x43F40
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct joystick_hwdata
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int joystate;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to scan the system for joysticks.
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function should set SDL_numjoysticks to the number of available
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * joysticks.  Joystick 0 should be the system default joystick.
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * It should return number of joysticks, or -1 on an unrecoverable fatal error.
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickInit(void)
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi_regs regs;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 /* Try to read joystick 0 */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[0] = 0;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Switch works so assume we've got a joystick */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Switch fails so it looks like there's no joystick here */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to get the device-dependent name of a joystick */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char *SDL_SYS_JoystickName(int index)
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (index == 0)
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return "RISC OS Joystick 0";
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("No joystick available with that index");
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(NULL);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to open a joystick for use.
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The joystick to open is specified by the index field of the joystick.
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   This should fill the nbuttons and naxes fields of the joystick structure.
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   It returns 0, or -1 if there is an error.
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi_regs regs;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata))))
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[0] = joystick->index;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't know how to get exact count of buttons so assume max of 4 for now */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nbuttons=4;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nhats=0;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nballs=0;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->naxes=2;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->hwdata->joystate=0;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to update the state of a joystick - called as a device poll.
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function shouldn't update the joystick structure directly,
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * but instead should call SDL_PrivateJoystick*() to deliver events
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * and update joystick device state.
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi_regs regs;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[0] = joystick->index;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int newstate = regs.r[0];
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int oldstate = joystick->hwdata->joystate;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (newstate != oldstate)
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((newstate & 0xFF) != (oldstate & 0xFF))
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int y = regs.r[0] & 0xFF;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Convert to signed values */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (y >= 128) y -= 256;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateJoystickAxis(joystick,1,-y * 256); /* Up and down opposite to result in SDL */
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((newstate & 0xFF00) != (oldstate & 0xFF00))
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int x = (regs.r[0] & 0xFF00) >> 8;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (x >= 128) x -= 256;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateJoystickAxis(joystick,0,x * 256);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((newstate & 0xFF0000) != (oldstate & 0xFF0000))
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int buttons = (regs.r[0] & 0xFF0000) >> 16;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int oldbuttons = (oldstate & 0xFF0000) >> 16;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int i;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for (i = 0; i < joystick->nbuttons; i++)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ((buttons & (1<<i)) != (oldbuttons & (1<<i)))
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (buttons & (1<<i)) SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						else SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			joystick->hwdata->joystate = newstate;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to close a joystick after use */
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickClose(SDL_Joystick *joystick)
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(joystick->hwdata)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(joystick->hwdata);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to perform any system-specific joystick related cleanup */
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickQuit(void)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numjoysticks=0;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_JOYSTICK_RISCOS */
177