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>
May 28, 2006
May 28, 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 */
May 28, 2006
May 28, 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
{
May 28, 2006
May 28, 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.
*/
May 28, 2006
May 28, 2006
56
int
May 29, 2006
May 29, 2006
57
SDL_SYS_JoystickInit(void)
May 28, 2006
May 28, 2006
59
int numdevs;
May 28, 2006
May 28, 2006
61
int p, u;
May 28, 2006
May 28, 2006
63
64
65
numdevs = 0;
for (p = 0; p < MAPLE_PORT_COUNT; p++) {
for (u = 0; u < MAPLE_UNIT_COUNT; u++) {
May 29, 2006
May 29, 2006
66
67
if (maple_device_func(p, u) & MAPLE_FUNC_CONTROLLER) {
SYS_Joystick_addr[numdevs] = maple_addr(p, u);
May 28, 2006
May 28, 2006
68
69
70
71
numdevs++;
}
}
}
May 28, 2006
May 28, 2006
73
return (numdevs);
74
75
76
}
/* Function to get the device-dependent name of a joystick */
May 28, 2006
May 28, 2006
77
const char *
May 29, 2006
May 29, 2006
78
SDL_SYS_JoystickName(int index)
May 28, 2006
May 28, 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.
*/
May 28, 2006
May 28, 2006
92
int
May 29, 2006
May 29, 2006
93
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
May 28, 2006
May 28, 2006
95
96
/* allocate memory for system specific hardware data */
joystick->hwdata =
May 29, 2006
May 29, 2006
97
(struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
May 28, 2006
May 28, 2006
98
if (joystick->hwdata == NULL) {
May 29, 2006
May 29, 2006
99
SDL_OutOfMemory();
May 28, 2006
May 28, 2006
100
101
return (-1);
}
May 29, 2006
May 29, 2006
102
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
May 28, 2006
May 28, 2006
103
104
105
106
107
108
/* 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.
*/
May 28, 2006
May 28, 2006
118
void
May 29, 2006
May 29, 2006
119
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
May 28, 2006
May 28, 2006
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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];
May 29, 2006
May 29, 2006
137
if (cont_get_cond(addr, &cond) < 0)
May 28, 2006
May 28, 2006
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
May 29, 2006
May 29, 2006
155
SDL_PrivateJoystickHat(joystick, 0, hat);
May 28, 2006
May 28, 2006
156
157
158
159
160
161
162
163
164
165
166
167
168
}
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;
May 29, 2006
May 29, 2006
169
SDL_PrivateJoystickHat(joystick, 1, hat);
May 28, 2006
May 28, 2006
170
171
}
May 29, 2006
May 29, 2006
172
for (i = 0; i < sizeof(sdl_buttons) / sizeof(sdl_buttons[0]); i++) {
May 28, 2006
May 28, 2006
173
if (changed & sdl_buttons[i]) {
May 29, 2006
May 29, 2006
174
175
176
SDL_PrivateJoystickButton(joystick, i,
(buttons & sdl_buttons[i]) ?
SDL_PRESSED : SDL_RELEASED);
May 28, 2006
May 28, 2006
177
178
179
180
181
}
}
prev_cond = &joystick->hwdata->prev_cond;
if (cond.joyx != prev_cond->joyx)
May 29, 2006
May 29, 2006
182
SDL_PrivateJoystickAxis(joystick, 0, cond.joyx - 128);
May 28, 2006
May 28, 2006
183
if (cond.joyy != prev_cond->joyy)
May 29, 2006
May 29, 2006
184
SDL_PrivateJoystickAxis(joystick, 1, cond.joyy - 128);
May 28, 2006
May 28, 2006
185
if (cond.rtrig != prev_cond->rtrig)
May 29, 2006
May 29, 2006
186
SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig);
May 28, 2006
May 28, 2006
187
if (cond.ltrig != prev_cond->ltrig)
May 29, 2006
May 29, 2006
188
SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig);
May 28, 2006
May 28, 2006
189
if (cond.joy2x != prev_cond->joy2x)
May 29, 2006
May 29, 2006
190
SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x - 128);
May 28, 2006
May 28, 2006
191
if (cond.joy2y != prev_cond->joy2y)
May 29, 2006
May 29, 2006
192
SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y - 128);
May 28, 2006
May 28, 2006
193
194
195
joystick->hwdata->prev_buttons = buttons;
joystick->hwdata->prev_cond = cond;
196
197
198
}
/* Function to close a joystick after use */
May 28, 2006
May 28, 2006
199
void
May 29, 2006
May 29, 2006
200
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
May 28, 2006
May 28, 2006
202
203
if (joystick->hwdata != NULL) {
/* free system specific hardware data */
May 29, 2006
May 29, 2006
204
SDL_free(joystick->hwdata);
May 28, 2006
May 28, 2006
205
}
206
207
208
}
/* Function to perform any system-specific joystick related cleanup */
May 28, 2006
May 28, 2006
209
void
May 29, 2006
May 29, 2006
210
SDL_SYS_JoystickQuit(void)
May 28, 2006
May 28, 2006
212
return;
Apr 14, 2006
Apr 14, 2006
214
215
#endif /* SDL_JOYSTICK_DC */
May 28, 2006
May 28, 2006
216
/* vi: set ts=4 sw=4 expandtab: */