src/joystick/riscos/SDL_sysjoystick.c
author Cameron Cawley <ccawley2011@gmail.com>
Tue, 18 Jun 2019 23:55:01 +0100
branchSDL-1.2
changeset 12913 d07b4feae69c
parent 12779 8f5a01f3a9bd
permissions -rw-r--r--
riscos: Improve error reporting
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 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@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@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 #ifndef Joystick_Read
    43 #define Joystick_Read 0x43F40
    44 #endif
    45 
    46 struct joystick_hwdata 
    47 {
    48 	int joystate;
    49 };
    50 
    51 
    52 /* Function to scan the system for joysticks.
    53  * This function should set SDL_numjoysticks to the number of available
    54  * joysticks.  Joystick 0 should be the system default joystick.
    55  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
    56  */
    57 int SDL_SYS_JoystickInit(void)
    58 {
    59 	_kernel_swi_regs regs;
    60 
    61 	 /* Try to read joystick 0 */
    62 	regs.r[0] = 0;
    63 	if (_kernel_swi(Joystick_Read, &regs, &regs) == NULL)
    64 	{
    65 		/* Switch works so assume we've got a joystick */
    66 		return 1;
    67 	}
    68 	/* Switch fails so it looks like there's no joystick here */
    69 
    70 	return(0);
    71 }
    72 
    73 /* Function to get the device-dependent name of a joystick */
    74 const char *SDL_SYS_JoystickName(int index)
    75 {
    76 	if (index == 0)
    77 	{
    78 		return "RISC OS Joystick 0";
    79 	}
    80 
    81 	SDL_SetError("No joystick available with that index");
    82 	return(NULL);
    83 }
    84 
    85 /* Function to open a joystick for use.
    86    The joystick to open is specified by the index field of the joystick.
    87    This should fill the nbuttons and naxes fields of the joystick structure.
    88    It returns 0, or -1 if there is an error.
    89  */
    90 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
    91 {
    92 	if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata)))) {
    93 		SDL_OutOfMemory();
    94 		return -1;
    95 	}
    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 */