SDL2 joystick code cleanup

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@6011 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 5b5dc4a854c28a7204c4de15bee398231b84bd73
Former-commit-id: 3f81dc2295055e8c9caa1e186215495e6c737b96
This commit is contained in:
beaglejoe 2015-05-19 18:28:37 +00:00
parent 5488fb685f
commit 433a869f1c
5 changed files with 105 additions and 51 deletions

View file

@ -138,6 +138,7 @@ static int gfctrlJoyPresent = GFCTRL_JOY_UNTESTED;
#if SDL_JOYSTICK
static SDL_Joystick *Joysticks[GFCTRL_JOY_NUMBER] = {NULL};
static tCtrlJoyInfo *joyInfoCopy = NULL;
//static tCtrlJoyInfo joyInfo;
#if SDL_MAJOR_VERSION >= 2
static SDL_Haptic *Haptics[GFCTRL_JOY_NUMBER] = {NULL};
static SDL_HapticEffect cfx[GFCTRL_JOY_NUMBER];
@ -312,8 +313,7 @@ gfctrlJoyInit(void)
#if SDL_MAJOR_VERSION >= 2
memset(&cfx, 0, sizeof(cfx));
SDL_SetHint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1");
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_TIMER) < 0) {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
#else
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
#endif
@ -321,7 +321,10 @@ gfctrlJoyInit(void)
gfctrlJoyPresent = GFCTRL_JOY_UNTESTED;
return;
}
#if SDL_MAJOR_VERSION >= 2
// Ignore the joystick events, we will poll directly as it is faster
SDL_JoystickEventState(SDL_IGNORE);
#endif
gfctrlJoyPresent = SDL_NumJoysticks();
if (gfctrlJoyPresent > GFCTRL_JOY_NUMBER) gfctrlJoyPresent = GFCTRL_JOY_NUMBER;
@ -344,7 +347,7 @@ gfctrlJoyInit(void)
if (!Haptics[index]) {
GfLogInfo("Joystick %d does not support haptic\n", index);
break;
#if 1
#if 0
} else {
// add an CF effect on startup
gfctrlJoyConstantForce(index, 50000, 9000);
@ -355,7 +358,7 @@ gfctrlJoyInit(void)
if (SDL_HapticRumbleSupported(Haptics[index]) == SDL_TRUE) {
if (SDL_HapticRumbleInit(Haptics[index]) != 0)
GfLogError("Couldn't init rumble on joystick %d: %s\n", index, SDL_GetError());
#if 1
#if 0
else
gfctrlJoyRumble(index, 0.5);
#endif
@ -460,17 +463,11 @@ tCtrlJoyInfo *
GfctrlJoyCreate(void)
{
if (gfctrlJoyPresent == GFCTRL_JOY_UNTESTED)
gfctrlJoyInit();
#if SDL_JOYSTICK
if (joyInfoCopy != NULL) return joyInfoCopy;
#endif
gfctrlJoyInit();
tCtrlJoyInfo* joyInfo = (tCtrlJoyInfo *)calloc(1, sizeof(tCtrlJoyInfo));
#if SDL_JOYSTICK
joyInfoCopy = joyInfo;
#endif
return joyInfo;
}
@ -483,10 +480,7 @@ GfctrlJoyCreate(void)
void
GfctrlJoyRelease(tCtrlJoyInfo *joyInfo)
{
free(joyInfo);
#if SDL_JOYSTICK
joyInfoCopy = NULL;
#endif
freez(joyInfo);
}
@ -508,6 +502,63 @@ GfctrlJoyIsAnyPresent(void)
return gfctrlJoyPresent;
}
int
GfctrlSDL2JoyGetCurrentStates(tCtrlJoyInfo *joyInfo)
{
int ind;
int i,j;
unsigned int b;
unsigned int mask;
if (gfctrlJoyPresent == GFCTRL_JOY_PRESENT)
{
// Update all the joysticks
SDL_JoystickUpdate();
for (ind = 0; ind < gfctrlJoyPresent; ind++)
{
if (Joysticks[ind])
{
j = SDL_JoystickNumAxes(Joysticks[ind]);
if (j > GFCTRL_JOY_MAX_AXES) j = GFCTRL_JOY_MAX_AXES;
for (i=0; i < j;i++)
joyInfo->ax[GFCTRL_JOY_MAX_AXES * ind + i] = ((float) SDL_JoystickGetAxis(Joysticks[ind],i)) / 32768;
b = 0;
for (i=0; i < GFCTRL_JOY_MAX_BUTTONS;i++)
{
mask = (unsigned int)SDL_JoystickGetButton(Joysticks[ind], i);
b |= (mask << i);
}
/* Joystick buttons */
for (i = 0, mask = 1; i < GFCTRL_JOY_MAX_BUTTONS; i++, mask *= 2)
{
if (((b & mask) != 0) && ((joyInfo->oldb[ind] & mask) == 0)) {
joyInfo->edgeup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
} else {
joyInfo->edgeup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
}
if (((b & mask) == 0) && ((joyInfo->oldb[ind] & mask) != 0)) {
joyInfo->edgedn[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
} else {
joyInfo->edgedn[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
}
if ((b & mask) != 0) {
joyInfo->levelup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
} else {
joyInfo->levelup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
}
}
joyInfo->oldb[ind] = b;
}
}
}
else
{
return -1;
}
return 0;
}
/** Get the current state of the joysticks
@ingroup ctrl
@ -519,6 +570,9 @@ GfctrlJoyIsAnyPresent(void)
int
GfctrlJoyGetCurrentStates(tCtrlJoyInfo *joyInfo)
{
#if SDL_MAJOR_VERSION >= 2
return GfctrlSDL2JoyGetCurrentStates(joyInfo);
#endif
int ind;
#ifndef SDL_JOYSTICK
int i;

View file

@ -130,7 +130,7 @@ gfuiInit(void)
glUseProgram = (PFNGLUSEPROGRAMOBJECTARBPROC)wglGetProcAddress("glUseProgram");
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB");
#endif
//gfctrlJoyInit(); // Not here ; done later on the fly, when really needed.
gfctrlJoyInit(); // Not here ; done later on the fly, when really needed.
}
void
@ -658,7 +658,7 @@ GfuiScreenActivate(void *screen)
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
else
SDL_EnableKeyRepeat(0, 0);
#else
//#else
#if SDL_JOYSTICK
GfuiApp().eventLoop().setJoystickAxisCB(GfctrlJoySetAxis);
GfuiApp().eventLoop().setJoystickButtonCB(GfctrlJoySetButton);

View file

@ -114,7 +114,7 @@ static tCmdDispInfo CmdDispInfo[] = {
};
#if SDL_JOYSTICK
static tCtrlJoyInfo *joyInfo = NULL;
static tCtrlJoyInfo joyInfo;// = NULL;
static tCtrlJoyInfo joyCenter;
#else
static jsJoystick *Joystick[GFCTRL_JOY_NUMBER];
@ -210,7 +210,7 @@ onQuit(void *prevMenu)
{
/* Release joysticks */
#if SDL_JOYSTICK
GfctrlJoyRelease(joyInfo);
// GfctrlJoyRelease(joyInfo);
#else
for (int jsInd = 0; jsInd < GFCTRL_JOY_NUMBER; jsInd++)
if (Joystick[jsInd]) {
@ -346,8 +346,8 @@ getMovedAxis(int joy_number)
for (i = GFCTRL_JOY_MAX_AXES * joy_number; i < GFCTRL_JOY_MAX_AXES * (joy_number+1); i++) {
#if SDL_JOYSTICK
if (maxDiff < fabs(joyInfo->ax[i] - joyCenter.ax[i])) {
maxDiff = fabs(joyInfo->ax[i] - joyCenter.ax[i]);
if (maxDiff < fabs(joyInfo.ax[i] - joyCenter.ax[i])) {
maxDiff = fabs(joyInfo.ax[i] - joyCenter.ax[i]);
#else
if (maxDiff < fabs(JoyAxis[i] - JoyAxisCenter[i])) {
maxDiff = fabs(JoyAxis[i] - JoyAxisCenter[i]);
@ -412,7 +412,7 @@ IdleWaitForInput(void)
/* Check for a Joystick button pressed */
#if SDL_JOYSTICK
GfctrlJoyGetCurrentStates(joyInfo);
GfctrlJoyGetCurrentStates(&joyInfo);
#endif
for (index = 0; index < GFCTRL_JOY_NUMBER; index++) {
#ifndef SDL_JOYSTICK
@ -427,7 +427,7 @@ IdleWaitForInput(void)
if (axis != -1 && Cmd[CurrentCmd].pref != HM_ATT_JOY_REQ_AXIS) {
GfSleep(0.3);
#if SDL_JOYSTICK
GfctrlJoyGetCurrentStates(joyInfo);
GfctrlJoyGetCurrentStates(&joyInfo);
#else
Joystick[index]->read(&b, &JoyAxis[index * GFCTRL_JOY_MAX_AXES]);
#endif
@ -436,7 +436,7 @@ IdleWaitForInput(void)
/* Joystick buttons */
#if SDL_JOYSTICK
for (i = 0; i < GFCTRL_JOY_MAX_BUTTONS; i++) {
if (joyInfo->levelup[i + GFCTRL_JOY_MAX_BUTTONS * index]) {
if (joyInfo.levelup[i + GFCTRL_JOY_MAX_BUTTONS * index]) {
#else
for (i = 0, mask = 1; i < 32; i++, mask *= 2) {
if (((b & mask) != 0) && ((JoyButtons[index] & mask) == 0)) {
@ -445,7 +445,7 @@ IdleWaitForInput(void)
if (axis == -1 && Cmd[CurrentCmd].pref != HM_ATT_JOY_REQ_BUT) {
GfSleep(0.3);
#if SDL_JOYSTICK
GfctrlJoyGetCurrentStates(joyInfo);
GfctrlJoyGetCurrentStates(&joyInfo);
#else
Joystick[index]->read(&b, &JoyAxis[index * GFCTRL_JOY_MAX_AXES]);
#endif
@ -575,8 +575,8 @@ onPush(void *vi)
/* Read initial joysticks status */
#if SDL_JOYSTICK
GfctrlJoyGetCurrentStates(joyInfo);
memcpy(&joyCenter, joyInfo, sizeof(joyCenter));
GfctrlJoyGetCurrentStates(&joyInfo);
memcpy(&joyCenter, &joyInfo, sizeof(joyCenter));
#else
for (index = 0; index < GFCTRL_JOY_NUMBER; index++)
if (Joystick[index])
@ -592,8 +592,8 @@ static void
onActivate(void * /* dummy */)
{
#if SDL_JOYSTICK
joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(joyInfo);
//joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(&joyInfo);
#else
// Create and test joysticks ; only keep the up and running ones.
for (int jsInd = 0; jsInd < GFCTRL_JOY_NUMBER; jsInd++) {

View file

@ -60,7 +60,7 @@ static int MaxCmd;
// Joystick info.
#if SDL_JOYSTICK
static tCtrlJoyInfo *joyInfo = NULL;
static tCtrlJoyInfo joyInfo;// = NULL;
static tCtrlJoyInfo joyCenter;
#else
static jsJoystick* Joystick[GFCTRL_JOY_NUMBER];
@ -102,7 +102,7 @@ onNext(void * /* dummy */)
/* Release up and running joysticks */
#if SDL_JOYSTICK
GfctrlJoyRelease(joyInfo);
//GfctrlJoyRelease(joyInfo);
#else
for (index = 0; index < GFCTRL_JOY_NUMBER; index++)
if (Joystick[index]) {
@ -186,7 +186,7 @@ JoyCalAutomaton(void)
case 0:
/* Grab snapshot of 'NULL' position */
#if SDL_JOYSTICK
memcpy(&joyCenter, joyInfo, sizeof(joyCenter));
memcpy(&joyCenter, &joyInfo, sizeof(joyCenter));
#else
memcpy(JoyAxisCenter, JoyAxis, sizeof(JoyAxisCenter));
#endif
@ -215,7 +215,7 @@ JoyCalAutomaton(void)
new_in_list = (linked_item_t*)malloc(sizeof(linked_item_t));
new_in_list->command = AtobCount;
#if SDL_JOYSTICK
new_in_list->value = joyInfo->ax[AtobAxis];
new_in_list->value = joyInfo.ax[AtobAxis];
#else
new_in_list->value = JoyAxis[AtobAxis];
#endif
@ -293,9 +293,9 @@ Idle2(void)
int index;
#if SDL_JOYSTICK
/* Check for activity on Joystick buttons */
GfctrlJoyGetCurrentStates(joyInfo);
GfctrlJoyGetCurrentStates(&joyInfo);
for (index = 0; index < GFCTRL_JOY_NUMBER * GFCTRL_JOY_MAX_BUTTONS; index++) {
if (joyInfo->edgedn[index]) {
if (joyInfo.edgedn[index]) {
/* Check whether to ignore */
if(Cmd[CalState + CmdOffset].butIgnore == index)
break;
@ -351,8 +351,8 @@ onActivate(void * /* dummy */)
int index;
#if SDL_JOYSTICK
joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(joyInfo);
//joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(&joyInfo);
#else
// Create and test joysticks ; only keep the up and running ones.
for (index = 0; index < GFCTRL_JOY_NUMBER; index++) {

View file

@ -65,7 +65,7 @@ static int MaxCmd;
// Joystick info.
#if SDL_JOYSTICK
static tCtrlJoyInfo *joyInfo = NULL;
static tCtrlJoyInfo joyInfo;// = NULL;
static tCtrlJoyInfo joyCenter;
#else
static jsJoystick* Joystick[GFCTRL_JOY_NUMBER];
@ -96,7 +96,7 @@ static void
onNext(void * /* dummy */)
{
#if SDL_JOYSTICK
GfctrlJoyRelease(joyInfo);
// GfctrlJoyRelease(joyInfo);
#else
int index;
@ -133,7 +133,7 @@ JoyCalAutomaton(void)
switch (CalState) {
case 0:
#if SDL_JOYSTICK
memcpy(&joyCenter, joyInfo, sizeof(joyCenter));
memcpy(&joyCenter, &joyInfo, sizeof(joyCenter));
#else
memcpy(JoyAxisCenter, JoyAxis, sizeof(JoyAxisCenter));
#endif
@ -143,7 +143,7 @@ JoyCalAutomaton(void)
axis = Cmd[CalState + CmdOffset].ref.index;
#if SDL_JOYSTICK
Cmd[CalState + CmdOffset].min = joyCenter.ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo->ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo.ax[axis];
#else
Cmd[CalState + CmdOffset].min = JoyAxisCenter[axis];
Cmd[CalState + CmdOffset].max = JoyAxis[axis];
@ -156,7 +156,7 @@ JoyCalAutomaton(void)
Cmd[CalState + CmdOffset].pow = -1.0;
#if SDL_JOYSTICK
sprintf(buf, "%.2f", joyInfo->ax[axis]);
sprintf(buf, "%.2f", joyInfo.ax[axis]);
#else
sprintf(buf, "%.2f", JoyAxis[axis]);
#endif
@ -167,7 +167,7 @@ JoyCalAutomaton(void)
axis = Cmd[CalState + CmdOffset].ref.index;
#if SDL_JOYSTICK
Cmd[CalState + CmdOffset].min = joyCenter.ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo->ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo.ax[axis];
#else
Cmd[CalState + CmdOffset].min = JoyAxisCenter[axis];
Cmd[CalState + CmdOffset].max = JoyAxis[axis];
@ -179,7 +179,7 @@ JoyCalAutomaton(void)
else
Cmd[CalState + CmdOffset].pow = -1.0;
#if SDL_JOYSTICK
sprintf(buf, "%.2f", joyInfo->ax[axis]);
sprintf(buf, "%.2f", joyInfo.ax[axis]);
#else
sprintf(buf, "%.2f", JoyAxis[axis]);
#endif
@ -192,7 +192,7 @@ JoyCalAutomaton(void)
axis = Cmd[CalState + CmdOffset].ref.index;
#if SDL_JOYSTICK
Cmd[CalState + CmdOffset].min = joyCenter.ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo->ax[axis];
Cmd[CalState + CmdOffset].max = joyInfo.ax[axis];
#else
Cmd[CalState + CmdOffset].min = JoyAxisCenter[axis];
Cmd[CalState + CmdOffset].max = JoyAxis[axis];
@ -205,7 +205,7 @@ JoyCalAutomaton(void)
#endif
GfuiLabelSetText(ScrHandle, LabMinId[CalState - 2], buf);
#if SDL_JOYSTICK
sprintf(buf, "%.2f", joyInfo->ax[axis]);
sprintf(buf, "%.2f", joyInfo.ax[axis]);
#else
sprintf(buf, "%.2f", JoyAxis[axis]);
#endif
@ -233,9 +233,9 @@ Idle2(void)
int index;
#if SDL_JOYSTICK
/* Check for activity on Joystick buttons */
GfctrlJoyGetCurrentStates(joyInfo);
GfctrlJoyGetCurrentStates(&joyInfo);
for (index = 0; index < GFCTRL_JOY_NUMBER * GFCTRL_JOY_MAX_BUTTONS; index++) {
if (joyInfo->edgedn[index]) {
if (joyInfo.edgedn[index]) {
/* Check whether to ignore */
if(Cmd[CalState + CmdOffset].butIgnore == index)
break;
@ -290,8 +290,8 @@ onActivate(void * /* dummy */)
int i;
int step;
#if SDL_JOYSTICK
joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(joyInfo);
//joyInfo = GfctrlJoyCreate();
GfctrlJoyGetCurrentStates(&joyInfo);
#else
int index;