mirror of
https://github.com/izzy2lost/dolphin.git
synced 2026-06-19 01:16:48 -07:00
Remove unnecessary Src/ folders
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
#include "Android.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
|
||||
namespace Android
|
||||
{
|
||||
|
||||
void Init( std::vector<Core::Device*>& devices )
|
||||
{
|
||||
devices.push_back(new Touchscreen(0));
|
||||
devices.push_back(new Touchscreen(1));
|
||||
devices.push_back(new Touchscreen(2));
|
||||
devices.push_back(new Touchscreen(3));
|
||||
}
|
||||
|
||||
// Touchscreens and stuff
|
||||
std::string Touchscreen::GetName() const
|
||||
{
|
||||
return "Touchscreen";
|
||||
}
|
||||
|
||||
std::string Touchscreen::GetSource() const
|
||||
{
|
||||
return "Android";
|
||||
}
|
||||
|
||||
int Touchscreen::GetId() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Touchscreen::Touchscreen(int padID)
|
||||
: _padID(padID)
|
||||
{
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_A));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_B));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_START));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_X));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_Y));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_Z));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_UP));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_DOWN));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_LEFT));
|
||||
AddInput(new Button(_padID, ButtonManager::BUTTON_RIGHT));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::STICK_MAIN_LEFT, -1.0f), new Axis(_padID, ButtonManager::STICK_MAIN_RIGHT));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::STICK_MAIN_UP, -1.0f), new Axis(_padID, ButtonManager::STICK_MAIN_DOWN));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::STICK_C_UP, -1.0f), new Axis(_padID, ButtonManager::STICK_C_DOWN));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::STICK_C_LEFT, -1.0f), new Axis(_padID, ButtonManager::STICK_C_RIGHT));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::TRIGGER_L), new Axis(_padID, ButtonManager::TRIGGER_L));
|
||||
AddAnalogInputs(new Axis(_padID, ButtonManager::TRIGGER_R), new Axis(_padID, ButtonManager::TRIGGER_R));
|
||||
}
|
||||
// Buttons and stuff
|
||||
|
||||
std::string Touchscreen::Button::GetName() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "Button " << (int)_index;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
ControlState Touchscreen::Button::GetState() const
|
||||
{
|
||||
return ButtonManager::GetButtonPressed(_padID, _index);
|
||||
}
|
||||
std::string Touchscreen::Axis::GetName() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "Axis " << (int)_index;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
ControlState Touchscreen::Axis::GetState() const
|
||||
{
|
||||
return ButtonManager::GetAxisValue(_padID, _index) * _neg;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
#ifndef _CIFACE_ANDROID_H_
|
||||
#define _CIFACE_ANDROID_H_
|
||||
|
||||
#include "../Device.h"
|
||||
#include "Android/ButtonManager.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace Android
|
||||
{
|
||||
|
||||
void Init( std::vector<Core::Device*>& devices );
|
||||
class Touchscreen : public Core::Device
|
||||
{
|
||||
private:
|
||||
class Button : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Button(int padID, ButtonManager::ButtonType index) : _padID(padID), _index(index) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const int _padID;
|
||||
const ButtonManager::ButtonType _index;
|
||||
};
|
||||
class Axis : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Axis(int padID, ButtonManager::ButtonType index, float neg = 1.0f) : _padID(padID), _index(index), _neg(neg) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const int _padID;
|
||||
const ButtonManager::ButtonType _index;
|
||||
const float _neg;
|
||||
};
|
||||
|
||||
public:
|
||||
bool UpdateInput() { return true; }
|
||||
bool UpdateOutput() { return true; }
|
||||
|
||||
Touchscreen(int padID);
|
||||
~Touchscreen() {}
|
||||
|
||||
std::string GetName() const;
|
||||
int GetId() const;
|
||||
std::string GetSource() const;
|
||||
private:
|
||||
const int _padID;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,316 @@
|
||||
#include "ControllerInterface.h"
|
||||
|
||||
#ifdef CIFACE_USE_XINPUT
|
||||
#include "XInput/XInput.h"
|
||||
#endif
|
||||
#ifdef CIFACE_USE_DINPUT
|
||||
#include "DInput/DInput.h"
|
||||
#endif
|
||||
#ifdef CIFACE_USE_XLIB
|
||||
#include "Xlib/Xlib.h"
|
||||
#ifdef CIFACE_USE_X11_XINPUT2
|
||||
#include "Xlib/XInput2.h"
|
||||
#endif
|
||||
#endif
|
||||
#ifdef CIFACE_USE_OSX
|
||||
#include "OSX/OSX.h"
|
||||
#endif
|
||||
#ifdef CIFACE_USE_SDL
|
||||
#include "SDL/SDL.h"
|
||||
#endif
|
||||
#ifdef CIFACE_USE_ANDROID
|
||||
#include "Android/Android.h"
|
||||
#endif
|
||||
|
||||
#include "Thread.h"
|
||||
|
||||
using namespace ciface::ExpressionParser;
|
||||
|
||||
namespace
|
||||
{
|
||||
const float INPUT_DETECT_THRESHOLD = 0.55f;
|
||||
}
|
||||
|
||||
ControllerInterface g_controller_interface;
|
||||
|
||||
//
|
||||
// Init
|
||||
//
|
||||
// detect devices and inputs outputs / will make refresh function later
|
||||
//
|
||||
void ControllerInterface::Initialize()
|
||||
{
|
||||
if (m_is_init)
|
||||
return;
|
||||
|
||||
#ifdef CIFACE_USE_DINPUT
|
||||
ciface::DInput::Init(m_devices, (HWND)m_hwnd);
|
||||
#endif
|
||||
#ifdef CIFACE_USE_XINPUT
|
||||
ciface::XInput::Init(m_devices);
|
||||
#endif
|
||||
#ifdef CIFACE_USE_XLIB
|
||||
ciface::Xlib::Init(m_devices, m_hwnd);
|
||||
#ifdef CIFACE_USE_X11_XINPUT2
|
||||
ciface::XInput2::Init(m_devices, m_hwnd);
|
||||
#endif
|
||||
#endif
|
||||
#ifdef CIFACE_USE_OSX
|
||||
ciface::OSX::Init(m_devices, m_hwnd);
|
||||
#endif
|
||||
#ifdef CIFACE_USE_SDL
|
||||
ciface::SDL::Init(m_devices);
|
||||
#endif
|
||||
#ifdef CIFACE_USE_ANDROID
|
||||
ciface::Android::Init(m_devices);
|
||||
#endif
|
||||
|
||||
m_is_init = true;
|
||||
}
|
||||
|
||||
//
|
||||
// DeInit
|
||||
//
|
||||
// remove all devices/ call library cleanup functions
|
||||
//
|
||||
void ControllerInterface::Shutdown()
|
||||
{
|
||||
if (false == m_is_init)
|
||||
return;
|
||||
|
||||
std::vector<Device*>::const_iterator
|
||||
d = m_devices.begin(),
|
||||
de = m_devices.end();
|
||||
for ( ;d != de; ++d )
|
||||
{
|
||||
std::vector<Device::Output*>::const_iterator
|
||||
o = (*d)->Outputs().begin(),
|
||||
oe = (*d)->Outputs().end();
|
||||
// set outputs to ZERO before destroying device
|
||||
for ( ;o!=oe; ++o)
|
||||
(*o)->SetState(0);
|
||||
// update output
|
||||
(*d)->UpdateOutput();
|
||||
|
||||
//delete device
|
||||
delete *d;
|
||||
}
|
||||
|
||||
m_devices.clear();
|
||||
|
||||
#ifdef CIFACE_USE_XINPUT
|
||||
ciface::XInput::DeInit();
|
||||
#endif
|
||||
#ifdef CIFACE_USE_DINPUT
|
||||
// nothing needed
|
||||
#endif
|
||||
#ifdef CIFACE_USE_XLIB
|
||||
// nothing needed
|
||||
#endif
|
||||
#ifdef CIFACE_USE_OSX
|
||||
ciface::OSX::DeInit();
|
||||
#endif
|
||||
#ifdef CIFACE_USE_SDL
|
||||
// TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
|
||||
SDL_Quit();
|
||||
#endif
|
||||
#ifdef CIFACE_USE_ANDROID
|
||||
// nothing needed
|
||||
#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(const bool force)
|
||||
{
|
||||
std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
|
||||
|
||||
if (force)
|
||||
lk.lock();
|
||||
else if (!lk.try_lock())
|
||||
return false;
|
||||
|
||||
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
|
||||
// disabled. it might be causing problems
|
||||
//(*d)->ClearInputState();
|
||||
}
|
||||
|
||||
return (m_devices.size() == ok_count);
|
||||
}
|
||||
|
||||
//
|
||||
// UpdateOutput
|
||||
//
|
||||
// update output for all devices, return true if all devices returned successful
|
||||
//
|
||||
bool ControllerInterface::UpdateOutput(const bool force)
|
||||
{
|
||||
std::unique_lock<std::recursive_mutex> lk(update_lock, std::defer_lock);
|
||||
|
||||
if (force)
|
||||
lk.lock();
|
||||
else if (!lk.try_lock())
|
||||
return false;
|
||||
|
||||
size_t ok_count = 0;
|
||||
|
||||
for (auto d = m_devices.cbegin(); d != m_devices.cend(); ++d)
|
||||
{
|
||||
if ((*d)->UpdateOutput())
|
||||
++ok_count;
|
||||
}
|
||||
|
||||
return (m_devices.size() == ok_count);
|
||||
}
|
||||
|
||||
//
|
||||
// InputReference :: State
|
||||
//
|
||||
// get the state of an input reference
|
||||
// override function for ControlReference::State ...
|
||||
//
|
||||
ControlState ControllerInterface::InputReference::State( const ControlState ignore )
|
||||
{
|
||||
if (parsed_expression)
|
||||
return parsed_expression->GetValue() * range;
|
||||
else
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
if (parsed_expression)
|
||||
parsed_expression->SetValue(state);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
//
|
||||
// UpdateReference
|
||||
//
|
||||
// updates a controlreference's binded devices/controls
|
||||
// need to call this to re-parse a control reference's expression after changing it
|
||||
//
|
||||
void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref
|
||||
, const DeviceQualifier& default_device) const
|
||||
{
|
||||
delete ref->parsed_expression;
|
||||
ref->parsed_expression = NULL;
|
||||
|
||||
ControlFinder finder(*this, default_device, ref->is_input);
|
||||
ref->parse_error = ParseExpression(ref->expression, finder, &ref->parsed_expression);
|
||||
}
|
||||
|
||||
//
|
||||
// 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
|
||||
// upon input, return pointer to detected Control
|
||||
// else return NULL
|
||||
//
|
||||
Device::Control* ControllerInterface::InputReference::Detect(const unsigned int ms, Device* const device)
|
||||
{
|
||||
unsigned int time = 0;
|
||||
std::vector<bool> states(device->Inputs().size());
|
||||
|
||||
if (device->Inputs().size() == 0)
|
||||
return NULL;
|
||||
|
||||
// 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 (std::vector<bool>::iterator state = states.begin(); i != e; ++i)
|
||||
*state++ = ((*i)->GetState() > (1 - INPUT_DETECT_THRESHOLD));
|
||||
|
||||
while (time < ms)
|
||||
{
|
||||
device->UpdateInput();
|
||||
i = device->Inputs().begin();
|
||||
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i,++state)
|
||||
{
|
||||
// detected an input
|
||||
if ((*i)->IsDetectable() && (*i)->GetState() > INPUT_DETECT_THRESHOLD)
|
||||
{
|
||||
// input was released at some point during Detect call
|
||||
// return the detected input
|
||||
if (false == *state)
|
||||
return *i;
|
||||
}
|
||||
else if ((*i)->GetState() < (1 - INPUT_DETECT_THRESHOLD))
|
||||
{
|
||||
*state = false;
|
||||
}
|
||||
}
|
||||
Common::SleepCurrentThread(10); time += 10;
|
||||
}
|
||||
|
||||
// no input was detected
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// OutputReference :: Detect
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// set all binded outputs to <range> power for x milliseconds return false
|
||||
//
|
||||
Device::Control* ControllerInterface::OutputReference::Detect(const unsigned int ms, Device* const device)
|
||||
{
|
||||
// ignore device
|
||||
|
||||
// don't hang if we don't even have any controls mapped
|
||||
if (BoundCount() > 0)
|
||||
{
|
||||
State(1);
|
||||
unsigned int slept = 0;
|
||||
|
||||
// this loop is to make stuff like flashing keyboard LEDs work
|
||||
while (ms > (slept += 10))
|
||||
{
|
||||
// TODO: improve this to update more than just the default device's output
|
||||
device->UpdateOutput();
|
||||
Common::SleepCurrentThread(10);
|
||||
}
|
||||
|
||||
State(0);
|
||||
device->UpdateOutput();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
#ifndef _DEVICEINTERFACE_H_
|
||||
#define _DEVICEINTERFACE_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
|
||||
#include "Common.h"
|
||||
#include "Thread.h"
|
||||
#include "ExpressionParser.h"
|
||||
#include "Device.h"
|
||||
|
||||
// enable disable sources
|
||||
#ifdef _WIN32
|
||||
#define CIFACE_USE_XINPUT
|
||||
#define CIFACE_USE_DINPUT
|
||||
#define CIFACE_USE_SDL
|
||||
#endif
|
||||
#if defined(HAVE_X11) && HAVE_X11
|
||||
#define CIFACE_USE_XLIB
|
||||
#define CIFACE_USE_SDL
|
||||
#if defined(HAVE_X11_XINPUT2) && HAVE_X11_XINPUT2
|
||||
#define CIFACE_USE_X11_XINPUT2
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
#define CIFACE_USE_OSX
|
||||
#endif
|
||||
#ifdef ANDROID
|
||||
#define CIFACE_USE_ANDROID
|
||||
#endif
|
||||
|
||||
using namespace ciface::Core;
|
||||
|
||||
//
|
||||
// ControllerInterface
|
||||
//
|
||||
// some crazy shit I made to control different device inputs and outputs
|
||||
// from lots of different sources, hopefully more easily
|
||||
//
|
||||
class ControllerInterface : public DeviceContainer
|
||||
{
|
||||
public:
|
||||
|
||||
//
|
||||
// ControlReference
|
||||
//
|
||||
// these are what you create to actually use the inputs, InputReference or OutputReference
|
||||
//
|
||||
// after being bound to devices and controls with ControllerInterface::UpdateReference,
|
||||
// each one can link to multiple devices and controls
|
||||
// when you change a ControlReference's expression,
|
||||
// you must use ControllerInterface::UpdateReference on it to rebind controls
|
||||
//
|
||||
class ControlReference
|
||||
{
|
||||
friend class ControllerInterface;
|
||||
public:
|
||||
virtual ControlState State(const ControlState state = 0) = 0;
|
||||
virtual Device::Control* Detect(const unsigned int ms, Device* const device) = 0;
|
||||
|
||||
ControlState range;
|
||||
std::string expression;
|
||||
const bool is_input;
|
||||
ciface::ExpressionParser::ExpressionParseStatus parse_error;
|
||||
|
||||
virtual ~ControlReference() {
|
||||
delete parsed_expression;
|
||||
}
|
||||
|
||||
int BoundCount() {
|
||||
if (parsed_expression)
|
||||
return parsed_expression->num_controls;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
ControlReference(const bool _is_input) : range(1), is_input(_is_input), parsed_expression(NULL) {}
|
||||
ciface::ExpressionParser::Expression *parsed_expression;
|
||||
};
|
||||
|
||||
//
|
||||
// InputReference
|
||||
//
|
||||
// control reference for inputs
|
||||
//
|
||||
class InputReference : public ControlReference
|
||||
{
|
||||
public:
|
||||
InputReference() : ControlReference(true) {}
|
||||
ControlState State(const ControlState state);
|
||||
Device::Control* Detect(const unsigned int ms, Device* const device);
|
||||
};
|
||||
|
||||
//
|
||||
// OutputReference
|
||||
//
|
||||
// control reference for outputs
|
||||
//
|
||||
class OutputReference : public ControlReference
|
||||
{
|
||||
public:
|
||||
OutputReference() : ControlReference(false) {}
|
||||
ControlState State(const ControlState state);
|
||||
Device::Control* Detect(const unsigned int ms, Device* const device);
|
||||
};
|
||||
|
||||
ControllerInterface() : m_is_init(false), m_hwnd(NULL) {}
|
||||
|
||||
void SetHwnd(void* const hwnd);
|
||||
void Initialize();
|
||||
void Shutdown();
|
||||
bool IsInit() const { return m_is_init; }
|
||||
|
||||
void UpdateReference(ControlReference* control, const DeviceQualifier& default_device) const;
|
||||
bool UpdateInput(const bool force = false);
|
||||
bool UpdateOutput(const bool force = false);
|
||||
|
||||
std::recursive_mutex update_lock;
|
||||
|
||||
private:
|
||||
bool m_is_init;
|
||||
void* m_hwnd;
|
||||
};
|
||||
|
||||
extern ControllerInterface g_controller_interface;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
|
||||
#include "DInput.h"
|
||||
|
||||
#include "StringUtil.h"
|
||||
|
||||
#include "DInputJoystick.h"
|
||||
#include "DInputKeyboardMouse.h"
|
||||
|
||||
#pragma comment(lib, "Dinput8.lib")
|
||||
#pragma comment(lib, "dxguid.lib")
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace DInput
|
||||
{
|
||||
|
||||
//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef)
|
||||
//{
|
||||
// ((std::list<DIEFFECTINFO>*)pvRef)->push_back(*pdei);
|
||||
// return DIENUM_CONTINUE;
|
||||
//}
|
||||
|
||||
BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
|
||||
{
|
||||
((std::list<DIDEVICEOBJECTINSTANCE>*)pvRef)->push_back(*lpddoi);
|
||||
return DIENUM_CONTINUE;
|
||||
}
|
||||
|
||||
BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
|
||||
{
|
||||
((std::list<DIDEVICEINSTANCE>*)pvRef)->push_back(*lpddi);
|
||||
return DIENUM_CONTINUE;
|
||||
}
|
||||
|
||||
std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device)
|
||||
{
|
||||
DIPROPSTRING str = {};
|
||||
str.diph.dwSize = sizeof(str);
|
||||
str.diph.dwHeaderSize = sizeof(str.diph);
|
||||
str.diph.dwHow = DIPH_DEVICE;
|
||||
|
||||
std::string result;
|
||||
if (SUCCEEDED(device->GetProperty(DIPROP_PRODUCTNAME, &str.diph)))
|
||||
{
|
||||
result = StripSpaces(UTF16ToUTF8(str.wsz));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Init(std::vector<Core::Device*>& devices, HWND hwnd)
|
||||
{
|
||||
IDirectInput8* idi8;
|
||||
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&idi8, NULL)))
|
||||
return;
|
||||
|
||||
InitKeyboardMouse(idi8, devices, hwnd);
|
||||
InitJoystick(idi8, devices, hwnd);
|
||||
|
||||
idi8->Release();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef _CIFACE_DINPUT_H_
|
||||
#define _CIFACE_DINPUT_H_
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
#define DINPUT_SOURCE_NAME "DInput"
|
||||
|
||||
#define DIRECTINPUT_VERSION 0x0800
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <dinput.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace DInput
|
||||
{
|
||||
|
||||
//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef);
|
||||
BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef);
|
||||
BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
|
||||
std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device);
|
||||
|
||||
void Init(std::vector<Core::Device*>& devices, HWND hwnd);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,109 @@
|
||||
#ifndef _CIFACE_DINPUT_JOYSTICK_H_
|
||||
#define _CIFACE_DINPUT_JOYSTICK_H_
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
#define DIRECTINPUT_VERSION 0x0800
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <dinput.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace DInput
|
||||
{
|
||||
|
||||
void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND hwnd);
|
||||
|
||||
class Joystick : public Core::Device
|
||||
{
|
||||
private:
|
||||
struct EffectState
|
||||
{
|
||||
EffectState(LPDIRECTINPUTEFFECT eff) : iface(eff), params(NULL), size(0) {}
|
||||
|
||||
LPDIRECTINPUTEFFECT iface;
|
||||
void* params; // null when force hasn't changed
|
||||
u8 size; // zero when force should stop
|
||||
};
|
||||
|
||||
class Button : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Button(u8 index, const BYTE& button) : m_index(index), m_button(button) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const BYTE& m_button;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
class Axis : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Axis(u8 index, const LONG& axis, LONG base, LONG range) : m_index(index), m_axis(axis), m_base(base), m_range(range) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const LONG& m_axis;
|
||||
const LONG m_base, m_range;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
class Hat : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Hat(u8 index, const DWORD& hat, u8 direction) : m_index(index), m_hat(hat), m_direction(direction) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const DWORD& m_hat;
|
||||
const u8 m_index, m_direction;
|
||||
};
|
||||
|
||||
template <typename P>
|
||||
class Force : public Output
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Force(u8 index, EffectState& state);
|
||||
void SetState(ControlState state);
|
||||
private:
|
||||
EffectState& m_state;
|
||||
P params;
|
||||
const u8 m_index;
|
||||
};
|
||||
typedef Force<DICONSTANTFORCE> ForceConstant;
|
||||
typedef Force<DIRAMPFORCE> ForceRamp;
|
||||
typedef Force<DIPERIODIC> ForcePeriodic;
|
||||
|
||||
public:
|
||||
bool UpdateInput();
|
||||
bool UpdateOutput();
|
||||
|
||||
void ClearInputState();
|
||||
|
||||
Joystick(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;
|
||||
|
||||
DIJOYSTATE m_state_in;
|
||||
std::list<EffectState> m_state_out;
|
||||
|
||||
bool m_buffered;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,311 @@
|
||||
|
||||
#include "DInputKeyboardMouse.h"
|
||||
#include "DInput.h"
|
||||
|
||||
// (lower would be more sensitive) user can lower sensitivity by setting range
|
||||
// seems decent here ( at 8 ), I don't 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 DInput
|
||||
{
|
||||
|
||||
static const struct
|
||||
{
|
||||
const BYTE code;
|
||||
const char* const name;
|
||||
} named_keys[] =
|
||||
{
|
||||
#include "NamedKeys.h"
|
||||
};
|
||||
|
||||
static const struct
|
||||
{
|
||||
const BYTE code;
|
||||
const char* const name;
|
||||
} named_lights[] =
|
||||
{
|
||||
{ VK_NUMLOCK, "NUM LOCK" },
|
||||
{ VK_CAPITAL, "CAPS LOCK" },
|
||||
{ VK_SCROLL, "SCROLL LOCK" }
|
||||
};
|
||||
|
||||
// lil silly
|
||||
static HWND hwnd;
|
||||
|
||||
void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd)
|
||||
{
|
||||
hwnd = _hwnd;
|
||||
|
||||
// mouse and keyboard are a combined device, to allow shift+click and stuff
|
||||
// if that's 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
|
||||
|
||||
LPDIRECTINPUTDEVICE8 kb_device = NULL;
|
||||
LPDIRECTINPUTDEVICE8 mo_device = NULL;
|
||||
|
||||
if (SUCCEEDED(idi8->CreateDevice( GUID_SysKeyboard, &kb_device, NULL)))
|
||||
{
|
||||
if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)))
|
||||
{
|
||||
if (SUCCEEDED(kb_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
||||
{
|
||||
if (SUCCEEDED(idi8->CreateDevice( GUID_SysMouse, &mo_device, NULL )))
|
||||
{
|
||||
if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)))
|
||||
{
|
||||
if (SUCCEEDED(mo_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
||||
{
|
||||
devices.push_back(new KeyboardMouse(kb_device, mo_device));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (kb_device)
|
||||
kb_device->Release();
|
||||
if (mo_device)
|
||||
mo_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_kb_device->Acquire();
|
||||
m_mo_device->Acquire();
|
||||
|
||||
m_last_update = GetTickCount();
|
||||
|
||||
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 (u8 i = 0; i < sizeof(named_keys)/sizeof(*named_keys); ++i)
|
||||
AddInput(new Key(i, m_state_in.keyboard[named_keys[i].code]));
|
||||
// add lights
|
||||
for (u8 i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i)
|
||||
AddOutput(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 (u8 i = 0; i < mouse_caps.dwButtons; ++i)
|
||||
AddInput(new Button(i, m_state_in.mouse.rgbButtons[i]));
|
||||
// mouse axes
|
||||
for (unsigned int i = 0; i < mouse_caps.dwAxes; ++i)
|
||||
{
|
||||
const LONG& ax = (&m_state_in.mouse.lX)[i];
|
||||
|
||||
// each axis gets a negative and a positive input instance associated with it
|
||||
AddInput(new Axis(i, ax, (2==i) ? -1 : -MOUSE_AXIS_SENSITIVITY));
|
||||
AddInput(new Axis(i, ax, -(2==i) ? 1 : MOUSE_AXIS_SENSITIVITY));
|
||||
}
|
||||
// cursor, with a hax for-loop
|
||||
for (unsigned int i=0; i<4; ++i)
|
||||
AddInput(new Cursor(!!(i&2), (&m_state_in.cursor.x)[i/2], !!(i&1)));
|
||||
}
|
||||
|
||||
void GetMousePos(float* const x, float* const y)
|
||||
{
|
||||
unsigned int win_width = 2, win_height = 2;
|
||||
POINT point = { 1, 1 };
|
||||
GetCursorPos(&point);
|
||||
// Get the cursor position relative to the upper left corner of the rendering window
|
||||
ScreenToClient(hwnd, &point);
|
||||
|
||||
// Get the size of the rendering window. (In my case Rect.top and Rect.left was zero.)
|
||||
RECT rect;
|
||||
GetClientRect(hwnd, &rect);
|
||||
// Width and height is the size of the rendering window
|
||||
win_width = rect.right - rect.left;
|
||||
win_height = rect.bottom - rect.top;
|
||||
|
||||
// Return the mouse position as a range from -1 to 1
|
||||
*x = (float)point.x / (float)win_width * 2 - 1;
|
||||
*y = (float)point.y / (float)win_height * 2 - 1;
|
||||
}
|
||||
|
||||
bool KeyboardMouse::UpdateInput()
|
||||
{
|
||||
DIMOUSESTATE2 tmp_mouse;
|
||||
|
||||
// if mouse position hasn't been updated in a short while, skip a dev state
|
||||
DWORD cur_time = GetTickCount();
|
||||
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;
|
||||
|
||||
HRESULT kb_hr = m_kb_device->GetDeviceState(sizeof(m_state_in.keyboard), &m_state_in.keyboard);
|
||||
HRESULT mo_hr = m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
|
||||
|
||||
if (DIERR_INPUTLOST == kb_hr || DIERR_NOTACQUIRED == kb_hr)
|
||||
m_kb_device->Acquire();
|
||||
|
||||
if (DIERR_INPUTLOST == mo_hr || DIERR_NOTACQUIRED == mo_hr)
|
||||
m_mo_device->Acquire();
|
||||
|
||||
if (SUCCEEDED(kb_hr) && SUCCEEDED(mo_hr))
|
||||
{
|
||||
// need to smooth out the axes, otherwise it doesn't 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));
|
||||
|
||||
// update mouse cursor
|
||||
GetMousePos(&m_state_in.cursor.x, &m_state_in.cursor.y);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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] > GetTickCount() % 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;
|
||||
}
|
||||
}
|
||||
|
||||
if (kbinputs.size())
|
||||
return ( kbinputs.size() == SendInput( (UINT)kbinputs.size(), &kbinputs[0], sizeof( kbinputs[0] ) ) );
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
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 DINPUT_SOURCE_NAME;
|
||||
}
|
||||
|
||||
// names
|
||||
std::string KeyboardMouse::Key::GetName() const
|
||||
{
|
||||
return named_keys[m_index].name;
|
||||
}
|
||||
|
||||
std::string KeyboardMouse::Button::GetName() const
|
||||
{
|
||||
return std::string("Click ") + char('0' + m_index);
|
||||
}
|
||||
|
||||
std::string KeyboardMouse::Axis::GetName() const
|
||||
{
|
||||
static char tmpstr[] = "Axis ..";
|
||||
tmpstr[5] = (char)('X' + m_index);
|
||||
tmpstr[6] = (m_range<0 ? '-' : '+');
|
||||
return tmpstr;
|
||||
}
|
||||
|
||||
std::string KeyboardMouse::Cursor::GetName() const
|
||||
{
|
||||
static char tmpstr[] = "Cursor ..";
|
||||
tmpstr[7] = (char)('X' + m_index);
|
||||
tmpstr[8] = (m_positive ? '+' : '-');
|
||||
return tmpstr;
|
||||
}
|
||||
|
||||
std::string KeyboardMouse::Light::GetName() const
|
||||
{
|
||||
return named_lights[m_index].name;
|
||||
}
|
||||
|
||||
// get/set state
|
||||
ControlState KeyboardMouse::Key::GetState() const
|
||||
{
|
||||
return (m_key != 0);
|
||||
}
|
||||
|
||||
ControlState KeyboardMouse::Button::GetState() const
|
||||
{
|
||||
return (m_button != 0);
|
||||
}
|
||||
|
||||
ControlState KeyboardMouse::Axis::GetState() const
|
||||
{
|
||||
return std::max(0.0f, ControlState(m_axis) / m_range);
|
||||
}
|
||||
|
||||
ControlState KeyboardMouse::Cursor::GetState() const
|
||||
{
|
||||
return std::max(0.0f, ControlState(m_axis) / (m_positive ? 1.0f : -1.0f));
|
||||
}
|
||||
|
||||
void KeyboardMouse::Light::SetState(const ControlState state)
|
||||
{
|
||||
//state_out[m_index] = (unsigned char)(state * 255);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#ifndef _CIFACE_DINPUT_KBM_H_
|
||||
#define _CIFACE_DINPUT_KBM_H_
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
#define DIRECTINPUT_VERSION 0x0800
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <dinput.h>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace DInput
|
||||
{
|
||||
|
||||
void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd);
|
||||
|
||||
class KeyboardMouse : public Core::Device
|
||||
{
|
||||
private:
|
||||
struct State
|
||||
{
|
||||
BYTE keyboard[256];
|
||||
DIMOUSESTATE2 mouse;
|
||||
struct
|
||||
{
|
||||
float x, y;
|
||||
} cursor;
|
||||
};
|
||||
|
||||
class Key : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Key(u8 index, const BYTE& key) : m_index(index), m_key(key) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const BYTE& m_key;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
class Button : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Button(u8 index, const BYTE& button) : m_index(index), m_button(button) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const BYTE& m_button;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
class Axis : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Axis(u8 index, const LONG& axis, LONG range) : m_index(index), m_axis(axis), m_range(range) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const LONG& m_axis;
|
||||
const LONG m_range;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
class Cursor : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
bool IsDetectable() { return false; }
|
||||
Cursor(u8 index, const float& axis, const bool positive) : m_index(index), m_axis(axis), m_positive(positive) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const float& m_axis;
|
||||
const u8 m_index;
|
||||
const bool m_positive;
|
||||
};
|
||||
|
||||
class Light : public Output
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Light(u8 index) : m_index(index) {}
|
||||
void SetState(ControlState state);
|
||||
private:
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
public:
|
||||
bool UpdateInput();
|
||||
bool UpdateOutput();
|
||||
|
||||
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;
|
||||
|
||||
DWORD 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
|
||||
@@ -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" },
|
||||
@@ -0,0 +1,193 @@
|
||||
|
||||
#include "Device.h"
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace Core
|
||||
{
|
||||
|
||||
//
|
||||
// Device :: ~Device
|
||||
//
|
||||
// Destructor, delete all inputs/outputs on device destruction
|
||||
//
|
||||
Device::~Device()
|
||||
{
|
||||
{
|
||||
// delete inputs
|
||||
std::vector<Device::Input*>::iterator
|
||||
i = m_inputs.begin(),
|
||||
e = m_inputs.end();
|
||||
for ( ;i!=e; ++i)
|
||||
delete *i;
|
||||
}
|
||||
|
||||
{
|
||||
// delete outputs
|
||||
std::vector<Device::Output*>::iterator
|
||||
o = m_outputs.begin(),
|
||||
e = m_outputs.end();
|
||||
for ( ;o!=e; ++o)
|
||||
delete *o;
|
||||
}
|
||||
}
|
||||
|
||||
void Device::AddInput(Device::Input* const i)
|
||||
{
|
||||
m_inputs.push_back(i);
|
||||
}
|
||||
|
||||
void Device::AddOutput(Device::Output* const o)
|
||||
{
|
||||
m_outputs.push_back(o);
|
||||
}
|
||||
|
||||
Device::Input* Device::FindInput(const std::string &name) const
|
||||
{
|
||||
std::vector<Input*>::const_iterator
|
||||
it = m_inputs.begin(),
|
||||
itend = m_inputs.end();
|
||||
for (; it != itend; ++it)
|
||||
if ((*it)->GetName() == name)
|
||||
return *it;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Device::Output* Device::FindOutput(const std::string &name) const
|
||||
{
|
||||
std::vector<Output*>::const_iterator
|
||||
it = m_outputs.begin(),
|
||||
itend = m_outputs.end();
|
||||
for (; it != itend; ++it)
|
||||
if ((*it)->GetName() == name)
|
||||
return *it;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// Device :: ClearInputState
|
||||
//
|
||||
// Device classes should override this function
|
||||
// ControllerInterface will call this when the device returns failure during UpdateInput
|
||||
// used to try to set all buttons and axes to their default state when user unplugs a gamepad during play
|
||||
// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging
|
||||
//
|
||||
void 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
|
||||
}
|
||||
|
||||
//
|
||||
// DeviceQualifier :: ToString
|
||||
//
|
||||
// get string from a device qualifier / serialize
|
||||
//
|
||||
std::string DeviceQualifier::ToString() const
|
||||
{
|
||||
if (source.empty() && (cid < 0) && name.empty())
|
||||
return "";
|
||||
std::ostringstream ss;
|
||||
ss << source << '/';
|
||||
if ( cid > -1 )
|
||||
ss << cid;
|
||||
ss << '/' << name;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
//
|
||||
// DeviceQualifier :: FromString
|
||||
//
|
||||
// set a device qualifier from a string / unserialize
|
||||
//
|
||||
void DeviceQualifier::FromString(const std::string& str)
|
||||
{
|
||||
std::istringstream ss(str);
|
||||
|
||||
std::getline(ss, source = "", '/');
|
||||
|
||||
// silly
|
||||
std::getline(ss, name, '/');
|
||||
std::istringstream(name) >> (cid = -1);
|
||||
|
||||
std::getline(ss, name = "");
|
||||
}
|
||||
|
||||
//
|
||||
// DeviceQualifier :: FromDevice
|
||||
//
|
||||
// set a device qualifier from a device
|
||||
//
|
||||
void DeviceQualifier::FromDevice(const Device* const dev)
|
||||
{
|
||||
name = dev->GetName();
|
||||
cid = dev->GetId();
|
||||
source= dev->GetSource();
|
||||
}
|
||||
|
||||
bool DeviceQualifier::operator==(const Device* const dev) const
|
||||
{
|
||||
if (dev->GetId() == cid)
|
||||
if (dev->GetName() == name)
|
||||
if (dev->GetSource() == source)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeviceQualifier::operator==(const DeviceQualifier& devq) const
|
||||
{
|
||||
if (cid == devq.cid)
|
||||
if (name == devq.name)
|
||||
if (source == devq.source)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Device* DeviceContainer::FindDevice(const DeviceQualifier& devq) const
|
||||
{
|
||||
std::vector<Device*>::const_iterator
|
||||
di = m_devices.begin(),
|
||||
de = m_devices.end();
|
||||
for (; di!=de; ++di)
|
||||
if (devq == *di)
|
||||
return *di;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const
|
||||
{
|
||||
if (def_dev)
|
||||
{
|
||||
Device::Input* const inp = def_dev->FindInput(name);
|
||||
if (inp)
|
||||
return inp;
|
||||
}
|
||||
|
||||
std::vector<Device*>::const_iterator
|
||||
di = m_devices.begin(),
|
||||
de = m_devices.end();
|
||||
for (; di != de; ++di)
|
||||
{
|
||||
Device::Input* const i = (*di)->FindInput(name);
|
||||
|
||||
if (i)
|
||||
return i;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const
|
||||
{
|
||||
return def_dev->FindOutput(name);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
|
||||
#ifndef _DEVICE_H_
|
||||
#define _DEVICE_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
// idk in case I wanted to change it to double or something, idk what's best
|
||||
typedef float ControlState;
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace Core
|
||||
{
|
||||
|
||||
// Forward declarations
|
||||
class DeviceQualifier;
|
||||
|
||||
//
|
||||
// Device
|
||||
//
|
||||
// a device class
|
||||
//
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
class Input;
|
||||
class Output;
|
||||
|
||||
//
|
||||
// Control
|
||||
//
|
||||
// control includes inputs and outputs
|
||||
//
|
||||
class Control // input or output
|
||||
{
|
||||
public:
|
||||
virtual std::string GetName() const = 0;
|
||||
virtual ~Control() {}
|
||||
|
||||
virtual Input* ToInput() { return NULL; }
|
||||
virtual Output* ToOutput() { return NULL; }
|
||||
};
|
||||
|
||||
//
|
||||
// Input
|
||||
//
|
||||
// an input on a device
|
||||
//
|
||||
class Input : public Control
|
||||
{
|
||||
public:
|
||||
// things like absolute axes/ absolute mouse position will override this
|
||||
virtual bool IsDetectable() { return true; }
|
||||
|
||||
virtual ControlState GetState() const = 0;
|
||||
|
||||
Input* ToInput() { return this; }
|
||||
};
|
||||
|
||||
//
|
||||
// Output
|
||||
//
|
||||
// an output on a device
|
||||
//
|
||||
class Output : public Control
|
||||
{
|
||||
public:
|
||||
virtual ~Output() {}
|
||||
|
||||
virtual void SetState(ControlState state) = 0;
|
||||
|
||||
Output* ToOutput() { return this; }
|
||||
};
|
||||
|
||||
virtual ~Device();
|
||||
|
||||
virtual std::string GetName() const = 0;
|
||||
virtual int GetId() const = 0;
|
||||
virtual std::string GetSource() const = 0;
|
||||
virtual bool UpdateInput() = 0;
|
||||
virtual bool UpdateOutput() = 0;
|
||||
|
||||
virtual void ClearInputState();
|
||||
|
||||
const std::vector<Input*>& Inputs() const { return m_inputs; }
|
||||
const std::vector<Output*>& Outputs() const { return m_outputs; }
|
||||
|
||||
Input* FindInput(const std::string& name) const;
|
||||
Output* FindOutput(const std::string& name) const;
|
||||
|
||||
protected:
|
||||
void AddInput(Input* const i);
|
||||
void AddOutput(Output* const o);
|
||||
|
||||
class FullAnalogSurface : public Input
|
||||
{
|
||||
public:
|
||||
FullAnalogSurface(Input* low, Input* high)
|
||||
: m_low(*low), m_high(*high)
|
||||
{}
|
||||
|
||||
ControlState GetState() const
|
||||
{
|
||||
return (1 + m_high.GetState() - m_low.GetState()) / 2;
|
||||
}
|
||||
|
||||
std::string GetName() const
|
||||
{
|
||||
return m_low.GetName() + *m_high.GetName().rbegin();
|
||||
}
|
||||
|
||||
private:
|
||||
Input& m_low;
|
||||
Input& m_high;
|
||||
};
|
||||
|
||||
void AddAnalogInputs(Input* low, Input* high)
|
||||
{
|
||||
AddInput(low);
|
||||
AddInput(high);
|
||||
AddInput(new FullAnalogSurface(low, high));
|
||||
AddInput(new FullAnalogSurface(high, low));
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Input*> m_inputs;
|
||||
std::vector<Output*> m_outputs;
|
||||
};
|
||||
|
||||
//
|
||||
// DeviceQualifier
|
||||
//
|
||||
// device qualifier used to match devices
|
||||
// currently has ( source, id, name ) properties which match a device
|
||||
//
|
||||
class DeviceQualifier
|
||||
{
|
||||
public:
|
||||
DeviceQualifier() : cid(-1) {}
|
||||
DeviceQualifier(const std::string& _source, const int _id, const std::string& _name)
|
||||
: source(_source), cid(_id), name(_name) {}
|
||||
void FromDevice(const Device* const dev);
|
||||
void FromString(const std::string& str);
|
||||
std::string ToString() const;
|
||||
bool operator==(const DeviceQualifier& devq) const;
|
||||
bool operator==(const Device* const dev) const;
|
||||
|
||||
std::string source;
|
||||
int cid;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class DeviceContainer
|
||||
{
|
||||
public:
|
||||
Device::Input* FindInput(const std::string& name, const Device* def_dev) const;
|
||||
Device::Output* FindOutput(const std::string& name, const Device* def_dev) const;
|
||||
|
||||
const std::vector<Device*>& Devices() const { return m_devices; }
|
||||
Device* FindDevice(const DeviceQualifier& devq) const;
|
||||
protected:
|
||||
std::vector<Device*> m_devices;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,69 @@
|
||||
|
||||
#ifndef _EXPRESSIONPARSER_H_
|
||||
#define _EXPRESSIONPARSER_H_
|
||||
|
||||
#include <string>
|
||||
#include "Device.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace ExpressionParser
|
||||
{
|
||||
|
||||
class ControlQualifier
|
||||
{
|
||||
public:
|
||||
bool has_device;
|
||||
Core::DeviceQualifier device_qualifier;
|
||||
std::string control_name;
|
||||
|
||||
ControlQualifier() : has_device(false) {}
|
||||
|
||||
operator std::string()
|
||||
{
|
||||
if (has_device)
|
||||
return device_qualifier.ToString() + ":" + control_name;
|
||||
else
|
||||
return control_name;
|
||||
}
|
||||
};
|
||||
|
||||
class ControlFinder
|
||||
{
|
||||
public:
|
||||
ControlFinder(const Core::DeviceContainer &container_, const Core::DeviceQualifier &default_, const bool is_input_) : container(container_), default_device(default_), is_input(is_input_) {}
|
||||
Core::Device::Control *FindControl(ControlQualifier qualifier);
|
||||
|
||||
private:
|
||||
Core::Device *FindDevice(ControlQualifier qualifier);
|
||||
const Core::DeviceContainer &container;
|
||||
const Core::DeviceQualifier &default_device;
|
||||
bool is_input;
|
||||
};
|
||||
|
||||
class ExpressionNode;
|
||||
class Expression
|
||||
{
|
||||
public:
|
||||
Expression() : node(NULL) {}
|
||||
Expression(ExpressionNode *node);
|
||||
~Expression();
|
||||
ControlState GetValue();
|
||||
void SetValue (ControlState state);
|
||||
int num_controls;
|
||||
ExpressionNode *node;
|
||||
};
|
||||
|
||||
enum ExpressionParseStatus
|
||||
{
|
||||
EXPRESSION_PARSE_SUCCESS = 0,
|
||||
EXPRESSION_PARSE_SYNTAX_ERROR,
|
||||
EXPRESSION_PARSE_NO_DEVICE,
|
||||
};
|
||||
|
||||
ExpressionParseStatus ParseExpression(std::string expr, ControlFinder &finder, Expression **expr_out);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace OSX
|
||||
{
|
||||
|
||||
void Init(std::vector<Core::Device*>& devices, void *window);
|
||||
void DeInit();
|
||||
|
||||
void DeviceElementDebugPrint(const void *, void *);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
#include <Foundation/Foundation.h>
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
#include <Cocoa/Cocoa.h>
|
||||
|
||||
#include "OSX.h"
|
||||
#include "OSXKeyboard.h"
|
||||
#include "OSXJoystick.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace OSX
|
||||
{
|
||||
|
||||
|
||||
static IOHIDManagerRef HIDManager = NULL;
|
||||
static CFStringRef OurRunLoop = CFSTR("DolphinOSXInput");
|
||||
static std::map<std::string, int> kbd_name_counts, joy_name_counts;
|
||||
|
||||
void DeviceElementDebugPrint(const void *value, void *context)
|
||||
{
|
||||
IOHIDElementRef e = (IOHIDElementRef)value;
|
||||
bool recurse = false;
|
||||
if (context)
|
||||
recurse = *(bool*)context;
|
||||
|
||||
std::string type = "";
|
||||
switch (IOHIDElementGetType(e)) {
|
||||
case kIOHIDElementTypeInput_Axis:
|
||||
type = "axis";
|
||||
break;
|
||||
case kIOHIDElementTypeInput_Button:
|
||||
type = "button";
|
||||
break;
|
||||
case kIOHIDElementTypeInput_Misc:
|
||||
type = "misc";
|
||||
break;
|
||||
case kIOHIDElementTypeInput_ScanCodes:
|
||||
type = "scancodes";
|
||||
break;
|
||||
case kIOHIDElementTypeOutput:
|
||||
type = "output";
|
||||
break;
|
||||
case kIOHIDElementTypeFeature:
|
||||
type = "feature";
|
||||
break;
|
||||
case kIOHIDElementTypeCollection:
|
||||
type = "collection";
|
||||
break;
|
||||
}
|
||||
|
||||
std::string c_type = "";
|
||||
if (type == "collection")
|
||||
{
|
||||
switch (IOHIDElementGetCollectionType(e)) {
|
||||
case kIOHIDElementCollectionTypePhysical:
|
||||
c_type = "physical";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeApplication:
|
||||
c_type = "application";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeLogical:
|
||||
c_type = "logical";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeReport:
|
||||
c_type = "report";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeNamedArray:
|
||||
c_type = "namedArray";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeUsageSwitch:
|
||||
c_type = "usageSwitch";
|
||||
break;
|
||||
case kIOHIDElementCollectionTypeUsageModifier:
|
||||
c_type = "usageModifier";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
c_type.append(" ");
|
||||
NSLog(@"%s%s%spage: 0x%x usage: 0x%x name: %@ "
|
||||
"lmin: %ld lmax: %ld pmin: %ld pmax: %ld",
|
||||
type.c_str(),
|
||||
type == "collection" ? ":" : "",
|
||||
type == "collection" ? c_type.c_str() : " ",
|
||||
IOHIDElementGetUsagePage(e),
|
||||
IOHIDElementGetUsage(e),
|
||||
IOHIDElementGetName(e), // usually just NULL
|
||||
IOHIDElementGetLogicalMin(e),
|
||||
IOHIDElementGetLogicalMax(e),
|
||||
IOHIDElementGetPhysicalMin(e),
|
||||
IOHIDElementGetPhysicalMax(e));
|
||||
|
||||
if ((type == "collection") && recurse)
|
||||
{
|
||||
CFArrayRef elements = IOHIDElementGetChildren(e);
|
||||
CFRange range = {0, CFArrayGetCount(elements)};
|
||||
// this leaks...but it's just debug code, right? :D
|
||||
CFArrayApplyFunction(elements, range,
|
||||
DeviceElementDebugPrint, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceDebugPrint(IOHIDDeviceRef device)
|
||||
{
|
||||
#if 0
|
||||
#define shortlog(x) NSLog(@"%s: %@", \
|
||||
x, IOHIDDeviceGetProperty(device, CFSTR(x)));
|
||||
NSLog(@"-------------------------");
|
||||
NSLog(@"Got Device: %@",
|
||||
IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)));
|
||||
shortlog(kIOHIDTransportKey)
|
||||
shortlog(kIOHIDVendorIDKey)
|
||||
shortlog(kIOHIDVendorIDSourceKey)
|
||||
shortlog(kIOHIDProductIDKey)
|
||||
shortlog(kIOHIDVersionNumberKey)
|
||||
shortlog(kIOHIDManufacturerKey)
|
||||
shortlog(kIOHIDProductKey)
|
||||
shortlog(kIOHIDSerialNumberKey)
|
||||
shortlog(kIOHIDCountryCodeKey)
|
||||
shortlog(kIOHIDLocationIDKey)
|
||||
shortlog(kIOHIDDeviceUsageKey)
|
||||
shortlog(kIOHIDDeviceUsagePageKey)
|
||||
shortlog(kIOHIDDeviceUsagePairsKey)
|
||||
shortlog(kIOHIDPrimaryUsageKey)
|
||||
shortlog(kIOHIDPrimaryUsagePageKey)
|
||||
shortlog(kIOHIDMaxInputReportSizeKey)
|
||||
shortlog(kIOHIDMaxOutputReportSizeKey)
|
||||
shortlog(kIOHIDMaxFeatureReportSizeKey)
|
||||
shortlog(kIOHIDReportIntervalKey)
|
||||
shortlog(kIOHIDReportDescriptorKey)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *g_window;
|
||||
|
||||
static void DeviceMatching_callback(void* inContext,
|
||||
IOReturn inResult,
|
||||
void *inSender,
|
||||
IOHIDDeviceRef inIOHIDDeviceRef)
|
||||
{
|
||||
NSString *pName = (NSString *)
|
||||
IOHIDDeviceGetProperty(inIOHIDDeviceRef, CFSTR(kIOHIDProductKey));
|
||||
std::string name = (pName != NULL) ? [pName UTF8String] : "Unknown device";
|
||||
|
||||
DeviceDebugPrint(inIOHIDDeviceRef);
|
||||
|
||||
std::vector<Core::Device*> *devices =
|
||||
(std::vector<Core::Device*> *)inContext;
|
||||
|
||||
// Add to the devices vector if it's of a type we want
|
||||
if (IOHIDDeviceConformsTo(inIOHIDDeviceRef,
|
||||
kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard))
|
||||
devices->push_back(new Keyboard(inIOHIDDeviceRef,
|
||||
name, kbd_name_counts[name]++, g_window));
|
||||
#if 0
|
||||
else if (IOHIDDeviceConformsTo(inIOHIDDeviceRef,
|
||||
kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse))
|
||||
devices->push_back(new Mouse(inIOHIDDeviceRef,
|
||||
name, mouse_name_counts[name]++));
|
||||
#endif
|
||||
else
|
||||
devices->push_back(new Joystick(inIOHIDDeviceRef,
|
||||
name, joy_name_counts[name]++));
|
||||
}
|
||||
|
||||
void Init(std::vector<Core::Device*>& devices, void *window)
|
||||
{
|
||||
HIDManager = IOHIDManagerCreate(kCFAllocatorDefault,
|
||||
kIOHIDOptionsTypeNone);
|
||||
if (!HIDManager)
|
||||
NSLog(@"Failed to create HID Manager reference");
|
||||
|
||||
g_window = window;
|
||||
|
||||
IOHIDManagerSetDeviceMatching(HIDManager, NULL);
|
||||
|
||||
// Callbacks for acquisition or loss of a matching device
|
||||
IOHIDManagerRegisterDeviceMatchingCallback(HIDManager,
|
||||
DeviceMatching_callback, (void *)&devices);
|
||||
|
||||
// Match devices that are plugged in right now
|
||||
IOHIDManagerScheduleWithRunLoop(HIDManager,
|
||||
CFRunLoopGetCurrent(), OurRunLoop);
|
||||
if (IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone) !=
|
||||
kIOReturnSuccess)
|
||||
NSLog(@"Failed to open HID Manager");
|
||||
|
||||
kbd_name_counts.clear();
|
||||
joy_name_counts.clear();
|
||||
|
||||
// Wait while current devices are initialized
|
||||
while (CFRunLoopRunInMode(OurRunLoop, 0, TRUE) ==
|
||||
kCFRunLoopRunHandledSource) {};
|
||||
|
||||
// Things should be configured now
|
||||
// Disable hotplugging and other scheduling
|
||||
IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, NULL, NULL);
|
||||
IOHIDManagerUnscheduleFromRunLoop(HIDManager,
|
||||
CFRunLoopGetCurrent(), OurRunLoop);
|
||||
}
|
||||
|
||||
void DeInit()
|
||||
{
|
||||
// This closes all devices as well
|
||||
IOHIDManagerClose(HIDManager, kIOHIDOptionsTypeNone);
|
||||
CFRelease(HIDManager);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace OSX
|
||||
{
|
||||
|
||||
class Joystick : public Core::Device
|
||||
{
|
||||
private:
|
||||
class Button : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Button(IOHIDElementRef element, IOHIDDeviceRef device)
|
||||
: m_element(element), m_device(device) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const IOHIDElementRef m_element;
|
||||
const IOHIDDeviceRef m_device;
|
||||
};
|
||||
|
||||
class Axis : public Input
|
||||
{
|
||||
public:
|
||||
enum direction {
|
||||
positive = 0,
|
||||
negative
|
||||
};
|
||||
std::string GetName() const;
|
||||
Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir);
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const IOHIDElementRef m_element;
|
||||
const IOHIDDeviceRef m_device;
|
||||
std::string m_name;
|
||||
const direction m_direction;
|
||||
float m_neutral;
|
||||
float m_scale;
|
||||
};
|
||||
|
||||
class Hat : public Input
|
||||
{
|
||||
public:
|
||||
enum direction {
|
||||
up = 0,
|
||||
right,
|
||||
down,
|
||||
left
|
||||
};
|
||||
std::string GetName() const;
|
||||
Hat(IOHIDElementRef element, IOHIDDeviceRef device, direction dir);
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const IOHIDElementRef m_element;
|
||||
const IOHIDDeviceRef m_device;
|
||||
const char* m_name;
|
||||
const direction m_direction;
|
||||
};
|
||||
|
||||
public:
|
||||
bool UpdateInput();
|
||||
bool UpdateOutput();
|
||||
|
||||
Joystick(IOHIDDeviceRef device, std::string name, int index);
|
||||
|
||||
std::string GetName() const;
|
||||
std::string GetSource() const;
|
||||
int GetId() const;
|
||||
|
||||
private:
|
||||
const IOHIDDeviceRef m_device;
|
||||
const std::string m_device_name;
|
||||
const int m_index;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
#include <Foundation/Foundation.h>
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
|
||||
#include "OSXJoystick.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace OSX
|
||||
{
|
||||
|
||||
|
||||
Joystick::Joystick(IOHIDDeviceRef device, std::string name, int index)
|
||||
: m_device(device)
|
||||
, m_device_name(name)
|
||||
, m_index(index)
|
||||
{
|
||||
// Buttons
|
||||
NSDictionary *buttonDict =
|
||||
[NSDictionary dictionaryWithObjectsAndKeys:
|
||||
[NSNumber numberWithInteger: kIOHIDElementTypeInput_Button],
|
||||
@kIOHIDElementTypeKey,
|
||||
[NSNumber numberWithInteger: kHIDPage_Button],
|
||||
@kIOHIDElementUsagePageKey,
|
||||
nil];
|
||||
|
||||
CFArrayRef buttons = IOHIDDeviceCopyMatchingElements(m_device,
|
||||
(CFDictionaryRef)buttonDict, kIOHIDOptionsTypeNone);
|
||||
|
||||
if (buttons)
|
||||
{
|
||||
for (int i = 0; i < CFArrayGetCount(buttons); i++)
|
||||
{
|
||||
IOHIDElementRef e =
|
||||
(IOHIDElementRef)CFArrayGetValueAtIndex(buttons, i);
|
||||
//DeviceElementDebugPrint(e, NULL);
|
||||
|
||||
AddInput(new Button(e, m_device));
|
||||
}
|
||||
CFRelease(buttons);
|
||||
}
|
||||
|
||||
// Axes
|
||||
NSDictionary *axisDict =
|
||||
[NSDictionary dictionaryWithObjectsAndKeys:
|
||||
[NSNumber numberWithInteger: kIOHIDElementTypeInput_Misc],
|
||||
@kIOHIDElementTypeKey,
|
||||
nil];
|
||||
|
||||
CFArrayRef axes = IOHIDDeviceCopyMatchingElements(m_device,
|
||||
(CFDictionaryRef)axisDict, kIOHIDOptionsTypeNone);
|
||||
|
||||
if (axes)
|
||||
{
|
||||
for (int i = 0; i < CFArrayGetCount(axes); i++)
|
||||
{
|
||||
IOHIDElementRef e =
|
||||
(IOHIDElementRef)CFArrayGetValueAtIndex(axes, i);
|
||||
//DeviceElementDebugPrint(e, NULL);
|
||||
|
||||
if (IOHIDElementGetUsage(e) == kHIDUsage_GD_Hatswitch) {
|
||||
AddInput(new Hat(e, m_device, Hat::up));
|
||||
AddInput(new Hat(e, m_device, Hat::right));
|
||||
AddInput(new Hat(e, m_device, Hat::down));
|
||||
AddInput(new Hat(e, m_device, Hat::left));
|
||||
} else {
|
||||
AddAnalogInputs(new Axis(e, m_device, Axis::negative),
|
||||
new Axis(e, m_device, Axis::positive));
|
||||
}
|
||||
}
|
||||
CFRelease(axes);
|
||||
}
|
||||
}
|
||||
|
||||
bool Joystick::UpdateInput()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Joystick::UpdateOutput()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Joystick::GetName() const
|
||||
{
|
||||
return m_device_name;
|
||||
}
|
||||
|
||||
std::string Joystick::GetSource() const
|
||||
{
|
||||
return "Input";
|
||||
}
|
||||
|
||||
int Joystick::GetId() const
|
||||
{
|
||||
return m_index;
|
||||
}
|
||||
|
||||
ControlState Joystick::Button::GetState() const
|
||||
{
|
||||
IOHIDValueRef value;
|
||||
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
||||
return IOHIDValueGetIntegerValue(value);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Joystick::Button::GetName() const
|
||||
{
|
||||
std::ostringstream s;
|
||||
s << IOHIDElementGetUsage(m_element);
|
||||
return std::string("Button ") + s.str();
|
||||
}
|
||||
|
||||
Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir)
|
||||
: m_element(element)
|
||||
, m_device(device)
|
||||
, m_direction(dir)
|
||||
{
|
||||
// Need to parse the element a bit first
|
||||
std::string description("unk");
|
||||
|
||||
int const usage = IOHIDElementGetUsage(m_element);
|
||||
switch (usage)
|
||||
{
|
||||
case kHIDUsage_GD_X:
|
||||
description = "X";
|
||||
break;
|
||||
case kHIDUsage_GD_Y:
|
||||
description = "Y";
|
||||
break;
|
||||
case kHIDUsage_GD_Z:
|
||||
description = "Z";
|
||||
break;
|
||||
case kHIDUsage_GD_Rx:
|
||||
description = "Rx";
|
||||
break;
|
||||
case kHIDUsage_GD_Ry:
|
||||
description = "Ry";
|
||||
break;
|
||||
case kHIDUsage_GD_Rz:
|
||||
description = "Rz";
|
||||
break;
|
||||
case kHIDUsage_GD_Wheel:
|
||||
description = "Wheel";
|
||||
break;
|
||||
case kHIDUsage_Csmr_ACPan:
|
||||
description = "Pan";
|
||||
break;
|
||||
default:
|
||||
{
|
||||
std::ostringstream s;
|
||||
s << usage;
|
||||
description = s.str();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_name = std::string("Axis ") + description;
|
||||
m_name.append((m_direction == positive) ? "+" : "-");
|
||||
|
||||
m_neutral = (IOHIDElementGetLogicalMax(m_element) +
|
||||
IOHIDElementGetLogicalMin(m_element)) / 2.;
|
||||
m_scale = 1 / fabs(IOHIDElementGetLogicalMax(m_element) - m_neutral);
|
||||
}
|
||||
|
||||
ControlState Joystick::Axis::GetState() const
|
||||
{
|
||||
IOHIDValueRef value;
|
||||
|
||||
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
||||
{
|
||||
// IOHIDValueGetIntegerValue() crashes when trying
|
||||
// to convert unusually large element values.
|
||||
if (IOHIDValueGetLength(value) > 2)
|
||||
return 0;
|
||||
|
||||
float position = IOHIDValueGetIntegerValue(value);
|
||||
|
||||
if (m_direction == positive && position > m_neutral)
|
||||
return (position - m_neutral) * m_scale;
|
||||
if (m_direction == negative && position < m_neutral)
|
||||
return (m_neutral - position) * m_scale;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Joystick::Axis::GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
Joystick::Hat::Hat(IOHIDElementRef element, IOHIDDeviceRef device, direction dir)
|
||||
: m_element(element)
|
||||
, m_device(device)
|
||||
, m_direction(dir)
|
||||
{
|
||||
switch (dir) {
|
||||
case up:
|
||||
m_name = "Up";
|
||||
break;
|
||||
case right:
|
||||
m_name = "Right";
|
||||
break;
|
||||
case down:
|
||||
m_name = "Down";
|
||||
break;
|
||||
case left:
|
||||
m_name = "Left";
|
||||
break;
|
||||
default:
|
||||
m_name = "unk";
|
||||
}
|
||||
}
|
||||
|
||||
ControlState Joystick::Hat::GetState() const
|
||||
{
|
||||
IOHIDValueRef value;
|
||||
int position;
|
||||
|
||||
if (IOHIDDeviceGetValue(m_device, m_element, &value) == kIOReturnSuccess)
|
||||
{
|
||||
position = IOHIDValueGetIntegerValue(value);
|
||||
|
||||
switch (position) {
|
||||
case 0:
|
||||
if (m_direction == up)
|
||||
return 1;
|
||||
break;
|
||||
case 1:
|
||||
if (m_direction == up || m_direction == right)
|
||||
return 1;
|
||||
break;
|
||||
case 2:
|
||||
if (m_direction == right)
|
||||
return 1;
|
||||
break;
|
||||
case 3:
|
||||
if (m_direction == right || m_direction == down)
|
||||
return 1;
|
||||
break;
|
||||
case 4:
|
||||
if (m_direction == down)
|
||||
return 1;
|
||||
break;
|
||||
case 5:
|
||||
if (m_direction == down || m_direction == left)
|
||||
return 1;
|
||||
break;
|
||||
case 6:
|
||||
if (m_direction == left)
|
||||
return 1;
|
||||
break;
|
||||
case 7:
|
||||
if (m_direction == left || m_direction == up)
|
||||
return 1;
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Joystick::Hat::GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
|
||||
#include "../Device.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace OSX
|
||||
{
|
||||
|
||||
class Keyboard : public Core::Device
|
||||
{
|
||||
private:
|
||||
class Key : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Key(IOHIDElementRef element, IOHIDDeviceRef device);
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const IOHIDElementRef m_element;
|
||||
const IOHIDDeviceRef m_device;
|
||||
std::string m_name;
|
||||
};
|
||||
class Cursor : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
bool IsDetectable() { return false; }
|
||||
Cursor(u8 index, const float& axis, const bool positive) : m_axis(axis), m_index(index), m_positive(positive) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const float& m_axis;
|
||||
const u8 m_index;
|
||||
const bool m_positive;
|
||||
};
|
||||
class Button : public Input
|
||||
{
|
||||
public:
|
||||
std::string GetName() const;
|
||||
Button(u8 index, const unsigned char& button) : m_button(button), m_index(index) {}
|
||||
ControlState GetState() const;
|
||||
private:
|
||||
const unsigned char& m_button;
|
||||
const u8 m_index;
|
||||
};
|
||||
|
||||
public:
|
||||
bool UpdateInput();
|
||||
bool UpdateOutput();
|
||||
|
||||
Keyboard(IOHIDDeviceRef device, std::string name, int index, void *window);
|
||||
|
||||
std::string GetName() const;
|
||||
std::string GetSource() const;
|
||||
int GetId() const;
|
||||
|
||||
private:
|
||||
struct
|
||||
{
|
||||
float x, y;
|
||||
} m_cursor;
|
||||
|
||||
const IOHIDDeviceRef m_device;
|
||||
const std::string m_device_name;
|
||||
int m_index;
|
||||
uint32_t m_windowid;
|
||||
unsigned char m_mousebuttons[3];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user