%
% anyremote configuration file for qmmp/KDE4 management. (Server-mode)
%

% STATUS nonstable
% XTEST no
% SOUND app/amixer
% ENV no
% ALL_IN_ONE 
% TODO Wait MPRIS v2.0 to support playlist operations (v1 do not allow to select track in playlist)

GuiAppName=QMMP
GuiAppBinary=qmmp
GuiAppRun=echo 'A=`ps -ef|grep qmmp|grep -v grep`; if [ "x$A" == "x" ]; then echo NOK; else echo OK; fi'|bash -f -s
GuiAppIcon=qmmp.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=QMMP is an audio-player, written with help of Qt library. The user interface is similar to winamp or xmms. It is suggested to activate MPRIS module for more comprehensive control.

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(SETUP_DEFAULT_MIXER);
	Make(var,player_name,by_value,QMMP);

(Connect)=Make(mode,qmmp);

[Mode]=qmmp

(EnterMode)=\
	Exec(A=`ps -ef|grep qmmp|grep -v grep|grep -v edit|grep -v anyremote`; if [ "x$A" = "x" ]; then qmmp& fi);\
	Set(layout,default);\
	Set(icons,QMMP,1,vol_down,2,mute,3,vol_up,4,prev,5,play,6,next,7,prev,8,pause,9,next,*,question,0,stop,#,no);\
	Macro(APP_MENU);\
	Macro(USEMPRIS,sleep 3;Q=`qdbus|grep org.mpris.qmmp`;if [ "x$Q" = "x" ]; then echo 1; else echo 0; fi);

APP_MENU=Set(menu,replace,);Macro(ALLIN1MENU)

USEMPRIS=Make(mode,qmmp_mpris);

Cancel=Exec(killall qmmp);Exit

Back=Set(text,close);\
	Macro(APP_MENU);

1=Exec(qmmp --volume-dec > /dev/null)
2=Macro(DEFAULT_TOGGLE_MUTE)
3=Exec(qmmp --volume-inc > /dev/null)

4=Exec(qmmp --previous > /dev/null)
5=Exec(qmmp --play-pause > /dev/null)
6=Exec(qmmp --next > /dev/null)

7=Exec(qmmp --previous > /dev/null)
8=Exec(qmmp --pause > /dev/null)
9=Exec(qmmp --next > /dev/null)

* *=Set(text,replace,Help,1 - Vol-\n3 - Vol+\n4 - Previous\n5 - Play\n6 - Next\n7 - Previous\n8 - Pause\n9 - Next\n* * - Help\n0 - Stop\n# - Quit\n* # - Run/Quit);
* #=Exec(echo 'A=`ps -ef|grep qmmp|grep -v grep`; if [ "x$A" == "x" ]; then qmmp& else killall qmmp; fi'|bash -f -s);\
0=Exec(qmmp --stop)
#=Exec(killall qmmp > /dev/null)

[ModeEnd]

[Mode]=qmmp_mpris

(EnterMode)=\
	Make(var,qmmp_volume,by_value,-1);\
	Set(icons,QMMP,2,mute,4,rewind,6,forward);\
	Macro(APP_MENU);

Cancel=Exec(killall qmmp);Exit

APP_MENU=\
	Set(menu,replace,Playlist,Repeat,Random);\
	Macro(ALLIN1MENU);
	
QMMP_DELTIMER=\
	Timer(QMMP_SETTITLE,cancel);
QMMP_SETTITLE=\
	Make(var,artist,echo 'TN=`qdbus org.mpris.qmmp /TrackList GetCurrentTrack  2> /dev/null`; if [ "x$TN" != "x" ]; then qdbus org.mpris.qmmp /TrackList GetMetadata $TN|grep artist|cut -f 2 -d ":"; fi'|bash -f -s);\
	Make(var,title,echo 'TN=`qdbus org.mpris.qmmp /TrackList GetCurrentTrack  2> /dev/null`; if [ "x$TN" != "x" ]; then qdbus org.mpris.qmmp /TrackList GetMetadata $TN|grep title|cut -f 2 -d ":"; fi'|bash -f -s);\
	Set(title,$(artist) - $(title));

%	Make(var,album,echo 'TN=`qdbus org.mpris.qmmp /TrackList GetCurrentTrack`; if [ "x$P" != "x" ]; then qdbus org.mpris.qmmp /TrackList GetMetadata $TN|grep album|cut -f 2 -d ":"; fi'|bash -f -s);\
%

QMMP_POS=Make(var,qmmp_pos,qdbus org.mpris.qmmp /Player PositionGet 2> /dev/null);

Back=Set(text,close);\
	Macro(APP_MENU);

Back($$)=Set(list,close);\
	Macro(APP_MENU);


1=Exec(qmmp --volume-dec)

% VolumeGet add \n to value. Use some tricks to strip it
2=Make(var,qmmp_volume,\
                echo 'if [ "x$(qmmp_volume)" == "x-1" ] || [ "x$(qmmp_volume)" == "x" ]; \
		   then \
		        qdbus org.mpris.qmmp /Player VolumeGet;qdbus org.mpris.qmmp /Player VolumeSet 0; \
		   else \
		        qdbus org.mpris.qmmp /Player VolumeSet $(qmmp_volume); echo "-1"; fi'|bash -f -s);\
	Make(var,qmmp_volume,echo "$(qmmp_volume) an-trick-to-strip-new-line"|tr -d '\n'|cut -f 1 -d ' ');
			
3=Exec(qmmp --volume-inc)

4=Macro(QMMP_POS);\
        Make(var,qmmp_pos,echo "$(qmmp_pos)-10000"|bc);\
        Exec(qdbus org.mpris.qmmp /Player PositionSet $(qmmp_pos)  2> /dev/null);\
	Macro(QMMP_SETTITLE);
5=Exec(qmmp --play-pause > /dev/null);\
	Macro(QMMP_SETTITLE);\
	Timer(QMMP_SETTITLE,5,0);
6=Macro(QMMP_POS);\
        Make(var,qmmp_pos,echo "$(qmmp_pos)+10000"|bc);\
        Exec(qdbus org.mpris.qmmp /Player PositionSet $(qmmp_pos)  2> /dev/null);\
	Macro(QMMP_SETTITLE);

7=Exec(qmmp --previous > /dev/null);\
	Macro(QMMP_SETTITLE);
8=Exec(qmmp --pause > /dev/null);\
	Macro(QMMP_SETTITLE);
9=Exec(qmmp --next > /dev/null);\
	Macro(QMMP_SETTITLE);

* *=Set(text,replace,Help,1 - Vol-\n3 - Vol+\n4 - Rewind\n5 - Play\n6 - FF\n7 - Previous\n8 - Pause\n9 - Next\n* * - Help\n0 - Stop\n# - Quit\n* # - Run/Quit);
* #=Exec(echo 'A=`ps -ef|grep qmmp|grep -v grep`; if [ "x$A" == "x" ]; then qmmp& else killall qmmp; fi'|bash -f -s);\
0=Exec(qmmp --stop > /dev/null)
#=Exec(killall qmmp)


Playlist=ExecAndSet(list,replace,Playlist,$(CfgDir)/Utils/getPlayList.sh org.mpris.qmmp);\
  	ExecAndSet(list,select,expr 1 + `qdbus org.mpris.qmmp  /TrackList GetCurrentTrack`);\
	Set(menu,replace,Choose);
	
Push($$)=\
	Macro(Choose($$));
	
Choose($$)=Set(list,close);\
	Macro(APP_MENU);

Repeat=Set(list,replace,Repeat,Yes,No);

Random=Set(list,replace,Random,Yes,No);

Repeat($$)=Set(list,close);\
	Macro(APP_MENU);\
	Exec(qdbus org.mpris.qmmp /TrackList SetLoop `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);

Random($$)=Set(list,close);\
	Macro(APP_MENU);\
	Exec(qdbus org.mpris.qmmp /TrackList SetRandom `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);

[ModeEnd]

[End]
