21 #include "../SDL_internal.h" 31 #if !SDL_EVENTS_DISABLED 32 #include "../events/SDL_events_c.h" 34 #include "../video/SDL_sysvideo.h" 45 if (SDL_joystick_lock) {
53 if (SDL_joystick_lock) {
62 if (hint && *hint ==
'1') {
77 if (!SDL_joystick_lock) {
85 #if !SDL_EVENTS_DISABLED 128 static Uint32 zero_centered_joysticks[] = {
139 if (joystick->naxes == 2) {
145 if (
id == zero_centered_joysticks[i]) {
163 SDL_Joystick *joysticklist;
164 const char *joystickname =
NULL;
177 while (joysticklist) {
179 joystick = joysticklist;
180 ++joystick->ref_count;
184 joysticklist = joysticklist->next;
188 joystick = (SDL_Joystick *)
SDL_calloc(
sizeof(*joystick), 1);
189 if (joystick ==
NULL) {
205 joystick->name =
NULL;
207 if (joystick->naxes > 0) {
210 if (joystick->nhats > 0) {
213 if (joystick->nballs > 0) {
214 joystick->balls = (
struct balldelta *)
SDL_calloc(joystick->nballs,
sizeof(*joystick->balls));
216 if (joystick->nbuttons > 0) {
219 if (((joystick->naxes > 0) && !joystick->axes)
220 || ((joystick->nhats > 0) && !joystick->hats)
221 || ((joystick->nballs > 0) && !joystick->balls)
222 || ((joystick->nbuttons > 0) && !joystick->buttons)) {
234 for (i = 0; i < joystick->naxes; ++
i) {
235 joystick->axes[
i].has_initial_value =
SDL_TRUE;
242 ++joystick->ref_count;
263 if (joystick ==
NULL) {
282 return (joystick->naxes);
294 return (joystick->nhats);
306 return (joystick->nballs);
318 return (joystick->nbuttons);
332 if (axis < joystick->naxes) {
333 state = joystick->axes[
axis].value;
335 SDL_SetError(
"Joystick only has %d axes", joystick->naxes);
350 if (axis >= joystick->naxes) {
351 SDL_SetError(
"Joystick only has %d axes", joystick->naxes);
355 *state = joystick->axes[
axis].initial_value;
357 return joystick->axes[
axis].has_initial_value;
371 if (hat < joystick->nhats) {
372 state = joystick->hats[hat];
374 SDL_SetError(
"Joystick only has %d hats", joystick->nhats);
393 if (ball < joystick->nballs) {
395 *dx = joystick->balls[ball].dx;
398 *dy = joystick->balls[ball].dy;
400 joystick->balls[ball].dx = 0;
401 joystick->balls[ball].dy = 0;
403 return SDL_SetError(
"Joystick only has %d balls", joystick->nballs);
419 if (button < joystick->nbuttons) {
420 state = joystick->buttons[
button];
422 SDL_SetError(
"Joystick only has %d buttons", joystick->nbuttons);
452 return (joystick->instance_id);
461 SDL_Joystick *joystick;
464 for (joystick =
SDL_joysticks; joystick; joystick = joystick->next) {
465 if (joystick->instance_id == joyid) {
484 return (joystick->name);
493 SDL_Joystick *joysticklist;
494 SDL_Joystick *joysticklistprev;
503 if (--joystick->ref_count > 0) {
514 joystick->hwdata =
NULL;
517 joysticklistprev =
NULL;
518 while (joysticklist) {
519 if (joystick == joysticklist) {
520 if (joysticklistprev) {
522 joysticklistprev->next = joysticklist->next;
528 joysticklistprev = joysticklist;
529 joysticklist = joysticklist->next;
563 #if !SDL_EVENTS_DISABLED 570 if (SDL_joystick_lock) {
572 SDL_joystick_lock =
NULL;
597 #if !SDL_EVENTS_DISABLED 603 event.jdevice.which = device_index;
620 if (num_events <= 0) {
630 for (i = 0; i < num_events; ++
i) {
640 #if !SDL_EVENTS_DISABLED 646 event.jdevice.which = device_instance;
660 if (axis >= joystick->naxes) {
663 if (!joystick->axes[axis].has_initial_value) {
664 joystick->axes[
axis].initial_value =
value;
669 if (value == joystick->axes[axis].value) {
672 if (!joystick->axes[axis].sent_initial_value) {
675 if (
SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
687 if ((value > joystick->axes[axis].zero && value >= joystick->axes[axis].value) ||
698 #if !SDL_EVENTS_DISABLED 702 event.jaxis.which = joystick->instance_id;
703 event.jaxis.axis =
axis;
704 event.jaxis.value =
value;
717 if (hat >= joystick->nhats) {
720 if (value == joystick->hats[hat]) {
734 joystick->hats[hat] =
value;
738 #if !SDL_EVENTS_DISABLED 742 event.jhat.which = joystick->instance_id;
743 event.jhat.hat = hat;
744 event.jhat.value =
value;
758 if (ball >= joystick->nballs) {
768 joystick->balls[ball].dx += xrel;
769 joystick->balls[ball].dy += yrel;
773 #if !SDL_EVENTS_DISABLED 777 event.jball.which = joystick->instance_id;
778 event.jball.ball = ball;
779 event.jball.xrel = xrel;
780 event.jball.yrel = yrel;
791 #if !SDL_EVENTS_DISABLED 808 if (button >= joystick->nbuttons) {
811 if (state == joystick->buttons[button]) {
828 #if !SDL_EVENTS_DISABLED 830 event.jbutton.which = joystick->instance_id;
831 event.jbutton.button =
button;
832 event.jbutton.state =
state;
842 SDL_Joystick *joystick;
857 for (joystick =
SDL_joysticks; joystick; joystick = joystick->next) {
860 if (joystick->force_recentering) {
864 for (i = 0; i < joystick->naxes; i++) {
865 if (joystick->axes[i].has_initial_value) {
870 for (i = 0; i < joystick->nbuttons; i++) {
874 for (i = 0; i < joystick->nhats; i++) {
887 for (joystick =
SDL_joysticks; joystick; joystick = joystick->next) {
888 if (joystick->ref_count <= 0) {
904 #if SDL_EVENTS_DISABLED 907 const Uint32 event_list[] = {
939 guid16[1] == 0x0000 &&
941 guid16[3] == 0x0000 &&
950 *product = guid16[4];
953 *version = guid16[6];
970 static Uint32 wheel_joysticks[] = {
988 if (vidpid == wheel_joysticks[i]) {
997 static Uint32 flightstick_joysticks[] = {
1004 if (vidpid == flightstick_joysticks[i]) {
1013 static Uint32 throttle_joysticks[] = {
1020 if (vidpid == throttle_joysticks[i]) {
1033 if (guid.
data[14] ==
'x') {
1035 switch (guid.
data[15]) {
1183 if (joystick && joystick->is_game_controller) {
1193 static const char k_rgchHexToASCII[] =
"0123456789abcdef";
1196 if ((pszGUID ==
NULL) || (cbGUID <= 0)) {
1200 for (i = 0; i <
sizeof(guid.
data) && i < (cbGUID-1)/2; i++) {
1203 unsigned char c = guid.
data[
i];
1205 *pszGUID++ = k_rgchHexToASCII[c >> 4];
1206 *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
1219 if ((c >=
'0') && (c <=
'9')) {
1220 return (
unsigned char)(c -
'0');
1223 if ((c >=
'A') && (c <=
'F')) {
1224 return (
unsigned char)(c -
'A' + 0x0a);
1227 if ((c >=
'a') && (c <=
'f')) {
1228 return (
unsigned char)(c -
'a' + 0x0a);
1241 int maxoutputbytes=
sizeof(guid);
1252 for (i = 0; (i <
len) && ((p - (
Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
1263 joystick->epowerlevel = ePowerLevel;
1273 return joystick->epowerlevel;
static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid)
Uint16 SDL_JoystickGetVendor(SDL_Joystick *joystick)
void SDL_JoystickUpdate(void)
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
void SDL_LockJoysticks(void)
int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
SDL_JoyDeviceEvent jdevice
GLuint GLfloat GLfloat GLfloat x1
SDL_bool SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
#define MAKE_VIDPID(VID, PID)
void SDL_JoystickClose(SDL_Joystick *joystick)
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
Uint16 SDL_JoystickGetProduct(SDL_Joystick *joystick)
void SDL_GameControllerQuitMappings(void)
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
int SDL_GameControllerInitMappings(void)
#define SDL_IsGameController
static SDL_mutex * SDL_joystick_lock
#define SDL_QuitSubSystem
int SDL_JoystickInit(void)
static SDL_Event events[EVENT_BUF_SIZE]
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
int SDL_JoystickNumHats(SDL_Joystick *joystick)
#define SDL_InitSubSystem
Uint16 SDL_JoystickGetDeviceProduct(int device_index)
Uint16 SDL_JoystickGetProductVersion(SDL_Joystick *joystick)
void SDL_SYS_JoystickQuit(void)
#define SDL_GetKeyboardFocus
Uint16 SDL_JoystickGetDeviceProductVersion(int device_index)
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
GLuint const GLchar * name
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick)
static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid)
static void UpdateEventsForDeviceRemoval()
const char * SDL_JoystickName(SDL_Joystick *joystick)
int SDL_JoystickEventState(int state)
#define SDL_JOYSTICK_AXIS_MAX
int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
int SDL_NumJoysticks(void)
static SDL_bool SDL_joystick_allows_background_events
#define SDL_GetEventState(type)
#define SDL_stack_alloc(type, count)
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
GLsizei const GLfloat * value
void SDL_PrivateJoystickAdded(int device_index)
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
SDL_Joystick * SDL_JoystickOpen(int device_index)
void SDL_UnlockJoysticks(void)
int SDL_JoystickNumAxes(SDL_Joystick *joystick)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
SDL_Joystick * SDL_JoystickFromInstanceID(SDL_JoystickID joyid)
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)
#define SDL_assert(condition)
int SDL_SYS_JoystickInit(void)
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
int SDL_JoystickNumBalls(SDL_Joystick *joystick)
#define SDL_OutOfMemory()
int SDL_SYS_NumJoysticks(void)
static unsigned char nibble(char c)
SDL_JoystickID SDL_JoystickInstanceID(SDL_Joystick *joystick)
SDL_bool SDL_HasWindows(void)
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
void SDL_PrivateJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
#define SDL_AddHintCallback
int SDL_JoystickNumButtons(SDL_Joystick *joystick)
#define SDL_DelHintCallback
void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
GLuint GLuint GLsizei GLenum type
static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
#define SDL_arraysize(array)
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat)
void SDL_JoystickQuit(void)
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
#define SDL_stack_free(data)
SDL_bool SDL_JoystickGetAttached(SDL_Joystick *joystick)
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
Uint16 SDL_JoystickGetDeviceVendor(int device_index)
SDL_JoystickType SDL_JoystickGetDeviceType(int device_index)
static SDL_bool SDL_updating_joystick
SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
const char * SDL_JoystickNameForIndex(int device_index)
static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid)
static SDL_Joystick * SDL_joysticks
void SDL_SYS_JoystickDetect(void)
Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)