# Support for audio players in Fvwm-Crystal:
# initialisations and some shared functions
# Written by: Dominique Michel <dominique_libre@users.sourceforge.net>
#
# This file must be included at the top of the music player control files.
#	Include components/functions/Music
#
# Some recipes do have a media button, the other do have a menu.
# Recipes with media button: Amiga, Corner, Default, Default with ACPI, Dock
# The bindings of the media buttons are:
#
#       n button        n button   fumction		    comment
#	Button 1:	(Mouse 1) Menu /Player 		# Main player menu
#			(Mouse 2) Music-GUI 		# show/hide the player
#			(Mouse 3) Music-QJackGUI 	# show/hide QJackCtl + the audio mixer
#			(Mouse 4) Mixer-Volume-Up 	# main volume up
#			(Mouse 5) Mixer-Volume-Down 	# main volume down
#	Button 2:	(Mouse 1) Menu /Playlist-button # playlist menu
#			(Mouse 2) Menu /Music/Volume	# player volume menu
#			(Mouse 3) Menu /Mixer		# main mixer menu
#			(Mouse 4) Music-Next		# next media
#			(Mouse 5) Music-Prev		# previous media
#	Button 3:	(Mouse 1) Music-Play		# play
#			(Mouse 2) Music-Pause		# pause
#			(Mouse 3) Menu /Playback	# playback menu
#			(Mouse 4) Music-Speed-Up	# speed up
#			(Mouse 5) Music-Speed-Down	# speed down
#	Button 4:	(Mouse 1) Menu /Speed		# speed control menu
#			(Mouse 4) Music-Seek-+		# seek forward
#			(Mouse 5) Music-Seek--		# seek backward
# NOTE : the actual bindings are fixed into the recipes.
#
# Recipes with media menu: Clean, Clean Vertical, Light, Wing-Commander
# The media menu is: Popup /Music
#
# TODO : - Move the media button here
#	 - Find something to do with mouse 2 and 3 on button 4
#############################################################################

## Amixer and Mplayer are selected as default mixer and player if the user ##
##          didn't changed it. We keep them or the user settings.          ##

 ###########################################################################
# The playlists was previously in different player dependant places.
# They are now in 4 places:
# 	$[FVWM_USERDIR]/Playlists/Audio for the playlists of audio files
# 	$[FVWM_USERDIR]/Playlists/Video for the playlists of movie files
# 	$[HOME]/Playlists/Audio for "by the user managed" audio playlists
# 	$[HOME]/Playlists/Video for "by the user managed" video playlists
#
# 'Recreate the playlist' will manage the playlists in $[FVWM_USERDIR] in a
# very simple way: delete all and recreate all. At the same time, it will
# create the user managed directories if they don't exist.
#
# Note: according to /etc/xdg/user-dirs.defaults, the default medias paths
#	are $HOME/Music and $HOME/Videos and will be translated on a
#	per-path-element basis into the users locale.

# Mixer {{{1
Include components/functions/Mixer

# Variables {{{1
# playlists locations {{{2
InfoStoreAdd AudioPlaylists "$[FVWM_USERDIR]/Playlists/Audio"
InfoStoreAdd VideoPlaylists "$[FVWM_USERDIR]/Playlists/Video"
InfoStoreAdd UserManagedAudioPlaylists "$[HOME]/Playlists/Audio"
InfoStoreAdd UserManagedVideoPlaylists "$[HOME]/Playlists/Video"

# speed control (alsaplayer, mplayer) {{{2
InfoStoreRemove speed

# DVB channel lists (mplayer) {{{2
InfoStoreRemove MplayerChannelList1
InfoStoreRemove MplayerChannelList2
InfoStoreRemove MplayerChannelList3
InfoStoreRemove MplayerChannelList4

# Decimal point in use (alsaplayer) {{{2
InfoStoreRemove dec_point
PipeRead 'if [ $[infostore.MusicPlayer] = alsaplayer ] || [ $[infostore.MusicPlayer] = alsaplayerdaemon ]; \
		then echo "InfoStoreAdd dec_point `locale decimal_point`"; else echo "InfoStoreRemove dec_point"; fi'

# xmms2
InfoStoreRemove fvwm_audio_exec

# Styles for media players windows {{{1
Style Alsaplayer	NoActiveIconOverride, NoIcon, Sticky, WindowListSkip, StaysOnTop 
Style alsaplayer	UseStyle Alsaplayer

Style Audacious		StaysOnTop
Style Audacious		StartIconic, NoActiveIconOverride, NoIcon, !Title, Sticky, WindowListSkip
Style playlist		StartIconic, NoIcon, !Title, Sticky, WindowListSkip
Style equalizer		UseStyle playlist

Style beep-media-player !Title

# Cmus (NoIcon doesn't work -> ToDo: use FvwmIdent)
Style cmus		NoActiveIconOverride, NoIcon, Sticky, WindowListSkip, StaysOnTop
# Mocp (NoIcon doesn't work -> ToDo: use FvwmIdent)
Style mocp		NoActiveIconOverride, NoIcon, Sticky, WindowListSkip, StaysOnTop
Style MOC		UseStyle mocp

Style MPlayer		!Title, !Borders, GnomeIgnoreHints, EWMHIgnoreStackingOrderHints
Style "MPlayer - Video"	Title, Borders, GnomeIgnoreHints, EWMHIgnoreStackingOrderHints
Style XDRadio 		NoIcon, Sticky, WindowListSkip

Style quodlibet		StaysOnTop, NoIcon, Sticky, WindowListSkip

Style gxmms2		StaysOnTop, NoIcon, Sticky, WindowListSkip

Style qjackctl		NoActiveIconOverride, NoIcon, Sticky, WindowListSkip

# Functions {{{1
# menu /Player {{{2
# Recreate the playlists (all)
DestroyFunc Music-RecreatePlaylist
AddToFunc Music-RecreatePlaylist
+ I A $[FVWM_SYSTEMDIR]/scripts/make_all_playlists $[infostore.AudioPlaylists] $[infostore.VideoPlaylists] $[infostore.UserManagedAudioPlaylists] $[infostore.UserManagedVideoPlaylists]

# start the player(s) (alsaplayer, mplayer)
DestroyFunc Music-Start
DestroyMenu /Music/Start

# player quit (alsaplayer, mplayer)
DestroyFunc Music-Kill

# show/hyde the player GUI/menu (alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet)
DestroyFunc Music-GUI

# wrapper for cdcd
DestroyFunc Music-cdcd

# Second button {{{1
# Player controls and main vol 
# mouse 1 Menu /Playlist-button {{{2

# Remove playlist (all)
DestroyFunc Music-RemovePlaylist-generator
AddToFunc Music-RemovePlaylist-generator
+ I DestroyMenu recreate /Music/RemovePlaylist
+ I AddToMenu /Music/RemovePlaylist
+ I PipeRead 'for i in $[infostore.UserManagedAudioPlaylists]/*.{m3u,pls}; do \
  name=$(basename "$i"); \
  sname=${name%%.m3u}; \
  sname=${sname%%.pls}; \
  echo \"AddToMenu /Music/RemovePlaylist \'$sname\' Exec rm -f \'$i\'\"; done'

DestroyMenu /Music/RemovePlaylist
AddToMenu /Music/RemovePlaylist
+ DynamicPopupAction Function Music-RemovePlaylist-generator

# Copy playlist (all)
# Note: Visualy redundant; TODO: A recursive generator like in remove playlist
DestroyFunc FuncFvwmMenuCopyPlaylistDirectory
AddToFunc FuncFvwmMenuCopyPlaylistDirectory
+ I PipeRead 'case \"$0\" in \
	"$[infostore.AudioPlaylists]"*) myexec="Exec cp -r \'%f\' \'$[infostore.UserManagedAudioPlaylists]\'";; \
    esac; \
    test -f \"$0\"/.icontitle.png && mytitle=\"$0\"/.icontitle.png; \
    test -f \"$0\"/.media.png && mypng=\"$0\"/.media.png; \
    fvwm-menu-directory --icon-title "${mytitle:-22x22/categories/directory.png}" \
    --icon-file "${mypng:-22x22/categories/Audio.png}" \
    --dir \"$0\" --exec-title "-" --command-file "${myexec}" \
    --func-name "FuncFvwmMenuCopyPlaylistDirectory"'

DestroyMenu /Music/CopyPlaylist
AddToMenu /Music/CopyPlaylist
#+ '$[gt.Choose playlist to copy]' Nop
+ MissingSubmenuFunction FuncFvwmMenuCopyPlaylistDirectory
+ '%22x22/categories/video_movies_view.png%$[gt.Browse Medias]' Popup $[infostore.AudioPlaylists]

# Clear playlist (alsaplayer, audacious, mocp, mpd, xmms2)
DestroyFunc Music-ClearPlaylist

# Load movie playlist (mplayer)
DestroyFunc FuncFvwmMenuMovieDirectory
DestroyMenu /Music/LoadMovie
# Load Media Playlists (mplayer)
DestroyFunc FuncFvwmMenuMediaDirectory
DestroyMenu /Music/LoadMediaPlaylist
# Load playlist (alsaplayer, audacious, mocp, mplayer)
DestroyFunc FuncFvwmMenuAudioDirectory
# Add playlist (xmms2)
DestroyFunc FuncFvwmMenuPlaylistDirectory
DestroyMenu /Music/AddPlaylist
# Load playlist (mpd, xmms2)
DestroyFunc Music-LoadPlaylist-generator
# Load playlist (alsaplayer, audacious, mocp, mpd, mplayer, xmms2)
DestroyMenu /Music/LoadPlaylist
AddToMenu /Music/LoadPlaylist
+ '$[gt.Choose playlist to load]' Nop
+ MissingSubmenuFunction FuncFvwmMenuAudioDirectory
+ '%22x22/categories/video_movies_view.png%$[gt.Audio playlists]' Popup $[infostore.AudioPlaylists]
+ '%22x22/categories/video_movies_view.png%$[gt.Personal audio playlists]' Popup $[infostore.UserManagedAudioPlaylists]

# Load all the songs (mpd)
DestroyFunc Music-LoadAll

# Play DVD (mplayer)
DestroyFunc Load-DVD

# Show current playlist (xmms2)
DestroyFunc Music-CurrentPlaylist-generator
DestroyMenu /Music/CurrentPlaylist

# Remove song (audacious, mpd, xmms2)
DestroyFunc Music-RemoveSong

# Save playlist dialog (audacious, mpdm, xmms2)
All (Music-SavePlaylist-Dialog) Close
DestroyModuleConfig Music-SavePlaylist-Dialog: *
DestroyFunc Music-SavePlaylist

# Update the database (mpd)
DestroyFunc Music-Update

# alsaplayer
DestroyFunc Music-SongChange
DestroyMenu /Music/SongChange

# alsaplayer, audacious, cdcd, cmus, mocp, mpd, quodlibet, xmms2
DestroyFunc Music-Stop

# alsaplayer, audacious, cdcd, cmus, mocp, mpd, quodlibet
DestroyFunc Music-PlayPause

# cdcd, cmus, mpd, quodlibet
DestroyFunc Music-Forward

# cdcd, cmus, mpd, quodlibet
DestroyFunc Music-Backward

# audacious, cmus, mpd, quodlibet
DestroyFunc Music-ToggleRandom

# alsaplayer, mplayer
DestroyFunc Music-Jump
DestroyMenu /Music/Jump

# mpd
DestroyMenu /Music/Crossfade

# mpd, xmms2
DestroyFunc Music-Shuffle

# (all)
DestroyFunc Playback-generator

# alsaplayer
DestroyFunc Music-VolumeFr
DestroyFunc Music-VolumeEn
# audacious, mplayer
DestroyFunc Music-Volume
# mplayer
DestroyFunc Music-VolumeUp
DestroyFunc Music-VolumeDown

# alsaplayer, mplayer
DestroyFunc Music-Speed

# mouse 4 Button-Seek-+ {{{2
# mouse 5 Button-Seek--
# alsaplayer
DestroyFunc Button-Seek-+
DestroyFunc Button-Seek--

# Music menus {{{1
# main music menu (alsaplayer, audacious, cdcd, mocp) {{{2
DestroyFunc Music-generator

DestroyMenu /Music
AddToMenu /Music
+ DynamicPopupAction Function Music-generator

# Button menus {{{1
# # # # # # # # # #
# Button 1 mouse 1 {{{2
# (alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet)
DestroyFunc Player-generator
# Button menu (all)
DestroyMenu /Player
AddToMenu /Player
+ DynamicPopupAction Function Player-generator

# Button 1 mouse 3 {{{2
DestroyFunc Music-QJackGUI
AddToFunc Music-QJackGUI
+ I All (qjackctl) Iconify toggle
#+ I TestRc (NoMatch) Test (x qjackctl) Exec pidof qjackctl || exec qjackctl
+ I Mixer-GUI

# Button 1 mouse 4 {{{2
# Mixer-Volume-Up in components/Mixer
# Button 1 mouse 5
# Mixer-Volume-Down in components/Mixer

# Button 2 mouse 1 {{{1
# button menu (alsaplayer, audacious, mocp, mpd, mplayer, xmms2)
DestroyFunc Playlist-generator-button
DestroyMenu /Playlist-button
# mplayer
DestroyMenu /Playlist
DestroyMenu /Playlist-bot
DestroyFunc /Playlist-generator
DestroyFunc /Playlist-generator-bot
DestroyFunc Music-WatchDVB

# Button 2 mouse 2 (alsaplayer, audacious, mplayer) {{{2
DestroyMenu /Music/Volume

# Button 2 mouse 3 {{{2
# /Mixer : in components/Mixer

# Button 2 mouse 4 {{{2
# alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet, xmms2
DestroyFunc Music-Next

# Button 2 mouse 5 {{{2
# alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet, xmms2
DestroyFunc Music-Prev

# Button 3 mouse 1 {{{1
# alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet, xmms2
DestroyFunc Music-Play

# Button 3 mouse 2 {{{2
# alsaplayer, audacious, cdcd, cmus, mocp, mpd, mplayer, quodlibet, xmms2
DestroyFunc Music-Pause

# mplayer
DestroyFunc Music-FrameStep

# Button 3 mouse 3 (all) {{{2
DestroyMenu /Playback
AddToMenu /Playback
+ DynamicPopupAction Function Playback-generator

# Button 3 mouse 4 (alsaplayer, mplayer) {{{2
DestroyFunc Music-Speed-Up
# Button 3 mouse 4 (alsaplayer, mplayer) {{{2
DestroyFunc Music-Speed-Down

# Button 4 mouse 1 (alsaplayer, mplayer) {{{1
DestroyMenu /Speed

# Button 4 mouse 4 (alsaplayer, mplayer ) {{{2
# Button 4 mouse 5
DestroyFunc Music-Seek
DestroyMenu /Music/Seek

# Tooltip (alsaplayer, mplayer*) {{{1
DestroyFunc Tittle-Tooltip
DestroyFunc Music-Tooltip

# vim:ft=fvwm
