Skip to content

Commit

Permalink
Joystick support.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Feb 21, 2019
1 parent 69db974 commit bb8b62e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
94 changes: 92 additions & 2 deletions src/SDL12_compat.c
Expand Up @@ -702,6 +702,12 @@ typedef struct
SDL12_Rect **modes12; /* ptrs to each item in modeslist, for SDL_ListModes() */
} VideoModeList;

typedef struct
{
int device_index;
SDL_Joystick *joystick;
} JoystickOpenedItem;

// !!! FIXME: go through all of these.
static VideoModeList *VideoModes = NULL;
static int VideoModesCount = 0;
Expand All @@ -722,6 +728,7 @@ static SDL12_EventFilter EventFilter12 = NULL;
static SDL12_Cursor *CurrentCursor12 = NULL;
static Uint8 EventStates[SDL12_NUMEVENTS];
static int SwapInterval = 0;
static JoystickOpenedItem JoystickOpenList[16];

// !!! FIXME: need a mutex for the event queue.
#define SDL12_MAXEVENTS 128
Expand Down Expand Up @@ -843,10 +850,93 @@ SDL_Has3DNowExt(void)
return SDL20_HasSSE();
}

DECLSPEC SDL_Joystick * SDLCALL
SDL_JoystickOpen(int device_index)
{
int i;
SDL20_LockJoysticks();
for (i = 0; i < SDL_arraysize(JoystickOpenList); i++) {
if (JoystickOpenList[i].joystick == NULL) {
break;
}
}

if (i == SDL_arraysize(JoystickOpenList)) {
SDL20_UnlockJoysticks();
SDL20_SetError("Too many open joysticks");
return NULL;
}

JoystickOpenList[i].joystick = SDL20_JoystickOpen(device_index);
if (JoystickOpenList[i].joystick) {
JoystickOpenList[i].device_index = device_index;
}

SDL20_UnlockJoysticks();
return JoystickOpenList[i].joystick;
}

DECLSPEC void SDLCALL
SDL_JoystickClose(SDL_Joystick *joystick)
{
int i;
SDL20_LockJoysticks();
for (i = 0; i < SDL_arraysize(JoystickOpenList); i++) {
if (JoystickOpenList[i].joystick == joystick) {
break;
}
}

if (i < SDL_arraysize(JoystickOpenList)) {
JoystickOpenList[i].joystick = NULL;
}

SDL20_UnlockJoysticks();

SDL20_JoystickClose(joystick);
}

DECLSPEC const char * SDLCALL
SDL_JoystickName(int device_index)
{
return SDL20_JoystickNameForIndex(device_index);
}

DECLSPEC int SDLCALL
SDL_JoystickIndex(SDL_Joystick *joystick)
{
int i;

SDL20_LockJoysticks(); {
for (i = 0; i < SDL_arraysize(JoystickOpenList); i++) {
if (JoystickOpenList[i].joystick == joystick) {
break;
}
}

if (i < SDL_arraysize(JoystickOpenList)) {
SDL20_UnlockJoysticks();
return JoystickOpenList[i].device_index;
}

}
SDL20_UnlockJoysticks();
return SDL20_SetError("Can't find joystick");
}

DECLSPEC int SDLCALL SDL_JoystickOpened(int device_index)
{
FIXME("write me");
return 0;
int retval = 0;
int i;
SDL20_LockJoysticks();
for (i = 0; i < SDL_arraysize(JoystickOpenList); i++) {
if ((JoystickOpenList[i].joystick) && (JoystickOpenList[i].device_index == device_index)) {
retval = 1;
break;
}
}
SDL20_UnlockJoysticks();
return retval;
}

static int
Expand Down
10 changes: 6 additions & 4 deletions src/SDL20_syms.h
Expand Up @@ -188,9 +188,8 @@ SDL20_SYM_PASSTHROUGH(Uint32,GetTicks,(void),(),return)
SDL20_SYM_PASSTHROUGH(void,Delay,(Uint32 a),(a),)

SDL20_SYM_PASSTHROUGH(int,NumJoysticks,(void),(),return)
SDL20_SYM_PASSTHROUGH(const char *,JoystickName,(int a),(a),return)
SDL20_SYM_PASSTHROUGH(SDL_Joystick *,JoystickOpen,(int a),(a),return)
SDL20_SYM_PASSTHROUGH(int,JoystickIndex,(SDL_Joystick *a),(a),return)
SDL20_SYM(const char *,JoystickNameForIndex,(int a),(a),return)
SDL20_SYM(SDL_Joystick *,JoystickOpen,(int a),(a),return)
SDL20_SYM_PASSTHROUGH(int,JoystickNumAxes,(SDL_Joystick *a),(a),return)
SDL20_SYM_PASSTHROUGH(int,JoystickNumBalls,(SDL_Joystick *a),(a),return)
SDL20_SYM_PASSTHROUGH(int,JoystickNumHats,(SDL_Joystick *a),(a),return)
Expand All @@ -201,7 +200,10 @@ SDL20_SYM_PASSTHROUGH(Sint16,JoystickGetAxis,(SDL_Joystick *a, int b),(a,b),retu
SDL20_SYM_PASSTHROUGH(Uint8,JoystickGetHat,(SDL_Joystick *a, int b),(a,b),return)
SDL20_SYM_PASSTHROUGH(int,JoystickGetBall,(SDL_Joystick *a, int b, int *c, int *d),(a,b,c,d),return)
SDL20_SYM_PASSTHROUGH(Uint8,JoystickGetButton,(SDL_Joystick *a, int b),(a,b),return)
SDL20_SYM_PASSTHROUGH(void,JoystickClose,(SDL_Joystick *a),(a),return)
SDL20_SYM(void,JoystickClose,(SDL_Joystick *a),(a),return)
SDL20_SYM(void,LockJoysticks,(void),(),)
SDL20_SYM(void,UnlockJoysticks,(void),(),)
SDL20_SYM(SDL_JoystickID,JoystickGetDeviceInstanceID,(int a),(a),return)

SDL20_SYM(SDL_RWops *,RWFromFile,(const char *a, const char *b),(a,b),return)
SDL20_SYM(SDL_RWops *,RWFromFP,(FILE *a, int b),(a,b),return)
Expand Down

0 comments on commit bb8b62e

Please sign in to comment.