Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/Win32/Win32.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/ControllerInterface/Win32/Win32.h"
5
6 #include <Windows.h>
7 #include <cfgmgr32.h>
8 // must be before hidclass
9 #include <initguid.h>
10
11 #include <hidclass.h>
12
13 #include "Common/Flag.h"
14 #include "Common/Logging/Log.h"
15 #include "InputCommon/ControllerInterface/DInput/DInput.h"
16 #include "InputCommon/ControllerInterface/WGInput/WGInput.h"
17 #include "InputCommon/ControllerInterface/XInput/XInput.h"
18
19 #pragma comment(lib, "OneCoreUAP.Lib")
20
21 // TODO is this really needed?
22 static Common::Flag s_first_populate_devices_asked;
23 static HCMNOTIFICATION s_notify_handle;
24
25 namespace ciface::Win32
26 {
27 class InputBackend final : public ciface::InputBackend
28 {
29 public:
30 InputBackend(ControllerInterface* controller_interface);
31 ~InputBackend() override;
32
33 void PopulateDevices() override;
34 void HandleWindowChange() override;
35 HWND GetHWND();
36 };
37 } // namespace ciface::Win32
38
39 _Pre_satisfies_(EventDataSize >= sizeof(CM_NOTIFY_EVENT_DATA)) static DWORD CALLBACK
40 OnDevicesChanged(_In_ HCMNOTIFICATION hNotify, _In_opt_ PVOID Context,
41 _In_ CM_NOTIFY_ACTION Action,
42 _In_reads_bytes_(EventDataSize) PCM_NOTIFY_EVENT_DATA EventData,
43 _In_ DWORD EventDataSize)
44 {
45 if (Action == CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL ||
46 Action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL)
47 {
48 // Windows automatically sends this message before we ask for it and before we are "ready" to
49 // listen for it.
50 if (s_first_populate_devices_asked.IsSet())
51 {
52 // TODO: we could easily use the message passed alongside this event, which tells
53 // whether a device was added or removed, to avoid removing old, still connected, devices
54 g_controller_interface.PlatformPopulateDevices([&] {
55 ciface::DInput::PopulateDevices(
56 static_cast<ciface::Win32::InputBackend*>(Context)->GetHWND());
57 ciface::XInput::PopulateDevices();
58 });
59 }
60 }
61 return ERROR_SUCCESS;
62 }
63
64 namespace ciface::Win32
65 {
66 std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)
67 {
68 return std::make_unique<InputBackend>(controller_interface);
69 }
70
71 HWND InputBackend::GetHWND()
72 {
73 return static_cast<HWND>(GetControllerInterface().GetWindowSystemInfo().render_window);
74 }
75
76 InputBackend::InputBackend(ControllerInterface* controller_interface)
77 : ciface::InputBackend(controller_interface)
78 {
79 XInput::Init();
80 WGInput::Init();
81
82 CM_NOTIFY_FILTER notify_filter{.cbSize = sizeof(notify_filter),
83 .FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE,
84 .u{.DeviceInterface{.ClassGuid = GUID_DEVINTERFACE_HID}}};
85 const CONFIGRET cfg_rv =
86 CM_Register_Notification(&notify_filter, this, OnDevicesChanged, &s_notify_handle);
87 if (cfg_rv != CR_SUCCESS)
88 {
89 ERROR_LOG_FMT(CONTROLLERINTERFACE, "CM_Register_Notification failed: {:x}", cfg_rv);
90 }
91 }
92
93 void InputBackend::PopulateDevices()
94 {
95 g_controller_interface.PlatformPopulateDevices([this] {
96 s_first_populate_devices_asked.Set();
97 ciface::DInput::PopulateDevices(GetHWND());
98 ciface::XInput::PopulateDevices();
99 ciface::WGInput::PopulateDevices();
100 });
101 }
102
103 void InputBackend::HandleWindowChange()
104 {
105 g_controller_interface.PlatformPopulateDevices(
106 [this] { ciface::DInput::ChangeWindow(GetHWND()); });
107 }
108
109 InputBackend::~InputBackend()
110 {
111 s_first_populate_devices_asked.Clear();
112 DInput::DeInit();
113
114 if (s_notify_handle)
115 {
116 const CONFIGRET cfg_rv = CM_Unregister_Notification(s_notify_handle);
117 if (cfg_rv != CR_SUCCESS)
118 {
119 ERROR_LOG_FMT(CONTROLLERINTERFACE, "CM_Unregister_Notification failed: {:x}", cfg_rv);
120 }
121 s_notify_handle = nullptr;
122 }
123
124 XInput::DeInit();
125 WGInput::DeInit();
126 }
127
128 } // namespace ciface::Win32