21 #include "../../SDL_internal.h" 23 #ifdef SDL_JOYSTICK_WINMM 27 #include "../../core/windows/SDL_windows.h" 33 #include "../SDL_sysjoystick.h" 34 #include "../SDL_joystick_c.h" 36 #ifdef REGSTR_VAL_JOYOEMNAME 37 #undef REGSTR_VAL_JOYOEMNAME 39 #define REGSTR_VAL_JOYOEMNAME "OEMName" 41 #define MAX_JOYSTICKS 16 43 #define MAX_BUTTONS 32 44 #define AXIS_MIN -32768 45 #define AXIS_MAX 32767 47 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256) 48 #define JOY_BUTTON_FLAG(n) (1<<n) 52 static UINT SYS_JoystickID[MAX_JOYSTICKS];
54 static char *SYS_JoystickName[MAX_JOYSTICKS];
72 static void SetMMerror(
char *
function,
int code);
76 GetJoystickName(
int index,
const char *szRegKey)
93 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
94 hTopKey = HKEY_LOCAL_MACHINE;
95 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
96 if (regresult != ERROR_SUCCESS) {
97 hTopKey = HKEY_CURRENT_USER;
98 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
100 if (regresult != ERROR_SUCCESS) {
105 regsize =
sizeof(regname);
107 REGSTR_VAL_JOYOEMNAME);
109 RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, ®size);
112 if (regresult != ERROR_SUCCESS) {
119 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
120 if (regresult != ERROR_SUCCESS) {
125 regsize =
sizeof(regvalue);
127 RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0,
NULL, ®size);
128 if (regresult == ERROR_SUCCESS) {
133 regresult = RegQueryValueExA(hKey,
134 REGSTR_VAL_JOYOEMNAME, 0, 0,
135 (LPBYTE) name, ®size);
143 static int SDL_SYS_numjoysticks = 0;
159 for (i = 0; i < MAX_JOYSTICKS; ++
i) {
160 SYS_JoystickID[
i] = 0;
161 SYS_JoystickName[
i] =
NULL;
165 SDL_SYS_numjoysticks = 0;
166 maxdevs = joyGetNumDevs();
167 for (i = JOYSTICKID1; i < maxdevs && SDL_SYS_numjoysticks < MAX_JOYSTICKS; ++
i) {
169 joyinfo.dwSize =
sizeof(joyinfo);
170 joyinfo.dwFlags = JOY_RETURNALL;
171 result = joyGetPosEx(i, &joyinfo);
172 if (result == JOYERR_NOERROR) {
173 result = joyGetDevCapsA(i, &joycaps,
sizeof(joycaps));
174 if (result == JOYERR_NOERROR) {
175 SYS_JoystickID[SDL_SYS_numjoysticks] =
i;
177 SYS_JoystickName[SDL_SYS_numjoysticks] =
178 GetJoystickName(i, joycaps.szRegKey);
179 SDL_SYS_numjoysticks++;
183 return (SDL_SYS_numjoysticks);
188 return SDL_SYS_numjoysticks;
199 if (SYS_JoystickName[device_index] !=
NULL) {
200 return (SYS_JoystickName[device_index]);
221 int caps_flags[MAX_AXES - 2] =
222 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
223 int axis_min[MAX_AXES], axis_max[MAX_AXES];
227 index = device_index;
242 joystick->instance_id = device_index;
245 if (joystick->hwdata ==
NULL) {
248 SDL_memset(joystick->hwdata, 0,
sizeof(*joystick->hwdata));
251 joystick->hwdata->id = SYS_JoystickID[
index];
252 for (i = 0; i < MAX_AXES; ++
i) {
253 if ((i < 2) || (
SYS_Joystick[index].wCaps & caps_flags[i - 2])) {
254 joystick->hwdata->transaxis[
i].offset = AXIS_MIN - axis_min[
i];
255 joystick->hwdata->transaxis[
i].scale =
256 (float) (AXIS_MAX - AXIS_MIN) / (axis_max[
i] - axis_min[
i]);
258 joystick->hwdata->transaxis[
i].offset = 0;
259 joystick->hwdata->transaxis[
i].scale = 1.0;
281 TranslatePOV(DWORD
value)
286 if (value != JOY_POVCENTERED) {
287 if ((value > JOY_POVLEFT) || (value < JOY_POVRIGHT)) {
290 if ((value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD)) {
293 if ((value > JOY_POVRIGHT) && (value < JOY_POVLEFT)) {
296 if (value > JOY_POVBACKWARD) {
313 DWORD
flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
314 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
317 struct _transaxis *transaxis;
321 joyinfo.dwSize =
sizeof(joyinfo);
322 joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
323 if (!joystick->hats) {
324 joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
326 result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
327 if (result != JOYERR_NOERROR) {
328 SetMMerror(
"joyGetPosEx", result);
333 pos[0] = joyinfo.dwXpos;
334 pos[1] = joyinfo.dwYpos;
335 pos[2] = joyinfo.dwZpos;
336 pos[3] = joyinfo.dwRpos;
337 pos[4] = joyinfo.dwUpos;
338 pos[5] = joyinfo.dwVpos;
340 transaxis = joystick->hwdata->transaxis;
341 for (i = 0; i < joystick->naxes; i++) {
342 if (joyinfo.dwFlags & flags[i]) {
344 (int) (((
float) pos[i] +
345 transaxis[i].
offset) * transaxis[
i].scale);
346 change = (value - joystick->axes[
i]);
347 if ((change < -JOY_AXIS_THRESHOLD)
348 || (change > JOY_AXIS_THRESHOLD)) {
355 if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
356 for (i = 0; i < joystick->nbuttons; ++
i) {
357 if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
358 if (!joystick->buttons[i]) {
363 if (joystick->buttons[i]) {
372 if (joyinfo.dwFlags & JOY_RETURNPOV) {
375 pos = TranslatePOV(joyinfo.dwPOV);
376 if (pos != joystick->hats[0]) {
394 for (i = 0; i < MAX_JOYSTICKS; i++) {
396 SYS_JoystickName[
i] =
NULL;
414 const char *name = joystick->name;
423 SetMMerror(
char *
function,
int code)
426 static char errbuf[1024];
430 case MMSYSERR_NODRIVER:
431 error =
"Joystick driver not present";
434 case MMSYSERR_INVALPARAM:
436 error =
"Invalid parameter(s)";
439 case MMSYSERR_BADDEVICEID:
440 error =
"Bad device ID";
443 case JOYERR_UNPLUGGED:
444 error =
"Joystick not attached";
448 error =
"Can't capture joystick input";
453 "%s: Unknown Multimedia system error: 0x%x",
GLenum GLenum GLenum GLenum GLenum scale
JoyStick_DeviceData * SYS_Joystick
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
int SDL_SYS_NumJoysticks()
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
GLuint const GLchar * name
void SDL_SYS_JoystickQuit(void)
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
GLsizei const GLfloat * value
uint8_t Uint8
An unsigned 8-bit integer type.
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
void SDL_SYS_JoystickDetect()
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
int SDL_SYS_JoystickInit(void)
#define SDL_OutOfMemory()
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
#define SDL_arraysize(array)
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
int16_t Sint16
A signed 16-bit integer type.