2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #ifdef SDL_JOYSTICK_RISCOS
27 RISC OS - Joystick support by Alan Buckley (alan_baa@hotmail.com) - 10 April 2003
29 Note: Currently assumes joystick is present if joystick module is loaded
30 and that there is one joystick with four buttons.
33 /* This is the system specific header for the SDL joystick API */
35 #include "SDL_events.h"
36 #include "SDL_joystick.h"
37 #include "../SDL_sysjoystick.h"
38 #include "../SDL_joystick_c.h"
42 #define JOYSTICK_READ 0x43F40
44 struct joystick_hwdata
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.
55 int SDL_SYS_JoystickInit(void)
57 _kernel_swi_regs regs;
59 /* Try to read joystick 0 */
61 if (_kernel_swi(JOYSTICK_READ, ®s, ®s) == NULL)
63 /* Switch works so assume we've got a joystick */
66 /* Switch fails so it looks like there's no joystick here */
71 /* Function to get the device-dependent name of a joystick */
72 const char *SDL_SYS_JoystickName(int index)
76 return "RISC OS Joystick 0";
79 SDL_SetError("No joystick available with that index");
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.
88 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
90 _kernel_swi_regs regs;
92 if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata))))
95 regs.r[0] = joystick->index;
97 /* Don't know how to get exact count of buttons so assume max of 4 for now */
103 joystick->hwdata->joystate=0;
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.
114 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
116 _kernel_swi_regs regs;
117 regs.r[0] = joystick->index;
119 if (_kernel_swi(JOYSTICK_READ, ®s, ®s) == NULL)
121 int newstate = regs.r[0];
122 int oldstate = joystick->hwdata->joystate;
123 if (newstate != oldstate)
125 if ((newstate & 0xFF) != (oldstate & 0xFF))
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 */
132 if ((newstate & 0xFF00) != (oldstate & 0xFF00))
134 int x = (regs.r[0] & 0xFF00) >> 8;
135 if (x >= 128) x -= 256;
136 SDL_PrivateJoystickAxis(joystick,0,x * 256);
139 if ((newstate & 0xFF0000) != (oldstate & 0xFF0000))
141 int buttons = (regs.r[0] & 0xFF0000) >> 16;
142 int oldbuttons = (oldstate & 0xFF0000) >> 16;
144 for (i = 0; i < joystick->nbuttons; i++)
146 if ((buttons & (1<<i)) != (oldbuttons & (1<<i)))
148 if (buttons & (1<<i)) SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
149 else SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
153 joystick->hwdata->joystate = newstate;
160 /* Function to close a joystick after use */
161 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
164 SDL_free(joystick->hwdata);
168 /* Function to perform any system-specific joystick related cleanup */
169 void SDL_SYS_JoystickQuit(void)
176 #endif /* SDL_JOYSTICK_RISCOS */