diff --git a/src/userland/hid/main.c b/src/userland/hid/main.c index 2cdaeab..8c8f664 100644 --- a/src/userland/hid/main.c +++ b/src/userland/hid/main.c @@ -80,16 +80,19 @@ return "Unknown"; } -void insertInputReader(uint32_t reportSize, uint32_t usagePage, uint32_t usage, uint32_t data, ListElement **inputReaders) { +void insertInputReader(uint32_t reportSize, uint32_t usagePage, uint32_t usage, uint32_t data, ListElement **inputReaders, uint32_t logicalMin, uint32_t logicalMax) { InputReader *reader = malloc(sizeof(InputReader)); reader->size = reportSize; reader->usagePage = usagePage; reader->usage = usage; // signed integers are represented as 2s-complement + reader->isSigned = logicalMin > logicalMax; + reader->min = logicalMin; + reader->max = logicalMax; listAdd(inputReaders, reader); } -uint32_t input(uint32_t padding, uint32_t data, uint32_t reportCount, uint32_t reportSize, uint32_t currentUsagePage, ListElement **usages, ListElement **inputReaders, uint32_t usageMinimum, uint32_t usageMaximum) { +uint32_t input(uint32_t padding, uint32_t data, uint32_t reportCount, uint32_t reportSize, uint32_t currentUsagePage, ListElement **usages, ListElement **inputReaders, uint32_t usageMinimum, uint32_t usageMaximum, uint32_t logicalMin, uint32_t logicalMax) { // https://www.usb.org/sites/default/files/hid1_11.pdf // page 38, section 6.2.2.4, Main items table char *constant = data >> 0 & 1 ? "Constant" : "Data"; @@ -107,27 +110,27 @@ if (data >> 0 & 1) { // data is constant, no need to keep track of it for (uint32_t i = 0; i < reportCount; i++) { - insertInputReader(reportSize, currentUsagePage, i, data, inputReaders); + insertInputReader(reportSize, currentUsagePage, i, data, inputReaders, logicalMin, logicalMax); } } else if (currentUsagePage == 0x09) { printf("%p New input parser has BUTTON usage for all entries\n", padding); // TODO: research exact implementation for this for (uint32_t i = 0; i < reportCount; i++) { printf("%p Interpreting report %i as button %i\n", padding, i, i + 1); - insertInputReader(reportSize, currentUsagePage, i, data, inputReaders); + insertInputReader(reportSize, currentUsagePage, i, data, inputReaders, logicalMin, logicalMax); } } else if (usageCount == 1) { uint8_t currentUsage = U32(listGet(*usages, 0)); printf("%p New input parser has usage %s for all entries\n", padding, usage(currentUsagePage, currentUsage)); for (uint32_t i = 0; i < reportCount; i++) { - insertInputReader(reportSize, currentUsagePage, currentUsage, data, inputReaders); + insertInputReader(reportSize, currentUsagePage, currentUsage, data, inputReaders, logicalMin, logicalMax); } } else if (usageCount == reportCount) { printf("%p New input has the following usages:\n", padding); uint32_t i = 0; foreach (*usages, void *, currentUsage, { printf("%p Interpreting report %i as %s\n", padding, i++, usage(currentUsagePage, U32(currentUsage))); - insertInputReader(reportSize, currentUsagePage, U32(currentUsage), data, inputReaders); + insertInputReader(reportSize, currentUsagePage, U32(currentUsage), data, inputReaders, logicalMin, logicalMax); }); } else { printf("%p Input parser cannot deduce the usage of the reports, having %i reports and %i usages\n", padding, reportCount, usageCount); @@ -142,6 +145,7 @@ uint32_t currentUsagePage = 0, reportSize = 0, reportCount = 0; uint32_t totalBits = 0; uint32_t usageMinimum = 0, usageMaximum = 0; + uint32_t logicalMin = 0, logicalMax = 0; ListElement *usages = NULL; while (1) { uint8_t item = *read; @@ -163,9 +167,11 @@ break; case 0x05: printf("%pLogicalMinimum(%x)\n", padding, data); + logicalMin = data; break; case 0x09: printf("%pLogicalMaximum(%x)\n", padding, data); + logicalMax = data; break; case 0x06: printf("%pUsageMinimum(%x)\n", padding, data); @@ -180,7 +186,7 @@ reportSize = data; break; case 0x20: - totalBits += input(padding, data, reportCount, reportSize, currentUsagePage, &usages, inputReaders, usageMinimum, usageMaximum); + totalBits += input(padding, data, reportCount, reportSize, currentUsagePage, &usages, inputReaders, usageMinimum, usageMaximum, logicalMin, logicalMax); break; case 0x21: printf("%pReportId(%x)\n", padding, data);