Bodhi Linux Forums: Brightness scripts - Bodhi Linux Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Brightness scripts

#1 User is offline   BodhiJohn 

  • Member
  • Group: Members
  • Posts: 4
  • Joined: 13-August 12

Posted 13 August 2012 - 03:01 PM

One of the (few) broken features of Bodhi has been the brightness adjustment on my laptop. I'd like to share my solution to the problem.

Assigning key bindings to XF86MonBrightnessUp/Down does nothing. Prior to Bodhi 2.0, I used gnome-power-manager but this gets annoying with every use. There are suggestions to add GRUB command entries to "acpi_osi=Linux" and "acpi_backlight=vendor". This doesn't work for my ATI video card. Another solution is to use xbacklight, which also doesn't work for me.

There is a direct way of changing brightness by changing this file: /sys/class/backlight/acpi_video0/backlight
If you have this file, chances are that you can use my scripts. If you don't have this file, you may have the "acpi..." lines in GRUB and need to remove them. The directory may be called something other than "acpi_video0".

STEP 1: add write permissions to the file /sys/class/backlight/acpi_video0/backlight:
sudo chmod go+w /sys/class/backlight/acpi_video0/backlight

To enable the scripts to be run by users, add this command to your /etc/rc.local file so that it runs with every boot like this:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

automount
chmod go+w /sys/class/backlight/acpi_video0/brightness
/home/john/scripts/bright_down.sh
/home/john/scripts/bright_down.sh

exit 0


STEP 2: copy these 2 scripts that increase and decrease brightness. Try them out. They automatically stay within max/min brightness limits. You can set your preferred increment.

bright_down.sh
#!/bin/sh
#
# Script to change brightness in Bodhi 12.04 for laptops which don't respond
# to hardware keys
# Needs to have write permissions to /sys/class/backlight/acpi_video0/brightness
# which can be set in /etc/rc.local
#
# August 2012

BRIGHT_INCREMENT=2
MIN_BRIGHT=1

read MAX_BRIGHT < /sys/class/backlight/acpi_video0/max_brightness
read CURRENT_BRIGHT < /sys/class/backlight/acpi_video0/brightness

CURRENT_BRIGHT=`expr $CURRENT_BRIGHT - $BRIGHT_INCREMENT`

if [ $CURRENT_BRIGHT -ge $MIN_BRIGHT ]
then
	echo $CURRENT_BRIGHT > /sys/class/backlight/acpi_video0/brightness
fi


bright_up.sh
#!/bin/sh
#
# Script to change brightness in Bodhi 12.04 for laptops which don't respond
# to hardware keys
# Needs to have write permissions to /sys/class/backlight/acpi_video0/brightness
# which can be set in /etc/rc.local
#
# August 2012

BRIGHT_INCREMENT=2
MIN_BRIGHT=1

read MAX_BRIGHT < /sys/class/backlight/acpi_video0/max_brightness
read CURRENT_BRIGHT < /sys/class/backlight/acpi_video0/brightness

CURRENT_BRIGHT=`expr $CURRENT_BRIGHT + $BRIGHT_INCREMENT`

if [ $CURRENT_BRIGHT -le $MAX_BRIGHT ]
then
	echo $CURRENT_BRIGHT > /sys/class/backlight/acpi_video0/brightness
fi


STEP 3: Assign the above macros to the Fn keys using Key Bindings Settings/Launch/Command. If there's an error executing, make sure that the scripts have execute privelages and the brightness file write privelages.

STEP 4: If you want the computer to boot up with a certain brightness value set, you can directly issue the script in /etc/rc.local like I've done above (or just set it directly to the number you want)

Note that not every laptop has the same maximum brightness, which is why I read that value in. There could be more error checking in the scripts, but they do the job well with a minimum of effort. I don't go to the trouble of listing my hardware info because it seems to be common across all kinds of setups.
0

#2 Guest_Meji_D_*

  • Group: Guests

Posted 13 August 2012 - 04:07 PM

View PostBodhiJohn, on 13 August 2012 - 03:01 PM, said:


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

automount
chmod go+w /sys/class/backlight/acpi_video0/brightness
/home/john/scripts/bright_down.sh
/home/john/scripts/bright_down.sh

exit 0



Not tested this yet but I'm wondering, should not one of the lines above read: /home/john/scripts/bright_up.sh ?
0

#3 User is offline   BodhiJohn 

  • Member
  • Group: Members
  • Posts: 4
  • Joined: 13-August 12

Posted 13 August 2012 - 04:34 PM

View PostMeji_D, on 13 August 2012 - 04:07 PM, said:

Not tested this yet but I'm wondering, should not one of the lines above read: /home/john/scripts/bright_up.sh ?


My system defaults to full brightness on each boot, so I just run my bright_down.sh script twice to match my preference. You could just as easily set the brightness directly in 1 line. I wanted to show a more generic solution on this forum that doesn't rely on my laptop's specific brightness range. A 1 liner could be:

echo XX > /sys/class/backlight/acpi_video0/brightness

where XX is the brightness (depending on your hardware, a number from 1 to the maximum found in file /sys/class/backlight/acpi_video0/max_brightness)
0

#4 Guest_Meji_D_*

  • Group: Guests

Posted 13 August 2012 - 04:56 PM

My Vaio defaults to a dim 35%? so bright_down is not for me :D

I hope this works on the Vaio since all "old Vaio" backlight workarounds have failed on 2.0 so far.

Thx :)
0

#5 User is offline   alex2035 

  • Member
  • Group: Members
  • Posts: 22
  • Joined: 04-August 12
  • Locationbuenos aires

Posted 14 August 2012 - 05:24 PM

Want to thank you for this, it works perfectly on my Acer AspireOne. I just set a bootup value of 50 for a middle brightness level. from then on I can take it down or up with my keybindings F9 and F10, in 2 step increments as your script , which gives me total control of brightness. Only had to change the directory in /sys/class/backlight/xxxxx/brightness to point to the Poulsbo driver and edit references. Alex
Acer AspireOne A0751h - Bodhi 2.0.1 32bit
0

#6 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 14 September 2012 - 01:19 AM

For gma500, acpi_video0 doesn't work, but psb-bl does. So:
/sys/class/backlight/psb-bl/brightness

Also a few things to add:


1: Unless you want to enter your password every time in gksudo, or unless you're running as root, you want to make a udev rule to change the permissions on the file to allow users to write to it. Here is a generic udev rule that works with any video driver that creates a /sys/backlight tree (unlike most similar examples on various wiki's out there). So you don't have to hard code your particular video drivers name in the file. It just makes the brightness and power files owned by group video, and writable by group, and all users are already members of group video.

Create a new file: /etc/udev/rules.d/backlight-permissions.rules

One (long) line goes in it:
SUBSYSTEM=="backlight",RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power",RUN+="/bin/chmod 664 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"


Reboot and then ls -l /sys/class/backlight/*/* (it won't affect the acpi_video0 directory)


2: Next, the e17 backlight module doesn't do squat on gma500, nor can I figure out if it even can be configured or tweaked at all.
Neither does xbacklight work at all. Yet the sysfs files above work fine so it's not simply a broken driver lacking the support.

So you can make your own reasonably nice slider brighness setter with zenity. Zenity is just a fancy version of xmessage. Simple command line util that takes a few command line options and displays a message or a simple dialog, and then echos the users answer on stdout and exits.

So, it's only a few lines of shell to read the current brightness and max brightness similar to the scripts above (I didn't know about these before I came here, but I'm pleased to see someone else who cared to use "read" instead of foo=`cat somefile` !)

/usr/local/bin/zbl :
#!/bin/bash
# 20120913 Brian K. White <brian@aljex.com>
# zbl - Zenity Backlight Control
# usage: zbl <sysfs_backlight_driver_name>
# example: zbl psb-bl
#
# The backlight controller in e17 doesn't do squat on my gma500
# Yet the backlight is perfectly queryable and settable by just reading/writing
# /sys/class/backlight/<driver_dependant>/brightness
#
# You'll need a udev rule to allow members of group video to adjust backlight.
# Create a file: /etc/udev/backlight-permissions.rules
# Piut this one line in it:
# SUBSYSTEM=="backlight",RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power",RUN+="/bin/chmod 664 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"

# sysfs backlight driver name
# For psb_gfx or gma500_gfx kernel modules it's "psb-bl"
# Others are named *-bl
# Look in ls /sys/class/backlight
# If none supplied, use acpi since it's generic, but it probably won't work
# because the udev rule above doesn't touch the acpi_video0 files
DRIVER_NAME=${1:-acpi_video0}

##############################################
# Shouldn't have to change anything below here

# sysfs backlight path
SB=/sys/class/backlight/$DRIVER_NAME

# Maximum value
read MAX < ${SB}/max_brightness

# Title and prompt text
TXT="Backlight"

# Read the current brightness value
read CV < ${SB}/actual_brightness

zenity \
	--scale \
	--title=$TXT \
	--text=$TXT \
	--min-value=0 \
	--max-value=$MAX \
	--value=$CV \
	> ${SB}/brightness


I made it so you have to specify the driver name on the cmdline because it's easier for a user to copy the script without modifying it, and put their own driver name in the menu entry command line or desktop shortcut. So I for instance have "zbl psb-bl"

It's tempting to make the script scan the backlight directory and determine the driver name automatically but it will only be either unreliable and not handle odd cases like multiple video cards, or it will be idiotically complicated for very little benefit. Surely before very long the xorg people or the e/lxde/gnome/kde etc people will have adapted and this will be obsolete anyways.

A very similar script could use plain xmessage in case zenity isn't installed like on a live image.

I'm still trying to figure out what's the simplest way to make a new menu entry or shelf launcher. Everything seems to be obscure, unclear, maybe not possible on e17 vs lxde or xfce or anythng else I've used the last several years. A long time ago I used e12 through e15 and although the configuration was in the form of one huge text file, which lookjs daunting at first glance, I actually remember it being very straightforward and I could make it do anything I wanted. That is not the experience I'm having with e17 so far!
0

#7 User is offline   BodhiJohn 

  • Member
  • Group: Members
  • Posts: 4
  • Joined: 13-August 12

Posted 14 September 2012 - 11:57 AM

View Postbkw, on 14 September 2012 - 01:19 AM, said:

Everything seems to be obscure, unclear, maybe not possible on e17 vs lxde or xfce or anythng else I've used the last several years.


Agreed. This is one of several regressions I experienced while upgrading from 10.04-based Bodhi to 12.04. It's frustrating to spend hours on fixing lost functionality. To my surprise when experimenting with a secondary Ubuntu installation, I found that KDE has matured considerably since I tried it last time. KDE 4.8 has everything I was trying to get e17 to do and more. Admittedly, it's heavier on resources but for me worth the tradeoff.
0

#8 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 15 September 2012 - 11:34 AM

Update, a little excerpt from IRC #e (I'm KEYofR)
<raster> well the best path is complain to your xorg driver maintainers
<raster> as e backlight uses the randr bakclight controls if available first
<raster> it falls back to pickin g whatever backlight stuff it finds in /sys if this doesnt work
<raster> its a fallback
<raster> not a primary path
<raster> there isnt any way to tell it to use a specific one
[...]
<KEYofR> ok, thanks to the randr pointer, I just discovered that this works:  xrandr --output LVDS-0 --set backlight 40
<raster> hmm
<raster> interesting
<raster> that means ecore-x randr is trying to fiddle with the wrong output


So one sorta-kinda good news is that there is another way to set the backlight besides echoing to the sysfs file, and probably doesn't require the udev rule to set special permissions on the sysfs file.
xrandr --output LVDS-0 --set backlight 40

Where "LVDS-0" is an output name and "40" is any value in the range supported by that output on that driver.
xrandr --verbose
shows all properties for all displays.
That's how you can find the output name, "LVDS-0" in my case, and the allowable backlight range, 0-100 in my case.

Another sorta-kinda good news is that since raster acknowledged e was doing something wrong, and that the driver actually does have a working xrandr interface to the backlight (or at least it does on my laptop, a Sony Vaio P), maybe possibly sometime he'll fix the backlight module maybe.

The bad news is he didn't seem particularly interested or sympathetic that the sysfs interface existed and worked, yet the e backlight control couldn't make use of it. No way to specify the sysfs file? how hard could that be? It's already using that type of interface just failing to pick the right one automatically.
0

#9 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 17 September 2012 - 02:23 PM

Here's another brightness script.
This one uses xrandr.

Advantages:
* Doesn't need special udev rule in /etc to set special permissions on sysfs files.
* Doesn't require the user to know anything special about the driver name or sysfs directory name etc.
* Includes all 3 likely usage modes in one script
- non-interactive increase/decrease by 1/8 for use with keybindings
- non-interactive set any arbitrary % for use with startup/idle/battery/wall/etc scripts
- interactive slider for manual/interactive use from menu or shelf icon etc

Disadvantages:
* Slower than reading writing sysfs files
* May not work for some people, since it relies on xrandr, for the same reason that neither xbacklight or e17 work for me

Other notes:
Requires zenity just for the interactive manual slider and the desktop notifications.

It properly handles the fact that different video drivers have different ranges of values that their brightness interface accepts. I have seen 0-8, 0-32, 0-100, and 0-256 so far at least. So this always uses the range of values reported in xrandr. So if you ask for "50%" on a driver that accepts values from 0-8, it will set brightness 4. You don't have to know what are the valid values for your driver.

For keybindings, use "xbl -" and "xbl +" in Settings, Input, Keys, <some key>, Launch, Command, Action Params

For startup, idle dimmer, laptop battery vs wall power, scripts etc use, example "xbl 30" to set 30%

For normal menu or shelf icon, just use "xbl" with no arguments.

I would attach the file but it says "You aren't permitted to upload this kind of file"
Oh well, sorry you have to cut & paste and make sure not to break the long lines etc.

/usr/local/bin/xbl :
#!/bin/bash
# 20120915 Brian K. White <brian@aljex.com>
#h xbl - Use xrandr to set backlight brightness.
#h
# For whatever reason, neither xbacklight nor e17's backlight module works
# on my laptop, yet "xrandr --output LVDS-0 --set backlight <0-100>" does work.
# So this uses xrandr to query and control the backlight.
#
#h Usage:
#h xbl      # Display a slider. Requires the program "zenity".
#h xbl +    # Increase brightness by 1/8 of total range.
#h xbl -    # Decrease brightness by 1/8 of total range.
#h xbl <N>  # Set brightness to N percent (0-100).
#h
#h Environment variable XBL_STEPS sets the number of levels or steps from
#h min to max for the +/- keybinding options. Default is 8 if unset.
#h
#h Environment variable XBL_NOTIFY sets whether or not to generate a desktop
#h notification for each event. Valid values are any variation of true/false,
#h yes/no, 1/0, case insensitive. Default is true if unset.
#h
#h Example: Reduce brightness by 1/3, and don't generate a notification:
#h XBL_STEPS=3 XBL_NOTIFY=false xbl -
#h
# TODO: sysfs, ~/.xbl

# How many steps do you want from min to max for the +/- keybindings ?
# Default 8. Parent environment overrides.
: ${XBL_STEPS:=8}

# Generate a desktop notification for each event? (true/false,yes/no,y/n,1/0)
# Default true. Parent environment overrides.
: ${XBL_NOTIFY:=true}

###############################################################################
typeset -i INC=${XBL_STEPS} MIN MAX OLD NEW

# query backlight, first connected output that has a backlight property
qbl () {
 local o c l h r x
 xrandr --prop | while read -a x ; do
  [[ "${x[1]}" == "connected" ]] && o=${x[0]}
  [[ "${x[0]}" == "backlight:" ]] && { c=${x[1]} r=${x[4]//[\()]} l=${r%,*} h=${r#*,} ; }
  [[ "$o" && "$c" && "$l" && "$h" ]] || continue
  echo "OUT=$o OLD=$c MIN=$l MAX=$h"
  break
 done
}

# Get the current output name, and min, max, & current brightness values.
eval `qbl`

# Increment, decrement, set arbitrary level, or display slider.
case "$1" in
 "-"|"+") NEW=$((OLD$1($MAX-$MIN)/$INC)) ;;
 [0-9]*) NEW=$(($1*$MAX/100)) ;;
 "") NEW=`zenity --scale --title=Backlight --text=Backlight --min-value=$MIN --max-value=$MAX --value=$OLD` ;;
 -h|--help) awk -v s=${0##*/} '($1=="#h"){$1="";gsub("\\<xbl\\>",s);;print $0}' $0 ; exit 0 ;;
 *) echo "Usage: ${0##*/} [-h|--help|+|-|<percent>]" ; exit 1 ; ;;
esac

# Sanity-check user input
[[ $NEW -gt $MAX ]] && NEW=$MAX
[[ $NEW -lt $MIN ]] && NEW=$MIN
case "$XBL_NOTIFY" in
 0|[Nn]*|[Ff]*) XBL_NOTIFY=false ;;
 *) XBL_NOTIFY=true ;;
esac

echo "Output:$OUT Min:$MIN Max:$MAX Old:$OLD New:$NEW"

# Display notification. Useful for feedback about keybinding events.
${XBL_NOTIFY} && zenity --notification --text="Backlight ${NEW}%" --timeout=1 &

# Set the new backlight level. Do last so exit value propagates.
xrandr --output $OUT --set backlight $NEW

0

#10 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 19 September 2012 - 09:43 PM

Backlight script v 2.2!

* Supports both xrandr and sysfs.
* Controllable by environment variables.
* Variables may be put in a config file ~/.xbl , and/or placed on the command line.

So this one scripts handles essentially every situation:
* non-interacive keybinding usage (increase 1/N or decrease 1/N from pressing your hardware brightness keys)
* non-interactive startup usage (setting brightness to desired brightness at startup instead of 100%)
* non-interactive battery vs wall power setting (set brightness to specific levels on battery vs plugged in)
* manual interactive (select brightness on a gui slider)

And all of those work the same whether you need to use sysfs ("echo 50 >/sys/class/backlight/<something>/backlight"), or need to use xrandr.

In the case of sysfs, you can specify the sysfs directory name right on the command line, or write it in ~/.xbl, or if you don't do either of those, it will scan /sys/class/backlight and prompt you to select one.

Examples:

Default behavior, for keybindings, use xrandr, generate desktop notifications:
For brightness-down: "xbl -"
For brightness-up: "xbl +"

Specify to use /sys/class/backlight/psb-bl/backlight without any config file, and suppress generating a desktop notification:
For brightness-down: "XBL_METHOD=sysfs XBL_SYSFS_DIR=psb-bl XBL_NOTIFY=false xbl -"
For brightness-up: "XBL_METHOD=sysfs XBL_SYSFS_DIR=psb-bl XBL_NOTIFY=false xbl +"

Or you could create ~/.xbl and write this in it:
XBL_METHOD=sysfs
XBL_SYSFS_DIR=psb-bl
XBL_NOTIFY=false

And then when you run just "xbl -" it will use sysfs psb-bl and no notifications.

If xrandr doesn't work and you don't know what your sysfs directory is ("psb-bl" is only for the psb_gfx and gma500_gfx kernel modules, other kernel video driver modules use other names), no problem, just tell it to use sysfs (XBL_METHOD=sysfs), but don't tell it any directory name (don't set XBL_SYSFS_DIR), and it will prompt you with all available options. You can try each one until it works, then you can store that name in the config file later by adding "XBL_SYSFS_DIR=..." .

If you want to set 65% brightness at start up, no notification, using xrandr, put this in a desktop startup script:
XBL_NOTIFY=false xbl 65

If you want to get help, just run "xbl --help"

There actually are some other things named "xbl" out there, such as
http://www710.univ-l...tp/xbl/xbl.html
so if you save the script as some other name than "xbl" no problem, the help will swap in whatever name it was called as in place of "xbl" throughout the help.

Remember to chmod 755 /usr/local/bin/xbl

Remember that IF you want to use sysfs, you probably want to install this udev rule:
* Create a file named /etc/udev/rules.d/backlight-permissions.rules
* Put this one long line in it (well, one active line plus comments):
# Allow users (members of group video) to write to sysfs backlight control files
# 20120913 Brian K. White <brian@aljex.com>
SUBSYSTEM=="backlight",RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power",RUN+="/bin/chmod 664 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"

It's printed in the --help also for reference.

xrandr mode is more automatic but slower.

sysfs mode requires slightly more set-up, but it's a lot faster.

On my Sony Vaio P, xrandr mode takes almost a full second, while sysfs is immediate.

/usr/local/bin/xbl:
#!/bin/bash
# 20120920 Brian K. White <brian@aljex.com>
#h xbl - Use xrandr or sysfs to set backlight brightness.
#h v 2.2
#h
# For whatever reason, neither xbacklight nor e17's backlight module works
# on my laptop, yet both:
#   "xrandr --output LVDS-0 --set backlight <0-100>"
# and
#   "echo <0-100> > /sys/class/backlight/psb-bl/backlight"
# DO work. So this uses xrandr or sysfs to query and control the backlight.
#
#h Usage:
#h xbl      # Display a slider. Requires the program "zenity".
#h xbl +    # Increase brightness by 1/8 of total range.
#h xbl -    # Decrease brightness by 1/8 of total range.
#h xbl <N>  # Set brightness to N percent (0-100).
#h
#h Environment variables:
#h XBL_STEPS - sets the number of levels or steps from min to max
#h for the +/- keybinding options. Default is 8 if unset.
#h
#h XBL_NOTIFY - true/false - Sets whether or not to generate a desktop
#h notification for each event. Default is true if unset.
#h
#h XBL_METHOD - "xrandr" or "sysfs" - Sets which interface to use. If using
#h sysfs, you will usually also need to install a udev rule to set permissions
#h on the sysfs backlight file so that the user can write to it. (see below)
#h Default is xrandr because it's automatic, doesn't require special
#h udev rules, and doesn't require the user to know the sysfs directory name.
#h
#h XBL_SYSFS_DIR - If using sysfs, sets the /sys/class/backlight/* directory name.
#h If using sysfs, and XBL_SYSFS_DIR is either not specified, or no such directory
#h actually exists, the user is prompted interactively to select one of the
#h existing directories. Example, if your directory is /sys/class/backlight/psb-bl
#h then use XBL_SYSFS_DIR=psb-bl
#h
#h XBL_XRANDR_OUT - Override autodetection of xrandr output name and specify
#h one manually. This script only exists in the first place because at least two
#h other utilities (Enlightenment's backlight module and the xbacklight program)
#h already try to do the same thing as this script, fail to do so automatically,
#h and offer no way for the user to specify manually that which they fail to do
#h automatically. Default is autodetected. A typical example is "LVDS-0".
#h Use "xrandr --prop" or "xrandr --verbose" to find yours.
#h
#h XBL_CFG - Path/name of config file to use. Default is ~/.xbl
#h
#h Any or all of these variables may be defined in the config file or
#h in the environment or omitted altogether. Likewise the config file itself is
#h optional. If no variables are set and no config file exists, xrandr
#h will be used since it's more automatic and doesn't require the user to know
#h any driver/directory/output names, and doesn't require special permissions
#h or the udev rule to set the special permissions.
#h
#h If using sysfs, create this udev rules file and reboot.
#h (everything after SUBSYSTEM is one long line)
#h /etc/udev/rules.d/backlight-permissions.rules:
#h # Allow users to write to sysfs backlight control files
#h # 20120913 Brian K. White <brian@aljex.com>
#h SUBSYSTEM=="backlight",RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power",RUN+="/bin/chmod 664 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"
#h
#h Example: Reduce brightness by 1/3, and don't generate a notification:
#h XBL_STEPS=3 XBL_NOTIFY=false xbl -
#h

# How many steps do you want from min to max for the +/- keybindings ?
# Default 8. Parent environment overrides.
: ${XBL_STEPS:=8}

# Generate a desktop notification for each event? (true/false, yes/no, 0/1)
# Default true. Parent environment overrides.
: ${XBL_NOTIFY:=true}

# Use xrandr or use sysfs?
: ${XBL_METHOD:=xrandr}

# Config file
: ${XBL_CFG:=~/.xbl}

# Sysfs backlight directory name. Normally set this in your own ~/.xbl
# config file or on the commandline, not hard-coded here.
# This is only used in sysfs mode.
# XBL_SYSFS_DIR=psb-bl

# Xrandr output name. Normally allow this to autodetect, or set in your own
# ~/.xbl config file, or on the commandline, not hard-coded here.
# This is only used in xrandr mode.
# XBL_XRANDR_OUT=LVDS-0

###############################################################################
[[ -s $XBL_CFG ]] && . $XBL_CFG
typeset -i INC=$XBL_STEPS MIN MAX OLD NEW

# Query backlight using xrandr
get_xrandr () {
 local u=$XBL_XRANDR_OUT o c l h r x
 xrandr --prop | while read -a x ; do
  [[ -z "$u" && "${x[1]}" == "connected" ]] || [[ "$u" && "${x[0]}" == "$u" ]] && o=${x[0]}
  [[ "$o" && "${x[0]}" == "backlight:" ]] && { c=${x[1]} r=${x[4]//[\()]} l=${r%,*} h=${r#*,} ; }
  [[ "$o" && "$c" && "$l" && "$h" ]] || continue
  echo "OUT=$o OLD=$c MIN=$l MAX=$h"
  break
 done
}

# Query backlight using sysfs
get_sysfs () {
 local o=$XBL_SYSFS_DIR s=/sys/class/backlight d l c h
 d=${s}/$o
 [[ -w ${d}/brightness ]] || {
  cd $s || exit 1
  shopt -s nullglob
  for d in * ;do l+=" FALSE $d" ;done
  o=`zenity --list --radiolist --text="Select Driver" --hide-header --column= --column= $l`
  d=${s}/$o
 }
 read c < ${d}/actual_brightness
 read h < ${d}/max_brightness
 [[ "$o" && "$c" && "$h" ]] || exit 1
 echo "OUT=${d}/brightness OLD=$c MIN=0 MAX=$h"
}

# Set backlight using xrandr
set_xrandr () { xrandr --output $1 --set backlight $2 ; }

# Set backlight using sysfs
set_sysfs () { echo "$2" > $1 ; }

# Sanity check user input
case "$XBL_METHOD" in "xrandr"|"sysfs") : ;; *) exit 1 ;; esac

# Get the current output name, and min, max, & current brightness values. 
eval `get_$XBL_METHOD`

# Prevent uglier errors later if get_* failed.
[[ "$OUT" ]] || echo "No valid xrandr output or sysfs directory."
[[ "$MIN" ]] || echo "Could not find minimum backlight value."
[[ "$MAX" ]] || echo "Could not find maximum backlight value."
[[ "$OLD" ]] || echo "Could not find current backlight value."
[[ "$OUT" && "$MIN" && "$MAX" && "$OLD" ]] || {
 echo "Output:\"$OUT\" Min:\"$MIN\" Max:\"$MAX\" Old:\"$OLD\""
 set |grep '^XBL_'
 exit 1
}

# Determine new brightness level.
# Increment 1/n, Decrement 1/n, Set to arbitrary percent, or Display slider.
case "$1" in
 "-"|"+") NEW=$((OLD$1($MAX-$MIN)/$INC)) ;;
 [0-9]*) NEW=$(($1*$MAX/100)) ;;
 "") NEW=`zenity --scale --title=Backlight --text=Backlight --min-value=$MIN --max-value=$MAX --value=$OLD` ;;
 -h|--help) awk -v s=${0##*/} '($1=="#h"){$1="";gsub("\\<xbl\\>",s);;print $0}' $0 ; exit 0 ;;
 *) echo "Usage: ${0##*/} [-h|--help|+|-|<percent>]" ; exit 1 ; ;;
esac

# Sanity-check user input
[[ $NEW -gt $MAX ]] && NEW=$MAX
[[ $NEW -lt $MIN ]] && NEW=$MIN
case "$XBL_NOTIFY" in
 0|[Nn]*|[Ff]*) XBL_NOTIFY=false ;;
 *) XBL_NOTIFY=true ;;
esac

echo "Output:$OUT Min:$MIN Max:$MAX Old:$OLD New:$NEW"

# Display notification, useful for feedback about keybinding events.
$XBL_NOTIFY && zenity --notification --text="Backlight ${NEW}%" --timeout=1 &

# Set the new backlight level.
set_$XBL_METHOD $OUT $NEW

0

#11 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 20 September 2012 - 12:00 PM

Updated, v2.1
* Fixed bug in setting XBL_CFG. It would never see the config file.
* Added variable XBL_XRANDR_OUT to override autodetection of the xrandr output name and manually specify one.
* Changed name of XBL_SYSFS to XBL_SYSFS_DIR to better describe it's purpose and to align with the xrandr equivalent above.
0

#12 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 20 September 2012 - 09:18 PM

Updated, v2.2
* More technically correct handling of xrandr output. If user specifies an output instead of letting it autodetect, then ensure the min, max, current brighness values come from that specific output, even if it is not the first one listed in xrandr --prop.
* Sanity check after running get_*. If get_* fails, or if the user specifies a non-existing xrandr output or sysfs directory, detect that and abort more gracefully before trying to use the bad results.
0

#13 User is offline   bkw 

  • Member
  • Group: Members
  • Posts: 11
  • Joined: 22-August 12

Posted 27 September 2012 - 04:30 PM

Renamed xbl to bltool and made a code.google.com project where all further work will proceed.

There already are other things named xbl, not just that old game either but some javascript library. Besides, you don't need X to use this so it's not really just "X BackLight" anyways.

https://code.google.com/p/bltool/

It's already updated slightly from above in that it avoids trying to run zenity or xrandr if there is no $DISPLAY

On the TODO list:
* curses and/or cli interactive modes for when zenity or X are not available or just not wanted.
* query-only option

[edit: these are done]
0

#14 User is offline   alex2035 

  • Member
  • Group: Members
  • Posts: 22
  • Joined: 04-August 12
  • Locationbuenos aires

Posted 01 October 2012 - 12:23 AM

How frustrating it is to have a very well tuned system just to be ruined by an update, I guess this is so common, I became accostumed , I started using Linux on Red Hat 5 or something, years ago, but now I am getting old and dont feel like doing scripts, configs, and all the s...t.
Updated to 2.1.0 then I lost brightness control and few other things, netbook started to lag, have to do everything again, thats when the whole idea of changing distro appears, if it only there was one that had everything a bit more friendly ....oh well.
Acer AspireOne A0751h - Bodhi 2.0.1 32bit
0

#15 User is offline   jaypmcwilliams 

  • Member
  • Group: Members
  • Posts: 1
  • Joined: 08-July 12
  • LocationColorado

Posted 29 October 2012 - 04:49 AM

View PostBodhiJohn, on 13 August 2012 - 03:01 PM, said:

One of the (few) broken features of Bodhi has been the brightness adjustment on my laptop. I'd like to share my solution to the problem.

Assigning key bindings to XF86MonBrightnessUp/Down does nothing. Prior to Bodhi 2.0, I used gnome-power-manager but this gets annoying with every use. There are suggestions to add GRUB command entries to "acpi_osi=Linux" and "acpi_backlight=vendor". This doesn't work for my ATI video card. Another solution is to use xbacklight, which also doesn't work for me.

There is a direct way of changing brightness by changing this file: /sys/class/backlight/acpi_video0/backlight
If you have this file, chances are that you can use my scripts. If you don't have this file, you may have the "acpi..." lines in GRUB and need to remove them. The directory may be called something other than "acpi_video0".

STEP 1: add write permissions to the file /sys/class/backlight/acpi_video0/backlight:
sudo chmod go+w /sys/class/backlight/acpi_video0/backlight

To enable the scripts to be run by users, add this command to your /etc/rc.local file so that it runs with every boot like this:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

automount
chmod go+w /sys/class/backlight/acpi_video0/brightness
/home/john/scripts/bright_down.sh
/home/john/scripts/bright_down.sh

exit 0


STEP 2: copy these 2 scripts that increase and decrease brightness. Try them out. They automatically stay within max/min brightness limits. You can set your preferred increment.

bright_down.sh
#!/bin/sh
#
# Script to change brightness in Bodhi 12.04 for laptops which don't respond
# to hardware keys
# Needs to have write permissions to /sys/class/backlight/acpi_video0/brightness
# which can be set in /etc/rc.local
#
# August 2012

BRIGHT_INCREMENT=2
MIN_BRIGHT=1

read MAX_BRIGHT < /sys/class/backlight/acpi_video0/max_brightness
read CURRENT_BRIGHT < /sys/class/backlight/acpi_video0/brightness

CURRENT_BRIGHT=`expr $CURRENT_BRIGHT - $BRIGHT_INCREMENT`

if [ $CURRENT_BRIGHT -ge $MIN_BRIGHT ]
then
	echo $CURRENT_BRIGHT > /sys/class/backlight/acpi_video0/brightness
fi


bright_up.sh
#!/bin/sh
#
# Script to change brightness in Bodhi 12.04 for laptops which don't respond
# to hardware keys
# Needs to have write permissions to /sys/class/backlight/acpi_video0/brightness
# which can be set in /etc/rc.local
#
# August 2012

BRIGHT_INCREMENT=2
MIN_BRIGHT=1

read MAX_BRIGHT < /sys/class/backlight/acpi_video0/max_brightness
read CURRENT_BRIGHT < /sys/class/backlight/acpi_video0/brightness

CURRENT_BRIGHT=`expr $CURRENT_BRIGHT + $BRIGHT_INCREMENT`

if [ $CURRENT_BRIGHT -le $MAX_BRIGHT ]
then
	echo $CURRENT_BRIGHT > /sys/class/backlight/acpi_video0/brightness
fi


STEP 3: Assign the above macros to the Fn keys using Key Bindings Settings/Launch/Command. If there's an error executing, make sure that the scripts have execute privelages and the brightness file write privelages.

STEP 4: If you want the computer to boot up with a certain brightness value set, you can directly issue the script in /etc/rc.local like I've done above (or just set it directly to the number you want)

Note that not every laptop has the same maximum brightness, which is why I read that value in. There could be more error checking in the scripts, but they do the job well with a minimum of effort. I don't go to the trouble of listing my hardware info because it seems to be common across all kinds of setups.



WORKED GREAT on Toshiba Sattelite L505D. THANK YOU. NOTE: I did change one of the bright_down to bright_up under automount & used MY username instead of JOHN. Thanks again. Jay
0

#16 User is offline   Reza 

  • Member
  • Group: Members
  • Posts: 1
  • Joined: 01-February 13

Posted 01 February 2013 - 10:36 PM

View PostBodhiJohn, on 13 August 2012 - 03:01 PM, said:


STEP 3: Assign the above macros to the Fn keys using Key Bindings Settings/Launch/Command. If there's an error executing, make sure that the scripts have execute privelages and the brightness file write privelages.

STEP 4: If you want the computer to boot up with a certain brightness value set, you can directly issue the script in /etc/rc.local like I've done above (or just set it directly to the number you want)




I want to add shortcuts for bright down and up. In Custom Shortcuts, for name for example I enter bright down and for command I enter /home/reza/scripts/bright_down.sh but nothing's happen.
could you please help with this?
0

#17 User is offline   Beerforfree 

  • Member
  • Group: Members
  • Posts: 4
  • Joined: 10-November 13

Posted 11 November 2013 - 01:03 AM

Actually I'm not finding where to do the key assignments in Bodhi 2.4.0 and the scripts should be located at /etc ?
thanks for any reply
0

#18 User is offline   Miguelito Vieira 

  • Member
  • Group: Members
  • Posts: 3
  • Joined: 25-February 14

Posted 01 March 2014 - 07:54 PM

Thanks for the great script!
In my laptop (Asus Vivobook S200E; lshw lists it as "X202E (ASUS-NotebookSKU)"), it worked after two adjustments:
  • adding "BL_XRANDR_OUT=LVDS1" to the config file (autodetection of the xrandr output name failed)
  • in the get_xrandr and set_xrandr code, changing "backlight" to "Backlight" (uppercase B)


View PostBeerforfree, on 11 November 2013 - 01:03 AM, said:

Actually I'm not finding where to do the key assignments in Bodhi 2.4.0 and the scripts should be located at /etc ?
thanks for any reply


Menu > Settings > All > Input > Key bindings. Or summon "Run Everything" (<Super/Win key> + <Space>) and search "key bindings".
You can place your scripts where you prefer. IIRC, typical locations would be /usr/local/bin or ~/bin.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users