根据 16.04 LTS 上的电源更新亮度级别

根据 16.04 LTS 上的电源更新亮度级别

我最近将我的东芝 L645 笔记本电脑从 32 位 14.04 LTS 迁移到 64 位 16.04 LTS。在 14.04 LTS 系统中,我有一个脚本可以根据电源自动更新亮度级别。不幸的是,在覆盖系统之前我没有保存该脚本。目前,我正在使用以下脚本

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: February 26 2016 
# Purpose: Brightness control that polls for
#          ac adapter presence. Uses
# Dependencies: on_ac_power script, dbus, Unity/Gnome 
# Written for: http://askubuntu.com/q/739617/295286
# Tested on: Ubuntu 14.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

# uncomment the line bellow for debugging
#set -x

ARGV0="$0"
ARGC=$#


main()
{

  # defaults
  local DISPLAY=:0
  local DECREASE=30
  local INCREASE=75
  local RCFILE="$HOME/.auto-backlightrc"
  #---

  # Check the settings
  if [ -f $RCFILE ]
  then 
       source $RCFILE 
  else
       create_rcfile $DECREASE $INCREASE
  fi
  #---

  # now actually test if we're using ac adapter
  if ! on_ac_power 
  then 
        change_brightness $DECREASE
  # The two lines bellow are optional for 
  # setting brightness if on AC. remove # 
  # if you want to use these two

  # else 
       # change_brightness $INCREASE
  fi

}

change_brightness()
{
  dbus-send --session --print-reply\
    --dest=org.gnome.SettingsDaemon.Power\
    /org/gnome/SettingsDaemon/Power \
    org.gnome.SettingsDaemon.Power.Screen.SetPercentage uint32:"$1"
}

create_rcfile()
{
  echo "DECREASE="$1 >  "$RCFILE"
  echo "INCREASE="$2 >> "$RCFILE"
}


while true
do
   main
   sleep 0.25
done

但是,此脚本仅在电源从交流电切换到电池供电时才有效,并且交流电恢复后不会恢复亮度级别。此外,一旦使用电池供电,此脚本就会始终尝试将亮度设置为预定义的级别,即使我尝试手动更改亮度,它也会重置亮度。我希望即使在电池模式下也能手动更改亮度级别。

答案1

介绍

下面的脚本可以根据笔记本电脑使用的电源记住亮度级别。电池默认亮度为 50%,交流电默认亮度为 90%。

选项和用法概述

source_monitor.sh [-a INT] [-b INT] [-v] [-h]

-a set initial brightness on AC adapter
-b set initial brightness on batter
-v enable verbose output
-h prints this help text

安装

git通过终端安装:

  1. 运行sudo apt-get install git安装git
  2. 运行。如果已存在,mkdir $HOME/bin则跳过此步骤$HOME/bin
  3. cd $HOME/bin
  4. 跑步git clone https://github.com/SergKolo/sergrep.git
  5. 该脚本将位于$HOME/bin/sergrep/source_monitor.sh。确保脚本可以使用chmod +x $HOME/bin/sergrep/source_monitor.sh
  6. 将脚本添加为启动应用程序。在 Unity Dash 或 Gnome 搜索中查找启动应用程序菜单。或者,gnome-session-properties在终端中运行命令以启动菜单。将脚本的完整路径添加为启动应用程序,以便每次登录 GUI 时都会启动它。

或者,您可以自行复制并保存脚本源,chmod +x file并执行上面描述的步骤#6。

要使脚本在每次登录 Gnome 或 Unity 时自动启动,请使用启动应用程序公用事业。

脚本源

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: June 18th 2016
# Purpose: Script that remembers and sets brightness
#      depending on power sources
# 
# Written for: https://askubuntu.com/q/788383/295286
# Tested on: Ubuntu 16.04 LTS , Ubuntu Kylin 16.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

ARGV0="$0"
ARGC=$#
wait_ac_connect()
{
  while ! on_ac_power ; do : ; sleep 0.25 ; done
  $VERBOSE && echo "<<< adapter plugged in"
}

wait_ac_disconnect()
{
  while on_ac_power ; do : ; sleep 1.25 ; done 
  $VERBOSE && echo "<<< adapter unplugged"
}

change_brightness()
{
  qdbus org.gnome.SettingsDaemon \
       /org/gnome/SettingsDaemon/Power \
      org.gnome.SettingsDaemon.Power.Screen.SetPercentage "$1"
}

get_brightness()
{
  qdbus org.gnome.SettingsDaemon \
        /org/gnome/SettingsDaemon/Power \
        org.gnome.SettingsDaemon.Power.Screen.GetPercentage
}

print_usage()
{
cat <<EOF

source_monitor.sh [-a INT] [-b INT] [-v] [-h]

-a set initial brightness on AC adapter
-b set initial brightness on batter
-v enable verbose output
-h prints this help text

Copyright Serg Kolo , 2016
EOF
}

parse_args()
{
 # boiler-pate code for reusing, options may change
 local OPTIND opt  # OPTIND must be there, 
                   # opt can be renamed to anything
 # no leading : means errors reported(which is what i want)
 # : after letter means options takes args, no :  - no args
 while getopts "a:b:vh" opt
 do
   case ${opt} in
      a)  AC_PERCENTAGE="${OPTARG}"
        ;;
      b) BAT_PERCENTAGE="${OPTARG}"
        ;;
      v) VERBOSE=true
        ;;
      h) print_usage && exit 0
        ;;
     \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
    esac
  done
  shift $((OPTIND-1))
}

main()
{

    # default values, if -a,-b, or -v options given
    # they will be changed
    local BAT_PERCENTAGE=50
    local AC_PERCENTAGE=90
    local VERBOSE=false # for debugging

    parse_args "$@"

    while true
    do

      if on_ac_power ; then
         wait_ac_disconnect
          AC_PERCENTAGE=$(($(get_brightness)+1)) # too long to explain why +1
                         # just try it for yourself
          sleep 0.25
          change_brightness "$BAT_PERCENTAGE" > /dev/null
      else
          wait_ac_connect
          BAT_PERCENTAGE=$(($(get_brightness)+1))
          sleep 0.25
          change_brightness "$AC_PERCENTAGE" > /dev/null
      fi

      sleep 0.25

    done

}

main "$@"

答案2

Serg 的脚本起初似乎有效。然而过了一段时间,百分比被错误地评估了,特别是在从休眠状态恢复后。这可能是一些问题,qdbus但亮度级别不会改变。所以,我决定使用文件中的值对亮度级别进行硬编码max_brightness。这是我的/usr/local/bin/auto-backlight.sh

#!/usr/bin/env bash

read MAX_BRIGHTNESS < /sys/class/backlight/intel_backlight/max_brightness

declare -i -r BATT_PERCENTAGE=45 
declare -i -r AC_PERCENTAGE=90 
declare -i -r ON_BATT=$(($MAX_BRIGHTNESS*$BATT_PERCENTAGE/100)) 
declare -i -r ON_AC=$(($MAX_BRIGHTNESS*$AC_PERCENTAGE/100))

wait_ac_connect() {   while ! on_ac_power ; do : ; sleep 0.25 ; done   echo "Adapter plugged in. Brightness level at $ON_AC" }

wait_ac_disconnect() {   while on_ac_power ; do : ; sleep 1.25 ; done  echo "Running on battery. Brightness level at $ON_BATT" }

main() {  
    while true
    do

      if on_ac_power ; then
        wait_ac_disconnect
         echo $ON_BATT > /sys/class/backlight/intel_backlight/brightness
      else
        wait_ac_connect
         echo $ON_AC > /sys/class/backlight/intel_backlight/brightness
      fi

      sleep 0.25

    done

}

main "$@"

与 Serg 的脚本不同,此脚本需要 root 权限才能写入brightness文件。因此我创建了一个systemd serviceat /etc/systemd/system/auto-backlight.service

[Unit]
Description=Change backlight on power source
ConditionFileIsExecutable=/usr/local/bin/auto-backlight.sh

[Service]
Type=simple
ExecStart=/usr/local/bin/auto-backlight.sh

[Install]
WantedBy=multi-user.target

最后让服务以 root 权限在启动时加载:

sudo systemctl enable auto-backlight.service

答案3

另一种方法是创建 udev 规则并调用一个简单的脚本来改变亮度值:首先,auto-backlight.sh用您最喜欢的编辑器(如 gedit)在您的主目录中创建一个名为的文件(或您喜欢的任何其他文件),然后复制并粘贴下一个代码:

#!/bin/sh
# Adjust brightness of backlights based on power source
case $1 in
    # On battery
    true)
        # Dim screen backlight
        expr `cat /sys/class/backlight/intel_backlight/max_brightness` / 10 > \
            /sys/class/backlight/intel_backlight/brightness
    ;;

    # On AC
    false)
        # Dim screen backlight
        cat /sys/class/backlight/intel_backlight/max_brightness > \
            /sys/class/backlight/intel_backlight/brightness
    ;;
esac

return 0

请注意,在您的系统中,这/sys/class/backlight/intel_backlight/可能会有所不同,例如/sys/class/backlight/acpi_video0/。还请注意,您可能需要/ 10根据的值更改的值max_brightness,它可能是 100、50、5 等,因为它是一个除法因子。

赋予新创建的脚本执行权限: chmod 771 auto-backlight.sh

然后用您喜欢的编辑器创建一个名为的文件99auto-backlight.rules并将其放在/etc/udev/rules.d/文件夹中:(sudo gedit /etc/udev/rules.d/99auto-backlight.rules或使用“ln”命令链接它),包含以下两行:

SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/path/to/your/script/auto-backlight.sh true"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/path/to/your/script/auto-backlight.sh false"

替换/path/to/your/script/为 auto-backlight.sh 脚本所在的实际路径。

感谢 Alex Layton 提出他的想法:https://unix.stackexchange.com/a/110469并感谢 Pilot6 提出的想法:https://askubuntu.com/a/613816

答案4

Cuttlefish 是一款可以根据事件(例如插入和拔出电源)自动更改设置的工具

相关内容