comparison src/joystick/dc/SDL_sysjoystick.c @ 509:dad72daf44b3

Added initial support for Dreamcast (thanks HERO!)
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Oct 2002 16:50:56 +0000
parents
children b8d311d90021
comparison
equal deleted inserted replaced
508:9ff7e90aaa94 509:dad72daf44b3
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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 BERO
20 bero@geocities.co.jp
21
22 based on win32/SDL_mmjoystick.c
23
24 Sam Lantinga
25 slouken@libsdl.org
26 */
27
28 #ifdef SAVE_RCSID
29 static char rcsid =
30 "@(#) $Id$";
31 #endif
32
33 /* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */
34
35 #include <stdlib.h>
36 #include <stdio.h> /* For the definition of NULL */
37
38 #include "SDL_error.h"
39 #include "SDL_joystick.h"
40 #include "SDL_sysjoystick.h"
41 #include "SDL_joystick_c.h"
42
43 #include <dc/maple.h>
44 #include <dc/maple/controller.h>
45
46 #define MAX_JOYSTICKS 8 /* only 2 are supported in the multimedia API */
47 #define MAX_AXES 6 /* each joystick can have up to 6 axes */
48 #define MAX_BUTTONS 8 /* and 8 buttons */
49 #define MAX_HATS 2
50
51 #define JOYNAMELEN 8
52
53 /* array to hold joystick ID values */
54 static uint8 SYS_Joystick_addr[MAX_JOYSTICKS];
55
56 /* The private structure used to keep track of a joystick */
57 struct joystick_hwdata
58 {
59 cont_cond_t prev_cond;
60 int prev_buttons;
61 };
62
63 /* Function to scan the system for joysticks.
64 * This function should set SDL_numjoysticks to the number of available
65 * joysticks. Joystick 0 should be the system default joystick.
66 * It should return 0, or -1 on an unrecoverable fatal error.
67 */
68 int SDL_SYS_JoystickInit(void)
69 {
70 int numdevs;
71
72 int p,u;
73
74 numdevs = 0;
75 for(p=0;p<MAPLE_PORT_COUNT;p++) {
76 for(u=0;u<MAPLE_UNIT_COUNT;u++) {
77 if (maple_device_func(p,u)&MAPLE_FUNC_CONTROLLER) {
78 SYS_Joystick_addr[numdevs] = maple_addr(p,u);
79 numdevs++;
80 }
81 }
82 }
83
84 return(numdevs);
85 }
86
87 /* Function to get the device-dependent name of a joystick */
88 const char *SDL_SYS_JoystickName(int index)
89 {
90 maple_device_t *dev;
91 if (maple_compat_resolve(SYS_Joystick_addr[index],&dev,MAPLE_FUNC_CONTROLLER)!=0) return NULL;
92 return dev->info.product_name;
93 }
94
95 /* Function to open a joystick for use.
96 The joystick to open is specified by the index field of the joystick.
97 This should fill the nbuttons and naxes fields of the joystick structure.
98 It returns 0, or -1 if there is an error.
99 */
100 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
101 {
102 /* allocate memory for system specific hardware data */
103 joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
104 if (joystick->hwdata == NULL)
105 {
106 SDL_OutOfMemory();
107 return(-1);
108 }
109 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
110
111 /* fill nbuttons, naxes, and nhats fields */
112 joystick->nbuttons = MAX_BUTTONS;
113 joystick->naxes = MAX_AXES;
114 joystick->nhats = MAX_HATS;
115 return(0);
116 }
117
118
119 /* Function to update the state of a joystick - called as a device poll.
120 * This function shouldn't update the joystick structure directly,
121 * but instead should call SDL_PrivateJoystick*() to deliver events
122 * and update joystick device state.
123 */
124
125 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
126 {
127 const int sdl_buttons[] = {
128 CONT_C,
129 CONT_B,
130 CONT_A,
131 CONT_START,
132 CONT_Z,
133 CONT_Y,
134 CONT_X,
135 CONT_D
136 };
137
138 uint8 addr;
139 cont_cond_t cond,*prev_cond;
140 int buttons,prev_buttons,i,changed;
141
142 addr = SYS_Joystick_addr[joystick->index];
143 if (cont_get_cond(addr,&cond)<0) return;
144
145 buttons = cond.buttons;
146 prev_buttons = joystick->hwdata->prev_buttons;
147 changed = buttons^prev_buttons;
148
149 if ((changed)&(CONT_DPAD_UP|CONT_DPAD_DOWN|CONT_DPAD_LEFT|CONT_DPAD_RIGHT)) {
150 int hat = SDL_HAT_CENTERED;
151 if (buttons&CONT_DPAD_UP) hat|=SDL_HAT_UP;
152 if (buttons&CONT_DPAD_DOWN) hat|=SDL_HAT_DOWN;
153 if (buttons&CONT_DPAD_LEFT) hat|=SDL_HAT_LEFT;
154 if (buttons&CONT_DPAD_RIGHT) hat|=SDL_HAT_RIGHT;
155 SDL_PrivateJoystickHat(joystick, 0, hat);
156 }
157 if ((changed)&(CONT_DPAD2_UP|CONT_DPAD2_DOWN|CONT_DPAD2_LEFT|CONT_DPAD2_RIGHT)) {
158 int hat = SDL_HAT_CENTERED;
159 if (buttons&CONT_DPAD2_UP) hat|=SDL_HAT_UP;
160 if (buttons&CONT_DPAD2_DOWN) hat|=SDL_HAT_DOWN;
161 if (buttons&CONT_DPAD2_LEFT) hat|=SDL_HAT_LEFT;
162 if (buttons&CONT_DPAD2_RIGHT) hat|=SDL_HAT_RIGHT;
163 SDL_PrivateJoystickHat(joystick, 1, hat);
164 }
165
166 for(i=0;i<sizeof(sdl_buttons)/sizeof(sdl_buttons[0]);i++) {
167 if (changed & sdl_buttons[i]) {
168 SDL_PrivateJoystickButton(joystick, i, (buttons & sdl_buttons[i])?SDL_PRESSED:SDL_RELEASED);
169 }
170 }
171
172 prev_cond = &joystick->hwdata->prev_cond;
173 if (cond.joyx!=prev_cond->joyx)
174 SDL_PrivateJoystickAxis(joystick, 0, cond.joyx-128);
175 if (cond.joyy!=prev_cond->joyy)
176 SDL_PrivateJoystickAxis(joystick, 1, cond.joyy-128);
177 if (cond.rtrig!=prev_cond->rtrig)
178 SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig);
179 if (cond.ltrig!=prev_cond->ltrig)
180 SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig);
181 if (cond.joy2x!=prev_cond->joy2x)
182 SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x-128);
183 if (cond.joy2y!=prev_cond->joy2y)
184 SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y-128);
185
186 joystick->hwdata->prev_buttons = buttons;
187 joystick->hwdata->prev_cond = cond;
188 }
189
190 /* Function to close a joystick after use */
191 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
192 {
193 if (joystick->hwdata != NULL) {
194 /* free system specific hardware data */
195 free(joystick->hwdata);
196 }
197 }
198
199 /* Function to perform any system-specific joystick related cleanup */
200 void SDL_SYS_JoystickQuit(void)
201 {
202 return;
203 }