Last active
April 16, 2020 13:50
-
-
Save ungive/bf6cded6b2eccf47297ff56aa70ba9d1 to your computer and use it in GitHub Desktop.
Regulate your screen brightness live with a simple zenity slider. Made this script because the built-in slider in Manjaro's task bar doesn't work on my older iMac and I don't want to put too much effort into fixing it. Based on this script: https://gist.github.com/ViktorNova/a13c6613dd365b2c67b0 NOTE: Check line 60 for window positioning
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/python3 | |
import sys, subprocess, os, time | |
from subprocess import PIPE, Popen | |
from threading import Thread | |
from queue import Queue, Empty | |
import atexit | |
dir_path = os.path.dirname(os.path.realpath(__file__)) | |
lockfile=os.path.join(dir_path, '.brightness-lock~') | |
def createlock(): | |
with open(lockfile, 'w') as fp: | |
pass | |
def removelock(): | |
os.remove(lockfile) | |
if os.path.isfile(lockfile): | |
exit(1) | |
createlock() | |
atexit.register(removelock) | |
def enqueue_output(out, queue): | |
for line in iter(out.readline, b''): | |
queue.put(line) | |
out.close() | |
def popen_stdout(args): | |
with open(os.devnull, 'w') as devnull: | |
p = Popen(args, stdout=subprocess.PIPE, stderr=devnull) | |
p.wait() | |
if p.returncode == 1: raise Exception() | |
return p.communicate()[0] | |
def set_brightness(opval): | |
int_val = int(opval) | |
with open(brightness_file, 'w') as fh: | |
fh.write("%d" % int_val) | |
brightness_files = [ | |
"/sys/class/backlight/radeon_bl0/brightness", | |
"/sys/class/backlight/radeon_bl1/brightness", | |
] | |
low = 1 | |
high = 255 | |
window_title = 'Screen Brightness' | |
index = 0 | |
while index < len(brightness_files): | |
try: | |
brightness_file = brightness_files[index] | |
curr = int(popen_stdout(["cat",brightness_file]).strip()) | |
except Exception: index += 1 | |
else: break | |
if index >= len(brightness_files): | |
raise Exception("None of the brightness files exist: %s" % | |
', '.join(brightness_files)) | |
p_wmctrl_init = Popen('wmctrl -R "%s"' % (window_title), shell=True) | |
p = Popen([ | |
"zenity","--scale","--print-partial", "--value=%d" % curr, | |
"--title",window_title,"--text","","--min-value=%d" % low, | |
"--max-value=%d" % high, "--step=1" | |
], stdout=PIPE, close_fds=True) | |
q = Queue() | |
t = Thread(target=enqueue_output, args=(p.stdout, q)) | |
t.daemon = True | |
t.start() | |
retcode=1 | |
while retcode != 0: | |
p_wmctrl = Popen('wmctrl -r "%s" -R "%s" -e 0,1452,0,-1,-1' \ | |
% (window_title,window_title), shell=True) | |
time.sleep(.025) | |
p_wmctrl.wait() | |
retcode=p_wmctrl.returncode | |
while True: | |
try: | |
line = q.get_nowait() | |
except Empty: | |
if not t.is_alive(): | |
p.wait() | |
if p.returncode == 1: | |
set_brightness(curr) | |
break | |
else: | |
set_brightness(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment