Run KVM switch in background and target DDM by monitor model
This commit is contained in:
+62
-9
@@ -26,6 +26,8 @@ class DDMCommandResult:
|
||||
class DDMBackend(Protocol):
|
||||
def is_available(self) -> bool: ...
|
||||
|
||||
def supports_monitor_targeting(self) -> bool: ...
|
||||
|
||||
def resolve_alienware_slot(self, force: bool = False) -> int | None: ...
|
||||
|
||||
def invalidate_slot(self) -> None: ...
|
||||
@@ -51,6 +53,9 @@ class RealDDMBackend:
|
||||
def is_available(self) -> bool:
|
||||
return self.executable_path is not None and self.executable_path.exists()
|
||||
|
||||
def supports_monitor_targeting(self) -> bool:
|
||||
return True
|
||||
|
||||
def invalidate_slot(self) -> None:
|
||||
with self._lock:
|
||||
self._cached_slot = None
|
||||
@@ -86,17 +91,43 @@ class RealDDMBackend:
|
||||
return DDMCommandResult(False, f"Port {port_name} is not supported.")
|
||||
|
||||
ddm_input = str(port_spec["ddm_input"])
|
||||
monitor_selector = f"/MNT:{ALIENWARE_MODEL_TOKEN}"
|
||||
|
||||
output = self._run_logged_command(f"/{slot}:WriteActiveInput", ddm_input)
|
||||
line = _extract_result_line(output, f"{slot}:WriteActiveInput")
|
||||
if line and "INVALID COMMAND" in line.upper():
|
||||
return DDMCommandResult(False, "DDM rejected the WriteActiveInput command.", output)
|
||||
|
||||
return DDMCommandResult(
|
||||
True,
|
||||
f"Alienware DDM slot {slot} was instructed to switch to {ddm_input}.",
|
||||
output,
|
||||
targeted_output = self._run_logged_command(
|
||||
monitor_selector,
|
||||
"/WriteActiveInput",
|
||||
ddm_input,
|
||||
)
|
||||
targeted_line = _extract_result_line_fragment(targeted_output, "WRITEACTIVEINPUT")
|
||||
if not _output_has_failure(targeted_line, targeted_output):
|
||||
return DDMCommandResult(
|
||||
True,
|
||||
f"Alienware {ALIENWARE_MODEL_TOKEN} was instructed to switch to {ddm_input}.",
|
||||
targeted_output,
|
||||
)
|
||||
|
||||
if slot > 0:
|
||||
legacy_output = self._run_logged_command(f"/{slot}:WriteActiveInput", ddm_input)
|
||||
legacy_line = _extract_result_line(legacy_output, f"{slot}:WriteActiveInput")
|
||||
if not _output_has_failure(legacy_line, legacy_output):
|
||||
return DDMCommandResult(
|
||||
True,
|
||||
f"Alienware DDM slot {slot} was instructed to switch to {ddm_input}.",
|
||||
legacy_output,
|
||||
)
|
||||
merged_output = "\n".join(
|
||||
block
|
||||
for block in [
|
||||
"[targeted]",
|
||||
targeted_output.strip(),
|
||||
"[legacy]",
|
||||
legacy_output.strip(),
|
||||
]
|
||||
if block
|
||||
)
|
||||
return DDMCommandResult(False, "DDM rejected the WriteActiveInput command.", merged_output)
|
||||
|
||||
return DDMCommandResult(False, "DDM rejected the WriteActiveInput command.", targeted_output)
|
||||
|
||||
def _run_logged_command(self, *command_args: str) -> str:
|
||||
assert self.executable_path is not None
|
||||
@@ -148,3 +179,25 @@ def _extract_result_line(text: str, prefix: str) -> str:
|
||||
if normalized.startswith(prefix):
|
||||
return normalized
|
||||
return ""
|
||||
|
||||
|
||||
def _extract_result_line_fragment(text: str, fragment: str) -> str:
|
||||
needle = fragment.upper()
|
||||
for line in text.splitlines():
|
||||
normalized = line.strip()
|
||||
if needle in normalized.upper():
|
||||
return normalized
|
||||
return ""
|
||||
|
||||
|
||||
def _output_has_failure(line: str, output: str) -> bool:
|
||||
normalized_line = line.upper()
|
||||
normalized_output = output.upper()
|
||||
checks = (
|
||||
"INVALID COMMAND",
|
||||
"UNSUPPORTED",
|
||||
"NOT FOUND",
|
||||
"FAILED",
|
||||
"FAIL",
|
||||
)
|
||||
return any(token in normalized_line or token in normalized_output for token in checks)
|
||||
|
||||
Reference in New Issue
Block a user