Plugin that should eventually replace the GCPad and emulated Wiimote plugin:

It's currently only really usable on Windows. Linux/OS X will need keyboard support added in, but SDL should work for joysticks on non-Windows without too many modifications.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5254 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2010-03-30 19:14:11 +00:00
parent d69abeac43
commit f51d8fffa0
31 changed files with 6108 additions and 0 deletions

View File

@ -0,0 +1,556 @@
#include "ControllerInterface.h"
#ifdef CIFACE_USE_XINPUT
#include "XInput/XInput.h"
#endif
#ifdef CIFACE_USE_DIRECTINPUT
#include "DirectInput/DirectInput.h"
#endif
#ifdef CIFACE_USE_XLIB
#include "Xlib/Xlib.h"
#endif
#ifdef CIFACE_USE_SDL
#include "SDL/SDL.h"
#endif
//#define MAX_DOUBLE_TAP_TIME 400
//#define MAX_HOLD_DOWN_TIME 400
#define INPUT_DETECT_THRESHOLD 0.85
//
// Init
//
// detect devices and inputs outputs / will make refresh function later
//
void ControllerInterface::Init()
{
if ( m_is_init )
return;
#ifdef CIFACE_USE_XINPUT
ciface::XInput::Init( m_devices );
#endif
#ifdef CIFACE_USE_DIRECTINPUT
ciface::DirectInput::Init( m_devices/*, (HWND)m_hwnd*/ );
#endif
#ifdef CIFACE_USE_XLIB
ciface::XLIB::Init( m_devices, m_hwnd );
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::Init( m_devices );
#endif
m_is_init = true;
}
//
// DeInit
//
// remove all devices/ call library cleanup functions
//
void ControllerInterface::DeInit()
{
if ( false == m_is_init )
return;
std::vector<Device*>::const_iterator d = m_devices.begin(),
Devices_end = m_devices.end();
for ( ;d != Devices_end; ++d )
{
std::vector<Device::Output*>::const_iterator o = (*d)->Outputs().begin(),
e = (*d)->Outputs().end();
// set outputs to ZERO before destroying device
for ( ;o!=e; ++o)
(*d)->SetOutputState( *o, 0 );
// update output
(*d)->UpdateOutput();
//delete device
delete *d;
}
m_devices.clear();
#ifdef CIFACE_USE_XINPUT
// nothing needed
#endif
#ifdef CIFACE_USE_DIRECTINPUT
// nothing needed
#endif
#ifdef CIFACE_USE_XLIB
// nothing needed
#endif
#ifdef CIFACE_USE_SDL
// there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
SDL_Quit();
#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
//
bool ControllerInterface::UpdateInput()
{
size_t ok_count = 0;
std::vector<Device*>::const_iterator d = m_devices.begin(),
e = m_devices.end();
for ( ;d != e; ++d )
{
if ( (*d)->UpdateInput() )
++ok_count;
else
(*d)->ClearInputState();
}
return ( m_devices.size() == ok_count );
}
//
// UpdateOutput
//
// update output for all devices, return true if all devices returned successful
//
bool ControllerInterface::UpdateOutput()
{
size_t ok_count = 0;
std::vector<Device*>::const_iterator d = m_devices.begin(),
e = m_devices.end();
for ( ;d != e; ++d )
(*d)->UpdateOutput();
return ( m_devices.size() == ok_count );
}
//
// Devices
//
// i dont really like this but,
// return : constant copy of the devices vector
//
const std::vector<ControllerInterface::Device*>& ControllerInterface::Devices()
{
return m_devices;
}
//
// Device :: ~Device
//
// dtor, delete all inputs/outputs on device destruction
//
ControllerInterface::Device::~Device()
{
{
// delete inputs
std::vector<Device::Input*>::iterator i = inputs.begin(),
e = inputs.end();
for ( ;i!=e; ++i)
delete *i;
}
{
// delete outputs
std::vector<Device::Output*>::iterator o = outputs.begin(),
e = outputs.end();
for ( ;o!=e; ++o)
delete *o;
}
}
//
// 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
}
//
// Device :: Inputs
//
// get a const version of the device's input vector
//
const std::vector<ControllerInterface::Device::Input*>& ControllerInterface::Device::Inputs()
{
return inputs;
}
//
// Device :: Outputs
//
// get a const version of the device's outputs vector
//
const std::vector<ControllerInterface::Device::Output*>& ControllerInterface::Device::Outputs()
{
return outputs;
}
//
// HasInit
//
// check if interface is inited
//
bool ControllerInterface::IsInit()
{
return m_is_init;
}
//
// InputReference :: State
//
// get the state of an input reference
// override function for ControlReference::State ...
//
ControlState ControllerInterface::InputReference::State( const ControlState ignore )
{
if ( NULL == device )
return 0;
ControlState state;
// this mode thing will be turned into a switch statement
switch ( mode )
{
// OR
case 0 :
{
state = 0;
std::vector<Device::Control*>::const_iterator ci = controls.begin(),
ce = controls.end();
for ( ; ci != ce; ++ci )
state = std::max( state, device->GetInputState( (Device::Input*)*ci ) ); // meh casting
break;
}
// AND
case 1 :
{
// TODO: i think i can remove the if here
state = 1;
bool is_bound = false;
std::vector<Device::Control*>::const_iterator ci = controls.begin(),
ce = controls.end();
for ( ; ci != ce; ++ci )
{
is_bound = true;
state = std::min( state, device->GetInputState( (Device::Input*)*ci ) ); // meh casting
}
if ( !is_bound )
state = 0;
break;
}
// NOT
case 2 :
{
state = 0;
std::vector<Device::Control*>::const_iterator ci = controls.begin(),
ce = controls.end();
for ( ; ci != ce; ++ci )
state = std::max( state, device->GetInputState( (Device::Input*)*ci ) ); // meh casting
state = std::max( 0.0, 1.0 - state );
break;
}
}
return std::min( 1.0f, state * range );
}
//
// 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
//
ControlState ControllerInterface::OutputReference::State( const ControlState state )
{
std::vector<Device::Control*>::iterator ci = controls.begin(),
ce = controls.end();
for ( ; ci != ce; ++ci )
device->SetOutputState( (Device::Output*)*ci, state * range ); // casting again
return state; // just return the output, watever
}
//
// DeviceQualifier :: ToString
//
// get string from a device qualifier / serialize
//
std::string ControllerInterface::DeviceQualifier::ToString() const
{
if ( source.empty() && (id < 0) && name.empty() )
return "";
std::ostringstream ss;
ss << source << '/';
if ( id > -1 )
ss << id;
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);
// good
std::getline( ss, source = "", '/' );
// dum
std::getline( ss, name, '/' );
std::istringstream(name) >> (id = -1);
// good
std::getline( ss, name = "");
}
//
// DeviceQualifier :: FromDevice
//
// set a device qualifier from a device
//
void ControllerInterface::DeviceQualifier::FromDevice(const ControllerInterface::Device* const dev)
{
name = dev->GetName();
id = dev->GetId();
source= dev->GetSource();
}
//
// DeviceQualifier = = Device*
//
// check if a device matches a device qualifier
//
bool ControllerInterface::DeviceQualifier::operator==(const ControllerInterface::Device* const dev) const
{
if ( dev->GetName() == name )
if ( dev->GetId() == id )
if ( dev->GetSource() == source )
return true;
return false;
}
//
// ControlQualifier = FromControl
//
// set a control qualifier from a device control
//
void ControllerInterface::ControlQualifier::FromControl(const ControllerInterface::Device::Control* const c)
{
// hardly needs a function for this
name = c->GetName();
}
//
// ControlQualifier = = Device :: Control*
//
// check if a control qualifier matches a device control
// if qualifier name is /.../ form, uses a regular expression match
// also |control1|control2| form, || matches all
//
bool ControllerInterface::ControlQualifier::operator==(const ControllerInterface::Device::Control* const control) const
{
if ( name.size() )
{
#ifdef CIFACE_USE_REGEX
if ( '/' == name[0] && '/' == (*name.rbegin()) ) // check if regex
{
wxRegEx re(
wxString::FromAscii(name.substr( 1, name.size()-2).c_str()).Prepend(wxT("^(")).Append(wxT(")$"))
, wxRE_ADVANCED | wxRE_EXTENDED | wxRE_NOSUB | wxRE_ICASE );
if ( re.IsValid() )
return re.Matches( wxString::FromAscii( control->GetName().c_str() ) );
else
return true;
}
#endif
if ( '|' == name[0] && '|' == (*name.rbegin()) ) // check if using |button1|button2| format
{
return ( name.find( '|' + control->GetName() + '|' ) != name.npos || "||" == name );
}
}
return (control->GetName() == name);
}
//
// UpdateReference
//
// updates a controlreference's binded devices then update binded controls
// need to call this after changing a device qualifier on a control reference
// if the device qualifier hasnt changed, the below functions: "UpdateControls" can be used
//
void ControllerInterface::UpdateReference( ControllerInterface::ControlReference* ref )
{
ref->device = NULL;
std::vector<Device*>::const_iterator d = m_devices.begin(),
e = m_devices.end();
for ( ; d!=e; ++d )
if ( ref->device_qualifier == *d )
{
ref->device = *d;
break;
}
ref->UpdateControls();
}
//
// InputReference :: UpdateControls
//
// after changing a control qualifier, need to call this to rebind the new matching controls
//
void ControllerInterface::InputReference::UpdateControls()
{
controls.clear();
if ( device )
{
std::vector<Device::Input*>::const_iterator i = device->Inputs().begin(),
e = device->Inputs().end();
for ( ;i != e; ++i )
if ( control_qualifier == *i )
controls.push_back( *i );
}
}
//
// OutputReference :: UpdateControls
//
// same as the inputRef version
// after changing a control qualifier, need to call this to rebind the new matching controls
//
void ControllerInterface::OutputReference::UpdateControls()
{
controls.clear();
if ( device )
{
std::vector<Device::Output*>::const_iterator i = device->Outputs().begin(),
e = device->Outputs().end();
for ( ;i != e; ++i )
if ( control_qualifier == *i )
controls.push_back( *i );
}
}
//
// InputReference :: Detect
//
// wait for input on all binded devices
// supports waiting for n number of inputs
// 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
// upon input, set control qualifier, update controls and return true
// else return false
//
bool ControllerInterface::InputReference::Detect( const unsigned int ms, const unsigned int count )
{
unsigned int time = 0;
// don't wait if we don't have a device
if ( device )
{
bool* const states = new bool[device->Inputs().size()];
std::vector<Device::Input*>::const_iterator i = device->Inputs().begin(),
e = device->Inputs().end();
for ( unsigned int n=0;i != e; ++i,++n )
states[n] = ( device->GetInputState( *i ) > INPUT_DETECT_THRESHOLD );
std::vector<Device::Control*> detected;
while ( (time < ms) && (detected.size() < count) )
{
device->UpdateInput();
std::vector<Device::Input*>::const_iterator i = device->Inputs().begin(),
e = device->Inputs().end();
for ( unsigned int n=0;i != e; ++i,++n )
{
if ( device->GetInputState( *i ) > INPUT_DETECT_THRESHOLD )
{
if ( false == states[n] )
if ( std::find( detected.begin(), detected.end(), *i ) == detected.end() )
detected.push_back( *i );
}
else
states[n] = false;
}
wxMilliSleep( 10 ); time += 10;
}
delete states;
if ( detected.size() == count )
{
controls = detected;
if ( controls.size() > 1 )
{
control_qualifier.name = '|';
std::vector<Device::Control*>::const_iterator i = controls.begin(),
e = controls.end();
for ( ; i!=e; ++i )
control_qualifier.name += (*i)->GetName() + '|';
}
else
control_qualifier.FromControl( controls[0] );
return true;
}
}
return false;
}
//
// 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
//
bool ControllerInterface::OutputReference::Detect( const unsigned int ms, const unsigned int ignored )
{
// dont hang if we dont even have any controls mapped
if ( controls.size() )
{
State( 1 );
unsigned int slept = 0;
// this loop is to make stuff like flashing keyboard LEDs work
while ( ms > ( slept += 10 ) )
{
device->UpdateOutput();
wxMilliSleep( 10 );
}
State( 0 );
device->UpdateOutput();
}
return false;
}

View File

@ -0,0 +1,258 @@
#ifndef _DEVICEINTERFACE_H_
#define _DEVICEINTERFACE_H_
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <algorithm>
//#define CIFACE_USE_REGEX
// enable disable sources
#ifdef _WIN32
#define CIFACE_USE_XINPUT
#define CIFACE_USE_DIRECTINPUT_JOYSTICK
#define CIFACE_USE_DIRECTINPUT_KBM
#define CIFACE_USE_DIRECTINPUT
#endif
#if defined(HAVE_X11) && HAVE_X11
// Xlib is not tested at all currently, it is like 80% complete at least though
#define CIFACE_USE_XLIB
#endif
#ifndef CIFACE_USE_DIRECTINPUT_JOYSTICK
#define CIFACE_USE_SDL
#endif
#ifdef CIFACE_USE_REGEX
#include <wx/regex.h>
#endif
#include <wx/stopwatch.h>
#include <wx/utils.h>
// idk in case i wanted to change it to double or somethin, idk what's best
typedef float ControlState;
//
// ControllerInterface
//
// some crazy shit i made to control different device inputs and outputs
// from lots of different sources, hopefully more easily
//
class ControllerInterface
{
public:
//
// Device
//
// Pretty obviously, a device class
//
class Device
{
public:
//
// Control
//
// control includes inputs and outputs
//
class Control // input or output
{
public:
virtual std::string GetName() const = 0;
};
//
// Input
//
// an input on a device
//
class Input : public Control
{
public:
virtual ~Input() {}
};
//
// Output
//
// guess wut it is, yup and output
//
class Output : public Control
{
public:
virtual ~Output() {}
};
virtual ~Device();
virtual std::string GetName() const = 0;
virtual int GetId() const = 0;
virtual std::string GetSource() const = 0;
virtual ControlState GetInputState( const Input* const input ) = 0;
virtual void SetOutputState( const Output* const output, const ControlState state ) = 0;
virtual bool UpdateInput() = 0;
virtual bool UpdateOutput() = 0;
virtual void ClearInputState();
const std::vector< Input* >& Inputs();
const std::vector< Output* >& Outputs();
protected:
std::vector<Input*> inputs;
std::vector<Output*> outputs;
};
//
// DeviceQualifier
//
// device qualifier used to match devices
// currently has ( source, id, name ) properties which match a device
//
class DeviceQualifier
{
public:
DeviceQualifier() : id(-1){}
DeviceQualifier( const std::string& _source, const int _id, const std::string& _name )
: source(_source), id(_id), name(_name) {}
bool operator==(const Device* const dev) const;
void FromDevice(const Device* const dev);
void FromString(const std::string& str);
std::string ToString() const;
std::string source;
int id;
std::string name;
};
//
// ControlQualifier
//
// control qualifier includes input and output qualifiers
// used to match controls on devices, only has name property
// if name is blank it matches nothing, if name is /../ form, it matches controls using a regex
// |input1|input2| form as well, || matches anything, might change this to * or something
//
class ControlQualifier
{
public:
ControlQualifier() {};
ControlQualifier( const std::string& _name ) : name(_name) {}
virtual bool operator==(const Device::Control* const in) const;
void FromControl(const Device::Control* const in);
std::string name;
};
//
// InputQualifier
//
// ControlQualifier for inputs
//
class InputQualifier : public ControlQualifier
{
public:
InputQualifier() {};
InputQualifier( const std::string& _name ) : ControlQualifier(_name) {}
};
//
// OutputQualifier
//
// ControlQualifier for outputs
//
class OutputQualifier : public ControlQualifier
{
public:
OutputQualifier() {};
OutputQualifier( const std::string& _name ) : ControlQualifier(_name) {}
};
//
// ControlReference
//
// these are what you create to actually use the inputs, InputReference or OutputReference
// they have a vector < struct { device , vector < controls > } >
//
// after being binded to devices and controls with ControllerInterface::UpdateReference,
// each one can binded to 0+ devices, and 0+ controls on each device
// ControlReference can update its own controls when you change its control qualifier
// using ControlReference::UpdateControls but when you change its device qualifer
// you must use ControllerInterface::UpdateReference
//
class ControlReference
{
public:
ControlReference( const bool _is_input ) : range(1), is_input(_is_input), device(NULL) {}
virtual ControlState State( const ControlState state = 0 ) = 0;
virtual bool Detect( const unsigned int ms, const unsigned int count = 1 ) = 0;
virtual void UpdateControls() = 0;
ControlState range;
DeviceQualifier device_qualifier;
ControlQualifier control_qualifier;
const bool is_input;
Device* device;
std::vector<Device::Control*> controls;
};
//
// InputReference
//
// control reference for inputs
//
class InputReference : public ControlReference
{
public:
InputReference() : ControlReference( true ) {}
ControlState State( const ControlState state );
bool Detect( const unsigned int ms, const unsigned int count );
void UpdateControls();
unsigned int mode;
};
//
// OutputReference
//
// control reference for outputs
//
class OutputReference : public ControlReference
{
public:
OutputReference() : ControlReference( false ) {}
ControlState State( const ControlState state );
bool Detect( const unsigned int ms, const unsigned int count );
void UpdateControls();
};
ControllerInterface() : m_is_init(false) {}
void SetHwnd( void* const hwnd );
void Init();
void DeInit();
bool IsInit();
void UpdateReference( ControlReference* control );
bool UpdateInput();
bool UpdateOutput();
const std::vector<Device*>& Devices();
private:
bool m_is_init;
std::vector<Device*> m_devices;
void* m_hwnd;
};
#endif

View File

@ -0,0 +1,35 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DIRECTINPUT
#include "DirectInput.h"
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "dxguid.lib")
namespace ciface
{
namespace DirectInput
{
void Init( std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ )
{
IDirectInput8* idi8;
if ( DI_OK != DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&idi8, NULL ) )
return;
#ifdef CIFACE_USE_DIRECTINPUT_KBM
InitKeyboardMouse( idi8, devices );
#endif
#ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK
InitJoystick( idi8, devices/*, hwnd*/ );
#endif
idi8->Release();
}
}
}
#endif

View File

@ -0,0 +1,29 @@
#ifndef _CIFACE_DIRECTINPUT_H_
#define _CIFACE_DIRECTINPUT_H_
#include "../ControllerInterface.h"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
#ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK
#include "DirectInputJoystick.h"
#endif
#ifdef CIFACE_USE_DIRECTINPUT_KBM
#include "DirectInputKeyboardMouse.h"
#endif
namespace ciface
{
namespace DirectInput
{
void Init( std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ );
}
}
#endif

View File

@ -0,0 +1,353 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DIRECTINPUT_JOYSTICK
#include "DirectInputJoystick.h"
namespace ciface
{
namespace DirectInput
{
std::string TStringToString( const std::basic_string<TCHAR>& in )
{
const int size = WideCharToMultiByte( CP_UTF8, 0, in.data(), int(in.length()), NULL, 0, NULL, NULL );
if ( 0 == size )
return "";
char* const data = new char[size];
WideCharToMultiByte( CP_UTF8, 0, in.data(), int(in.length()), data, size, NULL, NULL );
const std::string out( data, size );
delete[] data;
return out;
}
//BOOL CALLBACK DIEnumEffectsCallback( LPCDIEFFECTINFO pdei, LPVOID pvRef )
//{
// ((std::vector<DIEFFECTINFO>*)pvRef)->push_back( *pdei );
// return DIENUM_CONTINUE;
//}
BOOL CALLBACK DIEnumDeviceObjectsCallback( LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef )
{
((std::vector<DIDEVICEOBJECTINSTANCE>*)pvRef)->push_back( *lpddoi );
return DIENUM_CONTINUE;
}
BOOL CALLBACK DIEnumDevicesCallback( LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef )
{
((std::vector<DIDEVICEINSTANCE>*)pvRef)->push_back( *lpddi );
return DIENUM_CONTINUE;
}
void InitJoystick( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ )
{
std::vector<DIDEVICEINSTANCE> joysticks;
idi8->EnumDevices( DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, DIEDFL_ATTACHEDONLY );
// just a struct with an int that is set to ZERO by default
struct ZeroedInt{ZeroedInt():value(0){}unsigned int value;};
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map< std::basic_string<TCHAR>, ZeroedInt > name_counts;
std::vector<DIDEVICEINSTANCE>::iterator i = joysticks.begin(),
e = joysticks.end();
for ( ; i!=e; ++i )
{
// TODO: this has potential to mess up on createdev or setdatafmt failure
LPDIRECTINPUTDEVICE8 js_device;
if ( DI_OK == idi8->CreateDevice( i->guidInstance, &js_device, NULL ) )
if ( DI_OK == js_device->SetDataFormat( &c_dfDIJoystick ) )
// using foregroundwindow seems like a hack
if ( DI_OK != js_device->SetCooperativeLevel( GetForegroundWindow(), DISCL_BACKGROUND | DISCL_EXCLUSIVE ) )
{
// fall back to non-exclusive mode, with no rumble
if ( DI_OK != js_device->SetCooperativeLevel( NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ) )
{
js_device->Release();
continue;
}
}
if ( DI_OK == js_device->Acquire() )
{
Joystick* js = new Joystick( /*&*i, */js_device, name_counts[i->tszInstanceName].value++ );
// only add if it has some inputs/outpus
if ( js->Inputs().size() || js->Outputs().size() )
devices.push_back( js );
else
delete js;
}
else
js_device->Release();
}
}
Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index )
: m_device(device)
, m_index(index)
//, m_name(TStringToString(lpddi->tszInstanceName))
{
// get joystick caps
DIDEVCAPS js_caps;
ZeroMemory( &js_caps, sizeof(js_caps) );
js_caps.dwSize = sizeof(js_caps);
m_device->GetCapabilities(&js_caps);
// max of 32 buttons and 4 hats / the limit of the data format i am using
js_caps.dwButtons = std::min((DWORD)32, js_caps.dwButtons);
js_caps.dwPOVs = std::min((DWORD)4, js_caps.dwPOVs);
m_must_poll = ( ( js_caps.dwFlags & DIDC_POLLEDDATAFORMAT ) > 0 );
// buttons
for ( unsigned int i = 0; i < js_caps.dwButtons; ++i )
inputs.push_back( new Button( i ) );
// hats
for ( unsigned int i = 0; i < js_caps.dwPOVs; ++i )
{
// each hat gets 4 input instances associated with it, (up down left right)
for ( unsigned int d = 0; d<4; ++d )
inputs.push_back( new Hat( i, d ) );
}
// axes / only the first 6 currently
std::vector<DIDEVICEOBJECTINSTANCE> axes;
m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_AXIS );
for( unsigned int i = 0; i<axes.size() && i<6; ++i )
{
DIPROPRANGE range;
ZeroMemory( &range, sizeof(range ) );
range.diph.dwSize = sizeof(range);
range.diph.dwHeaderSize = sizeof(range.diph);
range.diph.dwHow = DIPH_BYID;
range.diph.dwObj = axes[i].dwType;
// try to set some nice power of 2 values (8192)
range.lMin = -(1<<13);
range.lMax = (1<<13);
// but i guess not all devices support setting range
m_device->SetProperty( DIPROP_RANGE, &range.diph );
// so i getproperty right afterward incase it didn't set :P
if ( DI_OK == m_device->GetProperty( DIPROP_RANGE, &range.diph ) )
{
const LONG base = (range.lMin + range.lMax) / 2;
// each axis gets a negative and a positive input instance associated with it
inputs.push_back( new Axis( i, base, range.lMin-base ) );
inputs.push_back( new Axis( i, base, range.lMax-base ) );
}
}
// do people need sliders? maybe later
// get supported ff effects
std::vector<DIDEVICEOBJECTINSTANCE> objects;
m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&objects, DIDFT_AXIS );
// got some ff axes or something
if ( objects.size() )
{
// temporary
DWORD rgdwAxes[] = { DIJOFS_X, DIJOFS_Y };
LONG rglDirection[] = { 0, 0 };
DICONSTANTFORCE cf = { 0 };
DIEFFECT eff;
ZeroMemory( &eff, sizeof( DIEFFECT ) );
eff.dwSize = sizeof( DIEFFECT );
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.dwDuration = INFINITE;
eff.dwGain = DI_FFNOMINALMAX;
eff.dwTriggerButton = DIEB_NOTRIGGER;
eff.cAxes = std::min( (DWORD)2, (DWORD)objects.size() );
eff.rgdwAxes = rgdwAxes;
eff.rglDirection = rglDirection;
eff.cbTypeSpecificParams = sizeof( DICONSTANTFORCE );
eff.lpvTypeSpecificParams = &cf;
LPDIRECTINPUTEFFECT pEffect;
if ( DI_OK == m_device->CreateEffect( GUID_ConstantForce, &eff, &pEffect, NULL ) )
{
// temp
outputs.push_back( new Force( 0 ) );
m_state_out.push_back( EffectState( pEffect ) );
}
}
// disable autocentering
if ( outputs.size() )
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof( DIPROPDWORD );
dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER );
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = FALSE;
m_device->SetProperty( DIPROP_AUTOCENTER, &dipdw.diph );
}
ClearInputState();
}
Joystick::~Joystick()
{
// release the ff effect iface's
std::vector<EffectState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
{
i->iface->Stop();
i->iface->Unload();
i->iface->Release();
}
m_device->Unacquire();
m_device->Release();
}
void Joystick::ClearInputState()
{
ZeroMemory(&m_state_in, sizeof(m_state_in));
// set hats to center
memset( m_state_in.rgdwPOV, 0xFF, sizeof(m_state_in.rgdwPOV) );
}
std::string Joystick::GetName() const
{
DIPROPSTRING str;
ZeroMemory( &str, sizeof(str) );
str.diph.dwSize = sizeof(str);
str.diph.dwHeaderSize = sizeof(str.diph);
str.diph.dwHow = DIPH_DEVICE;
m_device->GetProperty( DIPROP_PRODUCTNAME, &str.diph );
return TStringToString( str.wsz );
//return m_name;
}
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::GetSource() const
{
return "DirectInput";
}
// update IO
bool Joystick::UpdateInput()
{
if ( m_must_poll )
if ( DI_OK != m_device->Poll() )
return false;
return ( DI_OK == m_device->GetDeviceState( sizeof(m_state_in), &m_state_in ) );
}
bool Joystick::UpdateOutput()
{
// temporary
size_t ok_count = 0;
std::vector<EffectState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
{
if ( i->changed )
{
i->changed = false;
DICONSTANTFORCE cf;
cf.lMagnitude = 10000 * i->magnitude;
if ( cf.lMagnitude )
{
DIEFFECT eff;
ZeroMemory( &eff, sizeof( eff ) );
eff.dwSize = sizeof( DIEFFECT );
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.cbTypeSpecificParams = sizeof( cf );
eff.lpvTypeSpecificParams = &cf;
// set params and start effect
ok_count += ( DI_OK == i->iface->SetParameters( &eff, DIEP_TYPESPECIFICPARAMS | DIEP_START ) );
}
else
ok_count += ( DI_OK == i->iface->Stop() );
}
else
++ok_count;
}
return ( m_state_out.size() == ok_count );
}
// get name
std::string Joystick::Button::GetName() const
{
std::ostringstream ss;
ss << "Button " << m_index;
return ss.str();
}
std::string Joystick::Axis::GetName() const
{
std::ostringstream ss;
ss << "Axis " << m_index << ( m_range>0 ? '+' : '-' );
return ss.str();
}
std::string Joystick::Hat::GetName() const
{
std::ostringstream ss;
ss << "Hat " << m_index << ' ' << "NESW"[m_direction];
return ss.str();
}
std::string Joystick::Force::GetName() const
{
// temporary
return "Constant";
}
// get / set state
ControlState Joystick::GetInputState( const ControllerInterface::Device::Input* const input )
{
return ((Input*)input)->GetState( &m_state_in );
}
void Joystick::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
((Output*)output)->SetState( state, &m_state_out[0] );
}
// get / set state
ControlState Joystick::Axis::GetState( const DIJOYSTATE* const joystate )
{
return std::max( 0.0f, ControlState((&joystate->lX)[m_index]-m_base) / m_range );
}
ControlState Joystick::Button::GetState( const DIJOYSTATE* const joystate )
{
return ControlState( joystate->rgbButtons[m_index] > 0 );
}
ControlState Joystick::Hat::GetState( const DIJOYSTATE* const joystate )
{
// can this func be simplified ?
const DWORD val = joystate->rgdwPOV[m_index];
// hat centered code from msdn
if ( 0xFFFF == LOWORD(val) )
return 0;
return ( abs( (int)(val/4500-m_direction*2+8)%8 - 4) > 2 );
}
void Joystick::Force::SetState( const ControlState state, Joystick::EffectState* const joystate )
{
joystate[m_index].magnitude = state;
joystate[m_index].changed = true;
}
}
}
#endif

View File

@ -0,0 +1,130 @@
#ifndef _CIFACE_DIRECTINPUT_JOYSTICK_H_
#define _CIFACE_DIRECTINPUT_JOYSTICK_H_
#include "../ControllerInterface.h"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
namespace ciface
{
namespace DirectInput
{
void InitJoystick( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ );
class Joystick : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
struct EffectState
{
EffectState( LPDIRECTINPUTEFFECT eff ) : changed(0), iface(eff) {}
LPDIRECTINPUTEFFECT iface;
ControlState magnitude;
bool changed;
};
class Input : public ControllerInterface::Device::Input
{
friend class Joystick;
protected:
virtual ControlState GetState( const DIJOYSTATE* const joystate ) = 0;
};
// can probably eliminate this base class
class Output : public ControllerInterface::Device::Output
{
friend class Joystick;
protected:
virtual void SetState( const ControlState state, EffectState* const joystate ) = 0;
};
class Button : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : m_index(index) {}
ControlState GetState( const DIJOYSTATE* const joystate );
private:
const unsigned int m_index;
};
class Axis : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const LONG base, const LONG range ) : m_index(index), m_base(base), m_range(range) {}
ControlState GetState( const DIJOYSTATE* const joystate );
private:
const unsigned int m_index;
const LONG m_base;
const LONG m_range;
};
class Hat : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Hat( const unsigned int index, const unsigned int direction ) : m_index(index), m_direction(direction) {}
ControlState GetState( const DIJOYSTATE* const joystate );
private:
const unsigned int m_index;
const unsigned int m_direction;
};
class Force : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Force( const unsigned int index ) : m_index(index) {}
void SetState( const ControlState state, EffectState* const joystate );
private:
const unsigned int m_index;
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input );
void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );
void ClearInputState();
public:
Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index );
~Joystick();
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
const LPDIRECTINPUTDEVICE8 m_device;
const unsigned int m_index;
//const std::string m_name;
DIJOYSTATE m_state_in;
std::vector<EffectState> m_state_out;
bool m_must_poll;
};
}
}
#endif

View File

@ -0,0 +1,269 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_DIRECTINPUT_KBM
#include "DirectInputKeyboardMouse.h"
// TODO: maybe add a ClearInputState function to this device
// (lower would be more sensitive) user can lower sensitivity by setting range
// seems decent here ( at 8 ), I dont think anyone would need more sensitive than this
// and user can lower it much farther than they would want to with the range
#define MOUSE_AXIS_SENSITIVITY 8
// if input hasn't been received for this many ms, mouse input will be skipped
// otherwise it is just some crazy value
#define DROP_INPUT_TIME 250
namespace ciface
{
namespace DirectInput
{
struct
{
const BYTE code;
const char* const name;
} named_keys[] =
{
#include "NamedKeys.h"
};
struct
{
const BYTE code;
const char* const name;
} named_lights[] =
{
{ VK_NUMLOCK, "NUM LOCK" },
{ VK_CAPITAL, "CAPS LOCK" },
{ VK_SCROLL, "SCROLL LOCK" }
};
void InitKeyboardMouse( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices )
{
// mouse and keyboard are a combined device, to allow shift+click and stuff
// if thats dumb, i will make a VirtualDevice class that just uses ranges of inputs/outputs from other devices
// so there can be a separated Keyboard and mouse, as well as combined KeyboardMouse
// TODO: this has potential to not release devices if set datafmt or cooplevel fails
LPDIRECTINPUTDEVICE8 kb_device;
LPDIRECTINPUTDEVICE8 mo_device;
if ( DI_OK == idi8->CreateDevice( GUID_SysKeyboard, &kb_device, NULL ) )
if ( DI_OK == kb_device->SetDataFormat( &c_dfDIKeyboard ) )
if ( DI_OK == kb_device->SetCooperativeLevel( NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) )
if ( DI_OK == kb_device->Acquire() )
{
if ( DI_OK == idi8->CreateDevice( GUID_SysMouse, &mo_device, NULL ) )
if ( DI_OK == mo_device->SetDataFormat( &c_dfDIMouse2 ) )
if ( DI_OK == mo_device->SetCooperativeLevel( NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) )
if ( DI_OK == mo_device->Acquire() )
{
devices.push_back( new KeyboardMouse( kb_device, mo_device ) );
return;
}
else
goto release_mouse;
goto unacquire_kb;
}
else
goto release_kb;
release_mouse:
mo_device->Release();
unacquire_kb:
kb_device->Unacquire();
release_kb:
kb_device->Release();
}
KeyboardMouse::~KeyboardMouse()
{
// kb
m_kb_device->Unacquire();
m_kb_device->Release();
// mouse
m_mo_device->Unacquire();
m_mo_device->Release();
}
KeyboardMouse::KeyboardMouse( const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device )
: m_kb_device(kb_device)
, m_mo_device(mo_device)
{
m_last_update = wxGetLocalTimeMillis();
ZeroMemory( &m_state_in, sizeof(m_state_in) );
ZeroMemory( m_state_out, sizeof(m_state_out) );
ZeroMemory( &m_current_state_out, sizeof(m_current_state_out) );
// KEYBOARD
// add keys
for ( unsigned int i = 0; i < sizeof(named_keys)/sizeof(*named_keys); ++i )
inputs.push_back( new Key( i ) );
// add lights
for ( unsigned int i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i )
outputs.push_back( new Light( i ) );
// MOUSE
// get caps
DIDEVCAPS mouse_caps;
ZeroMemory( &mouse_caps, sizeof(mouse_caps) );
mouse_caps.dwSize = sizeof(mouse_caps);
m_mo_device->GetCapabilities(&mouse_caps);
// mouse buttons
for ( unsigned int i = 0; i < mouse_caps.dwButtons; ++i )
inputs.push_back( new Button( i ) );
// mouse axes
for ( unsigned int i = 0; i < mouse_caps.dwAxes; ++i )
{
// each axis gets a negative and a positive input instance associated with it
inputs.push_back( new Axis( i, (2==i) ? -1 : -MOUSE_AXIS_SENSITIVITY ) );
inputs.push_back( new Axis( i, -(2==i) ? 1 : MOUSE_AXIS_SENSITIVITY ) );
}
}
bool KeyboardMouse::UpdateInput()
{
DIMOUSESTATE2 tmp_mouse;
// if mouse position hasn't been updated in a short while, skip a dev state
wxLongLong cur_time = wxGetLocalTimeMillis();
if ( cur_time - m_last_update > DROP_INPUT_TIME )
{
// set axes to zero
ZeroMemory( &m_state_in.mouse, sizeof(m_state_in.mouse) );
// skip this input state
m_mo_device->GetDeviceState( sizeof(tmp_mouse), &tmp_mouse );
}
m_last_update = cur_time;
if ( DI_OK == m_kb_device->GetDeviceState( sizeof(m_state_in.keyboard), &m_state_in.keyboard )
&& DI_OK == m_mo_device->GetDeviceState( sizeof(tmp_mouse), &tmp_mouse ) )
{
// need to smooth out the axes, otherwise it doesnt work for shit
for ( unsigned int i = 0; i < 3; ++i )
((&m_state_in.mouse.lX)[i] += (&tmp_mouse.lX)[i]) /= 2;
// copy over the buttons
memcpy( m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons) );
return true;
}
else
return false;
}
bool KeyboardMouse::UpdateOutput()
{
class KInput : public INPUT
{
public:
KInput( const unsigned char key, const bool up = false )
{
memset( this, 0, sizeof(*this) );
type = INPUT_KEYBOARD;
ki.wVk = key;
if (up) ki.dwFlags = KEYEVENTF_KEYUP;
}
};
std::vector< KInput > kbinputs;
for ( unsigned int i = 0; i < sizeof(m_state_out)/sizeof(*m_state_out); ++i )
{
bool want_on = false;
if ( m_state_out[i] )
want_on = m_state_out[i] > wxGetLocalTimeMillis() % 255 ; // light should flash when output is 0.5
// lights are set to their original state when output is zero
if ( want_on ^ m_current_state_out[i] )
{
kbinputs.push_back( KInput( named_lights[i].code ) ); // press
kbinputs.push_back( KInput( named_lights[i].code, true ) ); // release
m_current_state_out[i] ^= 1;
}
}
return ( kbinputs.size() == SendInput( (UINT)kbinputs.size(), &kbinputs[0], sizeof( kbinputs[0] ) ) );
}
std::string KeyboardMouse::GetName() const
{
return "Keyboard Mouse";
}
int KeyboardMouse::GetId() const
{
// should this be -1, idk
return 0;
}
std::string KeyboardMouse::GetSource() const
{
return "DirectInput";
}
ControlState KeyboardMouse::GetInputState( const ControllerInterface::Device::Input* const input )
{
return ( ((Input*)input)->GetState( &m_state_in ) );
}
void KeyboardMouse::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
((Output*)output)->SetState( state, m_state_out );
}
// names
std::string KeyboardMouse::Key::GetName() const
{
return named_keys[m_index].name;
}
std::string KeyboardMouse::Button::GetName() const
{
return std::string("Button ") + char('0'+m_index);
}
std::string KeyboardMouse::Axis::GetName() const
{
std::string tmpstr("Mouse ");
tmpstr += "XYZ"[m_index]; tmpstr += ( m_range>0 ? '+' : '-' );
return tmpstr;
}
std::string KeyboardMouse::Light::GetName() const
{
return named_lights[ m_index ].name;
}
// get/set state
ControlState KeyboardMouse::Key::GetState( const State* const state )
{
return ( state->keyboard[named_keys[m_index].code] > 0 );
}
ControlState KeyboardMouse::Button::GetState( const State* const state )
{
return ( state->mouse.rgbButtons[m_index] > 0 );
}
ControlState KeyboardMouse::Axis::GetState( const State* const state )
{
return std::max( 0.0f, ControlState((&state->mouse.lX)[m_index]) / m_range );
}
void KeyboardMouse::Light::SetState( const ControlState state, unsigned char* const state_out )
{
state_out[ m_index ] = state * 255;
}
}
}
#endif

View File

@ -0,0 +1,122 @@
#ifndef _CIFACE_DIRECTINPUT_KBM_H_
#define _CIFACE_DIRECTINPUT_KBM_H_
#include "../ControllerInterface.h"
#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>
namespace ciface
{
namespace DirectInput
{
void InitKeyboardMouse( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices );
class KeyboardMouse : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
struct State
{
BYTE keyboard[256];
DIMOUSESTATE2 mouse;
};
class Input : public ControllerInterface::Device::Input
{
friend class KeyboardMouse;
protected:
virtual ControlState GetState( const State* const boardstate ) = 0;
};
class Output : public ControllerInterface::Device::Output
{
friend class KeyboardMouse;
protected:
virtual void SetState( const ControlState state, unsigned char* const state_out ) = 0;
};
class Key : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Key( const unsigned int index ) : m_index(index) {}
ControlState GetState( const State* const state );
private:
const unsigned int m_index;
};
class Button : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : m_index(index) {}
ControlState GetState( const State* const state );
private:
const unsigned int m_index;
};
class Axis : public Input
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const LONG range ) : m_index(index), m_range(range) {}
ControlState GetState( const State* const state );
private:
const unsigned int m_index;
const LONG m_range;
};
class Light : public Output
{
friend class KeyboardMouse;
public:
std::string GetName() const;
protected:
Light( const unsigned int index ) : m_index(index) {}
void SetState( const ControlState state, unsigned char* const state_out );
private:
const unsigned int m_index;
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input );
void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );
public:
KeyboardMouse( const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device );
~KeyboardMouse();
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
const LPDIRECTINPUTDEVICE8 m_kb_device;
const LPDIRECTINPUTDEVICE8 m_mo_device;
wxLongLong m_last_update;
State m_state_in;
unsigned char m_state_out[3]; // NUM CAPS SCROLL
bool m_current_state_out[3]; // NUM CAPS SCROLL
};
}
}
#endif

View File

@ -0,0 +1,144 @@
{ DIK_A, "A" },
{ DIK_B, "B" },
{ DIK_C, "C" },
{ DIK_D, "D" },
{ DIK_E, "E" },
{ DIK_F, "F" },
{ DIK_G, "G" },
{ DIK_H, "H" },
{ DIK_I, "I" },
{ DIK_J, "J" },
{ DIK_K, "K" },
{ DIK_L, "L" },
{ DIK_M, "M" },
{ DIK_N, "N" },
{ DIK_O, "O" },
{ DIK_P, "P" },
{ DIK_Q, "Q" },
{ DIK_R, "R" },
{ DIK_S, "S" },
{ DIK_T, "T" },
{ DIK_U, "U" },
{ DIK_V, "V" },
{ DIK_W, "W" },
{ DIK_X, "X" },
{ DIK_Y, "Y" },
{ DIK_Z, "Z" },
{ DIK_0, "0" },
{ DIK_1, "1" },
{ DIK_2, "2" },
{ DIK_3, "3" },
{ DIK_4, "4" },
{ DIK_5, "5" },
{ DIK_6, "6" },
{ DIK_7, "7" },
{ DIK_8, "8" },
{ DIK_9, "9" },
{ DIK_UP, "UP" },
{ DIK_DOWN, "DOWN" },
{ DIK_LEFT, "LEFT" },
{ DIK_RIGHT, "RIGHT" },
{ DIK_ABNT_C1, "ABNT_C1" },
{ DIK_ABNT_C2, "ABNT_C2" },
{ DIK_ADD, "ADD" },
{ DIK_APOSTROPHE, "APOSTROPHE" },
{ DIK_APPS, "APPS" },
{ DIK_AT, "AT" },
{ DIK_AX, "AX" },
{ DIK_BACK, "BACK" },
{ DIK_BACKSLASH, "BACKSLASH" },
{ DIK_CALCULATOR, "CALCULATOR" },
{ DIK_CAPITAL, "CAPITAL" },
{ DIK_COLON, "COLON" },
{ DIK_COMMA, "COMMA" },
{ DIK_CONVERT, "CONVERT" },
{ DIK_DECIMAL, "DECIMAL" },
{ DIK_DELETE, "DELETE" },
{ DIK_DIVIDE, "DIVIDE" },
{ DIK_EQUALS, "EQUALS" },
{ DIK_ESCAPE, "ESCAPE" },
{ DIK_F1, "F1" },
{ DIK_F2, "F2" },
{ DIK_F3, "F3" },
{ DIK_F4, "F4" },
{ DIK_F5, "F5" },
{ DIK_F6, "F6" },
{ DIK_F7, "F7" },
{ DIK_F8, "F8" },
{ DIK_F9, "F9" },
{ DIK_F10, "F10" },
{ DIK_F11, "F11" },
{ DIK_F12, "F12" },
{ DIK_F13, "F13" },
{ DIK_F14, "F14" },
{ DIK_F15, "F15" },
{ DIK_GRAVE, "GRAVE" },
{ DIK_HOME, "HOME" },
{ DIK_END, "END" },
{ DIK_INSERT, "INSERT" },
{ DIK_KANA, "KANA" },
{ DIK_KANJI, "KANJI" },
{ DIK_MAIL, "MAIL" },
{ DIK_MEDIASELECT, "MEDIASELECT" },
{ DIK_MEDIASTOP, "MEDIASTOP" },
{ DIK_MINUS, "MINUS" },
{ DIK_MULTIPLY, "MULTIPLY" },
{ DIK_MUTE, "MUTE" },
{ DIK_MYCOMPUTER, "MYCOMPUTER" },
{ DIK_NEXTTRACK, "NEXTTRACK" },
{ DIK_NOCONVERT, "NOCONVERT" },
{ DIK_NUMLOCK, "NUMLOCK" },
{ DIK_NUMPAD0, "NUMPAD0" },
{ DIK_NUMPAD1, "NUMPAD1" },
{ DIK_NUMPAD2, "NUMPAD2" },
{ DIK_NUMPAD3, "NUMPAD3" },
{ DIK_NUMPAD4, "NUMPAD4" },
{ DIK_NUMPAD5, "NUMPAD5" },
{ DIK_NUMPAD6, "NUMPAD6" },
{ DIK_NUMPAD7, "NUMPAD7" },
{ DIK_NUMPAD8, "NUMPAD8" },
{ DIK_NUMPAD9, "NUMPAD9" },
{ DIK_NUMPADCOMMA, "NUMPADCOMMA" },
{ DIK_NUMPADENTER, "NUMPADENTER" },
{ DIK_NUMPADEQUALS, "NUMPADEQUALS" },
{ DIK_OEM_102, "OEM_102" },
{ DIK_PAUSE, "PAUSE" },
{ DIK_PERIOD, "PERIOD" },
{ DIK_PLAYPAUSE, "PLAYPAUSE" },
{ DIK_POWER, "POWER" },
{ DIK_PREVTRACK, "PREVTRACK" },
{ DIK_PRIOR, "PRIOR" },
{ DIK_NEXT, "NEXT" },
{ DIK_RETURN, "RETURN" },
{ DIK_LBRACKET, "LBRACKET" },
{ DIK_RBRACKET, "RBRACKET" },
{ DIK_LCONTROL, "LCONTROL" },
{ DIK_RCONTROL, "RCONTROL" },
{ DIK_LMENU, "LMENU" },
{ DIK_RMENU, "RMENU" },
{ DIK_LSHIFT, "LSHIFT" },
{ DIK_RSHIFT, "RSHIFT" },
{ DIK_LWIN, "LWIN" },
{ DIK_RWIN, "RWIN" },
{ DIK_SCROLL, "SCROLL" },
{ DIK_SEMICOLON, "SEMICOLON" },
{ DIK_SLASH, "SLASH" },
{ DIK_SLEEP, "SLEEP" },
{ DIK_SPACE, "SPACE" },
{ DIK_STOP, "STOP" },
{ DIK_SUBTRACT, "SUBTRACT" },
{ DIK_SYSRQ, "SYSRQ" },
{ DIK_TAB, "TAB" },
{ DIK_UNDERLINE, "UNDERLINE" },
{ DIK_UNLABELED, "UNLABELED" },
{ DIK_VOLUMEDOWN, "VOLUMEDOWN" },
{ DIK_VOLUMEUP, "VOLUMEUP" },
{ DIK_WAKE, "WAKE" },
{ DIK_WEBBACK, "WEBBACK" },
{ DIK_WEBFAVORITES, "WEBFAVORITES" },
{ DIK_WEBFORWARD, "WEBFORWARD" },
{ DIK_WEBHOME, "WEBHOME" },
{ DIK_WEBREFRESH, "WEBREFRESH" },
{ DIK_WEBSEARCH, "WEBSEARCH" },
{ DIK_WEBSTOP, "WEBSTOP" },
{ DIK_YEN, "YEN" },

View File

@ -0,0 +1,283 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_SDL
#include "SDL.h"
#ifdef _WIN32
#pragma comment(lib, "SDL.1.3.lib")
#endif
// temp for debuggin
//#include <fstream>
namespace ciface
{
namespace SDL
{
void Init( std::vector<ControllerInterface::Device*>& devices )
{
if ( SDL_Init( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC ) >= 0 )
{
// joysticks
for( int i = 0; i < SDL_NumJoysticks(); ++i )
{
SDL_Joystick* dev;
if ( dev = SDL_JoystickOpen( i ) )
{
Joystick* js = new Joystick( dev, i );
// only add if it has some inputs/outputs
if ( js->Inputs().size() || js->Outputs().size() )
devices.push_back( js );
else
delete js;
}
}
}
}
Joystick::Joystick( SDL_Joystick* const joystick, const unsigned int index ) : m_joystick(joystick), m_index(index)
{
// get buttons
for ( int i = 0; i < SDL_JoystickNumButtons( m_joystick ); ++i )
{
inputs.push_back( new Button( i ) );
}
// get hats
for ( int i = 0; i < SDL_JoystickNumHats( m_joystick ); ++i )
{
// each hat gets 4 input instances associated with it, (up down left right)
for ( unsigned int d = 0; d < 4; ++d )
inputs.push_back( new Hat( i, d ) );
}
// get axes
for ( int i = 0; i < SDL_JoystickNumAxes( m_joystick ); ++i )
{
// each axis gets a negative and a positive input instance associated with it
inputs.push_back( new Axis( i, -32768 ) );
inputs.push_back( new Axis( i, 32767 ) );
}
// try to get supported ff effects
if ( m_haptic = SDL_HapticOpenFromJoystick( m_joystick ) )
{
//SDL_HapticSetGain( m_haptic, 1000 );
//SDL_HapticSetAutocenter( m_haptic, 0 );
const unsigned int supported_effects = SDL_HapticQuery( m_haptic ); // use this later
// constant effect
if ( supported_effects & SDL_HAPTIC_CONSTANT )
{
outputs.push_back( new ConstantEffect( m_state_out.size() ) );
m_state_out.push_back( EffectIDState() );
}
// ramp effect
if ( supported_effects & SDL_HAPTIC_RAMP )
{
outputs.push_back( new RampEffect( m_state_out.size() ) );
m_state_out.push_back( EffectIDState() );
}
}
}
Joystick::~Joystick()
{
if ( m_haptic )
{
// stop/destroy all effects
//SDL_HapticStopAll( m_haptic ); // ControllerInterface handles this
std::vector<EffectIDState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
if ( i->id != -1 )
SDL_HapticDestroyEffect( m_haptic, i->id );
// close haptic first
SDL_HapticClose( m_haptic );
}
// close joystick
SDL_JoystickClose( m_joystick );
}
//std::string Joystick::Effect::GetName() const
//{
// return haptic_named_effects[m_index].name;
//}
std::string Joystick::ConstantEffect::GetName() const
{
return "Constant";
}
std::string Joystick::RampEffect::GetName() const
{
return "Ramp";
}
void Joystick::ConstantEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
{
if ( state )
{
//debuggin here ...
//memset( &effect->effect, 0, sizeof(effect->effect) );
effect->effect.type = SDL_HAPTIC_CONSTANT;
effect->effect.constant.length = SDL_HAPTIC_INFINITY;
//effect->effect.constant.attack_length = 3000;
//effect->effect.constant.direction.type = SDL_HAPTIC_CARTESIAN;
//effect->effect
//effect->effect.constant.button = 0xFFFFFFFF;
}
else
effect->effect.type = 0;
Sint16 old = effect->effect.constant.level;
effect->effect.constant.level = state * 0x7FFF;
if ( old != effect->effect.constant.level )
effect->changed = true;
}
void Joystick::RampEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
{
if ( state )
{
effect->effect.type = SDL_HAPTIC_RAMP;
effect->effect.ramp.length = SDL_HAPTIC_INFINITY;
}
else
effect->effect.type = 0;
Sint16 old = effect->effect.ramp.start;
effect->effect.ramp.start = state * 0x7FFF;
if ( old != effect->effect.ramp.start )
effect->changed = true;
}
ControlState Joystick::GetInputState(const ControllerInterface::Device::Input* input)
{
return ((Input*)input)->GetState( m_joystick );
}
void Joystick::SetOutputState(const ControllerInterface::Device::Output* output, const ControlState state)
{
((Output*)output)->SetState( state, &m_state_out[ ((Output*)output)->m_index ] );
}
bool Joystick::UpdateInput()
{
SDL_JoystickUpdate(); // each joystick is doin this, o well
return true;
}
bool Joystick::UpdateOutput()
{
std::vector<EffectIDState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
if ( i->changed ) // if SetState was called on this output
{
if ( -1 == i->id ) // effect isn't currently uploaded
{
if ( i->effect.type ) // if outputstate is >0 this would be true
if ( (i->id = SDL_HapticNewEffect( m_haptic, &i->effect )) > -1 ) // upload the effect
{
//std::ofstream file( "SDLgood.txt" );
/*if ( 0 == */SDL_HapticRunEffect( m_haptic, i->id, 1 );//) // run the effect
// file << "all good";
//else
// file << "not good";
//file.close();
}
else
{
// DEBUG
//std::ofstream file( "SDLerror.txt" );
//file << SDL_GetError();
//file.close();
}
}
else // effect is already uploaded
{
if ( i->effect.type ) // if ouputstate >0
SDL_HapticUpdateEffect( m_haptic, i->id, &i->effect ); // update the effect
else
{
SDL_HapticStopEffect( m_haptic, i->id ); // else, stop and remove the effect
SDL_HapticDestroyEffect( m_haptic, i->id );
i->id = -1; // mark it as not uploaded
}
}
i->changed = false;
}
return true;
}
std::string Joystick::GetName() const
{
return SDL_JoystickName( m_index );
}
std::string Joystick::GetSource() const
{
return "SDL";
}
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::Button::GetName() const
{
std::ostringstream ss;
ss << "Button " << m_index;
return ss.str();
}
std::string Joystick::Axis::GetName() const
{
std::ostringstream ss;
ss << "Axis " << m_index << ( m_range>0 ? '+' : '-' );
return ss.str();
}
std::string Joystick::Hat::GetName() const
{
std::ostringstream ss;
ss << "Hat " << m_index << ' ' << "NESW"[m_direction];
return ss.str();
}
ControlState Joystick::Button::GetState( SDL_Joystick* const js ) const
{
return SDL_JoystickGetButton( js, m_index );
}
ControlState Joystick::Axis::GetState( SDL_Joystick* const js ) const
{
return std::max( 0.0f, ControlState(SDL_JoystickGetAxis( js, m_index )) / m_range );
}
ControlState Joystick::Hat::GetState( SDL_Joystick* const js ) const
{
return (SDL_JoystickGetHat( js, m_index ) & ( 1 << m_direction )) > 0;
}
}
}
#endif

View File

@ -0,0 +1,141 @@
#ifndef _CIFACE_SDL_H_
#define _CIFACE_SDL_H_
#include "../ControllerInterface.h"
// getting rid of warning, sdl/wxwidgets both define it
#undef M_PI
// really dum
#ifdef _WIN32
#include <sdl.h>
#include <sdl_haptic.h>
#else
#include <SDL/sdl.h>
#include <SDL/sdl_haptic.h>
#endif
namespace ciface
{
namespace SDL
{
void Init( std::vector<ControllerInterface::Device*>& devices );
class Joystick : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
class EffectIDState
{
friend class Joystick;
protected:
EffectIDState() : id(-1), changed(false) { memset( &effect, 0, sizeof(effect)); }
protected:
SDL_HapticEffect effect;
int id;
bool changed;
};
class Input : public ControllerInterface::Device::Input
{
friend class Joystick;
protected:
Input( const unsigned int index ) : m_index(index) {}
virtual ControlState GetState( SDL_Joystick* const js ) const = 0;
const unsigned int m_index;
};
class Output : public ControllerInterface::Device::Output
{
friend class Joystick;
protected:
Output( const size_t index ) : m_index(index) {}
virtual void SetState( const ControlState state, EffectIDState* const effect ) = 0;
const size_t m_index;
};
class Button : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : Input(index) {}
ControlState GetState( SDL_Joystick* const js ) const;
};
class Axis : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const Sint16 range ) : Input(index), m_range(range) {}
ControlState GetState( SDL_Joystick* const js ) const;
private:
const Sint16 m_range;
};
class Hat : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Hat( const unsigned int index, const unsigned int direction ) : Input(index), m_direction(direction) {}
ControlState GetState( SDL_Joystick* const js ) const;
private:
const unsigned int m_direction;
};
class ConstantEffect : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
ConstantEffect( const size_t index ) : Output(index) {}
void SetState( const ControlState state, EffectIDState* const effect );
};
class RampEffect : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
RampEffect( const size_t index ) : Output(index) {}
void SetState( const ControlState state, EffectIDState* const effect );
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input );
void SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state );
public:
Joystick( SDL_Joystick* const joystick, const unsigned int index );
~Joystick();
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
std::vector<EffectIDState> m_state_out;
SDL_Haptic* m_haptic;
SDL_Joystick* const m_joystick;
const unsigned int m_index;
};
}
}
#endif

View File

@ -0,0 +1,214 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_XINPUT
#include "XInput.h"
namespace ciface
{
namespace XInput
{
struct
{
const char* const name;
const WORD bitmask;
} named_buttons[] =
{
{ "Button A", XINPUT_GAMEPAD_A },
{ "Button B", XINPUT_GAMEPAD_B },
{ "Button X", XINPUT_GAMEPAD_X },
{ "Button Y", XINPUT_GAMEPAD_Y },
{ "Pad N", XINPUT_GAMEPAD_DPAD_UP },
{ "Pad S", XINPUT_GAMEPAD_DPAD_DOWN },
{ "Pad W", XINPUT_GAMEPAD_DPAD_LEFT },
{ "Pad E", XINPUT_GAMEPAD_DPAD_RIGHT },
{ "Start", XINPUT_GAMEPAD_START },
{ "Back", XINPUT_GAMEPAD_BACK },
{ "Shoulder L", XINPUT_GAMEPAD_LEFT_SHOULDER },
{ "Shoulder R", XINPUT_GAMEPAD_RIGHT_SHOULDER },
{ "Thumb L", XINPUT_GAMEPAD_LEFT_THUMB },
{ "Thumb R", XINPUT_GAMEPAD_RIGHT_THUMB }
};
const char* named_triggers[] =
{
"Trigger L",
"Trigger R"
};
const char* named_axes[] =
{
"Left X",
"Left Y",
"Right X",
"Right Y"
};
const char* named_motors[] =
{
"Motor L",
"Motor R"
};
void Init( std::vector<ControllerInterface::Device*>& devices )
{
XINPUT_CAPABILITIES caps;
for ( int i = 0; i < 4; ++i )
if ( ERROR_SUCCESS == XInputGetCapabilities( i, 0, &caps ) )
devices.push_back( new Device( &caps, i ) );
}
Device::Device( const XINPUT_CAPABILITIES* const caps, const unsigned int index )
: m_index(index), m_subtype(caps->SubType)
{
ZeroMemory( &m_state_out, sizeof(m_state_out) );
// XInputGetCaps seems to always claim all capabilities are supported
// but i will leave all this stuff in, incase m$ fixes xinput up a bit
// get supported buttons
for ( int i = 0; i < sizeof(named_buttons)/sizeof(*named_buttons); ++i )
if ( named_buttons[i].bitmask & caps->Gamepad.wButtons )
inputs.push_back( new Button( /*xinput_named_buttons[i].bitmask, */i ) );
// get supported triggers
for ( int i = 0; i < sizeof(named_triggers)/sizeof(*named_triggers); ++i )
{
//BYTE val = (&caps->Gamepad.bLeftTrigger)[i]; // should be max value / msdn lies
if ( (&caps->Gamepad.bLeftTrigger)[i] )
inputs.push_back( new Trigger( i, 255 ) );
}
// get supported axes
for ( int i = 0; i < sizeof(named_axes)/sizeof(*named_axes); ++i )
{
//SHORT val = (&caps->Gamepad.sThumbLX)[i]; // xinput doesnt give the range / msdn is lier
if ( (&caps->Gamepad.sThumbLX)[i] )
{
// each axis gets a negative and a positive input instance associated with it
inputs.push_back( new Axis( i, -32768 ) );
inputs.push_back( new Axis( i, 32767 ) );
}
}
// get supported motors
for ( int i = 0; i < sizeof(named_motors)/sizeof(*named_motors); ++i )
{
//WORD val = (&caps->Vibration.wLeftMotorSpeed)[i]; // should be max value / nope, more lies
if ( (&caps->Vibration.wLeftMotorSpeed)[i] )
outputs.push_back( new Motor(i, 65535 ) );
}
ClearInputState();
}
void Device::ClearInputState()
{
ZeroMemory( &m_state_in, sizeof(m_state_in) );
}
std::string Device::GetName() const
{
// why aren't these defined
// subtype doesn't seem to work, i tested with 2 diff arcade sticks, both were shown as gamepad
// ill leave it in anyway, maybe m$ will fix it
switch ( m_subtype )
{
case XINPUT_DEVSUBTYPE_GAMEPAD : return "Gamepad"; break;
case 0x02 /*XINPUT_DEVSUBTYPE_WHEEL*/ : return "Wheel"; break;
case 0x03 /*XINPUT_DEVSUBTYPE_ARCADE_STICK*/ : return "Arcade Stick"; break;
case 0x04 /*XINPUT_DEVSUBTYPE_FLIGHT_STICK*/ : return "Flight Stick"; break;
case 0x05 /*XINPUT_DEVSUBTYPE_DANCE_PAD*/ : return "Dance Pad"; break;
case 0x06 /*XINPUT_DEVSUBTYPE_GUITAR*/ : return "Guitar"; break;
case 0x08 /*XINPUT_DEVSUBTYPE_DRUM_KIT*/ : return "Drum Kit"; break;
default : return "Device"; break;
}
}
int Device::GetId() const
{
return m_index;
}
std::string Device::GetSource() const
{
return "XInput";
}
// update i/o
bool Device::UpdateInput()
{
return ( ERROR_SUCCESS == XInputGetState( m_index, &m_state_in ) );
}
bool Device::UpdateOutput()
{
return ( ERROR_SUCCESS == XInputSetState( m_index, &m_state_out ) );
}
// GET name/source/id
std::string Device::Button::GetName() const
{
return named_buttons[m_index].name;
}
std::string Device::Axis::GetName() const
{
return std::string(named_axes[m_index]) + ( m_range>0 ? '+' : '-' );
}
std::string Device::Trigger::GetName() const
{
return named_triggers[m_index];
}
std::string Device::Motor::GetName() const
{
return named_motors[m_index];
}
// get/set control state
ControlState Device::GetInputState( const ControllerInterface::Device::Input* const input )
{
return ((Input*)input)->GetState( &m_state_in.Gamepad );
}
void Device::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
return ((Output*)output)->SetState( state, &m_state_out );
}
// GET / SET STATES
ControlState Device::Button::GetState( const XINPUT_GAMEPAD* const gamepad )
{
return (gamepad->wButtons & named_buttons[m_index].bitmask) > 0;
}
ControlState Device::Trigger::GetState( const XINPUT_GAMEPAD* const gamepad )
{
return ControlState((&gamepad->bLeftTrigger)[m_index]) / m_range;
}
ControlState Device::Axis::GetState( const XINPUT_GAMEPAD* const gamepad )
{
return std::max( 0.0f, ControlState((&gamepad->sThumbLX)[m_index]) / m_range );
}
void Device::Motor::SetState( const ControlState state, XINPUT_VIBRATION* const vibration )
{
(&vibration->wLeftMotorSpeed)[m_index] = (WORD)(state * m_range);
}
}
}
#endif

View File

@ -0,0 +1,116 @@
#ifndef _CIFACE_XINPUT_H_
#define _CIFACE_XINPUT_H_
#include "../ControllerInterface.h"
#define NOMINMAX
#include <Windows.h>
#include <XInput.h>
namespace ciface
{
namespace XInput
{
void Init( std::vector<ControllerInterface::Device*>& devices );
class Device : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
class Input : public ControllerInterface::Device::Input
{
friend class Device;
protected:
virtual ControlState GetState( const XINPUT_GAMEPAD* const gamepad ) = 0;
};
class Output : public ControllerInterface::Device::Output
{
friend class Device;
protected:
virtual void SetState( const ControlState state, XINPUT_VIBRATION* const vibration ) = 0;
};
class Button : public Input
{
friend class Device;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : m_index(index) {}
ControlState GetState( const XINPUT_GAMEPAD* const gamepad );
private:
const unsigned int m_index;
};
class Axis : public Input
{
friend class Device;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const SHORT range ) : m_index(index), m_range(range) {}
ControlState GetState( const XINPUT_GAMEPAD* const gamepad );
private:
const unsigned int m_index;
const SHORT m_range;
};
class Trigger : public Input
{
friend class Device;
public:
std::string GetName() const;
protected:
Trigger( const unsigned int index, const BYTE range ) : m_index(index), m_range(range) {}
ControlState GetState( const XINPUT_GAMEPAD* const gamepad );
private:
const unsigned int m_index;
const BYTE m_range;
};
class Motor : public Output
{
friend class Device;
public:
std::string GetName() const;
protected:
Motor( const unsigned int index, const WORD range ) : m_index(index), m_range(range) {}
void SetState( const ControlState state, XINPUT_VIBRATION* const vibration );
private:
const unsigned int m_index;
const WORD m_range;
};
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input );
void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );
void ClearInputState();
public:
Device( const XINPUT_CAPABILITIES* const capabilities, const unsigned int index );
std::string GetName() const;
int GetId() const;
std::string GetSource() const;
private:
const unsigned int m_index;
XINPUT_STATE m_state_in;
XINPUT_VIBRATION m_state_out;
const unsigned int m_subtype;
};
}
}
#endif

View File

@ -0,0 +1,98 @@
#include "../ControllerInterface.h"
#ifdef CIFACE_USE_XLIB
#include "Xlib.h"
namespace ciface
{
namespace Xlib
{
void Init( std::vector<ControllerInterface::Device*>& devices, void* const hwnd )
{
// mouse will be added to this, Keyboard class will be turned into KeyboardMouse
// single device for combined keyboard/mouse, this will allow combinations like shift+click more easily
devices.push_back( new Keyboard( (Display*)display ) );
}
Keyboard::Keyboard( Display* const display ) : m_display(display)
{
memset( &m_state, 0, sizeof(m_state) );
// this is probably dumb
for ( KeySym i = 0; i < 1024; ++i )
{
if ( XKeysymToString( m_keysym ) ) // if it isnt NULL
inputs.push_back( new Key( i ) );
}
}
Keyboard::~Keyboard()
{
// might not need this func
}
ControlState Keyboard::GetInputState( const ControllerInterface::Device::Input* const input )
{
return ((Input*)input)->GetState( &m_state );
}
void Keyboard::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
}
bool Keyboard::UpdateInput()
{
XQueryKeymap( m_display, m_state.keyboard );
// mouse stuff in here too
return true;
}
bool Keyboard::UpdateOutput()
{
return true;
}
std::string Keyboard::GetName() const
{
return "Keyboard";
//return "Keyboard Mouse"; // change to this later
}
std::string Keyboard::GetSource() const
{
return "Xlib";
}
int Keyboard::GetId() const
{
return 0;
}
ControlState Keyboard::Key::GetState( const State* const state )
{
key_code = XKeysymToKeycode(m_display, m_keysym );
return (state->keyboard[key_code/8] & (1 << (key_code%8))); // need >0 ?
}
std::string Keyboard::Key::GetName() const
{
return XKeysymToString( m_keysym );
}
}
}
#endif

View File

@ -0,0 +1,70 @@
#ifndef _CIFACE_XLIB_H_
#define _CIFACE_XLIB_H_
#include "../ControllerInterface.h"
#include <X11/Xlib.h>
namespace ciface
{
namespace XInput
{
void Init( std::vector<ControllerInterface::Device*>& devices, void* const hwnd );
class Keyboard : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
struct State
{
char keyboard[32];
// mouse crap will go here
};
class Input : public ControllerInterface::Input
{
friend class Keyboard;
protected:
ControlState GetState( const State* const state ) = 0;
}
class Key : public Input
{
friend class Keyboard;
public:
std::string GetName() const;
protected:
Key( KeySym keysym ) : m_keysym(keysym) {}
ControlState GetState( const State* const state );
private:
const KeySym m_keysym
};
bool UpdateInput();
bool UpdateOutput();
ControlState Keyboard::GetInputState( const ControllerInterface::Device::Input* const input );
void Keyboard::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state );
public:
Keyboard( Display* const display );
~Keyboard();
std::string GetName() const;
std::string GetSource() const;
int GetId() const;
private:
Display* const m_display;
State m_state;
};
}
}
#endif