3

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?

  • Can be done with a script. Can you list requirements ? Like in bullet points or numbers – Sergiy Kolodyazhnyy Feb 27 '16 at 2:59
  • Do you mean value of brightness? I can set that myself if you suggest what the script should be. Let's say 35 on batter, 80 on ac. – user3556519 Feb 27 '16 at 4:03
  • I've actually written the script already. Didn't take me 40 mins. In addition, it polls an rc file. I'll finalize it and post in 15 mins or so. – Sergiy Kolodyazhnyy Feb 27 '16 at 4:05
  • Answer posted, let me know if you have any questions or anything is unclear. This script uses settings inside $HOME/.auto-backlight file. By default it alters brightness only when AC adapter is removed. You can alter the source code by removing two commented lines in the main function, so that you can have permanently set AC and battery power brightness – Sergiy Kolodyazhnyy Feb 27 '16 at 4:26
1

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
| improve this answer | |
  • 1
    Wow, Serg, thank you so much, it works beautifully! Hopefully others will find it useful as well. – user3556519 Feb 27 '16 at 9:20
  • Tried to run this automatically by adding it to my .profile file, but I have a problem. After rebooting, I couldn't login, the screen remained empty after loging in. I went to the console with CTRL ALT F1, and I saw the output of the script along with the following message, repeating: dbus-send --session --print-reply --dest=org.gnome.SettingsDaemon.Power /org/gnome/SettingsDaemon/Power org.gnome.SettingsDaemon.Power.Screen.SetPercentage uint32:"$1" Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $Display for X11 – user3556519 Feb 28 '16 at 1:02
  • Adding this script to .profile isn't a good idea, because each time you open shell, be it tty or terminal emulator, you spawn a new instance of the script. The error messages are due to dbus. It's meant to be for GUI, not command line, thus it does not work because when you log in to TTY, you try to launch it (through .profile) in GUI-less environment. Pressing Ctrl + C did the trick in killing the error messages for me, so try that. As for login issue, I strongly doubt it's due to the script - it performs limited functions that don't affect GUI except for brightness. – Sergiy Kolodyazhnyy Feb 28 '16 at 1:52
  • Try to log into tty again, press Ctrl + C to kill the error messages , and do the following stat $HOME/.Xauthority. Note the dot before X , and no space between / and the dot. Let me know what ownership it has. Typical issue is that when you try to run some kind of GUI app with sudo , that file .Xauthority will become owned by root user, and it prevents you from logging in. Frequent issue, had it lots of times – Sergiy Kolodyazhnyy Feb 28 '16 at 1:54
  • 1
    OK, so I think i know what was going on. Scripts by nature are sequential, so .profile basically was waiting for the script to finish , and unity couldn't start if .profile didn't finish running. Using & to run the script in background from .profile did the trick. However, it is still not a good idea to put any script into .profile, because you will be spawning multiple instances each time you open terminal. – Sergiy Kolodyazhnyy Feb 28 '16 at 3:03
1

On laptops with Intel's laptop you can also use backlight-tracer Debian package. After installed it runs as a service and behaves similar to Windows backlight control; i.e. it remembers last state for both plug and battery and restores brightness accordingly.

There is a blog post describing it but the gist of it is usage of /sys/class/backlight/intel_backlight/brightness to set/read levels.

It works on Ubuntu 19.04 and 19.10 as far as I tested it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.