Skip to content

Instantly share code, notes, and snippets.

@steelbrain
Last active August 14, 2020 05:07
Show Gist options
  • Save steelbrain/db94aaf142b628f93f40a343b7c4a44f to your computer and use it in GitHub Desktop.
Save steelbrain/db94aaf142b628f93f40a343b7c4a44f to your computer and use it in GitHub Desktop.
OLED Brightness Daemon

OLED Brightness Daemon

I'm using this for my Razer Blade 17 Pro 2020 to keep the brightness slider in Ubuntu GNOME up to date with the actual brightness of the display.

It's not pretty but it works. Remember to add this to your startup (gnome-session-properties).

License

Licensed under MIT. Use however.

#!/usr/bin/env python3
import os
import sys
import time
import subprocess
DISPLAY_NAME = "DP-2" # Run `xrandr` to see the active display
PATH_BACKLIGHT = "/sys/class/backlight/acpi_video0/actual_brightness"
MIN_BRIGHTNESS = 0.2 # Don't let people turn off the screen completely
DEFAULT_BRIGHTNESS = 0.5 # In case we cannot read the source file
POLLING_INTERVAL_SECONDS = 0.5
if not os.path.exists(PATH_BACKLIGHT):
# We're on Intel Graphics. No need to run this monitor
sys.stdout.write("Ignoring self because of non-NVIDIA Graphics\n")
sys.exit()
def getBrightness():
try:
with open(PATH_BACKLIGHT) as file:
value = file.read().strip()
fraction = int(value) / 100
except:
sys.stderr.write("ERROR: Failed to read backlight value\n")
fraction = DEFAULT_BRIGHTNESS
return MIN_BRIGHTNESS if fraction < MIN_BRIGHTNESS else fraction
def setBrightness(value):
subprocess.Popen(["xrandr", "--output", DISPLAY_NAME, "--brightness", str(value)],
stdin =subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
bufsize=0)
setBrightness(getBrightness())
while 1:
time.sleep(POLLING_INTERVAL_SECONDS)
setBrightness(getBrightness())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment