Last active
August 1, 2018 00:57
-
-
Save Orochimarufan/04da79841395710c0dbc9d28a22e29f2 to your computer and use it in GitHub Desktop.
Adjust Intel GFX backlight using Rw.exe
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/env python3 | |
# Intel Backlight Hack | |
# CC0 2018 Taeyeon Mori aka /u/Haruha | |
# Must be absolute! | |
rw_path = "D:\\Development\\Win32-Intel-Backlight\\Rw-Everything\\Rw.exe" | |
# Actuall Rw batch code follows | |
rw_code = """\ | |
Read Intel GFX BAR1 | |
> Local0 = rPCIe32(0, 2, 0, 0x10) | |
Null out flags to get address | |
> Local1 = and(Local0, 0xFFFFFFFFFFFFFFF0) | |
LVX offset = 0xC8254 | |
See https://github.com/RehabMan/OS-X-Intel-Backlight/blob/master/IntelBacklight/IntelBacklightHandler.cpp#L39 | |
> Local2 = or(Local1, 0xC8254) | |
Get LVX | |
> Local3 = r32(Local2) | |
Get max brightness | |
> Local4 = shr(Local3, 16) | |
Get relative brightness | |
> Local5 = mul(Local4, {level}) | |
> Local5 = div(Local5, 100) | |
Construct new LVX | |
> Local6 = shl(Local4, 16) | |
> Local6 = or(Local6, Local5) | |
Write to hardware | |
> w32 Local2 Local6 | |
""" | |
rw_exit_code = "> RwExit" | |
import sys | |
import ctypes | |
def is_admin(): | |
try: | |
return ctypes.windll.shell32.IsUserAnAdmin() | |
except: | |
return False | |
if not is_admin(): | |
# Re-run the program with admin rights | |
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join([__file__] + sys.argv[1:]), None, 1) | |
sys.exit() | |
import os | |
import argparse | |
import subprocess | |
import tempfile | |
parser = argparse.ArgumentParser() | |
parser.add_argument("level", help="New Backlight level", choices=list(range(0,101)), type=int, metavar="0-100", nargs="?") | |
parser.add_argument("--noexit", action="store_true", help="Don't exit Rw after running the script") | |
parser.add_argument("--pause", action="store_true", help="Pause after execution") | |
args = parser.parse_args() | |
if args.level is None: | |
print("Enter new brightness level in %, or \"x\" to abort") | |
while True: | |
print("Brightness: ", end="") | |
v = input() | |
if v.lower() == "x": | |
print("Aborted") | |
sys.exit(12) | |
try: | |
args.level = int(v) | |
except ValueError: | |
continue | |
else: | |
if args.level <= 100 and args.level >= 0: | |
break | |
with tempfile.NamedTemporaryFile("w", delete=False) as f: | |
f.write(rw_code.format(level=args.level)) | |
if not args.noexit: | |
f.write(rw_exit_code) | |
path = os.path.dirname(f.name) | |
name = os.path.basename(f.name) | |
subprocess.run([rw_path, "/Command=%s" % name], cwd=path) | |
os.unlink(os.path.join(path, name)) | |
if args.pause: | |
print("Press enter to continue") | |
input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment