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