[Solved] switch monitors in script

Everything about X, Gnome, KDE, ... and everything running on it

[Solved] switch monitors in script

Postby peer » 2020-07-04 05:50

I have an pc (Debian buster, kde plasma) with a tv attached to the HDMI port.
I can switch between the monitor and the tv with the shortcut 'Meta-P' (KDE Daemon).
Choosing between 'external screen' or 'laptop screen' works. When I choose 'Unify outputs' I get an error message on my tv: "not supported".

I want a toggle monitor script like:
Code: Select all
#!/bin/bash
intern=LVDS1
extern=VGA1

if xrandr | grep "$extern disconnected"; then
    xrandr --output "$extern" --off --output "$intern" --auto
else
    xrandr --output "$intern" --off --output "$extern" --auto
fi

see: https://wiki.archlinux.org/index.php/Xr ... al_monitor
But xrandr shows me that both VGA-1 and HDMI-1 are connected so this script will not work.
Also I cannot turn the HDMI-1 off with 'xrandr --output "HDMI-1" --off'.
Last edited by peer on 2020-07-05 07:44, edited 1 time in total.
peer
 
Posts: 125
Joined: 2017-03-26 10:14

Re: switch monitors in script

Postby ruwolf » 2020-07-05 02:14

Do you use correct names?
Code: Select all
xrandr -q
should give you names of your outputs.
I am asking, because you use VGA1 without hyphen, but HDMI-1 with hyphen.
User avatar
ruwolf
 
Posts: 427
Joined: 2008-02-18 05:04
Location: Slovakia, Banovce nad Bebravou, Matice slovenskej 1260/4-7

Re: switch monitors in script

Postby peer » 2020-07-05 07:01

The command "xrandr"shows that both "VGA-1"and "HDMI-1"are connected.
The command "xrandr --listmonitors" shows only the active output.
I will try a bit further.

edit:
I now got a working script:
Code: Select all
#!/bin/bash

if xrandr --listmonitors | grep "VGA-1"; then
    xrandr --output "VGA-1" --off --output "HDMI-1" --auto
else
    if xrandr --listmonitors | grep "HDMI-1"; then
        xrandr --output "HDMI-1" --off --output "VGA-1" --auto
    fi
fi
fi

The script checks which monitor is active and then switches to the other monitor.
I also created a shortcut for this script (Meta + Pause).
peer
 
Posts: 125
Joined: 2017-03-26 10:14


Return to Desktop & Multimedia

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable