Good day to all.
In fact, the implementation of this small automation made me lazy.
Actually, how it all began.
I have a laptop with gentoo installed, and an i3wm window manager. There are also several monitors (at home, at work, and so on). The permissions on all monitors are different, the connection methods too (VGA, HDMI, DVI) are different. Actively, I use the first two.
')
Previously, when you connected a second monitor, you had to call commands that initialized this same monitor. Running a command with an automatic key did not always give the desired result (did not guess the resolution).
xrandr --auto
Therefore, I had to run the same command, but with a set of other keys, permission for example.
--mode
And although, in xrandr for each monitor, I have several options for resolutions, there is one (maximum for a given monitor that satisfies), but on each monitor it is different (since the monitors themselves are different).
So I had to find a solution ...
First of all, I had to somehow resolve the issue, revealing the situation of connecting / disconnecting the second monitor.
As it turned out, there was nothing complicated.
All we need is to look in the
/etc/udev/rules.d
directory, where our rules for the monitor will actually be stored.
Next you need to create a file there. Creating files in this directory obey a couple of small rules. Namely, the fact that file names must have the extension
.rules . And the second is not so important in this situation. All files are arranged in alphabetical order, which affects the order in which the file is executed.
I named my file this way - 70-persistent-monitor.rules - just by analogy with the files that were already in this folder.
The file contents are as follows
KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/bin/change_monitor.sh"
To get the KERNEL and SUBSYSTEM values, run the command:
sudo udevadm monitor
And connect / disconnect the second monitor

ACTION - indicates what actions to respond, in this case, both on and off. And actually RUN is the script that we will run. The example shows that the file is called change_monitor.sh and is located in / usr / bin /. Although the location may be different. For example / home / user / bin
Well, actually the file itself
Perhaps the implementation is not very beautiful, but working. We are trying to get the maximum resolution for the connected monitor from the xrandr output command. If the monitor is connected, the string / VGA1 connected will be found. If not, it will be VGA1 disconnected and nothing will be found.
PS on different systems naming devices (VGA1 HDMI1) - may differ. in order to see, it is enough to type xrandr in the console

Here I think everything is clear.