Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix POV bugs, simplify buttons #1

Merged
merged 3 commits into from
Jul 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ void Config::Load(const char *iniFileName)
control->Get("LargeControls", &bLargeControls, false);
// control->Get("KeyMapping",iMappingMap);
control->Get("AccelerometerToAnalogHoriz", &bAccelerometerToAnalogHoriz, false);
control->Get("ForceInputDevice", &iForceInputDevice, -1);
control->Get("RightStickBind", &iRightStickBind, 0);
control->Get("SwapDInputRightAxes", &iSwapRightAxes, 0);
control->Get("TouchButtonOpacity", &iTouchButtonOpacity, 65);
Expand Down Expand Up @@ -258,7 +257,6 @@ void Config::Save()
control->Set("LargeControls", bLargeControls);
// control->Set("KeyMapping",iMappingMap);
control->Set("AccelerometerToAnalogHoriz", bAccelerometerToAnalogHoriz);
control->Set("ForceInputDevice", iForceInputDevice);
control->Set("RightStickBind", iRightStickBind);
control->Set("SwapDInputRightAxes", iSwapRightAxes);
control->Set("TouchButtonOpacity", iTouchButtonOpacity);
Expand Down
1 change: 0 additions & 1 deletion Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ struct Config
int iSwapRightAxes;

// Control
int iForceInputDevice;
int iTouchButtonOpacity;
float fButtonScale;

Expand Down
20 changes: 0 additions & 20 deletions Windows/ControlMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@

#include "InputDevice.h"

struct RawInputState
{
UINT button;
UINT prevButton;
RawInputState() {
button = 0;
prevButton = 0;
}
};

#define POV_CODE_UP 0x0100
#define POV_CODE_DOWN 0x0200
#define POV_CODE_LEFT 0x0400
#define POV_CODE_RIGHT 0x0800
#define XBOX_CODE_LEFTTRIGGER 0x00010000
#define XBOX_CODE_RIGHTTRIGGER 0x00020000

#define CONTROLS_KEYBOARD_INDEX 0
#define CONTROLS_DIRECT_INPUT_INDEX 1
#define CONTROLS_XINPUT_INDEX 2
#define CONTROLS_KEYBOARD_ANALOG_INDEX 3
#define CONTROLS_DEVICE_NUM 4

244 changes: 72 additions & 172 deletions Windows/DinputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,21 @@
#undef max
#endif

static const struct {int from, to;} dinput_ctrl_map[] = {
{ 11, KEYCODE_BUTTON_THUMBR },
{ 10, KEYCODE_BUTTON_THUMBL },
{ 1, KEYCODE_BUTTON_B },
{ 2, KEYCODE_BUTTON_A },
{ 0, KEYCODE_BUTTON_Y },
{ 3, KEYCODE_BUTTON_X },
{ 8, KEYCODE_BUTTON_SELECT },
{ 9, KEYCODE_BUTTON_START },
{ 4, KEYCODE_BUTTON_L2 },
{ 5, KEYCODE_BUTTON_R2 },
{ 6, KEYCODE_BUTTON_L1 },
{ 7, KEYCODE_BUTTON_R1 },
{ POV_CODE_UP, KEYCODE_DPAD_UP },
{ POV_CODE_DOWN, KEYCODE_DPAD_DOWN },
{ POV_CODE_LEFT, KEYCODE_DPAD_LEFT },
{ POV_CODE_RIGHT, KEYCODE_DPAD_RIGHT },
// In order from 0. There can be 128, but most controllers do not have that many.
static const int dinput_buttons[] = {
KEYCODE_BUTTON_Y,
KEYCODE_BUTTON_A,
KEYCODE_BUTTON_B,
KEYCODE_BUTTON_X,
KEYCODE_BUTTON_L2,
KEYCODE_BUTTON_R2,
KEYCODE_BUTTON_L1,
KEYCODE_BUTTON_R1,
KEYCODE_BUTTON_SELECT,
KEYCODE_BUTTON_START,
KEYCODE_BUTTON_THUMBL,
KEYCODE_BUTTON_THUMBR,
KEYCODE_BUTTON_Z,
};

struct Stick {
Expand All @@ -60,8 +58,6 @@ struct Stick {

static Stick NormalizedDeadzoneFilter(short x, short y);

const unsigned int dinput_ctrl_map_size = sizeof(dinput_ctrl_map) / sizeof(dinput_ctrl_map[0]);

#define DIFF (JOY_POVRIGHT - JOY_POVFORWARD) / 2
#define JOY_POVFORWARD_RIGHT JOY_POVFORWARD + DIFF
#define JOY_POVRIGHT_BACKWARD JOY_POVRIGHT + DIFF
Expand Down Expand Up @@ -89,6 +85,8 @@ bool IsXInputDevice( const GUID* pGuidProductFromDirectInput ) {
DinputDevice::DinputDevice() {
pJoystick = NULL;
pDI = NULL;
memset(lastButtons_, 0, sizeof(lastButtons_));
memset(lastPOV_, 0, sizeof(lastPOV_));

if(FAILED(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&pDI,NULL)))
return;
Expand Down Expand Up @@ -151,7 +149,6 @@ DinputDevice::~DinputDevice() {
}

int DinputDevice::UpdateState(InputState &input_state) {
if (g_Config.iForceInputDevice == 0) return -1;
if (!pJoystick) return -1;

DIJOYSTATE2 js;
Expand Down Expand Up @@ -211,10 +208,64 @@ static Stick NormalizedDeadzoneFilter(short x, short y) {
}

void DinputDevice::ApplyButtons(DIJOYSTATE2 &state, InputState &input_state) {
BYTE *buttons = state.rgbButtons;
u32 downMask = 0x80;

for (int i = 0; i < ARRAY_SIZE(dinput_buttons); ++i) {
if (state.rgbButtons[i] == lastButtons_[i]) {
continue;
}

bool down = (state.rgbButtons[i] & downMask) == downMask;
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0;
key.flags = down ? KEY_DOWN : KEY_UP;
key.keyCode = dinput_buttons[i];
NativeKey(key);

lastButtons_[i] = state.rgbButtons[i];
}

// Now the POV hat, which can technically go in any degree but usually does not.
if (LOWORD(state.rgdwPOV[0]) != lastPOV_[0]) {
KeyInput dpad[4];
for (int i = 0; i < 4; ++i) {
dpad[i].deviceId = DEVICE_ID_PAD_0;
dpad[i].flags = KEY_UP;
}
dpad[0].keyCode = KEYCODE_DPAD_UP;
dpad[1].keyCode = KEYCODE_DPAD_LEFT;
dpad[2].keyCode = KEYCODE_DPAD_DOWN;
dpad[3].keyCode = KEYCODE_DPAD_RIGHT;

if (LOWORD(state.rgdwPOV[0]) != JOY_POVCENTERED) {
// These are the edges, so we use or.
if (state.rgdwPOV[0] >= JOY_POVLEFT_FORWARD || state.rgdwPOV[0] <= JOY_POVFORWARD_RIGHT) {
dpad[0].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVBACKWARD_LEFT && state.rgdwPOV[0] <= JOY_POVLEFT_FORWARD) {
dpad[1].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVRIGHT_BACKWARD && state.rgdwPOV[0] <= JOY_POVBACKWARD_LEFT) {
dpad[2].flags = KEY_DOWN;
}
if (state.rgdwPOV[0] >= JOY_POVFORWARD_RIGHT && state.rgdwPOV[0] <= JOY_POVRIGHT_BACKWARD) {
dpad[3].flags = KEY_DOWN;
}
}

NativeKey(dpad[0]);
NativeKey(dpad[1]);
NativeKey(dpad[2]);
NativeKey(dpad[3]);

lastPOV_[0] = LOWORD(state.rgdwPOV[0]);
}

// TODO: Remove this once proper analog stick
// binding is implemented.
const LONG rthreshold = 8000;

KeyInput RAS;
RAS.deviceId = DEVICE_ID_PAD_0;
switch (g_Config.iRightStickBind) {
Expand Down Expand Up @@ -411,156 +462,5 @@ void DinputDevice::ApplyButtons(DIJOYSTATE2 &state, InputState &input_state) {
}
break;
}

u32 downMask = 0x80;

for(u8 i = 0; i < dinput_ctrl_map_size; i++) {
if (dinput_ctrl_map[i].from < DIRECTINPUT_RGBBUTTONS_MAX && (state.rgbButtons[dinput_ctrl_map[i].from] & downMask)) {
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0;
key.flags = KEY_DOWN;
key.keyCode = dinput_ctrl_map[i].to;
NativeKey(key);

// Hack needed to let the special buttons work..
// TODO: Is there no better way to handle this with DirectInput?
switch(dinput_ctrl_map[i].to) {
case KEYCODE_BUTTON_THUMBL:
input_state.pad_buttons |= PAD_BUTTON_LEFT_THUMB;
break;
case KEYCODE_BUTTON_THUMBR:
input_state.pad_buttons |= PAD_BUTTON_BACK;
break;
case KEYCODE_BUTTON_L2:
input_state.pad_buttons |= PAD_BUTTON_LEFT_TRIGGER;
break;
case KEYCODE_BUTTON_R2:
input_state.pad_buttons |= PAD_BUTTON_RIGHT_TRIGGER;
break;
case KEYCODE_BACK:
input_state.pad_buttons |= PAD_BUTTON_BACK;
break;

}
}

if (dinput_ctrl_map[i].from < DIRECTINPUT_RGBBUTTONS_MAX && (state.rgbButtons[dinput_ctrl_map[i].from] == 0)) {
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0;
key.flags = KEY_UP;
key.keyCode = dinput_ctrl_map[i].to;
NativeKey(key);
}

// TODO: Is there really no better way to handle the POV buttons?
if(dinput_ctrl_map[i].from < DIRECTINPUT_RGBBUTTONS_MAX) {
KeyInput key;
key.deviceId = DEVICE_ID_PAD_0;
switch(state.rgdwPOV[0]) {
case JOY_POVFORWARD:
key.keyCode = KEYCODE_DPAD_UP;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVLEFT_FORWARD:
key.keyCode = KEYCODE_DPAD_UP;
key.flags = KEY_DOWN;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_LEFT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVFORWARD_RIGHT:
key.keyCode = KEYCODE_DPAD_UP;
key.flags = KEY_DOWN;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_RIGHT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVBACKWARD:
key.keyCode = KEYCODE_DPAD_DOWN;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVBACKWARD_LEFT:
key.keyCode = KEYCODE_DPAD_DOWN;
key.flags = KEY_DOWN;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_LEFT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVRIGHT_BACKWARD:
key.keyCode = KEYCODE_DPAD_DOWN;
key.flags = KEY_DOWN;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_LEFT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVLEFT:
key.keyCode = KEYCODE_DPAD_LEFT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

case JOY_POVRIGHT:
key.keyCode = KEYCODE_DPAD_RIGHT;
key.flags = KEY_DOWN;
NativeKey(key);
break;

default:
key.keyCode = KEYCODE_DPAD_UP;
key.flags = KEY_UP;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_DOWN;
key.flags = KEY_UP;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_LEFT;
key.flags = KEY_UP;
NativeKey(key);
key.keyCode = KEYCODE_DPAD_RIGHT;
key.flags = KEY_UP;
NativeKey(key);
break;
}
}
}
}

int DinputDevice::UpdateRawStateSingle(RawInputState &rawState) {
if (g_Config.iForceInputDevice == 0) return FALSE;
if (!pJoystick) return FALSE;

DIJOYSTATE2 js;

if (FAILED(pJoystick->Poll())) {
if(pJoystick->Acquire() == DIERR_INPUTLOST)
return FALSE;
}

if(FAILED(pJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js)))
return -1;
switch (js.rgdwPOV[0]) {
case JOY_POVFORWARD: rawState.button = POV_CODE_UP; return TRUE;
case JOY_POVBACKWARD: rawState.button = POV_CODE_DOWN; return TRUE;
case JOY_POVLEFT: rawState.button = POV_CODE_LEFT; return TRUE;
case JOY_POVRIGHT: rawState.button = POV_CODE_RIGHT; return TRUE;
}

for (int i = 0; i < DIRECTINPUT_RGBBUTTONS_MAX; i++) {
if (js.rgbButtons[i] & 0x80) {
rawState.button = i;
return TRUE;
}
}
return FALSE;
}
5 changes: 2 additions & 3 deletions Windows/DinputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "InputDevice.h"
#include "dinput.h"

struct RawInputState;

class DinputDevice :
public InputDevice
{
Expand All @@ -32,10 +30,11 @@ class DinputDevice :
~DinputDevice();
virtual int UpdateState(InputState &input_state);
virtual bool IsPad() { return true; }
int UpdateRawStateSingle(RawInputState &rawState);
private:
void ApplyButtons(DIJOYSTATE2 &state, InputState &input_state);
LPDIRECTINPUT8 pDI;
LPDIRECTINPUTDEVICE8 pJoystick;
bool analog;
BYTE lastButtons_[128];
WORD lastPOV_[4];
};
1 change: 0 additions & 1 deletion Windows/XinputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct Stick {
static Stick NormalizedDeadzoneFilter(short x, short y);

int XinputDevice::UpdateState(InputState &input_state) {
if (g_Config.iForceInputDevice > 0) return -1;
if (this->check_delay-- > 0) return -1;
XINPUT_STATE state;
ZeroMemory( &state, sizeof(XINPUT_STATE) );
Expand Down
2 changes: 0 additions & 2 deletions Windows/XinputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "InputDevice.h"
#include "Xinput.h"

struct RawInputState;

class XinputDevice :
public InputDevice
{
Expand Down