AKZN Notes

Archives for My Lazy and Forgetful Mind

[ubuntu][xrandr] Autodetect Display

cant get to work, use autorandr instead
https://github.com/phillipberndt/autorandr
config file on ~/.config/autorandr/ just in case some error(like what happened to me), just edit some parameter on those files that make errors,

taken from https://unix.stackexchange.com/a/11891 with fix and personal customization

I'm using this simple (homemade) script that keeps polling RandR and switches between LVDS1 and VGA1 when VGA gets connected/disconnected. (For HDMI outputs, in the following script file, change all the VGA1 to HDMI1)

It's a dirty solution, yet it's working just fine.

It's customized for my setup: you'll most likely need to change RandR output names (LVDS-1 and VGA-1) and unlike me you'll probably be fine with your RandR default mode for VGA.

Add Missing Resolution

Check display name

  • type xrandr in console

Check parameter to -newmode

  • type cvt [res diagonal] [res vertical]
    • ie: cvt 1360 768
  • you will see
    # 1360x768 59.80 Hz (CVT) hsync: 47.72 kHz; pclk: 84.75 MHz
    Modeline "1360x768_60.00"   84.75  1360 1432 1568 1776  768 771 781 798 -hsync +vsync
  • add string after Modeline to xrander --newmode
    xrandr --newmode "1360x768-60.00"  84.75  1360 1432 1568 1776  768 771 781 798 $
  • then xrandr --addmode
    xrandr --addmode VGA-1 1360x768-60.00

    Auto detect display connected and auto change xrandr mode

  1. Make file into profile.d

    sudo nano /etc/profile.d/xrandr_external_monitor.sh

    fill with below code, this code is customized, you can change xrandr --output whatever you need

    #!/bin/bash
    
    # setting up new mode for my VGA
    xrandr --newmode "1360x768-60.00"  84.75  1360 1432 1568 1776  768 771 781 798 $
    
    xrandr --addmode VGA-1 1360x768-60.00
    
    # default monitor is LVDS-1
    MONITOR=LVDS-1
    
    # functions to switch from LVDS1 to VGA and vice versa
    function ActivateVGA {
        echo "Switching to VGA-1"
        xrandr --output VGA-1 --mode 1360x768-60.00 --dpi 160 --output LVDS-1 --off
        MONITOR=VGA-1
    }
    function DeactivateVGA {
        echo "Switching to LVDS1"
        xrandr --output VGA-1 --off --output LVDS-1 --auto
        MONITOR=LVDS-1
    }
    
    # functions to check if VGA is connected and in use
    function VGAActive {
        [ $MONITOR = "VGA-1" ]
    }
    function VGAConnected {
        ! xrandr | grep "^VGA-1" | grep disconnected
    }
    
    # actual script
    while true
    do
        if ! VGAActive && VGAConnected
        then
         ActivateVGA
        elif VGAActive && ! VGAConnected
        then
           DeactivateVGA
        else
            xrandr --auto
        fi
    
        sleep 1s
    done
    
  2. Make the .sh file executable by typing the following command in the terminal

    chmod +x /etc/profile.d/xrandr_external_monitor.sh

  3. reboot to see the effect

Leave a Reply

Your email address will not be published.