Browse Source

Implement dynamic detectors - run-once functions that can register detectors dynamically

merge-requests/525/merge
Adam Honse 8 months ago
parent
commit
d18f66f995
  1. 15
      Controllers/QMKOpenRGBController/QMKOpenRGBControllerDetect.cpp
  2. 5
      Detector.h
  3. 9
      DeviceDetector.h
  4. 35
      ResourceManager.cpp
  5. 7
      ResourceManager.h

15
Controllers/QMKOpenRGBController/QMKOpenRGBControllerDetect.cpp

@ -65,8 +65,13 @@ void DetectQMKOpenRGBControllers(hid_device_info *info, const std::string&)
}
}
REGISTER_HID_DETECTOR_IPU("Massdrop Alt", DetectQMKOpenRGBControllers, MASSDROP_VID, MASSDROP_ALT_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("Massdrop Ctrl", DetectQMKOpenRGBControllers, MASSDROP_VID, MASSDROP_CTRL_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("SonixQMK 0C45:5004", DetectQMKOpenRGBControllers, SONIXQMK_VID, SONIXQMK_5004_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("SonixQMK 0C45:5104", DetectQMKOpenRGBControllers, SONIXQMK_VID, SONIXQMK_5104_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("KBDFans KBD67 MKII RGB", DetectQMKOpenRGBControllers, KBDFANS_VID, KBDFANS_K67_MKII_RGB_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
void RegisterQMKDetectors()
{
REGISTER_HID_DETECTOR_IPU("Massdrop Alt", DetectQMKOpenRGBControllers, MASSDROP_VID, MASSDROP_ALT_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("Massdrop Ctrl", DetectQMKOpenRGBControllers, MASSDROP_VID, MASSDROP_CTRL_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("SonixQMK 0C45:5004", DetectQMKOpenRGBControllers, SONIXQMK_VID, SONIXQMK_5004_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("SonixQMK 0C45:5104", DetectQMKOpenRGBControllers, SONIXQMK_VID, SONIXQMK_5104_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
REGISTER_HID_DETECTOR_IPU("KBDFans KBD67 MKII RGB", DetectQMKOpenRGBControllers, KBDFANS_VID, KBDFANS_K67_MKII_RGB_PID, 1, QMK_USAGE_PAGE, QMK_USAGE);
}
REGISTER_DYNAMIC_DETECTOR("QMK OpenRGB Devices", RegisterQMKDetectors);

5
Detector.h

@ -2,12 +2,13 @@
#include "DeviceDetector.h"
#define REGISTER_DETECTOR(name, func) static DeviceDetector device_detector_obj_##func(name, func)
#define REGISTER_DETECTOR(name, func) static DeviceDetector device_detector_obj_##func(name, func)
#define REGISTER_I2C_DETECTOR(name, func) static I2CDeviceDetector device_detector_obj_##func(name, func)
#define REGISTER_I2C_BUS_DETECTOR(func) static I2CBusDetector device_detector_obj_##func(func)
#define REGISTER_I2C_BUS_DETECTOR(func) static I2CBusDetector device_detector_obj_##func(func)
#define REGISTER_HID_DETECTOR(name, func, vid, pid) static HIDDeviceDetector device_detector_obj_##vid##pid(name, func, vid, pid, HID_INTERFACE_ANY, HID_USAGE_PAGE_ANY, HID_USAGE_ANY)
#define REGISTER_HID_DETECTOR_I(name, func, vid, pid, interface) static HIDDeviceDetector device_detector_obj_##vid##pid##_##interface(name, func, vid, pid, interface, HID_USAGE_PAGE_ANY, HID_USAGE_ANY)
#define REGISTER_HID_DETECTOR_IP(name, func, vid, pid, interface, page) static HIDDeviceDetector device_detector_obj_##vid##pid##_##interface##_##page(name, func, vid, pid, interface, page, HID_USAGE_ANY)
#define REGISTER_HID_DETECTOR_IPU(name, func, vid, pid, interface, page, usage) static HIDDeviceDetector device_detector_obj_##vid##pid##_##interface##_##page##_##usage(name, func, vid, pid, interface, page, usage)
#define REGISTER_HID_DETECTOR_P(name, func, vid, pid, page) static HIDDeviceDetector device_detector_obj_##vid##pid##__##page(name, func, vid, pid, HID_INTERFACE_ANY, page, HID_USAGE_ANY)
#define REGISTER_HID_DETECTOR_PU(name, func, vid, pid, page, usage) static HIDDeviceDetector device_detector_obj_##vid##pid##__##page##_##usage(name, func, vid, pid, HID_INTERFACE_ANY, page, usage)
#define REGISTER_DYNAMIC_DETECTOR(name, func) static DynamicDetector device_detector_obj_##func(name, func)

9
DeviceDetector.h

@ -41,3 +41,12 @@ public:
ResourceManager::get()->RegisterHIDDeviceDetector(name, detector, vid, pid, interface, usage_page, usage);
}
};
class DynamicDetector
{
public:
DynamicDetector(std::string name, DynamicDetectorFunction detector)
{
ResourceManager::get()->RegisterDynamicDetector(name, detector);
}
};

35
ResourceManager.cpp

@ -37,11 +37,12 @@ ResourceManager::ResourceManager()
/*-------------------------------------------------------------------------*\
| Initialize Detection Variables |
\*-------------------------------------------------------------------------*/
detection_enabled = true;
detection_percent = 100;
detection_string = "";
detection_is_required = false;
DetectDevicesThread = nullptr;
detection_enabled = true;
detection_percent = 100;
detection_string = "";
detection_is_required = false;
DetectDevicesThread = nullptr;
dynamic_detectors_processed = false;
SetupConfigurationDirectory();
@ -185,6 +186,12 @@ void ResourceManager::RegisterHIDDeviceDetector(std::string name,
hid_device_detector_strings.push_back(name);
}
void ResourceManager::RegisterDynamicDetector(std::string name, DynamicDetectorFunction detector)
{
dynamic_detector_strings.push_back(name);
dynamic_detectors.push_back(detector);
}
void ResourceManager::RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg)
{
DeviceListChangeCallbacks.push_back(new_callback);
@ -503,8 +510,26 @@ void ResourceManager::Cleanup()
}
}
void ResourceManager::ProcessDynamicDetectors()
{
for(unsigned int detector_idx = 0; detector_idx < dynamic_detectors.size(); detector_idx++)
{
dynamic_detectors[detector_idx]();
}
dynamic_detectors_processed = true;
}
void ResourceManager::DetectDevices()
{
/*-----------------------------------------------------*\
| Process Dynamic Detectors |
\*-----------------------------------------------------*/
if(!dynamic_detectors_processed)
{
ProcessDynamicDetectors();
}
/*-----------------------------------------------------*\
| Call detection start callbacks |
\*-----------------------------------------------------*/

7
ResourceManager.h

@ -36,6 +36,7 @@ typedef std::function<void()> I2CBusDetect
typedef std::function<void(std::vector<RGBController*>&)> DeviceDetectorFunction;
typedef std::function<void(std::vector<i2c_smbus_interface*>&)> I2CDeviceDetectorFunction;
typedef std::function<void(hid_device_info*, const std::string&)> HIDDeviceDetectorFunction;
typedef std::function<void()> DynamicDetectorFunction;
typedef struct
{
std::string name;
@ -111,6 +112,7 @@ public:
int interface = HID_INTERFACE_ANY,
int usage_page = HID_USAGE_PAGE_ANY,
int usage = HID_USAGE_ANY);
void RegisterDynamicDetector (std::string name, DynamicDetectorFunction detector);
void RegisterDeviceListChangeCallback(DeviceListChangeCallback new_callback, void * new_callback_arg);
void RegisterDetectionProgressCallback(DetectionProgressCallback new_callback, void * new_callback_arg);
@ -135,6 +137,7 @@ public:
void SetConfigurationDirectory(std::string directory);
void ProcessDynamicDetectors();
void UpdateDeviceList();
void DeviceListChanged();
void DetectionProgressChanged();
@ -207,6 +210,10 @@ private:
std::vector<std::string> i2c_device_detector_strings;
std::vector<HIDDeviceDetectorBlock> hid_device_detectors;
std::vector<std::string> hid_device_detector_strings;
std::vector<DynamicDetectorFunction> dynamic_detectors;
std::vector<std::string> dynamic_detector_strings;
bool dynamic_detectors_processed;
/*-------------------------------------------------------------------------------------*\
| Detection Thread and Detection State |

Loading…
Cancel
Save