📜 ⬆️ ⬇️

Automatic detection of the connection / disconnection of the second monitor

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

 #!/bin/sh #export Display for output export DISPLAY=:0.0 #some scripts for work xrandr_command="/usr/bin/xrandr" awk_command="/bin/awk" #get max resolution of connected devises resolution_VGA=`${xrandr_command} | $awk_command '/VGA1 connected/ { getline; print $1}'` resolution_HDMI=`${xrandr_command} | $awk_command '/HDMI1 connected/ { getline; print $1}'` #if resolution exist we get true if [ -n "$resolution_VGA" ]; then #notify-send $resolution_VGA xrandr --output VGA1 --above LVDS1 --mode $resolution_VGA elif [ -n "$resolution_HDMI" ]; then #notify-send $resolution_HDMI xrandr --output HDMI1 --above LVDS1 --mode $resolution_HDMI else xrandr --auto fi 


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.

Source: https://habr.com/ru/post/229167/


All Articles