2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/Thread.h"
|
2016-06-25 19:46:39 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/XInput/XInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Xlib/XInput2.h"
|
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/OSX/OSX.h"
|
2016-08-08 16:38:22 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Quartz/Quartz.h"
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/SDL/SDL.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Android/Android.h"
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2015-06-29 00:17:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/evdev/evdev.h"
|
2015-06-29 00:17:35 +00:00
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#ifdef CIFACE_USE_PIPES
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Pipes/Pipes.h"
|
2015-10-25 03:20:03 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
using namespace ciface::ExpressionParser;
|
|
|
|
|
2011-09-01 01:46:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
2014-11-09 20:02:18 +00:00
|
|
|
const ControlState INPUT_DETECT_THRESHOLD = 0.55;
|
2011-09-01 01:46:04 +00:00
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-10-12 19:42:29 +00:00
|
|
|
ControllerInterface g_controller_interface;
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Init
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Detect devices and inputs outputs / will make refresh function later
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-10-13 16:45:47 +00:00
|
|
|
void ControllerInterface::Initialize(void* const hwnd)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_is_init)
|
|
|
|
return;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_hwnd = hwnd;
|
2014-10-13 16:45:47 +00:00
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::DInput::Init((HWND)hwnd);
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-06-12 12:57:28 +00:00
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::XInput::Init();
|
2010-06-12 12:57:28 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::XInput2::Init(hwnd);
|
2016-06-24 08:43:46 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::OSX::Init(hwnd);
|
2016-08-08 16:38:22 +00:00
|
|
|
ciface::Quartz::Init(hwnd);
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::SDL::Init();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::Android::Init();
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2015-06-29 00:17:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::evdev::Init();
|
2015-06-29 00:17:35 +00:00
|
|
|
#endif
|
2015-10-25 03:20:03 +00:00
|
|
|
#ifdef CIFACE_USE_PIPES
|
2016-06-12 15:08:04 +00:00
|
|
|
ciface::Pipes::Init();
|
2015-10-25 03:20:03 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_init = true;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:45:47 +00:00
|
|
|
void ControllerInterface::Reinitialize()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
2014-10-13 16:45:47 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Shutdown();
|
|
|
|
Initialize(m_hwnd);
|
2014-10-13 16:45:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeInit
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Remove all devices/ call library cleanup functions
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2010-10-12 19:42:29 +00:00
|
|
|
void ControllerInterface::Shutdown()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_is_init)
|
|
|
|
return;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::XInput::DeInit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::OSX::DeInit();
|
2016-08-08 16:38:22 +00:00
|
|
|
ciface::Quartz::DeInit();
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
|
|
|
|
SDL_Quit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
2016-06-24 08:43:46 +00:00
|
|
|
// nothing needed
|
2013-04-15 04:02:53 +00:00
|
|
|
#endif
|
2016-07-14 15:50:35 +00:00
|
|
|
#ifdef CIFACE_USE_EVDEV
|
|
|
|
ciface::evdev::Shutdown();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
{
|
|
|
|
// Set outputs to ZERO before destroying device
|
|
|
|
for (ciface::Core::Device::Output* o : d->Outputs())
|
|
|
|
o->SetState(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_devices.clear();
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_is_init = false;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device)
|
2016-06-12 15:08:04 +00:00
|
|
|
{
|
2016-06-12 15:31:41 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
2016-07-14 08:25:52 +00:00
|
|
|
// Try to find an ID for this device
|
|
|
|
int id = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const auto it = std::find_if(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) {
|
|
|
|
return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() &&
|
|
|
|
d->GetId() == id;
|
|
|
|
});
|
|
|
|
if (it == m_devices.end()) // no device with the same name with this ID, so we can use it
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
device->SetId(id);
|
2016-06-25 19:46:39 +00:00
|
|
|
m_devices.emplace_back(std::move(device));
|
2016-06-12 15:08:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
|
|
|
|
[&callback](const auto& dev) { return callback(dev.get()); }),
|
|
|
|
m_devices.end());
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// UpdateInput
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2016-01-09 18:42:56 +00:00
|
|
|
// Update input for all devices
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-11-13 08:55:14 +00:00
|
|
|
void ControllerInterface::UpdateInput()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-07-15 09:34:18 +00:00
|
|
|
// Don't block the UI or CPU thread (to avoid a short but noticeable frame drop)
|
|
|
|
if (m_devices_mutex.try_lock())
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex, std::adopt_lock);
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
d->UpdateInput();
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:11:47 +00:00
|
|
|
//
|
|
|
|
// RegisterHotplugCallback
|
|
|
|
//
|
|
|
|
// Register a callback to be called from the input backends' hotplug thread
|
|
|
|
// when there is a new device
|
|
|
|
//
|
|
|
|
void ControllerInterface::RegisterHotplugCallback(std::function<void()> callback)
|
|
|
|
{
|
|
|
|
m_hotplug_callbacks.emplace_back(std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// InvokeHotplugCallbacks
|
|
|
|
//
|
|
|
|
// Invoke all callbacks that were registered
|
|
|
|
//
|
|
|
|
void ControllerInterface::InvokeHotplugCallbacks() const
|
|
|
|
{
|
|
|
|
for (const auto& callback : m_hotplug_callbacks)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// InputReference :: State
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Gets the state of an input reference
|
2010-04-02 02:48:24 +00:00
|
|
|
// override function for ControlReference::State ...
|
|
|
|
//
|
2016-06-24 08:43:46 +00:00
|
|
|
ControlState ControllerInterface::InputReference::State(const ControlState ignore)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (parsed_expression)
|
|
|
|
return parsed_expression->GetValue() * range;
|
|
|
|
else
|
|
|
|
return 0.0;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// OutputReference :: State
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Set the state of all binded outputs
|
2016-06-24 08:43:46 +00:00
|
|
|
// overrides ControlReference::State .. combined them so I could make the GUI simple / inputs ==
|
|
|
|
// same as outputs one list
|
2014-01-31 00:51:21 +00:00
|
|
|
// I was lazy and it works so watever
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2010-06-21 03:12:16 +00:00
|
|
|
ControlState ControllerInterface::OutputReference::State(const ControlState state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (parsed_expression)
|
|
|
|
parsed_expression->SetValue(state);
|
|
|
|
return 0.0;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// UpdateReference
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Updates a controlreference's binded devices/controls
|
2010-06-21 03:12:16 +00:00
|
|
|
// need to call this to re-parse a control reference's expression after changing it
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2016-06-24 08:43:46 +00:00
|
|
|
void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref,
|
|
|
|
const ciface::Core::DeviceQualifier& default_device) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
delete ref->parsed_expression;
|
|
|
|
ref->parsed_expression = nullptr;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ControlFinder finder(*this, default_device, ref->is_input);
|
|
|
|
ref->parse_error = ParseExpression(ref->expression, finder, &ref->parsed_expression);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// InputReference :: Detect
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Wait for input on all binded devices
|
2010-04-02 02:48:24 +00:00
|
|
|
// supports not detecting inputs that were held down at the time of Detect start,
|
|
|
|
// which is useful for those crazy flightsticks that have certain buttons that are always held down
|
|
|
|
// or some crazy axes or something
|
2010-06-21 03:12:16 +00:00
|
|
|
// upon input, return pointer to detected Control
|
2014-03-09 20:14:26 +00:00
|
|
|
// else return nullptr
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::Core::Device::Control*
|
|
|
|
ControllerInterface::InputReference::Detect(const unsigned int ms,
|
|
|
|
ciface::Core::Device* const device)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
unsigned int time = 0;
|
|
|
|
std::vector<bool> states(device->Inputs().size());
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (device->Inputs().size() == 0)
|
|
|
|
return nullptr;
|
2011-01-30 08:12:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// get starting state of all inputs,
|
|
|
|
// so we can ignore those that were activated at time of Detect start
|
|
|
|
std::vector<ciface::Core::Device::Input *>::const_iterator i = device->Inputs().begin(),
|
|
|
|
e = device->Inputs().end();
|
|
|
|
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i)
|
|
|
|
*state++ = ((*i)->GetState() > (1 - INPUT_DETECT_THRESHOLD));
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
while (time < ms)
|
|
|
|
{
|
|
|
|
device->UpdateInput();
|
|
|
|
i = device->Inputs().begin();
|
|
|
|
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i, ++state)
|
|
|
|
{
|
|
|
|
// detected an input
|
|
|
|
if ((*i)->IsDetectable() && (*i)->GetState() > INPUT_DETECT_THRESHOLD)
|
|
|
|
{
|
|
|
|
// input was released at some point during Detect call
|
|
|
|
// return the detected input
|
|
|
|
if (false == *state)
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
else if ((*i)->GetState() < (1 - INPUT_DETECT_THRESHOLD))
|
|
|
|
{
|
|
|
|
*state = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Common::SleepCurrentThread(10);
|
|
|
|
time += 10;
|
|
|
|
}
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// no input was detected
|
|
|
|
return nullptr;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// OutputReference :: Detect
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2016-06-24 08:43:46 +00:00
|
|
|
// Totally different from the inputReference detect / I have them combined so it was simpler to make
|
|
|
|
// the GUI.
|
|
|
|
// The GUI doesn't know the difference between an input and an output / it's odd but I was lazy and
|
|
|
|
// it was easy
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
|
|
|
// set all binded outputs to <range> power for x milliseconds return false
|
|
|
|
//
|
2016-06-24 08:43:46 +00:00
|
|
|
ciface::Core::Device::Control*
|
|
|
|
ControllerInterface::OutputReference::Detect(const unsigned int ms,
|
|
|
|
ciface::Core::Device* const device)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// ignore device
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// don't hang if we don't even have any controls mapped
|
|
|
|
if (BoundCount() > 0)
|
|
|
|
{
|
|
|
|
State(1);
|
|
|
|
unsigned int slept = 0;
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// this loop is to make stuff like flashing keyboard LEDs work
|
|
|
|
while (ms > (slept += 10))
|
|
|
|
Common::SleepCurrentThread(10);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
State(0);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|