Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I can use something like

setpci -s 00:02.0 F4.B=XX

replacing 00:02.0 with your device address (obtained via lspci | grep VGA) and XX with a value between 00 and FF (FF = 100%, 7F = 50%, etc...) to manually change my brightness from the terminal (as root, or with proper chmods).

I wish to make a script to manually change my brightness via keystrokes. To do so, I need to get the current state (XX above) in order to relay to the script the current brightness of the screen.

How can I obtain the current state?

share|improve this question

4 Answers 4

Xbacklight doesn't work on my netbook; the backlight isn't visible to the ACPI subsystem. I've hacked these two shell scripts together, though:

brightup.sh:

#!/bin/bash
var1=`sudo setpci -s 00:02.0 f4.b | tr [:lower:] [:upper:]`;
var2=$(echo "obase=16;ibase=16;${var1} + 8" |bc)
sudo setpci -s 00:02.0 f4.b=$var2

brightdown.sh:

#!/bin/bash
var1=`sudo setpci -s 00:02.0 f4.b | tr [:lower:] [:upper:]`;
var2=$(echo "obase=16;ibase=16;${var1} - 8" |bc)
sudo setpci -s 00:02.0 f4.b=$var2

The problem I'm having is that it requires sudo. The end goal of this is to set these two shell scripts to run as keyboard shortcuts, so I need to figure out how to set setpci to be able to run without a password with sudo. I know, it's probably insecure, and a kludge, but I don't see any other options at this point.

also, I apologize for necroing this thread, but these are two scripts I've been looking for on the interwebs ever since I switched back to using Linux on a daily basis.

share|improve this answer
    
Good first post. –  slm Dec 25 '12 at 15:09

I know this is not a very secure way of doing it .. but it works.

#!/bin/bash

password="your_password_here"

set_value_to=$(zenity --title="Brightness control" --scale --text="Adjust the     brightness value" --min-value=5 --max-value=95 --value=50)

echo $password | sudo -S setpci -s 00:02.0 f4.b=$set_value_to
share|improve this answer

I ended up making a few scripts to solve my problem.

  • I bound the following three scripts to convenient keys (CTRL+ALT+PGUP for brightness up, CTRL+ALT+PGDN for brigthtness down and CTRL+ALT+HOME for restoring brigtness to a default value).
  • The scripts require sudoless setpci (you can probably play with some chmods after looking at errors you get by running setpci to avoid editing sudoers) via a line like:

    androith ALL=(ALL) NOPASSWD: /usr/bin/setpci

  • Finally, you will have to hold a "state file" in something akin to /home/androith/.scripts/brightness/state containing only a state (BF to begin with is OK).

brightness-up.sh

#!/bin/bash

# Get the stored state from file
exec < /home/androith/.scripts/brightness/state # stdin replaced by this file
read state # first line of file goes to state variable

# Increase the state, up to FF
new_state=$(echo "$[0x$state+0x10]") # in decimal
if [ "$new_state" -gt 255 ] # 255 = FF in hex
then
    echo "Already at maximal brightness."
else
    state=$(echo "obase=16; $new_state" | bc) # put into hex
    echo "Setting brightness to $state and storing state..."
    sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
    echo $state > /home/androith/.scripts/brightness/state
fi

brightness-down.sh

#!/bin/bash

# Get the stored state from file
exec < /home/androith/.scripts/brightness/state # stdin replaced by this file
read state # first line of file goes to state variable

# Decrease the state, down to FF
new_state=$(echo "$[0x$state-0x10]") # in decimal
if [ "$new_state" -lt 15 ] # 15 = 0F in hex
then
    echo "Already at minimal brightness."
else
    state=$(echo "obase=16; $new_state" | bc) # update and put into hex
    echo "Setting brightness to $state and storing state..."
    sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
    echo $state > /home/androith/.scripts/brightness/state
fi

brightness-default.sh

#!/bin/bash

# Set brightness to default value
state=AF
sudo setpci -s 00:02.0 F4.B=$state # passwordless sudo
echo $state > /home/androith/.scripts/brightness/state
share|improve this answer

There is a little program called xbacklight. It can get and set the brightness of your screen. If you have a webcam, you can even let it set your brightness automatically by taking a picture, getting the average brightness and setting the screen brightness accordingly.

image='/home/kim/brightness.jpeg'
streamer -c /dev/video0 -b 16 -o $image >/dev/null 2>/dev/null && \
meanline=`convert $image -colorspace gray -verbose info: | grep '^\s*mean: '`
if [[ $meanline =~ \((.*)\) ]]; then
    brightness="${BASH_REMATCH[1]}"
    brightness=`echo "$brightness * 200" | bc`
    echo $brightness
    xbacklight -set $brightness
    xbacklight
fi
rm -f "$image"
share|improve this answer
    
Two complaints: 1) I don't like automation of brightness; and more importantly 2) xbacklight won't work for me. This is why I have to interact via setpci directly. –  Syzygy May 15 '11 at 17:27
    
if xbacklight doesn't work for you, why not file a bug? –  Kim May 16 '11 at 4:05
    
It's due to the kernel settings, I think... –  Syzygy May 16 '11 at 7:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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