#!/usr/bin/env sh ###################################################################### # @author : swytch (adapted from Luke Smith - lukesmith.xyz) # @file : displayselect # @license : GPLv3 # @created : Wednesday May 20, 2020 18:04:01 CEST # # @description : detect displays and select an organisation ###################################################################### twoscreen() { # If multi-monitor is selected and there are two screens. mirror=$(printf "no\\nyes" | dmenu -i -p "Mirror displays?") # Mirror displays using native resolution of external display and a scaled # version for the internal display if [ "$mirror" = "yes" ]; then external=$(echo "$screens" | dmenu -i -p "Optimize resolution for:") internal=$(echo "$screens" | grep -v -w "$external") res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \ tail -n 1 | awk '{print $1}') res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \ tail -n 1 | awk '{print $1}') res_ext_x=$(echo $res_external | sed 's/x.*//') res_ext_y=$(echo $res_external | sed 's/.*x//') res_int_x=$(echo $res_internal | sed 's/x.*//') res_int_y=$(echo $res_internal | sed 's/.*x//') scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l) scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l) xrandr --output "$external" --auto --scale 1.0x1.0 \ --output "$internal" --auto --same-as "$external" \ --scale "$scale_x"x"$scale_y" else primary=$(echo "$screens" | dmenu -i -p "Select primary display:") secondary=$(echo "$screens" | grep -v -w "$primary") direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?") rotation=$(printf "normal\\nleft\\nright" | dmenu -i -p "Rotate $secondary?") setup fi } default() { [ -z "$primary" ] && primary=$(echo "$allposs" | grep -w "connected primary" | awk '{ print $1 }') secondary=$(echo "$screens" | grep -v -w "$primary") direction="left" rotation="normal" mode="mode 2560x1440" setup } external() { internal=$(echo "$screens" | dmenu -i -p "Select internal display:") screens=$(echo "$screens" | grep -v -w "$internal") primary=$(echo "$screens" | dmenu -i -p "Select primary display:") xrandr --output $primary --primary && default } setup() { [ -z "$mode" ] && mode="auto" xrandr --output "$primary" --primary --auto --scale 1.0x1.0 \ --output "$secondary" --"$direction"-of "$primary" --$mode [ -n "$internal" ] && xrandr --output "$internal" --off } morescreen() { # If multi-monitor is selected and there are more than two screens. primary=$(echo "$screens" | dmenu -i -p "Select primary display:") secondary=$(echo "$screens" | grep -v -w "$primary" | dmenu -i -p "Select secondary display:") direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?") tertiary=$(echo "$screens" | grep -v -w "$primary" | grep -v "$secondary" | dmenu -i -p "Select third display:") xrandr --output "$primary" --auto --output "$secondary" --"$direction"-of "$primary" --auto --output "$tertiary" --"$(printf "left\\nright" | grep -v "$direction")"-of "$primary" --auto } multimon() { # Multi-monitor handler. case "$(echo "$screens" | wc -l)" in 1) xrandr $(echo "$allposs" | grep -v -w "$screens" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;; 2) twoscreen ;; *) morescreen ;; esac ;} # Get all possible displays allposs=$(xrandr -q | grep -w "connected") # Get all connected screens. screens=$(echo "$allposs" | awk '{print $1}') # Get user choice including multi-monitor and manual selection: chosen=$(printf "default\\nexternal\\nmulti-monitor\\n%s\\nmanual selection" "$screens" | dmenu -i -p "Select display arangement:") && case "$chosen" in "default") default ;; "external") external ;; "manual selection") arandr ; exit ;; "multi-monitor") multimon ;; *) xrandr --output "$chosen" --auto --scale 1.0x1.0 $(echo "$allposs" | grep -v "$chosen" | awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;; esac setbg # Fix background if screen size/arangement has changed. pgrep -x dunst >/dev/null && kill -9 $(pidof dunst) && setsid dunst & # Restart dunst to ensure proper location on screen