Last active
December 26, 2022 21:05
-
-
Save nathanharper/8167040 to your computer and use it in GitHub Desktop.
Wonky-ass script to adjust brightness on my Asus laptop running Crunchbang and Arch Linux.
This file contains hidden or 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/env python | |
""" | |
Adjust screen brightness. | |
Usage: `bright 10` | |
Default is to adjust brightness via hardware. | |
'-s' flag will make the tool adjust brightness via software. | |
'-g' flag prints current brightness (hardware) | |
Use '-d' and '-i' to decrement and increment, respectively. (hardware) | |
TODO: make '-sg' show software brightness | |
""" | |
from __future__ import division | |
from argparse import ArgumentParser | |
from subprocess import call, check_call | |
from sys import exit | |
BRIGHTNESS_FILE = '/sys/class/backlight/acpi_video0/brightness' | |
MAX_BRIGHTNESS = '/sys/class/backlight/acpi_video0/max_brightness' | |
def get_max(args): | |
"Get max brightness as int" | |
if args.soft: | |
return 10 # software is in range 0.0 -> 1.0, or 0 -> 10 | |
else: | |
try: | |
with open(MAX_BRIGHTNESS, 'r') as f: | |
return int(f.read()) | |
except Exception: | |
return 15 # this is the max when i wrote this... | |
def get_curr(): | |
"Get current brightness (hardware only). False on failure" | |
try: | |
with open(BRIGHTNESS_FILE, 'r') as f: | |
return int(f.read()) | |
except Exception: | |
return False | |
def fail(): | |
print('FAIL') | |
exit(1) | |
def donezo(): | |
print("Nothing to do.") | |
exit(0) | |
parse = ArgumentParser(description='Adjust blacklight brightness') | |
parse.add_argument('-s', '--soft', action='store_true',\ | |
help='Adjust brightness from software. Default is hardware.') | |
parse.add_argument('-g', '--get', action='store_true',\ | |
help='Display brightness instead of setting it') | |
parse.add_argument('-i', '--inc', action='store_true',\ | |
help='Increment brightness 1 notch. Hardware only.') | |
parse.add_argument('-d', '--dec', action='store_true',\ | |
help='Decrement brightness 1 notch. Hardware only.') | |
parse.add_argument('num', nargs='?', type=int, help='Brightness value') | |
args = parse.parse_args() | |
if args.get: | |
curr = get_curr() | |
if curr is not False: | |
print("%d of %d" % (curr, get_max(args))) | |
exit(0) | |
fail() | |
# set max and min values | |
mn = 0 | |
mx = get_max(args) | |
if args.dec: | |
curr = get_curr() | |
if curr <= 0: | |
donezo() | |
nxt = curr-1 | |
elif args.inc: | |
curr = get_curr() | |
if curr >= mx: | |
donezo() | |
nxt = curr+1 | |
elif args.num is None: | |
parse.print_help() | |
exit(1) | |
else: | |
nxt = args.num | |
if nxt < mn or nxt > mx: | |
print("Brightness must be between %d and %d" % (mn, mx)) | |
exit(1) | |
# increment and decrement not supported for software | |
if args.soft and not args.dec and not args.inc: | |
check_call(['xrandr','--output','LVDS1','--brightness',str(nxt/10)]) | |
else: | |
check_call([ | |
'sudo','-u','root','bash','-c','echo -n %d > %s' % (nxt,BRIGHTNESS_FILE) | |
]) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment