When an external display connect, we need to trigger a shell script
We can do this by using udev
First, if you don't have udev on your Ubuntu
Run the following command:
sudo apt install udev
Monitor (output) UDEV events with udevadm
This step will be most important for each user. Run udevadm monitor --environment --udev
. Then connect your HDMI cable.
udev rule
Based on the output from the above command, the user created this udev rule at /etc/udev/rules.d/95-monitor-hotplug.rules
.
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/YOUR_USER_NAME_GOES_HERE/.Xauthority", RUN+="/usr/local/bin/hotplug_monitor.sh"
Where YOUR_USER_NAME_GOES_HERE
is your username.
You may want to adjust the xrandr
options to suit your needs.
First run xrandr
to detect your current screens:
$ xrandr
Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
LVDS1 connected (normal left inverted right x axis y axis)
1600x900 60.0*+ 40.0
1024x768 60.0
800x600 60.3 56.2
640x480 59.9
HDMI3 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 521mm x 293mm
1920x1080 60.0*+
1600x1200 60.0
1680x1050 59.9
1680x945 60.0
[...]
To Set the displays so that the external monitor will mirror the current display:
$ xrandr --output HDMI3 --same-as LVDS1
Check your screen display names (LVDS1 etc..).
xrandr script to put at /usr/local/bin/hotplug_monitor.sh
:
#! /usr/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/YOUR_USER_NAME_GOES_HERE/.Xauthority
function connect(){
xrandr --output HDMI3 --same-as LVDS1
}
function disconnect(){
xrandr --output HDMI3 --off
}
xrandr | grep "HDMI3 connected" &> /dev/null && connect || disconnect
monitors.xml
file found in~/.config
. This file is managed through the Gnome Control Center. I've run into similar situations when using multiple displays, and I noticed that themonitors.xml
file can get rather messy. You may even want to rename that file (e.g.,orig_monitors.xml
) and have Gnome Control Center recreate it. In my case, the "clean"monitors.xml
file resolved my display configuration issues. – richbl Sep 25 '22 at 17:48