Skip to content

Instantly share code, notes, and snippets.

@fabiant7t
Last active December 28, 2015 08:19
Show Gist options
  • Save fabiant7t/7470931 to your computer and use it in GitHub Desktop.
Save fabiant7t/7470931 to your computer and use it in GitHub Desktop.
Backlight Brightness, Version 1.0 A utility to increase and decrease the backlight brightness using xrandr on linux boxes. Written, because the backlight of my Asus notebook was working with maximum brightness only and I had the idea of using xrandr and key bindings.
#!/usr/bin/env python
"""
The MIT License (MIT)
Copyright (c) 2013 by Fabian Topfstedt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Backlight Brightness, Version 1.1
A utility to increase and decrease the backlight brightness using xrandr on
linux boxes.
Written, because the backlight of my Asus notebook was working with maximum
brightness only and I had the idea of using xrandr and key bindings.
"""
from optparse import OptionParser
import os
import re
from subprocess import call, check_output
import tempfile
BRIGHTNESS_FILE = os.path.join(tempfile.gettempdir(), "backlight_brightness")
STEP = 0.10
def get_actual_brightness():
with open(BRIGHTNESS_FILE) as f:
return float(f.read().strip())
def get_current_outputs():
pattern = re.compile(r"^(?P<output>[A-Z0-9]*)\ connected.*")
outputs = []
for line in check_output("xrandr --current", shell=True).split(os.linesep):
search_result = re.search(pattern, line)
if search_result:
outputs.append(search_result.group("output"))
return outputs
def set_brightness(brightness):
outputs = get_current_outputs()
for output in outputs:
call([
"xrandr",
"--output",
output,
"--brightness",
str(brightness)
])
with open(BRIGHTNESS_FILE, "w") as f:
f.write(str(brightness))
def increase_brightness():
brightness = get_actual_brightness() + STEP
if brightness > 1.0:
brightness = 1.0
set_brightness(brightness)
def decrease_brightness():
brightness = get_actual_brightness() - STEP
if brightness < 0.0:
brightness = 0.0
set_brightness(brightness)
if __name__ == '__main__':
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-i", "--increase", action="store_true", dest="brighter",
default=True, help="Increases the brightness [default]")
parser.add_option("-d", "--decrease", action="store_false",
dest="brighter", help="Decreases the brightness")
(options, args) = parser.parse_args()
if not os.path.exists(BRIGHTNESS_FILE):
set_brightness(1.0)
if options.brighter:
increase_brightness()
else:
decrease_brightness()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment