在很棒的窗口管理器,每当我离开计算机几分钟时,我的屏幕亮度就会重置为 100%。如何让它维持在屏幕保护程序/电源管理超时之前设置的亮度?
背景:
使用自动键,我创建了 3 个脚本,使我能够使用热键调整屏幕亮度。这些脚本使用 debian 包brightnessctl
来完成此操作:
sudo apt install brightnessctl
为了对其他人有帮助,我将在下面包含这些脚本。
增加亮度:
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)
降低亮度:
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)
通过对话框设置亮度:
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)
虽然这些脚本运行良好,但我确实有一个问题:
当我离开计算机几分钟然后回来时,我的显示器将处于某种类型的省电状态,显示器已关闭或显示黑屏。当我唤醒显示器(通过按键或移动鼠标)时,我之前设置的亮度(使用brightnessctl
)已更改回 100% 亮度。
如何在这些屏幕保护程序/省电超时期间维持我的亮度设置?
答案1
不知道直接答案,但这里有一个解决方法。
将所需的屏幕亮度值存储在参数文件中,并在第一次运行时使用合适的默认值。
编写一个在登录时启动的守护程序脚本(使用 bash 或您选择的语言)。大多数桌面环境都有一个自动启动工具来为您运行此类脚本。 (KDE和Gnome都有这个功能,AWM我不知道。)
该脚本将在后台永远循环,并在每次迭代中都有适当的延迟,以保持较低的资源使用率。
在每次迭代中,脚本将读取当前屏幕亮度并将其与文件中的值进行比较。如果它们不同,它将适当地向上或向下调整亮度。
修改 AutoKey 脚本以使用/修改文件中的值,而不是使用 AutoKey 全局存储变量。
您的脚本应该检测屏幕何时关闭并且不执行任何操作,这样它就不会无意中打开屏幕。
如果您愿意,您还可以添加脚本查找的内容(例如另一个文件或其参数文件中的另一个值)。如果该值不存在或为真,则脚本将终止。您可能还需要类似的东西,这样您的脚本就不会干扰特殊的屏幕亮度设置,当电池电量低或非常低时,电源管理可能会应用这些设置。
不过,在需要时使用 pkill 来删除它可能更容易。
答案2
下面是一个创建 systemd 服务的脚本,该服务在从挂起状态恢复后维持屏幕亮度级别。
我在我的主文件夹中创建了一个名为“scripts”的文件夹。然后我创建了一个名为“SetupSustainBrightnessService.sh”的文件,其中包含以下内容:
# 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
将以下文本保存到文件中:
#!/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."
运行脚本
# Navigate to the scripts folder
cd ~/scripts
# Make script above executable
chmod +x SetupSustainBrightnessService.sh
# Run the script
./SetupSustainBrightnessService.sh
运行脚本的输出:
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.
现在,当从挂起状态恢复时,systemD 运行此脚本,该脚本可维持之前的亮度:setup-brightness-restore.sh
在此文件中设置默认亮度,其值介于 1 到 100 之间:nano ~/scripts/default_brightness