Skip to content
Permalink
Browse files

Add default bindings for the keyboard

  • Loading branch information...
ergo720 committed Sep 9, 2019
1 parent 278d511 commit f6e331f484087cd991de7bdd1d603ecfbe7e5ff7
@@ -153,7 +153,7 @@ BEGIN
GROUPBOX "Rumble", IDC_XID_RUMBLE, 396, 212, 121, 34, WS_GROUP
PUSHBUTTON "", IDC_SET_MOTOR, 443, 224, 57, 14, BS_FLAT
LTEXT "Motor", IDC_STATIC, 412, 224, 26, 14, SS_CENTERIMAGE
PUSHBUTTON "XInput Default", IDC_XINP_DEFAULT, 362, 256, 69, 14, BS_FLAT
PUSHBUTTON "Default Bindings", IDC_XID_DEFAULT, 362, 256, 69, 14, BS_FLAT
PUSHBUTTON "Clear", IDC_XID_CLEAR, 443, 256, 50, 14, BS_FLAT
END

@@ -40,11 +40,6 @@ void Button::UpdateText(const char* text) const
SendMessage(m_button_hwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(text));
}

void Button::UpdateText() const // xinput specific
{
SendMessage(m_button_hwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(m_xinput_button.c_str()));
}

void Button::ClearText() const
{
SendMessage(m_button_hwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(""));
@@ -55,6 +50,12 @@ void Button::GetText(char* const text, size_t size) const
SendMessage(m_button_hwnd, WM_GETTEXT, size, reinterpret_cast<LPARAM>(text));
}

std::string Button::GetName(int api, int idx) const
{
assert(api == XINPUT_DEFAULT || api == DINPUT_DEFAULT);
return button_xbox_ctrl_names[idx][api];
}

LRESULT CALLBACK ButtonSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
@@ -38,21 +38,17 @@
class Button
{
public:
Button(const char* name, const char* xname, int id, int index, HWND hwnd) : m_name(name),
m_xinput_button(xname), m_id(id), m_index(index), m_button_hwnd(GetDlgItem(hwnd, m_id)) {};
Button(int id, int index, HWND hwnd) : m_id(id), m_index(index), m_button_hwnd(GetDlgItem(hwnd, m_id)) {};
void EnableButton(bool enable) const;
void UpdateText(const char* text) const;
void UpdateText() const;
void ClearText() const;
void GetText(char* const text, size_t size) const;
std::string GetName() const { return m_name; }
std::string GetName(int api, int idx) const;
int GetId() const { return m_id; }
int GetIndex() const { return m_index; }


private:
std::string m_name;
std::string m_xinput_button;
int m_id;
int m_index;
HWND m_button_hwnd;
@@ -57,32 +57,32 @@ static int button_xbox_ctrl_id[XBOX_CTRL_NUM_BUTTONS] = {
IDC_SET_MOTOR,
};

const char* button_xbox_ctrl_names[XBOX_CTRL_NUM_BUTTONS][2] = {
"D Pad Up", "Pad N",
"D Pad Down", "Pad S",
"D Pad Left", "Pad W",
"D Pad Right", "Pad E",
"Start", "Start",
"Back", "Back",
"L Thumb", "Thumb L",
"R Thumb", "Thumb R",
"A", "Button A",
"B", "Button B",
"X", "Button X",
"Y", "Button Y",
"Black", "Shoulder R",
"White", "Shoulder L",
"L Trigger", "Trigger L",
"R Trigger", "Trigger R",
"Left Axis X+", "Left X+",
"Left Axis X-", "Left X-",
"Left Axis Y+", "Left Y+",
"Left Axis Y-", "Left Y-",
"Right Axis X+", "Right X+",
"Right Axis X-", "Right X-",
"Right Axis Y+", "Right Y+",
"Right Axis Y-", "Right Y-",
"Motor", "LeftRight",
const char* button_xbox_ctrl_names[XBOX_CTRL_NUM_BUTTONS][3] = {
"D Pad Up", "Pad N", "UP",
"D Pad Down", "Pad S", "DOWN",
"D Pad Left", "Pad W", "LEFT",
"D Pad Right", "Pad E", "RIGHT",
"Start", "Start", "RETURN",
"Back", "Back", "SPACE",
"L Thumb", "Thumb L", "B",
"R Thumb", "Thumb R", "M",
"A", "Button A", "S",
"B", "Button B", "D",
"X", "Button X", "W",
"Y", "Button Y", "E",
"Black", "Shoulder R", "C",
"White", "Shoulder L", "X",
"L Trigger", "Trigger L", "Q",
"R Trigger", "Trigger R", "R",
"Left Axis X+", "Left X+", "H",
"Left Axis X-", "Left X-", "F",
"Left Axis Y+", "Left Y+", "T",
"Left Axis Y-", "Left Y-", "G",
"Right Axis X+", "Right X+", "L",
"Right Axis X-", "Right X-", "J",
"Right Axis Y+", "Right Y+", "I",
"Right Axis Y-", "Right Y-", "K",
"Motor", "LeftRight", "",
};


@@ -93,8 +93,7 @@ EmuDevice::EmuDevice(int type, HWND hwnd)
case to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_DUKE): {
m_hwnd = hwnd;
for (int i = 0; i < ARRAY_SIZE(button_xbox_ctrl_id); i++) {
m_buttons.push_back(new Button(button_xbox_ctrl_names[i][0], button_xbox_ctrl_names[i][1],
button_xbox_ctrl_id[i], i, hwnd));
m_buttons.push_back(new Button(button_xbox_ctrl_id[i], i, hwnd));

// Install the subclass for the button control
SetWindowSubclass(GetDlgItem(hwnd, button_xbox_ctrl_id[i]), ButtonSubclassProc, 0, reinterpret_cast<DWORD_PTR>(m_buttons[i]));
@@ -131,10 +130,10 @@ Button* EmuDevice::FindButtonByIndex(int index)
return m_buttons[index];
}

void EmuDevice::BindXInput()
void EmuDevice::BindDefault(int api)
{
std::for_each(m_buttons.begin(), m_buttons.end(), [](const auto button) {
button->UpdateText();
std::for_each(m_buttons.begin(), m_buttons.end(), [&api](const auto button) {
button->UpdateText(button->GetName(api, button->GetIndex()).c_str());
});
}

@@ -30,7 +30,7 @@
#include "Button.h"
#include "common\util\CxbxUtil.h"

extern const char* button_xbox_ctrl_names[XBOX_CTRL_NUM_BUTTONS][2];
extern const char* button_xbox_ctrl_names[XBOX_CTRL_NUM_BUTTONS][3];
extern int dev_num_buttons[to_underlying(XBOX_INPUT_DEVICE::DEVICE_MAX)];


@@ -42,7 +42,7 @@ class EmuDevice
~EmuDevice();
Button* FindButtonById(int id);
Button* FindButtonByIndex(int index);
void BindXInput();
void BindDefault(int api);
void ClearButtons();


@@ -54,6 +54,7 @@ void InputWindow::Initialize(HWND hwnd, int port_num, int dev_type)
m_hwnd_window = hwnd;
m_hwnd_device_list = GetDlgItem(m_hwnd_window, IDC_DEVICE_LIST);
m_hwnd_profile_list = GetDlgItem(m_hwnd_window, IDC_XID_PROFILE_NAME);
m_hwnd_default = GetDlgItem(m_hwnd_window, IDC_XID_DEFAULT);
m_dev_type = dev_type;
m_max_num_buttons = dev_num_buttons[dev_type];
m_port_num = port_num;
@@ -232,10 +233,11 @@ void InputWindow::BindButton(int ControlID)
}
}

void InputWindow::BindXInput()
void InputWindow::BindDefault()
{
if (std::strncmp(m_host_dev.c_str(), "XInput", std::strlen("XInput")) == 0) {
m_DeviceConfig->BindXInput();
int api = EnableDefaultButton();
if (api != -1) {
m_DeviceConfig->BindDefault(api);
m_bHasChanges = true;
}
}
@@ -247,6 +249,22 @@ void InputWindow::ClearBindings()
m_bHasChanges = true;
}

int InputWindow::EnableDefaultButton()
{
if (std::strncmp(m_host_dev.c_str(), "XInput", std::strlen("XInput")) == 0) {
EnableWindow(m_hwnd_default, TRUE);
return XINPUT_DEFAULT;
}
else if (std::strncmp(m_host_dev.c_str(), "DInput", std::strlen("DInput")) == 0) {
EnableWindow(m_hwnd_default, TRUE);
return DINPUT_DEFAULT;
}
else {
EnableWindow(m_hwnd_default, FALSE);
return -1;
}
}

InputWindow::ProfileIt InputWindow::FindProfile(std::string& name)
{
auto it = std::find_if(g_Settings->m_input_profiles[m_dev_type].begin(),
@@ -297,6 +315,7 @@ void InputWindow::LoadProfile(std::string& name)
return;
}
m_host_dev = profile->DeviceName;
EnableDefaultButton();
LRESULT dev_str_index = SendMessage(m_hwnd_device_list, CB_FINDSTRINGEXACT, 1, reinterpret_cast<LPARAM>(m_host_dev.c_str()));
if (dev_str_index == CB_ERR) {
SendMessage(m_hwnd_device_list, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(m_host_dev.c_str()));
@@ -391,6 +410,7 @@ void InputWindow::UpdateCurrentDevice()
char device_name[50];
SendMessage(m_hwnd_device_list, WM_GETTEXT, sizeof(device_name), reinterpret_cast<LPARAM>(device_name));
m_host_dev = device_name;
EnableDefaultButton();
}

void InputWindow::InitRumble(HWND hwnd)
@@ -40,6 +40,8 @@
#define RUMBLE_CLEAR 7
#define BUTTON_CLEAR 8

#define XINPUT_DEFAULT 1
#define DINPUT_DEFAULT 2

LRESULT CALLBACK ProfileNameSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

@@ -52,7 +54,7 @@ class InputWindow
~InputWindow();
void UpdateDeviceList();
void BindButton(int ControlID);
void BindXInput();
void BindDefault();
void ClearBindings();
void UpdateProfile(std::string& name, int command);
void UpdateRumble(int command);
@@ -70,6 +72,7 @@ class InputWindow
void DeleteProfile(std::string& name);
void OverwriteProfile(std::string& name);
void LoadDefaultProfile();
int EnableDefaultButton();

// xbox device under configuration
EmuDevice* m_DeviceConfig;
@@ -83,6 +86,8 @@ class InputWindow
HWND m_hwnd_rumble;
// handle of the rumble combobox
HWND m_hwnd_rumble_list;
// handle of the default bindings button
HWND m_hwnd_default;
// type of the device
int m_dev_type;
// num of buttons of device under configuration
@@ -99,9 +99,9 @@ INT_PTR CALLBACK DlgXidControllerConfigProc(HWND hWndDlg, UINT uMsg, WPARAM wPar
}
break;

case IDC_XINP_DEFAULT: {
case IDC_XID_DEFAULT: {
if (HIWORD(wParam) == BN_CLICKED) {
g_InputWindow->BindXInput();
g_InputWindow->BindDefault();
}
}
break;
@@ -282,7 +282,7 @@
#define IDC_CONFIGURE_PORT4 1284
#define IDC_DEVICE_LIST 1285
#define IDC_REFRESH_DEVICES 1286
#define IDC_XINP_DEFAULT 1287
#define IDC_XID_DEFAULT 1287
#define IDC_XID_PROFILE 1288
#define IDC_XID_PROFILE_NAME 1289
#define IDC_XID_PROFILE_SAVE 1291

0 comments on commit f6e331f

Please sign in to comment.
You can’t perform that action at this time.