0

In awesome window manager, my screen brightness gets reset to 100% each time I'm away from my computer for a few minutes. How can I get it to sustain the brightness I set prior to these screen-saver/power-management timeouts?

Background:

Using autokey, I've created 3 scripts that enable me to adjust my screen's brightness with hotkeys. These scripts use the debian package brightnessctl to accomplish this:

sudo apt install brightnessctl

For the sake of being helpful to others, I will include these scripts below.

Increase Brightness:

import os
currentBrightness = system.exec_command("brightnessctl g")
brightnessLevel = str(int(currentBrightness) + 1)
if int(brightnessLevel) > 100:
    brightnessLevel = '100'
if brightnessLevel:
    cmd = "brightnessctl s " + brightnessLevel
    os.system(cmd)
    store.set_global_value("lastBrightness",brightnessLevel)

Decrease Brightness:

import os
currentBrightness = system.exec_command("brightnessctl g")
brightnessLevel = str(int(currentBrightness) - 1)
if int(brightnessLevel) < 1:
    brightnessLevel = '1'
if brightnessLevel:
    cmd = "brightnessctl s " + brightnessLevel
    os.system(cmd)
    store.set_global_value("lastBrightness",brightnessLevel)

Set Brightness via Dialog:

import os
currentBrightness = system.exec_command("brightnessctl g")
lastBrightness = store.get_global_value("lastBrightness")
if not lastBrightness:
    lastBrightness = '10'
msg = None
if (currentBrightness == lastBrightness):
    msg = 'Current Brightness = ' + currentBrightness + '\n\nSpecifiy your desired brightness (max is 100)?'
else:
    msg = 'Current Brightness = ' + currentBrightness + '\n\nYou recently changed brightness to ' + lastBrightness + ', but something else changed it since then.\n\nSpecifiy your desired brightness (max is 100)?'
os.system("numlockx on") # Turn Numlock On
#ary = dialog.input_dialog(title="Brightness", message='Specifiy desired brightness (max is 100):', default='5')
ary = dialog.input_dialog(title="Brightness", message=msg, default="10")
brightnessLevel = ary[1]
if brightnessLevel and int(brightnessLevel) > 0:
    store.set_global_value("lastBrightness",brightnessLevel)
    cmd = "brightnessctl s " + brightnessLevel
    os.system(cmd)

While these scripts are working perfectly, I do have an issue:

When I'm away from my computer for a few minutes, and then come back, my monitor will be in some type of power saving state, where the monitor has been either turned off or displays a black screen. When I wake up the monitor (by hitting a key or moving my mouse) the brightness that I previously set (using brightnessctl), has been changed back to 100% brightness.

How can I sustain my brightness settings thorough these screen-saver/power-saving timeouts?

1

2 Answers 2

1

Don't know the direct answer, but here's a workaround.

Store the desired screen brightness value in a parameter file with a suitable default value for the first time it runs.

Write a daemon script (in bash or in your language of choice) that is launched at login. Most Desktop environments have an autostart facility to run such scripts for you. (KDE and Gnome both have this feature. I don't know about AWM.)

This script will loop forever in the background with a suitable delay in each iteration to keep its resource usage low.

In each iteration, the script will read the current screen brightness and compare it to the value in the file. If they're not the same it will adjust the brightness up or down appropriately.

Modify your AutoKey scripts to use/modify the value in the file instead of using the AutoKey global stored variable.

Your script should detect when the screen is off and do nothing so it doesn't inadvertently turn the screen on.

If you want to, you can also add something your script looks for (e.g. another file or another value in its parameter file). If this is not present or true, the script will terminate. You may also want something like this so your script doesn't interfere with special screen brightness settings which might be applied by power management when your battery is low or very low.

It's probably easier though just to use pkill to get rid of it when desired.

0

Below, is a script that creates a systemd service that sustains the screen brightness level after resuming from suspend.

I created a folder called "scripts" in my home folder. Then I created a file called "SetupSustainBrightnessService.sh", with these contents:

# Create directory called "scripts" in home folder:
mkdir ~/scripts

#Go to that folder
cd ~scripts

# Create a file to store a default brightness of 50
echo "50" > default_brightness

# Use nano to save the script into a file:
nano SetupSustainBrightnessService.sh

Save the text below into the file:

#!/bin/bash

# Directory to store the brightness setting script
script_dir="$HOME/scripts"

# Create the script directory if it doesn't exist
mkdir -p $script_dir

# Create the set-brightness.sh script
cat <<EOL > $script_dir/set-brightness.sh
#!/bin/bash
brightness=\$(cat ~/scripts/default_brightness) # Replace with where you store your brightness setting
brightnessctl set \$brightness
EOL

# Make the script executable
chmod +x $script_dir/set-brightness.sh

# Create the systemd service file
sudo bash -c "cat <<EOL > /etc/systemd/system/set-brightness.service
[Unit]
Description=Set brightness after resume
After=suspend.target

[Service]
Type=simple
ExecStart=$script_dir/set-brightness.sh

[Install]
WantedBy=suspend.target
EOL"

# Reload systemd, enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable set-brightness.service
sudo systemctl start set-brightness.service

echo "Brightness restore script and systemd service have been set up."

Run the Script

# Navigate to the scripts folder
cd ~/scripts

# Make script above executable
chmod +x SetupSustainBrightnessService.sh

# Run the script
./SetupSustainBrightnessService.sh

Output from running the script:

Created symlink /etc/systemd/system/suspend.target.wants/set-brightness.service → /etc/systemd/system/set-brightness.service.
Brightness restore script and systemd service have been set up.

Now, when resuming from suspend, systemD runs this script, which sustains previous brightness: setup-brightness-restore.sh

Set default brightness with a value between 1 and 100 in this file: nano ~/scripts/default_brightness

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .