2013-11-07 10:56:31 +01:00
|
|
|
#include "RawInput_Impl.h"
|
2013-11-06 22:52:00 +01:00
|
|
|
#include <WindowsX.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
static RawInput_Impl* gInstance = 0;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
template<typename FNC, typename PARAM> void DESTROY_LIST(SubscribeList<FNC, PARAM>* l)
|
|
|
|
{
|
|
|
|
SubscribeList<FNC, PARAM>* w = l;
|
|
|
|
SubscribeList<FNC, PARAM>* p = 0;
|
|
|
|
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
p = w;
|
|
|
|
w = w->next;
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename FNC, typename PARAM> void PROCESS_SUBSCRIBERS(SubscribeList<FNC, PARAM>* l)
|
|
|
|
{
|
|
|
|
while (l)
|
|
|
|
{
|
|
|
|
l->fnc(l->param);
|
|
|
|
l = l->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
RawInput* RawInput::Self()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-07 10:56:31 +01:00
|
|
|
return (RawInput*)RawInput_Impl::Self();
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
void RawInput::Destroy()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
RawInput::Destroy();
|
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
RawInput_Impl* RawInput_Impl::Self()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
if(!gInstance)
|
2013-11-07 10:56:31 +01:00
|
|
|
gInstance = new RawInput_Impl();
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
return gInstance;
|
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
void RawInput_Impl::Destroy ()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
/************************ Delete subscribers ****************************/
|
2013-11-07 10:56:31 +01:00
|
|
|
DESTROY_LIST(RawInput_Impl::Self()->_procInput);
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
/************************ Delete Other stuff ****************************/
|
|
|
|
ShowCursor(true);
|
|
|
|
RECT r;
|
|
|
|
GetWindowRect(GetDesktopWindow(), &r);
|
|
|
|
ClipCursor(&r);
|
|
|
|
|
|
|
|
/************************ Delete instance ****************************/
|
|
|
|
delete gInstance;
|
|
|
|
gInstance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
RawInput_Impl::RawInput_Impl ()
|
2013-11-06 22:52:00 +01:00
|
|
|
: _procInput(0)
|
|
|
|
, _enabled(1)
|
|
|
|
, _mouseEnabled(1)
|
|
|
|
, _KeyboardEnabled(1)
|
|
|
|
, _exclusive(0)
|
|
|
|
, _errorMsg(0)
|
|
|
|
, _msgHook(SetWindowsHookEx(WH_GETMESSAGE, WM_INPUT_TRANSLATE, (HINSTANCE)0, GetCurrentThreadId()))
|
|
|
|
{
|
|
|
|
if(!_msgHook) this->_errorMsg = L"Failed to initiate window message hook";
|
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
RawInput_Impl::~RawInput_Impl ()
|
2013-11-06 22:52:00 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
const wchar_t* RawInput_Impl::Input_GetError() const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
return this->_errorMsg;
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
bool RawInput_Impl::Input_AddDevice(IN const HWND& targetApplication)
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
assert(targetApplication != 0);
|
|
|
|
static const UINT c = 2;
|
|
|
|
RAWINPUTDEVICE devices[c] =
|
|
|
|
{
|
|
|
|
{ 0x01, RawInput_Usage_keyboard, RIDEV_NOLEGACY, targetApplication },
|
|
|
|
{ 0x01, RawInput_Usage_mouse, RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE, targetApplication }
|
|
|
|
};
|
|
|
|
|
|
|
|
if(! _addDevice( devices , c ) ) return false;
|
|
|
|
|
|
|
|
ShowCursor(FALSE);
|
|
|
|
//RECT r;
|
|
|
|
//GetWindow
|
|
|
|
//GetWindowRect(
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
bool RawInput_Impl::Input_AddDevice(IN const RAWINPUTDEVICE* d, const int& count)
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
if(!d[i].hwndTarget)
|
|
|
|
{
|
|
|
|
this->_errorMsg = L"Must specify target application";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(! _addDevice( d, count ) ) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//RAWINPUTDEVICE d = { 0x01, type, RIDEV_REMOVE, NULL };
|
|
|
|
//this->_errorMsg = L"Failed to unregister device";
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
void RawInput_Impl::Input_Disable()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
this->_enabled = false;
|
|
|
|
}
|
2013-11-07 10:56:31 +01:00
|
|
|
void RawInput_Impl::Input_Enable()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
this->_enabled = true;
|
|
|
|
}
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
void RawInput_Impl::Input_Read()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
//for (int i = 0; i < this->_idleKeyData.size(); i++)
|
|
|
|
// this->_proccessRawKeyboardData(this->_idleKeyData.pop());
|
|
|
|
//for (int i = 0; i < this->_idleMouseData.size(); i++)
|
|
|
|
// this->_proccessRawMouseData(this->_idleMouseData.pop());
|
|
|
|
//
|
|
|
|
//this->_idleKeyData.clear();
|
|
|
|
//this->_idleMouseData.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-07 10:56:31 +01:00
|
|
|
bool RawInput_Impl::_addDevice (const RAWINPUTDEVICE* k, const int& count)
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
if(RegisterRawInputDevices(k, count, sizeof(RAWINPUTDEVICE)) == FALSE)
|
|
|
|
{
|
|
|
|
DWORD h = GetLastError();
|
|
|
|
this->_errorMsg = L"Failed to register device";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int q = 0; q < count; q++)
|
|
|
|
{
|
|
|
|
RawInputDeviceInstance i;
|
|
|
|
memcpy(&i.description, &k[q], sizeof(RAWINPUTDEVICE));
|
|
|
|
this->_deviceList.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|