add "fail_ok" to config

This commit is contained in:
Daniel Langbein 2022-05-23 13:26:09 +02:00
parent 32acf8af44
commit 057b655470
2 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{ {
"screens": ["eDP", "HDMI-A-0", "eDP-1"], "screens": ["eDP", "HDMI-A-0", "eDP-1"],
"devices": [ "devices": [
{"screen": "eDP-1", "name_contains": "ELAN9038:00"} {"screen": "eDP-1", "name_contains": "ELAN9038:00", "fail_ok": true}
] ]
} }

View File

@ -3,6 +3,7 @@
import json import json
import subprocess import subprocess
import re import re
import sys
from pathlib import Path from pathlib import Path
from typing import NamedTuple from typing import NamedTuple
@ -12,6 +13,7 @@ orientations = ['normal', 'right', 'inverted', 'left']
class Device(NamedTuple): class Device(NamedTuple):
name: str name: str
id: int id: int
fail_ok: bool = False
class Screen(NamedTuple): class Screen(NamedTuple):
@ -44,7 +46,13 @@ def rotate_clockwise():
def rotate(screen: Screen, orientation): def rotate(screen: Screen, orientation):
Xrandr.rotate(screen.name, orientation) Xrandr.rotate(screen.name, orientation)
for device in screen.devices: for device in screen.devices:
Xinput.map_to_output(device.id, screen.name) try:
Xinput.map_to_output(device.name, screen.name)
except Exception as e:
if device.fail_ok:
print(f"Mapping of {device.name} to {screen.name} failed", file=sys.stderr)
else:
raise e
class Config: class Config:
@ -61,6 +69,8 @@ class Config:
{"screen": "eDP-1", "name": "ELAN9038:00"}, {"screen": "eDP-1", "name": "ELAN9038:00"},
# ... or by `name_contains`. # ... or by `name_contains`.
{"screen": "eDP-1", "name_contains": "ELAN9038"} {"screen": "eDP-1", "name_contains": "ELAN9038"}
# If `fail_ok` is set to true, errors during mapping of device to screen are ignored.
{"screen": "eDP-1", "name_contains": "ELAN9038", "fail_ok": true}
] ]
} }
""" """
@ -78,6 +88,8 @@ class Config:
for device in self.devices: for device in self.devices:
if device["screen"] != screen: if device["screen"] != screen:
continue continue
fail_ok = device["fail_ok"] if "fail_ok" in device else False
if "name" in device: if "name" in device:
match = None match = None
for x_dev in x_devs: for x_dev in x_devs:
@ -86,11 +98,13 @@ class Config:
break break
if match is None: if match is None:
raise Exception(f"Device {device['name']} not found.") raise Exception(f"Device {device['name']} not found.")
match.fail_ok = fail_ok
matches.append(match) matches.append(match)
if "name_contains" in device: if "name_contains" in device:
matched = False matched = False
for x_dev in x_devs: for x_dev in x_devs:
if device["name_contains"] in x_dev.name: if device["name_contains"] in x_dev.name:
x_dev.fail_ok = fail_ok
matches.append(x_dev) matches.append(x_dev)
matched = True matched = True
if not matched: if not matched:
@ -169,8 +183,8 @@ class Xrandr:
class Xinput: class Xinput:
@classmethod @classmethod
def map_to_output(cls, device_id: int, screen: str): def map_to_output(cls, device_name_or_id: str | int, screen: str):
execute(['xinput', '--map-to-output', str(device_id), screen]) execute(['xinput', '--map-to-output', str(device_name_or_id), screen])
@classmethod @classmethod
def get_devices(cls) -> list[Device]: def get_devices(cls) -> list[Device]:
@ -197,7 +211,8 @@ def execute(command: list[str]) -> str:
text=True, text=True,
) )
if completed.returncode != 0: if completed.returncode != 0:
raise Exception(f'Exit Code: {completed.returncode}\n' raise Exception(f'Command: {command}\n'
f'Exit Code: {completed.returncode}\n'
f'Stdout:\n{completed.stdout}\n' f'Stdout:\n{completed.stdout}\n'
f'Stderr:\n{completed.stderr}') f'Stderr:\n{completed.stderr}')
return completed.stdout return completed.stdout