Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux retrieve monitor names

Tags:

Situation: I'm using multiple monitors and I want to get their names in bash. Currently I'm using Ubuntu 10.04.

I know about xrandr. From it I can get only statistics data. What I want is to read all monitor names in an array to work with them.

Is there a clear way to do that without cutting names from some kind of string? A clear way would be reading them from file. A not clear way would be to pipe xrandr output to some sort a function to cut names out from it.

like image 215
Jānis Gruzis Avatar asked May 08 '12 14:05

Jānis Gruzis


2 Answers

Inspired by Beni's answer, this will read the EDID data using xrandr and extract the monitors names according to the EDID specification, with no need of any external tools like parse-edid:

#!/bin/bash while read -r output hex conn; do     [[ -z "$conn" ]] && conn=${output%%-*}     echo "# $output $conn   $(xxd -r -p <<< "$hex")" done < <(xrandr --prop | awk '     !/^[ \t]/ {         if (output && hex) print output, hex, conn         output=$1         hex=""     }     /ConnectorType:/ {conn=$2}     /[:.]/ && h {         sub(/.*000000fc00/, "", hex)         hex = substr(hex, 0, 26) "0a"         sub(/0a.*/, "", hex)         h=0     }     h {sub(/[ \t]+/, ""); hex = hex $0}     /EDID.*:/ {h=1}     END {if (output && hex) print output, hex, conn}     ' | sort ) 

Uses awk to precisely extract the monitor name only, and no extra garbage from the EDID, hence "magic numbers" like 000000fc00, 26 and 0a. Finally uses xxd to convert from hex to ASCII, printing one monitor name per line.

Based on this solution I made a handy script to switch monitors, which can also be used to simply list monitor info:

$ monitor-switch --list Connected monitors: # DFP5  HDMI    HT-R391 # DFP7  DVI-I   DELL U2412M  $ monitor-switch --list Connected monitors: # DisplayPort-1 DisplayPort DELL U2412M # DisplayPort-3 DisplayPort DELL U2415 # HDMI-A-2      HDMI        LG TV 
like image 191
MestreLion Avatar answered Sep 27 '22 20:09

MestreLion


Tested on Ubuntu 16.04, 18.04. (I know its too late to answer but this solution is relevant today)

$ sudo apt-get install -y hwinfo ... $ hwinfo --monitor --short monitor:                    SONY TV                    AUO LCD Monitor 

I have two monitors attached. One with the laptop and the other is an external display. As soon as the external monitor is plugged-in or out, this command reflects the change. You continuously need to poll. Removing the --short option gives more detailed information.

You can poll the state with the following background job:

$ while true; >  do >   hwinfo --monitor --short; >   sleep 2; >  done >> monitor.log & 

The while true loop runs infinite times. The sleep 2 pauses each iteration of the loop for 2 seconds. And the output of hwinfo --monitor --short is appended to monitor.log. This log file can give you the activity history of monitor plug-in and plug-out.

FYI: I am using a background (daemon) python script using the above command (and other similar ones) to detect if someone is doing some HW plug-ins and plug-outs with the systems in the computer lab. If so, I get appropriate notifications that someone plugged-out/in a monitor, mouse or keyboard in almost real-time!

More info about hwinfo command is here. Its man page is also a good source.

like image 28
codeman48 Avatar answered Sep 27 '22 20:09

codeman48