Skip to content

Instantly share code, notes, and snippets.

@vampy
Last active February 20, 2021 07:04
Show Gist options
  • Save vampy/ac5974f21d769cd064fb599225d812fd to your computer and use it in GitHub Desktop.
Save vampy/ac5974f21d769cd064fb599225d812fd to your computer and use it in GitHub Desktop.
Script to set monitor brightness on laptop + all external screens
#!/usr/bin/env python
import sys
import os
import re
SCRIPT_NAME = os.path.basename(__file__)
def help():
print(f"Usage: {SCRIPT_NAME} <brightness>")
sys.exit(4)
def run_command(cmd, read_lines=False):
print(f"Running: {cmd}")
stream = os.popen(cmd)
if read_lines:
return stream.read_lines()
return stream.read()
def get_ddc_displays_indices():
pattern = re.compile(r'Display (\d)')
output = run_command("ddcutil detect --nousb --async")
displays = []
for m in re.finditer(pattern, output):
displays.append(int(m.group(1)))
return displays
def set_laptop_display_brightness(brightness):
# Monitor laptop screen, is intel, we can use xbacklight
run_command(f"xbacklight -set {brightness}")
def set_ddc_display_brightness(display_index, brightness):
# For the rest we use ddc https://www.ddcutil.com/
run_command(f"ddcutil --display {display_index} setvcp 10 {brightness}")
# https://wiki.archlinux.org/index.php/Backlight
if __name__ == "__main__":
if len(sys.argv) <= 1:
help()
brightness = int(sys.argv[1])
# Laptop
set_laptop_display_brightness(brightness)
# External
displays = get_ddc_displays_indices()
for d in displays:
set_ddc_display_brightness(d, brightness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment