让 Ubuntu 记住没有 xbacklight 的屏幕亮度级别

让 Ubuntu 记住没有 xbacklight 的屏幕亮度级别

问题是 Ubuntu 每次重启后都会将亮度级别重置为最大值。我安装了该xbacklight实用程序,但命令(例如xbacklight -get或)xbacklight -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

答案1

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

介绍

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

基本思想:

  1. 将脚本存储在某处(可以通过这里或通过 github 获取)
  2. /etc/lightdm/lightdm.conf使用特权创建sudo
  3. 确保文件有 3 行:[SeatDefaults]display-setup-scriptdisplay-stopped script。详情见下文。脚本头也提供了概述。

脚本源

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# 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 "$@"

获取并设置脚本

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

sudo apt-get install git
cd /opt
sudo git clone https://github.com/SergKolo/sergrep.git

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

sudo chmod +x /opt/sergrep/brightness.sh

使其可执行。接下来,我们需要将其设置为与 lightdm 配合使用。创建文件/etc/lightdm/lightdm.conf,可以使用命令行nano文本编辑器(命令为sudo nano /etc/lightdm/lightdm.conf)或图形geditpkexec 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

保存并退出

深入概述

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

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

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

答案2

您是否尝试过这个:

  1. sudo nano /etc/rc.local
  2. 将此行添加到文件中(将 X 替换为所需的亮度级别):

    echo X > /sys/class/backlight/intel_backlight/brightness
    

答案3

如果您能够使用命令设置亮度,则在启动时将亮度级别更改为特定值很容易。请执行以下操作:

  • 按 Super/Windows 键打开 Dash
  • 输入“启动应用程序”并按回车键
  • 点击“添加”
  • 输入名称(无关)和命令
  • 点击“添加”

这应该会在启动时执行该命令。我不确定如何让它记住之前的亮度,但我希望这会有所帮助。

答案4

只需使用光即可...否则修复它会很麻烦,特别是当您同时拥有英特尔和 nvidia 显卡时!

这适用于 Ubuntu 16.04...并在 Alienware M14XR2 上进行了测试

结帐->https://github.com/haikarainen/light

相关内容