You are not logged in.

#1 2022-04-13 09:14:27

carlo711
Member
Registered: 2022-02-16
Posts: 8

How to turn off keyboard backlight when screen gets locked (dwm&slock)

Hello,

I am using dwm and slock as lockscreen. Everthing works fine but, it bothers me that when the screen is off the keyboard backlight is still on.
To control the keyboard backlight iI use brightness control, since xset is not working at all:(
Btw I just learned that xset led does not work on macbooks at all
So as suggested in the wiki I created a slock service which gets executed on suspend, when is I press the power button. To turn the keyboard backlight off I added the second ExecStartPre line.

slock@.service
[Unit]
Description=Lock X session using slock for user %i
Before=sleep.target

[Service]
User=%i
Environment=DISPLAY=:0
ExecStartPre=/usr/bin/xset dpms force suspend
ExecStartPre=/usr/bin/brightnessctl --device='smc::kbd_backlight' s 0 -q
ExecStart=/usr/local/bin/slock

[Install]
WantedBy=sleep.target

However when it gets executed the service fails because of brightnessctl with the error:

× slock@carlo711.service - Lock X session using slock for user carlo711
     Loaded: loaded (/etc/systemd/system/slock@.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Wed 2022-04-13 11:08:55 CEST; 26s ago
    Process: 1815 ExecStartPre=/usr/bin/xset dpms force suspend (code=exited, status=0/SUCCESS)
    Process: 1816 ExecStartPre=/usr/bin/brightnessctl --device=smc::kbd_backlight s 0 -q (code=exited, status=1/FAILURE)
        CPU: 8ms

Apr 13 11:08:55 macbookpro systemd[1]: Starting Lock X session using slock for user carlo711...
Apr 13 11:08:55 macbookpro brightnessctl[1816]: Failed to set brightness: Invalid request descriptor
Apr 13 11:08:55 macbookpro systemd[1]: slock@carlo711.service: Control process exited, code=exited, status=1/FAILURE
Apr 13 11:08:55 macbookpro systemd[1]: slock@carlo711.service: Failed with result 'exit-code'.
Apr 13 11:08:55 macbookpro systemd[1]: Failed to start Lock X session using slock for user carlo711.

Has anyone an idea how to fix this?

Last edited by carlo711 (2022-04-13 09:31:08)

Offline

#2 2022-04-13 12:58:47

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,314

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

I'd try to prepend the "-q" as the syntax is listed as "brightnessctl [options] [operation] [value...]"
https://man.archlinux.org/man/community … n#SYNOPSIS

Offline

#3 2022-04-13 14:23:58

carlo711
Member
Registered: 2022-02-16
Posts: 8

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

I just tried it, with -q before --device and after, even without but it still fails even doing a systemctl daemon-reload after every change. And the error is always the same. Maybe I should try this?

echo 0 > /sys/class/leds/smc::kbd_backlight/brightness

Does not work after the suspend the keyboard backlight shuts off but as soon as the service starts it is back at the brightness before, even it should have been written to 0.

○ slock@carlo711.service - Lock X session using slock for user carlo711
     Loaded: loaded (/etc/systemd/system/slock@.service; enabled; vendor preset: disabled)
     Active: inactive (dead)

Apr 13 16:25:38 macbookpro systemd[1]: Started Lock X session using slock for user carlo711.
Apr 13 16:26:08 macbookpro systemd[1]: slock@carlo711.service: Deactivated successfully.
Apr 13 16:26:21 macbookpro systemd[1]: Starting Lock X session using slock for user carlo711...
Apr 13 16:26:22 macbookpro echo[10849]: 0 > /sys/class/leds/smc::kbd_backlight/brightness
Apr 13 16:26:22 macbookpro systemd[1]: Started Lock X session using slock for user carlo711.
Apr 13 16:26:32 macbookpro systemd[1]: slock@carlo711.service: Deactivated successfully.
Apr 13 16:26:53 macbookpro systemd[1]: Starting Lock X session using slock for user carlo711...
Apr 13 16:26:53 macbookpro echo[10897]: 0 > /sys/class/leds/smc::kbd_backlight/brightness
Apr 13 16:26:53 macbookpro systemd[1]: Started Lock X session using slock for user carlo711.
Apr 13 16:27:05 macbookpro systemd[1]: slock@carlo711.service: Deactivated successfully.

Last edited by carlo711 (2022-04-13 14:29:09)

Offline

#4 2022-04-13 15:24:03

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,314

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

after the suspend the keyboard backlight shuts off but as soon as the service starts it is back at the brightness before

The slock service? What triggers the backlight change? The ExecStartPre in that service might simply hit too early (eg. if slock causes the backlight change for whatever reason)

ExecStart=/usr/local/bin/slock /bin/bash -c "sleep 1; echo 0 > /sys/class/leds/smc::kbd_backlight/brightness"

?

Offline

#5 2022-04-13 16:57:18

carlo711
Member
Registered: 2022-02-16
Posts: 8

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

I tried it and got the error that I don't have permission

Apr 13 18:54:37 macbookpro systemd[1]: Started Lock X session using slock for user carlo711.
Apr 13 18:54:43 macbookpro slock[1071]: /bin/bash: line 1: /sys/class/leds/smc::kbd_backlight/brightness: Permission denied
Apr 13 18:54:49 macbookpro systemd[1]: slock@carlo711.service: Deactivated successfully.
Apr 13 18:55:27 macbookpro systemd[1]: Started Lock X session using slock for user carlo711.
Apr 13 18:55:32 macbookpro slock[1126]: /bin/bash: line 1: /sys/class/leds/smc::kbd_backlight/brightness: Permission denied
Apr 13 18:55:35 macbookpro systemd[1]: slock@carlo711.service: Deactivated successfully.

I can do sudo in the terminal but even then it says permission denied.
Btw I am using wheel.

Offline

#6 2022-04-13 17:03:55

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,314

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

"sudo echo" won't work, you'd have to sudo the bash.
This could also explain the other errors since afaics ExecStartPre should™ run as User as well… (in which case sudo'ing brightnessctl or moving this away and into a system service might be the preferable approach)

Offline

#7 2022-04-13 17:27:21

carlo711
Member
Registered: 2022-02-16
Posts: 8

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

Ok ill look into it and by service you mean a service for turning off the kbd backlight? However in userspace brightness control does not require sudo so it should just work.
Btw I am using slock with the dpms patch so theoretically it should be possible to add a line of code in c to turn the keyboard light off, when the monitor turns off. But currently I am not that far into c.

Last edited by carlo711 (2022-04-13 17:29:01)

Offline

#8 2022-04-13 17:38:38

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,314

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

You dont need to patch slock - it executes commands anyway, there's the ExecStartPost and if brightnessctl has sufficient permissions in this context, I'd go w/ that.

ExecStart=/bin/slock /bin/bash -c "sleep 1; brightnessctl -q --device='smc::kbd_backlight' s 0"

(the backlight device might not be available immediately after the S3, causing the original error - so delaying the call a bit might help)

Offline

#9 2022-04-13 17:49:14

carlo711
Member
Registered: 2022-02-16
Posts: 8

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

Tried it but it just thrown the save error. So currently I am using the powerbutton to lock so I use the acpi event power/button to turn off the backlight which works fine. But turned off they wont turn on automatically again so is there any way to turn them on again like execpost or some acpi event?
And are there any possibilitys to bind the on/off state of the screen to the keyboard backlight?(maybe some service?)

Last edited by carlo711 (2022-04-13 17:52:36)

Offline

#10 2022-04-14 07:21:59

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,314

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

slock doesn't fork, so ExecStartPost will fire after you unlocked the screen.
There's no DPMS event, you'd have to continuosly poll the state, https://www.x.org/releases/X11R7.7/doc/ … l#DPMSInfo or "xset q | grep 'Monitor is'" - neither is a great idea sad

Offline

#11 2023-05-12 07:30:10

nkoq
Member
Registered: 2023-05-12
Posts: 1

Re: How to turn off keyboard backlight when screen gets locked (dwm&slock)

I had a similar problem. I was trying to run brightnessctl from cron (with cronie). The error was: "invalid request" and after some searching I realized that this is an error code, namely EBADR. Some head scratching ensued. Then I looked at the source code of brightnessctl and found this: https://github.com/Hummer12007/brightne … ermissions

I edited sudoers to allow sudo without passwords for brightnessctl with:

your_cron_user ALL = (root) NOPASSWD: /usr/bin/brightnessctl

and prepended sudo before brightnessctl and the problem got solved.

I hope this is useful to others!

Offline

Board footer

Powered by FluxBB