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.

innerand
  • 121
  • 1
  • 1
  • 10
  • 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 '14 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 '14 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 '14 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 '14 at 21:57
  • OK. You know what you are doing. I remove my previous comments to be less messy here. – falconer Jan 30 '14 at 13:38

4 Answers4

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
0
xrandr --listactivemonitors | awk '{print $NF}'
undg
  • 101
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.

s.m
  • 2,229
  • 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 '14 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 '14 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 ... – Raphael Ahrens May 24 '16 at 07:19
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.

Sharkytrs
  • 21
  • 4
  • 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 '14 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 '14 at 16:57
  • try xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1 – Sharkytrs Jan 29 '14 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 '14 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 '14 at 19:23
  • im not sure tha xrandr can give you that info then. Maybe another command, or query there of can give the information. perhaps you can query what the current focused window is and get the display it is on. though im not sure how that can be done – Sharkytrs Jan 29 '14 at 19:42
  • try querying xprop as it can give you a whle host of info about where the active window at the moment is. I can't test it as I have no other display, but im sure the desktop value would change depending on which monitor it is queryed on – Sharkytrs Jan 29 '14 at 19:51
  • AHA! I found a useful work around that might help. install xdotool . then you can get which screen the mouse is currently on with xdotool getmouselocation – Sharkytrs Jan 29 '14 at 19:59
  • I don't want to know where the mouse is. I want to know if the system uses a connected display at the moment. E.g. with xrandr --output HDMI2 --off I can disable a display, with xrandr --output HDMI2 --auto I can activate it. I want to know if it is on or off. – innerand Jan 29 '14 at 20:18
  • then xrandr telling you a screen is connected means it is on. disconnected means off. even if you have them on standby, the output from the graphics card will still be 'connected' to the screen as if it is trying to dispaly something to it. it will only say 'disconnected' if the system is not trying to out put to that screen. – Sharkytrs Jan 29 '14 at 20:29
  • No, connected means connected. You can switch it of with xrandr ... --off and it is still connected, but it's not longer used (no picture is send to it) from the system. – innerand Jan 29 '14 at 21:53