mon (2642B)
1 #!/bin/sh 2 3 xrandr_q="$(xrandr -q)" 4 5 find_ext() { 6 ext="$(printf '%s\n' "$xrandr_q" | grep ' connected' | awk 'END{ print $1 }')" 7 } 8 9 xrdb_normal() { 10 xrdb "${HOME}/.Xresources" 11 } 12 13 xrdb_hidpi() { 14 xrdb "${HOME}/.Xresources" 15 xrdb -merge "${HOME}/.Xresources-hidpi" 16 } 17 18 res_from_xrandr() { 19 awk -v disp="$1" '{ if (match($0, disp)) { getline; print $1; exit } }' 20 } 21 22 find_res() { 23 res_int="$(printf '%s' "$xrandr_q" | res_from_xrandr "$INT")" 24 if [ -n "$EXT" ]; then 25 res_ext="$(printf '%s' "$xrandr_q" | res_from_xrandr "$EXT")" 26 fi 27 28 res_int_x="${res_int%x*}" 29 res_int_y="${res_int#*x}" 30 if [ -n "$res_ext" ]; then 31 res_ext_x="${res_ext%x*}" 32 res_ext_y="${res_ext#*x}" 33 res_factor="$( 34 printf '%d/%d\n' "${res_int_x}" "${res_ext_x}" | bc -l)x$( 35 printf '%d/%d\n' "${res_int_y}" "${res_ext_y}" | bc -l)" 36 fi 37 } 38 39 die() { 40 printf '%s: error: %s\n' "${0##*/}" "$1" >&2 41 exit 1 42 } 43 44 int() { 45 xrdb_hidpi 46 xrandr --dpi "${DPI_INT}" --fb "${res_int}" \ 47 --output "${INT}" --mode "${res_int}" --scale 1x1 48 if [ -n "$EXT" ]; then 49 xrandr --output "${EXT}" --off 50 fi 51 } 52 53 ext() { 54 xrdb_hidpi 55 xrandr --dpi "${DPI_INT}" --fb "$res_int" \ 56 --output "${INT}" --off \ 57 --output "${EXT}" --mode "${res_ext}" \ 58 --scale "${res_factor}" \ 59 --panning "${res_int}" 60 } 61 62 ext_native() { 63 xrdb_normal 64 xrandr --fb "${res_ext}" \ 65 --output "${INT}" --off \ 66 --output "${EXT}" --mode "${res_ext}" \ 67 --scale 1x1 \ 68 --panning "${res_ext}" 69 } 70 71 dual() { 72 xrandr_hidpi 73 xrandr --dpi "${DPI_INT}" \ 74 --fb "$(printf '%d*2\n' "$res_int_x" | bc)x${res_int_y}" \ 75 --output "${INT}" --primary --mode "${res_int}" --scale 1x1 \ 76 --output "${EXT}" --mode "${res_ext}" \ 77 --scale "${res_factor}" \ 78 --pos "${res_int_x}x0" \ 79 --panning "${res_int}+${res_int_x}+0" 80 } 81 82 mirror() { 83 xrdb_hidpi 84 xrandr --dpi "${DPI_INT}" \ 85 --fb "${res_int}" \ 86 --output "${INT}" --primary --mode "${res_int}" --scale 1x1 \ 87 --output "${EXT}" --mode "${res_ext}" \ 88 --scale "${res_factor}" \ 89 --pos "0x0" 90 } 91 92 mirror_native() { 93 xrdb_normal 94 tmp="${EXT}" 95 EXT="${INT}" 96 INT="${tmp}" 97 find_res 98 xrandr --dpi "${DPI_INT}" \ 99 --fb "${res_int}" \ 100 --output "${INT}" --primary --mode "${res_int}" --scale 1x1 \ 101 --output "${EXT}" --mode "${res_ext}" \ 102 --scale "${res_factor}" \ 103 --pos "0x0" 104 } 105 106 if [ -z "$INT" ]; then INT=eDP-1; fi 107 if [ -z "$DPI_INT" ]; then DPI_INT=315; fi 108 if [ -z "$EXT" ]; then EXT="$(find_ext)"; fi 109 find_res 110 if [ $# -eq 0 ] || [ -z "$res_ext" ] || [ "$EXT" = "$INT" ]; then 111 int 112 else 113 case "$1" in 114 int) int;; 115 ext) ext;; 116 ext-native) ext_native;; 117 dual) dual;; 118 mirror) mirror;; 119 mirror-native) mirror_native;; 120 *) die "unknown mode $1";; 121 esac 122 fi