mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2026-09-16 23:28:04 -04:00
Build udev from DetectionManager
This commit is contained in:
1 parent
8121ee29f4
commit
33854f8886
4 files changed
+140
No files matched your search
@@ -11,6 +11,8 @@
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <chrono>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include "DetectionManager.h"
|
||||
#include "JsonUtils.h"
|
||||
#include "LogManager.h"
|
||||
@@ -2068,3 +2070,107 @@ bool DetectionManager::IsAnyDimmDetectorEnabled(json &detector_settings)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
/*---------------------------------------------------------*\
|
||||
| Udev rules generation function |
|
||||
\*---------------------------------------------------------*/
|
||||
bool DetectionManager::GenerateUdevRules(const std::string& filepath)
|
||||
{
|
||||
LOG_INFO("[%s] Generating udev rules file: %s", DETECTIONMANAGER, filepath.c_str());
|
||||
|
||||
FILE* output_file = fopen(filepath.c_str(), "w");
|
||||
if(!output_file)
|
||||
{
|
||||
LOG_ERROR("[%s] Failed to open output file: %s", DETECTIONMANAGER, filepath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Write udev rules header |
|
||||
\*-----------------------------------------------------*/
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "# OpenRGB udev rules - Generated by DetectionManager #\n");
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "\n");
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "# User I2C/SMBus Access #\n");
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "KERNEL==\"i2c-[0-99]*\", TAG+=\"uaccess\"\n");
|
||||
fprintf(output_file, "\n");
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "# Super I/O Access #\n");
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "KERNEL==\"port\", TAG+=\"uaccess\"\n");
|
||||
fprintf(output_file, "\n");
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Group detectors by name to avoid duplicate headers |
|
||||
\*-----------------------------------------------------*/
|
||||
std::map<std::string, std::vector<std::pair<uint16_t, uint16_t>>> detector_groups;
|
||||
|
||||
for(std::size_t detector_idx = 0; detector_idx < hid_specific_detectors.size(); detector_idx++)
|
||||
{
|
||||
HIDDeviceDetectorBlock& detector = hid_specific_detectors[detector_idx];
|
||||
|
||||
if(detector.vid == HID_VID_ANY || detector.pid == HID_PID_ANY)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
detector_groups[detector.name].push_back({(uint16_t)detector.vid, (uint16_t)detector.pid});
|
||||
}
|
||||
|
||||
for(std::size_t detector_idx = 0; detector_idx < hid_wrapped_specific_detectors.size(); detector_idx++)
|
||||
{
|
||||
HIDWrappedDeviceDetectorBlock& detector = hid_wrapped_specific_detectors[detector_idx];
|
||||
|
||||
if(detector.vid == HID_VID_ANY || detector.pid == HID_PID_ANY)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string group_name = detector.name + " (libusb)";
|
||||
detector_groups[group_name].push_back({(uint16_t)detector.vid, (uint16_t)detector.pid});
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Write grouped HID device rules |
|
||||
\*-----------------------------------------------------*/
|
||||
for(const std::pair<const std::string, std::vector<std::pair<uint16_t, uint16_t>>>& group : detector_groups)
|
||||
{
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
fprintf(output_file, "# %s\n", group.first.c_str());
|
||||
fprintf(output_file, "#---------------------------------------------------------------#\n");
|
||||
|
||||
std::string device_name_tag = group.first;
|
||||
for(auto it = device_name_tag.begin(); it != device_name_tag.end();)
|
||||
{
|
||||
if(*it == ' ')
|
||||
{
|
||||
*it = '_';
|
||||
++it;
|
||||
}
|
||||
else if(!isalnum(*it))
|
||||
{
|
||||
it = device_name_tag.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
for(const std::pair<uint16_t, uint16_t>& vid_pid : group.second)
|
||||
{
|
||||
fprintf(output_file, "SUBSYSTEMS==\"usb|hidraw\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", TAG+=\"uaccess\", TAG+=\"%s\"\n", vid_pid.first, vid_pid.second, device_name_tag.c_str());
|
||||
}
|
||||
fprintf(output_file, "\n");
|
||||
}
|
||||
|
||||
fclose(output_file);
|
||||
|
||||
LOG_INFO("[%s] Successfully generated udev rules file: %s", DETECTIONMANAGER, filepath.c_str());
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user