2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
#include "ControllerInterface.h"
|
|
|
|
|
2014-01-19 16:11:07 +00:00
|
|
|
#if USE_EGL
|
2014-01-20 07:46:21 +00:00
|
|
|
#include "GLInterface/GLInterface.h"
|
2014-01-19 16:11:07 +00:00
|
|
|
#endif
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_XINPUT
|
|
|
|
#include "XInput/XInput.h"
|
|
|
|
#endif
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
|
|
|
#include "DInput/DInput.h"
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
|
|
|
#include "Xlib/Xlib.h"
|
2013-07-10 06:49:58 +00:00
|
|
|
#ifdef CIFACE_USE_X11_XINPUT2
|
|
|
|
#include "Xlib/XInput2.h"
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
|
|
|
#include "OSX/OSX.h"
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
|
|
|
#include "SDL/SDL.h"
|
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
|
|
|
#include "Android/Android.h"
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
#include "Thread.h"
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
using namespace ciface::ExpressionParser;
|
|
|
|
|
2011-09-01 01:46:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
2011-12-13 05:24:10 +00:00
|
|
|
const float INPUT_DETECT_THRESHOLD = 0.55f;
|
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
|
|
|
//
|
2010-10-12 19:42:29 +00:00
|
|
|
void ControllerInterface::Initialize()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-10-12 19:42:29 +00:00
|
|
|
if (m_is_init)
|
2010-04-02 02:48:24 +00:00
|
|
|
return;
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
#ifdef CIFACE_USE_DINPUT
|
2010-07-10 06:48:24 +00:00
|
|
|
ciface::DInput::Init(m_devices, (HWND)m_hwnd);
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-06-12 12:57:28 +00:00
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2010-06-21 03:12:16 +00:00
|
|
|
ciface::XInput::Init(m_devices);
|
2010-06-12 12:57:28 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_XLIB
|
2014-01-19 16:11:07 +00:00
|
|
|
#if USE_EGL
|
|
|
|
if (GLWin.platform == EGL_PLATFORM_X11) {
|
|
|
|
#endif
|
2010-06-21 03:12:16 +00:00
|
|
|
ciface::Xlib::Init(m_devices, m_hwnd);
|
2013-07-10 06:49:58 +00:00
|
|
|
#ifdef CIFACE_USE_X11_XINPUT2
|
|
|
|
ciface::XInput2::Init(m_devices, m_hwnd);
|
|
|
|
#endif
|
2014-01-19 16:11:07 +00:00
|
|
|
#if USE_EGL
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2013-01-18 07:32:07 +00:00
|
|
|
ciface::OSX::Init(m_devices, m_hwnd);
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2010-06-21 03:12:16 +00:00
|
|
|
ciface::SDL::Init(m_devices);
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
|
|
|
ciface::Android::Init(m_devices);
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
m_is_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
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
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
if (!m_is_init)
|
2010-04-02 02:48:24 +00:00
|
|
|
return;
|
|
|
|
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Device* d : m_devices)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
// Set outputs to ZERO before destroying device
|
|
|
|
for (Device::Output* o : d->Outputs())
|
|
|
|
o->SetState(0);
|
|
|
|
|
|
|
|
// Update output
|
|
|
|
d->UpdateOutput();
|
|
|
|
|
|
|
|
// Delete device
|
|
|
|
delete d;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_devices.clear();
|
|
|
|
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
2013-10-19 09:27:57 +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
|
2010-04-02 02:48:24 +00:00
|
|
|
// nothing needed
|
|
|
|
#endif
|
|
|
|
#ifdef CIFACE_USE_XLIB
|
|
|
|
// nothing needed
|
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2010-04-25 18:04:55 +00:00
|
|
|
ciface::OSX::DeInit();
|
2010-04-02 09:41:43 +00:00
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
#ifdef CIFACE_USE_SDL
|
2010-06-06 03:52:11 +00:00
|
|
|
// TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
|
2010-10-12 19:42:29 +00:00
|
|
|
SDL_Quit();
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2013-04-15 04:02:53 +00:00
|
|
|
#ifdef CIFACE_USE_ANDROID
|
|
|
|
// nothing needed
|
|
|
|
#endif
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
m_is_init = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// SetHwnd
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Sets the hwnd used for some crap when initializing, use before calling Init
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
|
|
|
void ControllerInterface::SetHwnd( void* const hwnd )
|
|
|
|
{
|
|
|
|
m_hwnd = hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// UpdateInput
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Update input for all devices, return true if all devices returned successful
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2010-07-26 05:30:50 +00:00
|
|
|
bool ControllerInterface::UpdateInput(const bool force)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-07 00:16:38 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
|
2011-03-05 06:11:26 +00:00
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
if (force)
|
2011-03-05 06:11:26 +00:00
|
|
|
lk.lock();
|
|
|
|
else if (!lk.try_lock())
|
2014-01-31 00:51:21 +00:00
|
|
|
return false;
|
2010-07-26 05:30:50 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
size_t ok_count = 0;
|
|
|
|
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Device* d : m_devices)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
if (d->UpdateInput())
|
2010-04-02 02:48:24 +00:00
|
|
|
++ok_count;
|
2010-06-05 05:30:23 +00:00
|
|
|
//else
|
|
|
|
// disabled. it might be causing problems
|
|
|
|
//(*d)->ClearInputState();
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
return (m_devices.size() == ok_count);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// UpdateOutput
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Update output for all devices, return true if all devices returned successful
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2010-07-26 05:30:50 +00:00
|
|
|
bool ControllerInterface::UpdateOutput(const bool force)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2011-03-07 00:16:38 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
|
2011-03-05 06:11:26 +00:00
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
if (force)
|
2011-03-05 06:11:26 +00:00
|
|
|
lk.lock();
|
|
|
|
else if (!lk.try_lock())
|
2013-04-15 02:53:10 +00:00
|
|
|
return false;
|
2010-07-26 05:30:50 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
size_t ok_count = 0;
|
|
|
|
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Device* d : m_devices)
|
2013-09-18 14:09:32 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
if (d->UpdateOutput())
|
2013-09-18 14:09:32 +00:00
|
|
|
++ok_count;
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
return (m_devices.size() == ok_count);
|
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 ...
|
|
|
|
//
|
|
|
|
ControlState ControllerInterface::InputReference::State( const ControlState ignore )
|
|
|
|
{
|
2013-06-14 03:09:55 +00:00
|
|
|
if (parsed_expression)
|
2013-09-11 22:48:04 +00:00
|
|
|
return parsed_expression->GetValue() * range;
|
2013-06-14 03:09:55 +00:00
|
|
|
else
|
|
|
|
return 0.0f;
|
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
|
|
|
|
// overrides ControlReference::State .. combined them so I could make the GUI simple / inputs == same as outputs one list
|
|
|
|
// 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
|
|
|
{
|
2013-06-14 03:09:55 +00:00
|
|
|
if (parsed_expression)
|
|
|
|
parsed_expression->SetValue(state);
|
|
|
|
return 0.0f;
|
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
|
|
|
//
|
2010-06-21 03:12:16 +00:00
|
|
|
void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref
|
2013-06-17 00:07:10 +00:00
|
|
|
, const DeviceQualifier& default_device) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2013-06-14 03:09:55 +00:00
|
|
|
delete ref->parsed_expression;
|
|
|
|
ref->parsed_expression = NULL;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
ControlFinder finder(*this, default_device, ref->is_input);
|
2013-07-22 06:36:26 +00:00
|
|
|
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
|
|
|
|
// else return NULL
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
2013-06-17 00:07:10 +00:00
|
|
|
Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
unsigned int time = 0;
|
2011-03-22 07:27:23 +00:00
|
|
|
std::vector<bool> states(device->Inputs().size());
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2011-01-30 08:12:34 +00:00
|
|
|
if (device->Inputs().size() == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
// get starting state of all inputs,
|
2010-06-21 03:12:16 +00:00
|
|
|
// so we can ignore those that were activated at time of Detect start
|
|
|
|
std::vector<Device::Input*>::const_iterator
|
|
|
|
i = device->Inputs().begin(),
|
|
|
|
e = device->Inputs().end();
|
2011-03-22 07:27:23 +00:00
|
|
|
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i)
|
2013-01-17 21:40:44 +00:00
|
|
|
*state++ = ((*i)->GetState() > (1 - INPUT_DETECT_THRESHOLD));
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
while (time < ms)
|
|
|
|
{
|
|
|
|
device->UpdateInput();
|
|
|
|
i = device->Inputs().begin();
|
2011-03-22 07:27:23 +00:00
|
|
|
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i,++state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
// detected an input
|
2011-03-14 01:20:11 +00:00
|
|
|
if ((*i)->IsDetectable() && (*i)->GetState() > INPUT_DETECT_THRESHOLD)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
// input was released at some point during Detect call
|
|
|
|
// return the detected input
|
|
|
|
if (false == *state)
|
|
|
|
return *i;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2013-01-17 21:40:44 +00:00
|
|
|
else if ((*i)->GetState() < (1 - INPUT_DETECT_THRESHOLD))
|
2013-04-15 02:53:10 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
*state = false;
|
2013-04-15 02:53:10 +00:00
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2010-06-21 03:12:16 +00:00
|
|
|
Common::SleepCurrentThread(10); time += 10;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2010-06-21 03:12:16 +00:00
|
|
|
|
|
|
|
// no input was detected
|
|
|
|
return NULL;
|
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
|
|
|
//
|
2013-04-15 02:53:10 +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
|
|
|
|
//
|
2013-06-17 00:07:10 +00:00
|
|
|
Device::Control* ControllerInterface::OutputReference::Detect(const unsigned int ms, Device* const device)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
// ignore device
|
|
|
|
|
2013-04-15 02:53:10 +00:00
|
|
|
// don't hang if we don't even have any controls mapped
|
2013-06-14 03:09:55 +00:00
|
|
|
if (BoundCount() > 0)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
State(1);
|
2010-04-02 02:48:24 +00:00
|
|
|
unsigned int slept = 0;
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// this loop is to make stuff like flashing keyboard LEDs work
|
2010-06-21 03:12:16 +00:00
|
|
|
while (ms > (slept += 10))
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-07-03 08:04:10 +00:00
|
|
|
// TODO: improve this to update more than just the default device's output
|
2010-04-02 02:48:24 +00:00
|
|
|
device->UpdateOutput();
|
2010-06-21 03:12:16 +00:00
|
|
|
Common::SleepCurrentThread(10);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
State(0);
|
2010-04-02 02:48:24 +00:00
|
|
|
device->UpdateOutput();
|
|
|
|
}
|
2010-06-21 03:12:16 +00:00
|
|
|
return NULL;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|