#!/bin/bash
# pRecorder -- simple GTK frontend for wf-recorder and ffmpeg
# with basic screen recording options for Wayland compositors
# Copyright 2026 GPL3
# Roger Grider (radky)

# Dependencies: bash gtkdialog xdialog yad wf-recorder ffmpeg 

export TEXTDOMAIN=precorder
export OUTPUT_CHARSET=UTF-8

if [ ! -f /usr/bin/wf-recorder ]; then
	gtkdialog-splash -close never -timeout 5 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Please install wf-recorder !') " &
	exit 0
fi

[ -f /tmp/psnip-run ] && rm -f /tmp/psnip-run

export VERSION=1.2.1

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

APPDIR="/usr/local/precorder"
[ ! -d $APPDIR ] && mkdir -p $APPDIR
PREFDIR="$APPDIR/preferences"
[ ! -d $PREFDIR ] && mkdir -p $PREFDIR
[ ! -d $HOME/Videos ] && mkdir -p $HOME/Videos
[ ! -d $HOME/Videos/Screencasts ] && mkdir -p $HOME/Videos/Screencasts
SAVEDIR=`cat $PREFDIR/savedir 2>/dev/null`
[ ! "$SAVEDIR" ] && SAVEDIR="$HOME/Videos/Screencasts/" && echo "$SAVEDIR" > $PREFDIR/savedir
WORKDIR="/tmp/precorder-tmp"
[ ! -d $WORKDIR ] && mkdir -p $WORKDIR

#pRecording settings
AUDIO=`cat $PREFDIR/audio 2>/dev/null`
[ ! "$AUDIO" ] && AUDIO=true && echo "$AUDIO" > $PREFDIR/audio
AUDIO96=`cat $PREFDIR/audio96 2>/dev/null`
[ ! "$AUDIO96" ] && AUDIO96=false && echo "$AUDIO96" > $PREFDIR/audio96
AUDIOSOURCE=`cat $PREFDIR/audiosource 2>/dev/null`
[ ! "$AUDIOSOURCE" ] && AUDIOSOURCE=Default && echo "$AUDIOSOURCE" > $PREFDIR/audiosource
DATE=`cat $PREFDIR/date 2>/dev/null`
[ ! "$DATE" ] && DATE=false && echo "$DATE" > $PREFDIR/date
DELAY=`cat $PREFDIR/delay 2>/dev/null`
[ ! "$DELAY" ] && DELAY=0 && echo "$DELAY" > $PREFDIR/delay
FILEMANAGER=`cat $PREFDIR/filemanager 2>/dev/null`
if [ ! "$FILEMANAGER" ]; then
	for i in defaultfilemanager roxfiler rox spacefm pcmanfm xfe Thunar emelfm2; do [ "`which $i 2>/dev/null`" ] && { FILEMANAGER="$i"; break; } done
	echo "$FILEMANAGER" > $PREFDIR/filemanager
fi
if [ "$FILEMANAGER" = "defaultfilemanager" ]; then
	if [[ $(grep spacefm /usr/local/bin/defaultfilemanager 2>/dev/null) ]]; then
		FILEMANAGER="spacefm"
	fi
fi
[ "$FILEMANAGER" = "spacefm" ] && FILEMANAGER="spacefm -w"
FILENAME=`cat $PREFDIR/filename 2>/dev/null`
[ ! "$FILENAME" ] && FILENAME=Screencast && echo "$FILENAME" > $PREFDIR/filename
FILETYPE=`cat $PREFDIR/filetype 2>/dev/null`
[ ! "$FILETYPE" ] && FILETYPE=MP4 && echo "$FILETYPE" > $PREFDIR/filetype
FRAMES=`cat $PREFDIR/frames 2>/dev/null`
[ ! "$FRAMES" ] && FRAMES=Default && echo "$FRAMES" > $PREFDIR/frames
FULLSCREEN=`cat $PREFDIR/fullscreen 2>/dev/null`
[ ! "$FULLSCREEN" ] && FULLSCREEN=true && echo "$FULLSCREEN" > $PREFDIR/fullscreen
IMAGEVIEWER=`cat $PREFDIR/imageviewer 2>/dev/null`
if [ ! "$IMAGEVIEWER" ]; then
	for i in defaultimageviewer viewnior gpicview fotoxx geeqie gqview gwenview ristretto xnview; do [ "`which $i 2>/dev/null`" ] && { IMAGEVIEWER="$i"; break; } done
	echo "$IMAGEVIEWER" > $PREFDIR/imageviewer
fi
MEDIAPLAYER=`cat $PREFDIR/mediaplayer 2>/dev/null`
if [ ! "$MEDIAPLAYER" ]; then
	for i in defaultmediaplayer smplayer celluloid vlc kaffeine kodi mplayershell mplayer_shell parole totem; do [ "`which $i 2>/dev/null`" ] && { MEDIAPLAYER="$i"; break; } done
	echo "$MEDIAPLAYER" > $PREFDIR/mediaplayer
fi
MEDIADATA="$APPDIR/pmediainfo"
ROI=`cat $PREFDIR/roi 2>/dev/null`
[ ! "$ROI" ] && ROI=false && echo "$ROI" > $PREFDIR/roi
SCALE=`cat $PREFDIR/scale 2>/dev/null`
[ ! "$SCALE" ] && SCALE=false && echo "$SCALE" > $PREFDIR/scale
SCALE_PCT=`cat $PREFDIR/scalepct 2>/dev/null`
[ ! "$SCALE_PCT" ] && SCALE_PCT=75 && echo "$SCALE_PCT" > $PREFDIR/scalepct
TIME=`cat $PREFDIR/time 2>/dev/null`
[ ! "$TIME" ] && TIME=10 && echo "$TIME" > $PREFDIR/time
TIMER=`cat $PREFDIR/timer 2>/dev/null`
[ ! "$TIMER" ] && TIMER=true && echo "$TIMER" > $PREFDIR/timer
TRAYICON=`cat $PREFDIR/trayicon 2>/dev/null`
[ ! "$TRAYICON" ] && TRAYICON=false && echo "$TRAYICON" > $PREFDIR/trayicon

CLOSE=`cat $PREFDIR/close 2>/dev/null`
[ ! "$CLOSE" ] && CLOSE=true && echo "$CLOSE" > $PREFDIR/close
if [ "$CLOSE" = "true" ]; then
	CLOSEACTION="<action type=\"exit\">quit_now</action>"
else
	CLOSEACTION=""
fi

SFWBAR=$(pidof sfwbar 2>/dev/null)

export VERSION APPDIR PREFDIR WORKDIR FILEMANAGER IMAGEVIEWER MEDIAPLAYER MEDIADATA CLOSEACTION SFWBAR

#define title-bar icon
[ ! -f /usr/share/icons/hicolor/48x48/apps/precorder.svg ] && \
ln -sf $APPDIR/icons/precorder.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#define gui-title color
DARK_GTKT_THEME=$(grep 'gtk-theme-name' $HOME/.config/gtk-3.0/settings.ini | grep -Ei 'black|dark|dusk|night')
[ -n "$DARK_GTKT_THEME" ] && COLOR=#84aad9 || COLOR=#3272C0

#define $AUDIOSOURCE for audio source combobox
audiosource=$( echo -e "Default\n$(pactl list sources | grep Name | cut -d ' ' -f2- | sort)" )
AUDIOSOURCE_ITEMS=$(for a in $audiosource ; do echo -n "<item>$a</item> " ; done)

#define $FILETYPE_ITEMS for video format combobox
FILETYPE_ITEMS="<item>AVI</item> <item>FLV</item> <item>GIF</item> <item>MKV</item> <item>MP4</item> <item>WEBM</item> <item>WMV</item>"

#define $FRAME_ITEMS for video frames combobox
FRAME_ITEMS="<item>Default</item> <item>8</item> <item>16</item> <item>24</item> <item>30</item> <item>60</item> <item>120</item>"

########################################################################
#                                                                      #
# FUNCTIONS                                                            #
#                                                                      #
########################################################################

func_recorder_start(){
	DATE_STAMP=`date +%F`
	FILETYPE="`echo ${FILETYPE} | tr [:upper:] [:lower:]`"
	SAVEDIR="`echo "${SAVEDIR}" | sed -e 's%/*$%%' -e 's%$%\/%'`"
	if [ "$DATE" = "true" ]; then #enable dated filename, else use default nomenclature with sequential enumeration
		FILENAME="$(gettext 'Screencast')_$DATE_STAMP-(1)"
		num=1
		while [ -f "$SAVEDIR$FILENAME.$FILETYPE" ]
		do
		FILENAME="$(echo $(gettext 'Screencast')_$DATE_STAMP-"($num)")"
		let num=num+1
		done
	else
		FILENAME=`cat $PREFDIR/filename`
		FILENAME=$(echo "$FILENAME" | sed -e 's% %_%g' -e 's%[/\\"*|<>:?]%_%g')
		[ ! "$FILENAME" ] && FILENAME="$(gettext 'Screencast')"
		TEMPNAME=$(echo "$FILENAME" | rev | cut -f2- -d '-' | rev)
		[ "$TEMPNAME" = "$(gettext 'Screencast')" ] && FILENAME="$TEMPNAME-1" || FILENAME="$TEMPNAME"
		num=1
		while [ -f "$SAVEDIR$FILENAME.$FILETYPE" ]
		do
		FILENAME="$(echo "$TEMPNAME-$num")"
		let num=num+1
		done
	fi
	echo "$FILENAME" > $PREFDIR/filename
	OPT=""
	[ "$AUDIOSOURCE" = "Default" ] && AUDIOSOURCE=""
	[ "$AUDIO" = "true" ] && AUDIO="-a${AUDIOSOURCE}" || AUDIO=""
	[ "$AUDIO96" = "true" ] && SAMPLERATE="-R 96000" || SAMPLERATE="-R 48000"
	[ "$DATE" = "true" ] && DATE="$(date +%Y-%m-%d_%H-%M-%S)" FILENAME="" || DATE=""
	[ "$FILETYPE" = "gif" ] && AUDIO="" AUDIOSOURCE=""
	[ "$FILETYPE" = "webm" ] && OPT="--muxer webm --codec libvpx --audio-codec=libvorbis"
	[ "$FRAMES" = "Default" ] && FRAMES="" || FRAMES="-r $FRAMES" #if not defined, framerate defaults to encoder specifications 
	[ "${SCALE}" = "100" ] && SCALE=""
	[ "${SCALE}" = "true" ] && SCALE_PCT="-vf scale=iw/(100/${SCALE_PCT}):-1,pad=ceil(iw/2)*2:ceil(ih/2)*2" || SCALE_PCT=""
	[ "$TIME" = "0" ] && TIME="86400" #arbitrarily high timeout, so user must stop recording
	if [ "$ROI" = "true" ]; then #record selected region of interest (ROI)
		TIME=$(expr $TIME + 1) #adjust for slight delay in start of recording interval
		if [ "$TIMER" = "true" ]; then
			if [ "$FILETYPE" = "gif" ]; then
				timeout -s SIGINT ${TIME}s wf-recorder -y ${FRAMES} -g "$(slurp -d; func_timer_delay; sleep 0.5)" -f "${WORKDIR}/tmp-precorder.mkv"
				gtkdialog-splash -close never -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing video, please wait...') " &
				XPID=$!
				ffmpeg -y -i "${WORKDIR}/tmp-precorder.mkv" ${SCALE_PCT} -c:v gif "${WORKDIR}/tmp-precorder.gif"
				rm -f "${WORKDIR}/tmp-precorder.mkv"
				kill $XPID
			else
				timeout -s SIGINT ${TIME}s wf-recorder -y ${AUDIO} ${SAMPLERATE} ${FRAMES} ${OPT} -g "$(slurp -d; func_timer_delay; sleep 0.5)" -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}"
			fi
		else
			if [ "$FILETYPE" = "gif" ]; then
				timeout -s SIGINT ${TIME}s wf-recorder -y ${FRAMES} -g "$(slurp -d; sleep $DELAY)" -f "${WORKDIR}/tmp-precorder.mkv"
				gtkdialog-splash -close never -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing video, please wait...') " &
				XPID=$!
				ffmpeg -y -i "${WORKDIR}/tmp-precorder.mkv" ${SCALE_PCT} -c:v gif "${WORKDIR}/tmp-precorder.gif"
				rm -f "${WORKDIR}/tmp-precorder.mkv"
				kill $XPID
			else
				timeout -s SIGINT ${TIME}s wf-recorder -y ${AUDIO} ${SAMPLERATE} ${FRAMES} ${OPT} -g "$(slurp -d; sleep $DELAY)" -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}"
			fi
		fi
		echo "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}" > $PREFDIR/current
	else #record fullscreen
		OUTPUTS="`wlr-randr | grep -m 2 -v '^ ' | cut -f 1 -d ' ' | tr '\n' ' ' | sed 's/ $//'`" #define screen ID
		OUTPUT="${OUTPUTS%% *}"
		[ -z "$OUTPUT" ] && exit
		if [ "$OUTPUT" != "$OUTPUTS" ]; then #when multiple screens detected, use mouse-click to select a screen for fullscreen capture
			OUTPUT="`slurp -p -f %o`"
			[ -z "$OUTPUT" ] && exit
		fi
		TIME=$(expr $TIME + 1) #adjust for slight delay in start of recording interval
		if [ "$TIMER" = "true" ]; then
			func_timer_delay
			sleep .5
		else
			sleep $DELAY
		fi
		if [ "$FILETYPE" = "gif" ]; then
			timeout -s SIGINT ${TIME}s wf-recorder -o "$OUTPUT" -y ${FRAMES} -f "${WORKDIR}/tmp-precorder.mkv"
			gtkdialog-splash -close never -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing video, please wait...') " &
			XPID=$!
			ffmpeg -y -i "${WORKDIR}/tmp-precorder.mkv" ${SCALE_PCT} -c:v gif "${WORKDIR}/tmp-precorder.gif"
			rm -f "${WORKDIR}/tmp-precorder.mkv"
			kill $XPID
		else
			timeout -s SIGINT ${TIME}s wf-recorder -o "$OUTPUT" -y ${AUDIO} ${SAMPLERATE} ${FRAMES} ${OPT} -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}"
		fi
		echo "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}" > $PREFDIR/current
	fi
	if [ "${SCALE}" = "true" -o "$FILETYPE" = "gif" ]; then #apply ffmpeg filters for optimal video quality
		gtkdialog-splash -close never -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing video, please wait...') " &
		XPID=$!
		if [ "$FILETYPE" = "gif" ]; then #optimize gif color palette
			ffmpeg -y -i "${WORKDIR}/tmp-precorder.${FILETYPE}" -filter_complex "[0]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}"
			rm -f "${WORKDIR}/tmp-precorder.${FILETYPE}"
		elif [ "$FILETYPE" = "webm" ]; then #video scaling is lossy, so apply ffmpeg filters to improve output quality
			ffmpeg -y -i "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}" ${SCALE_PCT} -c:v libvpx-vp9 -crf 18 -b:v 0 -c:a libvorbis "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}"
			rm -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}"
		else #ditto
			ffmpeg -y -i "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}" ${SCALE_PCT} -c:v libx264 -crf 18 -preset slow -c:a copy "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}"
			rm -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}"
		fi
		kill $XPID
	else
		mv -f "${WORKDIR}/${FILENAME}${DATE}.${FILETYPE}" "${SAVEDIR}${FILENAME}${DATE}.${FILETYPE}"
	fi
	gtkdialog-splash -close never -timeout 3 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Screen recording completed !') "	
	[ "$CLOSE" = "true" ] && $APPDIR/precorder &
}
export -f func_recorder_start

func_recorder_restart(){
	$APPDIR/precorder &
}
export -f func_recorder_restart

func_recorder_stop(){
	kill -s 15 `pgrep wf-recorder` &
}
export -f func_recorder_stop

func_media_info(){
	RECORDING=`cat $PREFDIR/current 2>/dev/null`
	if [ -f "$RECORDING" ]; then
		$MEDIADATA "${RECORDING}"
	else
		$FILEMANAGER "${SAVEDIR}"
	fi
}
export -f func_media_info

func_media_view(){
	RECORDING=`cat $PREFDIR/current 2>/dev/null`
	if [ -f "$RECORDING" ]; then
		$MEDIAPLAYER "${RECORDING}"
	else
		$FILEMANAGER "${SAVEDIR}"
	fi
}
export -f func_media_view

func_timer_delay(){
 if [ ${DELAY} -gt 0 ] && [ "$TIMER" = "true" ]; then #countdown code by SFR
	echo '<window resizable="false" opacity="0.7" type-hint="6" title="" icon-name="" window-position="3">
		<vbox spacing="0" margin="0">
			<notebook show-border="false" show-tabs="false" tab-base-index="0">
				'$( for ((V=${DELAY}; V>=1; V--)); do
				echo '<text use-markup="true"><label>"<span font='"'Sans 24'"' size='"'xx-large'"' weight='"'bold'"' color='"'green'"'>'${V}'</span>"</label></text>'
				done )'
				<variable>varINDEX</variable>
				<input>echo $(( ($varINDEX+1) % '${DELAY}' ))</input>
			</notebook>
			<timer visible="false" interval="1">
				<action>refresh:varINDEX</action>
			</timer>
			<timer visible="false" interval="'${DELAY}'">
				<action>exit:EXIT</action>
			</timer>
		</vbox>
		<action signal="hide">exit:abort</action>
	</window>' | gtkdialog -s >/dev/null 2>&1
 else
	sleep ${DELAY}
 fi
}
export -f func_timer_delay

func_trayicon_start(){
if [ -n "$SFWBAR" ]; then
	sed -i '/clock.widget/i\  include(\"precorder.widget\")' $HOME/.config/sfwbar/includes.widget
	sleep 0.5
	killall -SIGHUP sfwbar &
else
	echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=precorder
Exec=/usr/local/precorder/trayicon' > $HOME/.config/autostart/precorder.desktop

yad --notification --item-separator=":" --no-middle \
  --text="pRecorder" \
  --image='/usr/local/precorder/icons/precorder.svg' \
  --icon-size 48 \
  --command="" \
  --menu="pRecorder:/usr/local/precorder/precorder:gtk-open|Start Recording:/usr/local/precorder/startrecord:gtk-media-record|Stop Recording:/usr/local/precorder/stoprecord:gtk-stop|————————:|pSnip:/usr/local/precorder/psnip:gtk-open|Take Screenshot:/usr/local/precorder/screenshot:gtk-ok|Quit:quit:gtk-quit"
fi
}
export -f func_trayicon_start

func_trayicon_stop(){
if [ -n "$SFWBAR" ]; then
	sed -i '/precorder/d'  $HOME/.config/sfwbar/includes.widget
	sleep 0.5
	killall -SIGHUP sfwbar &
else
	if [ "`which busybox 2>/dev/null`" ]; then
		precorderPID=$(busybox ps | grep -a 'sh ' | grep 'func_trayicon_start' | awk '{print $1}')
		precorderPID2=$(busybox ps | grep -a -- 'yad --notification' | grep 'precorder' | awk '{print $1}')
	else
		precorderPID=$(ps ax | grep -a 'sh ' | grep 'func_trayicon_start' | awk '{print $1}')
		precorderPID2=$(ps ax | grep -a -- 'yad --notification' | grep 'precorder' | awk '{print $1}')
	fi
	[ "$precorderPID" ] && kill $precorderPID 2>/dev/null
	[ "$precorderPID2" ] && kill $precorderPID2 2>/dev/null

	rm -f $HOME/.config/autostart/precorder.desktop
fi
}
export -f func_trayicon_stop

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

precorder_help() {
export PR_HELP="
<window title=\"pRecorder Help\" icon-name=\"precorder\" window-position=\"3\" resizable=\"false\">
	<vbox margin=\"10\" width-request=\"350\">
	<pixmap><input file>/usr/local/precorder/icons/precorder.svg</input><height>70</height><width>70</width></pixmap>
		<vbox margin=\"5\">
		<text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>pRecorder</span> $VERSION</b>\"</label></text>
		<text use-markup=\"true\"><label>\"<b><span size='"'large'"'>Wayland Screen Recorder</span></b>\"</label></text>
		<text use-markup=\"true\"><label>\"<b>$(gettext 'Frontend for wf-recorder &amp; ffmpeg')</b>\"</label></text>
		<text><label>\"Roger D. Grider (C) 2026 GPL3\"</label></text>
		</vbox>
		<hbox space-expand=\"true\" space-fill=\"true\" homogeneous=\"true\">
			<button space-expand=\"false\" space-fill=\"true\">
				<label>\"$(gettext 'Help')\"</label>
				<action>mdview \/usr\/local\/precorder\/help_precorder.md &</action>
			</button>
			<button space-expand=\"false\" space-fill=\"true\" 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 PR_HELP
unset PR_HELP
}
export -f precorder_help

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

export PR_MAIN="
<window title=\"Wayland Screen Recorder\" icon-name=\"precorder\" resizable=\"true\" window-position=\"3\">
	<vbox>
		<hbox margin=\"2\">
			<text width-request=\"75\" space-expand=\"false\" space-fill=\"false\"><label>\" \"</label></text>
			<text space-expand=\"true\" space-fill=\"true\"><label>\" \"</label></text>
			<text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>p</span><span size='"'x-large'"' color='"$COLOR"'>Recorder</span></b>\"</label></text>
			<text space-expand=\"true\" space-fill=\"true\"><label>\" \"</label></text>
			<hbox width-request=\"75\" space-expand=\"false\" space-fill=\"false\">
				<button relief=\"2\" height-request=\"32\" width-request=\"35\" tooltip-text=\" $(gettext 'Help') \"><input file>$APPDIR/icons/help.svg</input><height>18</height><width>18</width><action>precorder_help &</action></button>
			</hbox>
		</hbox>

		<vbox>
			<hbox margin=\"2\" space-expand=\"true\" space-fill=\"true\">
				<vbox space-expand=\"true\" space-fill=\"true\">
					<entry height-request=\"35\" space-expand=\"true\" space-fill=\"true\" has-focus=\"true\" tooltip-text=\" $(gettext 'Name for recorded media') \" secondary-icon-stock=\"gtk-clear\" secondary-icon-tooltip-text=\" $(gettext 'Clear name') \">
						<variable>FILENAME</variable>
						<input>cat $PREFDIR/filename</input>
						<action>echo "\$FILENAME" > $PREFDIR/filename</action>
						<action signal=\"secondary-icon-release\">clear:FILENAME</action>
						<action signal=\"secondary-icon-release\">echo > $PREFDIR/filename</action>
					</entry>
				</vbox>
			  	<vbox width-request=\"105\" homogeneous=\"true\" space-expand=\"false\" space-fill=\"false\">
					<hbox homogeneous=\"true\" space-expand=\"false\" space-fill=\"false\">
						<checkbox tooltip-text=\" "$(gettext "Assign current date to image file name")" \">
							<label>$(gettext 'Date')</label>
							<variable>DATE</variable>
							<input>cat $PREFDIR/date</input>
							<action>if true echo true > $PREFDIR/date</action>
							<action>if true clear:FILENAME</action>
							<action>if true echo "$(gettext 'Screencast')_$(date +%F)" > $PREFDIR/filename</action>
							<action>if true refresh:FILENAME</action>
							<action>if false echo false > $PREFDIR/date</action>
							<action>if false clear:FILENAME</action>
							<action>if false echo "$(gettext 'Screencast')" > $PREFDIR/filename</action>
							<action>if false refresh:FILENAME</action>
						</checkbox>
					</hbox>
				</vbox>
			</hbox>

			<hbox margin=\"2\" space-expand=\"true\" space-fill=\"true\">
				<vbox space-expand=\"true\" space-fill=\"true\">
					<entry height-request=\"35\" space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'Folder for recorded media') \" fs-action=\"directory\" fs-folder=\""$SAVEDIR"\" fs-title=\"$(gettext 'Screencast Folder')\">
						<input>cat $PREFDIR/savedir</input>
						<variable>SAVEDIR</variable>
						<action>echo \$SAVEDIR | sed -e 's%/*$%%' -e 's%$%\/%' > $PREFDIR/savedir</action>
					</entry>
				</vbox>
				<vbox space-expand=\"false\" space-fill=\"false\">
					<button relief=\"1\" height-request=\"35\" width-request=\"50\" space-expand=\"false\" space-fill=\"false\" tooltip-text=\" $(gettext 'Folder selector') \">
						<input file>$APPDIR/icons/open.svg</input><height>22</height><width>20</width>
						<action>fileselect:SAVEDIR</action>
						<action>echo \$SAVEDIR | sed -e 's%/*$%%' -e 's%$%\/%' > $PREFDIR/savedir</action>
						<action type=\"refresh\">SAVEDIR</action>
					</button>
			  	</vbox>
			  	<vbox space-expand=\"false\" space-fill=\"false\">
					<button relief=\"1\" height-request=\"35\" width-request=\"50\" space-expand=\"false\" space-fill=\"false\" tooltip-text=\" $(gettext 'Manage media with') $FILEMANAGER \">
						<input file>$APPDIR/icons/filemanager.svg</input><height>22</height><width>20</width>
						<action>$FILEMANAGER "$SAVEDIR" &</action>
					</button>
			  	</vbox>	
			</hbox>

			<hbox margin=\"2\" space-expand=\"true\" space-fill=\"true\">
				<vbox>
					<text><label>$(gettext 'Format')</label></text>
					<comboboxtext height-request=\"35\" width-request=\"120\" wrap-width=\"1\" tooltip-text=\" $(gettext 'Video file format') \">
						<variable>FILETYPE</variable>
						<default>"$FILETYPE"</default>
						$FILETYPE_ITEMS
						<action>echo "\$FILETYPE" > $PREFDIR/filetype</action>
					</comboboxtext>
			  	</vbox>
			  	<vbox>
					<text><label>$(gettext 'Frames')</label></text>
					<comboboxtext height-request=\"35\" width-request=\"120\" wrap-width=\"1\" tooltip-text=\" $(gettext 'Video frames per second  (fps)') \">
						<variable>FRAMES</variable>
						<default>"$FRAMES"</default>
						$FRAME_ITEMS
						<action>echo "\$FRAMES" > $PREFDIR/frames</action>
					</comboboxtext>
			  	</vbox>
			  	<vbox>
					<text><label>$(gettext 'Delay')</label></text>
					<spinbutton height-request=\"35\" width-request=\"120\" range-min=\"0\" range-max=\"9999\" range-step=\"1\" range-value=\"'${DELAY}'\" tooltip-text=\" $(gettext 'Recording delay in seconds') \">
						<variable>DELAY</variable>
						<default>"$DELAY"</default>
						<action>echo "\$DELAY" > $PREFDIR/delay</action>
					</spinbutton>
			 	</vbox>
    		  		<vbox>
					<text><label>$(gettext 'Time')</label></text>
					<spinbutton height-request=\"35\" width-request=\"120\" range-min=\"0\" range-max=\"9999\" range-step=\"1\" range-value=\"'${TIME}'\" tooltip-text=\" $(gettext 'Set the total recording time in seconds. 
 (0 value: user must stop the recording)') \">
						<variable>TIME</variable>
						<default>"$TIME"</default>
						<action>echo "\$TIME" > $PREFDIR/time</action>
					</spinbutton>
				</vbox>
			</hbox>
		</vbox>

		<hbox margin=\"2\">
			<vbox space-expand=\"false\" space-fill=\"false\">
				<hbox homogeneous=\"true\">
					<checkbox height-request=\"35\" tooltip-text=\" "$(gettext "Scale video frame size")" \">
						<label>$(gettext 'Scale (%)')</label>
						<variable>SCALE</variable>
						<input>cat $PREFDIR/scale</input>
						<action>if true echo true > $PREFDIR/scale</action>
						<action>if true enable:SCALE_PCT</action>
						<action>if false echo false > $PREFDIR/scale</action>
						<action>if false disable:SCALE_PCT</action>
					</checkbox>
				</hbox>
				<hbox>
					<spinbutton height-request=\"35\" width-request=\"120\" range-min=\"50\" range-max=\"150\" range-step=\"5\" range-value=\"'${SCALE_PCT}'\" tooltip-text=\" $(gettext 'Percent of original frame size (50-150%)') \">
						<variable>SCALE_PCT</variable>
						<default>"$SCALE_PCT"</default>
						<action>echo "\$SCALE_PCT" > $PREFDIR/scalepct</action>
						<sensitive>"${SCALE}"</sensitive>
					</spinbutton>
				</hbox>
			</vbox>
			<vbox space-expand=\"true\" space-fill=\"true\">
				<text height-request=\"35\"><label>$(gettext 'Audio Source')</label></text>
				<comboboxtext height-request=\"35\" wrap-width=\"1\" tooltip-text=\" $(gettext 'Audio input source') \">
					<variable>AUDIOSOURCE</variable>
					<default>"$AUDIOSOURCE"</default>
					$AUDIOSOURCE_ITEMS
					<action>echo "\$AUDIOSOURCE" > $PREFDIR/audiosource</action>
				</comboboxtext>
			</vbox>
		</hbox>

		<pixmap><input file>\"$APPDIR/icons/blank1.png\"</input></pixmap>
		<pixmap><input file>\"$APPDIR/icons/blank1.png\"</input></pixmap>

		<hbox homogeneous=\"true\">
			<hbox>
				<checkbox tooltip-text=\" $(gettext 'Record full-screen video') \">
					<label>$(gettext 'Fullscreen')     </label>
					<variable>FULLSCREEN</variable>
					<input>cat $PREFDIR/fullscreen</input>
					<action>if true echo true > $PREFDIR/fullscreen</action>
					<action>if true echo false > $PREFDIR/roi</action>
					<action>if false echo false > $PREFDIR/fullscreen</action>
					<action>if false echo true > $PREFDIR/roi</action>
					<action>refresh:ROI</action>
				</checkbox>

				<checkbox tooltip-text=\" $(gettext 'Record a rectangular region (ROI). 
 Use the mouse to outline the area 
 after pressing the Record button.') \">
					<label>$(gettext 'Region')     </label>
					<variable>ROI</variable>
					<input>cat $PREFDIR/roi</input>
					<action>if true echo true > $PREFDIR/roi</action>
					<action>if true echo false > $PREFDIR/fullscreen</action>
					<action>if false echo false > $PREFDIR/roi</action>
					<action>if false echo true > $PREFDIR/fullscreen</action>
					<action>refresh:FULLSCREEN</action>
				</checkbox>

				<checkbox tooltip-text=\" $(gettext 'Record audio stream  (GIF audio not supported)') \">
					<label>$(gettext 'Audio')   @ </label>
					<variable>AUDIO</variable>
					<input>cat $PREFDIR/audio</input>
					<action>if true echo true > $PREFDIR/audio</action>
					<action>if false echo false > $PREFDIR/audio</action>
					<action>if false echo false > $PREFDIR/audio96</action>
					<action>refresh:AUDIO96</action>
				</checkbox>

				<checkbox tooltip-text=\" $(gettext 'Enable 96 kHz audio (uncheck for default 48 kHz)') \">
					<label>$(gettext '96 kHz')</label>
					<variable>AUDIO96</variable>
					<input>cat $PREFDIR/audio96</input>
					<action>if true echo true > $PREFDIR/audio96</action>
					<action>if false echo false > $PREFDIR/audio96</action>
				</checkbox>
			</hbox>
		</hbox>

		<pixmap><input file>\"$APPDIR/icons/blank1.png\"</input></pixmap>

		<hbox homogeneous=\"true\">
			<hbox>
				<checkbox tooltip-text=\" $(gettext 'Show countdown timer') \">
					<label>$(gettext 'Show Timer')     </label>
					<variable>TIMER</variable>
					<input>cat $PREFDIR/timer</input>
					<action>if true echo true > $PREFDIR/timer</action>
					<action>if false echo false > $PREFDIR/timer</action>
				</checkbox>

				<checkbox tooltip-text=\" $(gettext 'Show tray applet icon') \">
					<label>$(gettext 'Show Tray Icon')     </label>
					<variable>TRAYICON</variable>
					<input>cat $PREFDIR/trayicon</input>
					<action>if true echo true > $PREFDIR/trayicon</action>
					<action>if true func_trayicon_start &</action>
					<action>if false echo false > $PREFDIR/trayicon</action>
					<action>if false func_trayicon_stop &</action>
				</checkbox>

				<checkbox tooltip-text=\" $(gettext 'Close GUI during screen recording') \">
					<label>$(gettext 'Close GUI')</label>
					<variable>CLOSE</variable>
					<input>cat $PREFDIR/close</input>
					<action>if true echo true > $PREFDIR/close</action>
					<action>if true func_recorder_restart &</action>
					<action>if false echo false > $PREFDIR/close</action>
					<action>if false func_recorder_restart &</action>
					<action type=\"exit\">quit_now</action>
				</checkbox>
			</hbox>
		</hbox>

		<pixmap><input file>\"$APPDIR/icons/blank1.png\"</input></pixmap>
		<pixmap><input file>\"$APPDIR/icons/blank1.png\"</input></pixmap>
		<hseparator space-expand=\"false\" space-fill=\"false\"></hseparator>

		<hbox space-expand=\"false\" space-fill=\"false\">
			<button height-request=\"35\" width-request=\"58\" space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'View media data') \">
				<input file>$APPDIR/icons/info.svg</input><height>20</height><width>20</width>
				<action>func_media_info &</action>
			</button>
			<button height-request=\"35\" width-request=\"58\" space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'Play recorded media') \">
				<input file>$APPDIR/icons/play.svg</input><height>20</height><width>20</width>
				<action>func_media_view &</action>
			</button>
			<button height-request=\"35\" width-request=\"120\" space-expand=\"true\" space-fill=\"true\" use-underline=\"true\" tooltip-text=\" $(gettext 'Start screen recording') \">
				<label>\"$(gettext '_Record') \"</label>
				<input file>$APPDIR/icons/record.svg</input><height>22</height><width>22</width>
				<action>func_recorder_start &</action>
				$CLOSEACTION
			</button>
			<button height-request=\"35\" width-request=\"120\" space-expand=\"true\" space-fill=\"true\" use-underline=\"true\" tooltip-text=\" $(gettext 'Stop screen recording') \">
				<label>\"$(gettext '_Stop') \"</label>
				<input file>$APPDIR/icons/stop.svg</input><height>20</height><width>20</width>
				<action>func_recorder_stop &</action>
			</button>
			<button height-request=\"35\" width-request=\"58\" space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'Capture screenshot') \">
				<input file>$APPDIR/icons/psnip.svg</input><height>20</height><width>20</width>
				<action>$APPDIR/psnip &</action>
				<action>touch /tmp/psnip-run</action>
				<action type=\"exit\">quit_now</action>
			</button>
			<button height-request=\"35\" width-request=\"58\" space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'Close pRecorder') \">
				<input file>$APPDIR/icons/quit.svg</input><height>20</height><width>20</width>
				<action type=\"exit\">quit_now</action>
			</button>
		</hbox>
	</vbox>
</window>"

gtkdialog -p PR_MAIN

unset PR_MAIN

exit 0
