Simply, I call the script (brightness.sh) and pass a three character value as an argument. I've got commands to strip individual numbers for math processes, and then the final commands to apply the changes. The rest is just simple if-then stuff.
I know I've got a lot of commands calling xrandr data, and that's slowing down the whole script. I would love to reduce it to just one instance of data collection, but I'm not skilled enough to make a super complex piped command that could do all that in one line.
What should I do to make this run more efficiently? Like, would it be elegant to put all of the --verbose
output into a variable, or file, then grep that variable/file for even faster processing? I would think all that data wouldn't fit into a variable.
#!/bin/bash
: '
gonna need some variables for later
collect some data
'
gv1=$(xrandr --verbose | grep -m 1 Gamma | awk '{print $2}')
gv2=$(xrandr --verbose | grep -m 2 Gamma | awk '{print $2}' | tail -n1)
c1=$(xrandr --verbose | grep -m 1 Brightness | awk '{print $2}')
c2=$(xrandr --verbose | grep -m 2 Brightness | awk '{print $2}' | tail -n1)
r1=$( echo $gv1 | cut -d':' -f1 )
g1=$( echo $gv1 | cut -d':' -f2 )
b1=$( echo $gv1 | cut -d':' -f3 )
r2=$( echo $gv2 | cut -d':' -f1 )
g2=$( echo $gv2 | cut -d':' -f2 )
b2=$( echo $gv2 | cut -d':' -f3 )
c=1.0
gm=0.01
br=0.1
# default values so the stupid thing doesn't freak out
cv1=$c1
cv2=$c2
: '
RGB is red green blue, C is Brightness, gm is delta gamma, br is delta brightness
only one value is coming in from the command line, which indicates what screen is changing with which value.
code being passed: i for inc, d for dec, b for balance (set to 1.0)
: r for red, g for green, b for blue
: 1 for DVI-I-1, 2 for DVI-I-2
If you use this script, run 'xrandr --verbose' in a terminal to see which screens are connected, then set the appropriate data here.
'
case $1 in
# #######
# brightness
# #######
dc2)
cv2=$( echo "$c2 - $br" | bc -l )
;;
ic2)
cv2=$( echo "$c2 + $br" | bc -l )
;;
bc2)
cv2=$c
;;
dc1)
cv1=$( echo "$c1 - $br" | bc -l )
;;
bc1)
cv1=$c
;;
ic1)
cv1=$( echo "$c1 + $br" | bc -l )
;;
# #######
# colors
# #######
ir1)
rv=$( echo "$r1 + $gm" | bc -l )
gv1=$rv:$g1:$b1
;;
br1)
gv1=$c:$g1:$b1
;;
dr1)
rv=$( echo "$r1 - $gm" | bc -l )
gv1=$rv:$g1:$b1
;;
ir2)
rv=$( echo "$r2 + $gm" | bc -l )
gv2=$rv:$g2:$b2
;;
br2)
gv2=$c:$g2:$b2
;;
dr2)
rv=$( echo "$r2 - $gm" | bc -l )
gv2=$rv:$g2:$b2
;;
ig1)
rv=$( echo "$g1 + $gm" | bc -l )
gv1=$r1:$rv:$b1
;;
bg1)
gv1=$r1:$c:$b1
;;
dg1)
rv=$( echo "$g1 - $gm" | bc -l )
gv1=$r1:$rv:$b1
;;
ig2)
rv=$( echo "$g2 + $gm" | bc -l )
gv2=$r2:$rv:$b2
;;
bg2)
gv2=$r2:$c:$b2
;;
dg2)
rv=$( echo "$g2 - $gm" | bc -l )
gv2=$r2:$rv:$b2
;;
ib1)
rv=$( echo "$b1 + $gm" | bc -l )
gv1=$r1:$g1:$rv
;;
bb1)
gv1=$r1:$g1:$c
;;
db1)
rv=$( echo "$b1 - $gm" | bc -l )
gv1=$r1:$g1:$rv
;;
ib2)
rv=$( echo "$b2 + $gm" | bc -l )
gv2=$r2:$g2:$rv
;;
bb2)
gv2=$r2:$g2:$c
;;
db2)
rv=$( echo "$b2 - $gm" | bc -l )
gv2=$r2:$g2:$rv
;;
*)
;;
esac
# #######
# apply changes
# #######
xrandr --output DVI-I-1 --brightness $cv1 --gamma $gv1
xrandr --output DVI-I-2 --brightness $cv2 --gamma $gv2
Update
So far the worst (maybe) way I've found to increase the speed of this script is to export six variables, allowing them to be altered in script:
gv1 gv2 cv1 cv2 c1 c2
With these on the outside, they can exist at least for the session you're running. Once exported, just comment out the xrandr calls and the script runs much faster.
As an aside, I'm having issues with xrandr not reporting the same gamma values I enter. I've partially solved this with the exported variables.
Update
In case you're unsure how to export the variables, use the following commands in another script, or enter at the cli.
gv1=$(xrandr --verbose | grep -m 1 Gamma | awk '{print $2}')
gv2=$(xrandr --verbose | grep -m 2 Gamma | awk '{print $2}' | tail -n1)
c1=$(xrandr --verbose | grep -m 1 Brightness | awk '{print $2}')
c2=$(xrandr --verbose | grep -m 2 Brightness | awk '{print $2}' | tail -n1)
cv1=$c1
cv2=$c2
export gv1 gv2 cv1 cv2 c1 c2
In the first script (once these are exported) you'll need to invert the cvx assignment to allow for new values being set environmentally and not retrieved programmatically... That is, the variables are now outside the script, so refreshing them from the script will override the data retrieved from the environment. In any case:
c1=$cv1
c2=$cv2
will be the new variable exchange in the script.
P.S. you'll have to export the variables every time you start a new session, unless you add them to the .bashrc, or run a script to retrieve and re-export the variables on log-in.