Last active
July 10, 2023 18:36
-
-
Save n3tn0de/76aebb98325e9adf90f095d2fe942d71 to your computer and use it in GitHub Desktop.
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
import sys | |
import argparse | |
import subprocess | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
) | |
parser.add_argument("-d", "--display", type=str, default=":0", help="xrandr display") | |
parser.add_argument("-i", "--input", type=str, default="HDMI-1", help="xrandr input") | |
parser.add_argument("width", type=int, help="Width in pixels") | |
parser.add_argument("height", type=int, help="Height in pixels") | |
args = parser.parse_args() | |
def trim(string): | |
return string[2:-1] | |
def split_newlines(string): | |
return string.split('\\n') | |
def cmd(cmd_string): | |
return trim(str(subprocess.check_output(cmd_string, shell=True))) | |
xrandr_cmd = 'xrandr -d {} {}' | |
def xrandr(xrandr_args): | |
return xrandr_cmd.format(args.display, xrandr_args) | |
list_modes_cmd = xrandr('-q') | |
modes_cmd_out = split_newlines(str(cmd(list_modes_cmd))) | |
modes = [] | |
for line in modes_cmd_out: | |
# how do I awk, lol | |
if line.find(' ') != -1: | |
mode = line[3:].split(' ', 1)[0] | |
# print(mode) | |
modes.append(mode) | |
# print(modes) | |
cvt_cmd = 'cvt {} {}'.format(args.width, args.height) | |
cvt_string = cmd(cvt_cmd) | |
mode_string = split_newlines(cvt_string)[1].split(' ', 1)[1] | |
mode_name = mode_string.split(' ', 1)[0] | |
output_cmd = xrandr('--output {} --mode {}'.format(args.input, mode_name)) | |
if mode_name.strip('"') in modes: | |
# print('Mode {} already added, swithcing...'.format(mode_name)) | |
cmd(output_cmd) | |
sys.exit() | |
newmode_cmd = xrandr('--newmode {}'.format(mode_string)) | |
addmode_cmd = xrandr('--addmode {} {}'.format(args.input, mode_name)) | |
cmd_queue = [ | |
newmode_cmd, | |
addmode_cmd, | |
output_cmd, | |
] | |
for command in cmd_queue: | |
cmd(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment