4

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?

4 Answers 4

3

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.

1
  • 1
    Good first post.
    – slm
    Commented Dec 25, 2012 at 15:09
2

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
1

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"
3
  • 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.
    – MarkovCh1
    Commented May 15, 2011 at 17:27
  • if xbacklight doesn't work for you, why not file a bug?
    – Kim
    Commented May 16, 2011 at 4:05
  • It's due to the kernel settings, I think...
    – MarkovCh1
    Commented May 16, 2011 at 7:24
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .