4

I'm writing a script in which I need to switch between two displays, but I can't find a way to get the information about the current active output.

Pretty much like this:

if Display0 is off and Display1 is on
     turn off Display1
     turn on Display0
else if Display1 is off and Display0 is on
     turn off Display0
     turn on Display1

The piece I'm missing is the first line: how do I know which display is currently active?

Note: xrandr -q doesn't help because it always print that both displays are "connected" regardless of whether the output is directed on Display0 or Display1, not sure if this is a bug.

oidualc
  • 1,453
  • Can you give the output of echo $DISPLAY? when both display connected and also when 1th is connected and then when 2th connected. (each result in separate line). thank yoou – αғsнιη Dec 27 '14 at 11:33
  • @KasiyA echo $DISPLAY returns :0 regardless of the connected display – oidualc Dec 27 '14 at 11:49
  • It that the same result when you connect both of the displays? – αғsнιη Dec 27 '14 at 11:54
  • What does DISPLAY=:0 xrandr | grep primary say? – muru Dec 27 '14 at 12:01
  • @KasiyA yes, same result when both displays are connected. As far as I can see the DISPLAY variable isn't affected at all by the disposition – oidualc Dec 27 '14 at 12:03
  • @muru DVI-0 connected primary... when DVI-0 is primary according to gnome-control-center display, DVI-1 connected primary... when DVI-1 is primary. This information though, still doesn't tell me if the non-primary is either on or off – oidualc Dec 27 '14 at 12:13
  • How do you turn off the display? Which command? xset? – muru Dec 27 '14 at 12:14
  • @muru I just use the mighty gnome-control-center – oidualc Dec 27 '14 at 12:15
  • @oidualc Post the content of this file gedit ~/.config/monitors.xml in body of your question.(connect both display) – αғsнιη Dec 27 '14 at 12:33
  • @KasiyA First of all thank you for your help. Pasting the content of this file would bloat the question, I would like to keep it as simple as possible, of course including all the necessary information. I'd like to know a command that tells me if a display is on or off, if such command exists (and I would be surprised if it doesn't). May I know what are you looking for in monitors.xml? – oidualc Dec 27 '14 at 13:22
  • @oidualc No problem all we are here to help users like you and when we ask you post the output of command we just wants to ensure how we can answer your question from your replies of commands. And I'm looking for any elation between your monitors in that file like <primary>yes</primary> tag between and if it possible making answer for your question. if you see the content of that file you will see what monitors is primary and which on is not. see the example of that in this answer – αғsнιη Dec 27 '14 at 13:30
  • @KasiyA when turning off a display, some related tags in monitors.xml including <primary>yes</primary> are deleted. However this doesn't help, because in order to extract this information from the file and put it in the script I'd need an xml parser, this would be overkill! – oidualc Dec 27 '14 at 13:59

1 Answers1

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

here is the command to find which monitors are on, which is your original question.

Also I wrote the script to switch between monitors: Swap between monitor display modes using shortcut

hyamanieu
  • 248
  • 4
  • 16