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_DC
26 #include "SDL_events.h"
27 #include "SDL_joystick.h"
28 #include "../SDL_sysjoystick.h"
29 #include "../SDL_joystick_c.h"
32 #include <dc/maple/controller.h>
34 #define MAX_JOYSTICKS 8 /* only 2 are supported in the multimedia API */
35 #define MAX_AXES 6 /* each joystick can have up to 6 axes */
36 #define MAX_BUTTONS 8 /* and 8 buttons */
41 /* array to hold joystick ID values */
42 static uint8 SYS_Joystick_addr[MAX_JOYSTICKS];
44 /* The private structure used to keep track of a joystick */
45 struct joystick_hwdata
47 cont_cond_t prev_cond;
51 /* Function to scan the system for joysticks.
52 * This function should set SDL_numjoysticks to the number of available
53 * joysticks. Joystick 0 should be the system default joystick.
54 * It should return 0, or -1 on an unrecoverable fatal error.
56 int SDL_SYS_JoystickInit(void)
63 for(p=0;p<MAPLE_PORT_COUNT;p++) {
64 for(u=0;u<MAPLE_UNIT_COUNT;u++) {
65 if (maple_device_func(p,u)&MAPLE_FUNC_CONTROLLER) {
66 SYS_Joystick_addr[numdevs] = maple_addr(p,u);
75 /* Function to get the device-dependent name of a joystick */
76 const char *SDL_SYS_JoystickName(int index)
79 if (maple_compat_resolve(SYS_Joystick_addr[index],&dev,MAPLE_FUNC_CONTROLLER)!=0) return NULL;
80 return dev->info.product_name;
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 /* allocate memory for system specific hardware data */
91 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
92 if (joystick->hwdata == NULL)
97 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
99 /* fill nbuttons, naxes, and nhats fields */
100 joystick->nbuttons = MAX_BUTTONS;
101 joystick->naxes = MAX_AXES;
102 joystick->nhats = MAX_HATS;
107 /* Function to update the state of a joystick - called as a device poll.
108 * This function shouldn't update the joystick structure directly,
109 * but instead should call SDL_PrivateJoystick*() to deliver events
110 * and update joystick device state.
113 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
115 const int sdl_buttons[] = {
127 cont_cond_t cond,*prev_cond;
128 int buttons,prev_buttons,i,changed;
130 addr = SYS_Joystick_addr[joystick->index];
131 if (cont_get_cond(addr,&cond)<0) return;
133 buttons = cond.buttons;
134 prev_buttons = joystick->hwdata->prev_buttons;
135 changed = buttons^prev_buttons;
137 if ((changed)&(CONT_DPAD_UP|CONT_DPAD_DOWN|CONT_DPAD_LEFT|CONT_DPAD_RIGHT)) {
138 int hat = SDL_HAT_CENTERED;
139 if (buttons&CONT_DPAD_UP) hat|=SDL_HAT_UP;
140 if (buttons&CONT_DPAD_DOWN) hat|=SDL_HAT_DOWN;
141 if (buttons&CONT_DPAD_LEFT) hat|=SDL_HAT_LEFT;
142 if (buttons&CONT_DPAD_RIGHT) hat|=SDL_HAT_RIGHT;
143 SDL_PrivateJoystickHat(joystick, 0, hat);
145 if ((changed)&(CONT_DPAD2_UP|CONT_DPAD2_DOWN|CONT_DPAD2_LEFT|CONT_DPAD2_RIGHT)) {
146 int hat = SDL_HAT_CENTERED;
147 if (buttons&CONT_DPAD2_UP) hat|=SDL_HAT_UP;
148 if (buttons&CONT_DPAD2_DOWN) hat|=SDL_HAT_DOWN;
149 if (buttons&CONT_DPAD2_LEFT) hat|=SDL_HAT_LEFT;
150 if (buttons&CONT_DPAD2_RIGHT) hat|=SDL_HAT_RIGHT;
151 SDL_PrivateJoystickHat(joystick, 1, hat);
154 for(i=0;i<sizeof(sdl_buttons)/sizeof(sdl_buttons[0]);i++) {
155 if (changed & sdl_buttons[i]) {
156 SDL_PrivateJoystickButton(joystick, i, (buttons & sdl_buttons[i])?SDL_PRESSED:SDL_RELEASED);
160 prev_cond = &joystick->hwdata->prev_cond;
161 if (cond.joyx!=prev_cond->joyx)
162 SDL_PrivateJoystickAxis(joystick, 0, cond.joyx-128);
163 if (cond.joyy!=prev_cond->joyy)
164 SDL_PrivateJoystickAxis(joystick, 1, cond.joyy-128);
165 if (cond.rtrig!=prev_cond->rtrig)
166 SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig);
167 if (cond.ltrig!=prev_cond->ltrig)
168 SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig);
169 if (cond.joy2x!=prev_cond->joy2x)
170 SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x-128);
171 if (cond.joy2y!=prev_cond->joy2y)
172 SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y-128);
174 joystick->hwdata->prev_buttons = buttons;
175 joystick->hwdata->prev_cond = cond;
178 /* Function to close a joystick after use */
179 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
181 if (joystick->hwdata != NULL) {
182 /* free system specific hardware data */
183 SDL_free(joystick->hwdata);
187 /* Function to perform any system-specific joystick related cleanup */
188 void SDL_SYS_JoystickQuit(void)
193 #endif /* SDL_JOYSTICK_DC */