📄 sdl_sysjoystick.c
字号:
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2006 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org*/#include "SDL_config.h"#ifdef SDL_JOYSTICK_OS2/* OS/2 Joystick driver, contributed by Daniel Caetano */#include <mem.h>#define INCL_DOSDEVICES#define INCL_DOSDEVIOCTL#define INCL_DOSMEMMGR#include <os2.h>#include "joyos2.h"#include "SDL_joystick.h"#include "SDL_events.h"#include "../SDL_sysjoystick.h"#include "../SDL_joystick_c.h"HFILE hJoyPort = NULL; /* Joystick GAME$ Port Address */#define MAX_JOYSTICKS 2 /* Maximum of two joysticks */#define MAX_AXES 4 /* each joystick can have up to 4 axes */#define MAX_BUTTONS 8 /* 8 buttons */#define MAX_HATS 0 /* 0 hats - OS/2 doesn't support it */#define MAX_BALLS 0 /* and 0 balls - OS/2 doesn't support it */#define AXIS_MIN -32768 /* minimum value for axes coordinate */#define AXIS_MAX 32767 /* maximum value for axes coordinate */#define MAX_JOYNAME 128 /* Joystick name may have 128 characters *//* limit axes to 256 possible positions to filter out noise */#define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256)/* Calc Button Flag for buttons A to D */#define JOY_BUTTON_FLAG(n) (1<<n)/* Joystick data... hold information about detected devices */typedef struct SYS_JoyData_s{Sint8 id; // Device IDchar szDeviceName[MAX_JOYNAME]; // Device Namechar axes; // Number of axeschar buttons; // Number of buttonschar hats; // Number of buttonschar balls; // Number of buttonsint axes_min[MAX_AXES]; // minimum callibration value for axesint axes_med[MAX_AXES]; // medium callibration value for axesint axes_max[MAX_AXES]; // maximum callibration value for axesint buttoncalc[4]; // Used for buttons 5, 6, 7 and 8.} SYS_JoyData_t, *SYS_JoyData_p;SYS_JoyData_t SYS_JoyData[MAX_JOYSTICKS];/* Structure used to convert data from OS/2 driver format to SDL format */struct joystick_hwdata{Sint8 id;struct _transaxes { int offset; /* Center Offset */ float scale1; /* Center to left/up Scale */ float scale2; /* Center to right/down Scale */ } transaxes[MAX_AXES];};/* Structure used to get values from Joystick Environment Variable */struct _joycfg{char name[MAX_JOYNAME];unsigned int axes;unsigned int buttons;unsigned int hats;unsigned int balls;};/* OS/2 Implementation Function Prototypes */APIRET joyPortOpen(HFILE * hGame);void joyPortClose(HFILE * hGame);int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars);int joyGetEnv(struct _joycfg * joydata);/************************************************************************//* Function to scan the system for joysticks. *//* This function should set SDL_numjoysticks to the number of available *//* joysticks. Joystick 0 should be the system default joystick. *//* It should return 0, or -1 on an unrecoverable fatal error. *//************************************************************************/int SDL_SYS_JoystickInit(void){APIRET rc; /* Generic OS/2 return code */GAME_PORT_STRUCT stJoyStatus; /* Joystick Status Structure */GAME_PARM_STRUCT stGameParms; /* Joystick Parameter Structure */GAME_CALIB_STRUCT stGameCalib; /* Calibration Struct */ULONG ulDataLen; /* Size of data */ULONG ulLastTick; /* Tick Counter for timing operations */Uint8 maxdevs; /* Maximum number of devices */Uint8 numdevs; /* Number of present devices */Uint8 maxbut; /* Maximum number of buttons... */Uint8 i; /* Temporary Count Vars */Uint8 ucNewJoystickMask; /* Mask for Joystick Detection */struct _joycfg joycfg; /* Joy Configuration from envvar *//* Get Max Number of Devices */rc = joyPortOpen(&hJoyPort); /* Open GAME$ port */if (rc != 0) return 0; /* Cannot open... report no joystick */ulDataLen = sizeof(stGameParms);rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS, NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */if (rc != 0) { joyPortClose(&hJoyPort); SDL_SetError("Could not read joystick port."); return -1; }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) { joyPortClose(&hJoyPort); SDL_SetError("Could not read callibration data."); return -1; }/* Determine how many joysticks are active */numdevs = 0; /* Points no device */ucNewJoystickMask = 0x0F; /* read all 4 joystick axis */ulDataLen = sizeof(ucNewJoystickMask);rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET, &ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0, NULL);if (rc == 0) { ulDataLen = sizeof(stJoyStatus); rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET, NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen); if (rc != 0) { joyPortClose(&hJoyPort); SDL_SetError("Could not call joystick port."); return -1; } ulLastTick = stJoyStatus.ulJs_Ticks; 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 (numdevs>maxdevs) numdevs=maxdevs;/* If *any* joystick was detected... Let's configure SDL for them */if (numdevs > 0) { /* Verify if it is a "user defined" joystick */ if (joyGetEnv(&joycfg)) { GAME_3POS_STRUCT * axis[4]; axis[0] = &stGameCalib.Ax; axis[1] = &stGameCalib.Ay; axis[2] = &stGameCalib.Bx; axis[3] = &stGameCalib.By; /* Say it has one device only (user defined is always one device only) */ numdevs = 1; /* Define Device 0 as... */ SYS_JoyData[0].id=0; /* Define Number of Axes... up to 4 */ 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; SYS_JoyData[0].buttons = joycfg.buttons; /* Define number of 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; SYS_JoyData[0].balls = joycfg.balls; /* Initialize Axes Callibration Values */ 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); /* Intialize Joystick Name */ SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName)); } /* Default Init ... autoconfig */ else { /* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */ if (numdevs==2) { /* Define Device 0 as 4 axes, 4 buttons */ SYS_JoyData[0].id=0; SYS_JoyData[0].axes = 4; SYS_JoyData[0].buttons = 4; SYS_JoyData[0].hats = 0; SYS_JoyData[0].balls = 0; SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower; SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre; SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper; SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower; SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre; SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper; SYS_JoyData[0].axes_min[2] = stGameCalib.Bx.lower; SYS_JoyData[0].axes_med[2] = stGameCalib.Bx.centre; SYS_JoyData[0].axes_max[2] = stGameCalib.Bx.upper; SYS_JoyData[0].axes_min[3] = stGameCalib.By.lower; 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].axes = 2; SYS_JoyData[1].buttons = 2; SYS_JoyData[1].hats = 0; SYS_JoyData[1].balls = 0; SYS_JoyData[1].axes_min[0] = stGameCalib.Bx.lower; SYS_JoyData[1].axes_med[0] = stGameCalib.Bx.centre; SYS_JoyData[1].axes_max[0] = stGameCalib.Bx.upper; SYS_JoyData[1].axes_min[1] = stGameCalib.By.lower; SYS_JoyData[1].axes_med[1] = stGameCalib.By.centre; SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper; } /* One joystick only? */ else { /* If it is joystick A... */ if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) { /* Define Device 0 as 2 axes, 4 buttons */ SYS_JoyData[0].id=0; SYS_JoyData[0].axes = 2; SYS_JoyData[0].buttons = 4; SYS_JoyData[0].hats = 0; SYS_JoyData[0].balls = 0; SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower; SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre; SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper; SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower; SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre; SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper; } /* If not, it is joystick B */ else { /* Define Device 1 as 2 axes, 2 buttons */ SYS_JoyData[0].id=1; SYS_JoyData[0].axes = 2; SYS_JoyData[0].buttons = 2; SYS_JoyData[0].hats = 0; SYS_JoyData[0].balls = 0; SYS_JoyData[0].axes_min[0] = stGameCalib.Bx.lower; SYS_JoyData[0].axes_med[0] = stGameCalib.Bx.centre; SYS_JoyData[0].axes_max[0] = stGameCalib.Bx.upper; SYS_JoyData[0].axes_min[1] = stGameCalib.By.lower; SYS_JoyData[0].axes_med[1] = stGameCalib.By.centre; SYS_JoyData[0].axes_max[1] = stGameCalib.By.upper; } } /* Hack to define Joystick Port Names */ if ( numdevs > maxdevs ) numdevs = maxdevs; 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 */return(numdevs);}/***********************************************************//* Function to get the device-dependent name of a joystick *//***********************************************************/const char *SDL_SYS_JoystickName(int index){/* No need to verify if device exists, already done in upper layer */return(SYS_JoyData[index].szDeviceName);}/******************************************************************************//* Function to open a joystick for use. *//* The joystick to open is specified by the index field of the joystick. *//* This should fill the nbuttons and naxes fields of the joystick structure. *//* It returns 0, or -1 if there is an error. *//******************************************************************************/int SDL_SYS_JoystickOpen(SDL_Joystick *joystick){int index; /* Index shortcut for index in joystick structure */int i; /* Generic Counter *//* allocate memory for system specific hardware data */joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));if (joystick->hwdata == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -