Improvements to keyboard backlight.

This commit is contained in:
ikolokolnikov committed 2026-09-26 19:41:31 -07:00
1 parent c47f7668df
commit a6905ebe4f
1 file changed
+59 -54
+59 -54
View File
@@ -191,6 +191,7 @@ void update_crtc_infos(Display *dpy)
void set_screen_on(Display *dpy) void set_screen_on(Display *dpy)
{ {
#if !DEBUG
if(crtcres) { if(crtcres) {
int i; int i;
for(i = 0; i < crtcres->ncrtc; i++) { for(i = 0; i < crtcres->ncrtc; i++) {
@@ -206,10 +207,12 @@ void set_screen_on(Display *dpy)
info->noutput); info->noutput);
} }
} }
#endif
} }
void set_screen_off(Display *dpy) void set_screen_off(Display *dpy)
{ {
#if !DEBUG
if(crtcres) { if(crtcres) {
int i; int i;
for(i = 0; i < crtcres->ncrtc; i++) { for(i = 0; i < crtcres->ncrtc; i++) {
@@ -229,6 +232,7 @@ void set_screen_off(Display *dpy)
} }
} }
} }
#endif
} }
static void static void
@@ -627,43 +631,24 @@ static void dpms_enable(Display *dpy, struct dpms_state *state)
} }
} }
struct keyboard_backlight_state { struct keyboard_backlight {
int available; int available;
int brightness;
int fd; int fd;
}; };
static struct keyboard_backlight_state get_keyboard_backlight_state() static int
get_keyboard_backlight_brightness(struct keyboard_backlight *kbl)
{ {
if(kbl->available) {
static char bl_entry[1024];
char *leds;
DIR *d;
struct keyboard_backlight_state bl_state;
memset(&bl_state, 0, sizeof(bl_state));
leds = "/sys/class/leds";
d = opendir(leds);
if(d) {
struct dirent *entry;
while((entry = readdir(d)) != 0) {
if(str_contains(entry->d_name, "kbd_backlight")) {
static char read_buf[32];
int fd;
snprintf(bl_entry, 1024,
"%s/%s/brightness", leds, entry->d_name);
fd = open(bl_entry, O_RDWR);
if(fd != -1) {
int read_amnt, s; int read_amnt, s;
char *endptr;
long num; long num;
static char read_buf[32];
char *endptr;
read_amnt = 0; read_amnt = 0;
do { do {
s = read(fd, read_buf+read_amnt, 32-read_amnt); s = read(kbl->fd, read_buf+read_amnt, 32-read_amnt);
if(s == -1) if(s == -1)
break; break;
read_amnt += s; read_amnt += s;
@@ -674,12 +659,39 @@ static struct keyboard_backlight_state get_keyboard_backlight_state()
num = strtol(read_buf, &endptr, 10); num = strtol(read_buf, &endptr, 10);
if(!(errno == ERANGE || endptr == read_buf)) { if(!(errno == ERANGE || endptr == read_buf)) {
bl_state.available = 1; return (int)num;
bl_state.fd = fd;
bl_state.brightness = num;
} }
} }
} }
return 0;
}
static struct keyboard_backlight init_keyboard_backlight()
{
static char bl_entry[1024];
char *leds;
DIR *d;
struct keyboard_backlight kbl;
memset(&kbl, 0, sizeof(kbl));
leds = "/sys/class/leds";
d = opendir(leds);
if(d) {
struct dirent *entry;
while((entry = readdir(d)) != 0) {
if(str_contains(entry->d_name, "kbd_backlight")) {
int fd;
snprintf(bl_entry, 1024,
"%s/%s/brightness", leds, entry->d_name);
fd = open(bl_entry, O_RDWR);
if(fd != -1) {
kbl.available = 1;
kbl.fd = fd;
}
break; break;
} }
@@ -687,27 +699,20 @@ static struct keyboard_backlight_state get_keyboard_backlight_state()
closedir(d); closedir(d);
} }
return bl_state; return kbl;
} }
static void static void
reset_keyboard_backlight(struct keyboard_backlight_state *bl_state) set_keyboard_backlight(struct keyboard_backlight *kbl,
int brightness)
{ {
#if !DEBUG
static char buf[512]; static char buf[512];
if(bl_state->available) { if(kbl->available) {
snprintf(buf, sizeof(buf), "%d\n", bl_state->brightness); snprintf(buf, sizeof(buf), "%d\n", brightness);
write(bl_state->fd, buf, str_len(buf)); write(kbl->fd, buf, str_len(buf));
}
}
static void
set_keyboard_backlight_off(struct keyboard_backlight_state *bl_state)
{
if(bl_state->available) {
char buf[512];
snprintf(buf, sizeof(buf), "%d\n", 0);
write(bl_state->fd, buf, str_len(buf));
} }
#endif
} }
static void update_timerfd(int tfd) static void update_timerfd(int tfd)
@@ -728,9 +733,9 @@ static void update_timerfd(int tfd)
} }
static void static void
readpw(struct arena *arena, run_loop(struct arena *arena,
Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
const char *hash, struct keyboard_backlight_state *bl_state) const char *hash, struct keyboard_backlight *kbl)
{ {
XRRScreenChangeNotifyEvent *rre; XRRScreenChangeNotifyEvent *rre;
char buf[32], passwd[256], *inputhash; char buf[32], passwd[256], *inputhash;
@@ -743,6 +748,7 @@ readpw(struct arena *arena,
unsigned long long rendering_timeout_millis; unsigned long long rendering_timeout_millis;
struct dpms_state dpms_state; struct dpms_state dpms_state;
struct fonts fonts; struct fonts fonts;
int kbl_brightness;
int error; int error;
int needs_rendering; int needs_rendering;
@@ -760,8 +766,8 @@ readpw(struct arena *arena,
tfd = timerfd_create(CLOCK_REALTIME, 0); tfd = timerfd_create(CLOCK_REALTIME, 0);
update_crtc_infos(dpy); update_crtc_infos(dpy);
dpms_disable(dpy, &dpms_state); dpms_disable(dpy, &dpms_state);
kbl_brightness = get_keyboard_backlight_brightness(kbl);
if(!load_fonts(&fonts)) if(!load_fonts(&fonts))
die("slock: load_fonts\n"); die("slock: load_fonts\n");
@@ -910,10 +916,10 @@ readpw(struct arena *arena,
if(dson_saved != dson) { if(dson_saved != dson) {
if(dson) { if(dson) {
reset_keyboard_backlight(bl_state); set_keyboard_backlight(kbl, kbl_brightness);
set_screen_on(dpy); set_screen_on(dpy);
} else { } else {
set_keyboard_backlight_off(bl_state); set_keyboard_backlight(kbl, 0);
set_screen_off(dpy); set_screen_off(dpy);
} }
} }
@@ -945,7 +951,7 @@ readpw(struct arena *arena,
if(ret == 0) { if(ret == 0) {
dson = 0; dson = 0;
set_screen_off(dpy); set_screen_off(dpy);
set_keyboard_backlight_off(bl_state); set_keyboard_backlight(kbl, 0);
} else if(ret > 0 && FD_ISSET(xfd, &fds)) { } else if(ret > 0 && FD_ISSET(xfd, &fds)) {
} else if(ret > 0 && FD_ISSET(tfd, &fds)) { } else if(ret > 0 && FD_ISSET(tfd, &fds)) {
} }
@@ -1121,8 +1127,7 @@ main(int argc, char **argv) {
if (!DPMSQueryExtension(dpy, &event_base, &error_base)) if (!DPMSQueryExtension(dpy, &event_base, &error_base))
die("slock: dpms extension is not supported\n"); die("slock: dpms extension is not supported\n");
struct keyboard_backlight_state bl_state = struct keyboard_backlight kbl = init_keyboard_backlight();
get_keyboard_backlight_state();
/* drop privileges */ /* drop privileges */
if (setgroups(0, NULL) < 0) if (setgroups(0, NULL) < 0)
@@ -1164,7 +1169,7 @@ main(int argc, char **argv) {
/* everything is now blank. Wait for the correct password */ /* everything is now blank. Wait for the correct password */
struct arena arena = arena_init(bf, GB(1)); struct arena arena = arena_init(bf, GB(1));
readpw(&arena, dpy, &rr, locks, nscreens, hash, &bl_state); run_loop(&arena, dpy, &rr, locks, nscreens, hash, &kbl);
return 0; return 0;
} }