You are not logged in.
Hi there
I'm trying to write a script to control my LCD brightness because Arch always set it to 70% or so at startup.
I would like to have full brightness when the AC adapter is present and to have a decrease in brightness when I'm running on battery.
It seems to work quite well but I would like to have suggestions on how to improve it because I have not bash scripting / programming skills at all!
#!/bin/bash
if cat /proc/acpi/ac_adapter/ACAD/state | grep on-line
then
echo 9 | sudo tee /sys/class/backlight/acpi_video0/brightness
echo 9 | sudo tee /sys/class/backlight/acpi_video1/brightness
echo 9 | sudo tee /sys/devices/virtual/backlight/acpi_video0/brightness
echo 9 | sudo tee /sys/devices/virtual/backlight/acpi_video1/brightness
echo 100 | sudo tee /proc/acpi/video/GFX0/DD03/brightness
echo 100 | sudo tee /proc/acpi/video/GFX0/DD04/brightness
xbacklight -set 100
exit 0
else
echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
echo 4 | sudo tee /sys/class/backlight/acpi_video1/brightness
echo 4 | sudo tee /sys/devices/virtual/backlight/acpi_video0/brightness
echo 4 | sudo tee /sys/devices/virtual/backlight/acpi_video1/brightness
echo 40 | sudo tee /proc/acpi/video/GFX0/DD03/brightness
echo 40 | sudo tee /proc/acpi/video/GFX0/DD04/brightness
xbacklight -set 40
exit 1
fi
What do you think of it? Maybe it's too redundant?
Give me your feedback! Thanks!
Last edited by rent0n (2009-12-01 20:00:59)
rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686
Offline
echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
Can't you replace that with:
echo 4 > sudo tee /sys/class/backlight/acpi_video0/brightness
Offline
What do you think of it? Maybe it's too redundant?
Give me your feedback! Thanks!
Yes, I would use functions.
#!/bin/bash
backlight()
{ # usage: backlight int
local files=(
/sys/class/backlight/acpi_video0/brightness
/sys/class/backlight/acpi_video1/brightness
/sys/devices/virtual/backlight/acpi_video0/brightness
/sys/devices/virtual/backlight/acpi_video1/brightness
)
echo $1 | sudo tee ${files[@]}
}
video()
{ # usage: video int
local files=(
/proc/acpi/video/GFX0/DD03/brightness
/proc/acpi/video/GFX0/DD04/brightness
)
echo $1 | sudo tee ${files[@]}
}
if cat /proc/acpi/ac_adapter/ACAD/state | grep -q on-line
then
backlight 9
video 100
xbacklight -set 100
exit 0
else
backlight 4
video 40
xbacklight -set 40
exit 1
fi
I haven't tested this nor can I comment on the brightness interface stuff.
Edit 2: I would remove the sudoes and call the whole script using sudo.
Edit:
echo 4 | sudo tee /sys/class/backlight/acpi_video0/brightness
Can't you replace that with:echo 4 > sudo tee /sys/class/backlight/acpi_video0/brightness
No, sudo is a command, not a file.
Last edited by fsckd (2009-12-01 22:26:13)
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
Ugh, my network is not great today.
Anyways, two links that can help you in learning bash:
http://tldp.org/LDP/Bash-Beginners-Guide/html/
Edit: Explanation of my changes above.
I added two functions, backlight and video which each take a single argument (I believe should be an integer). The files are listed as arrays using the array_var=( item1 item2 ... itemn ) syntax. This allows for using the array as arguments to a command thanks to array expansion, like so, command ${array_var[@]}. Finally I added -q to the grep to silence it. See grep's manual for more scintillating options. Hope that helps.
Last edited by fsckd (2009-12-01 22:45:14)
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
Does acpid still work? I tried to start the daemon and get (yeah, put it before hal,dbun in the daemon array):
acpid: can't open /proc/acpi/event: Device or resource busy
The reason I talk about this is because acpid is used for power management events and it may be able to be used for mapping to your brightness keys. If that doesn't work you can use xbindkeys. If that isn't enough, a udev event can be created that detects when your battery is plugged in/out and trigger an event.
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
@fsckd: thank you very much for your help, this would definitely improve my script. However, it sounds a bit more complicated to me now. I really would like to learn bash scripting, if only I have enough time!
@Gen2ly: Well, actually, my brightness keys are working perfectly. The problem was just to set the default brightness level at startup because arch always sets it to 70% or so.
rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686
Offline
Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:
ac_adapter)
if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
then
logger "AC Adapter disconnected"
echo 7 > /sys/class/backlight/acpi_video0/brightness
else
logger "AC Adpater connected"
echo 10 > /sys/class/backlight/acpi_video0/brightness
fi
;;
This works quite well for me.
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:
ac_adapter) if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ]; then logger "AC Adapter disconnected" echo 7 > /sys/class/backlight/acpi_video0/brightness else logger "AC Adpater connected" echo 10 > /sys/class/backlight/acpi_video0/brightness fi ;;
This works quite well for me.
Well this is interesting...Acpid is probably the best way to handle these issues.
I can't understand what the "logger" part is doing, can you please explain it?
Last edited by rent0n (2009-12-02 22:12:06)
rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686
Offline
NeoXP wrote:Don't know if it is of any use to you, but I'm using acpid to handle this. The part regarding screen brightness is handled in the following part of my handler.sh:
...
This works quite well for me.
Well this is interesting...Acpid is probably the best way to handle these issues.
I can't understand what the "logger" part is doing, can you please explain it?
It logs the text between the quotes in my messages.log and in my everything.log. If something goes wrong on my system it helps me find clues.
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
It logs the text between the quotes in my messages.log and in my everything.log. If something goes wrong on my system it helps me find clues.
I was thinking that your solution should work well when you switch between ac and battery power, however it shouldn't handle LCD brightness at startup, when you boot your laptop, because no acpi event is triggered. Am I wrong?
In my case, I want to set the correct brightness level when I power up my laptop and, for this reason, I placed my script in my autostarted program list. Xfce-power-manager already reduces screen brightness when I switch from ac to battery power.
rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686
Offline
I was thinking that your solution should work well when you switch between ac and battery power, however it shouldn't handle LCD brightness at startup, when you boot your laptop, because no acpi event is triggered. Am I wrong?
In my case, I want to set the correct brightness level when I power up my laptop and, for this reason, I placed my script in my autostarted program list. Xfce-power-manager already reduces screen brightness when I switch from ac to battery power.
I think you're right, there is no difference in brightness if I boot plugged or unplugged. Your script is indeed a good idea to set brightness at boot time.
Thx.
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline