Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/platform/linux/uhid_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <utility>
Expand Down Expand Up @@ -348,6 +349,13 @@ namespace lvh::detail {
destination[length] = 0;
}

template<std::size_t Size>
void copy_string(std::array<char, Size> &destination, std::string_view source) {
const auto length = std::min(source.size(), Size - 1);
std::memcpy(destination.data(), source.data(), length);
destination[length] = 0;
}

std::optional<std::string> read_first_line(const std::filesystem::path &path) {
std::ifstream file {path};
if (!file) {
Expand Down Expand Up @@ -524,7 +532,7 @@ namespace lvh::detail {
}

if (key_code >= 0x30 && key_code <= 0x39) {
static constexpr int digit_keys[] {
static constexpr std::array digit_keys {
KEY_0,
KEY_1,
KEY_2,
Expand All @@ -539,7 +547,7 @@ namespace lvh::detail {
return digit_keys[key_code - 0x30];
}
if (key_code >= 0x41 && key_code <= 0x5A) {
static constexpr int letter_keys[] {
static constexpr std::array letter_keys {
KEY_A,
KEY_B,
KEY_C,
Expand Down Expand Up @@ -570,7 +578,7 @@ namespace lvh::detail {
return letter_keys[key_code - 0x41];
}
if (key_code >= 0x60 && key_code <= 0x69) {
static constexpr int keypad_digit_keys[] {
static constexpr std::array keypad_digit_keys {
KEY_KP0,
KEY_KP1,
KEY_KP2,
Expand Down Expand Up @@ -600,7 +608,7 @@ namespace lvh::detail {
return KEY_KPSLASH;
}
if (key_code >= 0x70 && key_code <= 0x87) {
static constexpr int function_keys[] {
static constexpr std::array function_keys {
KEY_F1,
KEY_F2,
KEY_F3,
Expand Down
54 changes: 27 additions & 27 deletions tests/fixtures/linux_backend_test_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,9 @@ namespace lvh::detail::test {
} // namespace

std::string linux_copy_string_char_buffer(const std::string &source) {
char destination[5] {};
std::array<char, 5> destination {};
copy_string(destination, source);
return destination;
return destination.data();
}

int linux_key_code(KeyboardKeyCode key_code) {
Expand Down Expand Up @@ -899,8 +899,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_keyboard_submit_pipe(const KeyboardEvent &event) {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -917,8 +917,8 @@ namespace lvh::detail::test {
}

OperationStatus linux_uinput_user_device_pipe() {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno);
}

Expand Down Expand Up @@ -948,8 +948,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_mouse_submit_pipe(const MouseEvent &event) {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -962,8 +962,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_touchscreen_contact_pipe(const TouchContact &contact) {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -979,8 +979,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_trackpad_contact_pipe(const TouchContact &contact) {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -1002,8 +1002,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_pen_tablet_tool_pipe(const PenToolState &state) {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -1022,8 +1022,8 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_trackpad_multi_contact_pipe() {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

Expand All @@ -1048,8 +1048,8 @@ namespace lvh::detail::test {
}

OperationStatus linux_uinput_touchscreen_invalid_contacts() {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno);
}

Expand All @@ -1074,14 +1074,14 @@ namespace lvh::detail::test {
}

LinuxInputSubmissionResult linux_uinput_pen_tablet_transition_pipe() {
int descriptors[2] {-1, -1};
if (::pipe(descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}

UinputPenTablet pen_tablet {descriptors[1]};
auto status = OperationStatus::success();
const PenToolType tools[] {
constexpr std::array tools {
PenToolType::pen,
PenToolType::eraser,
PenToolType::brush,
Expand Down Expand Up @@ -1131,8 +1131,8 @@ namespace lvh::detail::test {

LinuxUhidRoundTripResult linux_uhid_socketpair_roundtrip() {
LinuxUhidRoundTripResult result;
int descriptors[2] {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors.data()) != 0) {
result.create_status = system_error_status(ErrorCode::backend_failure, "failed to create socketpair", errno);
result.submit_status = result.create_status;
result.close_status = result.create_status;
Expand Down Expand Up @@ -1213,8 +1213,8 @@ namespace lvh::detail::test {

LinuxUhidRoundTripResult linux_dualsense_uhid_socketpair_reports() {
LinuxUhidRoundTripResult result;
int descriptors[2] {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors.data()) != 0) {
result.create_status = system_error_status(ErrorCode::backend_failure, "failed to create socketpair", errno);
result.submit_status = result.create_status;
result.close_status = result.create_status;
Expand Down Expand Up @@ -1281,8 +1281,8 @@ namespace lvh::detail::test {

LinuxUhidRoundTripResult linux_dualsense_bluetooth_uhid_socketpair_reports() {
LinuxUhidRoundTripResult result;
int descriptors[2] {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors) != 0) {
std::array<int, 2> descriptors {-1, -1};
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, descriptors.data()) != 0) {
result.create_status = system_error_status(ErrorCode::backend_failure, "failed to create socketpair", errno);
result.submit_status = result.create_status;
result.close_status = result.create_status;
Expand Down