Skip to content

Commit

Permalink
fix(controllers): restore legacy passing of HID data in plain array
Browse files Browse the repository at this point in the history
Fixes #11461, alternative to #11463
  • Loading branch information
Swiftb0y committed Apr 12, 2023
1 parent c7a4bd5 commit e8fad18
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions res/controllers/common-hid-packet-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
this.HIDDebug = function(message) {
console.log(`HID ${message}`);
};
/**
* creates a `DataView` from any ArrayBuffer, TypedArray
* or plain Array (clamped to 8-bit width).
*
* @param {number[] | ArrayBuffer | TypedArray} bufferLike Object that can be represented as a sequence of bytes
* @returns {DataView} dataview over the bufferLike object
*/
const createDataView = function(bufferLike) {
return new DataView((() => {
if (Array.isArray(bufferLike)) {
return new Uint8ClampedArray(bufferLike).buffer;
} else if (ArrayBuffer.isView(bufferLike)) {
return bufferLike.buffer;
} else {
return bufferLike;
}
})());
};

/**
* Callback function to call when, the packet represents an HID InputReport, and new data for this
Expand Down Expand Up @@ -413,7 +431,7 @@ class HIDPacket {
return;
}

const dataView = new DataView(data.buffer);
const dataView = createDataView(data);
switch (field.pack) {
case "b":
dataView.setInt8(field.offset, value);
Expand Down Expand Up @@ -453,7 +471,7 @@ class HIDPacket {
* @returns {number} Value for the field in data, represented according the fields packing type
*/
unpack(data, field) {
const dataView = new DataView(data.buffer);
const dataView = createDataView(data);
switch (field.pack) {
case "b":
return dataView.getInt8(field.offset);
Expand Down

0 comments on commit e8fad18

Please sign in to comment.