summaryrefslogtreecommitdiff
path: root/src/bin/my-brightness.sh
blob: c7ddaa78128be2d016978de1f2f780e330e2298b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#
# @author Andreas Fankhauser
#
# This script required root access to some files in "/sys/class/backlight/".
# You may want to make root owner of this file and whitelist the script in the
# sudoers file:
# +----------------------------------------------------------------------------
# | Cmnd_Alias CMD_BRIGHTNESS = /path/to/this/script/my-brightness.sh
# | yourUsername ALL=(ALL) NOPASSWD: CMD_BRIGHTNESS
# +----------------------------------------------------------------------------


cur=$( cat /sys/class/backlight/intel_backlight/brightness )
min=$( cat /sys/class/backlight/intel_backlight/bl_power )
max=$( cat /sys/class/backlight/intel_backlight/max_brightness )
range=$( echo $max-$min | bc )

curPercent=$( echo "($cur-$min)*100/$range" | bc )

if [ $# -eq 0 ] ;then
	# No argument given. Print current brightness in percent.
	echo "$curPercent"
	exit;
elif [ "$1" == "--help" ] ;then
	echo "examples:"
	echo "	- $0 50   # Set brightness to 50%"
	echo "	- $0 +10  # Increase brightness by 10%"
	echo "	- $0 -10  # Decrease brightness by 10%"
	exit;
fi


if [ "$EUID" -ne 0 ] ;then
	# We're not root. Run ourself with root privileges.
	sudo $0 $@;
	exit;
fi


level=$1


if [[ "$level" =~ ^[+|-] ]] ;then
	# Calculate absolute for specified relative value.
	level=$( echo "0$level+$curPercent" | bc )
fi

# Fix max and min exceedations.
if [ $level -lt 2 ] ;then
	echo "[WARN ] Level '$level' set to 2.";
	level=2
elif [ $level -gt 100 ] ;then
	echo "[WARN ] Level '$level' cut to 100."
	level=100
fi

# Translate percent to target scale
nxt=$( echo $level*$range/100 | bc )

# Write evaluated brightness

# Variant using hardware (usually sudo required)
echo $nxt | sudo tee /sys/class/backlight/intel_backlight/brightness > /dev/null

# Software variant (usually sudo NOT required).
#xrandr  \
#	--output DVI-I-1 --brightness $level  \
#	--output HDMI-0 --brightness $level  \
#	--output DVI-D-0 --brightness $level  \
#;