Ubuntu – Automatically reduce brightness on battery in Ubuntu 15.10

15.10batterybrightnesspower-management

How can I automatically reduce brightness when I unplug the AC in Ubuntu 15.10?

I tried modifying dconf-editor settings as suggested here, https://askubuntu.com/a/312619/511925, but there are no longer such settings in Ubuntu 15.10.

I tried install Cuttlefish, but it is not available for Ubuntu 15.10.

Any ideas?

Best Answer

Intro

The script bellow uses dbus and on_ac_power shell script (which comes by default with Ubuntu) to poll for presence of an ac adapter and sets brightness according to values set in $HOME/.auto-backlightrc file.

Installation

Installation using git through terminal:

  1. Run sudo apt-get install git to install git
  2. Run mkdir $HOME/bin. Skip this step if $HOME/bin exists already
  3. cd $HOME/bin
  4. Run git clone https://github.com/SergKolo/sergrep.git
  5. The script will be in $HOME/bin/sergrep/auto-backlight.sh. Make sure the script is executable with chmod +x $HOME/bin/sergrep/auto-backlight.sh
  6. Add the script as a startup application. Look for Startup Applications menu in Unity Dash or Gnome search. Alternatively, run gnome-session-properties command in terminal to launch the menu. Add the full path to script as startup application so that it launches every time you log into GUI.

Alternatively, you can copy and save script source by oneself, chmod +x file, and go through the step #6 described above.

To make the script automatically start every time you log in to Gnome or Unity, use Startup Applications utility.

NOTE: if you want the script to always set AC brightness as well uncomment the else statement on lines 60 and 61 , specifically this part

 # The two lines bellow are optional for 
 # setting brightness if on AC. remove # 
 # if you want to use these two

 # else 
       # change_brightness $INCREASE

Script source

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com 
# Date: February 26 2016 
# Purpose: Brightness control that polls for
#          ac adapter presence. Uses
# Dependencies: on_ac_power script, dbus, Unity/Gnome 
# Written for: https://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
Related Question