2019-02-04 17:57:34 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
{{ ansible_managed }}
|
|
|
|
|
|
|
|
This Script changes the monitor in KODI's guisettings.xml to the wanted output
|
|
|
|
according to the DISPLAY environment variable. It works with KODI 18 (not KODI 17!).
|
2019-02-04 22:18:48 +01:00
|
|
|
|
|
|
|
In order to change the display we need to modify the settings/videoscreen nodes.
|
|
|
|
|
|
|
|
Basic algorithm:
|
|
|
|
- get the current videoscreen.monitor
|
|
|
|
- check if it needs to be changed
|
|
|
|
- create a backup of the videoscreen nodes in /var/lib/vdr/.kodi/.display_cache/{CONNETOR}-videoscreen.xml
|
|
|
|
- check if there is an existing backup for the new CONNECTOR
|
|
|
|
- parse the backup of the videoscreen nodes
|
|
|
|
- replace the videoscreen nodes with the backup data
|
2019-02-04 17:57:34 +01:00
|
|
|
"""
|
|
|
|
|
2019-02-04 22:18:48 +01:00
|
|
|
import copy
|
2019-02-04 17:57:34 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2019-02-04 22:18:48 +01:00
|
|
|
from lxml import etree as ET
|
2019-02-04 17:57:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
GUISETTINGS = '/var/lib/vdr/.kodi/userdata/guisettings.xml'
|
|
|
|
CACHE_DIR = '/var/lib/vdr/.kodi/.display_cache'
|
|
|
|
VIDEOSCREEN_TEMPLATE = """<settings version="2">
|
2019-02-04 22:18:48 +01:00
|
|
|
<setting id="videoscreen.monitor">{}</setting>
|
2019-02-04 17:57:34 +01:00
|
|
|
</settings>"""
|
|
|
|
|
|
|
|
|
2019-02-04 22:18:48 +01:00
|
|
|
def create_cache_dir():
|
|
|
|
try:
|
|
|
|
os.makedirs(CACHE_DIR, exist_ok=True)
|
|
|
|
except PermissionError:
|
|
|
|
sys.exit(f"Error: insufficient permissions to create cachedir {CACHE_DIR}")
|
|
|
|
except Exception as e:
|
|
|
|
sys.exit(f"Unexpected Error when trying to create {CACHE_DIR}:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_output_name():
|
2019-02-04 17:57:34 +01:00
|
|
|
"""
|
|
|
|
get display name from xrandr output for given DISPLAY environment variable
|
|
|
|
"""
|
2019-02-04 22:18:48 +01:00
|
|
|
try:
|
|
|
|
xrandr_output = [
|
|
|
|
l for l in subprocess.check_output(
|
|
|
|
["xrandr"],
|
|
|
|
env={"DISPLAY": os.environ["DISPLAY"]}
|
|
|
|
).decode("utf-8").splitlines()
|
|
|
|
]
|
|
|
|
return next(l.split()[0] for l in xrandr_output if " connected " in l)
|
|
|
|
except Exception as e:
|
|
|
|
sys.exit("could not determine output name", e)
|
2019-02-04 17:57:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_template(template_path, template, output=""):
|
|
|
|
"""read videoscreen settings from backup or create a stub file"""
|
|
|
|
try:
|
|
|
|
xml_tree = ET.parse(template_path)
|
2019-02-04 22:18:48 +01:00
|
|
|
except OSError:
|
|
|
|
print(f"{template_path} not found, creating stub file", file=sys.stderr)
|
2019-02-04 17:57:34 +01:00
|
|
|
xml_template = ET.fromstring(template.format(output))
|
|
|
|
xml_tree = ET.ElementTree(xml_template)
|
|
|
|
finally:
|
|
|
|
xml_tree.write(template_path)
|
|
|
|
return xml_tree
|
|
|
|
|
|
|
|
|
2019-02-04 22:18:48 +01:00
|
|
|
def main(output):
|
|
|
|
guisettings = parse_template(GUISETTINGS, VIDEOSCREEN_TEMPLATE, "Default")
|
|
|
|
|
|
|
|
# parse guisettings Etree for display name an backup videoscreen data
|
|
|
|
root = guisettings.getroot()
|
|
|
|
old_output = root.find("./setting[@id='videoscreen.monitor']").text
|
|
|
|
if old_output == output:
|
|
|
|
print("no changes necessary, exiting", file=sys.stderr)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# create a minimal guisettings etree
|
|
|
|
xml_path = os.path.join(CACHE_DIR, f'{old_output}-videoscreen.xml')
|
2019-02-04 17:57:34 +01:00
|
|
|
base_tree = ET.fromstring('<settings version="2"></settings>')
|
|
|
|
xml_tree = ET.ElementTree(base_tree)
|
|
|
|
backup_root = xml_tree.getroot()
|
|
|
|
|
2019-02-04 22:18:48 +01:00
|
|
|
# copy videoscreen elements to backup etree
|
|
|
|
videoscreen_elements = root.xpath(
|
|
|
|
"./setting[starts-with(@id, 'videoscreen.')]")
|
|
|
|
for element in videoscreen_elements:
|
|
|
|
backup_root.append(copy.deepcopy(element))
|
|
|
|
element.getparent().remove(element)
|
|
|
|
xml_tree.write(xml_path)
|
|
|
|
print(f"written backup for {old_output} to {xml_path}", file=sys.stderr)
|
2019-02-04 17:57:34 +01:00
|
|
|
|
2019-02-04 22:18:48 +01:00
|
|
|
# change videoscreen node to content of backup file
|
|
|
|
xml_path = os.path.join(CACHE_DIR, f'{output}-videoscreen.xml')
|
|
|
|
videodir_xml = parse_template(xml_path, VIDEOSCREEN_TEMPLATE, output)
|
|
|
|
videodir_root = videodir_xml.getroot()
|
|
|
|
videoscreen = videodir_root.find("./setting[@id='videoscreen.monitor']")
|
|
|
|
# copy videoscreen.* elements from Backup
|
|
|
|
videoscreen_elements = videodir_root.xpath(
|
|
|
|
"./setting[starts-with(@id, 'videoscreen.')]")
|
|
|
|
for element in videoscreen_elements:
|
|
|
|
new_element = copy.deepcopy(element)
|
|
|
|
root.append(new_element)
|
|
|
|
guisettings.write(GUISETTINGS)
|
2019-02-04 17:57:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-02-04 22:18:48 +01:00
|
|
|
create_cache_dir()
|
|
|
|
output = get_output_name()
|
2019-02-04 17:57:34 +01:00
|
|
|
try:
|
2019-02-04 22:18:48 +01:00
|
|
|
main(output)
|
2019-02-04 17:57:34 +01:00
|
|
|
except Exception as e:
|
2019-02-04 22:18:48 +01:00
|
|
|
print("Could not change videoscreen.* settings:", str(e), file=sys.stderr)
|