| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef _WIN32 |
| 5 | |
| 6 | #include "Common/WindowsDevice.h" |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "Hidclass.h" |
| 11 | |
| 12 | #include "Common/CommonFuncs.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | |
| 15 | namespace Common |
| 16 | { |
| 17 | std::optional<std::wstring> GetPropertyHelper(auto function, auto dev, |
| 18 | const DEVPROPKEY* requested_property, |
| 19 | DEVPROPTYPE expected_type) |
| 20 | { |
| 21 | DEVPROPTYPE type{}; |
| 22 | ULONG buffer_size{}; |
| 23 | |
| 24 | if (const auto result = function(dev, requested_property, &type, nullptr, &buffer_size, 0); |
| 25 | result != CR_SUCCESS && result != CR_BUFFER_SMALL) |
| 26 | { |
| 27 | if (result != CR_NO_SUCH_VALUE) |
| 28 | WARN_LOG_FMT(COMMON, "CM_Get_DevNode_Property returned: {}", result); |
| 29 | return std::nullopt; |
| 30 | } |
| 31 | if (type != expected_type) |
| 32 | { |
| 33 | WARN_LOG_FMT(COMMON, "CM_Get_DevNode_Property unexpected type: 0x{:x}", type); |
| 34 | return std::nullopt; |
| 35 | } |
| 36 | |
| 37 | std::optional<std::wstring> property; |
| 38 | // FYI: It's legal to write the null terminator at data()[size()] of std::basic_string. |
| 39 | property.emplace(buffer_size / sizeof(WCHAR) - 1, L'\0'); |
| 40 | if (const auto result = function(dev, requested_property, &type, |
| 41 | reinterpret_cast<BYTE*>(property->data()), &buffer_size, 0); |
| 42 | result != CR_SUCCESS) |
| 43 | { |
| 44 | ERROR_LOG_FMT(COMMON, "CM_Get_DevNode_Property returned: {}", result); |
| 45 | return std::nullopt; |
| 46 | } |
| 47 | return property; |
| 48 | } |
| 49 | |
| 50 | std::optional<std::wstring> GetDevNodeStringProperty(DEVINST dev, |
| 51 | const DEVPROPKEY* requested_property) |
| 52 | { |
| 53 | return GetPropertyHelper(CM_Get_DevNode_Property, dev, requested_property, DEVPROP_TYPE_STRING); |
| 54 | } |
| 55 | |
| 56 | std::optional<std::wstring> GetDeviceInterfaceStringProperty(LPCWSTR iface, |
| 57 | const DEVPROPKEY* requested_property) |
| 58 | { |
| 59 | return GetPropertyHelper(CM_Get_Device_Interface_Property, iface, requested_property, |
| 60 | DEVPROP_TYPE_STRING); |
| 61 | } |
| 62 | |
| 63 | NullTerminatedStringList<WCHAR> GetDeviceInterfaceList(LPGUID iface_class_guid, DEVINSTID device_id, |
| 64 | ULONG flags) |
| 65 | { |
| 66 | while (true) |
| 67 | { |
| 68 | ULONG list_size = 0; |
| 69 | const auto size_result = |
| 70 | CM_Get_Device_Interface_List_Size(&list_size, iface_class_guid, device_id, flags); |
| 71 | if (size_result != CR_SUCCESS || list_size == 0) |
| 72 | list_size = 1; |
| 73 | |
| 74 | auto buffer = std::make_unique_for_overwrite<WCHAR[]>(list_size); |
| 75 | const auto list_result = |
| 76 | CM_Get_Device_Interface_List(iface_class_guid, device_id, buffer.get(), list_size, flags); |
| 77 | |
| 78 | // "A new device can be added to the system causing the size returned to no longer be valid." |
| 79 | // Microsoft recommends trying again in a loop. |
| 80 | if (list_result == CR_BUFFER_SMALL) |
| 81 | continue; |
| 82 | |
| 83 | if (list_result != CR_SUCCESS) |
| 84 | { |
| 85 | ERROR_LOG_FMT(COMMON, "CM_Get_Device_Interface_List: {}", list_result); |
| 86 | buffer[0] = 0; |
| 87 | } |
| 88 | |
| 89 | return {std::move(buffer)}; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static __callback DWORD OnDevicesChanged(_In_ HCMNOTIFICATION notify_handle, _In_opt_ PVOID context, |
| 94 | _In_ CM_NOTIFY_ACTION action, |
| 95 | _In_reads_bytes_(event_data_size) |
| 96 | PCM_NOTIFY_EVENT_DATA event_data, |
| 97 | _In_ DWORD event_data_size) |
| 98 | { |
| 99 | auto& callback = *static_cast<DeviceChangeNotification::CallbackType*>(context); |
| 100 | switch (action) |
| 101 | { |
| 102 | case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL: |
| 103 | callback(DeviceChangeNotification::EventType::Arrival); |
| 104 | break; |
| 105 | case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL: |
| 106 | callback(DeviceChangeNotification::EventType::Removal); |
| 107 | break; |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | return ERROR_SUCCESS; |
| 112 | } |
| 113 | |
| 114 | DeviceChangeNotification::DeviceChangeNotification() = default; |
| 115 | |
| 116 | DeviceChangeNotification::~DeviceChangeNotification() |
| 117 | { |
| 118 | Unregister(); |
| 119 | } |
| 120 | |
| 121 | void DeviceChangeNotification::Register(CallbackType callback) |
| 122 | { |
| 123 | Unregister(); |
| 124 | m_callback = std::move(callback); |
| 125 | |
| 126 | CM_NOTIFY_FILTER notify_filter{ |
| 127 | .cbSize = sizeof(notify_filter), |
| 128 | .FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, |
| 129 | .u{.DeviceInterface{.ClassGuid = GUID_DEVINTERFACE_HID}}, |
| 130 | }; |
| 131 | const CONFIGRET cfg_rv = |
| 132 | CM_Register_Notification(¬ify_filter, &m_callback, OnDevicesChanged, &m_notify_handle); |
| 133 | if (cfg_rv != CR_SUCCESS) |
| 134 | { |
| 135 | ERROR_LOG_FMT(COMMON, "CM_Register_Notification failed: {:x}", cfg_rv); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void DeviceChangeNotification::Unregister() |
| 140 | { |
| 141 | if (m_notify_handle == nullptr) |
| 142 | return; |
| 143 | |
| 144 | const CONFIGRET cfg_rv = CM_Unregister_Notification(m_notify_handle); |
| 145 | if (cfg_rv != CR_SUCCESS) |
| 146 | { |
| 147 | ERROR_LOG_FMT(COMMON, "CM_Unregister_Notification failed: {:x}", cfg_rv); |
| 148 | } |
| 149 | m_notify_handle = nullptr; |
| 150 | } |
| 151 | |
| 152 | } // namespace Common |
| 153 | |
| 154 | #endif |