Actually, I would like to make my Ubuntu remember the level of
brightness that was used last. How exactly can I do that? Here's some
information:
Introduction
The script below addresses OP's need to store and restore screen brightness that was used last. It works in conjunction with the lightdm
greeter and is activated by lightdm
. There's no requirement to use lightdm
, so if you prefer to use a cron job, you can do so.
Basic idea:
- Have the script stored somewhere (either by getting it here or through github)
- Create
/etc/lightdm/lightdm.conf
using the sudo
privilege.
- Make sure that file has 3 lines:
[SeatDefaults]
, display-setup-script
, and display-stopped script
. Look below for details. Script header also gives overview.
Script Source
#!/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 "$@"
Getting and Setting up the Script
You can either copy the script directly or follow these steps from the command line ( to open command line , press CtrlAltT )
sudo apt-get install git
cd /opt
sudo git clone https://github.com/SergKolo/sergrep.git
The script will be located in /opt/sergrep/brightness.sh
, so we do a:
sudo chmod +x /opt/sergrep/brightness.sh
to make it executable. Next, we need to set it up to work with lightdm. Create the /etc/lightdm/lightdm.conf
file , either by opening it with command line nano
text editor (the command is sudo nano /etc/lightdm/lightdm.conf
) or graphical gedit
( pkexec gedit /etc/lightdm/lightdm.conf
)
Write to that file the following lines:
[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
Save and exit
Indepth Overview
You've already discovered that you can write to /sys/class/backlight/*/brightness
file directly , and you can also read in those values as well. Problem is that /sys
is a virtual filesystem, so once you reboot, all the files in that filesystem disappear.
So you could store value in /sys/class/backlight/*/actual_brightness
into a permanent file on every reboot. The question is how - via cron job, via lightdm, or via other means. Personally, I chose the lightdm
route.
Basically we take advantage of lightdm's feature - being able to run a script before the greeter starts and after the session exits. The brightness is recorded to /opt/.last_brightness
file and read from each time the script starts. We're essentially performing two actions with the same script, just by passing different arguments.