Change resolution on signal
With the help of Gdk, we can act on several signals, a.o. on connecting/disconnecting monitors. If we connect both the monitor-added
and monitor-removed
-signal (Gdk.Display) to update the monitor resolution automatically, depending on the TV being attached, we're done.
In a script
#!/usr/bin/env python3
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import subprocess
triggermon = "DVI-I-1" # if this one is connected, reduce resolution
tochange = "HDMI-0" # the monitor to lower resolution on
lowres = "1920x1080" # the low resolution
highres = "3840x2160" # the default resolution
class WatchOut:
def __init__(self):
self.gdkdsp = Gdk.Display.get_default()
# use connect/disconnect signales
self.gdkdsp.connect("monitor_added", self.act_onchange)
self.gdkdsp.connect("monitor_removed", self.act_onchange)
# make sure initial configuration is correct
self.act_onchange()
# we need a gtk thread to use the signals
Gtk.main()
def act_onchange(self, *args):
# let's default to highres
curr_res = highres
# get attached n-monitors, lower res if triggermon is connected
n_mons = self.gdkdsp.get_n_monitors()
for n in [n for n in range(self.gdkdsp.get_n_monitors())]:
mon_name = self.gdkdsp.get_monitor(n).get_model()
if mon_name == triggermon:
curr_res = lowres
# set resolution
subprocess.Popen([
"xrandr", "--output", tochange, "--mode", curr_res
])
WatchOut()
Set up
Copy the script into an empty file, save it as actonmonitor.py
and make it executable (if you run it without calling the interpreter explicitely).
The monitor names are in the head of the script, and so the resolutions you mentioned, change them if needed.
Test- run it in a terminal window.
/path/to/actonmonitor.py
See if all works as it should.
Add it to your startup applications if you like.
N.B.
The exact position of the secundary monitor in the monitor layout is not in the (your) output of xrandr. If the position matters, you probably need to add an additional xrandr
command to the script.