# FVWM-Crystal: Fullscreen
# Written by: Maciej Delmanowski <harnir@linux.net.pl>
# Modifications: Thomas Adam <thomas@edulinux.homeunix.org>
# Bug fixes, disappear and restart functions: Dominique Michel 2008-2013
# <dominique_libre@users.sourceforge.net>
#
# Fullscreen overview:
# After using this function, current window will lose it's border and title
# and will be maximized to the full screen (wow). After using Fullscreen
# function on a "full-screened" window it will return to it's previous size and
# position and border/title will be brought back.
#
# How to use:
# Bind "Fullscreen" command somewhere, example:
#
# 	Key KP_Multiply		A $[Mod1] Fullscreen
#
# Disappear overview
# When using this function, Fvwm-Crystal will cycle through all the fullscreened
# windows and the workbench on the current page.
# To use it, bind "Disappear" command somewhere.
#
# 	Key KP_Multiply		A $[Mod2] Disappear
#
# The other window size functions are into Window-Buttons.
#
# 'Fullscreen-Start' and 'Fullscreen-Stop' functions shouldn't be called
# directly, use 'Fullscreen' function instead.
# 'Disappear-On' and 'Disappear-Off' functions shouldn't be called
# directly, use 'Disappear' function instead.
#
# Know bugs, limitations:
# - after a recipe change, if the new desktop geometry is not the same, some
#   window(s) can be lost. Is it needed to add a logic fot that?
# - some terminals does not scale corectly in full-screen. As example, Terminator
#   scale correctly, Gnome Terminal, ATerm, Xterm and MRxwt does not, URxwt does not scale
#   correctly the first time it is put in full screen, but it does scale OK afterward.
#   If you know how to fix this, please send me a mail:
#   <dominique.michel@users.sourceforge.net>

InfoStoreAdd TmpDirectory "/tmp"

# Wrapper function {{{1
# State 19 = FS (fullscreen), State 20 = visible
DestroyFunc Fullscreen
AddToFunc Fullscreen
+ I ThisWindow (State 19, !FvwmButtons) Fullscreen-Stop
+ I TestRc (NoMatch) ThisWindow (!State 19, !FvwmButtons) Fullscreen-Start

# fullscreen {{{2
DestroyFunc Fullscreen-Start
AddToFunc Fullscreen-Start
+ I WindowStyle !Title, !Borders, !Handles, Iconifiable, ResizeHintOverride
+ I UpdateStyles
+ I WindowStyle State 19
+ I WindowStyle State 20
+ I Maximize ewmhiwa True 100 100
+ I WindowStyle !Maximizable, FixedSize, FixedPosition
+ I UpdateStyles
# We need 1 temporary file for restoring the window state after restart
+ I Exec exec touch $[infostore.TmpDirectory]/fullscreen.19.$[w.id].$[page.nx].$[page.ny].tmp

# quit fullscreen {{{2
DestroyFunc Fullscreen-Stop
AddToFunc Fullscreen-Stop
+ I WindowStyle Title, Borders, Handles, HandleWidth $[infostore.handle_width], Iconifiable, !ResizeHintOverride
+ I WindowStyle Maximizable, !FixedSize, !FixedPosition
+ I WindowStyle !State 19
+ I WindowStyle !State 20
+ I Test (f $[infostore.TmpDirectory]/fullscreen.19.$[w.id].$[page.nx].$[page.ny].tmp) Exec exec rm -f $[infostore.TmpDirectory]/fullscreen.19.$[w.id].$[page.nx].$[page.ny].tmp
+ I UpdateStyles
+ I NS-Default

# Wrapper function {{{1
DestroyFunc Disappear
AddToFunc Disappear
# if FS; then if !State20; then show; else hide all; fi
+ I PipeRead 'if [[ "$(ls $[infostore.TmpDirectory]/fullscreen.19.0x???????.$[page.nx].$[page.ny].tmp 2>/dev/null)" != "" ]]; then \
	for i in $(ls $[infostore.TmpDirectory]/fullscreen.19.0x???????.$[page.nx].$[page.ny].tmp 2>/dev/null); \
	do file=`echo "${i}"|sed -e \'s:fullscreen.19:fullscreen.20:\'`; \
	    if [ -e "${file}" ]; then echo "WindowId $(echo $file | cut -d . -f 3) Disappear-Off"; exit; \
	    fi; \
	done; \
	for i in $(ls $[infostore.TmpDirectory]/fullscreen.19.0x???????.$[page.nx].$[page.ny].tmp 2>/dev/null); \
	do  echo "WindowId $(echo $i | cut -d . -f 3) Disappear-On"; done; fi'

# disappear {{{2
# file tmp_dir/fuulscreen.20.* = hidden
DestroyFunc Disappear-On
AddToFunc Disappear-On
# Those 2 lines are needed if we want to cycle onto the windows original pages
+ I WarpToWindow $[pointer.x]p $[pointer.y]p
+ I Exec exec touch $[infostore.TmpDirectory]/fullscreen.20.$[w.id].$[page.nx].$[page.ny].tmp
+ I WindowStyle !State 20
+ I WindowStyle NoIcon
+ I UpdateStyles
+ I Iconify true

# appear {{{2
DestroyFunc Disappear-Off
AddToFunc Disappear-Off
+ I Iconify false
+ I WindowStyle State 20
+ I WindowStyle Icon
+ I UpdateStyles
+ I Test (f $[infostore.TmpDirectory]/fullscreen.20.$[w.id].$[page.nx].$[page.ny].tmp) Exec exec rm -f $[infostore.TmpDirectory]/fullscreen.20.$[w.id].$[page.nx].$[page.ny].tmp

# Restore Fullscreen after restart {{{1
# Called by StartFunction
DestroyFunc Fullscreen-Recover
AddToFunc Fullscreen-Recover
+ I WindowStyle !Title, !Borders, !Handles, Iconifiable, ResizeHintOverride
+ I UpdateStyles
+ I WindowStyle State 19
+ I WindowStyle State 20
+ I Maximize ewmhiwa True 100 100
+ I WindowStyle !Maximizable, FixedSize
+ I UpdateStyles

DestroyFunc Disappear-Recover
AddToFunc Disappear-Recover
+ I WindowStyle !State 20
+ I WindowStyle NoIcon
+ I UpdateStyles
+ I Iconify true

DestroyFunc Disappear-Test
AddToFunc Disappear-Test
+ I Test (F $[infostore.TmpDirectory]/fullscreen.20.$[w.id].$[page.nx].$[page.ny].tmp) WindowId $[w.id] Disappear-Recover

# wrapper {{{2
DestroyFunc RecoverFullscreen
AddToFunc RecoverFullscreen
PipeRead 'for i in $[infostore.TmpDirectory]/fullscreen.19.*; do wid=`basename "$i" | awk --field-separator .  \'{print $$3}\'`; echo "+ I WindowId ${wid} ThisWindow Fullscreen-Recover"; done'
PipeRead 'for i in $[infostore.TmpDirectory]/fullscreen.20.*; do wid=`basename "$i" | awk --field-separator .  \'{print $$3}\'`; echo "+ I WindowId ${wid} ThisWindow Disappear-Recover"; done'
+ I All Test (F $[infostore.TmpDirectory]/fullscreen.20.$[w.id].$[page.ny].$[page.ny].tmp) WindowId $[w.id] Disappear-Recover

AddToFunc StartFunction I Schedule 500 RecoverFullscreen

AddToFunc ExitFunction I Test (!ToRestart) Exec rm -f $[infostore.TmpDirectory]/fullscreen.19.*
AddToFunc ExitFunction I Test (!ToRestart) Exec rm -f $[infostore.TmpDirectory]/fullscreen.20.*

# vim:ft=fvwm
