summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBe <be@mixxx.org>2020-10-01 15:58:43 -0500
committerBe <be@mixxx.org>2020-10-01 16:06:21 -0500
commit2343bbf41a1a0908da5df3071f0a278c44298f79 (patch)
treeb51136300eef736be790cdac35738a2bd47ee992
parent447a4324e6fe674d2aed077eff458c91a8fb48b3 (diff)
HidController: reorganize polling loop code
-rw-r--r--src/controllers/hid/hidcontroller.cpp43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/controllers/hid/hidcontroller.cpp b/src/controllers/hid/hidcontroller.cpp
index 3fd227357b..9a5506e895 100644
--- a/src/controllers/hid/hidcontroller.cpp
+++ b/src/controllers/hid/hidcontroller.cpp
@@ -248,41 +248,40 @@ int HidController::close() {
bool HidController::poll() {
Trace hidRead("HidController poll");
- int result = 1;
// This loop risks becoming a high priority endless loop in case processing
// the mapping JS code takes longer than the controller polling rate.
// This could stall other low priority tasks.
// There is no safety net for this because it has not been demonstrated to be
// a problem in practice.
- while (result > 0) {
+ while (true) {
// Rotate between two buffers so the memcmp below does not require deep copying to another buffer.
unsigned char* pPreviousBuffer = m_pPollData[m_iPollingBufferIndex];
m_iPollingBufferIndex = (m_iPollingBufferIndex + 1) % kNumBuffers;
unsigned char* pCurrentBuffer = m_pPollData[m_iPollingBufferIndex];
- result = hid_read(m_pHidDevice, pCurrentBuffer, kBufferSize);
- if (result == -1) {
+ int bytesRead = hid_read(m_pHidDevice, pCurrentBuffer, kBufferSize);
+ if (bytesRead == -1) {
return false;
- } else if (result > 0) {
- Trace process("HidController process packet");
- // Some controllers such as the Gemini GMX continuously send input packets even if it
- // is identical to the previous packet. If this loop processed all those redundant
- // packets, it would be a big performance problem to run JS code for every packet and
- // would be unnecessary.
- // This assumes that the redundant packets all use the same report ID. In practice we
- // have not encountered any controllers that send redundant packets with different report
- // IDs. If any such devices exist, this may be changed to use a separate buffer to store
- // the last packet for each report ID.
- if (memcmp(pCurrentBuffer, pPreviousBuffer, kBufferSize) == 0) {
- continue;
- }
- auto incomingData = QByteArray::fromRawData(
- reinterpret_cast<char*>(pCurrentBuffer), result);
- receive(incomingData, mixxx::Time::elapsed());
+ } else if (bytesRead == 0) {
+ return true;
}
- }
- return true;
+ Trace process("HidController process packet");
+ // Some controllers such as the Gemini GMX continuously send input packets even if it
+ // is identical to the previous packet. If this loop processed all those redundant
+ // packets, it would be a big performance problem to run JS code for every packet and
+ // would be unnecessary.
+ // This assumes that the redundant packets all use the same report ID. In practice we
+ // have not encountered any controllers that send redundant packets with different report
+ // IDs. If any such devices exist, this may be changed to use a separate buffer to store
+ // the last packet for each report ID.
+ if (memcmp(pCurrentBuffer, pPreviousBuffer, kBufferSize) == 0) {
+ continue;
+ }
+ auto incomingData = QByteArray::fromRawData(
+ reinterpret_cast<char*>(pCurrentBuffer), bytesRead);
+ receive(incomingData, mixxx::Time::elapsed());
+ }
}
bool HidController::isPolling() const {