Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Created March 30, 2018 22:12
Show Gist options
  • Save cesarkawakami/2e72613ecedcc3719937f4cf4646e5c1 to your computer and use it in GitHub Desktop.
Save cesarkawakami/2e72613ecedcc3719937f4cf4646e5c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
def get_monitor_name():
monitors = subprocess.run(
['xrandr', '--listmonitors'],
stdout=subprocess.PIPE,
check=True,
).stdout.splitlines()[1:]
for monitor in monitors:
_, _, _, monitor_name = monitor.split()
return monitor_name
def new_mode(mode_name, width, height, frame_rate):
mode_line = subprocess.run(
['cvt', str(width), str(height), str(frame_rate)],
stdout=subprocess.PIPE,
check=True,
).stdout.splitlines()[1].split()[2:]
subprocess.run(['xrandr', '--newmode', mode_name, *mode_line], check=True)
return mode_name
def add_mode(monitor_name, mode_name):
subprocess.run(
['xrandr', '--addmode', monitor_name, mode_name],
check=True,
)
def set_mode(monitor_name, mode_name):
subprocess.run(
['xrandr', '--output', monitor_name, '--mode', mode_name],
check=True,
)
def main():
monitor_name = get_monitor_name()
width = 1920
height = 1080
frame_rate = 60
mode_name = '{}x{}x{}'.format(width, height, frame_rate)
try:
new_mode(mode_name, 1920, 1080, 60)
add_mode(monitor_name, mode_name)
except subprocess.CalledProcessError:
pass
set_mode(monitor_name, mode_name)
print('All Done.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment