Organize monitors in screens

This commit is contained in:
Roberto Alsina 2020-02-05 17:58:04 -03:00
parent c40526118e
commit 6e18d870eb
3 changed files with 17 additions and 9 deletions

View File

@ -3,9 +3,9 @@ from xrandroll.xrandr import parse_data
def test_parse_data(test_data):
data = test_data.read("sample_1.txt", deserialize=False).splitlines()
monitors = parse_data(data)
assert len(monitors) == 2
assert [m.output for m in monitors] == ["eDP", "HDMI-A-0"]
screen = parse_data(data)
assert len(screen.monitors) == 2
assert [m.output for m in screen.monitors.values()] == ["eDP", "HDMI-A-0"]
def test_parse_with_disconnected_monitors(test_data):

View File

@ -9,6 +9,7 @@ from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QGraphicsScene, QLabel
from .monitor_item import MonitorItem
from . import xrandr
def parse_mode(mode):
@ -377,6 +378,8 @@ class Window(QObject):
self.ui.sceneView.scale(scale_factor, scale_factor)
def get_xrandr_info(self):
self.monitors = xrandr.parse_data(xrandr.read_data())
data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
name = None
for line in data:

View File

@ -5,6 +5,16 @@ import subprocess
from .monitor import Monitor, _split_by_lines_matching
class Screen:
"""A Screen is a collection of monitors."""
def __init__(self, data):
self.monitors = {}
for monitor_data in _split_by_lines_matching(r"^[^ \t].*", data[1:]):
m = Monitor(monitor_data)
self.monitors[m.output] = m
def read_data():
data = subprocess.check_output(
["xrandr", "--verbose"], encoding="utf-8"
@ -14,9 +24,4 @@ def read_data():
def parse_data(data):
# Going to pretend there can only be one screen because life is short.
screen = _split_by_lines_matching("^Screen ", data)[0]
result = []
for monitor_data in _split_by_lines_matching(r"^[^ \t].*", screen[1:]):
result.append(Monitor(monitor_data))
return result
return Screen(_split_by_lines_matching("^Screen ", data)[0])