2014-02-17 10:18:15 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2013-06-18 12:09:20 +00:00
|
|
|
|
2013-10-26 10:36:20 +00:00
|
|
|
#include <unordered_map>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2014-08-09 13:49:45 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
|
|
|
#include "Common/Thread.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DolphinWX/Android/ButtonManager.h"
|
2013-06-18 12:09:20 +00:00
|
|
|
|
|
|
|
namespace ButtonManager
|
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
const std::string touchScreenKey = "Touchscreen";
|
2013-10-26 10:36:20 +00:00
|
|
|
std::unordered_map<std::string, InputDevice*> m_controllers;
|
2014-04-23 08:49:25 +00:00
|
|
|
std::vector<std::string> configStrings = {
|
2014-02-17 04:51:41 +00:00
|
|
|
"InputA",
|
|
|
|
"InputB",
|
|
|
|
"InputStart",
|
|
|
|
"InputX",
|
|
|
|
"InputY",
|
|
|
|
"InputZ",
|
|
|
|
"DPadUp",
|
|
|
|
"DPadDown",
|
|
|
|
"DPadLeft",
|
|
|
|
"DPadRight",
|
|
|
|
"MainUp",
|
|
|
|
"MainDown",
|
|
|
|
"MainLeft",
|
|
|
|
"MainRight",
|
|
|
|
"CStickUp",
|
|
|
|
"CStickDown",
|
|
|
|
"CStickLeft",
|
|
|
|
"CStickRight",
|
|
|
|
"InputL",
|
|
|
|
"InputR"
|
|
|
|
};
|
2014-04-23 08:49:25 +00:00
|
|
|
std::vector<ButtonType> configTypes = {
|
|
|
|
BUTTON_A,
|
|
|
|
BUTTON_B,
|
|
|
|
BUTTON_START,
|
|
|
|
BUTTON_X,
|
|
|
|
BUTTON_Y,
|
|
|
|
BUTTON_Z,
|
|
|
|
BUTTON_UP,
|
|
|
|
BUTTON_DOWN,
|
|
|
|
BUTTON_LEFT,
|
|
|
|
BUTTON_RIGHT,
|
|
|
|
STICK_MAIN_UP,
|
|
|
|
STICK_MAIN_DOWN,
|
|
|
|
STICK_MAIN_LEFT,
|
|
|
|
STICK_MAIN_RIGHT,
|
|
|
|
STICK_C_UP,
|
|
|
|
STICK_C_DOWN,
|
|
|
|
STICK_C_LEFT,
|
|
|
|
STICK_C_RIGHT,
|
|
|
|
TRIGGER_L,
|
|
|
|
TRIGGER_R
|
|
|
|
};
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-05-22 02:22:52 +00:00
|
|
|
static void AddBind(const std::string& dev, sBind *bind)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_controllers.find(dev);
|
|
|
|
if (it != m_controllers.end())
|
|
|
|
{
|
|
|
|
it->second->AddBind(bind);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_controllers[dev] = new InputDevice(dev);
|
|
|
|
m_controllers[dev]->AddBind(bind);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
// Initialize our touchScreenKey buttons
|
2013-11-24 21:04:53 +00:00
|
|
|
for (int a = 0; a < 4; ++a)
|
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_A, BIND_BUTTON, BUTTON_A, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_B, BIND_BUTTON, BUTTON_B, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_START, BIND_BUTTON, BUTTON_START, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_X, BIND_BUTTON, BUTTON_X, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_Y, BIND_BUTTON, BUTTON_Y, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_Z, BIND_BUTTON, BUTTON_Z, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_UP, BIND_BUTTON, BUTTON_UP, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_DOWN, BIND_BUTTON, BUTTON_DOWN, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_LEFT, BIND_BUTTON, BUTTON_LEFT, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, BUTTON_RIGHT, BIND_BUTTON, BUTTON_RIGHT, 1.0f));
|
2013-06-18 12:09:20 +00:00
|
|
|
|
2014-04-23 08:49:25 +00:00
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_MAIN_UP, BIND_AXIS, STICK_MAIN_UP, -1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_MAIN_DOWN, BIND_AXIS, STICK_MAIN_DOWN, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_MAIN_LEFT, BIND_AXIS, STICK_MAIN_LEFT, -1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_MAIN_RIGHT, BIND_AXIS, STICK_MAIN_RIGHT, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_C_UP, BIND_AXIS, STICK_C_UP, -1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_C_DOWN, BIND_AXIS, STICK_C_DOWN, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_C_LEFT, BIND_AXIS, STICK_C_LEFT, -1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, STICK_C_RIGHT, BIND_AXIS, STICK_C_RIGHT, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, TRIGGER_L, BIND_AXIS, TRIGGER_L, 1.0f));
|
|
|
|
AddBind(touchScreenKey, new sBind(a, TRIGGER_R, BIND_AXIS, TRIGGER_R, 1.0f));
|
2013-11-24 21:04:53 +00:00
|
|
|
}
|
2013-06-18 12:09:20 +00:00
|
|
|
// Init our controller bindings
|
|
|
|
IniFile ini;
|
|
|
|
ini.Load(File::GetUserPath(D_CONFIG_IDX) + std::string("Dolphin.ini"));
|
2014-04-23 08:49:25 +00:00
|
|
|
for (u32 a = 0; a < configStrings.size(); ++a)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2013-11-25 16:58:05 +00:00
|
|
|
for (int padID = 0; padID < 4; ++padID)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2013-11-25 16:58:05 +00:00
|
|
|
std::ostringstream config;
|
|
|
|
config << configStrings[a] << "_" << padID;
|
|
|
|
BindType type;
|
|
|
|
int bindnum;
|
|
|
|
char dev[128];
|
|
|
|
bool hasbind = false;
|
2014-04-23 08:49:25 +00:00
|
|
|
char modifier = '+';
|
2013-11-25 16:58:05 +00:00
|
|
|
std::string value;
|
2014-06-16 05:12:43 +00:00
|
|
|
ini.GetOrCreateSection("Android")->Get(config.str(), &value, "None");
|
2013-11-25 16:58:05 +00:00
|
|
|
if (value == "None")
|
|
|
|
continue;
|
|
|
|
if (std::string::npos != value.find("Axis"))
|
|
|
|
{
|
|
|
|
hasbind = true;
|
|
|
|
type = BIND_AXIS;
|
2014-07-23 14:03:49 +00:00
|
|
|
sscanf(value.c_str(), "Device '%127[^\']'-Axis %d%c", dev, &bindnum, &modifier);
|
2013-11-25 16:58:05 +00:00
|
|
|
}
|
|
|
|
else if (std::string::npos != value.find("Button"))
|
|
|
|
{
|
|
|
|
hasbind = true;
|
|
|
|
type = BIND_BUTTON;
|
2014-07-23 14:03:49 +00:00
|
|
|
sscanf(value.c_str(), "Device '%127[^\']'-Button %d", dev, &bindnum);
|
2013-11-25 16:58:05 +00:00
|
|
|
}
|
|
|
|
if (hasbind)
|
2014-04-23 08:49:25 +00:00
|
|
|
AddBind(std::string(dev), new sBind(padID, configTypes[a], type, bindnum, modifier == '-' ? -1.0f : 1.0f));
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-11-24 21:04:53 +00:00
|
|
|
bool GetButtonPressed(int padID, ButtonType button)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
bool pressed = m_controllers[touchScreenKey]->ButtonValue(padID, button);
|
2013-06-18 12:09:20 +00:00
|
|
|
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const auto& ctrl : m_controllers)
|
|
|
|
pressed |= ctrl.second->ButtonValue(padID, button);
|
2013-06-18 12:09:20 +00:00
|
|
|
|
|
|
|
return pressed;
|
|
|
|
}
|
2013-11-24 21:04:53 +00:00
|
|
|
float GetAxisValue(int padID, ButtonType axis)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
float value = m_controllers[touchScreenKey]->AxisValue(padID, axis);
|
|
|
|
if (value == 0.0f)
|
|
|
|
{
|
|
|
|
for (const auto& ctrl : m_controllers)
|
|
|
|
{
|
|
|
|
value = ctrl.second->AxisValue(padID, axis);
|
|
|
|
if (value != 0.0f)
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
2013-11-14 21:17:51 +00:00
|
|
|
}
|
2015-05-22 02:22:52 +00:00
|
|
|
bool GamepadEvent(const std::string& dev, int button, int action)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_controllers.find(dev);
|
|
|
|
if (it != m_controllers.end())
|
2015-02-23 02:36:30 +00:00
|
|
|
return it->second->PressEvent(button, action);
|
|
|
|
|
2013-06-18 12:09:20 +00:00
|
|
|
m_controllers[dev] = new InputDevice(dev);
|
2015-02-23 02:36:30 +00:00
|
|
|
return m_controllers[dev]->PressEvent(button, action);
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
2015-05-22 02:22:52 +00:00
|
|
|
void GamepadAxisEvent(const std::string& dev, int axis, float value)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_controllers.find(dev);
|
|
|
|
if (it != m_controllers.end())
|
|
|
|
{
|
|
|
|
it->second->AxisEvent(axis, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_controllers[dev] = new InputDevice(dev);
|
|
|
|
m_controllers[dev]->AxisEvent(axis, value);
|
|
|
|
}
|
|
|
|
void Shutdown()
|
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
for (const auto& controller : m_controllers)
|
|
|
|
delete controller.second;
|
2013-09-23 06:43:18 +00:00
|
|
|
m_controllers.clear();
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputDevice
|
2015-02-23 02:36:30 +00:00
|
|
|
bool InputDevice::PressEvent(int button, int action)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2015-02-23 02:36:30 +00:00
|
|
|
bool handled = false;
|
2014-04-23 08:49:25 +00:00
|
|
|
for (const auto& binding : _inputbinds)
|
|
|
|
{
|
|
|
|
if (binding.second->_bind == button)
|
|
|
|
{
|
|
|
|
if (binding.second->_bindtype == BIND_BUTTON)
|
|
|
|
_buttons[binding.second->_buttontype] = action == BUTTON_PRESSED ? true : false;
|
|
|
|
else
|
|
|
|
_axises[binding.second->_buttontype] = action == BUTTON_PRESSED ? 1.0f : 0.0f;
|
2015-02-23 02:36:30 +00:00
|
|
|
handled = true;
|
2014-04-23 08:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:36:30 +00:00
|
|
|
return handled;
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
2013-12-12 20:42:25 +00:00
|
|
|
void InputDevice::AxisEvent(int axis, float value)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
for (const auto& binding : _inputbinds)
|
|
|
|
{
|
|
|
|
if (binding.second->_bind == axis)
|
|
|
|
{
|
|
|
|
if (binding.second->_bindtype == BIND_AXIS)
|
|
|
|
_axises[binding.second->_buttontype] = value;
|
|
|
|
else
|
|
|
|
_buttons[binding.second->_buttontype] = value > 0.5f ? true : false;
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
2013-11-25 16:58:05 +00:00
|
|
|
bool InputDevice::ButtonValue(int padID, ButtonType button)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
const auto& binding = _inputbinds.find(std::make_pair(padID, button));
|
|
|
|
if (binding == _inputbinds.end())
|
2013-11-25 16:58:05 +00:00
|
|
|
return false;
|
2014-04-23 08:49:25 +00:00
|
|
|
|
|
|
|
if (binding->second->_bindtype == BIND_BUTTON)
|
|
|
|
return _buttons[binding->second->_buttontype];
|
2013-06-18 12:09:20 +00:00
|
|
|
else
|
2014-04-23 08:49:25 +00:00
|
|
|
return (_axises[binding->second->_buttontype] * binding->second->_neg) > 0.5f;
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
2013-11-25 16:58:05 +00:00
|
|
|
float InputDevice::AxisValue(int padID, ButtonType axis)
|
2013-06-18 12:09:20 +00:00
|
|
|
{
|
2014-04-23 08:49:25 +00:00
|
|
|
const auto& binding = _inputbinds.find(std::make_pair(padID, axis));
|
|
|
|
if (binding == _inputbinds.end())
|
2013-06-18 12:09:20 +00:00
|
|
|
return 0.0f;
|
2014-04-23 08:49:25 +00:00
|
|
|
|
|
|
|
if (binding->second->_bindtype == BIND_AXIS)
|
|
|
|
return _axises[binding->second->_buttontype] * binding->second->_neg;
|
2013-10-29 05:23:17 +00:00
|
|
|
else
|
2014-04-23 08:49:25 +00:00
|
|
|
return _buttons[binding->second->_buttontype] == BUTTON_PRESSED ? 1.0f : 0.0f;
|
2013-06-18 12:09:20 +00:00
|
|
|
}
|
|
|
|
}
|