src/joystick/riscos/SDL_sysjoystick.c
changeset 630 550bccdf04bd
child 769 b8d311d90021
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/riscos/SDL_sysjoystick.c	Thu May 29 04:44:13 2003 +0000
     1.3 @@ -0,0 +1,180 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +/*
    1.27 +   RISCOS - Joystick support by Alan Buckley (alan_baa@hotmail.com) - 10 April 2003
    1.28 +
    1.29 +   Note: Currently assumes joystick is present if joystick module is loaded
    1.30 +   and that there is one joystick with four buttons.
    1.31 +*/
    1.32 +
    1.33 +#ifdef SAVE_RCSID
    1.34 +static char rcsid =
    1.35 + "@(#) $Id$";
    1.36 +#endif
    1.37 +
    1.38 +/* This is the system specific header for the SDL joystick API */
    1.39 +
    1.40 +#include <stdio.h>		/* For the definition of NULL */
    1.41 +#include <stdlib.h>
    1.42 +
    1.43 +#include "SDL_error.h"
    1.44 +#include "SDL_joystick.h"
    1.45 +#include "SDL_sysjoystick.h"
    1.46 +#include "SDL_joystick_c.h"
    1.47 +
    1.48 +#include "kernel.h"
    1.49 +
    1.50 +#define JOYSTICK_READ 0x43F40
    1.51 +
    1.52 +struct joystick_hwdata 
    1.53 +{
    1.54 +	int joystate;
    1.55 +};
    1.56 +
    1.57 +
    1.58 +/* Function to scan the system for joysticks.
    1.59 + * This function should set SDL_numjoysticks to the number of available
    1.60 + * joysticks.  Joystick 0 should be the system default joystick.
    1.61 + * It should return number of joysticks, or -1 on an unrecoverable fatal error.
    1.62 + */
    1.63 +int SDL_SYS_JoystickInit(void)
    1.64 +{
    1.65 +	_kernel_swi_regs regs;
    1.66 +
    1.67 +	 /* Try to read joystick 0 */
    1.68 +	regs.r[0] = 0;
    1.69 +	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
    1.70 +	{
    1.71 +		/* Switch works so assume we've got a joystick */
    1.72 +		return 1;
    1.73 +	}
    1.74 +	/* Switch fails so it looks like there's no joystick here */
    1.75 +
    1.76 +	return(0);
    1.77 +}
    1.78 +
    1.79 +/* Function to get the device-dependent name of a joystick */
    1.80 +const char *SDL_SYS_JoystickName(int index)
    1.81 +{
    1.82 +	if (index == 0)
    1.83 +	{
    1.84 +		return "RISCOS Joystick 0";
    1.85 +	}
    1.86 +
    1.87 +	SDL_SetError("No joystick available with that index");
    1.88 +	return(NULL);
    1.89 +}
    1.90 +
    1.91 +/* Function to open a joystick for use.
    1.92 +   The joystick to open is specified by the index field of the joystick.
    1.93 +   This should fill the nbuttons and naxes fields of the joystick structure.
    1.94 +   It returns 0, or -1 if there is an error.
    1.95 + */
    1.96 +int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
    1.97 +{
    1.98 +	_kernel_swi_regs regs;
    1.99 +
   1.100 +	if(!(joystick->hwdata=malloc(sizeof(struct joystick_hwdata))))
   1.101 +		return -1;
   1.102 +
   1.103 +	regs.r[0] = joystick->index;
   1.104 +
   1.105 +	/* Don't know how to get exact count of buttons so assume max of 4 for now */
   1.106 +	joystick->nbuttons=4;
   1.107 +
   1.108 +	joystick->nhats=0;
   1.109 +	joystick->nballs=0;
   1.110 +	joystick->naxes=2;
   1.111 +	joystick->hwdata->joystate=0;
   1.112 +
   1.113 +	return 0;
   1.114 +
   1.115 +}
   1.116 +
   1.117 +/* Function to update the state of a joystick - called as a device poll.
   1.118 + * This function shouldn't update the joystick structure directly,
   1.119 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.120 + * and update joystick device state.
   1.121 + */
   1.122 +void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   1.123 +{
   1.124 +	_kernel_swi_regs regs;
   1.125 +	regs.r[0] = joystick->index;
   1.126 +
   1.127 +	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
   1.128 +	{
   1.129 +		int newstate = regs.r[0];
   1.130 +		int oldstate = joystick->hwdata->joystate;
   1.131 +		if (newstate != oldstate)
   1.132 +		{
   1.133 +			if ((newstate & 0xFF) != (oldstate & 0xFF))
   1.134 +			{
   1.135 +				int y = regs.r[0] & 0xFF;
   1.136 +				/* Convert to signed values */
   1.137 +				if (y >= 128) y -= 256;
   1.138 +				SDL_PrivateJoystickAxis(joystick,1,-y * 256); /* Up and down opposite to result in SDL */
   1.139 +			}
   1.140 +			if ((newstate & 0xFF00) != (oldstate & 0xFF00))
   1.141 +			{
   1.142 +				int x = (regs.r[0] & 0xFF00) >> 8;
   1.143 +				if (x >= 128) x -= 256;
   1.144 +				SDL_PrivateJoystickAxis(joystick,0,x * 256);
   1.145 +			}
   1.146 +
   1.147 +			if ((newstate & 0xFF0000) != (oldstate & 0xFF0000))
   1.148 +			{
   1.149 +				int buttons = (regs.r[0] & 0xFF0000) >> 16;
   1.150 +				int oldbuttons = (oldstate & 0xFF0000) >> 16;
   1.151 +				int i;
   1.152 +				for (i = 0; i < joystick->nbuttons; i++)
   1.153 +				{
   1.154 +					if ((buttons & (1<<i)) != (oldbuttons & (1<<i)))
   1.155 +					{
   1.156 +						if (buttons & (1<<i)) SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
   1.157 +						else SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
   1.158 +					}
   1.159 +				}
   1.160 +			}
   1.161 +			joystick->hwdata->joystate = newstate;
   1.162 +		}		
   1.163 +	}
   1.164 +
   1.165 +	return;
   1.166 +}
   1.167 +
   1.168 +/* Function to close a joystick after use */
   1.169 +void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   1.170 +{
   1.171 +	if(joystick->hwdata)
   1.172 +		free(joystick->hwdata);
   1.173 +	return;
   1.174 +}
   1.175 +
   1.176 +/* Function to perform any system-specific joystick related cleanup */
   1.177 +void SDL_SYS_JoystickQuit(void)
   1.178 +{
   1.179 +	SDL_numjoysticks=0;
   1.180 +
   1.181 +	return;
   1.182 +}
   1.183 +