fixed fragile code

This commit is contained in:
2026-07-17 10:23:13 +03:00
parent 247de6f02a
commit 2f35e33d6b
3 changed files with 29 additions and 3 deletions

View File

@@ -48,6 +48,8 @@ static constexpr const char* SOCK_PATH = "/run/acer-rgbd.sock";
static constexpr const char* STATE_DIR = "/var/lib/acer-rgbd";
static constexpr const char* STATE_PATH = "/var/lib/acer-rgbd/state.txt";
static std::string g_hidraw_path;
struct Settings {
std::string hidraw = "/dev/hidraw2";
uint8_t device = KEYBOARD_RGB_ID;
@@ -84,6 +86,23 @@ static std::optional<std::string> read_file(const std::string& path) {
return out;
}
// Scan /sys/class/hidraw for the ENE keyboard controller by looking for
// HID usage page 0xFF5A (bytes 06 5A FF) at the start of the report descriptor.
// This survives kernel updates that renumber hidraw devices.
static std::string find_ene_hidraw() {
for (int i = 0; i < 16; i++) {
std::string desc_path = "/sys/class/hidraw/hidraw" + std::to_string(i) + "/device/report_descriptor";
auto desc = read_file(desc_path);
if (!desc || desc->size() < 3) continue;
if ((uint8_t)(*desc)[0] == 0x06 &&
(uint8_t)(*desc)[1] == 0x5A &&
(uint8_t)(*desc)[2] == 0xFF) {
return "/dev/hidraw" + std::to_string(i);
}
}
return "";
}
static std::vector<std::string> split_ws(std::string_view s) {
std::vector<std::string> v;
size_t i = 0;
@@ -159,7 +178,7 @@ static std::optional<uint8_t> parse_zone(const std::string& s) {
}
static bool hid_write_feature(const Settings& cfg, std::string& err) {
int fd = ::open(cfg.hidraw.c_str(), O_RDWR | O_NONBLOCK);
int fd = ::open(g_hidraw_path.c_str(), O_RDWR | O_NONBLOCK);
if (fd < 0) { err = "open hidraw failed"; return false; }
std::vector<uint8_t> bytes = {
@@ -287,7 +306,7 @@ static std::string serialize_one(const Settings& s) {
return std::format(
"SET dev={} hidraw={} effect={} bright={} speed={} dir={} r={} g={} b={} zone={}\n",
device_to_str(s.device),
s.hidraw,
g_hidraw_path,
effect_to_str(s.effect),
(int)s.brightness,
(int)s.speed,
@@ -394,6 +413,13 @@ int main() {
ensure_dirs();
g_hidraw_path = find_ene_hidraw();
if (g_hidraw_path.empty()) {
std::println("[ERR] ENE RGB controller not found in /dev/hidraw*");
return 1;
}
std::println("ENE RGB controller: {}", g_hidraw_path);
// 3 estados: keyboard/lid/button
std::array<Settings,3> states;