#!/bin/bash
# Display Control
# Set screen tint/brightness & backlight
# Credit: brightness-set by James Budiono
# Gammastep required for Wayland Display
# License: (C) radky 2025 : GPL >= Ver. 3

export TEXTDOMAIN=displaycontrol
export OUTPUT_CHARSET=UTF-8

if [ "`which busybox`" ]; then
	DCPID=$(busybox ps | grep -a -- gtkdialog | grep -a -- DCONTROL | awk '{print $1}')
else
	DCPID=$(ps ax | grep -a -- gtkdialog | grep -a -- DCONTROL | awk '{print $1}')
fi
if [ "$DCPID" ]; then
	sleep 1
	[ "$DCPID" ] && gtkdialog-splash -close never -timeout 5 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Display Control is already active !') " && exit 0
fi

VERSION=1.5
APPDIR=/usr/local/dcontrol
ICONDIR=$APPDIR/icons
PREFDIR=$APPDIR/preferences
TMPDIR=/tmp/dcontrol
mkdir -p $ICONDIR $PREFDIR $TMPDIR
export VERSION APPDIR ICONDIR PREFDIR TMPDIR

[ "`which gtkdialog4 2>/dev/null`" ] && GTKDIALOG=gtkdialog4 || GTKDIALOG=gtkdialog
export GTKDIALOG

# set icon-name
[ ! -f /usr/share/icons/hicolor/48x48/apps/dcontrol.svg ] && \
ln -sf $ICONDIR/monitor.svg /usr/share/icons/hicolor/48x48/apps/dcontrol.svg && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#define GTK2 theme settings
if ! ldd $(which gtkdialog) | grep -iq 'gtk\-3'; then
 echo 'style "user-font" {
 font_name="Sans 10" }
 widget_class "*" style "user-font"' > $APPDIR/gtkrc_user-font
 export GTK2_RC_FILES=$HOME/.gtkrc-2.0:$APPDIR/gtkrc_user-font
fi

########################################################################
#                                                                      #
# GUI PARAMETERS                                                       #
#                                                                      #
########################################################################

# define width of gui widgets
CURRDPI=$(grep -a "^Xft\\.dpi:" $HOME/.Xresources 2>/dev/null | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' ')
[ ! "$CURRDPI" ] && CURRDPI="110"
if [[ $CURRDPI -ge 130 ]]; then
	widget_width="328"
elif [[ $CURRDPI -ge 120 ]]; then
	widget_width="308"
elif [[ $CURRDPI -ge 110 ]]; then
	widget_width="288"
else
	widget_width="268"
fi
export widget_width

# set header
XML_INFO_COLOR='#EDEBD7' # background color
XML_INFO_OPACITY=0.5 # background opacity
. $APPDIR/xml_info_dcontrol gtk > /dev/null # build bg_pixmap for gtk-theme

BOX_HEIGHT=90 # HEADER
ICON=$ICONDIR/monitor.svg
ICON_HEIGHT=60
MSG_1="<b><span size='"'x-large'"'>$(gettext "Display Control")</span></b>"
MSG_2="<b>$(gettext "Set screen backlight intensity")</b>"
[ -n "$WAYLAND_DISPLAY" ] && MSG_2="<b>$(gettext "Set screen tint and brightness")</b>"
ALIGN=center # center or left
HEADER="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

# set screen lock parameters
if [ -f /sbin/puplock -a -f /bin/swaylock ] && [ -n "$WAYLAND_DISPLAY" ]; then
	SCREENLOCK='<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen lock settings')"' ">
			<input file>'$ICONDIR'/screen_lock.svg</input><height>36</height><width>36</width>
			<action>exec /usr/bin/screenlock.sh &</action>
		</button>'
else
	SCREENLOCK='<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen lock')"' ">
			<input file>'$ICONDIR'/screen_lock.svg</input><height>36</height><width>36</width>
			<action>'$APPDIR'/xlock-wrapper &</action>
		</button>
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen lock settings')"' ">
			<input file>'$ICONDIR'/screen_lock_ctl.svg</input><height>36</height><width>36</width>
			<action>/usr/local/apps/Xlock/AppRun -configure &</action>
		</button>'
fi

# define screen configuration manager
if  [ -n "$WAYLAND_DISPLAY" ]; then
	if [ -x /usr/bin/wdisplays-wrapper ]; then
		SCREENCONFIG='<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen configuration')"' ">
			<input file>'$ICONDIR'/lxrandr.svg</input><height>36</height><width>36</width>
			<action>wdisplays-wrapper &</action>
		</button>'
	elif [ -x /usr/sbin/wmonitors.sh ]; then
		SCREENCONFIG='<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen configuration')"' ">
			<input file>'$ICONDIR'/lxrandr.svg</input><height>36</height><width>36</width>
			<action>wmonitors.sh &</action>
		</button>'
	fi
fi

# define video framrate
if [ "`which glxgears 2>/dev/null`" ]; then
	GLXGEARS='
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Video framerate')"' ">
			<input file>'$ICONDIR'/graph.svg</input><height>36</height><width>36</width>
			<action>xterm -hold -e glxgears &</action>
		</button>'
fi

########################################################################
#                                                                      #
# GAMMASTEP FUNCTION                                                   #
#                                                                      #
########################################################################

if [ -n "$WAYLAND_DISPLAY" ]; then
	# set screen color temperature and brightness
	func_gammastep_preset(){
		[ "$gammastep_preset" = "$(gettext 'daylight')" ] && ctemp=6500 brightness=1.00
		[ "$gammastep_preset" = "$(gettext 'reading')"  ] && ctemp=5500 brightness=0.90
		[ "$gammastep_preset" = "$(gettext 'twilight')" ] && ctemp=4500 brightness=0.85
		[ "$gammastep_preset" = "$(gettext 'night-1')"  ] && ctemp=4000 brightness=0.80
		[ "$gammastep_preset" = "$(gettext 'night-2')"  ] && ctemp=3500 brightness=0.75
		[ "$gammastep_preset" = "$(gettext 'night-3')"  ] && ctemp=3000 brightness=0.70
		[ "$gammastep_preset" = "$(gettext 'night-4')"  ] && ctemp=2500 brightness=0.65
		[ "$gammastep_preset" = "$(gettext 'night-5')"  ] && ctemp=2000 brightness=0.60
		kill -9 $(pgrep gammastep)
		gammastep -P -O ${ctemp} -b ${brightness} &
		echo "#!/bin/sh
gammastep -P -O ${ctemp} -b ${brightness} &" > $HOME/Startup/gammastep-preset
		chmod +x $HOME/Startup/gammastep-preset
	}
	export -f func_gammastep_preset

	# current gammastep presets (comboboxtext)
	gammastep_preset=$(cat $PREFDIR/gammastep-preset 2>/dev/null)
	[ ! "$gammastep_preset" ] && gammastep_preset="daylight" && echo "$gammastep_preset" > $PREFDIR/gammastep-preset

	GAMMASTEP='
		<hbox homogeneous="true">
		<vbox>
			<text tooltip-text=" '"$(gettext 'Tint and brightness presets')"' 
 '$(gettext 'daylight')'	6500K		1.00 
 '$(gettext 'reading')' 	5500K		0.90 
 '$(gettext 'twilight')'	4500K		0.85 
 '$(gettext 'night-1')' 	4000K		0.80 
 '$(gettext 'night-2')' 	3500K		0.75 
 '$(gettext 'night-3')' 	3000K		0.70 
 '$(gettext 'night-4')' 	2500K		0.65
 '$(gettext 'night-5')' 	2000K		0.60 "><label>"'"$(gettext 'Screen Presets')"'"</label></text>
			<comboboxtext width-request="'"$widget_width"'">
				<variable>gammastep_preset</variable>
				<default>'$gammastep_preset'</default>
				<item>'$(gettext 'daylight')'</item>
				<item>'$(gettext 'reading')'</item>
				<item>'$(gettext 'twilight')'</item>
				<item>'$(gettext 'night-1')'</item>
				<item>'$(gettext 'night-2')'</item>
				<item>'$(gettext 'night-3')'</item>
				<item>'$(gettext 'night-4')'</item>
				<item>'$(gettext 'night-5')'</item>
				<action>echo "$gammastep_preset" > '$PREFDIR'/gammastep-preset</action>
				<action>func_gammastep_preset</action>
			</comboboxtext>
		</vbox>
	</hbox>'
else
	GAMMASTEP=''
fi

########################################################################
#                                                                      #
# BACKLIGHT FUNCTION                                                   #
#                                                                      #
########################################################################

# set screen backlight (brightness-set or brightnessctl)
func_backlight(){
	backlight_option=$(cat $PREFDIR/backlight-option 2>/dev/null)
	if [ "$backlight_option" = "brightnessctl" ]; then
		brightnessctl --class=backlight set ${backlight}%
		echo "#!/bin/sh" > $HOME/Startup/brightness-set
		echo "brightnessctl --class=backlight set ${backlight}%" >> $HOME/Startup/brightness-set
		chmod +x $HOME/Startup/brightness-set
	else
		$APPDIR/brightness-set ${backlight}
		echo "#!/bin/sh" > $HOME/Startup/brightness-set
		echo "$APPDIR/brightness-set ${backlight}" >> $HOME/Startup/brightness-set
		chmod +x $HOME/Startup/brightness-set
	fi
}
export -f func_backlight

########################################################################
#                                                                      #
# CURRENT DISPLAY SETTINGS                                             #
#                                                                      #
########################################################################

BACKLIGHT=''
BACKLIGHT_OPTION='true' # false to disable

if [ "$BACKLIGHT_OPTION" = "true" ]; then
  DEVPATH=${DEVPATH:-/sys/class/backlight}
  DEVICE=${DEVICE:-$(ls $DEVPATH | head -n 1)}
  if [ -n "$DEVICE" ]; then
	[ ! -f /usr/bin/brightness-set ] && ln -sf $APPDIR/brightness-set /usr/bin/brightness-set
	backlight=$(cat $PREFDIR/backlight 2>/dev/null)
	[ ! "$backlight" ] && backlight=100 && echo "$backlight" > $PREFDIR/backlight
	echo "$backlight" > $TMPDIR/backlight
	func_backlight

	BACKLIGHT='
	<hbox homogeneous="true">
	<vbox>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<text tooltip-text=" '"$(gettext 'Hardware backlight intensity (1-100%)')"' "><label>"'"$(gettext 'Backlight')"'"</label></text>
		<hscale width-request="'"$widget_width"'" height-request="15" scale-min="1" scale-max="100" scale-step="1" value-pos="1" inverted="false">
			<input file>'$TMPDIR'/backlight</input>
			<variable>backlight</variable>
			<action>echo "$backlight" > '$TMPDIR'/backlight</action>
			<action>func_backlight</action>
		</hscale>
	</vbox>
	</hbox>'

	[ "`which brightnessctl 2>/dev/null`" ] && BRIGHTNESS_CTL='
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Toggle backlight control')"' ">
			<input file>'$ICONDIR'/toggle2.svg</input><height>36</height><width>36</width>
			<action>'$APPDIR'/backlight-app &</action>
			<action>exit:quit_now</action>
		</button>'

	APP_TXT="<text use-markup=\"true\"><label>\"<b>$(gettext 'Set screen backlight intensity')</b>\"</label></text>"
	[ -n "$WAYLAND_DISPLAY" ] && APP_TXT="<text use-markup=\"true\"><label>\"<b>$(gettext 'Set screen tint and brightness')</b>\"</label></text>"

  else
	MSG_1="<b><span size='"'x-large'"'>$(gettext "Screen Saver")</span></b>"
	MSG_2="<b>$(gettext "Set screen saver options")</b>"
	HEADER="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

	APP_TXT="<text use-markup=\"true\"><label>\"<b>$(gettext 'Set screen saver options')</b>\"</label></text>"
  fi
else
  MSG_1="<b><span size='"'x-large'"'>$(gettext "Screen Saver")</span></b>"
  MSG_2="<b>$(gettext "Set screen saver options")</b>"
  HEADER="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

  APP_TXT="<text use-markup=\"true\"><label>\"<b>$(gettext 'Set screen saver options')</b>\"</label></text>"
fi

export APP_TXT

########################################################################
#                                                                      #
# HELP DIALOG                                                          #
#                                                                      #
########################################################################

display_control_help()
{
export DCH="
<window default-width=\"425\" title=\"$(gettext 'Display Control')\" icon-name=\"dcontrol\" window-position=\"3\" resizable=\"false\">
	<vbox margin=\"10\" width-request=\"425\">
		<pixmap><input file>$ICONDIR/monitor.svg</input><height>72</height><width>72</width></pixmap>
		<text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>Display Control</span> $VERSION</b>\"</label></text>
		$APP_TXT
		<text><label>\"Roger D. Grider (C) 2025 GPL3\"</label></text>
		<pixmap><input file>/usr/local/dcontrol/icons/blank1.png</input></pixmap>
		<pixmap><input file>/usr/local/dcontrol/icons/blank1.png</input></pixmap>
		<hbox space-expand=\"false\" space-fill=\"false\" homogeneous=\"true\">
			<button width-request=\"250\" tooltip-text=\" http://www.gnu.org/licenses \">
				<label>\"$(gettext 'License')\"</label>
				<action>defaulthtmlviewer http:\/\/www.gnu.org\/licenses\/ &</action>
			</button>
		</hbox>
	</vbox>
</window>"
$GTKDIALOG -p DCH
unset DCH
}
export -f display_control_help

########################################################################
#                                                                      #
# MAIN DIALOG                                                          #
#                                                                      #
########################################################################

export DCONTROL='
<window title="'"$(gettext 'Display Control')"'" icon-name="dcontrol" window-position="3" resizable="false">
<vbox>

	'$HEADER'

	<hbox homogeneous="true">
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
	</hbox>

	'$GAMMASTEP'
	'$BACKLIGHT'

	<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
	<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
	<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
	<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>

	<hbox space-expand="false" space-fill="true" homogeneous="true">
		<text space-expand="true" space-fill="true"><label>" "</label></text>
		<text space-expand="true" space-fill="true"><label>" "</label></text>
		'$BRIGHTNESS_CTL'
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen saver settings')"' ">
			<input file>'$ICONDIR'/screensaver.svg</input><height>36</height><width>36</width>
			<action>'$APPDIR'/dcontrol-screensaver &</action>
		</button>
		'$SCREENLOCK'
		'$SCREENCONFIG'
		'$GLXGEARS'
		<text space-expand="true" space-fill="true"><label>" "</label></text>
		<text space-expand="true" space-fill="true"><label>" "</label></text>
	</hbox>

	<hseparator space-expand="false" space-fill="false"></hseparator>

	<hbox space-expand="false" space-fill="true" homogeneous="true">
		<button use-underline="true">
			<label>'$(gettext '_Help')'</label>
			<action>display_control_help &</action>
		</button>
		<button use-underline="true">
			<label>'$(gettext '_Quit')'</label>
			<action>exit:quit_now</action>
		</button>
	</hbox>
</vbox>

<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

$GTKDIALOG -p DCONTROL --styles=/tmp/dcontrol/gtkrc_xml_info.css

unset DCONTROL

[ -f $TMPDIR/backlight ] && cp -f $TMPDIR/backlight $PREFDIR/backlight

exit 0
