Fix led indicator getting turned off when turning caps lock/numlock on/off

This commit is contained in:
dec05eba
2025-11-20 21:50:20 +01:00
parent b875f96885
commit 5a13bd2491
2 changed files with 83 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include <sys/types.h>
#include <vector>
#include <mglpp/system/Clock.hpp>
namespace gsr {
@@ -17,11 +18,16 @@ namespace gsr {
private:
bool run_gsr_global_hotkeys_set_leds(bool enabled);
void update_led(bool new_state);
void update_led_with_active_status();
void check_led_status_outdated();
private:
pid_t gsr_global_hotkeys_pid = -1;
bool led_indicator_on = false;
bool led_enabled = false;
bool perform_blink = false;
mgl::Clock blink_timer;
std::vector<int> led_brightness_files;
mgl::Clock read_led_brightness_timer;
};
}

View File

@@ -1,16 +1,64 @@
#include "../include/LedIndicator.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// TODO: Support hotplug for led indicator check (led_brightness_files)
namespace gsr {
static bool string_starts_with(const char *str, const char *sub) {
const int str_len = strlen(str);
const int sub_len = strlen(sub);
return str_len >= sub_len && memcmp(str, sub, sub_len) == 0;
}
static bool string_ends_with(const char *str, const char *sub) {
const int str_len = strlen(str);
const int sub_len = strlen(sub);
return str_len >= sub_len && memcmp(str + str_len - sub_len, sub, sub_len) == 0;
}
static std::vector<int> open_device_leds_brightness_files(const char *led_name_path) {
std::vector<int> files;
DIR *dir = opendir("/sys/class/leds");
if(!dir)
return files;
char brightness_filepath[1024];
struct dirent *entry;
while((entry = readdir(dir)) != NULL) {
if(entry->d_name[0] == '.')
continue;
if(!string_starts_with(entry->d_name, "input") || !string_ends_with(entry->d_name, led_name_path))
continue;
snprintf(brightness_filepath, sizeof(brightness_filepath), "/sys/class/leds/%s/brightness", entry->d_name);
const int led_brightness_file_fd = open(brightness_filepath, O_RDONLY | O_NONBLOCK);
if(led_brightness_file_fd > 0)
files.push_back(led_brightness_file_fd);
}
closedir(dir);
return files;
}
LedIndicator::LedIndicator() {
led_brightness_files = open_device_leds_brightness_files("scrolllock");
run_gsr_global_hotkeys_set_leds(false);
}
LedIndicator::~LedIndicator() {
for(int led_brightness_file_fd : led_brightness_files) {
close(led_brightness_file_fd);
}
run_gsr_global_hotkeys_set_leds(false);
if(gsr_global_hotkeys_pid > 0) {
int status;
@@ -73,6 +121,11 @@ namespace gsr {
}
void LedIndicator::update() {
update_led_with_active_status();
check_led_status_outdated();
}
void LedIndicator::update_led_with_active_status() {
if(perform_blink) {
const double blink_elapsed_sec = blink_timer.get_elapsed_time_seconds();
if(blink_elapsed_sec < 0.2) {
@@ -90,4 +143,28 @@ namespace gsr {
update_led(led_enabled);
}
}
void LedIndicator::check_led_status_outdated() {
// The display server will unset our scroll lock led when pressing capslock/numlock as it updates
// all leds at the same time (not just the button pressed) (or at least xorg server does that).
// When that is done we want to set the scroll lock led on again if it should be on.
// TODO: Improve this. Dont do this with a timer.. but inotify doesn't work sysfs. netlink should work (man 7 netlink).
if(read_led_brightness_timer.get_elapsed_time_seconds() > 0.2) {
read_led_brightness_timer.restart();
bool led_status_outdated = false;
char buffer[32];
for(int led_brightness_file_fd : led_brightness_files) {
const ssize_t bytes_read = read(led_brightness_file_fd, buffer, sizeof(buffer));
if(bytes_read > 0) {
if(buffer[0] == '0')
led_status_outdated = true;
lseek(led_brightness_file_fd, 0, SEEK_SET);
}
}
if(led_status_outdated && led_enabled)
run_gsr_global_hotkeys_set_leds(true);
}
}
}