Skip to content

Instantly share code, notes, and snippets.

@ryochack
Last active December 23, 2018 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryochack/55f47eeafe300a3923622604f7b01135 to your computer and use it in GitHub Desktop.
Save ryochack/55f47eeafe300a3923622604f7b01135 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# This script is to set xrandr as mirroring
set -eu
DEBUG_ON=0
function log_output() {
if [[ $DEBUG_ON -ne 1 || $# -eq 0 ]]; then
return
fi
echo "$@"
}
function log_arr_output() {
if [[ $DEBUG_ON -ne 1 || $# -eq 0 ]]; then
return
fi
local i=0
local arr=($@)
for e in ${arr[@]}; do
echo "[$i] ${e}"
i=$((++i))
done
}
# look for common resulution from resolution array parameter
# [in]: array of display resolution parameters
# eg: ("1920x1080 1680x1050 1280x1024" "1600x1200 1280x1024 1280x960" "1920x1200 1600x1200 1680x1050 1280x1024")
# [out]: common resolution of given resolution parameters
# eg: "1280x1024", if not found common_resolution, return ""
function find_common_resolution() {
if [[ $# -eq 0 ]]; then
echo ""
return
fi
local reso_arr=($@)
local arr_len=${#reso_arr[@]}
local common_resolution=""
if [[ $arr_len -eq 0 ]]; then
:
elif [[ $arr_len -eq 1 ]]; then
common_resolution=$(awk '{printf($1)}' <<< ${reso_arr[0]})
else
local IFS_BKUP=$IFS
IFS=$' '
for reso_primary in ${reso_arr[0]}; do
local _not_found=0
for ((i=1; i<arr_len; i++)); do
if [[ ! "${reso_arr[$i]}" =~ $reso_primary ]]; then
_not_found=1
break
fi
done
if [[ _not_found -eq 0 ]]; then
common_resolution=$reso_primary
break;
fi
done
IFS=$IFS_BKUP
fi
echo $common_resolution
}
xrandr_status=$(xrandr)
declare -a conn_vouts
declare -a disconn_vouts
log_output "${xrandr_status}"
log_output ---
declare -A vout_resolutions
current_vout=""
# parse xrandr output to get output-list and each resolutions
DEFAULT_IFS=$IFS
IFS=$'\n'
for line in ${xrandr_status}; do
if [[ "$line" =~ disconnected ]]; then
current_vout=""
disconn_vouts+=("$(awk '/^\S.*\<disconnected\>/{printf("%s", $1)}' <<< $line)")
continue
elif [[ "$line" =~ connected ]]; then
current_vout=$(awk '/^\S.*\<connected\>/{printf("%s", $1)}' <<< $line)
conn_vouts+=("${current_vout}")
log_output "->" ${current_vout}
continue
fi
if [[ ${current_vout} == "" ]]; then
continue
fi
if [[ "$line" =~ ([0-9]+x[0-9]+) ]]; then
RESO=${BASH_REMATCH[1]}
vout_resolutions[${current_vout}]+="${RESO} "
fi
done
log_output "__connected_vouts__"
log_arr_output ${conn_vouts[@]}
log_output "__disconnected_vouts__"
log_arr_output ${disconn_vouts[@]}
# push array from hashmap to pass find_common_resolution()
declare -a resos_array
for key in "${!vout_resolutions[@]}"; do
resos_array+=("${vout_resolutions[$key]}")
done
log_output "__resolutions__"
log_arr_output ${resos_array[@]}
common_reso=$(find_common_resolution ${resos_array[@]})
if [[ $common_reso == "" ]]; then
echo "not found common resolution"
exit 0
fi
# xrandr --output eDP --mode 1280x1024 --rotate normal
# --output DisplayPort-3 --mode 1280x1024 --rotate normal --same-as eDP
# --output DisplayPort-4 --mode 1280x1024 --rotate normal --same-as eDP
# --output HDMI-A-0 --off
# --output DisplayPort-1 --off
# --output DisplayPort-2 --off
primary_vout="${conn_vouts[0]}"
xrandr_args=" --output ${primary_vout} --mode $common_reso --rotate normal"
for ((i=1; i<${#conn_vouts[@]}; i++)); do
xrandr_args+=" --output ${conn_vouts[$i]} --mode $common_reso --rotate normal --same-as ${primary_vout}"
done
for ((i=0; i<${#disconn_vouts[@]}; i++)); do
xrandr_args+=" --output ${disconn_vouts[$i]} --off"
done
log_output "$xrandr_args"
IFS=$DEFAULT_IFS
xrandr ${xrandr_args}
echo $common_reso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment