2010-04-02 02:48:24 +00:00
|
|
|
#include "ControllerInterface.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#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
|
|
|
|
#include "Thread.h"
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
#define INPUT_DETECT_THRESHOLD 0.85
|
|
|
|
|
2010-10-12 19:42:29 +00:00
|
|
|
ControllerInterface g_controller_interface;
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
|
|
|
// Init
|
|
|
|
//
|
|
|
|
// detect devices and inputs outputs / will make refresh function later
|
|
|
|
//
|
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
|
2010-06-21 03:12:16 +00:00
|
|
|
ciface::Xlib::Init(m_devices, m_hwnd);
|
2010-04-02 02:48:24 +00:00
|
|
|
#endif
|
2010-04-02 09:41:43 +00:00
|
|
|
#ifdef CIFACE_USE_OSX
|
2010-06-21 03:12:16 +00:00
|
|
|
ciface::OSX::Init(m_devices);
|
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
|
|
|
|
|
|
|
|
m_is_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DeInit
|
|
|
|
//
|
|
|
|
// remove all devices/ call library cleanup functions
|
|
|
|
//
|
2010-10-12 19:42:29 +00:00
|
|
|
void ControllerInterface::Shutdown()
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-10-12 19:42:29 +00:00
|
|
|
if (false == m_is_init)
|
2010-04-02 02:48:24 +00:00
|
|
|
return;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device*>::const_iterator
|
|
|
|
d = m_devices.begin(),
|
|
|
|
de = m_devices.end();
|
|
|
|
for ( ;d != de; ++d )
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device::Output*>::const_iterator
|
|
|
|
o = (*d)->Outputs().begin(),
|
|
|
|
oe = (*d)->Outputs().end();
|
2010-04-02 02:48:24 +00:00
|
|
|
// set outputs to ZERO before destroying device
|
2010-06-21 03:12:16 +00:00
|
|
|
for ( ;o!=oe; ++o)
|
|
|
|
(*d)->SetOutputState(*o, 0);
|
2010-04-02 02:48:24 +00:00
|
|
|
// update output
|
|
|
|
(*d)->UpdateOutput();
|
2010-06-06 03:52:11 +00:00
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//delete device
|
|
|
|
delete *d;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_devices.clear();
|
|
|
|
|
|
|
|
#ifdef CIFACE_USE_XINPUT
|
|
|
|
// nothing needed
|
|
|
|
#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
|
|
|
|
|
|
|
|
m_is_init = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SetHwnd
|
|
|
|
//
|
|
|
|
// sets the hwnd used for some crap when initializing, use before calling Init
|
|
|
|
//
|
|
|
|
void ControllerInterface::SetHwnd( void* const hwnd )
|
|
|
|
{
|
|
|
|
m_hwnd = hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// UpdateInput
|
|
|
|
//
|
|
|
|
// update input for all devices, return true if all devices returned successful
|
|
|
|
//
|
2010-07-26 05:30:50 +00:00
|
|
|
bool ControllerInterface::UpdateInput(const bool force)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-07-26 05:30:50 +00:00
|
|
|
if (force)
|
|
|
|
update_lock.Enter();
|
|
|
|
else if (false == update_lock.TryEnter())
|
|
|
|
return false;
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
size_t ok_count = 0;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device*>::const_iterator
|
|
|
|
d = m_devices.begin(),
|
|
|
|
e = m_devices.end();
|
2010-04-02 02:48:24 +00:00
|
|
|
for ( ;d != e; ++d )
|
|
|
|
{
|
2010-06-21 03:12:16 +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-07-26 05:30:50 +00:00
|
|
|
update_lock.Leave();
|
2010-06-21 03:12:16 +00:00
|
|
|
return (m_devices.size() == ok_count);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// UpdateOutput
|
|
|
|
//
|
|
|
|
// update output for all devices, return true if all devices returned successful
|
|
|
|
//
|
2010-07-26 05:30:50 +00:00
|
|
|
bool ControllerInterface::UpdateOutput(const bool force)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-07-26 05:30:50 +00:00
|
|
|
if (force)
|
|
|
|
update_lock.Enter();
|
|
|
|
else if (false == update_lock.TryEnter())
|
|
|
|
return false;
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
size_t ok_count = 0;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device*>::const_iterator
|
|
|
|
d = m_devices.begin(),
|
|
|
|
e = m_devices.end();
|
|
|
|
for (;d != e; ++d)
|
2010-04-02 02:48:24 +00:00
|
|
|
(*d)->UpdateOutput();
|
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
update_lock.Leave();
|
2010-06-21 03:12:16 +00:00
|
|
|
return (m_devices.size() == ok_count);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Device :: ~Device
|
|
|
|
//
|
|
|
|
// dtor, delete all inputs/outputs on device destruction
|
|
|
|
//
|
|
|
|
ControllerInterface::Device::~Device()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// delete inputs
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device::Input*>::iterator
|
|
|
|
i = m_inputs.begin(),
|
|
|
|
e = m_inputs.end();
|
2010-04-02 02:48:24 +00:00
|
|
|
for ( ;i!=e; ++i)
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// delete outputs
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<Device::Output*>::iterator
|
|
|
|
o = m_outputs.begin(),
|
|
|
|
e = m_outputs.end();
|
2010-04-02 02:48:24 +00:00
|
|
|
for ( ;o!=e; ++o)
|
|
|
|
delete *o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
void ControllerInterface::Device::AddInput(Input* const i)
|
|
|
|
{
|
|
|
|
m_inputs.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::Device::AddOutput(Output* const o)
|
|
|
|
{
|
|
|
|
m_outputs.push_back(o);
|
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
|
|
|
// Device :: ClearInputState
|
|
|
|
//
|
|
|
|
// device classes should override this func
|
|
|
|
// ControllerInterface will call this when the device returns failure durring UpdateInput
|
|
|
|
// used to try to set all buttons and axes to their default state when user unplugs a gamepad durring play
|
|
|
|
// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging
|
|
|
|
//
|
|
|
|
void ControllerInterface::Device::ClearInputState()
|
|
|
|
{
|
|
|
|
// this is going to be called for every UpdateInput call that fails
|
|
|
|
// kinda slow but, w/e, should only happen when user unplugs a device while playing
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// InputReference :: State
|
|
|
|
//
|
|
|
|
// get the state of an input reference
|
|
|
|
// override function for ControlReference::State ...
|
|
|
|
//
|
|
|
|
ControlState ControllerInterface::InputReference::State( const ControlState ignore )
|
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
//if (NULL == device)
|
|
|
|
//return 0;
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-02 20:35:12 +00:00
|
|
|
ControlState state = 0;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::vector<DeviceControl>::const_iterator
|
|
|
|
ci = m_controls.begin(),
|
|
|
|
ce = m_controls.end();
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
// bit of hax for NOT to work at start of expression
|
|
|
|
if (ci != ce)
|
|
|
|
if (ci->mode == 2)
|
2010-04-02 02:48:24 +00:00
|
|
|
state = 1;
|
2010-06-21 03:12:16 +00:00
|
|
|
|
|
|
|
for (; ci!=ce; ++ci)
|
|
|
|
{
|
|
|
|
const ControlState istate = ci->device->GetInputState((Device::Input*)ci->control);
|
|
|
|
|
|
|
|
switch (ci->mode)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
// OR
|
|
|
|
case 0 :
|
|
|
|
state = std::max(state, istate);
|
|
|
|
break;
|
|
|
|
// AND
|
|
|
|
case 1 :
|
|
|
|
state = std::min(state, istate);
|
|
|
|
break;
|
|
|
|
// NOT
|
|
|
|
case 2 :
|
|
|
|
state = std::max(std::min(state, 1.0f - istate), 0.0f);
|
|
|
|
break;
|
|
|
|
// ADD
|
|
|
|
case 3 :
|
|
|
|
state += istate;
|
|
|
|
break;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
return std::min(1.0f, state * range);
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// OutputReference :: State
|
|
|
|
//
|
|
|
|
// 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-06-21 03:12:16 +00:00
|
|
|
ControlState ControllerInterface::OutputReference::State(const ControlState state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
const ControlState tmp_state = std::min(1.0f, state * range);
|
|
|
|
|
|
|
|
// output ref just ignores the modes ( |&!... )
|
|
|
|
|
|
|
|
std::vector<DeviceControl>::iterator
|
|
|
|
ci = m_controls.begin(),
|
|
|
|
ce = m_controls.end();
|
|
|
|
for (; ci != ce; ++ci)
|
|
|
|
ci->device->SetOutputState((Device::Output*)ci->control, tmp_state);
|
2010-04-02 02:48:24 +00:00
|
|
|
|
|
|
|
return state; // just return the output, watever
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DeviceQualifier :: ToString
|
|
|
|
//
|
|
|
|
// get string from a device qualifier / serialize
|
|
|
|
//
|
|
|
|
std::string ControllerInterface::DeviceQualifier::ToString() const
|
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
if (source.empty() && (cid < 0) && name.empty())
|
2010-04-02 02:48:24 +00:00
|
|
|
return "";
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << source << '/';
|
2010-04-02 09:41:43 +00:00
|
|
|
if ( cid > -1 )
|
|
|
|
ss << cid;
|
2010-04-02 02:48:24 +00:00
|
|
|
ss << '/' << name;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DeviceQualifier :: FromString
|
|
|
|
//
|
|
|
|
// set a device qualifier from a string / unserialize
|
|
|
|
//
|
|
|
|
void ControllerInterface::DeviceQualifier::FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
std::istringstream ss(str);
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::getline(ss, source = "", '/');
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
// silly
|
|
|
|
std::getline(ss, name, '/');
|
2010-04-02 09:41:43 +00:00
|
|
|
std::istringstream(name) >> (cid = -1);
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
std::getline(ss, name = "");
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DeviceQualifier :: FromDevice
|
|
|
|
//
|
|
|
|
// set a device qualifier from a device
|
|
|
|
//
|
|
|
|
void ControllerInterface::DeviceQualifier::FromDevice(const ControllerInterface::Device* const dev)
|
|
|
|
{
|
|
|
|
name = dev->GetName();
|
2010-04-02 09:41:43 +00:00
|
|
|
cid = dev->GetId();
|
2010-04-02 02:48:24 +00:00
|
|
|
source= dev->GetSource();
|
|
|
|
}
|
|
|
|
|
2010-06-22 08:30:33 +00:00
|
|
|
bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::Device* const dev) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-22 08:30:33 +00:00
|
|
|
if (dev->GetId() == cid)
|
|
|
|
if (dev->GetName() == name)
|
|
|
|
if (dev->GetSource() == source)
|
2010-04-13 05:15:38 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 18:25:49 +00:00
|
|
|
bool ControllerInterface::Device::Control::operator==(const std::string& name) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 18:25:49 +00:00
|
|
|
return GetName() == name;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::DeviceQualifier& devq) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
if (cid == devq.cid)
|
|
|
|
if (name == devq.name)
|
|
|
|
if (source == devq.source)
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// UpdateReference
|
|
|
|
//
|
2010-06-21 03:12:16 +00:00
|
|
|
// updates a controlreference's binded devices/controls
|
|
|
|
// 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
|
|
|
|
, const ControllerInterface::DeviceQualifier& default_device) const
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
ref->m_controls.clear();
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
// adding | to parse the last item, silly
|
|
|
|
std::istringstream ss(ref->expression + '|');
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
const std::string mode_chars("|&!^");
|
2010-06-21 03:12:16 +00:00
|
|
|
|
|
|
|
ControlReference::DeviceControl devc;
|
|
|
|
|
|
|
|
std::string dev_str;
|
|
|
|
std::string ctrl_str;
|
|
|
|
|
|
|
|
char c = 0;
|
|
|
|
while (ss.read(&c, 1))
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
const size_t f = mode_chars.find(c);
|
|
|
|
|
|
|
|
if (mode_chars.npos != f)
|
|
|
|
{
|
|
|
|
// add ctrl
|
|
|
|
if (ctrl_str.size())
|
|
|
|
{
|
|
|
|
DeviceQualifier devq;
|
|
|
|
|
|
|
|
// using default device or alterate device inside `backticks`
|
|
|
|
if (dev_str.empty())
|
|
|
|
devq = default_device;
|
|
|
|
else
|
|
|
|
devq.FromString(dev_str);
|
|
|
|
|
|
|
|
// find device
|
2010-06-22 08:30:33 +00:00
|
|
|
devc.device = FindDevice(devq);
|
2010-04-02 02:48:24 +00:00
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
if (devc.device)
|
|
|
|
{
|
|
|
|
// control
|
|
|
|
|
|
|
|
// inputs or outputs, i don't like this
|
|
|
|
if (ref->is_input)
|
|
|
|
{
|
2010-06-21 18:25:49 +00:00
|
|
|
std::vector<Device::Input*>::const_iterator i;
|
|
|
|
|
|
|
|
// FIXME: Use std::find instead of a for loop
|
|
|
|
// i = std::find(devc.device->Inputs().begin(), devc.device->Inputs().end(), ctrl_str);
|
2010-06-22 08:30:33 +00:00
|
|
|
for(i = devc.device->Inputs().begin(); i < devc.device->Inputs().end(); ++i)
|
2010-06-21 18:25:49 +00:00
|
|
|
if(*(*i) == ctrl_str)
|
|
|
|
break;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
if (devc.device->Inputs().end() != i)
|
|
|
|
{
|
|
|
|
devc.control = *i;
|
|
|
|
ref->m_controls.push_back(devc);
|
|
|
|
}
|
2010-10-19 23:11:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// the input wasn't found, look through all the other devices
|
|
|
|
|
|
|
|
std::vector<Device*>::const_iterator
|
|
|
|
deviter = m_devices.begin(),
|
|
|
|
devend = m_devices.end();
|
|
|
|
|
|
|
|
for(; deviter != devend; ++deviter)
|
|
|
|
{
|
|
|
|
for(i = (*deviter)->Inputs().begin(); i < (*deviter)->Inputs().end(); ++i)
|
|
|
|
if(*(*i) == ctrl_str)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((*deviter)->Inputs().end() != i)
|
|
|
|
{
|
|
|
|
devc.device = *deviter;
|
|
|
|
devc.control = *i;
|
|
|
|
ref->m_controls.push_back(devc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-21 03:12:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-21 18:25:49 +00:00
|
|
|
std::vector<Device::Output*>::const_iterator i;
|
|
|
|
|
|
|
|
// FIXME: Use std::find instead of a for loop
|
|
|
|
// i = std::find(devc.device->Outputs().begin(), devc.device->Outputs().end(), ctrl_str);
|
2010-06-22 08:30:33 +00:00
|
|
|
for(i = devc.device->Outputs().begin(); i < devc.device->Outputs().end(); ++i)
|
2010-06-21 18:25:49 +00:00
|
|
|
if(*(*i) == ctrl_str)
|
|
|
|
break;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
if (devc.device->Outputs().end() != i)
|
|
|
|
{
|
|
|
|
devc.control = *i;
|
|
|
|
ref->m_controls.push_back(devc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset stuff for next ctrl
|
|
|
|
devc.mode = (int)f;
|
|
|
|
devc.device = NULL;
|
|
|
|
ctrl_str.clear();
|
|
|
|
}
|
|
|
|
else if ('`' == c)
|
|
|
|
{
|
|
|
|
// different device
|
2011-01-28 21:27:49 +00:00
|
|
|
if (false == /*XXX*/(bool)std::getline(ss, dev_str, '`'))
|
2010-06-21 03:12:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ctrl_str += c;
|
|
|
|
}
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 08:30:33 +00:00
|
|
|
ControllerInterface::Device* ControllerInterface::FindDevice(const ControllerInterface::DeviceQualifier& devq) const
|
|
|
|
{
|
|
|
|
std::vector<ControllerInterface::Device*>::const_iterator
|
|
|
|
di = m_devices.begin(),
|
|
|
|
de = m_devices.end();
|
|
|
|
for (; di!=de; ++di)
|
|
|
|
if (devq == *di)
|
|
|
|
return *di;
|
2010-06-21 18:25:49 +00:00
|
|
|
|
2010-06-22 08:30:33 +00:00
|
|
|
return NULL;
|
2010-06-21 18:25:49 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
//
|
|
|
|
// InputReference :: Detect
|
|
|
|
//
|
|
|
|
// wait for input on all binded devices
|
|
|
|
// 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
|
|
|
//
|
2010-06-21 03:12:16 +00:00
|
|
|
ControllerInterface::Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
|
|
|
unsigned int time = 0;
|
2010-06-21 03:12:16 +00:00
|
|
|
bool* const states = new bool[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;
|
|
|
|
|
2010-06-21 03:12:16 +00:00
|
|
|
// get starting state of all inputs,
|
|
|
|
// 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();
|
|
|
|
for (bool* state=states; i != e; ++i)
|
|
|
|
*state++ = (device->GetInputState(*i) > 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();
|
|
|
|
for (bool* state=states; i != e; ++i,++state)
|
2010-04-02 02:48:24 +00:00
|
|
|
{
|
2010-06-21 03:12:16 +00:00
|
|
|
// detected an input
|
|
|
|
if ((*i)->IsDetectable() && device->GetInputState(*i) > 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
|
|
|
}
|
|
|
|
else
|
2010-06-21 03:12:16 +00:00
|
|
|
*state = false;
|
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
|
|
|
|
|
|
|
delete[] states;
|
|
|
|
|
|
|
|
// no input was detected
|
|
|
|
return NULL;
|
2010-04-02 02:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// OutputReference :: Detect
|
|
|
|
//
|
|
|
|
// totally different from the inputReference detect / i have them combined so it was simplier to make the gui.
|
|
|
|
// the gui doesnt know the difference between an input and an output / its odd but i was lazy and it was easy
|
|
|
|
//
|
|
|
|
// set all binded outputs to <range> power for x milliseconds return false
|
|
|
|
//
|
2010-06-21 03:12:16 +00:00
|
|
|
ControllerInterface::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
|
|
|
|
|
2010-04-02 02:48:24 +00:00
|
|
|
// dont hang if we dont even have any controls mapped
|
2010-06-21 03:12:16 +00:00
|
|
|
if (m_controls.size())
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|