23 #ifdef SDL_JOYSTICK_WINMM
27 #include "../../core/windows/SDL_windows.h"
33 #include "../SDL_sysjoystick.h"
34 #include "../SDL_joystick_c.h"
36 #define MAX_JOYSTICKS 16
38 #define MAX_BUTTONS 32
39 #define AXIS_MIN -32768
40 #define AXIS_MAX 32767
42 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256)
43 #define JOY_BUTTON_FLAG(n) (1<<n)
47 static UINT SYS_JoystickID[MAX_JOYSTICKS];
48 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS];
49 static char *SYS_JoystickName[MAX_JOYSTICKS];
67 static void SetMMerror(
char *
function,
int code);
71 GetJoystickName(
int index,
const char *szRegKey)
88 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
89 hTopKey = HKEY_LOCAL_MACHINE;
90 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
91 if (regresult != ERROR_SUCCESS) {
92 hTopKey = HKEY_CURRENT_USER;
93 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
95 if (regresult != ERROR_SUCCESS) {
100 regsize =
sizeof(regname);
102 REGSTR_VAL_JOYOEMNAME);
104 RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, ®size);
107 if (regresult != ERROR_SUCCESS) {
114 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
115 if (regresult != ERROR_SUCCESS) {
120 regsize =
sizeof(regvalue);
122 RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0,
NULL, ®size);
123 if (regresult == ERROR_SUCCESS) {
128 regresult = RegQueryValueExA(hKey,
129 REGSTR_VAL_JOYOEMNAME, 0, 0,
130 (LPBYTE) name, ®size);
138 static int SDL_SYS_numjoysticks = 0;
155 for (i = 0; i < MAX_JOYSTICKS; ++
i) {
156 SYS_JoystickID[
i] = 0;
157 SYS_JoystickName[
i] =
NULL;
161 SDL_SYS_numjoysticks = 0;
162 maxdevs = joyGetNumDevs();
163 for (i = JOYSTICKID1; i < maxdevs && SDL_SYS_numjoysticks < MAX_JOYSTICKS; ++
i) {
165 joyinfo.dwSize =
sizeof(joyinfo);
166 joyinfo.dwFlags = JOY_RETURNALL;
167 result = joyGetPosEx(i, &joyinfo);
168 if (result == JOYERR_NOERROR) {
169 result = joyGetDevCaps(i, &joycaps,
sizeof(joycaps));
170 if (result == JOYERR_NOERROR) {
171 SYS_JoystickID[SDL_SYS_numjoysticks] =
i;
172 SYS_Joystick[SDL_SYS_numjoysticks] = joycaps;
173 SYS_JoystickName[SDL_SYS_numjoysticks] =
174 GetJoystickName(i, joycaps.szRegKey);
175 SDL_SYS_numjoysticks++;
179 return (SDL_SYS_numjoysticks);
184 return SDL_SYS_numjoysticks;
200 if (SYS_JoystickName[device_index] !=
NULL) {
201 return (SYS_JoystickName[device_index]);
203 return (SYS_Joystick[device_index].szPname);
222 int caps_flags[MAX_AXES - 2] =
223 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
224 int axis_min[MAX_AXES], axis_max[MAX_AXES];
228 index = device_index;
229 axis_min[0] = SYS_Joystick[
index].wXmin;
230 axis_max[0] = SYS_Joystick[
index].wXmax;
231 axis_min[1] = SYS_Joystick[
index].wYmin;
232 axis_max[1] = SYS_Joystick[
index].wYmax;
233 axis_min[2] = SYS_Joystick[
index].wZmin;
234 axis_max[2] = SYS_Joystick[
index].wZmax;
235 axis_min[3] = SYS_Joystick[
index].wRmin;
236 axis_max[3] = SYS_Joystick[
index].wRmax;
237 axis_min[4] = SYS_Joystick[
index].wUmin;
238 axis_max[4] = SYS_Joystick[
index].wUmax;
239 axis_min[5] = SYS_Joystick[
index].wVmin;
240 axis_max[5] = SYS_Joystick[
index].wVmax;
253 for (i = 0; i < MAX_AXES; ++
i) {
254 if ((i < 2) || (SYS_Joystick[index].wCaps & caps_flags[i - 2])) {
255 joystick->
hwdata->transaxis[
i].offset = AXIS_MIN - axis_min[
i];
256 joystick->
hwdata->transaxis[
i].scale =
257 (float) (AXIS_MAX - AXIS_MIN) / (axis_max[
i] - axis_min[
i]);
259 joystick->
hwdata->transaxis[
i].offset = 0;
260 joystick->
hwdata->transaxis[
i].scale = 1.0;
266 joystick->
naxes = SYS_Joystick[
index].wNumAxes;
267 if (SYS_Joystick[index].wCaps & JOYCAPS_HASPOV) {
287 if (value != JOY_POVCENTERED) {
288 if ((value > JOY_POVLEFT) || (value < JOY_POVRIGHT)) {
291 if ((value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD)) {
294 if ((value > JOY_POVRIGHT) && (value < JOY_POVLEFT)) {
297 if (value > JOY_POVBACKWARD) {
314 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
315 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
318 struct _transaxis *transaxis;
322 joyinfo.dwSize =
sizeof(joyinfo);
323 joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
324 if (!joystick->
hats) {
325 joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
327 result = joyGetPosEx(joystick->
hwdata->id, &joyinfo);
328 if (result != JOYERR_NOERROR) {
329 SetMMerror(
"joyGetPosEx", result);
334 pos[0] = joyinfo.dwXpos;
335 pos[1] = joyinfo.dwYpos;
336 pos[2] = joyinfo.dwZpos;
337 pos[3] = joyinfo.dwRpos;
338 pos[4] = joyinfo.dwUpos;
339 pos[5] = joyinfo.dwVpos;
341 transaxis = joystick->
hwdata->transaxis;
342 for (i = 0; i < joystick->
naxes; i++) {
343 if (joyinfo.dwFlags & flags[i]) {
345 (
int) (((
float) pos[i] +
346 transaxis[i].
offset) * transaxis[
i].scale);
347 change = (value - joystick->
axes[
i]);
348 if ((change < -JOY_AXIS_THRESHOLD)
349 || (change > JOY_AXIS_THRESHOLD)) {
356 if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
357 for (i = 0; i < joystick->
nbuttons; ++
i) {
358 if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
373 if (joyinfo.dwFlags & JOY_RETURNPOV) {
376 pos = TranslatePOV(joyinfo.dwPOV);
377 if (pos != joystick->
hats[0]) {
397 for (i = 0; i < MAX_JOYSTICKS; i++) {
399 SYS_JoystickName[
i] =
NULL;
417 const char *name = joystick->
name;
426 SetMMerror(
char *
function,
int code)
429 static char errbuf[1024];
433 case MMSYSERR_NODRIVER:
434 error =
"Joystick driver not present";
437 case MMSYSERR_INVALPARAM:
439 error =
"Invalid parameter(s)";
442 case MMSYSERR_BADDEVICEID:
443 error =
"Bad device ID";
446 case JOYERR_UNPLUGGED:
447 error =
"Joystick not attached";
451 error =
"Can't capture joystick input";
456 "%s: Unknown Multimedia system error: 0x%x",
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
int SDL_SYS_NumJoysticks()
DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt,...)
DECLSPEC void SDLCALL SDL_free(void *mem)
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
EGLImageKHR EGLint * name
void SDL_SYS_JoystickQuit(void)
typedef UINT(WINAPI *PFNWGLGETCONTEXTGPUIDAMDPROC)(HGLRC hglrc)
struct joystick_hwdata * hwdata
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len)
void SDL_SYS_JoystickDetect()
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
GLenum GLenum GLenum GLenum GLenum scale
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
SDL_bool SDL_SYS_JoystickNeedsPolling()
DECLSPEC size_t SDLCALL SDL_strlen(const char *str)
int SDL_SYS_JoystickInit(void)
#define SDL_OutOfMemory()
#define SDL_arraysize(array)
EGLSurface EGLint void ** value
DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len)
GLenum GLsizei GLsizei GLsizei GLsizei GLbitfield flags
uint8_t Uint8
An unsigned 8-bit integer type.
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
int16_t Sint16
A signed 16-bit integer type.
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
SDL_JoystickID instance_id
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)