Skip to content

Commit 2329208

Browse files
refactor(Windows): mark protocol headers C++-only
Update the shared Windows protocol headers to explicitly be C++-only and remove legacy C compatibility paths. This drops the `#else` constant/enums and `extern "C"` wrappers, leaving a single C++ definition path with `inline constexpr`, scoped enums, and the existing packed protocol structs.
1 parent 605bf93 commit 2329208

15 files changed

Lines changed: 212 additions & 310 deletions

src/platform/windows/broker/broker_request_validation.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
// standard includes
1111
#include <algorithm>
12+
#include <array>
1213
#include <cstddef>
1314
#include <cstdint>
1415
#include <iterator>
@@ -29,14 +30,14 @@ namespace lvh::windows::broker_validation {
2930
LVH_WINDOWS_GAMEPAD_FLAG_SUPPORTS_ADAPTIVE_TRIGGERS;
3031

3132
template<typename Value, std::size_t Size>
32-
bool all_zero(const Value (&values)[Size]) {
33+
bool all_zero(const std::array<Value, Size> &values) {
3334
return std::ranges::all_of(values, [](const auto value) {
3435
return value == Value {};
3536
});
3637
}
3738

3839
template<std::size_t Size>
39-
bool valid_c_string(const char (&value)[Size], bool allow_empty = true) {
40+
bool valid_c_string(const std::array<char, Size> &value, bool allow_empty = true) {
4041
const auto terminator = std::ranges::find(value, '\0');
4142
if (terminator == std::end(value) || (!allow_empty && terminator == std::begin(value))) {
4243
return false;
@@ -48,7 +49,7 @@ namespace lvh::windows::broker_validation {
4849
}
4950

5051
template<std::size_t Size>
51-
bool valid_sized_c_string(const char (&value)[Size], std::uint32_t size) {
52+
bool valid_sized_c_string(const std::array<char, Size> &value, std::uint32_t size) {
5253
if (size >= Size || value[size] != '\0') {
5354
return false;
5455
}

src/platform/windows/broker/libvirtualhid_broker.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ namespace lvh::detail::windows_broker_service {
470470
}
471471

472472
template<std::size_t Size>
473-
void copy_c_string(char (&target)[Size], std::string_view value) {
473+
void copy_c_string(std::array<char, Size> &target, std::string_view value) {
474474
std::ranges::fill(target, '\0');
475475
const auto count = std::min(value.size(), Size - 1U);
476-
std::memcpy(target, value.data(), count);
476+
std::memcpy(target.data(), value.data(), count);
477477
}
478478

479479
std::string windows_error_message(DWORD error_code) {
@@ -1189,7 +1189,7 @@ namespace lvh::detail::windows_broker_service {
11891189
const LvhWindowsSessionToken &lhs,
11901190
const LvhWindowsSessionToken &rhs
11911191
) {
1192-
return std::memcmp(lhs.bytes, rhs.bytes, sizeof(lhs.bytes)) == 0;
1192+
return std::ranges::equal(lhs.bytes, rhs.bytes);
11931193
}
11941194

11951195
LvhWindowsDestroyDeviceRequest make_destroy_device_request(
@@ -1690,8 +1690,8 @@ namespace lvh::detail::windows_broker_service {
16901690
return response;
16911691
}
16921692

1693-
const std::string license_key {request.license_key};
1694-
const auto instance_name = request.instance_name[0] == '\0' ? default_instance_name() : std::string {request.instance_name};
1693+
const std::string license_key {request.license_key.data()};
1694+
const auto instance_name = request.instance_name[0] == '\0' ? default_instance_name() : std::string {request.instance_name.data()};
16951695
if (license_key.empty()) {
16961696
response.status = std::to_underlying(LvhWindowsBrokerStatusCode::invalid_argument);
16971697
copy_c_string(response.message, "License key is required.");
@@ -2237,7 +2237,7 @@ namespace lvh::detail::windows_broker_service {
22372237

22382238
std::pair<LvhWindowsBrokerStatusCode, bool> authorize_gamepad_create(
22392239
LvhWindowsBrokerLicenseStatus &license,
2240-
char (&message)[LVH_WINDOWS_BROKER_MAX_MESSAGE_SIZE]
2240+
std::array<char, LVH_WINDOWS_BROKER_MAX_MESSAGE_SIZE> &message
22412241
) {
22422242
{
22432243
std::lock_guard lock {mutex_};
@@ -2281,7 +2281,7 @@ namespace lvh::detail::windows_broker_service {
22812281

22822282
if (!license_allowed(*license_state_) || !license_time_is_current_locked()) {
22832283
fill_license_status_locked(license);
2284-
copy_c_string(message, license.message);
2284+
copy_c_string(message, license.message.data());
22852285
return {LvhWindowsBrokerStatusCode::license_invalid, false};
22862286
}
22872287

src/platform/windows/control_protocol.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
// standard includes
88
#include <algorithm>
9+
#include <array>
910
#include <cstddef>
1011
#include <cstdint>
1112
#include <cstring>
@@ -94,24 +95,24 @@ namespace lvh::detail::windows {
9495
}
9596

9697
template<std::size_t Size>
97-
std::uint32_t copy_string(char (&target)[Size], std::string_view source) {
98+
std::uint32_t copy_string(std::array<char, Size> &target, std::string_view source) {
9899
std::ranges::fill(target, '\0');
99100

100101
const auto copied = std::min(source.size(), Size - 1U);
101102
if (copied > 0U) {
102-
std::memcpy(target, source.data(), copied);
103+
std::memcpy(target.data(), source.data(), copied);
103104
}
104105

105106
return static_cast<std::uint32_t>(copied);
106107
}
107108

108109
template<std::size_t Size>
109-
std::uint32_t copy_bytes(std::uint8_t (&target)[Size], const std::vector<std::uint8_t> &source) {
110+
std::uint32_t copy_bytes(std::array<std::uint8_t, Size> &target, const std::vector<std::uint8_t> &source) {
110111
std::ranges::fill(target, std::uint8_t {});
111112

112113
const auto copied = std::min(source.size(), Size);
113114
if (copied > 0U) {
114-
std::memcpy(target, source.data(), copied);
115+
std::memcpy(target.data(), source.data(), copied);
115116
}
116117

117118
return static_cast<std::uint32_t>(copied);

src/platform/windows/driver/libvirtualhid_umdf.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ namespace {
420420

421421
const auto descriptor_size = record->request.report_sizes.report_descriptor_size;
422422
record->report_descriptor.assign(
423-
record->request.report_descriptor,
424-
record->request.report_descriptor + descriptor_size
423+
record->request.report_descriptor.data(),
424+
record->request.report_descriptor.data() + descriptor_size
425425
);
426426
record->hardware_ids = lvh::detail::windows::make_hardware_ids(record->request);
427427

@@ -484,14 +484,14 @@ namespace {
484484
}
485485

486486
bool session_token_matches(const DeviceRecord &record, const LvhWindowsSessionToken &session_token) {
487-
return std::memcmp(record.session_token.bytes, session_token.bytes, sizeof(record.session_token.bytes)) == 0;
487+
return std::ranges::equal(record.session_token.bytes, session_token.bytes);
488488
}
489489

490490
NTSTATUS generate_session_token(LvhWindowsSessionToken &session_token) {
491491
const auto status = BCryptGenRandom(
492492
nullptr,
493-
session_token.bytes,
494-
static_cast<ULONG>(sizeof(session_token.bytes)),
493+
session_token.bytes.data(),
494+
static_cast<ULONG>(session_token.bytes.size()),
495495
BCRYPT_USE_SYSTEM_PREFERRED_RNG
496496
);
497497
if (!NT_SUCCESS(status)) {
@@ -674,8 +674,8 @@ namespace {
674674
const LvhWindowsSubmitInputReportRequest &request
675675
) {
676676
const auto report_id = record.request.hardware_ids.report_id;
677-
const auto report_begin = request.report;
678-
const auto report_end = request.report + request.report_size;
677+
const auto report_begin = request.report.data();
678+
const auto report_end = request.report.data() + request.report_size;
679679
if (report_id == 0U) {
680680
return {report_begin, report_end};
681681
}
@@ -703,7 +703,7 @@ namespace {
703703
}
704704

705705
if (payload_size > 0U) {
706-
std::memcpy(event.report + report_id_size, packet.reportBuffer, payload_size);
706+
std::memcpy(event.report.data() + report_id_size, packet.reportBuffer, payload_size);
707707
}
708708

709709
event.report_size = static_cast<std::uint32_t>(report_id_size + payload_size);
@@ -714,7 +714,7 @@ namespace {
714714
return;
715715
}
716716

717-
auto reply = lvh::detail::windows::make_switch_pro_reply({event.report, event.report_size});
717+
auto reply = lvh::detail::windows::make_switch_pro_reply({event.report.data(), event.report_size});
718718
if (!reply.has_value()) {
719719
return;
720720
}
@@ -778,7 +778,7 @@ namespace {
778778
std::lock_guard lock {record.mutex};
779779
return record.generic_pid_feature_state.handle_set_feature(
780780
static_cast<std::uint8_t>(report_id),
781-
{event.report, event.report_size}
781+
{event.report.data(), event.report_size}
782782
);
783783
}
784784
return lvh::detail::windows::is_playstation_gamepad(record.request.gamepad_kind);
@@ -792,21 +792,24 @@ namespace {
792792
std::lock_guard lock {record.mutex};
793793
static_cast<void>(record.generic_pid_feature_state.handle_output_report(
794794
static_cast<std::uint8_t>(event.report[0]),
795-
{event.report, event.report_size}
795+
{event.report.data(), event.report_size}
796796
));
797797
}
798798

799-
void set_device_path(std::uint64_t driver_device_id, char (&device_path)[LVH_WINDOWS_MAX_DEVICE_PATH_SIZE]) {
799+
void set_device_path(
800+
std::uint64_t driver_device_id,
801+
std::array<char, LVH_WINDOWS_MAX_DEVICE_PATH_SIZE> &device_path
802+
) {
800803
constexpr auto path_prefix_size = sizeof(LVH_WINDOWS_CONTROL_DEVICE_PATH) - 1U;
801804
constexpr auto separator_size = 1U;
802805
static_assert(path_prefix_size + separator_size < LVH_WINDOWS_MAX_DEVICE_PATH_SIZE);
803806

804-
std::memcpy(device_path, LVH_WINDOWS_CONTROL_DEVICE_PATH, path_prefix_size);
807+
std::memcpy(device_path.data(), LVH_WINDOWS_CONTROL_DEVICE_PATH, path_prefix_size);
805808
device_path[path_prefix_size] = '#';
806809

807810
const auto output = std::to_chars(
808-
device_path + path_prefix_size + separator_size,
809-
device_path + sizeof(device_path) - 1U,
811+
device_path.data() + path_prefix_size + separator_size,
812+
device_path.data() + device_path.size() - 1U,
810813
driver_device_id
811814
);
812815
if (output.ec == std::errc {}) {

0 commit comments

Comments
 (0)