让 Ubuntu 在没有 xbacklight 的情况下记住屏幕亮度级别

mis*_*sha 5 scripts backlight brightness

问题是 Ubuntu 在每次重新启动后总是将亮度级别重置为最大值。我安装了该xbacklight实用程序,但类似xbacklight -getxbacklight -set XX不工作的命令。我没有得到任何输出。

实际上,我想让我的 Ubuntu 记住上次使用的亮度级别。我怎么能做到这一点?以下是一些信息:

ls -l /sys/class/backlight/
total 0
lrwxrwxrwx 1 root root 0 Feb 27 09:43 radeon_bl0 -> ../../devices/pci0000:00/0000:00:01.0/drm/card0/card0-LVDS-1/radeon_bl0


ls -l /sys/class/backlight/radeon_bl0/
total 0
-r--r--r-- 1 root root 4096 Feb 27 09:54 actual_brightness
-rw-r--r-- 1 root root 4096 Feb 27 09:54 bl_power
-rw-r--r-- 1 root root 4096 Feb 27 09:47 brightness
lrwxrwxrwx 1 root root    0 Feb 27 09:54 device -> ../../card0-LVDS-1
-r--r--r-- 1 root root 4096 Feb 27 09:43 max_brightness
drwxr-xr-x 2 root root    0 Feb 27 09:54 power
lrwxrwxrwx 1 root root    0 Feb 27 09:54 subsystem -> ../../../../../../../class/backlight
-r--r--r-- 1 root root 4096 Feb 27 09:43 type
-rw-r--r-- 1 root root 4096 Feb 27 09:42 uevent

uname -r
4.2.0-30-generic
Run Code Online (Sandbox Code Playgroud)

Ser*_*nyy 6

实际上,我想让我的 Ubuntu 记住上次使用的亮度级别。我怎么能做到这一点?以下是一些信息:

介绍

下面的脚本解决了 OP 需要存储和恢复上次使用的屏幕亮度。它与the lightdm欢迎程序一起使用,并由 激活lightdm。没有要求使用lightdm,因此如果您更喜欢使用 cron 作业,则可以这样做。

基本思路:

  1. 将脚本存储在某处(通过此处或通过 github 获取)
  2. /etc/lightdm/lightdm.conf使用sudo特权创建。
  3. 确保文件有3行:[SeatDefaults]display-setup-script,和display-stopped script。详情请往下看。脚本头也给出了概述。

脚本源

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com 
# Date: March 7th, 2016
# Purpose: Script that will remember screen brightness
#          Must be used in conjunction with lightdm
#          Place the following 5 lines into /etc/lightdm/lightdm.conf
#
#           [SeatDefaults]
#           #display-setup-script = Script to run when starting a greeter session (runs as root)
#           display-setup-script = /home/USER/bin/sergrep/brightness.sh restore
#           #display-stopped-script = Script to run after stopping the display server (runs as root)
#           display-stopped-script = /home/USER/bin/sergrep/brightness.sh store
#
#           Basic idea is that you must give full path and either store or restore as an option 
# Written for: http://askubuntu.com/q/739654/295286
# Tested on:  Ubuntu 14.04 LTS
# Version: 1.2 , added brightness limit, file creation
###########################################################
# 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=$#



store()
{
   cat "$SYSDIR"/*/actual_brightness > "$1"
}
#----------

# This function restores brightness. We avoid 
# setting brightness to complete 0, hence
# minimum is 10% that can be restored.

restore()
{
  MAX=$(cat "$SYSDIR"/*/max_brightness  )
  LIMIT=$((MAX/10)) # get approx 10 percent value
  VAL=$(cat "$1" )
  if [ "$VAL" -lt "$LIMIT"  ] ;
  then
       # avoid going bellow 10% of brightness
       # we don't want user's screen to be completely dark
       echo "$LIMIT" > "$SYSDIR"/*/brightness
  else
       echo "$VAL" > "$SYSDIR"/*/brightness
  fi
}
#------------

# This function works for initial run of the script; the script cannot set
# brightness unless datafile exists first, so here we create the file
# Initial value there will be whatever the current brightness on first
# reboot was

create_datafile()
{
  cat "$SYSDIR"/*/actual_brightness > "$1" 
}

puke(){
    printf "%s\n" "$@" > /dev/stderr
    exit 1
}

main()
{
  local DATAFILE="/opt/.last_brightness"
  local SYSDIR="/sys/class/backlight" # sysfs location of all the data

  # Check pre-conditions for running the script
  if [ "$ARGC" -ne 1  ];then
     puke "Script requires 1 argument"
  fi

  if [ $(id -u) -ne 0 ]   ; then
     puke "Script has to run as root"
  fi

  # ensure datafile exists
  [ -f "$DATAFILE"  ] || create_datafile "$DATAFILE"

  # perform storing or restoring function
  case "$1" in
     'restore') restore  $DATAFILE ;;
     'store') store $DATAFILE ;;
     *) puke "Unknown argument";;
  esac

}

main "$@"
Run Code Online (Sandbox Code Playgroud)

获取和设置脚本

您可以直接复制脚本或从命令行执行这些步骤(要打开命令行,请按CtrlAltT

sudo apt-get install git
cd /opt
sudo git clone https://github.com/SergKolo/sergrep.git
Run Code Online (Sandbox Code Playgroud)

该脚本将位于 中/opt/sergrep/brightness.sh,因此我们执行以下操作:

sudo chmod +x /opt/sergrep/brightness.sh
Run Code Online (Sandbox Code Playgroud)

使其可执行。接下来,我们需要将其设置为与 lightdm 一起使用。/etc/lightdm/lightdm.conf通过使用命令行nano文本编辑器(命令是sudo nano /etc/lightdm/lightdm.conf)或图形gedit( pkexec gedit /etc/lightdm/lightdm.conf)打开文件来创建文件

将以下几行写入该文件:

[SeatDefaults]
#display-setup-script = Script to run when starting a greeter session (runs as root)
display-setup-script = /opt/sergrep/brightness.sh restore
#display-stopped-script = Script to run after stopping the display server (runs as root)
display-stopped-script = /opt/sergrep/brightness.sh store
Run Code Online (Sandbox Code Playgroud)

保存并退出

深入概述

您已经发现可以/sys/class/backlight/*/brightness直接写入文件,也可以读取这些值。问题是这/sys是一个虚拟文件系统,所以一旦重新启动,该文件系统中的所有文件都会消失。

因此,您可以/sys/class/backlight/*/actual_brightness在每次重新启动时将值存储到永久文件中。问题是如何 - 通过 cron 作业、lightdm 或其他方式。就我个人而言,我选择了这lightdm条路线。

基本上我们利用了 lightdm 的特性——能够在欢迎程序开始之前和会话退出之后运行脚本。亮度被记录到/opt/.last_brightness文件中,并在每次脚本启动时读取。我们本质上是用同一个脚本执行两个操作,只是通过传递不同的参数。