Automatic HDMI Device Detection

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
brand10
Experienced User
Posts: 62
Joined: Mon Mar 29, 2010 10:31 am

Automatic HDMI Device Detection

Post by brand10 » Mon Sep 26, 2011 1:05 pm

Hi there,

I want to use eventghost for the following situation:
My HTPC is connected to my AV-Receiver, which is connected to the LCD-TV. All over HDMI.
The AV-Receiver has the "HDMI Passthrough" feature, which allows to passthrough the HDMI Signal to the TV, even if the AV-Receiver is turned off.

At the moment i need to switch between "Dolby Digital Passthrough" (if AV-Receiver is turned ON) and "Decode to Stereo" (if AV is turned OFF) in ffdshow Audio manually.

Is it possible for eventghost to detect the connected Display Device and switch the settings automatically? The Display Device shows the Name of the TV if AV is turned off, and the name of the AV if its turned ON. So all i need is a plugin which triggers an event if a certain Display device is found.

If theres no plugin, is there some documentation or do u have any tips how i could write one by myself?

xavier
Posts: 6
Joined: Mon Oct 11, 2010 9:36 am

Re: Automatic HDMI Device Detection

Post by xavier » Mon Sep 26, 2011 9:53 pm

brand10 wrote:Is it possible for eventghost to detect the connected Display Device and switch the settings automatically? The Display Device shows the Name of the TV if AV is turned off, and the name of the AV if its turned ON. So all i need is a plugin which triggers an event if a certain Display device is found.
You can do whatever you want but you need additional hardware.

I'm using for a long time now the RCAware HDMI-CEC to USB bridge, with its plugin, and is extremely powerful.

It handles all events in the HDMI chain, allowing EG to interpret and interact with them.

This is an example of how the RCAware plugin reads a common HDMI chain:

Image

Each of the elements of the chain generates events on the bus HDMI, you can use these events to handle any action in EG.

Regards,
xavier.

brand10
Experienced User
Posts: 62
Joined: Mon Mar 29, 2010 10:31 am

Re: Automatic HDMI Device Detection

Post by brand10 » Mon Sep 26, 2011 11:46 pm

Well, this would be a solution, but i don't want to spend some money for it.
All i need is a "pc local" detection which detects, which display device is connected currently.

xavier
Posts: 6
Joined: Mon Oct 11, 2010 9:36 am

Re: Automatic HDMI Device Detection

Post by xavier » Tue Sep 27, 2011 4:27 pm

brand10 wrote:Well, this would be a solution, but i don't want to spend some money for it.
All i need is a "pc local" detection which detects, which display device is connected currently.
The HDMI output of your PC does not normally have the ability to read data in transit on the CEC bus.

That's why I told you to buy additional hardware.

I could solve by connecting your PC to your TV via the VGA cable, but obviously you would have lower quality video image.

Regards,
xavier.

hotbuddha
Experienced User
Posts: 99
Joined: Tue Mar 29, 2011 5:14 am

Re: Automatic HDMI Device Detection

Post by hotbuddha » Wed Sep 28, 2011 4:59 pm

You are in luck. The CEC physical address of the computer is provided in the EDID data and is probably the only piece of data needed for CEC which is available without additional hardware. Here is a piece of code taken from my RCAware plugin. You can turn use this in a Python script and call it once per second (or whatever) with a timer. Your code would compare the previous physical address with the newly detected one and when it changes raise an event, or directly continue to process whatever it is you want to do in Python. It requires you import wmi.py (attached). You could turn this into a plugin pretty easily...

Code: Select all

class DetPhysAddr(eg.ActionBase):
    name = 'Determine Physical Address'
    description = """\
    Find the current physical address; returns physical address of the active monitor."""

    def __call__(self):
        pAddr=None

        def parse_edid(pEdid):
            p1=None
            p2=None
            addr=None
            for i in range(252):
                try:
                    if [pEdid[i]+pEdid[i+1]+pEdid[i+2]] == ['\x03\x0c\x00']:
                        try:
                            p1=hex(ord(pEdid[i+3]))+'00'
                        except:
                            p1=[pEdid[i+3]]+'00'
                        try:
                            p2=hex(ord(pEdid[i+4]))+'00'
                        except:
                            p2=pEdid[i+4]+'00'
                        addr=p1[2]+'.'+p1[3]+'.'+p2[2]+'.'+p2[3]
                        break
                except:
                    pass
            return addr

        if sys.platform == "win32":
            if sys.getwindowsversion() >= (6, ):
                # Use WMI for Vista/Win7
                import wmi
                wmi_connection = wmi.WMI(namespace="WMI")
            else:
                # Use registry as fallback for Win2k/XP/2003
                import _winreg
                wmi_connection = None
            import win32api

        edid = None
        if sys.platform == "win32":
            try:
                if wmi_connection:
                    for monitor in wmi_connection.WmiMonitorDescriptorMethods():
                        try:
                            edid = monitor.WmiGetMonitorRawEEdidV1Block(1)
                        except:
                            pass
                        else:
                            edid = "".join(chr(i) for i in edid[0])
                            pAddr=parse_edid(edid)
                        if pAddr <> None:
                            break
                else:
                    for monitor in win32api.EnumDisplayMonitors(None, None):
                        moninfo = win32api.GetMonitorInfo(monitor[0])
                        device = win32api.EnumDisplayDevices(moninfo["Device"])
                        id = device.DeviceID.split("\\")[1]
                        driver = "\\".join(device.DeviceID.split("\\")[-2:])
                        subkey = "\\".join(["SYSTEM", "CurrentControlSet", "Enum", "DISPLAY", id])
                        key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey)
                        numsubkeys, numvalues, mtime = _winreg.QueryInfoKey(key)
                        for i in range(numsubkeys):
                            hkname = _winreg.EnumKey(key, i)
                            hk = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "\\".join([subkey, hkname]))
                            if _winreg.QueryValueEx(hk, "Driver")[0] == driver:
                                devparms = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "\\".join([subkey, hkname, "Device Parameters"]))
                                try:
                                    edid = _winreg.QueryValueEx(devparms, "EDID")[0]
                                    pAddr=parse_edid(edid)
                                    break
                                except:
                                    pass
                            if pAddr <> None:
                                break
            except:
                pass

        return pAddr
Attachments
wmi.py
(46.56 KiB) Downloaded 265 times

brand10
Experienced User
Posts: 62
Joined: Mon Mar 29, 2010 10:31 am

Re: Automatic HDMI Device Detection

Post by brand10 » Wed Sep 28, 2011 7:03 pm

Thanks for your script, i will take a look at it.

I found a primitive, but working solution too. Advantage is, that it's very simple:

Code: Select all

from win32api import EnumDisplayDevices

mon = EnumDisplayDevices('\\\\.\\DISPLAY1', 0, 0)

if mon.DeviceID.find('Toshiba') > -1:
    eg.result = "Toshiba LCD-TV found"
else:
    eg.result = None
Together with the two standard Eventghost actions: "if successful jump" and "if not successful jump" its possible to trigger some actions, depending on the current display device. I trigger this small python script with a timer or from MediaPortal actions (by a MediaPortal Plugin).


P.S.: In which directory i have to place the wmi.py?

User avatar
Livin
Experienced User
Posts: 792
Joined: Wed Oct 08, 2008 4:56 am

Re: Automatic HDMI Device Detection

Post by Livin » Wed Sep 28, 2011 10:15 pm

nice... I'll have to try that this weekend.

your python code should be placed in a Python 'Action'... not an actual file
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system

hotbuddha
Experienced User
Posts: 99
Joined: Tue Mar 29, 2011 5:14 am

Re: Automatic HDMI Device Detection

Post by hotbuddha » Thu Sep 29, 2011 12:34 am

You could put that into a python script action or into a .py file and put build a plugin around it.

brand10
Experienced User
Posts: 62
Joined: Mon Mar 29, 2010 10:31 am

Re: Automatic HDMI Device Detection

Post by brand10 » Thu Sep 29, 2011 2:19 am

Sorry, me again: Where i have to place the wmi.py ?

User avatar
Livin
Experienced User
Posts: 792
Joined: Wed Oct 08, 2008 4:56 am

Re: Automatic HDMI Device Detection

Post by Livin » Thu Sep 29, 2011 5:20 am

unless you want to build a complete plugin (lots more work) you will not use a .py file. Place your code in a SCRIPT ACTION
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system

brand10
Experienced User
Posts: 62
Joined: Mon Mar 29, 2010 10:31 am

Re: Automatic HDMI Device Detection

Post by brand10 » Thu Sep 29, 2011 7:26 am

Yes, i know. But wmi.py looks like a library, which should be placed somewhere, so i can "import" it from a script action.

User avatar
Livin
Experienced User
Posts: 792
Joined: Wed Oct 08, 2008 4:56 am

Re: Automatic HDMI Device Detection

Post by Livin » Thu Sep 29, 2011 3:30 pm

brand10 wrote:Yes, i know. But wmi.py looks like a library, which should be placed somewhere, so i can "import" it from a script action.
gotcha.. I was missing your point :)

Post a new thread with the question... how to use a library - or something like that.
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system

hotbuddha
Experienced User
Posts: 99
Joined: Tue Mar 29, 2011 5:14 am

Re: Automatic HDMI Device Detection

Post by hotbuddha » Tue Oct 04, 2011 2:13 am

Any .py files you wish to import for use in an EventGhost Python Script action can just be placed into the Program files\EventGhost directory.

nating
Posts: 5
Joined: Thu Oct 20, 2011 3:28 pm

Re: Automatic HDMI Device Detection

Post by nating » Sat Oct 22, 2011 8:16 pm

I am not having with problems with my hdmi detecting hardware, fortunately. Though there could be an issue with some components running an ample amount slower because of a lot of ports waiting to detect which hardware is plugged in. That way, it makes for a lot tougher time in detecting the hardware the remote will listen to.

I think it would be better if I could write some external plug-in to oversee the whole selection process.

Post Reply