#!/usr/bin/python3

import re
import sys
import subprocess

CONNECTOR_RE = re.compile(
    r"\n(?P<Name>[\w\-]+)"
    r"(?:\s+.*?"
    r"SignalFormat:\s+(?P<SignalFormat>[\w\-_]+)\s+.*?"
    r"ConnectorType:\s+(?P<ConnectorType>[\w\-_]+))?", re.S)
SVIDEO_RE = re.compile(r"s\-?video|din|cv", re.I)
DP_RE = re.compile(r"dp|displayport", re.I)


def main():
    try:
        xrandr_output = subprocess.check_output(
            ["xrandr", "-q", "--verbose"], universal_newlines=True)
    except subprocess.CalledProcessError:
        return 0

    xrandr_output = "\n" + ''.join(xrandr_output.splitlines(True)[1:])
    supported_connections = dict()

    for m in CONNECTOR_RE.finditer(xrandr_output):
        name = m.group('Name').lower()
        signal_format = connector_type = ''  # RandR 1.3 only
        if m.group('SignalFormat'):
            signal_format = m.group('SignalFormat').lower()
        if m.group('ConnectorType'):
            connector_type = m.group('ConnectorType').lower()

        if name.startswith('vga'):
            supported_connections['vga'] = 'supported'

        elif name.startswith('dvi'):
            supported_connections['dvi'] = 'supported'

        elif DP_RE.match(name):
            # HDMI uses TMDS links, DisplayPort (DP) uses LVDS pairs (lanes)
            # to transmit micro data packets.
            # Reported by NVIDIA proprietary drivers (version >= 302.17)
            if signal_format == 'tmds':
                supported_connections['hdmi'] = 'supported'
            else:
                supported_connections['dp'] = 'supported'

        elif name.startswith('hdmi'):
            supported_connections['hdmi'] = 'supported'

        elif SVIDEO_RE.match(name):
            supported_connections['svideo'] = 'supported'

        elif name.startswith('rca'):
            supported_connections['rca'] = 'supported'

        elif name.startswith('tv'):
            if SVIDEO_RE.match(signal_format):
                supported_connections['svideo'] = 'supported'
            else:
                supported_connections['tv'] = 'supported'

        # ATI/AMD proprietary FGLRX graphics driver codenames:
        elif name.startswith('crt') or name.startswith('dfp'):
            if connector_type.startswith('dvi'):
                supported_connections['dvi'] = 'supported'
            elif connector_type.startswith('vga'):
                supported_connections['vga'] = 'supported'
            elif DP_RE.match(connector_type) and DP_RE.match(signal_format):
                supported_connections['dp'] = 'supported'
            else:
                # HDMI ports may appear as unknown
                # (for both signal format and connector type).
                supported_connections['hdmi'] = 'supported'

    for connector in supported_connections.keys():
        print("%s: supported" % connector)

    return 0


if __name__ == "__main__":
    sys.exit(main())
