#!/bin/bash
# Program: Display Control
# Purpose: screen saver
# License: (C) radky 2025 : GPL >= Ver. 3

export TEXTDOMAIN=displaycontrol
export OUTPUT_CHARSET=UTF-8

VERSION=1.5
APPDIR=/usr/local/dcontrol
ICONDIR=$APPDIR/icons
PREFDIR=$APPDIR/preferences
TMPDIR=/tmp/"`basename $0`"
mkdir -p $ICONDIR $PREFDIR
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                                                       #
#                                                                      #
########################################################################

# 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/screensaver.svg
ICON_HEIGHT=60
MSG_1="<b><span size='"'x-large'"'>$(gettext "Screen Saver")</span></b>"
MSG_2="<b>$(gettext "Set screen blanking delay")</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>"

########################################################################
#                                                                      #
# CURRENT DISPLAY SETTINGS: SCREEN SAVER                               #
#                                                                      #
########################################################################

# round seconds to nearest minute increment (pupx compatibility)
round60(){
	echo $(( ((${1%.*}+30)/60)*60 ))
}

# current screensaver status
	if [ -n "$WAYLAND_DISPLAY" ]; then
		. /etc/environment
		SCREENSAVERDELAY="$XWAYLAND_SCREENSAVER_DELAY"
		DPMS_ON=""
		SSDELAY=1
		SENSITIVE=false
	else
		SCREENSAVERDELAY=$(xset q | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -d " " -f3)
		DPMS_ON=$(xset q | grep 'DPMS is Enabled' 2>/dev/null)
		SSDELAY=$(xset q | grep ' timeout: ' | grep ' cycle: ' | tr -s ' ' | cut -d " " -f3)
		[ "$SSDELAY" = "" ] && SSDELAY=0
		SSDELAY=$(expr $SSDELAY / 60)
		SENSITIVE=true
	fi
	[ "$SCREENSAVERDELAY" = "" ] && SCREENSAVERDELAY=0
	SCREENSAVERDELAY=$(round60 $SCREENSAVERDELAY)
	SCREENSAVERDELAY=$(expr $SCREENSAVERDELAY / 60)
	echo "$SCREENSAVERDELAY" > $PREFDIR/timeout

	if [ "$SCREENSAVERDELAY" = "0" ]; then
		ENABLED_VISIBLE="false"
		DISABLED_VISIBLE="false"
		cp -f $APPDIR/icons/cancel.svg $APPDIR/icons/status.svg
	elif [ "$DPMS_ON" -o "$SSDELAY" -gt "0" ]; then
		ENABLED_VISIBLE="false"
		DISABLED_VISIBLE="true"
		cp -f $APPDIR/icons/on.svg $APPDIR/icons/status.svg
	else
		ENABLED_VISIBLE="true"
		DISABLED_VISIBLE="false"
		cp -f $APPDIR/icons/off.svg $APPDIR/icons/status.svg
	fi

	UPTIME=$(uptime | awk -F'( |,|:)+' '{if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0"d",h+0"h",m+0"m"}')

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

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

	'$HEADER'

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

	<hbox homogeneous="true">
		<pixmap>
			<variable>IMAGE</variable>
			<input file>'$APPDIR'/icons/status.svg</input><height>48</height><width>48</width>
		</pixmap>
	</hbox>

	<vbox sensitive="'"$DISABLED_VISIBLE"'" space-expand="false" space-fill="false" homogeneous="true">
		<text use-markup="true"><label>"<b><span size='"'medium'"'>'"$(gettext 'Delay')"'</span></b>"</label></text>
		<text use-markup="true"><label>"<span size='"'medium'"'>'$SCREENSAVERDELAY' '"$(gettext 'min')"'</span>"</label></text>
		<variable>DELAY_TXT</variable>
	</vbox>

	<text space-expand="true" space-fill="true"><label>" "</label></text>

	<vbox space-expand="false" space-fill="false" homogeneous="true">
		<text use-markup="true"><label>"<b><span size='"'medium'"'>'"$(gettext 'Uptime')"'</span></b>"</label></text>
		<text use-markup="true"><label>"<span size='"'medium'"'>'$UPTIME'</span>"</label></text>
	</vbox>

	<text space-expand="true" space-fill="true"><label>" "</label></text>

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

	<hbox space-expand="true" space-fill="true" homogeneous="true">
	<vbox space-expand="true" space-fill="true">
		<button sensitive="'"$ENABLED_VISIBLE"'" tooltip-text=" '"$(gettext 'Enable screen blanking 
 and power save mode.')"' " use-underline="true">
			<label>'"$(gettext '_Enable')"'</label>
			<variable>ENABLE_BUTTON</variable>
			<action>echo "true" > '$PREFDIR'/ss-status</action>
			<action>$APPDIR/func</action>
			<action>refresh:IMAGE</action>
			<action>enable:DISABLE_BUTTON</action>
			<action>disable:ENABLE_BUTTON</action>
			<action>enable:DELAY_TXT</action>
		</button>
	</vbox>
	<vbox space-expand="true" space-fill="true">
		<button sensitive="'"$DISABLED_VISIBLE"'" tooltip-text=" '"$(gettext 'Disable screen blanking 
 and power save mode.')"' " use-underline="true">
			<label>'"$(gettext '_Disable')"'</label>
			<variable>DISABLE_BUTTON</variable>
			<action>echo "false" > '$PREFDIR'/ss-status</action>
			<action>$APPDIR/func</action>
			<action>refresh:IMAGE</action>
			<action>enable:ENABLE_BUTTON</action>
			<action>disable:DISABLE_BUTTON</action>
			<action>disable:DELAY_TXT</action>
		</button>
	</vbox>
	</hbox>

	<hbox space-expand="true" space-fill="true" homogeneous="true">
	<vbox space-expand="true" space-fill="true">
		<button tooltip-text=" '"$(gettext 'Set screensaver delay (timeout)')"' " use-underline="true">
			<label>'"$(gettext '_Set delay')"'</label>
			<action>. '$APPDIR'/ss-control &</action>
			<action>exit:quit_now</action>
		</button>
	</vbox>
	<vbox space-expand="true" space-fill="true">
		<button tooltip-text=" '"$(gettext 'Blank screen immediately')"' " use-underline="true">
			<label>'"$(gettext 'Quick _blank')"'</label>
			<action>sleep 1.5 ; xset dpms force off</action>
			<sensitive>'${SENSITIVE}'</sensitive>
		</button>
	</vbox>
	</hbox>

	<hbox space-expand="true" space-fill="true" homogeneous="true">
	<vbox space-expand="true" space-fill="true">
		<button use-underline="true">
			<label>'"$(gettext '_Preferences')"'</label>
			<action>'$APPDIR'/dcontrol-gui &</action>
			<action>exit:quit_now</action>
			<sensitive>'${SENSITIVE}'</sensitive>
		</button>
	</vbox>
	<vbox space-expand="true" space-fill="true">
		<button use-underline="true">
			<label>'"$(gettext '_Quit')"'</label>
			<action>exit:quit_now</action>
		</button>
	</vbox>
	</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

exit 0
