Skip to content

Instantly share code, notes, and snippets.

@revarcline
Last active November 25, 2022 02:59
Show Gist options
  • Save revarcline/6ccb9476ffc6c3be1e6379387d0049ab to your computer and use it in GitHub Desktop.
Save revarcline/6ccb9476ffc6c3be1e6379387d0049ab to your computer and use it in GitHub Desktop.
python script to adjust backlight settings
#!/bin/env python
# this script is specifically geared toward intel_backlight laptop displays
# with a maximum value of 96000. brightness will not set to zero in this script
# to avoid mishaps. it's recommended to whitelist this program in /etc/sudoers:
# Cmnd_Alias WITHOUTPW = /usr/local/bin/brightness
# Defaults!WITHOUTPW !authenticate
import os
import sys
import argparse
BRIGHTNESS_FILE = "/sys/class/backlight/intel_backlight/brightness"
# max value of brightness in that file ^
MAX_BRIGHTNESS = 96000
def flags():
parser = argparse.ArgumentParser()
parser.add_argument(
'-p', '--percent',
help='Prints brightness percent to stdout. Default action.',
action='store_true'
)
parser.add_argument(
'-v', '--value',
help='Prints raw brightness value',
action='store_true'
)
parser.add_argument(
'-s', '--set',
type=int,
help='Set brightness to integer percent value between 1 and 100',
)
parser.add_argument(
'-i', '--increment',
type=int,
help='Increment brightness by provided percent value up to 100',
)
parser.add_argument(
'-d', '--decrement',
type=int,
help='Decrement brightness by provided percent value down to 1',
)
return parser.parse_args()
def check_for_root():
if os.getuid() != 0:
print("This script must be run as superuser. Exiting.")
sys.exit(1)
def brightness_value_to_percent(brightness):
brightness = int(brightness)
if brightness > MAX_BRIGHTNESS:
brightness = MAX_BRIGHTNESS
elif brightness < 1:
brightness = 1
return int((brightness / MAX_BRIGHTNESS) * 100)
def percent_to_brightness_value(percent):
percent = int(percent)
if percent > 100:
percent = 100
elif percent < 1:
percent = 1
if percent not in range(1, 101):
print(f"percent value {brightness} must be between 0 and 100")
sys.exit(1)
return int(MAX_BRIGHTNESS * (percent / 100))
def set_brightness_percent(percent):
new_brightness = percent_to_brightness_value(percent)
write_brightness_value(new_brightness)
return percent
def get_brightness_percent():
current_brightness = get_brightness_value()
return brightness_value_to_percent(current_brightness)
def get_brightness_value():
with open(BRIGHTNESS_FILE, 'r') as file:
brightness = file.read()
return brightness
def write_brightness_value(brightness):
with open(BRIGHTNESS_FILE, 'w') as file:
file.write(str(brightness))
def increment_brightness(percent_interval):
current_percent = get_brightness_percent()
new_percent = current_percent + percent_interval
if new_percent > 100:
new_percent = 100
set_brightness_percent(new_percent)
return new_percent
def decrement_brightness(percent_interval):
current_percent = get_brightness_percent()
new_percent = current_percent - percent_interval
if new_percent < 1:
new_percent = 1
set_brightness_percent(new_percent)
return new_percent
if __name__ == '__main__':
check_for_root()
args = flags()
if not any([args.percent, args.value, args.set, args.increment, args.decrement]):
args.percent = True
if args.percent:
print(get_brightness_percent())
sys.exit(0)
if args.value:
print(get_brightness_value())
sys.exit(0)
if args.set:
print(set_brightness_percent(args.set))
sys.exit(0)
if args.increment:
print(increment_brightness(args.increment))
sys.exit(0)
if args.decrement:
print(decrement_brightness(args.decrement))
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment