#!/bin/bash
# Program: Display Control
# Purpose: gui for xrandr & sct utilities
# Action: adjust screen tint & brightness
# Dependency: sct with brightness support
# Credit: brightness-set by James Budiono
# License: (C) radky 2025 : GPL >= Ver. 3
# 24-Dec-2024: add Xwayland compatibility
# and update to Fabian Foerg's 64-bit sct

export TEXTDOMAIN=displaycontrol
export OUTPUT_CHARSET=UTF-8

# load cutdown dcontrol gui if Xwayland is active (no Xorg)
if [ -n "$WAYLAND_DISPLAY" ]; then
	/usr/local/dcontrol/dcontrol-backlight &
	exit 0
fi

# load cutdown dcontrol gui if user prefers screensaver interface only
gui_option=$(cat /usr/local/dcontrol/preferences/gui-option 2>/dev/null)
if [ "$gui_option" = "screensaver" ]; then
	/usr/local/dcontrol/dcontrol-screensaver &
	exit 0
fi

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/"`basename $0`"
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

# first-run message
display=$(cat $PREFDIR/display 2>/dev/null)
[ ! "$display" ] && gtkdialog-splash -close never -timeout 2 -margin 8 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing, please wait...') "

#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

#define sct executable (32-bit/64-bit)
if [ ! -L /usr/bin/sct ]; then
	. /etc/DISTRO_SPECS
	if [ "$DISTRO_TARGETARCH" = "x86_64" -o "$DISTRO_TARGETARCH" = "amd64" ]; then
		ln -snf $APPDIR/sct/64-bit/sct /usr/bin/sct
	elif [ "$DISTRO_TARGETARCH" = "x86" ]; then
		ln -snf $APPDIR/sct/32-bit/sct /usr/bin/sct
	fi
fi
chmod +x /usr/bin/sct

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

# define height of gui and width of 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
	gui_height="496" cbox1_width="161" cbox2_width="162" cbox3_width="328" scale_width="328"
elif [[ $CURRDPI -ge 120 ]]; then
	gui_height="456" cbox1_width="151" cbox2_width="152" cbox3_width="308" scale_width="308"
elif [[ $CURRDPI -ge 110 ]]; then
	gui_height="436" cbox1_width="141" cbox2_width="142" cbox3_width="288" scale_width="288"
else
	gui_height="416" cbox1_width="131" cbox2_width="132" cbox3_width="268" scale_width="268"
fi
export cbox1_width cbox2_width cbox3_width scale_width

# show/hide first tab of gui (xrandr interface)
	tab1=$(cat $PREFDIR/tab1 2>/dev/null)
	[ ! "$tab1" ] && tab1="true" && echo "$tab1" > $PREFDIR/tab1
	export tab1

# show/hide second tab of gui (sct interface)
	if [ "`which sct 2>/dev/null`" ]; then
		tab2=$(cat $PREFDIR/tab2 2>/dev/null)
		[ ! "$tab2" ] && tab2="false" && echo "$tab2" > $PREFDIR/tab2
		export tab2
	fi

# set headers
	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_1 (xrandr and sct interfaces)
	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 tint and brightness")</b>"
	ALIGN=center # center or left
	HEADER_1="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

	BOX_HEIGHT=90 # HEADER_2 (screen saver interface)
	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_2="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_dcontrol "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

########################################################################
#                                                                      #
# INPUT DATA: XRANDR                                                   #
#                                                                      #
########################################################################

	# RGB and Color Temperature values are
	# based on Redshift's colorramp.c with
	# minor g-float adjustments for xrandr
	export rgb_vals='
	1.00000000:0.18172716:0.00000000 1000K
	1.00000000:0.25503671:0.00000000 1100K
	1.00000000:0.30942099:0.00000000 1200K
	1.00000000:0.35357379:0.00000000 1300K
	1.00000000:0.39091524:0.00000000 1400K
	1.00000000:0.42322816:0.00000000 1500K
	1.00000000:0.45159884:0.00000000 1600K
	1.00000000:0.47675916:0.00000000 1700K
	1.00000000:0.49923747:0.00000000 1800K
	1.00000000:0.51943421:0.00000000 1900K
	1.00000000:0.54360078:0.08679949 2000K
	1.00000000:0.56618736:0.14065513 2100K
	1.00000000:0.58734976:0.18362641 2200K
	1.00000000:0.60724493:0.22137978 2300K
	1.00000000:0.62600248:0.25591950 2400K
	1.00000000:0.60373109:0.28819679 2500K
	1.00000000:0.62052319:0.31873863 2600K
	1.00000000:0.63645822:0.34786758 2700K
	1.00000000:0.65160518:0.37579588 2800K
	1.00000000:0.66602449:0.40267128 2900K
	1.00000000:0.67976951:0.42860152 3000K
	1.00000000:0.69288760:0.45366838 3100K
	1.00000000:0.70542112:0.47793608 3200K
	1.00000000:0.71740814:0.50145662 3300K
	1.00000000:0.72888303:0.52427322 3400K
	1.00000000:0.77987699:0.54642268 3500K
	1.00000000:0.79041843:0.56793692 3600K
	1.00000000:0.80053332:0.58884417 3700K
	1.00000000:0.81024551:0.60916971 3800K
	1.00000000:0.81957693:0.62893653 3900K
	1.00000000:0.82854786:0.64816570 4000K
	1.00000000:0.83717703:0.66687674 4100K
	1.00000000:0.84548188:0.68508786 4200K
	1.00000000:0.85347859:0.70281616 4300K
	1.00000000:0.86118227:0.72007777 4400K
	1.00000000:0.86860704:0.73688797 4500K
	1.00000000:0.87576611:0.75326132 4600K
	1.00000000:0.88267187:0.76921169 4700K
	1.00000000:0.88933596:0.78475236 4800K
	1.00000000:0.89576933:0.79989606 4900K
	1.00000000:0.90198230:0.81465502 5000K
	1.00000000:0.90963069:0.82838210 5100K
	1.00000000:0.91710889:0.84190889 5200K
	1.00000000:0.92441842:0.85523742 5300K
	1.00000000:0.93156127:0.86836903 5400K
	1.00000000:0.93853986:0.88130458 5500K
	1.00000000:0.94535695:0.89404470 5600K
	1.00000000:0.95201559:0.90658983 5700K
	1.00000000:0.95851906:0.91894041 5800K
	1.00000000:0.96487079:0.93109690 5900K
	1.00000000:0.97107439:0.94305985 6000K
	1.00000000:0.97713351:0.95482993 6100K
	1.00000000:0.98305189:0.96640795 6200K
	1.00000000:0.98883326:0.97779486 6300K
	1.00000000:0.99448139:0.98899179 6400K
	1.00000000:1.00000000:1.00000000 6500K
	0.98947904:0.99348723:1.00000000 6600K
	0.97940448:0.98722715:1.00000000 6700K
	0.96975025:0.98120637:1.00000000 6800K
	0.96049223:0.97541240:1.00000000 6900K
	0.95160805:0.96983355:1.00000000 7000K
	0.94303638:0.96443333:1.00000000 7100K
	0.93480451:0.95923080:1.00000000 7200K
	0.92689056:0.95421394:1.00000000 7300K
	0.91927697:0.94937330:1.00000000 7400K
	0.91194747:0.94470005:1.00000000 7500K
	0.90488690:0.94018594:1.00000000 7600K
	0.89808115:0.93582323:1.00000000 7700K
	0.89151710:0.93160469:1.00000000 7800K
	0.88518247:0.92752354:1.00000000 7900K
	0.87906581:0.92357340:1.00000000 8000K
	0.87315640:0.91974827:1.00000000 8100K
	0.86744421:0.91604254:1.00000000 8200K
	0.86191983:0.91245088:1.00000000 8300K
	0.85657444:0.90896831:1.00000000 8400K
	0.85139976:0.90559011:1.00000000 8500K
	0.84638799:0.90231183:1.00000000 8600K
	0.84153180:0.89912926:1.00000000 8700K
	0.83682430:0.89603843:1.00000000 8800K
	0.83225897:0.89303558:1.00000000 8900K
	0.82782969:0.89011714:1.00000000 9000K
	0.82353066:0.88727974:1.00000000 9100K
	0.81935641:0.88452017:1.00000000 9200K
	0.81530175:0.88183541:1.00000000 9300K
	0.81136180:0.87922257:1.00000000 9400K
	0.80753191:0.87667891:1.00000000 9500K'

########################################################################
#                                                                      #
# FUNCTIONS: XRANDR                                                    #
#                                                                      #
########################################################################

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

# set screen tint and brightness (xrandr)
func_set_xrandr(){
	ctemp="${ctemp_xrandr:0:2}00"
	gamma=$(echo "${rgb_vals}" | grep "${ctemp}K" | awk '{print $1}')
	brightness=$(echo "scale=2; ${brightness_xrandr} / 100" | bc)
	LC_NUMERIC=C xrandr --output ${display} --gamma ${gamma} --brightness ${brightness}
	echo "#!/bin/sh
	LC_NUMERIC=C xrandr --output ${display} --gamma ${gamma} --brightness ${brightness}" > $HOME/Startup/display-${display}
	chmod +x $HOME/Startup/display-${display}
}
export -f func_set_xrandr

# preset values for tint and brightness (xrandr)
func_preset_xrandr(){
	cct=$(cat $PREFDIR/cct-xrandr 2>/dev/null) || cct=6500
	cbr=$(cat $PREFDIR/cbr-xrandr 2>/dev/null) || cbr=100

	[ "$preset_xrandr" = "$(gettext 'gaming')"   ] && ctemp_xrandr=9500 brightness_xrandr=130
	[ "$preset_xrandr" = "$(gettext 'cinema')"   ] && ctemp_xrandr=8000 brightness_xrandr=115
	[ "$preset_xrandr" = "$(gettext 'daylight')" ] && ctemp_xrandr=6500 brightness_xrandr=100
	[ "$preset_xrandr" = "$(gettext 'reading')"  ] && ctemp_xrandr=5500 brightness_xrandr=85
	[ "$preset_xrandr" = "$(gettext 'twilight')" ] && ctemp_xrandr=4500 brightness_xrandr=70
	[ "$preset_xrandr" = "$(gettext 'night-1')"  ] && ctemp_xrandr=3500 brightness_xrandr=60
	[ "$preset_xrandr" = "$(gettext 'night-2')"  ] && ctemp_xrandr=3000 brightness_xrandr=50
	[ "$preset_xrandr" = "$(gettext 'custom')"   ] && ctemp_xrandr=$cct brightness_xrandr=$cbr

	echo "$ctemp_xrandr" > $TMPDIR/ctemp-xrandr
	echo "$brightness_xrandr" > $TMPDIR/brightness-xrandr
	func_set_xrandr
}
export -f func_preset_xrandr

# current screen_status (xrandr)
func_info_xrandr(){
	BKLIGHT=""
	if [ ! -z "$backlight_xrandr" ]; then
		BKLIGHT="\nBACKLIGHT\n(hardware)\n${backlight_xrandr}%\n"
	fi
	BRIGHTNESS=$(cat $TMPDIR/brightness-xrandr 2>/dev/null)
	ctemp_xrandr=$(cat $TMPDIR/ctemp-xrandr 2>/dev/null)
	ctemp="${ctemp_xrandr:0:2}00"
	read R G B ctemp << EOF
	`echo "$rgb_vals" | grep "$ctemp" | sed 's/:/ /g'`
EOF
	COLORTEMP=$ctemp_xrandr
	Xdialog --title "Screen status" --msgbox "\nSCREEN\n${display}\n\n               COLOR TEMPERATURE               \n${COLORTEMP}K\n\nGAMMA\nred: ${R}\ngreen: ${G}\nblue: ${B}\n\nBRIGHTNESS\n${BRIGHTNESS}%\n${BKLIGHT}" 0 0
}
export -f func_info_xrandr

# toggle xrandr and sct interfaces
func_toggle_gui(){
	tab1=$(cat $PREFDIR/tab1 2>/dev/null)
	if [ "$tab1" = "true" ]; then
		echo "false" > $PREFDIR/tab1
		echo "true" > $PREFDIR/tab2
		rm -f $HOME/Startup/display-* 2>/dev/null
	else
		echo "true" > $PREFDIR/tab1
		echo "false" > $PREFDIR/tab2
		rm -f $HOME/Startup/display-sct 2>/dev/null
	fi
	sleep 0.2 && $APPDIR/dcontrol &
}
export -f func_toggle_gui

# rescan screens and reset default options
func_reset_default(){
	rm -f $PREFDIR/tab1 $PREFDIR/display $PREFDIR/display-items $PREFDIR/preset-xrandr $TMPDIR/preset-xrandr $PREFDIR/brightness-xrandr $TMPDIR/brightness-xrandr $PREFDIR/ctemp-xrandr* $TMPDIR/ctemp-xrandr* $PREFDIR/backlight $TMPDIR/backlight $PREFDIR/cbr* $PREFDIR/cct* $HOME/Startup/display-* $HOME/Startup/brightness-set /etc/brightness.conf 2>/dev/null
	rm -f $PREFDIR/tab2 $PREFDIR/preset-sct $TMPDIR/preset-sct $PREFDIR/brightness-sct $TMPDIR/brightness-sct $PREFDIR/ctemp-sct $TMPDIR/ctemp-sct $HOME/Startup/display-sct 2>/dev/null
	$APPDIR/dcontrol &
}
export -f func_reset_default

########################################################################
#                                                                      #
# FUNCTIONS: SCT                                                       #
#                                                                      #
########################################################################

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

# set screen tint and brightness (sct)
func_set_sct(){
	ctemp="${ctemp_sct:0:2}00"
	brightness=$(echo "scale=2; ${brightness_sct} / 100" | bc)
	sct ${ctemp} ${brightness}
	echo "#!/bin/sh
	sct ${ctemp} ${brightness}" > $HOME/Startup/display-sct
	chmod +x $HOME/Startup/display-sct
}
export -f func_set_sct

# preset values for tint and brightness (sct)
func_preset_sct(){
	cct=$(cat $PREFDIR/cct-sct 2>/dev/null) || cct=6500
	cbr=$(cat $PREFDIR/cbr-sct 2>/dev/null) || cbr=100

	[ "$preset_sct" = "$(gettext 'gaming')"   ] && ctemp_sct=9500 brightness_sct=100
	[ "$preset_sct" = "$(gettext 'cinema')"   ] && ctemp_sct=8000 brightness_sct=100
	[ "$preset_sct" = "$(gettext 'daylight')" ] && ctemp_sct=6500 brightness_sct=100
	[ "$preset_sct" = "$(gettext 'reading')"  ] && ctemp_sct=5500 brightness_sct=90
	[ "$preset_sct" = "$(gettext 'twilight')" ] && ctemp_sct=4500 brightness_sct=80
	[ "$preset_sct" = "$(gettext 'night-1')"  ] && ctemp_sct=3500 brightness_sct=70
	[ "$preset_sct" = "$(gettext 'night-2')"  ] && ctemp_sct=3000 brightness_sct=60
	[ "$preset_sct" = "$(gettext 'custom')"   ] && ctemp_sct=$cct brightness_sct=$cbr

	echo "$ctemp_sct" > $TMPDIR/ctemp-sct
	echo "$brightness_sct" > $TMPDIR/brightness-sct
	func_set_sct
}
export -f func_preset_sct

# current screen_status (sct)
func_info_sct(){
	BKLIGHT=""
	if [ ! -z "$backlight_sct" ]; then
		BKLIGHT="\nBACKLIGHT\n(hardware)\n${backlight_sct}%\n"
	fi
	BRIGHTNESS=$(cat $TMPDIR/brightness-sct 2>/dev/null)
	ctemp_sct=$(cat $TMPDIR/ctemp-sct 2>/dev/null)
	ctemp="${ctemp_sct:0:2}00"
	read R G B ctemp << EOF
	`echo "$rgb_vals" | grep "$ctemp" | sed 's/:/ /g'`
EOF
	COLORTEMP=$ctemp_sct
	Xdialog --title "Screen status" --msgbox "\nSCREEN\n${display}\n\n               COLOR TEMPERATURE               \n${COLORTEMP}K\n\nGAMMA\nred: ${R}\ngreen: ${G}\nblue: ${B}\n\nBRIGHTNESS\n${BRIGHTNESS}%\n${BKLIGHT}" 0 0
}
export -f func_info_sct

########################################################################
#                                                                      #
# CURRENT DISPLAY SETTINGS: XRANDR                                     #
#                                                                      #
########################################################################

# display and display_items (comboboxtext)
	display=$(cat $PREFDIR/display 2>/dev/null)
	[ ! "$display" ] && display=$(xrandr --prop | grep " connected" | head -n1 | awk '{print $1}') && echo "$display" > $PREFDIR/display
	display_items=$(cat $PREFDIR/display-items 2>/dev/null)
	[ ! "$display_items" ] && display_items=$(xrandr --prop | grep " connected" | awk '{print $1}' | sort -u | awk '{print "<item>"$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8"</item>"}' | sed 's/ //g') && echo "$display_items" > $PREFDIR/display-items

# current preset (comboboxtext)
	preset_xrandr=$(cat $PREFDIR/preset-xrandr 2>/dev/null)
	[ ! "$preset_xrandr" ] && preset_xrandr="daylight" && echo "$preset_xrandr" > $PREFDIR/preset-xrandr
	echo "$preset_xrandr" > $TMPDIR/preset-xrandr

# current color temperature (hscale)
	ctemp_xrandr=$(cat $PREFDIR/ctemp-xrandr 2>/dev/null)
	[ ! "$ctemp_xrandr" ] && ctemp_xrandr="6500" && echo "$ctemp_xrandr" > $PREFDIR/ctemp-xrandr
	echo "$ctemp_xrandr" > $TMPDIR/ctemp-xrandr

# current brightness (hscale)
	brightness_xrandr=$(cat $PREFDIR/brightness-xrandr 2>/dev/null)
	[ ! "$brightness_xrandr" ] && brightness_xrandr="100" && echo "$brightness_xrandr" > $PREFDIR/brightness-xrandr
	echo "$brightness_xrandr" > $TMPDIR/brightness-xrandr

# apply current color temperature and brightness settings
	[ "$tab1" = "true" ] && func_set_xrandr

########################################################################
#                                                                      #
# CURRENT DISPLAY SETTINGS: BACKLIGHT                                  #
#                                                                      #
########################################################################

BACKLIGHT='' BRIGHTNESS_CTL=''
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_xrandr=$(cat $PREFDIR/backlight 2>/dev/null)
	[ ! "$backlight_xrandr" ] && backlight_xrandr=100 && echo "$backlight_xrandr" > $PREFDIR/backlight
	echo "$backlight_xrandr" > $TMPDIR/backlight
	func_backlight_set_xrandr

	BACKLIGHT_XRANDR='
	<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="'"$scale_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_xrandr</variable>
			<action>echo "$backlight_xrandr" > '$TMPDIR'/backlight</action>
			<action>func_backlight_set_xrandr</action>
		</hscale>
	</vbox>
	</hbox>'

	BACKLIGHT_SCT='
	<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="'"$scale_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_sct</variable>
			<action>echo "$backlight_sct" > '$TMPDIR'/backlight</action>
			<action>func_backlight_set_sct</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>28</height><width>28</width>
			<action>'$APPDIR'/backlight-app &</action>
			<action>exit:quit_now</action>
		</button>'
  fi
fi

########################################################################
#                                                                      #
# 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"}')

########################################################################
#                                                                      #
# OPTIONAL DISPLAY SETTINGS: XORG-VIDEO-WIZARD AND LXRANDR             #
#                                                                      #
########################################################################

XORG_WIZARD='' LXRANDR='' 

[ "`which xorgwizard 2>/dev/null`" ] && XORG_WIZARD='
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Xorg video wizard')"' ">
			<input file>'$ICONDIR'/xorg.svg</input><height>28</height><width>28</width>
			<action>xorgwizard &</action>
		</button>'

[ "`which lxrandr 2>/dev/null`" ] && LXRANDR='
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Display settings')"' ">
			<input file>'$ICONDIR'/lxrandr.svg</input><height>28</height><width>28</width>
			<action>lxrandr &</action>
		</button>'

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

########################################################################
#                                                                      #
# OPTIONAL GRAPHICAL INTERFACE: SCT                                    #
#                                                                      #
########################################################################

if [ "`which sct 2>/dev/null`" ]; then
	# current preset_sct (comboboxtext)
	preset_sct=$(cat $PREFDIR/preset-sct 2>/dev/null)
	[ ! "$preset_sct" ] && preset_sct="daylight" && echo "$preset_sct" > $PREFDIR/preset-sct
	echo "$preset_sct" > $TMPDIR/preset-sct

	# current sct (hscale)
	ctemp_sct=$(cat $PREFDIR/ctemp-sct 2>/dev/null)
	[ ! "$ctemp_sct" ] && ctemp_sct="6500" && echo "$ctemp_sct" > $PREFDIR/ctemp-sct
	echo "$ctemp_sct" > $TMPDIR/ctemp-sct

	# current brightness_sct (hscale)
	brightness_sct=$(cat $PREFDIR/brightness-sct 2>/dev/null)
	[ ! "$brightness_sct" ] && brightness_sct="100" && echo "$brightness_sct" > $PREFDIR/brightness-sct
	echo "$brightness_sct" > $TMPDIR/brightness-sct

	# apply current color temperature and brightness settings
	[ "$tab2" = "true" ] && func_set_sct

	NB_LABELS='<notebook labels=" '"$(gettext 'Xrandr') | $(gettext 'Sct') | $(gettext 'Screen Saver')"' ">'

	TOGGLE_GUI='
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Toggle xrandr and sct interfaces')"' ">
			<input file>'$ICONDIR'/toggle.svg</input><height>28</height><width>28</width>
			<action>func_toggle_gui &</action>
			<action>exit:quit_now</action>
		</button>'

	SCT_TAB='<vbox visible="'"$tab2"'" height-request="'"$gui_height"'">

	'$HEADER_1'

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

	<hbox homogeneous="true">
	<hbox>
	<vbox>
		<text tooltip-text=" '"$(gettext 'Tint and brightness presets')"' "><label>"'"$(gettext 'Presets')"'"</label></text>
		<comboboxtext width-request="'"$cbox3_width"'">
			<variable>preset_sct</variable>
			<default>'$preset_sct'</default>
			<item>'$(gettext 'gaming')'</item>
			<item>'$(gettext 'cinema')'</item>
			<item>'$(gettext 'daylight')'</item>
			<item>'$(gettext 'reading')'</item>
			<item>'$(gettext 'twilight')'</item>
			<item>'$(gettext 'night-1')'</item>
			<item>'$(gettext 'night-2')'</item>
			<item>'$(gettext 'custom')'</item>
			<action>echo "$preset_sct" > '$TMPDIR'/preset-sct</action>
			<action>func_preset_sct</action>
			<action>refresh:ctemp_sct</action>
			<action>refresh:brightness_sct</action>
		</comboboxtext>
	</vbox>
	</hbox>
	</hbox>

	<hbox homogeneous="true">
	<vbox>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<text tooltip-text=" '"$(gettext 'Screen color temperature (1000-9500K)')"' "><label>"'"$(gettext 'Tint')"'"</label></text>
		<hscale width-request="'"$scale_width"'" height-request="15" scale-min="1000" scale-max="9500" scale-step="10" value-pos="1" inverted="false">
			<variable>ctemp_sct</variable>
			<input file>'$TMPDIR'/ctemp-sct</input>
			<action>echo "$ctemp_sct" > '$TMPDIR'/ctemp-sct</action>
			<action>func_set_sct</action>
		</hscale>
	</vbox>
	</hbox>

	<hbox homogeneous="true">
	<vbox>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<text tooltip-text=" '"$(gettext 'Relative screen illumination (30-100%)')"' "><label>"'"$(gettext 'Brightness')"'"</label></text>
		<hscale width-request="'"$scale_width"'" height-request="15" scale-min="30" scale-max="100" scale-step="1" value-pos="1" inverted="false">
			<variable>brightness_sct</variable>
			<input file>'$TMPDIR'/brightness-sct</input>
			<action>echo "$brightness_sct" > '$TMPDIR'/brightness-sct</action>
			<action>func_set_sct</action>
		</hscale>
	</vbox>
	</hbox>

	'$BACKLIGHT_SCT'

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

	<hbox space-expand="false" space-fill="true" homogeneous="true">
		<text space-expand="true" space-fill="true"><label>" "</label></text>
		'$TOGGLE_GUI'
		'$BRIGHTNESS_CTL'
		'$XORG_WIZARD'
		'$LXRANDR'
		'$GLXGEARS'
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Save current tint and brightness to custom preset')"' ">
			<input file>'$ICONDIR'/save.svg</input><height>28</height><width>28</width>
			<action>echo "$ctemp_sct" > '$PREFDIR'/cct-sct</action>
			<action>echo "$brightness_sct" > '$PREFDIR'/cbr-sct</action>
		</button>
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen status')"' ">
			<input file>'$ICONDIR'/dialog-info.svg</input><height>28</height><width>28</width>
			<action>func_info_sct &</action>
		</button>
		<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 tooltip-text=" '"$(gettext 'Rescan screens and reset default configuration')"' " use-underline="true">
			<label>'$(gettext '_Reset')'</label>
			<action>func_reset_default &</action>
			<action>exit:quit_now</action>
		</button>
		<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>
'
else
	NB_LABELS='<notebook labels=" '"$(gettext 'Xrandr') | $(gettext 'Screen Saver')"' ">'
	TOGGLE_GUI=''
	SCT_TAB=''
	[ -f $HOME/Startup/display-sct ] && rm -f $HOME/Startup/display-sct
fi

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

# help text
gettext "<b><span size='"'x-large'"'>Display Control</span></b>

<b>Purpose</b>
The Display Control interface calls either xrandr or the optional sct utility to adjust the screen color temperature and brightness for optimal display illumination. A toggle button is available to switch between the xrandr and sct interfaces, each providing unique advantages for improved screen viewing.

Importantly, xrandr and sct do not change the actual backlight intensity, rather the observed screen appearance represents software modulation of available light.

<b>Tint and Brightness</b>
This application is particularly useful at night when bright screen illumination may be visually stressful and excessive blue light may be disruptive to sleep-wake cycles. In this scenario, lower color temperatures (below 4500K) and lesser brightness levels will provide viewing comfort. On the other hand, higher color temperatures (above 6500K) and greater brightness values may be helpful for gaming or for improved screen viewing when sunlight intensity is high.

The color temperature and brightness values are accessible in hscale slider widgets which are easily navigated. If you prefer single-click profiles to quickly adjust the screen tint and brightness please go to the Presets Option where you will find multiple preconfigured viewing profiles plus a custom option for your favorite profile.

<b>Backlight</b>
Alternatively, the backlight option may be useful to modulate backlight intensity of the primary screen through /sys/class/backlight, but this function is not supported by all hardware interfaces. For those with compatible hardware, the actual backlight intensity changes but the color temperature (screen tint) is not altered.

Display Control will hide this option if /sys/class/backlight is missing or empty. The default script (brightness-set) which mediates backlight brightness control is courtesy of James Budiono of Fatdog Linux. If you require an alternate mediator of backlight control, the optional and versatile brightnessctl utility will integrate seamlessly with Display Control." > /tmp/dcontrol_help

# help dialog
help_dialog()
{
export Help='
<window default-height="450" default-width="555" title="'$(gettext 'Help')'" icon-name="dcontrol" window-position="1" resizable="true">
<vbox height-request="450" width-request="555" space-expand="true" space-fill="true">
	<vbox space-expand="true" space-fill="true">
		<vbox scrollable="true" shadow-type="3" space-expand="true" space-fill="true">
			<vbox space-expand="false" space-fill="false">
				<text xpad="20" ypad="15" use-markup="true"><input file>/tmp/dcontrol_help</input></text>
			</vbox>
		</vbox>
	</vbox>
	<hbox space-expand="false" space-fill="false">
		<button width-request="100">
			<label>'$(gettext 'Close')'</label>
		</button>
	</hbox>
</vbox>
</window>'

$GTKDIALOG -p Help
unset Help
}
export -f help_dialog

# help banner
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>
		<text use-markup=\"true\"><label>\"<b>$(gettext 'Set screen tint and brightness')</b>\"</label></text>
		<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=\"true\" homogeneous=\"true\">
			<button>
				<label>\"$(gettext 'Help')\"</label>
				<action>help_dialog &</action>
			</button>
			<button 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                                                          #
#                                                                      #
########################################################################

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

	'$HEADER_1'

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

	<hbox homogeneous="true">
	<hbox>
	<vbox>
		<text tooltip-text=" '"$(gettext 'Display identification')"' "><label>"'"$(gettext 'Screen')"'"</label></text>
		<comboboxtext width-request="'"$cbox1_width"'">
			<variable>display</variable>
			<default>'$display'</default>
			'$display_items'
			<action>echo "$display" > '$PREFDIR'/display</action>
		</comboboxtext>
	</vbox>
	<vbox>
		<text tooltip-text=" '"$(gettext 'Tint and brightness presets')"' "><label>"'"$(gettext 'Presets')"'"</label></text>
		<comboboxtext width-request="'"$cbox2_width"'">
			<variable>preset_xrandr</variable>
			<default>'$preset_xrandr'</default>
			<item>'$(gettext 'gaming')'</item>
			<item>'$(gettext 'cinema')'</item>
			<item>'$(gettext 'daylight')'</item>
			<item>'$(gettext 'reading')'</item>
			<item>'$(gettext 'twilight')'</item>
			<item>'$(gettext 'night-1')'</item>
			<item>'$(gettext 'night-2')'</item>
			<item>'$(gettext 'custom')'</item>
			<action>echo "$preset_xrandr" > '$TMPDIR'/preset-xrandr</action>
			<action>func_preset_xrandr</action>
			<action>refresh:ctemp_xrandr</action>
			<action>refresh:brightness_xrandr</action>
		</comboboxtext>
	</vbox>
	</hbox>
	</hbox>

	<hbox homogeneous="true">
	<vbox>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<text tooltip-text=" '"$(gettext 'Screen color temperature (2500-9500K)')"' "><label>"'"$(gettext 'Tint')"'"</label></text>
		<hscale width-request="'"$scale_width"'" height-request="15" scale-min="2500" scale-max="9500" scale-step="10" value-pos="1" inverted="false">
			<variable>ctemp_xrandr</variable>
			<input file>'$TMPDIR'/ctemp-xrandr</input>
			<action>echo "$ctemp_xrandr" > '$TMPDIR'/ctemp-xrandr</action>
			<action>func_set_xrandr</action>
		</hscale>
	</vbox>
	</hbox>

	<hbox homogeneous="true">
	<vbox>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<pixmap><input file>'$ICONDIR'/blank1.png</input></pixmap>
		<text tooltip-text=" '"$(gettext 'Relative screen illumination (30-130%)')"' "><label>"'"$(gettext 'Brightness')"'"</label></text>
		<hscale width-request="'"$scale_width"'" height-request="15" scale-min="30" scale-max="130" scale-step="1" value-pos="1" inverted="false">
			<variable>brightness_xrandr</variable>
			<input file>'$TMPDIR'/brightness-xrandr</input>
			<action>echo "$brightness_xrandr" > '$TMPDIR'/brightness-xrandr</action>
			<action>func_set_xrandr</action>
		</hscale>
	</vbox>
	</hbox>

	'$BACKLIGHT_XRANDR'

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

	<hbox space-expand="false" space-fill="true" homogeneous="true">
		<text space-expand="true" space-fill="true"><label>" "</label></text>
		'$TOGGLE_GUI'
		'$BRIGHTNESS_CTL'
		'$XORG_WIZARD'
		'$LXRANDR'
		'$GLXGEARS'
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Save current tint and brightness to custom preset')"' ">
			<input file>'$ICONDIR'/save.svg</input><height>28</height><width>28</width>
			<action>echo "$ctemp_xrandr" > '$PREFDIR'/cct-xrandr</action>
			<action>echo "$brightness_xrandr" > '$PREFDIR'/cbr-xrandr</action>
		</button>
		<button space-expand="false" space-fill="false" tooltip-text=" '"$(gettext 'Screen status')"' ">
			<input file>'$ICONDIR'/dialog-info.svg</input><height>28</height><width>28</width>
			<action>func_info_xrandr &</action>
		</button>
		<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 tooltip-text=" '"$(gettext 'Rescan screens and reset default configuration')"' " use-underline="true">
			<label>'$(gettext '_Reset')'</label>
			<action>func_reset_default &</action>
			<action>exit:quit_now</action>
		</button>
		<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>

'$SCT_TAB'

<vbox height-request="'"$gui_height"'">

	'$HEADER_2'

	<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>
		</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>
		</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>
</notebook>
<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/preset-xrandr ] && cp -f $TMPDIR/preset-xrandr $PREFDIR/preset-xrandr
[ -f $TMPDIR/ctemp-xrandr ] && cp -f $TMPDIR/ctemp-xrandr $PREFDIR/ctemp-xrandr
[ -f $TMPDIR/brightness-xrandr ] && cp -f $TMPDIR/brightness-xrandr $PREFDIR/brightness-xrandr

[ -f $TMPDIR/preset-sct ] && cp -f $TMPDIR/preset-sct $PREFDIR/preset-sct
[ -f $TMPDIR/ctemp-sct ] && cp -f $TMPDIR/ctemp-sct $PREFDIR/ctemp-sct
[ -f $TMPDIR/brightness-sct ] && cp -f $TMPDIR/brightness-sct $PREFDIR/brightness-sct

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

exit 0
