Created
July 17, 2017 08:12
-
-
Save kouya/7eff7e206c60a6bdc77ef2d7239a776c to your computer and use it in GitHub Desktop.
Backlight control script via sysfs and other hacky methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# This script can control a laptop's backlight via the sysfs interface /sys/class/backlight/intel_backlight | |
# (and others you add yourself ;) | |
# | |
# I use it on my Asus zenbook UX305LA since no other method or kernel parameters got the backlight control | |
# keys working. It probably works with any Intel HD graphics based graphics. | |
# | |
# To use, put it somewhere on your disk and give yourself sudo rights for this single file. The sysfs | |
# interface is only writable by root. Bind any key combination of your window manager to it. Naturally | |
# BrightnessDown and Up, if the key produces a keycode on your laptop. Doesn't on the UX305LA. As of kernel | |
# 4.10 it does! So no more use for this hacky method. | |
# | |
# decreases brghtness by 10% | |
# | |
# sudo poormans-backlight.py -t intel -d 10 | |
# | |
# increases brightness by 10% | |
# | |
# sudo poormans-backlight.py -t intel -i 10 | |
# | |
# License | |
# | |
# This code is public domain. No copyright whatsoever, no warranty. | |
from __future__ import division | |
from __future__ import print_function | |
import argparse | |
import os | |
class GenericIntel: | |
sysfs_path = '/sys/class/backlight/intel_backlight' | |
def get_max_brightness(self): | |
f = open(os.path.join(self.sysfs_path, 'max_brightness'), 'r') | |
max_brightness = int(f.readline().rstrip()) | |
return max_brightness | |
def get_brightness(self): | |
f = open(os.path.join(self.sysfs_path, 'brightness'), 'r') | |
brightness = int(f.readline().rstrip()) | |
return brightness | |
def set_brightness(self, value): | |
f = open(os.path.join(self.sysfs_path, 'brightness'), 'w') | |
print(value, file=f) | |
parser = argparse.ArgumentParser(description='Poor man\'s backlight control.') | |
parser.add_argument('-i', '--increase', help='Increase by percentage.', type=int) | |
parser.add_argument('-d', '--decrease', help='Decrease by percentage.', type=int) | |
parser.add_argument('-t', '--type', help='Hardware type.') | |
args = parser.parse_args() | |
if args.increase != None and args.decrease != None: | |
print("boom!") | |
exit(-1) | |
if args.type == 'intel': | |
backlight = GenericIntel() | |
else: | |
print("Unknown backlight type.") | |
exit(-1) | |
if args.increase == None: | |
# Decrease backlight | |
max_brightness = backlight.get_max_brightness() | |
pct = args.decrease / 100 | |
delta = int(max_brightness * pct) | |
brightness = backlight.get_brightness() | |
new_brightness = brightness - delta | |
if new_brightness < 0: | |
new_brightness = 0 | |
print(new_brightness) | |
backlight.set_brightness(new_brightness) | |
else: | |
# Increase backlight | |
max_brightness = backlight.get_max_brightness() | |
pct = args.increase / 100 | |
delta = int(max_brightness * pct) | |
brightness = backlight.get_brightness() | |
new_brightness = brightness + delta | |
if new_brightness > max_brightness: | |
new_brightness = max_brightness | |
print(new_brightness) | |
backlight.set_brightness(new_brightness) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment