# 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:
# - some terminals and gvim does not scale properly in full-screen. This is because
#   they use characters instead of pixels to adjust their width.
#   Sometime, it work well the second time you put the window in full screen.
#   It also depend on the terminal's font and the screnn's width.
# - tell me how I can get the State attributes in the FS function to work
#   after a Restart, or provide a patch. that in order to get ride of
#   the temporary files.

InfoStoreAdd TmpDirectory "/tmp"

# Wrapper function {{{1
# State 19 = FS (fullscreen), State 20 = visible
DestroyFunc Fullscreen
AddToFunc Fullscreen
+ I ThisWindow (State 19, !FvwmButtons, !FvwmPager, !FvwmMiniConsoleNeedsUniqueName, !QuakeConsoleNeedsUniqueName, !FvwmIconMan, !MPlayer) Fullscreen-Stop
+ I TestRc (NoMatch) ThisWindow (!State 19, !FvwmButtons, !FvwmPager, !FvwmMiniConsoleNeedsUniqueName, !QuakeConsoleNeedsUniqueName, !FvwmIconMan, !MPlayer) 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 ThisWindow MoveToPage $[page.nx] 0
+ 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} Fullscreen-Recover"; done'
PipeRead "for i in $[infostore.TmpDirectory]/fullscreen.20.*; do wid=`basename $i | awk --field-separator .  '{print $$3}'`; echo \"+ I WindowId ${wid} Disappear-Recover\"; done"

AddToFunc StartFunction I Schedule 1000 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.*
AddToFunc ExitFunction I Test (Quit) Exec rm -f $[infostore.TmpDirectory]/fullscreen.*

# Be sure the temporary files are destroyed when a window is closed
DestroyFunc Window-Fullscreen-Destroy
AddToFunc Window-Fullscreen-Destroy
+ i PipeRead 'for file in /tmp/fullscreen.??.$[w.id].?.?.tmp; do rm $file 2>/dev/null; done'

DestroyModuleConfig FvwmEvent-Window-Fullscreen-Destroy: *
*FvwmEvent-Window-FullScreen-Destroy: destroy_window Window-Fullscreen-Destroy
Module FvwmEvent FvwmEvent-Window-Fullscreen-Destroy

# Rearrange in fullscreen
DestroyFunc Fullscreen-Rearrange
AddToFunc Fullscreen-Rearrange
+ I All (CurrentPage, !Iconic, !FvwmButtons, !FvwmPager, !FvwmConsoleNeedsUniqueName, !QuakeConsoleNeedsUniqueName, !FvwmIconMan, !MPlayer) Fullscreen 

# Start in full screen at application statup
DestroyFunc Window-AutoFS
AddToFunc Window-AutoFS
PipeRead '$[FVWM_SYSTEMDIR]/scripts/FSApps'

DestroyModuleConfig FvwmEvent-Window-Fullscreen-Auto: *
*FvwmEvent-Window-FullScreen-Auto: add_window Window-AutoFS
Module FvwmEvent FvwmEvent-Window-Fullscreen-Auto

# vim:ft=fvwm
