2

I am trying to set up my system and to get some practice with scripting and the like. I am running Linux Mint 16 on a Samsung RC520 laptop (Linux 3.11.0-12-generic x86_64). I want to implement a script that will lower my screen brightness to a pre-set value when I unplug the power (and raise it when I plug it back in).

I can do some very basic scripting but not near this level. I have "xbacklight" which will set the brightness from the command line, so when I unplug the power the line I want to implement would be xbacklight -set 20. I also know that unplugging the power is registered by acpi ; when I plug out and then back in the power I get the following with acpi_listen:

$ acpi_listen
ac_adapter ADP1 00000080 00000000
processor CPU0 00000081 00000000
processor CPU1 00000081 00000000
processor CPU2 00000081 00000000
processor CPU3 00000081 00000000
ac_adapter ADP1 00000080 00000001
processor CPU0 00000081 00000000
processor CPU1 00000081 00000000
processor CPU2 00000081 00000000
processor CPU3 00000081 00000000
battery BAT1 00000080 00000001

So I know I have to use this but I have two main problems: I wouldn't know where to put the script so that it is automatically implemented, and I don't know how I would recognise the plug in/out in acpi because all I know how to do is acpi_listen.

2
  • You know you can do this through your desktop environment right? I am assuming you know but nevertheless you want to script this. If so, you should look into udev.
    – terdon
    Jan 21, 2014 at 18:29
  • I did not know this (how?); I am a Linux noob. However, I would still like to script it. I will look at udev, thanks. Apologies for the late reply.
    – ginny
    Feb 1, 2014 at 0:09

2 Answers 2

2

I believe Linux Mint comes with pm-utils, which is what I used to write a script along the lines of what you are asking for. Below is the script I wrote to dim my screen (and my keyboard) when my computer is unplugged, and restore the brightness when I plug it back in:

You have to make the script executable and place it in /etc/pm/power.d/.

#!/bin/sh
# Adjust brightness of backlights based on power source

case $1 in
    # On battery
    true)
        # Dim keyboard backlight
        echo 0 > /sys/class/leds/asus::kbd_backlight/brightness
        # Dim screen backlight
        expr `cat /sys/class/backlight/acpi_video0/max_brightness` / 100 > \
            /sys/class/backlight/acpi_video0/brightness
    ;;

    # On AC
    false)
        # Dim keyboard backlight
        cat /sys/class/leds/asus::kbd_backlight/max_brightness > \
            /sys/class/leds/asus::kbd_backlight/brightness
        # Dim screen backlight
        cat /sys/class/backlight/acpi_video0/max_brightness > \
            /sys/class/backlight/acpi_video0/brightness
    ;;
esac

return 0

The script is called with a parameter, true when the computer was unplugged and false when the computer was plugged in.

For more info about pm-utils here is a good place to look.

1
while [ 1 ]; do ( cat /proc/acpi/ac_adapter/AC/state | grep off )  &&  xbacklight -set 20; sleep 5; done

You must log in to answer this question.

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