Support us on Patreon to keep GamingOnLinux alive. This ensures all of our main content remains free for everyone. Just good, fresh content! Alternatively, you can donate through PayPal. You can also buy games using our partner links for GOG and Humble Store.
Turning one screen off and turn on another, might be usefull for couchgamers.
ysblokje Aug 3, 2016
Why?
On the telegram chat group somebody asked for a simple solution to turning on/off one of two screens.
This is especially useful for Linux gamers because Steam games mostly start on your main screen. If you want games to run on your TV or a different screen, it's a little bit of a hassle to manually switch which one is your main screen. This solution makes that much easier.

So I decided to write a little explanation on how it could be done using xrandr.

For a small description on xrandr I'll quote the man page
QuoteXrandr is used to set the size, orientation and/or reflection of the outputs for a screen. It can also set the screen size.

I'm doing this on my own desktop that has 2 monitors a 1080p and an old vga 1280x1024 one. These are the ones I'm using in the example. adapt to your own needs

I'm using a terminal to execute these commands so fire up xterm/lxterm/xfce-terminal/gterm/terminator/etc/etc.

1) Identify screens

xrandr -q

This returns 2 monitors for me
Screen 0: minimum 8 x 8, current 1920 x 1080, maximum 16384 x 16384
VGA-0 connected (normal left inverted right x axis y axis)
   1280x1024     60.02 +  75.02  
   1024x768      75.03    75.03    70.07    60.00  
   800x600       75.00    72.19    60.32    56.25  
   640x480       75.00    72.81    59.94  
DVI-D-0 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 368mm x 207mm
   1920x1080     60.00*+
   1680x1050     59.95  
   1600x900      60.00  
   1440x900      59.89  
   1280x1024     75.02    60.02  
   1280x960      60.00  
   1280x800      59.81  
   1280x720      60.00  
   1152x864      60.00  
   1024x768      75.03    70.07    60.00  
   800x600       75.00    72.19    60.32    56.25  
   640x480       75.00    72.81    59.94  


In my examples I shall use DVI-D-0 and VGA-0, replace them with your own names found.


2) Test with xrandr

So now that we know our monitors , let's try turning on the second one and turning off the first one. That way we don't have to work blind.

xrandr --output VGA-0 --auto

and turn off the first monitor

xrandr --output DVI-D-0 --off

And now let's reverse the process

xrandr --output DVI-D-0 --auto
xrandr --output VGA-0 --off



3) Profit!

The previous steps worked perfectly for me. I decided to create a script that would automate this for me.

3.1) example script
#!/bin/bash

#Either use the hardcoded stuff below 
 #replace these values with your own!!!
 #MON1=DVI-D-0
 #MON2=VGA-0
#OR try to get the first and last connected monitor 
MON1=`xrandr -q|grep \ connected|awk '{print $1}'|head -n1`
MON2=`xrandr -q|grep \ connected|awk '{print $1}'|tail -n1`
MON1_CURRENT=`xrandr --listactivemonitors| awk '{ print $4 }'| grep ${MON1}`

# 0 we want the 2nd monitor to turn on
# 1 we want the 1st monitor to turn on
STATE=0

# ASSUMPTION!
# if MON1_CURRENT is empty were probably using the secondary monitor
if [ "x$MON1_CURRENT" == "x" ] 
then 
    STATE=1
fi

if [ $STATE -eq 0 ]
then
#turn of MON1 and turn on MON2
    xrandr --output $MON1 --off
    xrandr --output $MON2 --auto
else
#turn of MON2 and turn on MON1
    xrandr --output $MON2 --off
    xrandr --output $MON1 --auto
fi


To turn this into a script for yourself you can copy/paste the code above into your favourite text-editor and save it with the name of your choice. I chose togglescreen.sh because that's what it does and it's a shell-script.

mark the file as being executable using your filemanager or using the terminal.

chmod +x owgodwheredidIsavethedamnthingthistime

I saved it in my homefolder so for me it's

chmod +x togglescreen.sh

3.2) take a testdrive

Either run it from your filemanager, probably using a doubleclick, or alternatively

/insomepath/where/i/saved/it/owgodwheredidIsavethedamnthingthistime

again for me that translates into
~/togglescreen.sh

Repeat running the scripts to revert to the old state.


As a sidenote you can adapt the commands to do other stuff than just turning monitors on and off. Do take the time to read the manpage for xrandr.

To read the manpage from the terminal
man xrandr

For people using nvidia proprietary drivers this link might help prevent tearing after switching screens.

I hope this helps a few people. Any suggestions or questions, add them below.

Regards,
Ysblokje

PS: thanks goes to Jan for proofreading and some suggestions
ysblokje Aug 3, 2016
Quoting: GuestWouldn’t it make more sense to store the state file in /tmp since a reboot will change the monitors state?
Also you ask for bash but AFAICS it’s pure sh.

You're right it doesn't make sense. So I changed to not use a statefile at all now.
Also it's my example script. You're free to modify it to your own needs.
While you're here, please consider supporting GamingOnLinux on:

Reward Tiers: Patreon. Plain Donations: PayPal.

This ensures all of our main content remains totally free for everyone! Patreon supporters can also remove all adverts and sponsors! Supporting us helps bring good, fresh content. Without your continued support, we simply could not continue!

You can find even more ways to support us on this dedicated page any time. If you already are, thank you!
Login / Register


Or login with...
Sign in with Steam Sign in with Google
Social logins require cookies to stay logged in.