windows: Move rawinput stuff into its own file.

This commit is contained in:
Unknown W. Brackets 2014-01-19 11:59:11 -08:00
parent 5508e62ed6
commit 8af9f62f6b
8 changed files with 204 additions and 114 deletions

View file

@ -4,10 +4,8 @@
#include "util/const_map.h"
#include "KeyMap.h"
#include "ControlMapping.h"
#include "Windows/WndMainWindow.h"
#include "KeyboardDevice.h"
#include "../Common/CommonTypes.h"
#include "../Core/HLE/sceCtrl.h"
#include "WinUser.h"

View file

@ -4,6 +4,8 @@
#include "base/mutex.h"
#include "InputDevice.h"
extern std::map<int, int> windowsTransTable;
class KeyboardDevice : public InputDevice {
public:
virtual int UpdateState(InputState &input_state);

View file

@ -293,6 +293,7 @@
<ClCompile Include="GEDebugger\VertexPreview.cpp" />
<ClCompile Include="InputDevice.cpp" />
<ClCompile Include="KeyboardDevice.cpp" />
<ClCompile Include="RawInput.cpp" />
<ClCompile Include="W32Util\DialogManager.cpp" />
<ClCompile Include="W32Util\Misc.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)%(Filename)2.obj</ObjectFileName>
@ -342,6 +343,7 @@
<ClInclude Include="GEDebugger\TabState.h" />
<ClInclude Include="InputDevice.h" />
<ClInclude Include="KeyboardDevice.h" />
<ClInclude Include="RawInput.h" />
<ClInclude Include="W32Util\DialogManager.h" />
<ClInclude Include="W32Util\Misc.h" />
<ClInclude Include="W32Util\PropertySheet.h" />

View file

@ -137,6 +137,9 @@
<ClCompile Include="GEDebugger\VertexPreview.cpp">
<Filter>Windows\GE Debugger</Filter>
</ClCompile>
<ClCompile Include="RawInput.cpp">
<Filter>Windows\Input</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Debugger\CtrlDisAsmView.h">
@ -248,6 +251,9 @@
<ClInclude Include="..\ios\ViewController.h">
<Filter>Other Platforms</Filter>
</ClInclude>
<ClInclude Include="RawInput.h">
<Filter>Windows\Input</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="icon1.ico">

166
Windows/RawInput.cpp Normal file
View file

@ -0,0 +1,166 @@
// Copyright (c) 2014- PPSSPP 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 or later versions.
// 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 git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <set>
#include <algorithm>
#include "base/NativeApp.h"
#include "input/input_state.h"
#include "Windows/RawInput.h"
#include "Windows/KeyboardDevice.h"
#include "Windows/WindowsHost.h"
#include "Core/Config.h"
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_POINTER
#define HID_USAGE_GENERIC_POINTER ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
#ifndef HID_USAGE_GENERIC_JOYSTICK
#define HID_USAGE_GENERIC_JOYSTICK ((USHORT) 0x04)
#endif
#ifndef HID_USAGE_GENERIC_GAMEPAD
#define HID_USAGE_GENERIC_GAMEPAD ((USHORT) 0x05)
#endif
#ifndef HID_USAGE_GENERIC_KEYBOARD
#define HID_USAGE_GENERIC_KEYBOARD ((USHORT) 0x06)
#endif
#ifndef HID_USAGE_GENERIC_KEYPAD
#define HID_USAGE_GENERIC_KEYPAD ((USHORT) 0x07)
#endif
#ifndef HID_USAGE_GENERIC_MULTIAXIS
#define HID_USAGE_GENERIC_MULTIAXIS ((USHORT) 0x07)
#endif
namespace WindowsRawInput {
static std::set<int> keyboardKeysDown;
static void *rawInputBuffer;
static size_t rawInputBufferSize;
void Init() {
RAWINPUTDEVICE dev[2];
memset(dev, 0, sizeof(dev));
dev[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
dev[0].usUsage = HID_USAGE_GENERIC_KEYBOARD;
dev[0].dwFlags = g_Config.bIgnoreWindowsKey ? RIDEV_NOHOTKEYS : 0;
dev[1].usUsagePage = HID_USAGE_PAGE_GENERIC;
dev[1].usUsage = HID_USAGE_GENERIC_MOUSE;
dev[1].dwFlags = 0;
RegisterRawInputDevices(dev, 2, sizeof(RAWINPUTDEVICE));
}
static int GetTrueVKey(const RAWKEYBOARD &kb) {
switch (kb.VKey) {
case VK_SHIFT:
return MapVirtualKey(kb.MakeCode, MAPVK_VSC_TO_VK_EX);
case VK_CONTROL:
if (kb.Flags & RI_KEY_E0)
return VK_RCONTROL;
else
return VK_LCONTROL;
case VK_MENU:
if (kb.Flags & RI_KEY_E0)
return VK_RMENU; // Right Alt / AltGr
else
return VK_LMENU; // Left Alt
default:
return kb.VKey;
}
}
LRESULT Process(WPARAM wParam, LPARAM lParam) {
UINT dwSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
if (!rawInputBuffer) {
rawInputBuffer = malloc(dwSize);
rawInputBufferSize = dwSize;
}
if (dwSize > rawInputBufferSize) {
rawInputBuffer = realloc(rawInputBuffer, dwSize);
}
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawInputBuffer, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)rawInputBuffer;
if (raw->header.dwType == RIM_TYPEKEYBOARD) {
KeyInput key;
key.deviceId = DEVICE_ID_KEYBOARD;
if (raw->data.keyboard.Message == WM_KEYDOWN || raw->data.keyboard.Message == WM_SYSKEYDOWN) {
key.flags = KEY_DOWN;
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) {
NativeKey(key);
keyboardKeysDown.insert(key.keyCode);
}
} else if (raw->data.keyboard.Message == WM_KEYUP) {
key.flags = KEY_UP;
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) {
NativeKey(key);
auto keyDown = std::find(keyboardKeysDown.begin(), keyboardKeysDown.end(), key.keyCode);
if (keyDown != keyboardKeysDown.end())
keyboardKeysDown.erase(keyDown);
}
}
} else if (raw->header.dwType == RIM_TYPEMOUSE) {
mouseDeltaX += raw->data.mouse.lLastX;
mouseDeltaY += raw->data.mouse.lLastY;
KeyInput key;
key.deviceId = DEVICE_ID_MOUSE;
int mouseRightBtnPressed = raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN;
int mouseRightBtnReleased = raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP;
if(mouseRightBtnPressed) {
key.flags = KEY_DOWN;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeKey(key);
}
else if(mouseRightBtnReleased) {
key.flags = KEY_UP;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeKey(key);
}
// TODO : Smooth and translate to an axis every frame.
// NativeAxis()
}
return 0;
}
void LoseFocus() {
// Force-release all held keys on the keyboard to prevent annoying stray inputs.
KeyInput key;
key.deviceId = DEVICE_ID_KEYBOARD;
key.flags = KEY_UP;
for (auto i = keyboardKeysDown.begin(); i != keyboardKeysDown.end(); ++i) {
key.keyCode = *i;
NativeKey(key);
}
}
};

24
Windows/RawInput.h Normal file
View file

@ -0,0 +1,24 @@
// Copyright (c) 2014- PPSSPP 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 or later versions.
// 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 git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/CommonWindows.h"
namespace WindowsRawInput {
void Init();
LRESULT Process(WPARAM wParam, LPARAM lParam);
void LoseFocus();
};

View file

@ -61,6 +61,7 @@
#include "Windows/W32Util/DialogManager.h"
#include "Windows/W32Util/ShellUtil.h"
#include "Windows/W32Util/Misc.h"
#include "Windows/RawInput.h"
#include "GPU/GPUInterface.h"
#include "GPU/GPUState.h"
#include "GPU/GLES/TextureScaler.h"
@ -103,20 +104,12 @@ static RECT g_normalRC = {0};
static std::wstring windowTitle;
extern bool g_TakeScreenshot;
extern InputState input_state;
static std::set<int> keyboardKeysDown;
#define TIMER_CURSORUPDATE 1
#define TIMER_CURSORMOVEUPDATE 2
#define CURSORUPDATE_INTERVAL_MS 1000
#define CURSORUPDATE_MOVE_TIMESPAN_MS 500
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
namespace MainWindow
{
HWND hwndMain;
@ -130,8 +123,6 @@ namespace MainWindow
static int prevCursorY = -1;
static bool mouseButtonDown = false;
static bool hideCursor = false;
static void *rawInputBuffer;
static size_t rawInputBufferSize;
static std::map<int, std::string> initialMenuKeys;
static std::vector<std::string> countryCodes;
static std::vector<std::string> availableShaders;
@ -799,17 +790,7 @@ namespace MainWindow
RegisterTouchWindow(hwndDisplay, TWF_WANTPALM);
#endif
RAWINPUTDEVICE dev[2];
memset(dev, 0, sizeof(dev));
dev[0].usUsagePage = 1;
dev[0].usUsage = 6;
dev[0].dwFlags = g_Config.bIgnoreWindowsKey ? RIDEV_NOHOTKEYS : 0;
dev[1].usUsagePage = HID_USAGE_PAGE_GENERIC;
dev[1].usUsage = HID_USAGE_GENERIC_MOUSE;
dev[1].dwFlags = 0;
RegisterRawInputDevices(dev, 2, sizeof(RAWINPUTDEVICE));
WindowsRawInput::Init();
SetFocus(hwndMain);
return TRUE;
@ -1036,28 +1017,6 @@ namespace MainWindow
}
return 0;
}
static int GetTrueVKey(const RAWKEYBOARD &kb) {
switch (kb.VKey) {
case VK_SHIFT:
return MapVirtualKey(kb.MakeCode, MAPVK_VSC_TO_VK_EX);
case VK_CONTROL:
if (kb.Flags & RI_KEY_E0)
return VK_RCONTROL;
else
return VK_LCONTROL;
case VK_MENU:
if (kb.Flags & RI_KEY_E0)
return VK_RMENU; // Right Alt / AltGr
else
return VK_LMENU; // Left Alt
default:
return kb.VKey;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int wmId, wmEvent;
@ -1085,14 +1044,7 @@ namespace MainWindow
}
if (wParam == WA_INACTIVE) {
// Force-release all held keys on the keyboard to prevent annoying stray inputs.
KeyInput key;
key.deviceId = DEVICE_ID_KEYBOARD;
key.flags = KEY_UP;
for (auto i = keyboardKeysDown.begin(); i != keyboardKeysDown.end(); ++i) {
key.keyCode = *i;
NativeKey(key);
}
WindowsRawInput::LoseFocus();
}
}
break;
@ -1529,67 +1481,7 @@ namespace MainWindow
break;
case WM_INPUT:
{
UINT dwSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
if (!rawInputBuffer) {
rawInputBuffer = malloc(dwSize);
rawInputBufferSize = dwSize;
}
if (dwSize > rawInputBufferSize) {
rawInputBuffer = realloc(rawInputBuffer, dwSize);
}
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawInputBuffer, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)rawInputBuffer;
if (raw->header.dwType == RIM_TYPEKEYBOARD) {
KeyInput key;
key.deviceId = DEVICE_ID_KEYBOARD;
if (raw->data.keyboard.Message == WM_KEYDOWN || raw->data.keyboard.Message == WM_SYSKEYDOWN) {
key.flags = KEY_DOWN;
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) {
NativeKey(key);
keyboardKeysDown.insert(key.keyCode);
}
} else if (raw->data.keyboard.Message == WM_KEYUP) {
key.flags = KEY_UP;
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) {
NativeKey(key);
auto keyDown = std::find(keyboardKeysDown.begin(), keyboardKeysDown.end(), key.keyCode);
if (keyDown != keyboardKeysDown.end())
keyboardKeysDown.erase(keyDown);
}
}
} else if (raw->header.dwType == RIM_TYPEMOUSE) {
mouseDeltaX += raw->data.mouse.lLastX;
mouseDeltaY += raw->data.mouse.lLastY;
KeyInput key;
key.deviceId = DEVICE_ID_MOUSE;
int mouseRightBtnPressed = raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN;
int mouseRightBtnReleased = raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP;
if(mouseRightBtnPressed) {
key.flags = KEY_DOWN;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeKey(key);
}
else if(mouseRightBtnReleased) {
key.flags = KEY_UP;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeKey(key);
}
// TODO : Smooth and translate to an axis every frame.
// NativeAxis()
}
}
return 0;
return WindowsRawInput::Process(wParam, lParam);
case WM_VERYSLEEPY_MSG:
switch (wParam) {