Home | History | Annotate | Download | only in riscos
      1 /*
      2     SDL - Simple DirectMedia Layer
      3     Copyright (C) 1997-2006 Sam Lantinga
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Lesser General Public
      7     License as published by the Free Software Foundation; either
      8     version 2.1 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Lesser General Public License for more details.
     14 
     15     You should have received a copy of the GNU Lesser General Public
     16     License along with this library; if not, write to the Free Software
     17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     18 
     19     Sam Lantinga
     20     slouken (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 #ifdef SDL_JOYSTICK_RISCOS
     25 
     26 /*
     27    RISC OS - Joystick support by Alan Buckley (alan_baa (at) hotmail.com) - 10 April 2003
     28 
     29    Note: Currently assumes joystick is present if joystick module is loaded
     30    and that there is one joystick with four buttons.
     31 */
     32 
     33 /* This is the system specific header for the SDL joystick API */
     34 
     35 #include "SDL_events.h"
     36 #include "SDL_joystick.h"
     37 #include "../SDL_sysjoystick.h"
     38 #include "../SDL_joystick_c.h"
     39 
     40 #include "kernel.h"
     41 
     42 #define JOYSTICK_READ 0x43F40
     43 
     44 struct joystick_hwdata
     45 {
     46 	int joystate;
     47 };
     48 
     49 
     50 /* Function to scan the system for joysticks.
     51  * This function should set SDL_numjoysticks to the number of available
     52  * joysticks.  Joystick 0 should be the system default joystick.
     53  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
     54  */
     55 int SDL_SYS_JoystickInit(void)
     56 {
     57 	_kernel_swi_regs regs;
     58 
     59 	 /* Try to read joystick 0 */
     60 	regs.r[0] = 0;
     61 	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
     62 	{
     63 		/* Switch works so assume we've got a joystick */
     64 		return 1;
     65 	}
     66 	/* Switch fails so it looks like there's no joystick here */
     67 
     68 	return(0);
     69 }
     70 
     71 /* Function to get the device-dependent name of a joystick */
     72 const char *SDL_SYS_JoystickName(int index)
     73 {
     74 	if (index == 0)
     75 	{
     76 		return "RISC OS Joystick 0";
     77 	}
     78 
     79 	SDL_SetError("No joystick available with that index");
     80 	return(NULL);
     81 }
     82 
     83 /* Function to open a joystick for use.
     84    The joystick to open is specified by the index field of the joystick.
     85    This should fill the nbuttons and naxes fields of the joystick structure.
     86    It returns 0, or -1 if there is an error.
     87  */
     88 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
     89 {
     90 	_kernel_swi_regs regs;
     91 
     92 	if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata))))
     93 		return -1;
     94 
     95 	regs.r[0] = joystick->index;
     96 
     97 	/* Don't know how to get exact count of buttons so assume max of 4 for now */
     98 	joystick->nbuttons=4;
     99 
    100 	joystick->nhats=0;
    101 	joystick->nballs=0;
    102 	joystick->naxes=2;
    103 	joystick->hwdata->joystate=0;
    104 
    105 	return 0;
    106 
    107 }
    108 
    109 /* Function to update the state of a joystick - called as a device poll.
    110  * This function shouldn't update the joystick structure directly,
    111  * but instead should call SDL_PrivateJoystick*() to deliver events
    112  * and update joystick device state.
    113  */
    114 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
    115 {
    116 	_kernel_swi_regs regs;
    117 	regs.r[0] = joystick->index;
    118 
    119 	if (_kernel_swi(JOYSTICK_READ, &regs, &regs) == NULL)
    120 	{
    121 		int newstate = regs.r[0];
    122 		int oldstate = joystick->hwdata->joystate;
    123 		if (newstate != oldstate)
    124 		{
    125 			if ((newstate & 0xFF) != (oldstate & 0xFF))
    126 			{
    127 				int y = regs.r[0] & 0xFF;
    128 				/* Convert to signed values */
    129 				if (y >= 128) y -= 256;
    130 				SDL_PrivateJoystickAxis(joystick,1,-y * 256); /* Up and down opposite to result in SDL */
    131 			}
    132 			if ((newstate & 0xFF00) != (oldstate & 0xFF00))
    133 			{
    134 				int x = (regs.r[0] & 0xFF00) >> 8;
    135 				if (x >= 128) x -= 256;
    136 				SDL_PrivateJoystickAxis(joystick,0,x * 256);
    137 			}
    138 
    139 			if ((newstate & 0xFF0000) != (oldstate & 0xFF0000))
    140 			{
    141 				int buttons = (regs.r[0] & 0xFF0000) >> 16;
    142 				int oldbuttons = (oldstate & 0xFF0000) >> 16;
    143 				int i;
    144 				for (i = 0; i < joystick->nbuttons; i++)
    145 				{
    146 					if ((buttons & (1<<i)) != (oldbuttons & (1<<i)))
    147 					{
    148 						if (buttons & (1<<i)) SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
    149 						else SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
    150 					}
    151 				}
    152 			}
    153 			joystick->hwdata->joystate = newstate;
    154 		}
    155 	}
    156 
    157 	return;
    158 }
    159 
    160 /* Function to close a joystick after use */
    161 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
    162 {
    163 	if(joystick->hwdata)
    164 		SDL_free(joystick->hwdata);
    165 	return;
    166 }
    167 
    168 /* Function to perform any system-specific joystick related cleanup */
    169 void SDL_SYS_JoystickQuit(void)
    170 {
    171 	SDL_numjoysticks=0;
    172 
    173 	return;
    174 }
    175 
    176 #endif /* SDL_JOYSTICK_RISCOS */
    177