How to list active displays (on command line)?
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
With VGA monitor off
Related videos on Youtube
Comments
-
innerand over 1 year
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 over 10 yearsThe 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 over 10 yearsIf 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 over 10 yearsOf 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 over 10 yearsThere 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.
-
falconer over 10 yearsOK. You know what you are doing. I remove my previous comments to be less messy here.
-
-
innerand over 10 yearsWith this I could find out which displays are connected. But I want to know if a specific display is currently active.
-
innerand over 10 yearsAs 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,...)
-
s.m over 10 yearsThere 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
-
Sharkytrs over 10 yearshmm 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...
-
innerand over 10 yearsDoesn'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)
-
Sharkytrs over 10 yearsim 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 over 10 yearstry 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 over 10 yearsAHA! 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
-
innerand over 10 yearsI 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.
-
Sharkytrs over 10 yearsthen 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.
-
innerand over 10 yearsNo, 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.
-
Raphael Ahrens almost 8 yearsIf you write
if (command | pipe); then ...
the command will be executed in a subshell. You should just drop the brackets.if command | pipe ;then ...