Skip to content

Commit

Permalink
Merge pull request #4521 from Be-ing/hid_getInputReport_bytearray
Browse files Browse the repository at this point in the history
HidController: return QByteArrays for get[Input/Feature]Report
  • Loading branch information
Swiftb0y authored Nov 28, 2021
2 parents 7763476 + 2bdf17c commit 1209b46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
32 changes: 12 additions & 20 deletions src/controllers/hid/hidcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void HidController::processInputReport(int bytesRead) {
receive(incomingData, mixxx::Time::elapsed());
}

QList<int> HidController::getInputReport(unsigned int reportID) {
QByteArray HidController::getInputReport(unsigned int reportID) {
Trace hidRead("HidController getInputReport");
int bytesRead;

Expand All @@ -185,17 +185,11 @@ QList<int> HidController::getInputReport(unsigned int reportID) {
// Otherwise minimum possible value is 1, because 1 byte is for the reportID,
// the smallest report with data is therefore 2 bytes.
DEBUG_ASSERT(bytesRead <= kReportIdSize);
return QList<int>();
return QByteArray();
}

// Convert array of bytes read in a JavaScript compatible return type
// For compatibility with the array provided by HidController::poll the reportID is contained as prefix
QList<int> dataList;
dataList.reserve(bytesRead);
for (int i = 0; i < bytesRead; i++) {
dataList.append(m_pPollData[m_pollingBufferIndex][i]);
}
return dataList;
return QByteArray::fromRawData(
reinterpret_cast<char*>(m_pPollData[m_pollingBufferIndex]), bytesRead);
}

bool HidController::poll() {
Expand Down Expand Up @@ -255,14 +249,14 @@ void HidController::sendBytesReport(QByteArray data, unsigned int reportID) {
}

void HidController::sendFeatureReport(
const QList<int>& dataList, unsigned int reportID) {
const QByteArray& reportData, unsigned int reportID) {
QByteArray dataArray;
dataArray.reserve(kReportIdSize + dataList.size());
dataArray.reserve(kReportIdSize + reportData.size());

// Append the Report ID to the beginning of dataArray[] per the API..
dataArray.append(reportID);

for (const int datum : dataList) {
for (const int datum : reportData) {
dataArray.append(datum);
}

Expand All @@ -287,7 +281,7 @@ ControllerJSProxy* HidController::jsProxy() {
return new HidControllerJSProxy(this);
}

QList<int> HidController::getFeatureReport(
QByteArray HidController::getFeatureReport(
unsigned int reportID) {
unsigned char dataRead[kReportIdSize + kBufferSize];
dataRead[0] = reportID;
Expand Down Expand Up @@ -318,10 +312,8 @@ QList<int> HidController::getFeatureReport(

// Convert array of bytes read in a JavaScript compatible return type
// For compatibility with input array HidController::sendFeatureReport, a reportID prefix is not added here
QList<int> dataList;
dataList.reserve(bytesRead - kReportIdSize);
for (int i = kReportIdSize; i < bytesRead; i++) {
dataList.append(dataRead[i]);
}
return dataList;
QByteArray byteArray;
byteArray.reserve(bytesRead - kReportIdSize);
auto featureReportStart = reinterpret_cast<const char*>(dataRead + kReportIdSize);
return QByteArray(featureReportStart, bytesRead);
}
14 changes: 7 additions & 7 deletions src/controllers/hid/hidcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HidController final : public Controller {
// 0x0.
void sendBytes(const QByteArray& data) override;
void sendBytesReport(QByteArray data, unsigned int reportID);
void sendFeatureReport(const QList<int>& dataList, unsigned int reportID);
void sendFeatureReport(const QByteArray& reportData, unsigned int reportID);

// getInputReport receives an input report on request.
// This can be used on startup to initialize the knob positions in Mixxx
Expand All @@ -55,7 +55,7 @@ class HidController final : public Controller {
// as in the polling functionality (including ReportID in first byte).
// The returned list can be used to call the incomingData
// function of the common-hid-packet-parser.
QList<int> getInputReport(unsigned int reportID);
QByteArray getInputReport(unsigned int reportID);

// getFeatureReport receives a feature reports on request.
// HID doesn't support polling feature reports, therefore this is the
Expand All @@ -64,7 +64,7 @@ class HidController final : public Controller {
// changing the other bits. The returned list matches the input
// format of sendFeatureReport, allowing it to be read, modified
// and sent it back to the controller.
QList<int> getFeatureReport(unsigned int reportID);
QByteArray getFeatureReport(unsigned int reportID);

const mixxx::hid::DeviceInfo m_deviceInfo;

Expand Down Expand Up @@ -96,17 +96,17 @@ class HidControllerJSProxy : public ControllerJSProxy {
m_pHidController->sendReport(data, length, reportID);
}

Q_INVOKABLE QList<int> getInputReport(
Q_INVOKABLE QByteArray getInputReport(
unsigned int reportID) {
return m_pHidController->getInputReport(reportID);
}

Q_INVOKABLE void sendFeatureReport(
const QList<int>& dataList, unsigned int reportID) {
m_pHidController->sendFeatureReport(dataList, reportID);
const QByteArray& reportData, unsigned int reportID) {
m_pHidController->sendFeatureReport(reportData, reportID);
}

Q_INVOKABLE QList<int> getFeatureReport(
Q_INVOKABLE QByteArray getFeatureReport(
unsigned int reportID) {
return m_pHidController->getFeatureReport(reportID);
}
Expand Down

0 comments on commit 1209b46

Please sign in to comment.