#!/bin/dash
# Fatdog Set XFT DPI
# License: GPL v3
# jamesbond 2023 - complete re-write

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh
# performance tweak - use "C" if there is no localisation
! [ -e $TEXTDOMAINDIR/${LANG%.*}/LC_MESSAGES/$TEXTDOMAIN.mo ] &&
! [ -e $TEXTDOMAINDIR/${LANG%_*}/LC_MESSAGES/$TEXTDOMAIN.mo ] && LANG=C

### confguration variables
APPTITLE="$(gettext 'Fatdog64 Set Global Font Size (Xft.dpi)')"
DPI_LIST="60 72 84 96 108 120 132 144 156 168 180"

### helpers
# compute "monitor" dpi
get_monitor_dpi() {
	xrandr --listactivemonitors | awk '
	{
		if ($3) {
			if (split($3,xy,"x")) {
				if (split(xy[1],resx,"/")) {
					sub("+","",$2)
					sub("*","",$2)
					printf "%s %s --- %d\n", $1, $2, resx[1]/resx[2]*25.4 + 1
				}
			}
		}
	}'
}

# $1-currently active DPI
make_radio_list() {
	local p found= list=
	for p in $DPI_LIST; do
		if [ "$p" = "$1" ]; then
			found=yes
			list="$list $p $p on"
		else
			list="$list $p $p off"
		fi
	done
	# if not found, prepend
	[ "$1" ] && [ -z "$found" ] && list="$1 $1 on $list "
	echo "$list custom $(gettext 'Custom') off"
}

# $1-current dpi
select_dpi() {
	Xdialog --title "$APPTITLE" --left --no-tags --stdout --radiolist "
$(gettext 'Please choose dpi (dots per inch) for the screen.')

$(gettext 'The larger the value, the bigger the antialiased fonts will render on the screen.
Adjust up or down if fonts are rendering too small or too big on screen.')

$(gettext 'Current DPI setting: ') $CUR_DPI
$(gettext 'Detected monitor(s) DPI: ')
$MONITOR_DPI

$(gettext 'Note1: Some apps do not use antialiased rendering and will not be affected by this setting.')
$(gettext 'Note2: Change will only take affect for newly launched applications.')" \
0 0 0 $(make_radio_list $1)
}

# $1-default value
get_custom_dpi() {
	Xdialog --title "$APPTITLE" --stdout  --inputbox "$(gettext 'Please enter custom DPI')" 0 0 $1
}

# $1-new dpi
set_and_active_dpi() {
	sed -i -e '/^Xft.dpi:/ d' $HOME/.Xresources	
	echo "Xft.dpi: $1" >> $HOME/.Xresources
	echo "Xft.dpi: $1"  | xrdb -merge # apply
}



### main

# 1. read current dpi setting
CUR_DPI=$(xrdb -get Xft.dpi)
[ -z "$CUR_DPI" ] && CUR_DPI=96

# 2. read the monitor and idea
CALC_DPI=$(get_monitor_dpi)
MONITOR_DPI=${CALC_DPI}

# 3. Show selection and activate
# loop until we're finished, or cancelled
while NEW_DPI=$(select_dpi $CUR_DPI); do
	case $NEW_DPI in
		custom) NEW_DPI=$(get_custom_dpi $CUR_DPI) || continue
	esac

	if [ -z "$NEW_DPI" ]; then
		Xdialog --title "$APPTITLE" --infobox "$(gettext 'DPI cannot be left blank')" 0 0 10000
		continue
	fi

	# set dpi and exit
	if [ $NEW_DPI = $CUR_DPI ]; then
		Xdialog --title "$APPTITLE" --infobox "
$(gettext 'New DPI setting of ') $NEW_DPI $(gettext ' is same as existing one.')
$(gettext 'Nothing is changed.')
" 0 0 10000	
	else
		set_and_active_dpi $NEW_DPI
		Xdialog --title "$APPTITLE" --infobox "
$(gettext 'DPI has been set to: ') $NEW_DPI
$(gettext 'and activated for the next launched applications.')
" 0 0 10000
	fi
	exit 0
done
Xdialog --title "$APPTITLE" --infobox "
$(gettext 'Cancelled. Nothing is changed.')
" 0 0 10000

