Reworking of joystick support :
- Joystick type now selectable - Added emulation of standard joysticks with up to 8 buttons - Added emulation of Microsoft Sidewinder pads - Host->emulated joystick button & axis mapping now configurable (only on Windows atm) - Gameport registers now take 1us to access - helps Sidewinder drivers
This commit is contained in:
parent
9021d9f2c2
commit
7980438e1f
17 changed files with 543 additions and 100 deletions
|
|
@ -1,5 +1,11 @@
|
|||
#define DIRECTINPUT_VERSION 0x0700
|
||||
#include <dinput.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
extern "C" {
|
||||
#include "device.h"
|
||||
#include "gameport.h"
|
||||
}
|
||||
#include "plat-joystick.h"
|
||||
#include "win.h"
|
||||
|
||||
|
|
@ -12,29 +18,72 @@ extern "C" void joystick_init();
|
|||
extern "C" void joystick_close();
|
||||
extern "C" void poll_joystick();
|
||||
|
||||
joystick_t joystick_state[2];
|
||||
plat_joystick_t plat_joystick_state[MAX_PLAT_JOYSTICKS];
|
||||
joystick_t joystick_state[MAX_JOYSTICKS];
|
||||
|
||||
static LPDIRECTINPUT lpdi;
|
||||
static LPDIRECTINPUTDEVICE2 lpdi_joystick[2] = {NULL, NULL};
|
||||
|
||||
int joysticks_present = 0;
|
||||
static GUID joystick_guids[2];
|
||||
static GUID joystick_guids[MAX_JOYSTICKS];
|
||||
|
||||
static BOOL CALLBACK joystick_enum_callback(LPCDIDEVICEINSTANCE lpddi, LPVOID data)
|
||||
{
|
||||
if (joysticks_present >= 2)
|
||||
if (joysticks_present >= MAX_JOYSTICKS)
|
||||
return DIENUM_STOP;
|
||||
|
||||
pclog("joystick_enum_callback : found joystick %i : %s\n", joysticks_present, lpddi->tszProductName);
|
||||
|
||||
joystick_guids[joysticks_present++] = lpddi->guidInstance;
|
||||
|
||||
if (joysticks_present >= 2)
|
||||
if (joysticks_present >= MAX_JOYSTICKS)
|
||||
return DIENUM_STOP;
|
||||
|
||||
return DIENUM_CONTINUE;
|
||||
}
|
||||
|
||||
BOOL CALLBACK DIEnumDeviceObjectsCallback(
|
||||
LPCDIDEVICEOBJECTINSTANCE lpddoi,
|
||||
LPVOID pvRef)
|
||||
{
|
||||
plat_joystick_t *state = (plat_joystick_t *)pvRef;
|
||||
|
||||
if (lpddoi->guidType == GUID_XAxis || lpddoi->guidType == GUID_YAxis || lpddoi->guidType == GUID_ZAxis ||
|
||||
lpddoi->guidType == GUID_RxAxis || lpddoi->guidType == GUID_RyAxis || lpddoi->guidType == GUID_RzAxis ||
|
||||
lpddoi->guidType == GUID_Slider)
|
||||
{
|
||||
strncpy(state->axis[state->nr_axes].name, lpddoi->tszName, sizeof(state->axis[state->nr_axes].name));
|
||||
pclog("Axis %i : %s %x %x\n", state->nr_axes, state->axis[state->nr_axes].name, lpddoi->dwOfs, lpddoi->dwType);
|
||||
if (lpddoi->guidType == GUID_XAxis)
|
||||
state->axis[state->nr_axes].id = 0;
|
||||
else if (lpddoi->guidType == GUID_YAxis)
|
||||
state->axis[state->nr_axes].id = 1;
|
||||
else if (lpddoi->guidType == GUID_ZAxis)
|
||||
state->axis[state->nr_axes].id = 2;
|
||||
else if (lpddoi->guidType == GUID_RxAxis)
|
||||
state->axis[state->nr_axes].id = 3;
|
||||
else if (lpddoi->guidType == GUID_RyAxis)
|
||||
state->axis[state->nr_axes].id = 4;
|
||||
else if (lpddoi->guidType == GUID_RzAxis)
|
||||
state->axis[state->nr_axes].id = 5;
|
||||
state->nr_axes++;
|
||||
}
|
||||
else if (lpddoi->guidType == GUID_Button)
|
||||
{
|
||||
strncpy(state->button[state->nr_buttons].name, lpddoi->tszName, sizeof(state->button[state->nr_buttons].name));
|
||||
pclog("Button %i : %s %x %x\n", state->nr_buttons, state->button[state->nr_buttons].name, lpddoi->dwOfs, lpddoi->dwType);
|
||||
state->nr_buttons++;
|
||||
}
|
||||
else if (lpddoi->guidType == GUID_POV)
|
||||
{
|
||||
strncpy(state->pov[state->nr_povs].name, lpddoi->tszName, sizeof(state->pov[state->nr_povs].name));
|
||||
pclog("POV %i : %s %x %x\n", state->nr_povs, state->pov[state->nr_povs].name, lpddoi->dwOfs, lpddoi->dwType);
|
||||
state->nr_povs++;
|
||||
}
|
||||
|
||||
return DIENUM_CONTINUE;
|
||||
}
|
||||
|
||||
void joystick_init()
|
||||
{
|
||||
int c;
|
||||
|
|
@ -55,12 +104,35 @@ void joystick_init()
|
|||
{
|
||||
LPDIRECTINPUTDEVICE lpdi_joystick_temp = NULL;
|
||||
DIPROPRANGE joy_axis_range;
|
||||
|
||||
DIDEVICEINSTANCE device_instance;
|
||||
DIDEVCAPS devcaps;
|
||||
int d;
|
||||
|
||||
if (FAILED(lpdi->CreateDevice(joystick_guids[c], &lpdi_joystick_temp, NULL)))
|
||||
fatal("joystick_init : CreateDevice failed\n");
|
||||
if (FAILED(lpdi_joystick_temp->QueryInterface(IID_IDirectInputDevice2, (void **)&lpdi_joystick[c])))
|
||||
fatal("joystick_init : CreateDevice failed\n");
|
||||
lpdi_joystick_temp->Release();
|
||||
|
||||
memset(&device_instance, 0, sizeof(device_instance));
|
||||
device_instance.dwSize = sizeof(device_instance);
|
||||
if (FAILED(lpdi_joystick[c]->GetDeviceInfo(&device_instance)))
|
||||
fatal("joystick_init : GetDeviceInfo failed\n");
|
||||
pclog("Joystick %i :\n", c);
|
||||
pclog(" tszInstanceName = %s\n", device_instance.tszInstanceName);
|
||||
pclog(" tszProductName = %s\n", device_instance.tszProductName);
|
||||
strncpy(plat_joystick_state[c].name, device_instance.tszInstanceName, 64);
|
||||
|
||||
memset(&devcaps, 0, sizeof(devcaps));
|
||||
devcaps.dwSize = sizeof(devcaps);
|
||||
if (FAILED(lpdi_joystick[c]->GetCapabilities(&devcaps)))
|
||||
fatal("joystick_init : GetCapabilities failed\n");
|
||||
pclog(" Axes = %i\n", devcaps.dwAxes);
|
||||
pclog(" Buttons = %i\n", devcaps.dwButtons);
|
||||
pclog(" POVs = %i\n", devcaps.dwPOVs);
|
||||
|
||||
lpdi_joystick[c]->EnumObjects(DIEnumDeviceObjectsCallback, &plat_joystick_state[c], DIDFT_ALL);
|
||||
|
||||
if (FAILED(lpdi_joystick[c]->SetCooperativeLevel(ghwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
||||
fatal("joystick_init : SetCooperativeLevel failed\n");
|
||||
if (FAILED(lpdi_joystick[c]->SetDataFormat(&c_dfDIJoystick)))
|
||||
|
|
@ -70,16 +142,18 @@ void joystick_init()
|
|||
joy_axis_range.lMax = 32767;
|
||||
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
|
||||
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
||||
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
|
||||
joy_axis_range.diph.dwObj = DIJOFS_X;
|
||||
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
|
||||
joy_axis_range.lMin = -32768;
|
||||
joy_axis_range.lMax = 32767;
|
||||
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
|
||||
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
||||
joy_axis_range.diph.dwObj = DIJOFS_Y;
|
||||
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
joy_axis_range.diph.dwObj = DIJOFS_Z;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
joy_axis_range.diph.dwObj = DIJOFS_RX;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
joy_axis_range.diph.dwObj = DIJOFS_RY;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
joy_axis_range.diph.dwObj = DIJOFS_RZ;
|
||||
lpdi_joystick[c]->SetProperty(DIPROP_RANGE, &joy_axis_range.diph);
|
||||
|
||||
if (FAILED(lpdi_joystick[c]->Acquire()))
|
||||
|
|
@ -103,11 +177,12 @@ void joystick_close()
|
|||
|
||||
void joystick_poll()
|
||||
{
|
||||
int c;
|
||||
int c, d;
|
||||
|
||||
for (c = 0; c < joysticks_present; c++)
|
||||
{
|
||||
DIJOYSTATE joystate;
|
||||
int b;
|
||||
|
||||
if (FAILED(lpdi_joystick[c]->Poll()))
|
||||
{
|
||||
|
|
@ -121,14 +196,60 @@ void joystick_poll()
|
|||
lpdi_joystick[c]->GetDeviceState(sizeof(DIJOYSTATE), (LPVOID)&joystate);
|
||||
}
|
||||
|
||||
joystick_state[c].x = joystate.lX;
|
||||
joystick_state[c].y = joystate.lY;
|
||||
joystick_state[c].b[0] = joystate.rgbButtons[0] & 0x80;
|
||||
joystick_state[c].b[1] = joystate.rgbButtons[1] & 0x80;
|
||||
joystick_state[c].b[2] = joystate.rgbButtons[2] & 0x80;
|
||||
joystick_state[c].b[3] = joystate.rgbButtons[3] & 0x80;
|
||||
plat_joystick_state[c].a[0] = joystate.lX;
|
||||
plat_joystick_state[c].a[1] = joystate.lY;
|
||||
plat_joystick_state[c].a[2] = joystate.lZ;
|
||||
plat_joystick_state[c].a[3] = joystate.lRx;
|
||||
plat_joystick_state[c].a[4] = joystate.lRy;
|
||||
plat_joystick_state[c].a[5] = joystate.lRz;
|
||||
|
||||
for (b = 0; b < 16; b++)
|
||||
plat_joystick_state[c].b[b] = joystate.rgbButtons[b] & 0x80;
|
||||
|
||||
for (b = 0; b < 4; b++)
|
||||
plat_joystick_state[c].p[b] = joystate.rgdwPOV[b];
|
||||
// pclog("joystick %i - x=%i y=%i b[0]=%i b[1]=%i %i\n", c, joystick_state[c].x, joystick_state[c].y, joystick_state[c].b[0], joystick_state[c].b[1], joysticks_present);
|
||||
}
|
||||
}
|
||||
|
||||
for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++)
|
||||
{
|
||||
if (joystick_state[c].plat_joystick_nr)
|
||||
{
|
||||
int joystick_nr = joystick_state[c].plat_joystick_nr - 1;
|
||||
|
||||
for (d = 0; d < joystick_get_axis_count(joystick_type); d++)
|
||||
{
|
||||
if (joystick_state[c].axis_mapping[d] & POV_X)
|
||||
{
|
||||
int pov = plat_joystick_state[joystick_nr].p[joystick_state[c].axis_mapping[d] & 3];
|
||||
|
||||
if (LOWORD(pov) == 0xFFFF)
|
||||
joystick_state[c].axis[d] = 0;
|
||||
else
|
||||
joystick_state[c].axis[d] = sin((2*M_PI * (double)pov) / 36000.0) * 32767;
|
||||
}
|
||||
else if (joystick_state[c].axis_mapping[d] & POV_Y)
|
||||
{
|
||||
int pov = plat_joystick_state[joystick_nr].p[joystick_state[c].axis_mapping[d] & 3];
|
||||
|
||||
if (LOWORD(pov) == 0xFFFF)
|
||||
joystick_state[c].axis[d] = 0;
|
||||
else
|
||||
joystick_state[c].axis[d] = -cos((2*M_PI * (double)pov) / 36000.0) * 32767;
|
||||
}
|
||||
else
|
||||
joystick_state[c].axis[d] = plat_joystick_state[joystick_nr].a[plat_joystick_state[joystick_nr].axis[joystick_state[c].axis_mapping[d]].id];
|
||||
}
|
||||
for (d = 0; d < joystick_get_button_count(joystick_type); d++)
|
||||
joystick_state[c].button[d] = plat_joystick_state[joystick_nr].b[joystick_state[c].button_mapping[d]];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (d = 0; d < joystick_get_axis_count(joystick_type); d++)
|
||||
joystick_state[c].axis[d] = 0;
|
||||
for (d = 0; d < joystick_get_button_count(joystick_type); d++)
|
||||
joystick_state[c].button[d] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue