DeaD_EyE icon

xrandr restore old settings

DeaD_EyE | PRO | 06/26/22 02:10:39 PM UTC (Edited) | 0 ⭐ | 727 👁️ | Never ⏰ | []
Python |

1.21 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python3
 
"""
https://askubuntu.com/questions/625761/how-to-change-displays-position-from-command-line
"""
 
import re
import subprocess
import json
from pathlib import Path
 
 
xrandr_reg = re.compile(r"^([\w-]+).+?(\d+x\d+)\+(\d+\+\d+)")
config_file = Path.home().joinpath(".config/xrandr_config.json")
 
 
def get_config():
    proc = subprocess.run(["xrandr", "-q"], capture_output=True, encoding="utf8")
    modes = []
    for line in proc.stdout.splitlines():
        if match := xrandr_reg.search(line):         
            port, mode, pos = match.groups()
            pos = pos.replace("+", "x")
            modes.append((port, mode, pos))
 
    return modes
 
 
def get_saved_config():
    if config_file.exists():
        with config_file.open() as fd:
            return json.load(fd)
    else:
        with config_file.open("w") as fd:
            config = get_config()
            json.dump(config, fd)
        return config
 
 
def set_modes(config):
    for port, mode, pos in config:
        cmd = ["xrandr", "--output", port, "--mode", mode, "--pos", pos]
        subprocess.run(cmd)
 
 
if __name__ == "__main__":
    config = get_saved_config()
    set_modes(config

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎