5

xrandr -q gives me a list of connected displays, but how can I find out (script friendly) if a display is currently active?

Context: I would like to write a script to toggle a Display. If it's active it should be turned off, if it isn't it should be turned on.

Note: xrandr -q basically provides this information since active modes are marked with a *, but this information is hard to extract within a bash script.

5
  • The Display is a TV. E.g. if I want to watch a movie the script should switch the Display to active (and set audio out to hdmi). After I watched the movie the script should turn the display of (and set audio out back to built in speakers). But the script should decide by itself what to do, therefore it needs the current state of the display.
    – innerand
    Jan 29, 2014 at 15:03
  • If the TV is active, the scirpt should turn it off. If the TV isn't active the script should turn it on. And with turn on I mean the system should use it. There are three displays connected (the internal notebook display, an external monitor and the tv, but only the external moinitor is active after boot.)
    – innerand
    Jan 29, 2014 at 19:19
  • Of course it needs that information. "it only has to know that you are starting or stopping watching the movie." Exactly, and therefore the script has to know the actual state of the display. Display On: Movie is finished, set Display off. Display off: Movie will be shown, set Display to on.
    – innerand
    Jan 29, 2014 at 20:23
  • There is no activator and deactivator script. There is only one script and that has do decide for it self if it should activate or deactivate the display, and for that it needs the state of the display. That's it. I won't discuss this any longer.
    – innerand
    Jan 29, 2014 at 21:57
  • OK. You know what you are doing. I remove my previous comments to be less messy here.
    – falconer
    Jan 30, 2014 at 13:38

4 Answers 4

3

The displays that are active have their resolution and offset number shown in the identifying line of xrandr output. Here's what I mean:

$ xrandr | grep connected                                    
eDP1 connected primary 1366x768+1280+256 (normal left inverted right x axis y axis) 345mm x 194mm
DP1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
VGA1 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 340mm x 270mm
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

In the output you can see that my laptop's built-in monitor and VGA1 both are connected, and have resolution ( in case of built-in display eDP1 it is 1366x768 ). Thus the task simply becomes text-processing of the output. For that purpose , I've written a small function that you can use in your scripts or ~/.bashrc:

get_active_monitors()
{
    xrandr | awk '/\ connected/ && /[[:digit:]]x[[:digit:]].*+/{print $1}'
}

Here's test runs:

With VGA monitor on

enter image description here

With VGA monitor off

enter image description here

0

with xrandr in the resolution list the, active display resolution should have an asterisk straight after it, if it is not present then the display is not active.

11
  • As I already mentioned in my description. The Problem is, that it's not comfortable to extract this information within a script (since resolution can change and the asterisk would be at another position, there are some other asterisks from other displays,...)
    – innerand
    Jan 29, 2014 at 14:48
  • hmm tricky, I guess you mean you want to know which display has focus at one moment? It must be possible as synergy does it, even across multiple machines, I'll have to think...
    – Sharkytrs
    Jan 29, 2014 at 16:57
  • try xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1
    – Sharkytrs
    Jan 29, 2014 at 18:55
  • although i can't test it with multiple screens, but this should give a value depending on what display it is run on
    – Sharkytrs
    Jan 29, 2014 at 18:57
  • Doesn't work, and I think connected is the wrong approach, the display is always connected, but the script needs to find out if it 's active (if it's used by the system, if there is shown something on it)
    – innerand
    Jan 29, 2014 at 19:23
0

Get all the Display connected using

 xrandr -q

Then you can create variable of all the display connected like

Display1=VGA1

and so on

then use script

 #!/bin/bash

 if (xrandr | grep "$DISPLAY1 connected"); then
    if (ls | grep status_flag); then  
        xrandr --output $DISPLAY1 --off
        rm status_flag
    else
        xrandr --output $DISPLAY1 --auto
        touch status_flag
    fi
 fi

Executing this script first time will turn on the monitor without knowing monitor is on or off ,and from second time it will turn off if the monitor is on and turn on if the monitor is off.

3
  • With this I could find out which displays are connected. But I want to know if a specific display is currently active.
    – innerand
    Jan 29, 2014 at 12:21
  • There is command called ddccontrol but I have not used this one, you can try to find if gives the off or on status of the Display. Other option without knowing the off/on status of the Display is to use the script that I have edited just now
    – s.m
    Jan 29, 2014 at 14:52
  • If you write if (command | pipe); then ... the command will be executed in a subshell. You should just drop the brackets. if command | pipe ;then ... May 24, 2016 at 7:19
0
xrandr --listactivemonitors | awk '{print $NF}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .