📄 sdl_sysjoystick.c
字号:
SDL_OutOfMemory(); return(-1); }/* Reset Hardware Data */SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));/* ShortCut Pointer */index = joystick->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 ) { joystick->hwdata->transaxes[i].offset = ((AXIS_MAX + AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i]; //joystick->hwdata->transaxes[i].scale = (float)((AXIS_MAX - AXIS_MIN)/(SYS_JoyData[index].axes_max[i]-SYS_JoyData[index].axes_min[i])); joystick->hwdata->transaxes[i].scale1 = (float)abs((AXIS_MIN/SYS_JoyData[index].axes_min[i])); joystick->hwdata->transaxes[i].scale2 = (float)abs((AXIS_MAX/SYS_JoyData[index].axes_max[i])); } else { joystick->hwdata->transaxes[i].offset = 0; //joystick->hwdata->transaxes[i].scale = 1.0; /* Just in case */ joystick->hwdata->transaxes[i].scale1 = 1.0; /* Just in case */ joystick->hwdata->transaxes[i].scale2 = 1.0; /* Just in case */ } }/* fill nbuttons, naxes, and nhats fields */joystick->nbuttons = SYS_JoyData[index].buttons;joystick->naxes = SYS_JoyData[index].axes;/* joystick->nhats = SYS_JoyData[index].hats; */joystick->nhats = 0; /* No support for hats at this time *//* joystick->nballs = SYS_JoyData[index].balls; */joystick->nballs = 0; /* No support for balls at this time */return 0;}/***************************************************************************//* Function to update the state of a joystick - called as a device poll. *//* This function shouldn't update the joystick structure directly, *//* but instead should call SDL_PrivateJoystick*() to deliver events *//* and update joystick device state. *//***************************************************************************/void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick){APIRET rc; /* Generic OS/2 return code */int index; /* index shortcurt to joystick index */int i; /* Generic counter */int normbut; /* Number of buttons reported by joystick */int corr; /* Correction for button names */Sint16 value, change; /* Values used to update axis values */struct _transaxes *transaxes; /* Shortcut for Correction structure */Uint32 pos[MAX_AXES]; /* Vector to inform the Axis status */ULONG ulDataLen; /* Size of data */GAME_STATUS_STRUCT stGameStatus; /* Joystick Status Structure */ulDataLen = sizeof(stGameStatus);rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS, NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen);if (rc != 0) { SDL_SetError("Could not read joystick status."); return; /* Could not read data */ }/* Shortcut pointer */index = joystick->index;/* joystick motion events */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; pos[4]=0; /* OS/2 basic drivers do not support more than 4 axes joysticks */ pos[5]=0; }else if (SYS_JoyData[index].id == 1) { pos[0] = stGameStatus.curdata.B.x; pos[1] = stGameStatus.curdata.B.y; pos[2]=0; pos[3]=0; pos[4]=0; pos[5]=0; }/* Corrects the movements using the callibration */transaxes = joystick->hwdata->transaxes;for (i = 0; i < joystick->naxes; i++) { value = pos[i] + transaxes[i].offset; if (value<0) { value*=transaxes[i].scale1; if (value>0) value = AXIS_MIN; } else { value*=transaxes[i].scale2; if (value<0) value = AXIS_MAX; } change = (value - joystick->axes[i]); if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); } }/* joystick button A to D events */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 ) { /* 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 ( ! joystick->buttons[i-corr] ) { SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED); } } else { if ( joystick->buttons[i-corr] ) { SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED); } } }/* Joystick button E to H buttons */ /* Button E: Axis 2 X Left Button F: Axis 2 Y Up 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>=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); }/* joystick hat events *//* Not Supported under OS/2 *//* joystick ball events *//* Not Supported under OS/2 */}/******************************************//* Function to close a joystick after use *//******************************************/void SDL_SYS_JoystickClose(SDL_Joystick *joystick){if (joystick->hwdata != NULL) { /* free system specific hardware data */ SDL_free(joystick->hwdata); }}/********************************************************************//* Function to perform any system-specific joystick related cleanup *//********************************************************************/void SDL_SYS_JoystickQuit(void){joyPortClose(&hJoyPort);}/************************//************************//* OS/2 Implementations *//************************//************************//*****************************************//* Open Joystick Port, if not opened yet *//*****************************************/APIRET joyPortOpen(HFILE * hGame){APIRET rc; /* Generic Return Code */ULONG ulAction; /* ? */ULONG ulVersion; /* Version of joystick driver */ULONG ulDataLen; /* Size of version data *//* Verifies if joyport is not already open... */if (*hGame != NULL) return 0;/* Open GAME$ for read */rc = DosOpen((PSZ)GAMEPDDNAME, hGame, &ulAction, 0, FILE_READONLY, FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL);if (rc != 0) { SDL_SetError("Could not open Joystick Port."); return -1; } /* Get Joystick Driver Version... must be 2.0 or higher */ulVersion = 0;ulDataLen = sizeof(ulVersion);rc = DosDevIOCtl( *hGame, IOCTL_CAT_USER, GAME_GET_VERSION, NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen);if (rc != 0) { joyPortClose(hGame); SDL_SetError("Could not get Joystick Driver version."); return -1; }if (ulVersion < GAME_VERSION) { joyPortClose(hGame); SDL_SetError("Driver too old. At least IBM driver version 2.0 required."); return -1; }return 0;}/****************************//* Close JoyPort, if opened *//****************************/void joyPortClose(HFILE * hGame){if (*hGame != NULL) DosClose(*hGame);*hGame = NULL;}/***************************//* Get SDL Joystick EnvVar *//***************************/int joyGetEnv(struct _joycfg * joydata){char *joyenv; /* Pointer to tested character */char tempnumber[5]; /* Temporary place to put numeric texts */joyenv = SDL_getenv("SDL_OS2_JOYSTICK");if (joyenv == NULL) return 0;/* Joystick Environment is defined! */while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... *//* If the string name starts with '... get if fully */if (*joyenv=='\'') joyenv+=joyGetData(++joyenv,joydata->name,'\'',sizeof(joydata->name));/* If not, get it until the next space */else if (*joyenv=='\"') joyenv+=joyGetData(++joyenv,joydata->name,'\"',sizeof(joydata->name));else joyenv+=joyGetData(joyenv,joydata->name,' ',sizeof(joydata->name));/* Now get the number of axes */while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));joydata->axes = atoi(tempnumber);/* Now get the number of buttons */while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));joydata->buttons = atoi(tempnumber);/* Now get the number of hats */while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));joydata->hats = atoi(tempnumber);/* Now get the number of balls */while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));joydata->balls = atoi(tempnumber);return 1;}/************************************************************************//* Get a text from in the string starting in joyenv until it finds *//* the stopchar or maxchars is reached. The result is placed in name. *//************************************************************************/int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars){char *nameptr; /* Pointer to the selected character */int chcnt=0; /* Count how many characters where copied */nameptr=name;while (*joyenv!=stopchar && *joyenv!=0) { if (nameptr<(name+(maxchars-1))) { *nameptr = *joyenv; /* Only copy if smaller than maximum */ nameptr++; } chcnt++; joyenv++; }if (*joyenv==stopchar) { joyenv++; /* Jump stopchar */ chcnt++; }*nameptr = 0; /* Mark last byte */return chcnt;}#endif /* SDL_JOYSTICK_OS2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -