-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrawinput.cpp
95 lines (85 loc) · 3.16 KB
/
rawinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <Windows.h>
#include <Dbt.h>
#include <hidsdi.h>
#include <hidpi.h>
void printRawInputData(LPARAM lParam)
{
UINT size = 0;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
RAWINPUT* input = (RAWINPUT*)malloc(size);
bool gotInput = GetRawInputData((HRAWINPUT)lParam, RID_INPUT, input, &size, sizeof(RAWINPUTHEADER)) > 0;
if (gotInput)
{
GetRawInputDeviceInfo(input->header.hDevice, RIDI_PREPARSEDDATA, 0, &size);
_HIDP_PREPARSED_DATA* data = (_HIDP_PREPARSED_DATA*)malloc(size);
bool gotPreparsedData = GetRawInputDeviceInfo(input->header.hDevice, RIDI_PREPARSEDDATA, data, &size) > 0;
if (gotPreparsedData)
{
HIDP_CAPS caps;
HidP_GetCaps(data, &caps);
printf("Values: ");
HIDP_VALUE_CAPS* valueCaps = (HIDP_VALUE_CAPS*)malloc(caps.NumberInputValueCaps * sizeof(HIDP_VALUE_CAPS));
HidP_GetValueCaps(HidP_Input, valueCaps, &caps.NumberInputValueCaps, data);
for (USHORT i = 0; i < caps.NumberInputValueCaps; ++i)
{
ULONG value;
HidP_GetUsageValue(HidP_Input, valueCaps[i].UsagePage, 0, valueCaps[i].Range.UsageMin, &value, data, (PCHAR)input->data.hid.bRawData, input->data.hid.dwSizeHid);
printf("%d:%5d ", i, value);
}
free(valueCaps);
printf("Buttons: ");
HIDP_BUTTON_CAPS* buttonCaps = (HIDP_BUTTON_CAPS*)malloc(caps.NumberInputButtonCaps * sizeof(HIDP_BUTTON_CAPS));
HidP_GetButtonCaps(HidP_Input, buttonCaps, &caps.NumberInputButtonCaps, data);
for (USHORT i = 0; i < caps.NumberInputButtonCaps; ++i)
{
ULONG usageCount = buttonCaps->Range.UsageMax - buttonCaps->Range.UsageMin + 1;
USAGE* usages = (USAGE*)malloc(sizeof(USAGE) * usageCount);
HidP_GetUsages(HidP_Input, buttonCaps[i].UsagePage, 0, usages, &usageCount, data, (PCHAR)input->data.hid.bRawData, input->data.hid.dwSizeHid);
for (ULONG usageIndex=0; usageIndex < usageCount; ++usageIndex) {
printf("%d ", usages[usageIndex]);
}
free(usages);
}
free(buttonCaps);
}
free(data);
}
free(input);
printf("\n");
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_INPUT) {
printRawInputData(lParam);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int main()
{
// Create a window, as we need a window precedure to recieve raw input
WNDCLASS wnd = { 0 };
wnd.hInstance = GetModuleHandle(0);
wnd.lpfnWndProc = WindowProcedure;
wnd.lpszClassName = TEXT("Raw input test");
RegisterClass(&wnd);
HWND hwnd = CreateWindow(wnd.lpszClassName, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wnd.hInstance, 0);
// Register devices
RAWINPUTDEVICE deviceList[2];
deviceList[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
deviceList[0].usUsage = HID_USAGE_GENERIC_GAMEPAD;
deviceList[0].dwFlags = RIDEV_INPUTSINK;
deviceList[0].hwndTarget = hwnd;
deviceList[1] = deviceList[0];
deviceList[1].usUsage = HID_USAGE_GENERIC_JOYSTICK;
UINT deviceCount = sizeof(deviceList)/sizeof(*deviceList);
RegisterRawInputDevices(deviceList, deviceCount, sizeof(RAWINPUTDEVICE));
// Message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}