You are not logged in.

#1 2016-07-09 17:20:14

jaggernought
Member
Registered: 2016-07-06
Posts: 2

How to detect monitor connection?

Hello,

I have two computers connected to my KVM switch. One is my holy Arch Box and another is a laptop. I's like to write a script on my Arch machine that can detect when the KVM selects it and runs a command only I don't know how to detect monitor input. Has anybody done this before?

Last edited by jaggernought (2016-07-09 17:40:06)

Offline

#2 2016-07-09 18:24:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,447
Website

Re: How to detect monitor connection?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-07-09 19:19:49

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: How to detect monitor connection?

There are also files under /sys/devices that you can monitor. For example, on my laptop I can monitor

/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/status 

for connection status.

I also have a script with the following:

checksum_="$(sha256sum '/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/edid')"
checksum_="${checksum_%% *}"

case "$checksum_" in
  aba161abb9e52b1dcc4c66b367356d66a87610fa49764b72a97ddb0f9be10733)
    # Do stuff here if monitor A is connected.
  ;;
  ed329858041295cbf1aca3be39874df75551f0ad1f52568550fbbff542282d13)
    # Do other stuff here if monitor B is connected.
  ;;
...
esac

The EDID should be unique for each monitor. I have bound the script to a key so that I can quickly set up my workspace in different locations (using xrandr configuration states).

In your case, I would try creating a script based on inotify to watch the file for changes. Given that files under /sys are not "real files", inotify may not work but if it does it then you can avoid a script that constantly polls the connection status.

edit
Fixed quoting.

Last edited by Xyne (2016-07-09 19:36:48)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#4 2016-07-09 19:44:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,447
Website

Re: How to detect monitor connection?

Xyne wrote:

if it does it then you can avoid a script that constantly polls the connection status.

No need for polling if you can write a few lines of C code.  Xrandr generates x11 events when monitors are plugged.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2016-07-09 19:54:00

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: How to detect monitor connection?

Trilby wrote:
Xyne wrote:

if it does it then you can avoid a script that constantly polls the connection status.

No need for polling if you can write a few lines of C code.  Xrandr generates x11 events when monitors are plugged.

I was unaware. A little C script to watch for the event is likely the best method then. I had hoped that this could be done neatly with a simple bash script but after a little testing I have found that files under /sys/devices do not generate inotify events.

Anyway, here's my inotify script modified to poll instead in case anyone wants to play with it.

#!/bin/bash

# Change this.
WATCHPATH_='/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/status'

# This doesn't work because files under /sys/devices are not "real" files.
# while inotifywait -e modify "$WATCHPATH_"
while true
do
  content_="$(< "$WATCHPATH_")"
  case "$content_" in
    connected)
      # Run setup script here.
    ;;
    disconnected)
      # Undo changes of setup script here, if any.
    ;;
  esac
  # Only needed because inotifywait does not work.
  sleep 1
done

edit
I was curious about how to write the C script. It required some digging to figure out. Although the end result is simple, it is not trivial so I have decided to post it online with 2 usage examples: http://xyne.archlinux.ca/scripts/system … changewait


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB