2

I use a desktop station to run an app for customers, I want to reduce the brightness after X minutes by Y% (not switching off the screen). I can't get that in my desktop. I can get the dim function working. Is there any solution ??

0

2 Answers 2

2

Script to dim the screen after x seconds

The script below will dim the screen after an arbitrary number of seconds if the computer is idle (no input from mouse or keyboard)

The script

#!/usr/bin/env python3
import subprocess
import time
import sys

# read arguments from the run command: idel time (in seconds
dimtime = int(sys.argv[1])*1000
# brightness when dimmed (between 0 and 1)
dimmed = sys.argv[2]

def get(cmd):
    # just a helper function
    return subprocess.check_output(cmd).decode("utf-8").strip()

# get the connected screens
screens = [l.split()[0] for l in get("xrandr").splitlines()
           if " connected" in l]

# initial state (idle time > set time
check1 = False

while True:
    time.sleep(2)
    # get the current idle time (millisecond)
    t = int(get("xprintidle"))
    # see if idle time exceeds set time (True/False)
    check2 = t > dimtime
    # compare with last state
    if check2 != check1:
        # if state chenges, define new brightness...
        newset = dimmed if check2 else "1"
        # ...and set it
        for scr in screens:
            subprocess.Popen([
                "xrandr", "--output", scr, "--brightness", newset
                ])
    # set current state as initial one for the next loop cycle
    check1 = check2

How to use

  1. The script needs xprintidle:

    sudo apt install xprintidle
    
  2. Copy the script into an empty file, save it as dimscreens.py
  3. Test- run it from a terminal, with the idle time and the desired brightness (dim state) as arguments:

    python3 /path/to/dimscreens.py 20 0.6
    

    where the script dims the screen after 20 seconds to 60% brightness.

  4. If all works fine, add to Startup Applications: Dash > Startup Applications > Add the command:

    /bin/bash -c "sleep 10 && python3 /path/to/dimscreens.py 20 0.6"
    

Explanation

An easy way to set screen brightness for your purpose is (e.g. 50%):

xrandr --output <screenname> --brightness 0.5

The script uses xprintidle to periodically get the current idle time, comparing it to the last cycle:

while True:
    time.sleep(2)
    t = int(get("xprintidle"))/1000
    check2 = t > dimtime

If either the time exceeds the set time or jumps back to unidle, the script takes action:

if check2 != check1:
    newset = dimmed if check2 else "1"
    for scr in screens:
        subprocess.Popen([
            "xrandr", "--output", scr, "--brightness", newset
            ])

...setting either the brightness to 1 (=100%) or the set dim-brightness.

More detailed explanation on the code is in the script.

Note

As it is, the script dims all screens . If you need to set only one screen, all is possible.

3
  • I get this error when I run it : Traceback (most recent call last): File "dimsceen.py", line 6, in <module> dimtime = int(sys.argv[1]) IndexError: list index out of range Feb 23, 2017 at 16:11
  • @user2997418 You need to set the time and the dim percentage, like in the example. The error message means you didn't :) (neither of them) python3 /path/to/dimscreens.py 20 0.6 mind the 20 0.6 Feb 23, 2017 at 16:18
  • @cl-netbox the message means the time nor dim percentage are set :) Thanks :) Feb 23, 2017 at 16:19
0

Wayland way

Thanks to @jacob-vlijm here is a modified script for Wayland users.

I use it for my odroid m1 device with touchscreen, so I only need to control one screen.

Explanation

Usage: ./dimscreen.py backlight 5 20

  • backlight - name of screen, get it from brightnessctl -l
  • 5 - seconds until idle
  • 20 - value between 0 and 255 for brigthness, see also brightnessctl -l

Old brightness value is saved and restored, not just full brightness.

You need to install package brightnessctl.

Because brightnessctl needs root privileges, set suid bit to run it as normal user:

sudo chmod u+s /usr/bin/brightnessctl

xprintidle has been replaced by dbus-send, thanks to https://askubuntu.com/a/1231995/801283

Script

#!/usr/bin/env python3
import subprocess
import time
import sys

# screen to dim
screen = sys.argv[1]
# read arguments from the run command: idel time (in seconds)
dimtime = int(sys.argv[2])*1000
# brightness when dimmed (between 0 and 255)
dimmed = sys.argv[3]

def get_cmd(cmd):
    return subprocess.Popen(["/bin/bash", "-c", cmd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

def set_cmd(cmd):
    subprocess.Popen(["/bin/bash", "-c", cmd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

get_idleTime = "dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime"
get_brightness = "brightnessctl -d " + screen + " g"
set_brightness = "brightnessctl -d " + screen + " s "

# initial state (idle time > set time)
check1 = False
oldBrightness = "255"

while True:
    time.sleep(1)
    # get the current idle time (millisecond)
    idleTime = int((get_cmd(get_idleTime)).communicate()[0].rsplit(None,1)[-1])

    # see if idle time exceeds set time (True/False)
    check2 = idleTime > dimtime
    # compare with last state
    if check2 != check1:
        # if state chenges, define new brightness...
        newBrightness = dimmed if check2 else oldBrightness
        oldBrightness = get_cmd(get_brightness).communicate()[0].rsplit(None,1)[0]
        # ...and set it
        set_cmd(set_brightness + newBrightness)
    # set current state as initial one for the next loop cycle
    check1 = check2

You must log in to answer this question.

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