#!/bin/bash

# Display mode switcher using xrandr
# Usage: disp [desktop|ipad|tv]

# when switched down to a lower resolution, xrandr sometimes forgets the
# modeline for the full monitor resolution, so it has to be added back, weirdly
add_desktop_mode() {
    if ! DISPLAY=:0 xrandr | grep -q "5120x2160"; then
        DISPLAY=:0 xrandr --newmode "5120x2160" 1186.50 5120 5168 5200 5280 2160 2163 2168 2222 +hsync -vsync
        DISPLAY=:0 xrandr --addmode DP-0 "5120x2160"
    fi
}

case "$1" in
  "desktop")
    # 5K x 2K ultrawide @ 72Hz
    add_desktop_mode
    DISPLAY=:0 xrandr --output DP-0 --mode 5120x2160 --rate 72
    ;;
  "ipad")
    # iPad Pro 2 at native resolution is 1366x1024 but nvidia driver won't
    # allow setting a modeline not in the edid block and I can't dump that.
    # This is in the neighborhood and has the same ratio, though Steam Link
    # still puts some bars on it and I'm tapped out on debugging.
    DISPLAY=:0 xrandr --output DP-0 --mode 1024x768
    ;;
  "tv")
    # 1080p
#    add_tv_mode
    DISPLAY=:0 xrandr --output DP-0 --mode 1920x1080 --rate 60
    ;;
  *)
    echo "Usage: disp [desktop|ipad|tv]"
    echo "Switches display resolution using xrandr"
    exit 1
    ;;
esac
