LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 12-29-2013, 06:46 PM   #1
nokangaroo
Member
 
Registered: Nov 2009
Posts: 141

Rep: Reputation: 25
How to set screen brightness with a bash script using xrandr


We all love MATE Desktop, and we all know that mate-power-manager does not work correctly. On my box the only thing that worked was setting the screen brightness, for which function I found a replacement:



Install xrandr and use the following script (which does not need root privileges):
Code:
#!/bin/bash
#xev output:
#Brightness down: keycode 232 (keysym 0x1008ff03, XF86MonBrightnessDown)
#Brightness up:   keycode 233 (keysym 0x1008ff02, XF86MonBrightnessUp)

if [[ -f "${HOME}/.brightnessrc" ]]; then
LEVEL=`cat ${HOME}/.brightnessrc`
else
LEVEL=1
fi


brightness ()
#the values for the xrandr command are from the output of xrandr --current --verbose; they may be different on different boxes. xrandr does not seem to have any safeguards against incorrect input, hence the tests:
{
if [[ "$LEVEL" =~ [[:digit:]] ]] && [[ "$(echo "$LEVEL <= 1" | bc)" -eq 1 ]] && [[ "$(echo "$LEVEL >= 0.1" | bc)" -eq 1 ]]; then
xrandr --output 0x52 --crtc 0 --brightness "$LEVEL"
echo "$LEVEL" > ${HOME}/.brightnessrc
exit 0
else
exit 1
fi
}

case $1 in
#Set brightness manually:
+)
LEVEL=$(echo "scale=2; $LEVEL + 0.05" | bc)
brightness
;;
-)
LEVEL=$(echo "scale=2; $LEVEL - 0.05" | bc)
brightness
;;
#For a startup object to set brightness:
*)
brightness
;;
esac
Save the script as /usr/local/bin/brightness or $HOME/brightness.
On the Apple keyboard the brightness keys (F1 and F2, XF86MonBrightnessDown and XF86MonBrightnessUp) can be assigned to the commands "brightness -" and "brightness +".

*****

How to implement automatic shutdown after an hour of inactivity:

Install xautolock and use the following .xinitrc:
Code:
#!/bin/sh
#
# ~/.xinitrc
#
# Executed by startx (run your window manager from here)

if [ -d /etc/X11/xinit/xinitrc.d ]; then
  for f in /etc/X11/xinit/xinitrc.d/*; do
    [ -x "$f" ] && . "$f"
  done
  unset f
fi

xautolock -time 60 -locker "/usr/bin/systemctl suspend" &
# exec gnome-session
# exec startkde
# exec startxfce4
# ...or the Window Manager of your choice
exec mate-session
This is for Archlinux but should also work for other distros, with the appropriate locker command.

Last edited by nokangaroo; 12-29-2013 at 06:51 PM.
 
Old 01-01-2014, 07:09 PM   #2
nokangaroo
Member
 
Registered: Nov 2009
Posts: 141

Original Poster
Rep: Reputation: 25
Edit: The same with an attempt at gamma correction (Maybe the linear correction I use is wrong; should it be logarithmic?)

Code:
#!/bin/bash
#Set display brightness with xrandr, with a (subjective) attempt at gamma correction
if [[ -f "${HOME}/.brightnessrc" ]]; then
LEVEL=`cat ${HOME}/.brightnessrc`
else
LEVEL=1
fi

case $1 in
#Set brightness manually:
+)
LEVEL="$(echo "scale=2; $LEVEL + 0.05" | bc)"
;;
-)
LEVEL="$(echo "scale=2; $LEVEL - 0.05" | bc)"
;;
*)
#For a startup object to set brightness: take the level from ~/.brightnessrc
;;
esac

#the values for the xrandr command are from the output of xrandr --current --verbose; they may be different on different boxes. xrandr does not seem to have any safeguards against incorrect input, hence the tests:

if [[ "$LEVEL" =~ [[:digit:]] ]] && [[ "$(echo "$LEVEL <= 1" | bc)" -eq 1 ]] && [[ "$(echo "$LEVEL >= 0.1" | bc)" -eq 1 ]]; then
#These values will probably be monitor-dependent! The examples here work (sort of) for the MacBook Pro 5.1:
GREENGAMMA="$(echo "scale=3; ($LEVEL * 0.3 + 0.7)" | bc)"
BLUEGAMMA="$(echo "scale=3; (($LEVEL * 0.6 + 0.4) * 0.778)" | bc)" #The value 0.778 is a shim for my nvidia display which is too blue
xrandr --output 0x249 --crtc 0 --gamma "1.0:$GREENGAMMA:$BLUEGAMMA" --brightness "$LEVEL"
echo "$LEVEL" > ${HOME}/.brightnessrc
exit 0
else
exit 1
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mint 15 = black screen (brightness = 0) on start up - have to manually set brightness Netnovice Linux Mint 4 01-16-2014 08:46 PM
Script toogle main screen on/off with xrandr num7 Programming 2 11-27-2013 09:02 AM
[SOLVED] how to set the screen brightness value permanently ? wubai Linux - Laptop and Netbook 2 11-12-2012 07:17 PM
Script to set-up brightness of backlight in XFCE ElectricZero Linux - Laptop and Netbook 1 09-19-2011 07:52 PM
[SOLVED] Brightness set to zero: moved to http://www.linuxquestions.org/questions/slackware-14/brightness-set Z0K4 Linux - Laptop and Netbook 5 09-15-2011 10:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration