%
% anyremote configuration file for management of MPRIS-compatible players. (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=MPRIS
GuiAppBinary=true
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=Manage appliucation through MPRIS (Media Player Remote Interfacing Specification)
[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(SETUP_DEFAULT_MIXER);\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(TEST_QDBUS);\
	Make(var,mpris_volume,by_value,-1);\
	Make(var,mpris_app,by_value,---);

(Connect)=\
	Make(mode,mpris);

[Mode]=mpris

(EnterMode)=\
	Set(layout,default);\
	Set(status,$(mpris_app));\
	Set(icons,MPRIS,1,vol_down,2,mute,3,vol_up,4,rewind,5,play,6,forward,7,prev,8,pause,9,next,*,question,0,stop,#,no);\
	Macro(MPRIS_MENU);

Cancel=\
    Make(exit);

MPRIS_MENU=\
	Set(menu,replace,Browse MPRIS,Run App,Repeat,Random,PowerMgmt);\
	Macro(ALLIN1MENU);
	
MPRIS_DELTIMER=\
	Timer(MPRIS_SETTITLE,cancel);
	
MPRIS_SETTITLE=\
	Make(var,artist,TN=`qdbus org.mpris.$(mpris_app) /TrackList GetCurrentTrack 2> /dev/null`; \
	     if [ "x$TN" != "x" ]; then qdbus org.mpris.$(mpris_app) /TrackList GetMetadata $TN 2> /dev/null|grep artist|cut -f 2 -d ":"; fi);\
	Make(var,title, TN=`qdbus org.mpris.$(mpris_app) /TrackList GetCurrentTrack 2> /dev/null`; \
	     if [ "x$TN" != "x" ]; then qdbus org.mpris.$(mpris_app) /TrackList GetMetadata $TN 2> /dev/null|grep title |cut -f 2 -d ":"; fi);\
	Make(var,album, TN=`qdbus org.mpris.$(mpris_app) /TrackList GetCurrentTrack 2> /dev/null`; \
	     if [ "x$TN" != "x" ]; then qdbus org.mpris.$(mpris_app) /TrackList GetMetadata $TN 2> /dev/null|grep album |cut -f 2 -d ":"; fi);\
	Set(title,$(album) - $(title));\
	Set(status,$(artist));

MPRIS_POS=\
	Make(var,mpris_pos,qdbus org.mpris.$(mpris_app) /Player PositionGet 2> /dev/null);

MPRIS_GETVOL=\
	Make(var,mpris_vol,qdbus org.mpris.$(mpris_app) /Player VolumeGet|tr -d '\n' 2> /dev/null);

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

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


1=Macro(MPRIS_GETVOL);\
	Make(var,mpris_vol,echo "$(mpris_vol)-5"|bc);\
	Exec(qdbus org.mpris.$(mpris_app) /Player VolumeSet $(mpris_vol));

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

3=Macro(MPRIS_GETVOL);\
	Make(var,mpris_vol,echo "$(mpris_vol)+5"|bc);\
	Exec(qdbus org.mpris.$(mpris_app) /Player VolumeSet $(mpris_vol) 2> /dev/null);

4=Macro(MPRIS_POS);\
        Make(var,mpris_pos,echo "$(mpris_pos)-10000"|bc);\
        Exec(qdbus org.mpris.$(mpris_app) /Player PositionSet $(mpris_pos)  2> /dev/null);\
	Macro(MPRIS_SETTITLE);

5=Exec(qdbus org.mpris.$(mpris_app) /Player Play 2> /dev/null);\
	Macro(MPRIS_SETTITLE);\
	Timer(MPRIS_SETTITLE,$(UpdateTimeout),0);

6=Macro(MPRIS_POS);\
        Make(var,mpris_pos,echo "$(mpris_pos)+10000"|bc);\
        Exec(qdbus org.mpris.$(mpris_app) /Player PositionSet $(mpris_pos)  2> /dev/null);\
	Macro(MPRIS_SETTITLE);

7=Exec(qdbus org.mpris.$(mpris_app) /Player Prev 2> /dev/null);\
	Macro(MPRIS_SETTITLE);
8=Exec(qdbus org.mpris.$(mpris_app) /Player Pause 2> /dev/null);\
	Macro(MPRIS_SETTITLE);
9=Exec(qdbus org.mpris.$(mpris_app) /Player Next 2> /dev/null);\
	Macro(MPRIS_SETTITLE);

* *=Set(text,replace,Help,1 - Vol-\n2 - Mute\n3 - Vol+\n4 - Rewind\n5 - Play\n6 - FF\n7 - Previous\n8 - Pause\n9 - Next\n* * - Help\n0 - Stop\n# - Kill app\n);
0=Exec(qdbus org.mpris.$(mpris_app) /Player Stop)
#=Exec(killall $(mpris_app))

% PowerMgmt by Utils/aliases-server.cfg

Browse MPRIS=\
	ExecAndSet(list,replace,MPRIS apps,qdbus|grep mpris|sed 's/org.mpris.//;s/$/,/');\
	Set(menu,replace,Choose,Kill);

Run App=\
	Set(list,replace,MPRIS apps,Amarok,Audacious,Clementine,Dragon,Kaffeine,QMMP,VLC);\
	Set(menu,replace,Start);

Choose($$)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,$(Param));\
	Make(var,mpris_volume,by_value,-1);\
	Set(status,$(mpris_app));\
	Macro(MPRIS_MENU);

Kill($$)=\
	Set(list,close);\
	Exec(qdbus org.mpris.$(Param) / Quit);\
	Macro(MPRIS_MENU);

%
% specify apps in non-parametrized way
%
Start(1,Amarok)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,amarok);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(amarok);\
	Macro(MPRIS_MENU);

Start(2,Audacious)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,audacious);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(audacious);\
	Macro(MPRIS_MENU);
	
Start(3,Clementine)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,clementine);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(clementine);\
	Macro(MPRIS_MENU);

Start(4,Dragon)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,dragon);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(dragon);\
	Macro(MPRIS_MENU);

Start(5,Kaffeine)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,kaffeine);\
	Set(status,$(mpris_app));\
	Exec(kaffeine);\
	Macro(MPRIS_MENU);

Start(6,QMMP)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,qmmp);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(qmmp);\
	Macro(MPRIS_MENU);

Start(7,VLC)=\
	Set(list,close);\
	Make(var,mpris_app,by_value,vlc);\
	Set(status,$(mpris_app));\
	Make(var,mpris_volume,by_value,-1);\
	Exec(vlc --control dbus);\
	Macro(MPRIS_MENU);

Repeat=\
	Make(mode,mpris_repeat);

Random=\
	Make(mode,mpris_random);

[Mode]=mpris_repeat

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

Back($$)=\
	Set(list,close);\
	Make(mode,mpris);
	
Push($$)=\
	Macro(Choose($$));
	
# depends on MPRIS version Random/SetRandom are used	
Choose($$)=\
	Set(list,close);\
	Exec(qdbus org.mpris.$(mpris_app) /TrackList SetLoop `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Exec(qdbus org.mpris.$(mpris_app) /TrackList Loop `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Make(mode,mpris);

[ModeEnd]

[Mode]=mpris_random

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

Push($$)=\
	Macro(Choose($$));
	
# depends on MPRIS version Loop/SetLoop are used	
Choose($$)=\
	Set(list,close);\
	Exec(qdbus org.mpris.$(mpris_app) /TrackList SetRandom `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Exec(qdbus org.mpris.$(mpris_app) /TrackList Random `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Make(mode,mpris);

[ModeEnd]

[End]
