Skip to content

Instantly share code, notes, and snippets.

@EcmaXp
Created August 14, 2023 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EcmaXp/97c78d632069b11810c5636141467d56 to your computer and use it in GitHub Desktop.
Save EcmaXp/97c78d632069b11810c5636141467d56 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# https://intellij-support.jetbrains.com/hc/en-us/community/posts/8438534916370-Window-is-rendered-incorrectly-rotated-90-degrees
import shlex
import subprocess
# https://github.com/jakehilborn/displayplacer
DISPLAYPLACER_BIN = "/opt/homebrew/bin/displayplacer"
def encode_screen_line(screen: dict) -> str:
return " ".join(f"{key}:{value}" for key, value in screen.items())
def decode_screen_line(screen_line: str) -> dict:
screen = {}
for item in screen_line.split(" "):
key, value = item.split(":")
screen[key] = value
return screen
def get_displayplacer_cmdline():
stdout = subprocess.check_output([DISPLAYPLACER_BIN, "list"]).decode('utf-8')
for line in stdout.splitlines():
if line.startswith("displayplacer"):
return line
raise ValueError("displayplacer command not found")
def get_screens() -> list[dict]:
cmdline = get_displayplacer_cmdline().removeprefix("displayplacer ")
return [decode_screen_line(screen_line) for screen_line in shlex.split(cmdline)]
def displayplacer(screen: dict, **kwargs):
new_screen = {**screen, **kwargs}
screen_line = encode_screen_line(new_screen)
subprocess.check_call([DISPLAYPLACER_BIN, screen_line])
def macos_screen_rotation_fix():
fixed_screen_count = 0
screens = get_screens()
for screen in screens:
normal_degree = int(screen["degree"])
invert_degree = (normal_degree + 180) % 360
if normal_degree == 0:
continue
fixed_screen_count += 1
print(encode_screen_line(screen))
displayplacer(screen, degree=invert_degree)
displayplacer(screen, degree=normal_degree)
print(f"Screen rotation fixed: {fixed_screen_count} screens")
if __name__ == '__main__':
macos_screen_rotation_fix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment