Skip to content

Instantly share code, notes, and snippets.

@hishamhm
Created November 10, 2019 17:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hishamhm/3a077df976ce144ffad8b7222f59ca90 to your computer and use it in GitHub Desktop.
Save hishamhm/3a077df976ce144ffad8b7222f59ca90 to your computer and use it in GitHub Desktop.
xrandr script to configure dual monitors
#!/bin/sh
# --- check which is the second monitor, if any ---
if xrandr | grep -q "HDMI1 connected"
then
monitor=HDMI1
elif xrandr | grep -q "^DP1 connected"
then
monitor=DP1
else
monitor=VGA1
fi
modes_for() {
xrandr | awk "
/$1 connected/ { s=1; next }
/connected/ { nextfile }
/^ .*/ { print }
" | sort -tx -n --reverse
}
if xrandr | grep -q "eDP1 connected"
then
# --- Dell XPS 12 ---
native_laptop_res=1920x1080
laptop=eDP1
xrandr --newmode "1366x768" 85.86 1368 1440 1584 1800 768 769 772 795 -HSync +Vsync &> /dev/null
xrandr --addmode eDP1 "1366x768" &> /dev/null
else
# --- HP pavillion ---
native_laptop_res=1366x768
laptop=LVDS1
fi
# --- default option: two monitors
if [ "$1" = "on" ] || [ "$1" = "" ] && xrandr | grep -q "$monitor connected"
then
if xrandr | grep -q "1920x1080"
then
#lablua_offset=0x220
lablua_offset=0x0
xrandr --output $monitor --mode 1920x1080 --primary
xrandr --output $laptop --left-of $monitor --mode 1366x768 --pos $lablua_offset
xrandr --output $laptop --pos $lablua_offset
#xrandr --output $monitor --gamma 1.44:1.44:1.44 # LG LabLua
#xrandr --output $laptop --gamma 0.97:0.97:0.97 # Dell XPS 12
#xinput set-prop "ATML1000:00 03EB:842F" --type=float "Coordinate Transformation Matrix" 0.41570298234936 0 0 0 1 0 0 0 1
#xinput set-prop "ATML1000:00 03EB:842F" --type=int "Evdev Axis Calibration" 5 4073 -42 5759
elif xrandr | grep -q "1280x1024"
then
xrandr --output $monitor --mode 1280x1024
xrandr --output $laptop --right-of $monitor --mode 1366x768 --pos 1281x0 --primary
fi
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
xbacklight -set 100
# --- external monitor only, in Full HD
elif [ "$1" = "filme" -o "$1" = "movie" ]
then
xrandr --output $laptop --off
xrandr --output $monitor --mode 1920x1080 --primary --preferred
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
# wmctrl -i -r `wmctrl -l | grep xfce4-panel | grep -v grep | cut -d" " -f1 | head -n 1` -e 0,0,0,-1,-1
# wmctrl -i -r `wmctrl -l | grep xfce4-panel | grep -v grep | cut -d" " -f1 | tail -n 1` -e 0,0,-1,-1,-1
xbacklight -set 10
xset s noblank
xset s off
# --- mirror modes for presentations
elif [ "$1" = "mirror" ]
then
xrandr --output $laptop --mode 1024x768
xrandr --output $monitor --right-of $laptop --mode 1024x768 --same-as $laptop
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
killall xscreensaver
xbacklight -set 100
elif [ "$1" = "mirrorhd" ]
then
xrandr --output $laptop --mode 1920x1080
xrandr --output $monitor --right-of $laptop --mode 1920x1080 --same-as $laptop
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
killall xscreensaver
xbacklight -set 100
elif [ "$1" = "mirror1024" ]
then
xrandr --output $laptop --mode 1024x768
xrandr --output $monitor --right-of $laptop --mode 1024x768 --same-as $laptop --rate 60
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
killall xscreensaver
xbacklight -set 100
elif [ "$1" = "mirror800" ]
then
xrandr --output $laptop --mode 800x600
xrandr --output $monitor --right-of $laptop --mode 800x600 --same-as $laptop --rate 60
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
killall xscreensaver
xbacklight -set 100
elif [ "$1" = "mirror640" ]
then
xrandr --output $laptop --mode 640x480
xrandr --output $monitor --right-of $laptop --mode 640x480 --same-as $laptop --rate 60
#alsactl --file /Users/hisham/bin/2monitors_front_alsa.conf restore
killall xscreensaver
xbacklight -set 100
# --- laptop screen only, list modes
elif [ "$1" = "list" ]
then
modes_for $laptop | cat -n
# --- laptop screen only, set mode
elif [ "$1" = "set" -o "$1" = "mode" ]
then
xrandr --output $laptop --mode $(modes_for $laptop | head -n $2 | tail -n 1 | awk '{print $1}' | tr -d '*')
modes_for $laptop | cat -n
xbacklight -set 10
# --- laptop screen only, lower res
elif [ "$1" = "lower" ]
then
res=$(modes_for $laptop | awk "
/\*/ { n=1; next }
/.*/ { if (n==1) { print \$1; nextfile } }
")
if [ "$res" ]
then
xrandr --output $laptop --mode $res
fi
xbacklight -set 10
# --- laptop screen only, higher res
elif [ "$1" = "higher" ]
then
res=$(modes_for $laptop | awk "
/\*/ { print prev; nextfile }
/.*/ { prev=\$1; next }
")
if [ "$res" ]
then
xrandr --output $laptop --mode $res
fi
xbacklight -set 10
# --- laptop screen only
else
xrandr --output $laptop --mode $native_laptop_res
xrandr --output HDMI1 --off
xrandr --output $monitor --off
#alsactl --file /Users/hisham/bin/2monitors_speakers_alsa.conf restore
xbacklight -set 10
fi
#xrandr --auto
kill -HUP `pidof awesome`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment