fixed fragile code
This commit is contained in:
Binary file not shown.
@@ -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_DIR = "/var/lib/acer-rgbd";
|
||||||
static constexpr const char* STATE_PATH = "/var/lib/acer-rgbd/state.txt";
|
static constexpr const char* STATE_PATH = "/var/lib/acer-rgbd/state.txt";
|
||||||
|
|
||||||
|
static std::string g_hidraw_path;
|
||||||
|
|
||||||
struct Settings {
|
struct Settings {
|
||||||
std::string hidraw = "/dev/hidraw2";
|
std::string hidraw = "/dev/hidraw2";
|
||||||
uint8_t device = KEYBOARD_RGB_ID;
|
uint8_t device = KEYBOARD_RGB_ID;
|
||||||
@@ -84,6 +86,23 @@ static std::optional<std::string> read_file(const std::string& path) {
|
|||||||
return out;
|
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) {
|
static std::vector<std::string> split_ws(std::string_view s) {
|
||||||
std::vector<std::string> v;
|
std::vector<std::string> v;
|
||||||
size_t i = 0;
|
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) {
|
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; }
|
if (fd < 0) { err = "open hidraw failed"; return false; }
|
||||||
|
|
||||||
std::vector<uint8_t> bytes = {
|
std::vector<uint8_t> bytes = {
|
||||||
@@ -287,7 +306,7 @@ static std::string serialize_one(const Settings& s) {
|
|||||||
return std::format(
|
return std::format(
|
||||||
"SET dev={} hidraw={} effect={} bright={} speed={} dir={} r={} g={} b={} zone={}\n",
|
"SET dev={} hidraw={} effect={} bright={} speed={} dir={} r={} g={} b={} zone={}\n",
|
||||||
device_to_str(s.device),
|
device_to_str(s.device),
|
||||||
s.hidraw,
|
g_hidraw_path,
|
||||||
effect_to_str(s.effect),
|
effect_to_str(s.effect),
|
||||||
(int)s.brightness,
|
(int)s.brightness,
|
||||||
(int)s.speed,
|
(int)s.speed,
|
||||||
@@ -394,6 +413,13 @@ int main() {
|
|||||||
|
|
||||||
ensure_dirs();
|
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
|
// 3 estados: keyboard/lid/button
|
||||||
std::array<Settings,3> states;
|
std::array<Settings,3> states;
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class RGBState:
|
|||||||
|
|
||||||
def to_command(self) -> str:
|
def to_command(self) -> str:
|
||||||
return (
|
return (
|
||||||
f"SET dev={self.device} hidraw={self.hidraw} "
|
f"SET dev={self.device} "
|
||||||
f"effect={self.effect} bright={self.brightness} "
|
f"effect={self.effect} bright={self.brightness} "
|
||||||
f"speed={self.speed} dir={self.direction} "
|
f"speed={self.speed} dir={self.direction} "
|
||||||
f"r={self.r} g={self.g} b={self.b} zone={self.zone}"
|
f"r={self.r} g={self.g} b={self.b} zone={self.zone}"
|
||||||
|
|||||||
Reference in New Issue
Block a user