不活动 X 分钟后降低亮度 - 桌面

不活动 X 分钟后降低亮度 - 桌面

我使用桌面工作站为客户运行应用程序,我想在 X 分钟后将亮度降低 Y%(不关闭屏幕)。我无法在桌面上实现这一点。我可以让暗淡功能正常工作。有什么解决办法吗?

答案1

在 x 秒后使屏幕变暗的脚本

如果计算机处于空闲状态(没有鼠标或键盘的输入),下面的脚本将在任意秒数后使屏幕变暗

剧本

#!/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

如何使用

  1. 该脚本需要xprintidle

    sudo apt install xprintidle
    
  2. 将脚本复制到一个空文件中,另存为dimscreens.py
  3. 测试——从终端运行它,使用空闲时间和所需亮度(暗淡状态)作为参数:

    python3 /path/to/dimscreens.py 20 0.6
    

    脚本会在 20 秒后将屏幕亮度调暗至 60%。

  4. 如果一切正常,添加到启​​动应用程序:Dash>启动应用程序>添加命令:

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

解释

一个简单的方法来设置屏幕亮度为您的目的(例如 50%):

xrandr --output <screenname> --brightness 0.5

该脚本使用打印空闲定期获取当前空闲时间,并将其与上一个周期进行比较:

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

如果时间超过设定时间或者跳回unidle,脚本采取行动:

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

...将亮度设置为 1 (=100%) 或设置暗亮度。

有关代码的更详细解释在脚本中。

笔记

事实上,脚本会使所有屏幕变暗。如果您只需要设置一个屏幕,那么所有屏幕都是可能的。

答案2

Wayland 方式

感谢@jacob-vlijm,这里有一个针对 Wayland 用户修改的脚本。

我将它用于带触摸屏的 odroid m1 设备,因此我只需要控制一个屏幕。

解释

用法:./dimscreen.py backlight 5 20

  • 背光-屏幕名称,从中获取brightnessctl -l
  • 5 秒后空闲
  • 20 - 亮度值介于 0 和 255 之间,另请参阅brightnessctl -l

保存并恢复旧的亮度值,而不仅仅是全亮度。

您需要安装包brightnessctl

由于 brightctl 需要 root 权限,因此设置 suid 位以以普通用户身份运行它:

sudo chmod u+s /usr/bin/brightnessctl

xprintidle已被取代dbus-send,感谢https://askubuntu.com/a/1231995/801283

脚本

#!/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

相关内容