Custom brightness controls script using dbus and zenity scale

Introduction:
Knowing that Ubuntu's Unity relies on dbus
service to communicate many settings and events to kernel and hardware, I've put together a simple bash script that relies on dbus
and zenity --scale
.
Installation
The script can be copied from here, or imported from my github.
To manually copy the script:
- Open
gedit
text editor, copy over the code, save the file. Remember the location. Preferably it would be in $HOME/bin
folder.
- Open terminal, navigate to the script location. Issue
chmod +x scriptName.sh
- At this point script is ready to work. You can bind it to keyboard shortcut, or desktop, or launcher.
To import from github:
sudo apt-get install git
- If you don't have
$HOME/bin
directory, create one.
cd $HOME/bin/; git clone https://github.com/SergKolo/sergrep.git
Once download completes, ubright.sh
is ready to be used, located in $HOME/bin/sergrep
.
Script Source
#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com
# Date: February 25th, 2016
# Purpose: Simple brightness control for Ubuntu Unity
# Written for: https://askubuntu.com/q/583863/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.
# set -x
ARGV0="$0"
ARGC="$#"
main ()
{
local DISPLAY=:0
getPercentage | setBrightness > /dev/null
# echo $(getPercentage)
}
setBrightness()
{
local PERCENTAGE
read PERCENTAGE
[[ -n "$PERCENTAGE" ]] || exit 1
dbus-send --session --print-reply\
--dest=org.gnome.SettingsDaemon.Power\
/org/gnome/SettingsDaemon/Power \
org.gnome.SettingsDaemon.Power.Screen.SetPercentage uint32:"$PERCENTAGE"
}
getPercentage()
{
local PCT
PCT="$(zenity --scale --text='Choose brightness level')"
if [[ -n PCT ]]
then
echo "${PCT}"
fi
}
main
Additional Info: While many answers here rely on xrandr
, it's worth to note that xrandr
is not an "actual" hardware solution, i.e., it may change colouring of the screen such that it appears less bright, but the actual power consumption from the screen does not decrease. From xrandr man page:
--brightness brightness
Multiply the gamma values on the crtc currently attached to the output to specified floating value. Useful
for overly bright or overly dim outputs. However, this is a software
only modification, if your hardware has support to actually change the
brightness, you will probably prefer to use xbacklight.
This answer relies on the dbus
interface, which alters the actual brightness setting, represented by a file in /sys/class/backlight
's sub-folder. Thus , through using dbus
we actually control the hardware.
Related posts
sudo apt install redshift
to install. – pbhj Feb 25 '23 at 14:41