LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-09-2013, 11:05 AM   #1
akamikeym
Member
 
Registered: May 2008
Posts: 112

Rep: Reputation: 21
Using udev events and edid to detect a specific monitor being plugged in


Hi,

I'm trying to use udev to detect an external monitor being plugged in and then by identifying the monitors edid info identify which monitor it is and set an appropriate resolution for it.

I've looked at what others have done and mostly come up with something that works but I'm stuck because I can't seem to be able to read the monitors edid from the udev hotplug script that I'm running.

The 2 files I have are:

/etc/udev/rules.d/80-vga.rules
Code:
ACTION=="change", SUBSYSTEM=="drm", RUN+="/etc/udev/scripts/hotplug.sh"
and:


/etc/udev/scripts/hotplug.sh
Code:
#!/bin/bash - 

# Treat unset variables as an error
set -u

# append STERR and STOUT to log file
exec &>> /var/log/hotplug.log

# read the status of my external VGA port
read status < /sys/class/drm/card0-VGA-1/status 

# export the X11 variables for my one system user 
export DISPLAY=:0
export XAUTHORITY=/home/mikey/.Xauthority

# are we connected to an external monitor?
if [ "$status" = "connected" ]; then

    # read in the interesting parts of parse-edid (from the read-edid package)
    IFS=$'\n' 
    edid=( $( parse-edid /sys/class/drm/card0-VGA-1/edid | 
              sed -n -e 's/"[^"]*$//' -e 's/\s*Mode\s\+"\|\s*VendorName\s\+"\|\s*ModelName\s\+"//p' ) )

    # default to using automatic modes (dosen't work for my TV)
    settings="--auto"

    # for the modes listed by edid see if there is a sitable one
    for (( i=2; i<${#edid[@]}; i++)); do
        # for debuging show what modes we have
        echo "Mode: ${edid[$i]}"

        # select a mode if it starts with 1280x???
        if [[ "${edid[$i]}" =~ ^1280x.*  ]]; then
            # set the mode from the edid info
            settings="--mode ${edid[$i]} --pos 0x0 --rotate normal"
        fi
    done

    # output the settings chosen
    if [ "${#edid[@]}" -ge 2 ]; then 
        echo "Detected monitor ${edid[0]} ${edid[1]} and selected: $settings"
    else 
        echo "Detected monitor but no EDID found, selected: $settings"
    fi

    # use xrandr to set the mode on the external monitor and turn of the internal
    IFS=' '
    /usr/bin/xrandr --output LVDS1 --off --output VGA1 --screen 0 $settings
    echo "Connected"
else
    # use xrandr to turn on the internal monitor and turn of the external
    /usr/bin/xrandr --output VGA1 --off --output LVDS1 --auto --screen 0
    echo "Disconnected"
fi
But while this script works without issue when called manually as soon as a monitor is put in parse-edid fails to retrieve any edid info when called by udev. Even using a simpler detection method of calling md5sum on the edid file fails to return the md5 that matches the monitor when called by udev.

I've even tried introducing a sleep statement into the script before trying to read the edid in case it was being called to early but it behaves in exactly the same way.

It's like the edid file (/sys/class/drm/card0-VGA-1/edid) isn't set until this script exits, which is odd since the other file the script checks from the same location (/sys/class/drm/card0-VGA-1/status) is happily set.

I wonder if any of the LQ gurus (or just someone a bit savier than me) can shed light on this phenomena.
 
Old 11-10-2013, 05:16 AM   #2
akamikeym
Member
 
Registered: May 2008
Posts: 112

Original Poster
Rep: Reputation: 21
Well I've come up with something that is behaving a bit like my script above is trying to but without actually detecting the edid which is still a mystery.

It uses the output of xrandr to pick what it thinks might be a suitable mode, rather than selecting a mode based on monitor identifier which I was really trying to achieve.

Code:
#!/bin/bash - 

# Treat unset variables as an error
set -u

# append STERR and STOUT to log file
exec &>> /var/log/hotplug.log

# read the status of my external VGA port
read status < /sys/class/drm/card0-VGA-1/status 

# export the X11 variables for my one system user 
export DISPLAY=:0
export XAUTHORITY=/home/mikey/.Xauthority

# are we connected to an external monitor?
if [ "$status" = "connected" ]; then

    # default to using automatic modes (dosen't work for my TV)
    settings="--auto"

    # use xrandr rather than edid to detect available modes
    IFS=$'\n' 
    modes=$( xrandr | sed -e '1,/^VGA1/d;/^[A-Za-z]/,$d' -e 's/^\s*\([0-9]\+x[0-9]\+\).*$/\1/' )

    # choose a suitable mode if one is found
    for mode in $modes; do
        echo "Mode: $mode"
        if [[ "$mode" =~ ^1280.* ]]; then
            settings="--mode $mode --pos 0x0 --rotate normal"
        fi
    done

    # output the settings choosen
    if [ "${#modes[@]}" -gt 0 ]; then 
        echo "Detected suitable resolution, selected: $settings"
    else 
        echo "Detected monitor falling back to auto detection, selected: $settings"
    fi

    # use xrandr to set the mode on the external monitor and turn of the internal
    IFS=' '
    /usr/bin/xrandr --output LVDS1 --off --output VGA1 --screen 0 $settings
    echo "Connected"
else
    # use xrandr to turn on the internal monitor and turn of the external
    /usr/bin/xrandr --output VGA1 --off --output LVDS1 --auto --screen 0
    echo "Disconnected"
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Second monitor, EDID checksum is invalid Zilvermeeuw Linux - Hardware 7 07-04-2012 01:14 PM
RedHat: Monitor specific security events and send notifications about them rstev39147 Linux - Security 8 08-03-2008 08:44 PM
Hang on triggering udev events- is there a buildup of events? sonichedgehog Slackware 20 07-11-2008 02:49 AM
How to detect programatically if a USB device is is plugged-in / plugged out? franc Linux - Software 3 02-01-2007 04:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration