1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from re import sub, finditer
5import subprocess
6
7from odoo.addons.hw_drivers.interface import Interface
8
9
10class DisplayInterface(Interface):
11    _loop_delay = 0
12    connection_type = 'display'
13
14    def get_devices(self):
15        display_devices = {}
16        displays = subprocess.check_output(['tvservice', '-l']).decode()
17        x_screen = 0
18        for match in finditer('Display Number (\d), type HDMI (\d)', displays):
19            display_id, hdmi_id = match.groups()
20            tvservice_output = subprocess.check_output(['tvservice', '-nv', display_id]).decode().strip()
21            if tvservice_output:
22                display_name = tvservice_output.split('=')[1]
23                display_identifier = sub('[^a-zA-Z0-9 ]+', '', display_name).replace(' ', '_') + "_" + str(hdmi_id)
24                iot_device = {
25                    'identifier': display_identifier,
26                    'name': display_name,
27                    'x_screen': str(x_screen),
28                }
29                display_devices[display_identifier] = iot_device
30                x_screen += 1
31
32        if not len(display_devices):
33            # No display connected, create "fake" device to be accessed from another computer
34            display_devices['distant_display'] = {
35                'name': "Distant Display",
36            }
37
38        return display_devices
39