| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#ifdef _WIN32 |
| 7 |
|
| 8 |
#include <iterator> |
| 9 |
#include <memory> |
| 10 |
#include <optional> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
#ifndef WIN32_LEAN_AND_MEAN |
| 14 |
#define WIN32_LEAN_AND_MEAN |
| 15 |
#endif |
| 16 |
#ifndef NOMINMAX |
| 17 |
#define NOMINMAX |
| 18 |
#endif |
| 19 |
#include <Windows.h> |
| 20 |
|
| 21 |
#include <SetupAPI.h> |
| 22 |
#include <cfgmgr32.h> |
| 23 |
#include <devpropdef.h> |
| 24 |
|
| 25 |
#include "Common/Functional.h" |
| 26 |
|
| 27 |
namespace Common |
| 28 |
{ |
| 29 |
std::optional<std::wstring> GetDevNodeStringProperty(DEVINST device, |
| 30 |
const DEVPROPKEY* requested_property); |
| 31 |
|
| 32 |
std::optional<std::wstring> GetDeviceInterfaceStringProperty(LPCWSTR iface, |
| 33 |
const DEVPROPKEY* requested_property); |
| 34 |
|
| 35 |
|
| 36 |
template <typename T> |
| 37 |
struct NullTerminatedStringList |
| 38 |
{ |
| 39 |
class Iterator |
| 40 |
{ |
| 41 |
friend NullTerminatedStringList; |
| 42 |
|
| 43 |
public: |
| 44 |
constexpr T* operator*() { return m_ptr; } |
| 45 |
|
| 46 |
constexpr Iterator& operator++() |
| 47 |
{ |
| 48 |
m_ptr += std::basic_string_view(m_ptr).size() + 1; |
| 49 |
return *this; |
| 50 |
} |
| 51 |
|
| 52 |
constexpr Iterator operator++(int) |
| 53 |
{ |
| 54 |
const auto result{*this}; |
| 55 |
++*this; |
| 56 |
return result; |
| 57 |
} |
| 58 |
|
| 59 |
constexpr bool operator==(std::default_sentinel_t) const { return *m_ptr == T{}; } |
| 60 |
|
| 61 |
private: |
| 62 |
constexpr Iterator(T* ptr) : m_ptr{ptr} {} |
| 63 |
|
| 64 |
T* m_ptr; |
| 65 |
}; |
| 66 |
|
| 67 |
constexpr auto begin() const { return Iterator{list.get()}; } |
| 68 |
constexpr auto end() const { return std::default_sentinel; } |
| 69 |
|
| 70 |
std::unique_ptr<T[]> list; |
| 71 |
}; |
| 72 |
|
| 73 |
NullTerminatedStringList<WCHAR> GetDeviceInterfaceList(LPGUID iface_class_guid, DEVINSTID device_id, |
| 74 |
ULONG flags); |
| 75 |
|
| 76 |
class DeviceChangeNotification |
| 77 |
{ |
| 78 |
public: |
| 79 |
enum class EventType : bool |
| 80 |
{ |
| 81 |
Arrival, |
| 82 |
Removal, |
| 83 |
}; |
| 84 |
using CallbackType = Common::MoveOnlyFunction<void(EventType)>; |
| 85 |
|
| 86 |
DeviceChangeNotification(); |
| 87 |
~DeviceChangeNotification(); |
| 88 |
|
| 89 |
|
| 90 |
void Register(CallbackType callback); |
| 91 |
void Unregister(); |
| 92 |
|
| 93 |
DeviceChangeNotification(const DeviceChangeNotification&) = delete; |
| 94 |
DeviceChangeNotification(DeviceChangeNotification&&) = delete; |
| 95 |
void operator=(const DeviceChangeNotification&) = delete; |
| 96 |
void operator=(DeviceChangeNotification&&) = delete; |
| 97 |
|
| 98 |
private: |
| 99 |
CallbackType m_callback{}; |
| 100 |
HCMNOTIFICATION m_notify_handle{nullptr}; |
| 101 |
}; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
#endif |