Skip to content

Commit

Permalink
Updated HidUsageTables to use static data structure from hidusagetabl…
Browse files Browse the repository at this point in the history
…esdata.h.

Added hid_usagepages_json2cppheader.py script to create C++ header from official JSON source
Modified related classes accordingly.
  • Loading branch information
JoergAtGithub committed Nov 24, 2024
1 parent b23d05a commit d74a1c5
Show file tree
Hide file tree
Showing 12 changed files with 3,758 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/controllers/controllermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void ControllerManager::slotInitialize() {
m_enumerators.append(new BulkEnumerator());
#endif
#ifdef __HID__
m_enumerators.append(new HidEnumerator(m_pConfig));
m_enumerators.append(new HidEnumerator());
#endif
}

Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions src/controllers/hid/hiddevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace mixxx {
namespace hid {

DeviceInfo::DeviceInfo(
const hid_device_info& device_info, const HidUsageTables& hidUsageTables)
const hid_device_info& device_info)
: vendor_id(device_info.vendor_id),
product_id(device_info.product_id),
release_number(device_info.release_number),
Expand All @@ -35,8 +35,7 @@ DeviceInfo::DeviceInfo(
m_productString(mixxx::convertWCStringToQString(device_info.product_string,
kDeviceInfoStringMaxLength)),
m_serialNumber(mixxx::convertWCStringToQString(
m_serialNumberRaw.data(), m_serialNumberRaw.size())),
m_hidUsageTables(hidUsageTables) {
m_serialNumberRaw.data(), m_serialNumberRaw.size())) {
switch (device_info.bus_type) {
case HID_API_BUS_USB:
m_physicalTransportProtocol = PhysicalTransportProtocol::USB;
Expand Down
8 changes: 3 additions & 5 deletions src/controllers/hid/hiddevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace hid {
class DeviceInfo final {
public:
explicit DeviceInfo(
const hid_device_info& device_info, const HidUsageTables& hidUsageTables);
const hid_device_info& device_info);

// The VID.
uint16_t getVendorId() const {
Expand Down Expand Up @@ -94,11 +94,11 @@ class DeviceInfo final {
}

QString getUsagePageDescription() const {
return m_hidUsageTables.getUsagePageDescription(usage_page);
return mixxx::hid::HidUsageTables::getUsagePageDescription(usage_page);
}

QString getUsageDescription() const {
return m_hidUsageTables.getUsageDescription(usage_page, usage);
return mixxx::hid::HidUsageTables::getUsageDescription(usage_page, usage);
}

bool isValid() const {
Expand Down Expand Up @@ -131,8 +131,6 @@ class DeviceInfo final {
QString m_manufacturerString;
QString m_productString;
QString m_serialNumber;

const HidUsageTables& m_hidUsageTables;
};

} // namespace hid
Expand Down
9 changes: 1 addition & 8 deletions src/controllers/hid/hidenumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ bool HidEnumerator::recognizeDevice(const hid_device_info& device_info) const {
return true;
}

HidEnumerator::HidEnumerator(UserSettingsPointer pConfig)
: m_pConfig(pConfig),
// Loading this JSON file is very fast (~8ms) and is only loaded here during startup,
// to be shared across all HID devices.
m_hidUsageTable(pConfig->getResourcePath() + "deviceinfo/HidUsageTables.json") {
}

HidEnumerator::~HidEnumerator() {
qDebug() << "Deleting HID devices...";
while (m_devices.size() > 0) {
Expand All @@ -105,7 +98,7 @@ QList<Controller*> HidEnumerator::queryDevices() {
for (const auto* device_info = device_info_list;
device_info;
device_info = device_info->next) {
auto deviceInfo = mixxx::hid::DeviceInfo(*device_info, m_hidUsageTable);
auto deviceInfo = mixxx::hid::DeviceInfo(*device_info);
// The hidraw backend of hidapi on Linux returns many duplicate hid_device_info's from hid_enumerate,
// so filter them out.
// https://github.com/libusb/hidapi/issues/298
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/hid/hidenumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ class HidEnumerator : public ControllerEnumerator {
Q_OBJECT
public:
bool recognizeDevice(const hid_device_info& device_info) const;
explicit HidEnumerator(UserSettingsPointer pConfig);
HidEnumerator() = default;
~HidEnumerator() override;

QList<Controller*> queryDevices() override;

private:
UserSettingsPointer m_pConfig;
QList<Controller*> m_devices;
mixxx::hid::HidUsageTables m_hidUsageTable;
};
43 changes: 11 additions & 32 deletions src/controllers/hid/hidusagetables.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "controllers/hid/hidusagetables.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include "controllers/hid/hidusagetablesdata.h" // Include the generated header

namespace {
// The HID Usage Tables 1.5 PDF specifies that the vendor-defined Usage-Page
Expand All @@ -16,47 +13,29 @@ namespace mixxx {

namespace hid {

HidUsageTables::HidUsageTables(const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Failed to open HID usage tables file:" << filePath;
m_hidUsageTables = QJsonObject();
return;
}
QByteArray fileData = file.readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(fileData);
m_hidUsageTables = jsonDoc.object();
}

QString HidUsageTables::getUsagePageDescription(unsigned short usagePage) const {
QString HidUsageTables::getUsagePageDescription(uint16_t usagePage) {

Check failure on line 16 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 24.04

no declaration matches ‘QString mixxx::hid::HidUsageTables::getUsagePageDescription(uint16_t)’

Check failure on line 16 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / coverage

no declaration matches ‘QString mixxx::hid::HidUsageTables::getUsagePageDescription(uint16_t)’

Check failure on line 16 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / macOS 12 x64

out-of-line definition of 'getUsagePageDescription' does not match any declaration in 'mixxx::hid::HidUsageTables'

Check failure on line 16 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

out-of-line definition of 'getUsagePageDescription' does not match any declaration in 'mixxx::hid::HidUsageTables'
if (usagePage >= kStartOfVendorDefinedUsagePageRange) {
return QStringLiteral("Vendor-defined");
}

const QJsonArray usagePages = m_hidUsageTables.value("UsagePages").toArray();
for (const QJsonValue& pageValue : usagePages) {
QJsonObject pageObject = pageValue.toObject();
if (pageObject.value("Id").toInt() == usagePage) {
return pageObject.value("Name").toString();
for (const auto& page : kHidUsagePages) {
if (page.id == usagePage) {
return page.name;
}
}
return QStringLiteral("Reserved");
}

QString HidUsageTables::getUsageDescription(unsigned short usagePage, unsigned short usage) const {
QString HidUsageTables::getUsageDescription(uint16_t usagePage, uint16_t usage) {

Check failure on line 29 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 24.04

no declaration matches ‘QString mixxx::hid::HidUsageTables::getUsageDescription(uint16_t, uint16_t)’

Check failure on line 29 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / coverage

no declaration matches ‘QString mixxx::hid::HidUsageTables::getUsageDescription(uint16_t, uint16_t)’

Check failure on line 29 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / macOS 12 x64

out-of-line definition of 'getUsageDescription' does not match any declaration in 'mixxx::hid::HidUsageTables'

Check failure on line 29 in src/controllers/hid/hidusagetables.cpp

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

out-of-line definition of 'getUsageDescription' does not match any declaration in 'mixxx::hid::HidUsageTables'
if (usagePage >= kStartOfVendorDefinedUsagePageRange) {
return QStringLiteral("Vendor-defined");
}

const QJsonArray usagePages = m_hidUsageTables.value("UsagePages").toArray();
for (const QJsonValue& pageValue : usagePages) {
QJsonObject pageObject = pageValue.toObject();
if (pageObject.value("Id").toInt() == usagePage) {
const QJsonArray usageIds = pageObject.value("UsageIds").toArray();
for (const QJsonValue& usageValue : usageIds) {
QJsonObject usageObject = usageValue.toObject();
if (usageObject.value("Id").toInt() == usage) {
return usageObject.value("Name").toString();
for (const auto& page : kHidUsagePages) {
if (page.id == usagePage) {
for (const auto& usageItem : page.usages) {
if (usageItem.id == usage) {
return usageItem.name;
}
}
break; // No need to continue if the usage page is found
Expand Down
11 changes: 3 additions & 8 deletions src/controllers/hid/hidusagetables.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#pragma once

#include <QJsonObject>

namespace mixxx {

namespace hid {

class HidUsageTables {
public:
explicit HidUsageTables(const QString& filePath);
QString getUsagePageDescription(unsigned short usagePage) const;
QString getUsageDescription(unsigned short usagePage, unsigned short usage) const;

private:
QJsonObject m_hidUsageTables;
HidUsageTables() = default;
static QString getUsagePageDescription(uint16_t usagePage);

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / Ubuntu 24.04

‘QString’ does not name a type

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / coverage

‘QString’ does not name a type

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 x64

unknown type name 'QString'

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 x64

unknown type name 'uint16_t'

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

unknown type name 'QString'

Check failure on line 10 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

unknown type name 'uint16_t'
static QString getUsageDescription(uint16_t usagePage, uint16_t usage);

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / Ubuntu 24.04

‘QString’ does not name a type

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / coverage

‘QString’ does not name a type

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 x64

unknown type name 'QString'

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 x64

unknown type name 'uint16_t'

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 x64

unknown type name 'uint16_t'

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

unknown type name 'QString'

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

unknown type name 'uint16_t'

Check failure on line 11 in src/controllers/hid/hidusagetables.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

unknown type name 'uint16_t'
};

} // namespace hid
Expand Down
Loading

0 comments on commit d74a1c5

Please sign in to comment.