-
Notifications
You must be signed in to change notification settings - Fork 16
/
winrt.cpp
88 lines (79 loc) · 2.73 KB
/
winrt.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
#include <list>
#include <winrt/Windows.Gaming.Input.h>
#include <winrt/Windows.Foundation.h>
#include <ppl.h>
#include <Windows.h>
struct Joystick
{
bool connected;
winrt::Windows::Gaming::Input::RawGameController rawGameController;
};
int main()
{
winrt::init_apartment();
WNDCLASSA wnd = {0};
wnd.hInstance = GetModuleHandleA(0);
wnd.lpfnWndProc = DefWindowProcA;
wnd.lpszClassName = "Input Focus Window";
wnd.hCursor = LoadCursorA(0, MAKEINTRESOURCEA(32512));
RegisterClassA(&wnd);
HWND hwnd = CreateWindowA(wnd.lpszClassName, wnd.lpszClassName, WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, 0, 0, wnd.hInstance, 0);
std::vector<Joystick> joysticks;
concurrency::critical_section controllerListLock;
winrt::Windows::Gaming::Input::RawGameController::RawGameControllerAdded([&joysticks, &controllerListLock](winrt::Windows::Foundation::IInspectable const&, winrt::Windows::Gaming::Input::RawGameController const& addedController)
{
concurrency::critical_section::scoped_lock {controllerListLock};
for (auto& joystick : joysticks) {
if (!joystick.connected) {
joystick.rawGameController = addedController;
joystick.connected = true;
return;
}
}
joysticks.push_back({true, addedController});
});
winrt::Windows::Gaming::Input::RawGameController::RawGameControllerRemoved([&joysticks, &controllerListLock](winrt::Windows::Foundation::IInspectable const&, winrt::Windows::Gaming::Input::RawGameController const& removedController)
{
concurrency::critical_section::scoped_lock {controllerListLock};
for (auto& joystick : joysticks) {
if (joystick.rawGameController == removedController) {
joystick.connected = false;
return;
}
}
});
while (1) {
Sleep(16);
MSG message;
while (PeekMessageA(&message, 0, 0, 0, true)) {
TranslateMessage(&message);
DispatchMessageA(&message);
}
concurrency::critical_section::scoped_lock {controllerListLock};
for (auto& joystick : joysticks)
{
if (joystick.connected)
{
wprintf(L"Joystick - %s, ", joystick.rawGameController.DisplayName().c_str());
bool buttons[64];
winrt::Windows::Gaming::Input::GameControllerSwitchPosition switches[64];
double axes[64];
joystick.rawGameController.GetCurrentReading(buttons, switches, axes);
printf("Axes - ");
for (int i = 0; i < joystick.rawGameController.AxisCount(); ++i) {
printf("%d:%f ", i, axes[i]);
}
printf("Switches - ");
for (int i = 0; i < joystick.rawGameController.SwitchCount(); ++i) {
printf("%d:%d ", i, switches[i]);
}
printf("Buttons - ");
for (int i = 0; i < joystick.rawGameController.ButtonCount(); ++i) {
if (buttons[i]) printf("%d ", i);
}
puts("");
}
}
}
return 0;
}