Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
216 lines (185 loc) · 6.35 KB

SDL_sysjoystick.c

File metadata and controls

216 lines (185 loc) · 6.35 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 14, 2006
Apr 14, 2006
24
25
#ifdef SDL_JOYSTICK_DC
Mar 2, 2006
Mar 2, 2006
26
#include "SDL_events.h"
27
#include "SDL_joystick.h"
Feb 16, 2006
Feb 16, 2006
28
29
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
30
31
32
33
#include <dc/maple.h>
#include <dc/maple/controller.h>
Jul 10, 2006
Jul 10, 2006
34
35
36
#define MAX_JOYSTICKS 8 /* only 2 are supported in the multimedia API */
#define MAX_AXES 6 /* each joystick can have up to 6 axes */
#define MAX_BUTTONS 8 /* and 8 buttons */
37
38
39
40
41
#define MAX_HATS 2
#define JOYNAMELEN 8
/* array to hold joystick ID values */
Jul 10, 2006
Jul 10, 2006
42
static uint8 SYS_Joystick_addr[MAX_JOYSTICKS];
43
44
45
46
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
Jul 10, 2006
Jul 10, 2006
47
48
cont_cond_t prev_cond;
int prev_buttons;
49
50
51
52
53
54
55
};
/* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
Jul 10, 2006
Jul 10, 2006
56
57
int
SDL_SYS_JoystickInit(void)
Jul 10, 2006
Jul 10, 2006
59
int numdevs;
Jul 10, 2006
Jul 10, 2006
61
int p, u;
Jul 10, 2006
Jul 10, 2006
63
64
65
66
67
68
69
70
71
numdevs = 0;
for (p = 0; p < MAPLE_PORT_COUNT; p++) {
for (u = 0; u < MAPLE_UNIT_COUNT; u++) {
if (maple_device_func(p, u) & MAPLE_FUNC_CONTROLLER) {
SYS_Joystick_addr[numdevs] = maple_addr(p, u);
numdevs++;
}
}
}
Jul 10, 2006
Jul 10, 2006
73
return (numdevs);
74
75
76
}
/* Function to get the device-dependent name of a joystick */
Jul 10, 2006
Jul 10, 2006
77
78
const char *
SDL_SYS_JoystickName(int index)
Jul 10, 2006
Jul 10, 2006
80
81
82
83
84
maple_device_t *dev;
if (maple_compat_resolve
(SYS_Joystick_addr[index], &dev, MAPLE_FUNC_CONTROLLER) != 0)
return NULL;
return dev->info.product_name;
85
86
87
88
89
90
91
}
/* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick.
This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error.
*/
Jul 10, 2006
Jul 10, 2006
92
93
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
Jul 10, 2006
Jul 10, 2006
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* allocate memory for system specific hardware data */
joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) {
SDL_OutOfMemory();
return (-1);
}
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
/* fill nbuttons, naxes, and nhats fields */
joystick->nbuttons = MAX_BUTTONS;
joystick->naxes = MAX_AXES;
joystick->nhats = MAX_HATS;
return (0);
109
110
111
112
113
114
115
116
117
}
/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
* and update joystick device state.
*/
Jul 10, 2006
Jul 10, 2006
118
119
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
Jul 10, 2006
Jul 10, 2006
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const int sdl_buttons[] = {
CONT_C,
CONT_B,
CONT_A,
CONT_START,
CONT_Z,
CONT_Y,
CONT_X,
CONT_D
};
uint8 addr;
cont_cond_t cond, *prev_cond;
int buttons, prev_buttons, i, changed;
addr = SYS_Joystick_addr[joystick->index];
if (cont_get_cond(addr, &cond) < 0)
return;
buttons = cond.buttons;
prev_buttons = joystick->hwdata->prev_buttons;
changed = buttons ^ prev_buttons;
if ((changed) &
(CONT_DPAD_UP | CONT_DPAD_DOWN | CONT_DPAD_LEFT | CONT_DPAD_RIGHT)) {
int hat = SDL_HAT_CENTERED;
if (buttons & CONT_DPAD_UP)
hat |= SDL_HAT_UP;
if (buttons & CONT_DPAD_DOWN)
hat |= SDL_HAT_DOWN;
if (buttons & CONT_DPAD_LEFT)
hat |= SDL_HAT_LEFT;
if (buttons & CONT_DPAD_RIGHT)
hat |= SDL_HAT_RIGHT;
SDL_PrivateJoystickHat(joystick, 0, hat);
}
if ((changed) &
(CONT_DPAD2_UP | CONT_DPAD2_DOWN | CONT_DPAD2_LEFT |
CONT_DPAD2_RIGHT)) {
int hat = SDL_HAT_CENTERED;
if (buttons & CONT_DPAD2_UP)
hat |= SDL_HAT_UP;
if (buttons & CONT_DPAD2_DOWN)
hat |= SDL_HAT_DOWN;
if (buttons & CONT_DPAD2_LEFT)
hat |= SDL_HAT_LEFT;
if (buttons & CONT_DPAD2_RIGHT)
hat |= SDL_HAT_RIGHT;
SDL_PrivateJoystickHat(joystick, 1, hat);
}
for (i = 0; i < sizeof(sdl_buttons) / sizeof(sdl_buttons[0]); i++) {
if (changed & sdl_buttons[i]) {
SDL_PrivateJoystickButton(joystick, i,
(buttons & sdl_buttons[i]) ?
SDL_PRESSED : SDL_RELEASED);
}
}
prev_cond = &joystick->hwdata->prev_cond;
if (cond.joyx != prev_cond->joyx)
SDL_PrivateJoystickAxis(joystick, 0, cond.joyx - 128);
if (cond.joyy != prev_cond->joyy)
SDL_PrivateJoystickAxis(joystick, 1, cond.joyy - 128);
if (cond.rtrig != prev_cond->rtrig)
SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig);
if (cond.ltrig != prev_cond->ltrig)
SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig);
if (cond.joy2x != prev_cond->joy2x)
SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x - 128);
if (cond.joy2y != prev_cond->joy2y)
SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y - 128);
joystick->hwdata->prev_buttons = buttons;
joystick->hwdata->prev_cond = cond;
196
197
198
}
/* Function to close a joystick after use */
Jul 10, 2006
Jul 10, 2006
199
200
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
Jul 10, 2006
Jul 10, 2006
202
203
204
205
if (joystick->hwdata != NULL) {
/* free system specific hardware data */
SDL_free(joystick->hwdata);
}
206
207
208
}
/* Function to perform any system-specific joystick related cleanup */
Jul 10, 2006
Jul 10, 2006
209
210
void
SDL_SYS_JoystickQuit(void)
Jul 10, 2006
Jul 10, 2006
212
return;
Apr 14, 2006
Apr 14, 2006
214
215
#endif /* SDL_JOYSTICK_DC */
Jul 10, 2006
Jul 10, 2006
216
/* vi: set ts=4 sw=4 expandtab: */