Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:ressources:astuces:udev_hdmi [2014/06/08 15:15] (current)
Line 1: Line 1:
 +====== Toggle an external HDMI display using udev ======
  
 +Use **udevadm monitor** to detect udev events when connecting or disconnecting the display:
 +
 +<​code>​
 +$ udevadm monitor
 +monitor will print the received events for:
 +UDEV - the event which udev sends out after rule processing
 +KERNEL - the kernel uevent
 +
 +<​connecting the display>
 +KERNEL[549920.709264] change ​  /​devices/​pci0000:​00/​0000:​00:​02.0/​drm/​card0 (drm)
 +UDEV  [549920.712808] change ​  /​devices/​pci0000:​00/​0000:​00:​02.0/​drm/​card0 (drm)
 +
 +<​disconnecting the display>
 +KERNEL[549932.063215] change ​  /​devices/​pci0000:​00/​0000:​00:​02.0/​drm/​card0 (drm)
 +UDEV  [549932.067059] change ​  /​devices/​pci0000:​00/​0000:​00:​02.0/​drm/​card0 (drm)
 +</​code>​
 +
 +Use **xrandr** to automatically extend the desktop to the new display:
 +<​code>​
 +$ xrandr --output HDMI1 --auto --above LVDS1
 +</​code>​
 +Or remove the HDMI display:
 +<​code>​
 +$ xrandr --output HDMI1 --off
 +</​code>​
 +
 +Now put all that into a udev rules:
 +<​code>​
 +$ sudo -s
 +# cat /​etc/​udev/​rules.d/​hdmi.rules
 +SUBSYSTEM=="​drm",​ ACTION=="​change",​ RUN+="/​usr/​local/​bin/​hdmi_toggle"​
 +</​code>​
 +
 +and create the script at **/​usr/​local/​bin/​hdmi_toggle**:​
 +<​file>​
 +#​!/​usr/​bin/​env bash
 +
 +# grab the display and xauthority cookie
 +export DISPLAY=$(w -h -s | grep ":​[0-9]\W"​ | head -1 | awk '​{print $2}')
 +X_USER=$(w -h -s | grep ":​[0-9]\W"​ | head -1 | awk '​{print $1}')
 +export XAUTHORITY=/​home/​$X_USER/​.Xauthority
 +
 +# get the status of the external display
 +status=$(cat /​var/​run/​hdmi1_status)
 +case $status in
 + connected)
 + #​disconnect
 + /​usr/​bin/​su $X_USER -c '/​bin/​xrandr --output HDMI1 --off'
 + echo disconnected > /​var/​run/​hdmi1_status
 + /​usr/​bin/​logger "​disabled hdmi output"​
 + ;;
 + *)
 + #connect
 + /​usr/​bin/​su $X_USER -c '/​bin/​xrandr --output HDMI1 --auto --above LVDS1'
 + echo connected > /​var/​run/​hdmi1_status
 + /​usr/​bin/​logger "​enabled hdmi output"​
 + ;;
 +esac
 +</​file>​
 +Make it executable:
 +<​code>​
 +# chmod +x /​usr/​local/​bin/​hdmi_toggle
 +</​code>​
 +
 +And that's it. Enjoy connecting and disconnecting your display at will. If you want another display layout, replace the **--above** part of the xrandr command with {--left-of, --right-of, --below}.
en/ressources/astuces/udev_hdmi.txt ยท Last modified: 2014/06/08 15:15 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0