diff options
-rwxr-xr-x | autodetect.sh | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/autodetect.sh b/autodetect.sh index 97aacf6..5ce4774 100755 --- a/autodetect.sh +++ b/autodetect.sh @@ -1,35 +1,67 @@ #!/bin/sh set -x -#Set this values on new system! + +## Set this values on new system! ########################################## -NOTE_PATH="/sys/class/drm/card0-eDP-1" -EXT1_PATH="/sys/class/drm/card0-DP-1" -EXT2_PATH="/sys/class/drm/card0-DP-3" NOTE_NAME="eDP1" -EXT1_NAME="DP1-1" -EXT2_NAME="DP1-3" +DPI="96" ########################################## -NOTE_STATUS=$(cat "$NOTE_PATH/status") -EXT1_STATUS=$(cat "$EXT1_PATH/status") -EXT2_STATUS=$(cat "$EXT2_PATH/status") +## Init +COMMAND="$1" +EXT1_NAME="none" +EXT2_NAME="none" +EXT1_STATUS="disconnected" +EXT2_STATUS="disconnected" -XRANDR_OUT=$(xrandr -q) -NOTE_MAX=$(echo "$XRANDR_OUT" | grep -A 1 $NOTE_NAME | tail -n1 | awk '{ print $1 }') -EXT2_MAX=$(echo "$XRANDR_OUT" | grep -A 1 $EXT2_NAME | tail -n1 | awk '{ print $1 }') -EXT1_MAX=$(echo "$XRANDR_OUT" | grep -A 1 $EXT1_NAME | tail -n1 | awk '{ print $1 }') +## Query current settings +XRANDR="$(xrandr -q)" -COMMAND="$1" +## Some functions +detect_status() { + echo "$XRANDR" | sed -n -e "s/$1\s*\([a-z]*\)\s*.*$/\1/p" +} + +detect_connected() { + sed -n -e 's/^\([[A-Za-z0-9-]*\)\s*connected.*$/\1/p' +} + +detect_disconnected() { + sed -n -e 's/^\([[A-Za-z0-9-]*\)\s*disconnected.*$/\1/p' +} + +rest_off() { + OFF_STRING="" + for device in $(echo "$XRANDR" | detect_disconnected); do + OFF_STRING="$OFF_STRING --output $device --off" + done + echo "$OFF_STRING" +} handle_non_exist_mode() { - xrandr --dpi 96 --output $2 --mode $4 --output $1 --mode $3 --left-of $2 + xrandr --dpi "$DPI" $(rest_off) --output $2 --mode $4 --output $1 --mode $3 --left-of $2 } -if [ "$COMMAND" = "external" -a "$EXT2_STATUS" = "connected" ]; then - xrandr --dpi 96 --output "$EXT2_NAME" --mode "$EXT2_MAX" --output "$NOTE_NAME" --off +## Get notebook display status +NOTE_STATUS="$(detect_status "$NOTE_NAME")" + +## Try to detect some external displays and theirs resolution +EXT1_NAME="$(echo "$XRANDR" | grep -v "$NOTE_NAME" | detect_connected | head -n 1)" +if [ -n "$EXT1_NAME" ]; then + EXT1_STATUS="$(detect_status "$EXT1_NAME")" + EXT2_NAME="$(echo "$XRANDR" | grep -v "\($NOTE_NAME\|$EXT1_NAME\)" | detect_connected | head -n 1)" + [ -n "$EXT2_NAME" ] && EXT2_STATUS="$(detect_status "$EXT2_NAME")" +fi + +NOTE_MAX=$(echo "$XRANDR" | grep -A 1 "$NOTE_NAME" | tail -n1 | awk '{ print $1 }') +EXT1_MAX=$(echo "$XRANDR" | grep -A 1 "$EXT1_NAME" | tail -n1 | awk '{ print $1 }') +EXT2_MAX=$(echo "$XRANDR" | grep -A 1 "$EXT2_NAME" | tail -n1 | awk '{ print $1 }') + +if [ "$COMMAND" = "external" -a "$EXT1_STATUS" = "connected" ]; then + xrandr --dpi "$DPI" --output "$EXT1_NAME" --mode "$EXT1_MAX" --output "$NOTE_NAME" --off -elif [ "$COMMAND" = "external" -a "$EXT1_STATUS" = "connected" ]; then - xrandr --dpi 96 --output "$EXT1_NAME" --mode "$EXT1_MAX" --output "$NOTE_NAME" --off +elif [ "$COMMAND" = "external" -a "$EXT2_STATUS" = "connected" ]; then + xrandr --dpi "$DPI" --output "$EXT2_NAME" --mode "$EXT2_MAX" --output "$NOTE_NAME" --off elif [ "$EXT2_STATUS" = "connected" -a "$NOTE_STATUS" = "connected" -a "$EXT1_STATUS" = "disconnected" ]; then handle_non_exist_mode "$EXT2_NAME" "$NOTE_NAME" "$EXT2_MAX" "$NOTE_MAX" @@ -38,7 +70,7 @@ elif [ "$EXT2_STATUS" = "disconnected" -a "$NOTE_STATUS" = "connected" -a "$EXT1 handle_non_exist_mode "$EXT1_NAME" "$NOTE_NAME" "$EXT1_MAX" "$NOTE_MAX" elif [ "$EXT2_STATUS" = "connected" -a "$NOTE_STATUS" = "connected" -a "$EXT1_STATUS" = "connected" ]; then - xrandr --dpi 96 --output "$NOTE_NAME" --mode "$NOTE_MAX" --output "$EXT1_NAME" --mode "$EXT1_MAX" --left-of "$NOTE_NAME" --output "$EXT2_NAME" --mode "$EXT2_MAX" --left-of "$EXT1_NAME" + xrandr --dpi "$DPI" --output "$NOTE_NAME" --mode "$NOTE_MAX" --output "$EXT1_NAME" --mode "$EXT1_MAX" --left-of "$NOTE_NAME" --output "$EXT2_NAME" --mode "$EXT2_MAX" --left-of "$EXT1_NAME" elif [ "$EXT2_STATUS" = "disconnected" -a "$NOTE_STATUS" = "connected" -a "$EXT1_STATUS" = "disconnected" ]; then ~/bin/dnote.sh |