Enkelt gränssnitt mot xrandr...

Här diskuterar vi skal, kommandon och klassiska linuxverktyg.
Användarvisningsbild
Osprey
Inlägg: 6310
Blev medlem: 06 apr 2008, 00:09
OS: Ubuntu
Ort: Göteborg/Mölndal/Falkenberg
Kontakt:

Enkelt gränssnitt mot xrandr...

Inlägg av Osprey »

Många gånger då jag bootat upp från en hemmagjord pinne, så har jag hamnat med en märklig skärmupplösning och drömmen hade ju då varit att bara kunna skriva "kommdo det-jag-vill-ha". I Linux finns ju då "xrandr", men det är delvis lite bökigt.

Så därför har jag hackat ihop ett script som gör det enkelt... :D

Det är bara att skriva "xr den-upplösning-du-vill-ha" och ja, jag har kallat scriptet för "xr", men det väljer man själv.

Scriptet är förberett för wayland, men den biten finns inte ännu.

Kod: Markera allt

#! /bin/bash
#
PROTOCOL=$(echo "$XDG_SESSION_TYPE")
if [[ $PROTOCOL != "x11" ]]; then
	echo "-As the protocol is $PROTOCOL, we cannot continue (only x11 is supported)"
	echo
	exit
fi
#
#====================================================================================
function usage {
	less << EOD

usage:  $CMDNAME [function] [WIDTHxHEIGTH]

	If "WIDTHxHEIGHT" is specified, this automatically implies a "set" function.

	FUNCTIONS:
	----------
	
	-c | --current
		Show current resolution, this is also what is shown if "$CMDNAME" is run
		without any specified function.

	-d | --define
		Define a new resolution for this monitor. The specified resolution must
		be defined by the VESA standard. If the specified resolution is already
		defined, a message is written and no further definition is made.

	--dryrun
		Performs all the actions specified except that no changes are made.

	-h | --help
		Show this.

	-i | --inter | --interactive
		All defined resolutions for this monitor is shown and it is possible
		to select one of them, by entering its number. It is also possible to
		enter "0" and then specify which resolution that is wanted.

		Running this and selecting the number for 1920x1080 (eg. "1" ) has the
		same result as running "$CMDNAME -l" to see that it is "1" and then run
		"$CMDNAME 1" (or "$CMDNAME 1920x1080" first.

		Running interactive and specifying "0" and then enter "1920x1080" has
		the same result as running "xd --define 1920x1080".

	-l | --list
		List all modes are currently defined for this monitor. It only shows all
		resolutions that are defined for the monitor, it does not show all possible
		resolutions.

	--rm | --remove
		Remove a defined resolution. Generally there is no need for this function
		as defined resolutions is removed by a reboot. It is not possible to remove
		any resolution defined by the system.

		This function sometimes misses, due to a bug in xrandr, but however a reboot
		works.

	-r | --reset
		Reset monitor to the previous resolution. Most often this is the original
		resolution.

		The "previus" resolution is only saved the first time a change of resolution
		is made. Therefore a reset after a number of changes only resets to the first
		resolution.

		The "reset-file" is deleted after each reset, but can however remain after a
		certain reboot.

	-v | --vesa
		List possible resolutions that are defined by VESA. This does not imply
		that the resolutions are supported by the actual monitor.

		The purpose of this function is not to be a complete list of all possible
		resolutions, but to give tips on the most common ones if none are available
		via "-l" for the monitor in question.
		
	-m MON | --mon=MON | --monitor=MON
		Specifies which monitor to operate on. If not specified it is assumed
		that only one monitor exists.

	Both "-c" and "-l" performs an exit when the info is shown. If both are specified
	only the first function will thus be performed. This is also the same when a resolution
	("set") is specified. A resolution setting is always superseeded if "-c" or "-l" is 
	specified.

	FILES:
		The command uses a file called ".xr" in the users home-directory, so do not use
		this name for anything else.

	EXAMPLES:

		$CMDNAME 5
			Setup this monitor to run with the resolution defined by line #5 as
			shown by "$CMDNAME -l".

		$CMDNAME 1920x1080
			Setup this monitor to run with the resolution "1920x1080". It only works if
			1920x1080 is already defined. Check with "--list" and use "--define" if it
			is not.

		$CMDNAME -c | --current
			Show current resolution - this is also default if no other action is specfied.

		$CMDNAME -d 1920x1080 | --define=1920x1080
			Define the resolution "1920x1080" for this monitor and change to it. If 1920x1080
			is already defined, a new definition will not be made.

		$CMDNAME -l | --list
			List all modes are currently defined for this monitor.

		$CMDNAME -m MON | --mon=MON | --monitor=MON
			Specify that the monitor to be used are for example "HDMI-1" or "VGA". Of course
			a function to be performed must also be specified, otherwise only the current
			resolution will be shown.

			If there only exist one monitor, it is not needed to specify this.

		$CMDNAME -v | --vesa
			Show the standard resolutions defined by VESA, as I have found so far. This script 
			will not try define anything else. Edit this script and add extra resolutions if
			you want and know what you are doing.

EOD
}
#
function checkIfResolIsDefined {
	RSL=$1
	ResolIsDefined=false
	if [[ $PROTOCOL == "x11" ]]; then
		LINES=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | awk '{ print $1 }')
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		RESOL="<wayland>"
		return 1
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		RESOL="<other>"
		return 1
	fi
	origIFS=$IFS
	IFS=$'\n'
	for LINE in $LINES; do
		if [[ $RSL == $LINE ]]; then
			ResolIsDefined=true
			RESOL=$RSL
			break
		fi
	done
	IFS=$origIFS
	if [[ $ResolIsDefined == false ]]; then
		return 1
	fi
	return 0
}
function currentModes {
	SPECIFIED_MODE=$1
	if [[ $PROTOCOL == "x11" ]]; then
		MODES=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | awk '{ print $1 }')
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		MODES="<wayland>"
		return 1
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		MODES="<other>"
		return 1
	fi
	for MODE in $MODES; do
		if [[ $SPECIFIED_MODE == $MODE ]]; then
			return 0
		fi
	done
	return 1
}
function doReset {
	getMonitor
	if [[ -r ~/.xr ]]; then
		RESOL=$(cat ~/.xr)
		echo "-Resetting resolution to $RESOL"
		echo "=> xrandr $OPT --output $MONITOR --mode $RESOL"
		xrandr $OPT --output $MONITOR --mode $RESOL
		rm ~/.xr
	else
		echo "-No previous resolution found..."
	fi
	echo
}
function findMonitor {
	NUM=$1
	if [[ $PROTOCOL == "x11" ]]; then
		xrandr --listmonitors | tail -n +2 | sed 's/^[[:space:]]*//' | grep "^$NUM:" | awk '{ print $4 }'
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "<wayland>"
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "<unknown>"
	fi
}
function getMonitor {
#
	if [[ -z $MONITOR ]]; then
		if [[ $PROTOCOL == "x11" ]]; then
			NMON=$(xrandr --listmonitors | grep "Monitors:" | awk '{ print $2 }')
		elif [[ $PROTOCOL == "wayland" ]]; then
			# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
			# It is just here for future development...
			NMON="0"
		else
			# We will never reach here as "$PROTOCOL==other" has already performed an exit...
			# It is just here for future development...
			NMON="0"
		fi
		if [[ $NMON -gt 1 ]]; then
			echo "-More than one monitor exists, you have specify which one to use with name or number"
			echo "-Existing monitors:"
			echo
			showMonitors
			echo
			read -p "_Monitor: " IN
			if [[ $PROTOCOL == "x11" ]]; then
				MONITORS=$(xrandr --listmonitors | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | awk '{ print $4 }')
			elif [[ $PROTOCOL == "wayland" ]]; then
				# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
				# It is just here for future development...
				MONITORS="<wayland>"
			else
				# We will never reach here as "$PROTOCOL==other" has already performed an exit...
				# It is just here for future development...
				MONITORS="<other>"
			fi
			isnum $IN
			if [[ $? == 0 ]]; then
				MONITOR=$(findMonitor $IN)
			else
				MONITOR=$(toupper $IN)
			fi
			if [[ ! -z $MONITOR ]]; then
				monitorExist $MONITOR
				if [[ $? != 0 ]]; then
					echo "-Specified monitor $MONITOR not found..."
					echo
					exit
				fi
				echo "-Monitor specified as $MONITOR"
				echo
			else
				echo "-Specified monitor not found..."
				echo
				exit
			fi
		else
			if [[ $PROTOCOL == "x11" ]]; then
				MONITOR=$(xrandr | grep -e " connected [^(]" | cut -d\  -f1 | xargs)
				echo "-Monitor implicitly set to $MONITOR (only 1 exists)..."
			elif [[ $PROTOCOL == "wayland" ]]; then
				# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
				# It is just here for future development...
				MONITOR="<wayland>"
			else
				# We will never reach here as "$PROTOCOL==other" has already performed an exit...
				# It is just here for future development...
				MONITOR="<other>"
			fi
		fi
	else
		monitorExist $MONITOR
		if [[ $? == 0 ]]; then
			echo "-Monitor specified as $MONITOR"
		else
			echo "-Monitor $MONITOR does not exist..."
			echo
			exit
		fi
	fi
}
function isnum {
	if [[ $1 =~ ^[0-9]+$ ]]; then
		return 0
	fi
	return 1
}
function listDefined {
	#xrandr | grep "^   "[0-9] | grep -v none
	echo '-Preferred resolutions is denoted by "+" (plus)'
	echo '-Current resolution is denoted by a "*" (asterisk)'
	echo '-A resolution that ends with an "i" means "interlaced" and a "p" means "progressive"'
	echo "-(Interlaced is mostly used for CRT monitors)"
	echo
	if [[ $PROTOCOL == "x11" ]]; then
		xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | cat -n
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "<wayland>"
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "<unknown>"
	fi
	echo
}
function monitorExist {
	MN=$1
	FOUNDMON=false
	if [[ $PROTOCOL == "x11" ]]; then
		MONITORS=$(xrandr --listmonitors | tail -n +2 | awk '{ print $4 }')
		for MONIT in $MONITORS; do
			if [[ $MONIT == $1 ]]; then
				FOUNDMON=true
				return 0
			fi
		done
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "<wayland>"
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "<unknown>"
	fi
	return 1
}
function number2resol {
	IN=$1
	NumToResol=false
	if [[ $PROTOCOL == "x11" ]]; then
		#LINES=$(xrandr | grep "^   "[0-9] | cat -n | sed -E 's/^[[:space:]]+//')
		LINES=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | cat -n)
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		RESOL="<wayland>"
		return 1
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		RESOL="<other>"
		return 1
	fi
	origIFS=$IFS
	IFS=$'\n'
	for LINE in $LINES; do
		NUM=$(echo $LINE | awk '{ print $1 }')
		if [[ $NUM == $IN ]]; then
			NumToResol=true
			break
		fi
	done
	IFS=$origIFS
	if [[ $NumToResol == true ]]; then
		RESOL=$(echo $LINE | awk '{ print $2 }')
	else
		RESOL=""
		return 1
	fi
	return 0
}
function removeResol {
	RESOL=$1
	NMON=$(xrandr --listmonitors | grep "Monitors:" | awk '{ print $2 }')
	if [[ $NMON -gt 1 ]]; then
		echo "-More than one monitor exists, you have specify which one to use with name or number"
		echo "-Existing monitors:"
		echo
		showMonitors
		echo
		read -p "_Monitor: " IN
		isnum $IN
		if [[ $? == 0 ]]; then
			MONITOR=$(findMonitor $IN)
		else
			MONITOR=$(toupper $IN)
		fi
		if [[ ! -z $MONITOR ]]; then
			monitorExist $MONITOR
			if [[ $? != 0 ]]; then
				echo "-Specified monitor $MONITOR not found..."
				echo
				exit
			fi
			echo "-Monitor specified as $MONITOR"
			echo
		else
			echo "-Specified monitor not found..."
			echo
			exit
		fi
	else
		MONITOR=$(xrandr | grep -e " connected [^(]" | cut -d\  -f1 | xargs)
	fi
	checkIfResolIsDefined $RESOL
	if [[ $? == 0 ]]; then
		echo "=> xrandr $OPT --delmode $MONITOR $RESOL 2> /dev/null"
		xrandr $OPT --delmode $MONITOR $RESOL 2> /dev/null
 		echo "=> xrandr $OPT --rmmode $RESOL 2> /dev/null"
 		xrandr $OPT --rmmode $RESOL 2> /dev/null
		if [[ $? != 0 ]]; then
			echo "-Remove failed, if it is a user defined resolution it  will be removed at next reboot..."
		fi
		echo
		exit
	else
		echo "-Resolution $RESOL is not defined..."
		echo
		exit
	fi
}
function setupDefine {
	ASK=$1
	MODE=$2
	if [[ $ASK == "yes" ]]; then
		read -p '_Enter "WIDTHxHEIGHT": ' MODE
		if [[ -z $MODE || $MODE == "q" || $MODE == "Q" ]]; then
			echo
			exit
		fi
	fi
	WxH=$(echo $MODE | awk -F \_ '{ print $1 }')
	checkIfResolIsDefined $MODE
	if [[ $? == 0 ]]; then
		DEFINE=false
		RESOL=$WxH
		echo "-Resolution $MODE is already defined for this monitor..."
		return 0
	fi
	vesaStd $WxH
	if [[ $? != 0 ]]; then
		echo "-Unsupported resolution (no such VESA standard) - $WxH..."
		echo
		exit
	fi
	checkIfResolIsDefined $MODE
	DEFINE=true
	FREQ=$(echo $MODE | awk -F _ '{ print $2 }')
	WIDTH=$(echo $WxH | awk -F x '{ print $1 }')
	HEIGHT=$(echo $WxH | awk -F x '{ print $2 }')
	#
	MODE=$(cvt $WIDTH $HEIGHT | grep -i modeline | awk '{ $1=""; print $0 }' | sed -E 's/^[[:space:]]+//')
	NAME=$(echo $MODE | awk '{ print $1 }' | sed 's/\"//g' | awk -F _ '{ print $1 }')
	LINE=$(echo $MODE | awk '{ $1=""; print $0 }' | sed -E 's/^[[:space:]]+//')
	MODELINE="$NAME $LINE"
	MODSET=true
	RESOL=$WxH
	currentModes $WxH
}
function showCurrent {
	if [[ $PROTOCOL == "x11" ]]; then
		CURRENT=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | grep \* | awk '{ print $1 }')
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		CURRENT="<wayland>"
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		CURRENT="<other>"
	fi
	echo "-Current resolution is:  $CURRENT"
	echo
}
function showFunctions {
	if [[ $CURRENT == true ]]; then
		echo "   current  - yes"
	else
		echo "   current  - no"
	fi
	if [[ $HELP == true ]]; then
		echo "   help     - yes"
	else
		echo "   help     - no"
	fi
	if [[ $INTER == true ]]; then
		echo "   inter    - yes"
	else
		echo "   inter    - no"
	fi
	if [[ $LIST == true ]]; then
		echo "   list     - yes"
	else
		echo "   list     - no"
	fi
	if [[ $RESET == true ]]; then
		echo "   reset    - yes"
	else
		echo "   reset    - no"
	fi
	if [[ $RM == true ]]; then
		echo "   rm       - yes"
	else
		echo "   rm       - no"
	fi
	if [[ $VESA == true ]]; then
		echo "   vesa     - yes"
	else
		echo "   vesa     - no"
	fi
	if [[ $SPEC == true ]]; then
		echo "   spec     - yes  (specified resolution)"
	else
		echo "   spec     - no   (specified resolution)"
	fi
}
function showMonitors {
	if [[ $PROTOCOL == "x11" ]]; then
		xrandr --listmonitors | grep -v Monitors:
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "<wayland>"
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "<other>"
	fi
}
function toupper {
	echo $1 | awk '{ print toupper($0) }'
}
function vesaList {
#
	cat << EOV
 This is not a complete list of all possible resolutions. It is only tips about common resolutions
 that possibly can be defined if there are to few pre defined as shown by "-l". 

 To define resolutions other than these, the script must be edited. The idea behind this, is to
 prevent for unreasonable resolutions.

EOV
#      4:3
	echo "   4:3"
	echo "     3840x2880"
	echo "     3200x2400"
	echo "     2560x1920"
	echo "     2048x1536"
	echo "     1920x1440"
	echo "     1600x1200"
	echo "     1400x1050"
	echo "     1280x960"
	echo "     1024x768"
	echo "     800x600"
	echo "     640x480"
	echo
#      5:4
	echo "   5:4"
	echo "     1280x1024"
	echo
#      15:9
	echo "   15:9"
	echo "     1280x768"
	echo
#      16:9
	echo "   16:9"
	echo "     5120x2880"
	echo "     4264x2400"
	echo "     3408x1920"
	echo "     2728x1536"
	echo "     2560x1440"
	echo "     2128x1200"
	echo "     1920x1080"
	echo "     1864x1050"
	echo "     1704x960"
	echo "     1360x768"
	echo "     1280x720"
	echo "     1064x600"
	echo "     848x480"
	echo
#      16:10
	echo "   16:10"
	echo "     4608x2880"
	echo "     3840x2400"
	echo "     3072x1920"
	echo "     2456x1536"
	echo "     2304x1440"
	echo "     1920x1200"
	echo "     1728x1080"
	echo "     1680x1050"
	echo "     1536x960"
	echo "     1224x768"
	echo "     1152x720"
	echo "     960x600"
	echo "     768x480"
	echo
}
function vesaStd {
	wxh=$1
#
#      4:3
	if [[ $wxh == 3840x2880 ]]; then
		return 0
	elif [[ $wxh == 3200x2400 ]]; then
		return 0
	elif [[ $wxh == 2560x1920 ]]; then
		return 0
	elif [[ $wxh == 2048x1536 ]]; then
		return 0
	elif [[ $wxh == 1920x1440 ]]; then
		return 0
	elif [[ $wxh == 1600x1200 ]]; then
		return 0
	elif [[ $wxh == 1400x1050 ]]; then
		return 0
	elif [[ $wxh == 1280x960 ]]; then
		return 0
	elif [[ $wxh == 1024x768 ]]; then
		return 0
	elif [[ $wxh == 800x600 ]]; then
		return 0
	elif [[ $wxh == 640x480 ]]; then
		return 0
#      5:4
	elif [[ $wxh == 1280x1024 ]]; then
		return 0
#      15:9
	elif [[ $wxh == 1280x768 ]]; then
		return 0
#      16:9
	elif [[ $wxh == 5120x2880 ]]; then
		return 0
	elif [[ $wxh == 4264x2400 ]]; then
		return 0
	elif [[ $wxh == 3408x1920 ]]; then
		return 0
	elif [[ $wxh == 2728x1536 ]]; then
		return 0
	elif [[ $wxh == 2560x1440 ]]; then
		return 0
	elif [[ $wxh == 2128x1200 ]]; then
		return 0
	elif [[ $wxh == 1920x1080 ]]; then
		return 0
	elif [[ $wxh == 1864x1050 ]]; then
		return 0
	elif [[ $wxh == 1704x960 ]]; then
		return 0
	elif [[ $wxh == 1360x768 ]]; then
		return 0
	elif [[ $wxh == 1280x720 ]]; then
		return 0
	elif [[ $wxh == 1064x600 ]]; then
		return 0
	elif [[ $wxh == 848x480 ]]; then
		return 0
#      16:10
	elif [[ $wxh == 4608x2880 ]]; then
		return 0
	elif [[ $wxh == 3840x2400 ]]; then
		return 0
	elif [[ $wxh == 3072x1920 ]]; then
		return 0
	elif [[ $wxh == 2456x1536 ]]; then
		return 0
	elif [[ $wxh == 2304x1440 ]]; then
		return 0
	elif [[ $wxh == 1920x1200 ]]; then
		return 0
	elif [[ $wxh == 1728x1080 ]]; then
		return 0
	elif [[ $wxh == 1680x1050 ]]; then
		return 0
	elif [[ $wxh == 1536x960 ]]; then
		return 0
	elif [[ $wxh == 1224x768 ]]; then
		return 0
	elif [[ $wxh == 1152x720 ]]; then
		return 0
	elif [[ $wxh == 960x600 ]]; then
		return 0
	elif [[ $wxh == 768x480 ]]; then
		return 0
	else
		return 1
	fi
}
#
#=================================================================================================
#
echo
TEMP=`getopt -ocdhilm:rv --long current,define,dryrun,help,inter,interactive,list,mon:,monitor:,rm,remove,vesa,reset -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then
	usage
	exit
fi
eval set -- "$TEMP"
#
MODELINE="<not created as we are not defining a new mode>"
RESNAME="<not created as we are not defining a new mode>"
#
OPT=""
CURR=false
CURRENT=false
DEFINE=false
DRYRUN=false
HELP=false
INTER=false
LIST=false
NONE=true
RESET=false
RM=false
SPEC=false
VESA=false
MODSET=false
let COUNT=0
CMDNAME=$(basename $0)
while true; do
	case $1 in
		-c|--current)
			NONE=false
			let COUNT+=1
			CURRENT=true
			shift
			;;
		-d|--define)
			DEFINE=true
			shift
			;;
		--dryrun)
			DRYRUN=true
			OPT="--dryrun "
			shift
			;;
		-h|--help)
			NONE=false
			let COUNT+=1
			HELP=true
			shift
			;;
		-i|--inter|--interactive)
			NONE=false
			let COUNT+=1
			INTER=true
			shift
			;;
		-l|--list)
			NONE=false
			let COUNT+=1
			LIST=true
			shift
			;;
		-m|--mon|--monitor)
			shift
			MONITOR=$(toupper $1)
			shift
			;;
		--rm|--remove)
			NONE=false
			RM=true
			shift
			;;
		-v|--vesa)
			NONE=false
			let COUNT+=1
			VESA=true
			shift
			;;
		-r|--reset)
			NONE=false
			let COUNT+=1
			RESET=true
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo "-Got a star... [$1]"
			exit
			;;
	esac
done
P1=$1
P2=$2
P3=$3
P4=$4
if [[ ! -z $P1 ]]; then
	NONE=false
	SPEC=true
	#RESOL=$P1
	let COUNT+=1
fi
if [[ $COUNT -gt 1 ]]; then
	echo "-Only possible to specify one function..."
	echo "-Specified functions:"
	showFunctions
	echo
	exit
fi
#
#=================================================================================================
#
ResolIsDefined=unknown
NumToResol=unknown
#
if [[ $CURRENT == true || $NONE == true ]]; then
	showCurrent
	exit
elif [[ $HELP == true ]]; then
	usage
	exit
elif [[ $LIST == true ]]; then
	listDefined
	exit
elif [[ $RESET == true ]]; then
	doReset
	exit
elif [[ $RM == true ]]; then
	removeResol $P1
	exit
elif [[ $VESA == true ]]; then
	vesaList
	exit
else	# -If $INTER or specified resolution - find which monitor
	getMonitor
fi
#
#=================================================================================================
#
if [[ $INTER == true ]]; then
	#
	# -Interactive was specified, so we have to show defined resolutions and ask about which to use.
	#
	if [[ $PROTOCOL == "x11" ]]; then
		listDefined
		echo '_Enter a number that exists to change to that resolution, "0" to define a new resolution or "q" to quit.'
		echo "-It is not possible to enter resolution directly here!!"
		read -p '_Number: ' IN
		if [[ -z $IN || $IN == "q" || $IN == "Q" ]]; then
			echo
			exit
		elif [[ $IN != 0 ]]; then
			number2resol $IN
			if [[ $? != 0 ]]; then
				echo "-No such resolution number - $IN..."
				echo 
				exit
			fi
		elif [[ $IN == 0 ]]; then
			DEFINE=true
			setupDefine "yes"
		fi
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is Wayland - not possible to fix yet-1..."
		echo
		exit
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is unknown - not possible to fix..."
		echo
		exit
	fi
elif [[ $SPEC == true ]]; then
	#
	# -A resolution was specified in the command line, so we will check it out and possibly use it.
	#
	X=$(echo $P1 | grep "x")
	if [[ ! -z $X ]]; then
		checkIfResolIsDefined $P1
		if [[ $DEFINE == true ]]; then
			if [[ $ResolIsDefined == false ]]; then
				setupDefine "no" $P1
			else
				echo "-Resolution $P1 is already defined for this monitor..."
			fi
		else
			if [[ $ResolIsDefined == false ]]; then
				echo "-Undefined resolution for this monitor - $P1..."
				echo
				exit
			fi
			RESOL=$P1
		fi
	elif [[ ! -z $P1 ]]; then
		number2resol $P1
		if [[ $? != 0 ]]; then
			echo "-No such resolution number - $P1 (check with \"$(basename $0) -l\"..."
			echo 
			exit
		elif [[ $DEFINE == true ]]; then
			echo "-Resolution $RESOL is already defined for this monitor..."
		fi
	else
		# We should never reach here, but add a message in case of...
		echo "-Unknown resolution error..."
	fi
fi
CURRENT=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | grep \* | awk '{ print $1 }')
if [[ $CURRENT == $RESOL ]]; then
	echo "-Specified resolution is the same as current - no reason to change..."
	echo
	exit
fi
#
if [[ $MODSET == true ]]; then
	RESNAME=$(echo $MODELINE | awk '{ print $1 }' | sed 's/"//g')
fi
TRACE=false
if [[ $TRACE == true ]]; then
	echo
	echo "IN:              $IN"
	echo "SPEC:            $SPEC"
	echo "FOUNDNUM:        $FOUNDNUM"
	echo "ResolIsDefined:  $ResolIsDefined"
	echo "NumToResol:      $NumToResol"
	echo "DEFINE:          $DEFINE"
	echo "MONITOR:         $MONITOR"
	echo "MODELINE:        $MODELINE"
	echo "RESNAME:         $RESNAME"
	echo "RESOL:           $RESOL"
	echo
fi
#
CURRENT=$(xrandr | sed -e 's/^[[:space:]]*//' | grep "^[0-9]" | grep \* | awk '{ print $1 }')
if [[ ($SPEC == true && $ResolIsDefined == true) || ($NumToResol == true) ]]; then
	if [[ $PROTOCOL == "x11" ]]; then
		echo "-Setting resolution to $RESOL"
		echo "=> xrandr $OPT --output $MONITOR --mode $RESOL"
		xrandr $OPT --output $MONITOR --mode $RESOL
		if [[ ! -e ~/.xr ]]; then
			echo $CURRENT > ~/.xr
		fi
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is Wayland - not possible to fix yet-2..."
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is unknown - not possible to fix..."
	fi
elif [[ $DEFINE == true ]]; then
	if [[ $PROTOCOL == "x11" ]]; then
		if [[ $? != 0 ]]; then
			echo "-Mode $WxH already exist, will not redefine it..."
		else
			echo "-Defining mode $RESOL"
			echo "=> xrandr $OPT --newmode $MODELINE"
			xrandr $OPT --newmode $MODELINE
			#
			echo "=> xrandr $OPT --addmode $MONITOR $RESNAME"
			xrandr $OPT --addmode $MONITOR $RESNAME
		fi
		echo "-Setting resolution to $RESOL"
		echo "=> xrandr $OPT --output $MONITOR --mode $RESOL"
		xrandr $OPT --output $MONITOR --mode $RESOL
		if [[ ! -e ~/.xr ]]; then
			echo $CURRENT > ~/.xr
		fi
	elif [[ $PROTOCOL == "wayland" ]]; then
		# We will never reach here as "$PROTOCOL==wayland" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is Wayland - not possible to fix yet..."
	else
		# We will never reach here as "$PROTOCOL==other" has already performed an exit...
		# It is just here for future development...
		echo "-Session protocol is unknown - not possible to fix..."
	fi
else
	echo "-Resolution $IN not found..."
fi
echo
Agera genom att ta och behålla initiativet, ta de risker detta kräver...
http://www.enargo.com/it/
OpenVMS Shark - i Linux finns inte SYS$CMKRNL...
Användarvisningsbild
johanre
Serveradmin
Inlägg: 3888
Blev medlem: 22 okt 2006, 09:13
OS: Ubuntu
Utgåva: 22.04 Jammy Jellyfish LTS
Ort: Malmö

Re: Enkelt gränssnitt mot xrandr...

Inlägg av johanre »

Snyggt!
Skriv svar

Återgå till "Terminalforum"