Automatic detection of connection \ disconnection of the second monitor

    Good day to all.

    In fact, laziness encouraged me to implement this little automation.

    Actually, how it all began.

    I have a laptop with gentoo installed and i3wm window manager. There are also several monitors (at home, at work, etc.). The resolutions on all monitors are different, the connection methods (VGA, HDMI, DVI) are different. Actively, I use the first two.

    Previously, when connecting a second monitor, it was necessary to call the commands that initialized this same monitor. Running a command with an automatic key did not always give the desired result (did not guess the permission).

    xrandr --auto

    Therefore, I had to run the same command, but with a set of other keys, permission for example.


    And although, in xrandr for each monitor I have several resolution options, there is one (maximum for this monitor that satisfies), but on each monitor it is different (since the monitors themselves are different).

    Therefore, I had to look for a solution ...

    First of all, I needed to somehow resolve the issue, identifying the situation of connecting / disconnecting the second monitor.
    As it turned out, there was nothing complicated.

    All we need is to look into the directory /etc/udev/rules.dwhere our monitor rules will actually be stored.

    Next, you need to create a file there. Creating files in this directory are subject to a couple of small rules. Namely, that the file names must have the extension .rules . And the second, not so important in this situation. All files are arranged in alphabetical order, which affects the execution order of the file.

    I named my file like this - 70-persistent-monitor.rules - only by analogy with the files that were already in this folder.
    The contents of the file are as follows.
    KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/bin/"

    To get the KERNEL and SUBSYSTEM values, run the command:

     sudo udevadm monitor

    And plug / unplug the second

    ACTION monitor - indicates what actions to respond to, in this case, both turn on and turn off. And actually RUN is the script that we will run. As you can see from the example, the file is called and is located in / usr / bin /. Although the location may be different. For example / home / user / bin

    Well, actually the file itself

    #export Display for output
    export DISPLAY=:0.0
    #some scripts for work
    #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
    xrandr --auto

    Perhaps the implementation is not very beautiful, but working. We try to get the maximum resolution for the connected monitor from the output of the xrandr command. If the monitor is connected, the line / VGA1 connected will be found. If not, it will be VGA1 disconnected and nothing will be found.

    PS On different systems, device naming (VGA1 HDMI1) may vary. in order to see, just type in the xrandr console.

    Here I think everything is clear.

    Also popular now: