2014-02-14 15:50:00 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by [Dennis Andersen] [2013]
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
#include "..\..\Include\Win32\Win32Input.h"
|
|
|
|
#include "..\..\Include\Keyboard.h"
|
|
|
|
#include <WindowsX.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace Input;
|
|
|
|
using namespace Input::Enum;
|
|
|
|
using namespace Input::Struct;
|
|
|
|
using namespace Input::Typedefs;
|
|
|
|
|
|
|
|
Win32Input *Win32Input::instance = 0;
|
2014-02-24 16:27:43 +01:00
|
|
|
TRACKMOUSEEVENT tme;
|
2014-02-14 15:50:00 +01:00
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
LRESULT Win32Input::RawInputParser(HWND h, LPARAM l)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
LRESULT val = 0;
|
2014-02-14 15:50:00 +01:00
|
|
|
//Get The size of the raw data buffer
|
|
|
|
UINT bufferSize;
|
|
|
|
GetRawInputData((HRAWINPUT)l, RID_INPUT, NULL, &bufferSize, sizeof(RAWINPUTHEADER));
|
|
|
|
if (bufferSize < 1)
|
2014-02-21 11:43:05 +01:00
|
|
|
{ return 0; }
|
2014-02-14 15:50:00 +01:00
|
|
|
|
|
|
|
//Create and read the raw input data
|
|
|
|
LPBYTE rawBufferIn = new BYTE[bufferSize];
|
|
|
|
UINT readBytes = GetRawInputData((HRAWINPUT)l, RID_INPUT, rawBufferIn, &bufferSize, sizeof(RAWINPUTHEADER));
|
|
|
|
if ( readBytes != bufferSize )
|
|
|
|
{
|
|
|
|
delete [] rawBufferIn;
|
2014-02-21 11:43:05 +01:00
|
|
|
return 0;
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RAWINPUT* raw = (RAWINPUT*)rawBufferIn;
|
|
|
|
|
|
|
|
if(!Win32Input::instance->enabled)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(raw->header.dwType == RIM_TYPEMOUSE)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
bool once = false;
|
2014-02-14 15:50:00 +01:00
|
|
|
for (unsigned int i = 0; i < Win32Input::instance->mouse.size(); i++)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
if(Win32Input::instance->mouse[i]->IsActive())
|
|
|
|
{
|
|
|
|
Win32Input::instance->mouse[i]->ProccessMouseData(raw->data.mouse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!once) val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(raw->header.dwType == RIM_TYPEKEYBOARD)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
bool once = false;
|
2014-02-14 15:50:00 +01:00
|
|
|
for (unsigned int i = 0; i < Win32Input::instance->keyboard.size(); i++)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
if(Win32Input::instance->keyboard[i]->IsActive())
|
|
|
|
{
|
|
|
|
Win32Input::instance->keyboard[i]->ProccessKeyboardData(raw->data.keyboard);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!once) val = DefRawInputProc(&raw, 1, sizeof(RAWINPUTHEADER));
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete raw;
|
2014-02-21 11:43:05 +01:00
|
|
|
return val;
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
LRESULT CALLBACK Win32Input::RawWindowCallback(HWND h, UINT m, WPARAM w, LPARAM l)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
LRESULT val = 0;
|
2014-02-26 14:07:46 +01:00
|
|
|
UINT te = 0;
|
|
|
|
if(m != WM_INPUT)
|
|
|
|
te = m;
|
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
switch (m)
|
|
|
|
{
|
|
|
|
case WM_INPUT:
|
2014-02-21 11:43:05 +01:00
|
|
|
return Win32Input::instance->RawInputParser(h, l);
|
|
|
|
break;
|
|
|
|
|
2014-02-26 14:07:46 +01:00
|
|
|
case WM_NCACTIVATE:
|
|
|
|
case WM_ACTIVATEAPP:
|
2014-02-14 15:50:00 +01:00
|
|
|
case WM_ACTIVATE:
|
|
|
|
Win32Input::instance->WindowActivate((w == TRUE));
|
|
|
|
break;
|
2014-02-26 14:07:46 +01:00
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
case WM_CREATE:
|
|
|
|
Win32Input::instance->WindowActivate(true);
|
|
|
|
break;
|
2014-02-26 14:07:46 +01:00
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(h, m, w, l);
|
|
|
|
}
|
|
|
|
void Win32Input::WindowActivate(bool activate)
|
|
|
|
{
|
|
|
|
if(activate)
|
|
|
|
{
|
2014-02-26 14:07:46 +01:00
|
|
|
SetCursor(0);
|
|
|
|
for (unsigned int i = 0; i < Win32Input::instance->mouse.size(); i++)
|
|
|
|
{
|
|
|
|
Win32Input::instance->mouse[i]->ToggleDefault(false);
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-26 14:07:46 +01:00
|
|
|
for (unsigned int i = 0; i < Win32Input::instance->mouse.size(); i++)
|
|
|
|
{
|
|
|
|
Win32Input::instance->mouse[i]->ToggleDefault(true);
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Win32Input::Win32Input()
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
this->targetHwin = 0;
|
2014-02-14 15:50:00 +01:00
|
|
|
if(!this->instance)
|
|
|
|
{
|
|
|
|
this->instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WNDCLASSEXW wc;
|
|
|
|
wc.cbSize = sizeof(WNDCLASSEXW);
|
|
|
|
wc.hIconSm = NULL;
|
|
|
|
wc.style = NULL;
|
|
|
|
wc.lpfnWndProc = RawWindowCallback;
|
|
|
|
wc.cbClsExtra = NULL;
|
|
|
|
wc.cbWndExtra = NULL;
|
|
|
|
wc.hInstance = (HINSTANCE)GetModuleHandle(0);
|
|
|
|
wc.hIcon = NULL;
|
|
|
|
wc.hCursor = NULL;
|
|
|
|
wc.hbrBackground = NULL;
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = L"RawInputCallbackFunc";
|
|
|
|
|
|
|
|
if( !RegisterClassExW(&wc) )
|
|
|
|
{ /*wrong*/ }
|
|
|
|
}
|
|
|
|
Win32Input::~Win32Input()
|
2014-02-25 01:05:37 +01:00
|
|
|
{ Destroy(); }
|
2014-02-14 15:50:00 +01:00
|
|
|
InputObject* Win32Input::CreateDevice(const SAIType inputType, Typedefs::WindowHandle targetApplication)
|
|
|
|
{
|
|
|
|
if(!this->instance->targetHwin)
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
RECT rc;
|
|
|
|
GetClientRect((HWND)targetApplication, &rc);
|
|
|
|
|
|
|
|
AdjustWindowRect(&rc, GetWindowStyle((HWND)targetApplication), FALSE);
|
|
|
|
|
|
|
|
rc.right = rc.right - rc.left;
|
|
|
|
rc.bottom = rc.bottom - rc.top;
|
|
|
|
|
|
|
|
this->targetHwin = CreateWindowExW( 0, L"RawInputCallbackFunc" , NULL, NULL, rc.left, rc.top, rc.right, rc.bottom,
|
|
|
|
(HWND)targetApplication, NULL, (HINSTANCE)GetModuleHandle(0), NULL );
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
InputObject* val = 0;
|
|
|
|
|
|
|
|
switch (inputType)
|
|
|
|
{
|
|
|
|
case SAIType_Keyboard:
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
Win32Keyboard* obj = new Win32Keyboard(this->targetHwin);
|
|
|
|
if(!obj->Create())
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
delete obj;
|
2014-02-14 15:50:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
|
|
|
|
this->keyboard.push_back(obj);
|
|
|
|
val = obj;
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAIType_Mouse:
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
Win32Mouse* obj = new Win32Mouse(this->targetHwin);
|
|
|
|
if(!obj->Create())
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
delete obj;
|
2014-02-14 15:50:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
|
|
|
|
this->mouse.push_back(obj);
|
|
|
|
val = obj;
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
void Win32Input::ToggleInputSystem(bool enable)
|
|
|
|
{
|
|
|
|
this->enabled = enable;
|
2014-02-24 16:01:06 +01:00
|
|
|
|
|
|
|
if(this->enabled)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->mouse.size(); i++)
|
|
|
|
{ this->mouse[i]->Deactivate(); }
|
|
|
|
for (unsigned int i = 0; i < this->keyboard.size(); i++)
|
|
|
|
{ this->keyboard[i]->Deactivate(); }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->mouse.size(); i++)
|
|
|
|
{ this->mouse[i]->Activate(); }
|
|
|
|
for (unsigned int i = 0; i < this->keyboard.size(); i++)
|
|
|
|
{ this->keyboard[i]->Activate(); }
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
void Win32Input::Destroy ()
|
|
|
|
{
|
|
|
|
ShowCursor(true);
|
2014-02-25 01:05:37 +01:00
|
|
|
ClipCursor(0);
|
2014-02-14 15:50:00 +01:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < this->keyboard.size(); i++)
|
|
|
|
{
|
|
|
|
delete this->keyboard[i];
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < this->mouse.size(); i++)
|
|
|
|
{
|
|
|
|
delete this->mouse[i];
|
|
|
|
}
|
|
|
|
this->mouse.resize(0);
|
|
|
|
this->keyboard.resize(0);
|
|
|
|
}
|
|
|
|
|