Commit c6e6b234 authored by Matt Reynolds's avatar Matt Reynolds Committed by Commit Bot

[gamepad] Pass report data as base::span<const uint8_t>

This cleanup CL changes methods that write HID gamepad reports
to take base::span<const uint8_t> instead of void*.

BUG=None

Change-Id: I89c88bbbd063c0cae26386de460a785b7138f108
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1750159Reviewed-by: default avatarOvidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Commit-Queue: Matt Reynolds <mattreynolds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#687416}
parent d6f013d0
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "device/gamepad/dualshock4_controller_base.h" #include "device/gamepad/dualshock4_controller_base.h"
#include <array>
namespace { namespace {
const uint16_t kVendorSony = 0x054c; const uint16_t kVendorSony = 0x054c;
const uint16_t kProductDualshock4 = 0x05c4; const uint16_t kProductDualshock4 = 0x05c4;
...@@ -46,9 +48,8 @@ bool Dualshock4ControllerBase::IsDualshock4(uint16_t vendor_id, ...@@ -46,9 +48,8 @@ bool Dualshock4ControllerBase::IsDualshock4(uint16_t vendor_id,
void Dualshock4ControllerBase::SetVibration(double strong_magnitude, void Dualshock4ControllerBase::SetVibration(double strong_magnitude,
double weak_magnitude) { double weak_magnitude) {
const size_t report_length = 32; std::array<uint8_t, 32> control_report;
uint8_t control_report[report_length]; control_report.fill(0);
memset(control_report, 0, report_length);
control_report[0] = 0x05; // report ID control_report[0] = 0x05; // report ID
control_report[1] = 0x01; // motor only, don't update LEDs control_report[1] = 0x01; // motor only, don't update LEDs
control_report[4] = control_report[4] =
...@@ -56,12 +57,7 @@ void Dualshock4ControllerBase::SetVibration(double strong_magnitude, ...@@ -56,12 +57,7 @@ void Dualshock4ControllerBase::SetVibration(double strong_magnitude,
control_report[5] = control_report[5] =
static_cast<uint8_t>(strong_magnitude * kRumbleMagnitudeMax); static_cast<uint8_t>(strong_magnitude * kRumbleMagnitudeMax);
WriteOutputReport(control_report, report_length); WriteOutputReport(control_report);
}
size_t Dualshock4ControllerBase::WriteOutputReport(void* report,
size_t report_length) {
return 0;
} }
} // namespace device } // namespace device
...@@ -19,7 +19,9 @@ class Dualshock4ControllerBase : public AbstractHapticGamepad { ...@@ -19,7 +19,9 @@ class Dualshock4ControllerBase : public AbstractHapticGamepad {
// AbstractHapticGamepad implementation. // AbstractHapticGamepad implementation.
void SetVibration(double strong_magnitude, double weak_magnitude) override; void SetVibration(double strong_magnitude, double weak_magnitude) override;
virtual size_t WriteOutputReport(void* report, size_t report_length); // Sends an output report to the gamepad. Derived classes should override this
// method with a platform-specific implementation.
virtual size_t WriteOutputReport(base::span<const uint8_t> report) = 0;
}; };
} // namespace device } // namespace device
......
...@@ -13,9 +13,11 @@ Dualshock4ControllerLinux::Dualshock4ControllerLinux(const base::ScopedFD& fd) ...@@ -13,9 +13,11 @@ Dualshock4ControllerLinux::Dualshock4ControllerLinux(const base::ScopedFD& fd)
Dualshock4ControllerLinux::~Dualshock4ControllerLinux() = default; Dualshock4ControllerLinux::~Dualshock4ControllerLinux() = default;
size_t Dualshock4ControllerLinux::WriteOutputReport(void* report, size_t Dualshock4ControllerLinux::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
ssize_t bytes_written = HANDLE_EINTR(write(fd_, report, report_length)); DCHECK_GE(report.size_bytes(), 1U);
ssize_t bytes_written =
HANDLE_EINTR(write(fd_, report.data(), report.size_bytes()));
return bytes_written < 0 ? 0 : static_cast<size_t>(bytes_written); return bytes_written < 0 ? 0 : static_cast<size_t>(bytes_written);
} }
......
...@@ -20,7 +20,7 @@ class Dualshock4ControllerLinux final : public Dualshock4ControllerBase { ...@@ -20,7 +20,7 @@ class Dualshock4ControllerLinux final : public Dualshock4ControllerBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// Dualshock4ControllerBase implementation. // Dualshock4ControllerBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
int fd_; // Not owned. int fd_; // Not owned.
......
...@@ -17,16 +17,16 @@ void Dualshock4ControllerMac::DoShutdown() { ...@@ -17,16 +17,16 @@ void Dualshock4ControllerMac::DoShutdown() {
device_ref_ = nullptr; device_ref_ = nullptr;
} }
size_t Dualshock4ControllerMac::WriteOutputReport(void* report, size_t Dualshock4ControllerMac::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
DCHECK_GE(report.size_bytes(), 1U);
if (!device_ref_) if (!device_ref_)
return 0; return 0;
const unsigned char* report_data = static_cast<unsigned char*>(report);
IOReturn success = IOReturn success =
IOHIDDeviceSetReport(device_ref_, kIOHIDReportTypeOutput, report_data[0], IOHIDDeviceSetReport(device_ref_, kIOHIDReportTypeOutput, report[0],
report_data, report_length); report.data(), report.size_bytes());
return (success == kIOReturnSuccess) ? report_length : 0; return (success == kIOReturnSuccess) ? report.size_bytes() : 0;
} }
base::WeakPtr<AbstractHapticGamepad> Dualshock4ControllerMac::GetWeakPtr() { base::WeakPtr<AbstractHapticGamepad> Dualshock4ControllerMac::GetWeakPtr() {
......
...@@ -22,7 +22,7 @@ class Dualshock4ControllerMac final : public Dualshock4ControllerBase { ...@@ -22,7 +22,7 @@ class Dualshock4ControllerMac final : public Dualshock4ControllerBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// Dualshock4ControllerBase implementation. // Dualshock4ControllerBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
IOHIDDeviceRef device_ref_; IOHIDDeviceRef device_ref_;
......
...@@ -35,10 +35,9 @@ void Dualshock4ControllerWin::DoShutdown() { ...@@ -35,10 +35,9 @@ void Dualshock4ControllerWin::DoShutdown() {
hid_handle_.Close(); hid_handle_.Close();
} }
size_t Dualshock4ControllerWin::WriteOutputReport(void* report, size_t Dualshock4ControllerWin::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
DCHECK(report); DCHECK_GE(report.size_bytes(), 1U);
DCHECK_GE(report_length, 1U);
if (!hid_handle_.IsValid()) if (!hid_handle_.IsValid())
return 0; return 0;
...@@ -49,8 +48,9 @@ size_t Dualshock4ControllerWin::WriteOutputReport(void* report, ...@@ -49,8 +48,9 @@ size_t Dualshock4ControllerWin::WriteOutputReport(void* report,
// Set up an asynchronous write. // Set up an asynchronous write.
DWORD bytes_written = 0; DWORD bytes_written = 0;
BOOL write_success = ::WriteFile(hid_handle_.Get(), report, report_length, BOOL write_success =
&bytes_written, &overlapped); ::WriteFile(hid_handle_.Get(), report.data(), report.size_bytes(),
&bytes_written, &overlapped);
if (!write_success) { if (!write_success) {
DWORD error = ::GetLastError(); DWORD error = ::GetLastError();
if (error == ERROR_IO_PENDING) { if (error == ERROR_IO_PENDING) {
......
...@@ -21,7 +21,7 @@ class Dualshock4ControllerWin final : public Dualshock4ControllerBase { ...@@ -21,7 +21,7 @@ class Dualshock4ControllerWin final : public Dualshock4ControllerBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// Dualshock4ControllerBase implementation. // Dualshock4ControllerBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
base::win::ScopedHandle hid_handle_; base::win::ScopedHandle hid_handle_;
......
...@@ -123,7 +123,7 @@ void HidHapticGamepadBase::SetVibration(double strong_magnitude, ...@@ -123,7 +123,7 @@ void HidHapticGamepadBase::SetVibration(double strong_magnitude,
std::copy(right_bytes.begin(), right_bytes.end(), std::copy(right_bytes.begin(), right_bytes.end(),
control_report.begin() + weak_offset_bytes_); control_report.begin() + weak_offset_bytes_);
} }
WriteOutputReport(control_report.data(), report_length_bytes_); WriteOutputReport(control_report);
} }
} // namespace device } // namespace device
...@@ -75,7 +75,7 @@ class DEVICE_GAMEPAD_EXPORT HidHapticGamepadBase ...@@ -75,7 +75,7 @@ class DEVICE_GAMEPAD_EXPORT HidHapticGamepadBase
void SetVibration(double strong_magnitude, double weak_magnitude) override; void SetVibration(double strong_magnitude, double weak_magnitude) override;
// Write the vibration output report to the device. // Write the vibration output report to the device.
virtual size_t WriteOutputReport(void* report, size_t report_length) = 0; virtual size_t WriteOutputReport(base::span<const uint8_t> report) = 0;
protected: protected:
HidHapticGamepadBase(const HapticReportData& data); HidHapticGamepadBase(const HapticReportData& data);
......
...@@ -58,13 +58,10 @@ class FakeHidHapticGamepad final : public HidHapticGamepadBase { ...@@ -58,13 +58,10 @@ class FakeHidHapticGamepad final : public HidHapticGamepadBase {
: HidHapticGamepadBase(data) {} : HidHapticGamepadBase(data) {}
~FakeHidHapticGamepad() override = default; ~FakeHidHapticGamepad() override = default;
size_t WriteOutputReport(void* report, size_t report_length) override { size_t WriteOutputReport(base::span<const uint8_t> report) override {
std::vector<uint8_t> report_bytes(report_length); output_reports_.push_back(
const uint8_t* report_begin = reinterpret_cast<uint8_t*>(report); std::vector<uint8_t>(report.begin(), report.end()));
const uint8_t* report_end = report_begin + report_length; return report.size_bytes();
std::copy(report_begin, report_end, report_bytes.begin());
output_reports_.push_back(std::move(report_bytes));
return report_length;
} }
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override { base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override {
......
...@@ -25,9 +25,11 @@ std::unique_ptr<HidHapticGamepadLinux> HidHapticGamepadLinux::Create( ...@@ -25,9 +25,11 @@ std::unique_ptr<HidHapticGamepadLinux> HidHapticGamepadLinux::Create(
return std::make_unique<HidHapticGamepadLinux>(fd, *haptic_data); return std::make_unique<HidHapticGamepadLinux>(fd, *haptic_data);
} }
size_t HidHapticGamepadLinux::WriteOutputReport(void* report, size_t HidHapticGamepadLinux::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
ssize_t bytes_written = HANDLE_EINTR(write(fd_, report, report_length)); DCHECK_GE(report.size_bytes(), 1U);
ssize_t bytes_written =
HANDLE_EINTR(write(fd_, report.data(), report.size_bytes()));
return bytes_written < 0 ? 0 : static_cast<size_t>(bytes_written); return bytes_written < 0 ? 0 : static_cast<size_t>(bytes_written);
} }
......
...@@ -26,7 +26,7 @@ class HidHapticGamepadLinux final : public HidHapticGamepadBase { ...@@ -26,7 +26,7 @@ class HidHapticGamepadLinux final : public HidHapticGamepadBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// HidHapticGamepadBase implementation. // HidHapticGamepadBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
// Not owned. // Not owned.
......
...@@ -29,16 +29,16 @@ std::unique_ptr<HidHapticGamepadMac> HidHapticGamepadMac::Create( ...@@ -29,16 +29,16 @@ std::unique_ptr<HidHapticGamepadMac> HidHapticGamepadMac::Create(
return std::make_unique<HidHapticGamepadMac>(device_ref, *haptic_data); return std::make_unique<HidHapticGamepadMac>(device_ref, *haptic_data);
} }
size_t HidHapticGamepadMac::WriteOutputReport(void* report, size_t HidHapticGamepadMac::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
DCHECK_GE(report.size_bytes(), 1U);
if (!device_ref_) if (!device_ref_)
return 0; return 0;
const unsigned char* report_data = static_cast<unsigned char*>(report);
IOReturn success = IOReturn success =
IOHIDDeviceSetReport(device_ref_, kIOHIDReportTypeOutput, report_data[0], IOHIDDeviceSetReport(device_ref_, kIOHIDReportTypeOutput, report[0],
report_data, report_length); report.data(), report.size_bytes());
return (success == kIOReturnSuccess) ? report_length : 0; return (success == kIOReturnSuccess) ? report.size_bytes() : 0;
} }
base::WeakPtr<AbstractHapticGamepad> HidHapticGamepadMac::GetWeakPtr() { base::WeakPtr<AbstractHapticGamepad> HidHapticGamepadMac::GetWeakPtr() {
......
...@@ -28,7 +28,7 @@ class HidHapticGamepadMac final : public HidHapticGamepadBase { ...@@ -28,7 +28,7 @@ class HidHapticGamepadMac final : public HidHapticGamepadBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// HidHapticGamepadBase implementation. // HidHapticGamepadBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
IOHIDDeviceRef device_ref_; IOHIDDeviceRef device_ref_;
......
...@@ -51,10 +51,9 @@ void HidHapticGamepadWin::DoShutdown() { ...@@ -51,10 +51,9 @@ void HidHapticGamepadWin::DoShutdown() {
hid_handle_.Close(); hid_handle_.Close();
} }
size_t HidHapticGamepadWin::WriteOutputReport(void* report, size_t HidHapticGamepadWin::WriteOutputReport(
size_t report_length) { base::span<const uint8_t> report) {
DCHECK(report); DCHECK_GE(report.size_bytes(), 1U);
DCHECK_GE(report_length, 1U);
if (!hid_handle_.IsValid()) if (!hid_handle_.IsValid())
return 0; return 0;
...@@ -68,8 +67,9 @@ size_t HidHapticGamepadWin::WriteOutputReport(void* report, ...@@ -68,8 +67,9 @@ size_t HidHapticGamepadWin::WriteOutputReport(void* report,
// Set up an asynchronous write. // Set up an asynchronous write.
DWORD bytes_written = 0; DWORD bytes_written = 0;
BOOL write_success = ::WriteFile(hid_handle_.Get(), report, report_length, BOOL write_success =
&bytes_written, &overlapped); ::WriteFile(hid_handle_.Get(), report.data(), report.size_bytes(),
&bytes_written, &overlapped);
if (!write_success) { if (!write_success) {
DWORD error = ::GetLastError(); DWORD error = ::GetLastError();
if (error == ERROR_IO_PENDING) { if (error == ERROR_IO_PENDING) {
......
...@@ -27,7 +27,7 @@ class HidHapticGamepadWin final : public HidHapticGamepadBase { ...@@ -27,7 +27,7 @@ class HidHapticGamepadWin final : public HidHapticGamepadBase {
base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override; base::WeakPtr<AbstractHapticGamepad> GetWeakPtr() override;
// HidHapticGamepadBase implementation. // HidHapticGamepadBase implementation.
size_t WriteOutputReport(void* report, size_t report_length) override; size_t WriteOutputReport(base::span<const uint8_t> report) override;
private: private:
base::win::ScopedHandle hid_handle_; base::win::ScopedHandle hid_handle_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment