Skip to content

Instantly share code, notes, and snippets.

@ralsina
Last active January 30, 2020 19:02
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 ralsina/8965da24f7b55b6f30c24da676a4f7be to your computer and use it in GitHub Desktop.
Save ralsina/8965da24f7b55b6f30c24da676a4f7be to your computer and use it in GitHub Desktop.
import subprocess
data = subprocess.check_output('xrandr').decode('utf8').splitlines()
outputs = [x for x in data if x and x[0] not in 'S \t']
def parse_output(line):
parts = line.split()
name = parts[0]
primary = 'primary' in parts
w_in_mm, h_in_mm = [p.split('mm')[0] for p in parts if p.endswith('mm')]
res_x, res_y = [p for p in parts if 'x' in p][0].split('+')[0].split('x')
return(name, primary, int(res_x), int(res_y), int(w_in_mm), int(h_in_mm))
for o in outputs:
name, primary, res_x, res_y, w_in_mm, h_in_mm = parse_output(o)
if primary:
prim_density_x = res_x / w_in_mm
prim_density_y = res_y / h_in_mm
break
print('xrandr ', end='')
for o in outputs:
name, primary, res_x, res_y, w_in_mm, h_in_mm = parse_output(o)
if primary:
continue
dens_x = res_x / w_in_mm
dens_y = res_y / h_in_mm
scale_x = prim_density_x / dens_x
scale_y = prim_density_y / dens_y
print(f'--output {name} --scale {scale_x}x{scale_y}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment