adjusted coding style in SDL_os2_joystick.c to match rest of SDL better

This commit is contained in:
Ozkan Sezer 2023-08-02 20:40:10 +03:00
parent b58557a3a6
commit 57f3c41b97

View file

@ -174,27 +174,33 @@ static int OS2_JoystickInit(void)
struct _joycfg joycfg; /* Joy Configuration from envvar */
/* Open GAME$ port */
if (joyPortOpen(&hJoyPort) < 0) return 0; /* Cannot open... report no joystick */
if (joyPortOpen(&hJoyPort) < 0) {
return 0; /* Cannot open... report no joystick */
}
/* Get Max Number of Devices */
ulDataLen = sizeof(stGameParms);
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS,
NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */
if (rc != 0)
{
if (rc != 0) {
joyPortClose(&hJoyPort);
return SDL_SetError("Could not read joystick port.");
}
maxdevs = 0;
if (stGameParms.useA != 0) maxdevs++;
if (stGameParms.useB != 0) maxdevs++;
if (maxdevs > MAX_JOYSTICKS) maxdevs = MAX_JOYSTICKS;
if (stGameParms.useA != 0) {
maxdevs++;
}
if (stGameParms.useB != 0) {
maxdevs++;
}
if (maxdevs > MAX_JOYSTICKS) {
maxdevs = MAX_JOYSTICKS;
}
/* Defines min/max axes values (callibration) */
ulDataLen = sizeof(stGameCalib);
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_CALIB,
NULL, 0, NULL, &stGameCalib, ulDataLen, &ulDataLen);
if (rc != 0)
{
if (rc != 0) {
joyPortClose(&hJoyPort);
return SDL_SetError("Could not read callibration data.");
}
@ -205,34 +211,35 @@ static int OS2_JoystickInit(void)
ulDataLen = sizeof(ucNewJoystickMask);
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET,
&ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0, NULL);
if (rc == 0)
{
if (rc == 0) {
ulDataLen = sizeof(stJoyStatus);
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
if (rc != 0)
{
if (rc != 0) {
joyPortClose(&hJoyPort);
return SDL_SetError("Could not call joystick port.");
}
ulLastTick = stJoyStatus.ulJs_Ticks;
while (stJoyStatus.ulJs_Ticks == ulLastTick)
{
while (stJoyStatus.ulJs_Ticks == ulLastTick) {
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
}
if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) numdevs++;
if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0) numdevs++;
if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) {
numdevs++;
}
if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0) {
numdevs++;
}
}
if (numdevs > maxdevs) numdevs = maxdevs;
if (numdevs > maxdevs) {
numdevs = maxdevs;
}
/* If *any* joystick was detected... Let's configure SDL for them */
if (numdevs > 0)
{
if (numdevs > 0) {
/* Verify if it is a "user defined" joystick */
if (joyGetEnv(&joycfg))
{
if (joyGetEnv(&joycfg)) {
GAME_3POS_STRUCT * axis[4];
axis[0] = &stGameCalib.Ax;
axis[1] = &stGameCalib.Ay;
@ -246,46 +253,61 @@ static int OS2_JoystickInit(void)
SYS_JoyData[0].id = 0;
/* Define Number of Axes... up to 4 */
if (joycfg.axes>MAX_AXES) joycfg.axes = MAX_AXES;
if (joycfg.axes>MAX_AXES) {
joycfg.axes = MAX_AXES;
}
SYS_JoyData[0].axes = joycfg.axes;
/* Define number of buttons... 8 if 2 axes, 6 if 3 axes and 4 if 4 axes */
maxbut = MAX_BUTTONS;
if (joycfg.axes>2) maxbut -= ((joycfg.axes - 2)<<1); /* MAX_BUTTONS - 2*(axes-2) */
if (joycfg.buttons > maxbut) joycfg.buttons = maxbut;
if (joycfg.axes > 2) {
maxbut -= ((joycfg.axes - 2) << 1); /* MAX_BUTTONS - 2*(axes-2) */
}
if (joycfg.buttons > maxbut) {
joycfg.buttons = maxbut;
}
SYS_JoyData[0].buttons = joycfg.buttons;
/* Define number of hats */
if (joycfg.hats > MAX_HATS) joycfg.hats = MAX_HATS;
if (joycfg.hats > MAX_HATS) {
joycfg.hats = MAX_HATS;
}
SYS_JoyData[0].hats = joycfg.hats;
/* Define number of balls */
if (joycfg.balls > MAX_BALLS) joycfg.balls = MAX_BALLS;
if (joycfg.balls > MAX_BALLS) {
joycfg.balls = MAX_BALLS;
}
SYS_JoyData[0].balls = joycfg.balls;
/* Initialize Axes Callibration Values */
for (i=0; i<joycfg.axes; i++)
{
for (i = 0; i < joycfg.axes; i++) {
SYS_JoyData[0].axes_min[i] = axis[i]->lower;
SYS_JoyData[0].axes_med[i] = axis[i]->centre;
SYS_JoyData[0].axes_max[i] = axis[i]->upper;
}
/* Initialize Buttons 5 to 8 structures */
if (joycfg.buttons>=5) SYS_JoyData[0].buttoncalc[0] = ((axis[2]->lower+axis[3]->centre)>>1);
if (joycfg.buttons>=6) SYS_JoyData[0].buttoncalc[1] = ((axis[3]->lower+axis[3]->centre)>>1);
if (joycfg.buttons>=7) SYS_JoyData[0].buttoncalc[2] = ((axis[2]->upper+axis[3]->centre)>>1);
if (joycfg.buttons>=8) SYS_JoyData[0].buttoncalc[3] = ((axis[3]->upper+axis[3]->centre)>>1);
if (joycfg.buttons >=5 ) {
SYS_JoyData[0].buttoncalc[0] = ((axis[2]->lower + axis[3]->centre) >> 1);
}
if (joycfg.buttons >=6 ) {
SYS_JoyData[0].buttoncalc[1] = ((axis[3]->lower + axis[3]->centre) >> 1);
}
if (joycfg.buttons >=7 ) {
SYS_JoyData[0].buttoncalc[2] = ((axis[2]->upper + axis[3]->centre) >> 1);
}
if (joycfg.buttons >=8 ) {
SYS_JoyData[0].buttoncalc[3] = ((axis[3]->upper + axis[3]->centre) >> 1);
}
/* Intialize Joystick Name */
SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName));
}
/* Default Init ... autoconfig */
else
{
else {
/* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */
if (numdevs == 2)
{
if (numdevs == 2) {
/* Define Device 0 as 4 axes, 4 buttons */
SYS_JoyData[0].id=0;
SYS_JoyData[0].id = 0;
SYS_JoyData[0].axes = 4;
SYS_JoyData[0].buttons = 4;
SYS_JoyData[0].hats = 0;
@ -303,7 +325,7 @@ static int OS2_JoystickInit(void)
SYS_JoyData[0].axes_med[3] = stGameCalib.By.centre;
SYS_JoyData[0].axes_max[3] = stGameCalib.By.upper;
/* Define Device 1 as 2 axes, 2 buttons */
SYS_JoyData[1].id=1;
SYS_JoyData[1].id = 1;
SYS_JoyData[1].axes = 2;
SYS_JoyData[1].buttons = 2;
SYS_JoyData[1].hats = 0;
@ -316,13 +338,11 @@ static int OS2_JoystickInit(void)
SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper;
}
/* One joystick only? */
else
{
else {
/* If it is joystick A... */
if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0)
{
if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) {
/* Define Device 0 as 2 axes, 4 buttons */
SYS_JoyData[0].id=0;
SYS_JoyData[0].id = 0;
SYS_JoyData[0].axes = 2;
SYS_JoyData[0].buttons = 4;
SYS_JoyData[0].hats = 0;
@ -335,10 +355,9 @@ static int OS2_JoystickInit(void)
SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
}
/* If not, it is joystick B */
else
{
else {
/* Define Device 1 as 2 axes, 2 buttons */
SYS_JoyData[0].id=1;
SYS_JoyData[0].id = 1;
SYS_JoyData[0].axes = 2;
SYS_JoyData[0].buttons = 2;
SYS_JoyData[0].hats = 0;
@ -353,16 +372,17 @@ static int OS2_JoystickInit(void)
}
/* Hack to define Joystick Port Names */
if (numdevs > maxdevs) numdevs = maxdevs;
if (numdevs > maxdevs) {
numdevs = maxdevs;
}
for (i = 0; i < numdevs; i++)
{
SDL_snprintf(SYS_JoyData[i].szDeviceName,
SDL_arraysize(SYS_JoyData[i].szDeviceName),
for (i = 0; i < numdevs; i++) {
SDL_snprintf(SYS_JoyData[i].szDeviceName, SDL_arraysize(SYS_JoyData[i].szDeviceName),
"Default Joystick %c", 'A'+SYS_JoyData[i].id);
}
}
}
/* Return the number of devices found */
numjoysticks = numdevs;
return numdevs;
@ -422,8 +442,7 @@ static int OS2_JoystickOpen(SDL_Joystick *joystick, int device_index)
/* allocate memory for system specific hardware data */
joystick->hwdata = (struct joystick_hwdata *) SDL_calloc(1, sizeof(*joystick->hwdata));
if (!joystick->hwdata)
{
if (!joystick->hwdata) {
return SDL_OutOfMemory();
}
@ -433,16 +452,12 @@ static int OS2_JoystickOpen(SDL_Joystick *joystick, int device_index)
/* Define offsets and scales for all axes */
joystick->hwdata->id = SYS_JoyData[index].id;
for (i = 0; i < MAX_AXES; ++i)
{
if ((i < 2) || i < SYS_JoyData[index].axes)
{
for (i = 0; i < MAX_AXES; ++i) {
if ((i < 2) || i < SYS_JoyData[index].axes) {
joystick->hwdata->transaxes[i].offset = ((SDL_JOYSTICK_AXIS_MAX + SDL_JOYSTICK_AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i];
joystick->hwdata->transaxes[i].scale1 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MIN/SYS_JoyData[index].axes_min[i]));
joystick->hwdata->transaxes[i].scale2 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MAX/SYS_JoyData[index].axes_max[i]));
}
else
{
} else {
joystick->hwdata->transaxes[i].offset = 0;
joystick->hwdata->transaxes[i].scale1 = 1.0f; /* Just in case */
joystick->hwdata->transaxes[i].scale2 = 1.0f; /* Just in case */
@ -514,8 +529,7 @@ static void OS2_JoystickUpdate(SDL_Joystick *joystick)
ulDataLen = sizeof(stGameStatus);
rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS,
NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen);
if (rc != 0)
{
if (rc != 0) {
SDL_SetError("Could not read joystick status.");
return; /* Could not read data */
}
@ -525,18 +539,22 @@ static void OS2_JoystickUpdate(SDL_Joystick *joystick)
/* joystick motion events */
if (SYS_JoyData[index].id == 0)
{
if (SYS_JoyData[index].id == 0) {
pos[0] = stGameStatus.curdata.A.x;
pos[1] = stGameStatus.curdata.A.y;
if (SYS_JoyData[index].axes >= 3) pos[2] = stGameStatus.curdata.B.x;
else pos[2] = 0;
if (SYS_JoyData[index].axes >= 4) pos[3] = stGameStatus.curdata.B.y;
else pos[3] = 0;
if (SYS_JoyData[index].axes >= 3) {
pos[2] = stGameStatus.curdata.B.x;
} else {
pos[2] = 0;
}
if (SYS_JoyData[index].axes >= 4) {
pos[3] = stGameStatus.curdata.B.y;
} else {
pos[3] = 0;
}
/* OS/2 basic drivers do not support more than 4 axes joysticks */
}
else if (SYS_JoyData[index].id == 1)
{
else if (SYS_JoyData[index].id == 1) {
pos[0] = stGameStatus.curdata.B.x;
pos[1] = stGameStatus.curdata.B.y;
pos[2] = 0;
@ -545,41 +563,42 @@ static void OS2_JoystickUpdate(SDL_Joystick *joystick)
/* Corrects the movements using the callibration */
transaxes = joystick->hwdata->transaxes;
for (i = 0; i < joystick->naxes; i++)
{
for (i = 0; i < joystick->naxes; i++) {
value = pos[i] + transaxes[i].offset;
if (value < 0)
{
if (value < 0) {
value *= transaxes[i].scale1;
if (value > 0) value = SDL_JOYSTICK_AXIS_MIN;
if (value > 0) {
value = SDL_JOYSTICK_AXIS_MIN;
}
else
{
} else {
value *= transaxes[i].scale2;
if (value < 0) value = SDL_JOYSTICK_AXIS_MAX;
if (value < 0) {
value = SDL_JOYSTICK_AXIS_MAX;
}
}
SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
}
/* joystick button A to D events */
if (SYS_JoyData[index].id == 1) corr = 2;
else corr = 0;
if (SYS_JoyData[index].id == 1) {
corr = 2;
} else {
corr = 0;
}
normbut = 4; /* Number of normal buttons */
if (joystick->nbuttons < normbut) normbut = joystick->nbuttons;
for (i = corr; (i-corr) < normbut; ++i)
{
if (joystick->nbuttons < normbut) {
normbut = joystick->nbuttons;
}
for (i = corr; (i-corr) < normbut; ++i) {
/*
Button A: 1110 0000
Button B: 1101 0000
Button C: 1011 0000
Button D: 0111 0000
*/
if ((~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i))
{
if ((~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i)) {
SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED);
}
else
{
} else {
SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED);
}
}
@ -591,25 +610,33 @@ static void OS2_JoystickUpdate(SDL_Joystick *joystick)
Button G: Axis 2 X Right
Button H: Axis 2 Y Down
*/
if (joystick->nbuttons >= 5)
{
if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED);
else SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED);
if (joystick->nbuttons >= 5) {
if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) {
SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED);
} else {
SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED);
}
if (joystick->nbuttons >= 6)
{
if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED);
else SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED);
}
if (joystick->nbuttons >= 7)
{
if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED);
else SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED);
if (joystick->nbuttons >= 6) {
if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) {
SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED);
} else {
SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED);
}
}
if (joystick->nbuttons >= 7) {
if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) {
SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED);
} else {
SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED);
}
}
if (joystick->nbuttons >= 8) {
if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) {
SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED);
} else {
SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED);
}
if (joystick->nbuttons >= 8)
{
if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED);
else SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED);
}
/* joystick hat events */
@ -656,13 +683,14 @@ static int joyPortOpen(HFILE * hGame)
ULONG ulDataLen; /* Size of version data */
/* Verifies if joyport is not already open... */
if (*hGame != NULLHANDLE) return 0;
if (*hGame != NULLHANDLE) {
return 0;
}
/* Open GAME$ for read */
rc = DosOpen("GAME$ ", hGame, &ulAction, 0, FILE_READONLY,
FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL);
if (rc != 0)
{
if (rc != 0) {
return SDL_SetError("Could not open Joystick Port.");
}
@ -671,16 +699,15 @@ static int joyPortOpen(HFILE * hGame)
ulDataLen = sizeof(ulVersion);
rc = DosDevIOCtl(*hGame, IOCTL_CAT_USER, GAME_GET_VERSION,
NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen);
if (rc != 0)
{
if (rc != 0) {
joyPortClose(hGame);
return SDL_SetError("Could not get Joystick Driver version.");
}
if (ulVersion < 0x20)
{
if (ulVersion < 0x20) {
joyPortClose(hGame);
return SDL_SetError("Driver too old. At least IBM driver version 2.0 required.");
}
return 0;
}
@ -689,7 +716,9 @@ static int joyPortOpen(HFILE * hGame)
/****************************/
static void joyPortClose(HFILE * hGame)
{
if (*hGame != NULLHANDLE) DosClose(*hGame);
if (*hGame != NULLHANDLE) {
DosClose(*hGame);
}
*hGame = NULLHANDLE;
}
@ -726,24 +755,33 @@ static int joyGetEnv(struct _joycfg * joydata)
}
/* Now get the number of axes */
while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
while (*joyenv == ' ' && *joyenv != 0) {
joyenv++; /* jump spaces... */
}
joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
joydata->axes = SDL_atoi(tempnumber);
/* Now get the number of buttons */
while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
while (*joyenv == ' ' && *joyenv != 0) {
joyenv++; /* jump spaces... */
}
joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
joydata->buttons = SDL_atoi(tempnumber);
/* Now get the number of hats */
while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
while (*joyenv == ' ' && *joyenv != 0) {
joyenv++; /* jump spaces... */
}
joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
joydata->hats = SDL_atoi(tempnumber);
/* Now get the number of balls */
while (*joyenv==' ' && *joyenv != 0) joyenv++; /* jump spaces... */
while (*joyenv==' ' && *joyenv != 0) {
joyenv++; /* jump spaces... */
}
joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
joydata->balls = SDL_atoi(tempnumber);
return 1;
}
@ -757,18 +795,15 @@ static int joyGetData(const char *joyenv, char *name, char stopchar, size_t maxc
int chcnt = 0; /* Count how many characters where copied */
nameptr = name;
while (*joyenv!=stopchar && *joyenv!=0)
{
if (nameptr < (name + (maxchars-1)))
{
while (*joyenv!=stopchar && *joyenv != 0) {
if (nameptr < (name + (maxchars-1))) {
*nameptr = *joyenv; /* Only copy if smaller than maximum */
nameptr++;
}
chcnt++;
joyenv++;
}
if (*joyenv == stopchar)
{
if (*joyenv == stopchar) {
joyenv++; /* Jump stopchar */
chcnt++;
}
@ -776,8 +811,7 @@ static int joyGetData(const char *joyenv, char *name, char stopchar, size_t maxc
return chcnt;
}
SDL_JoystickDriver SDL_OS2_JoystickDriver =
{
SDL_JoystickDriver SDL_OS2_JoystickDriver = {
OS2_JoystickInit,
OS2_NumJoysticks,
OS2_JoystickDetect,