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

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

GuiAppName=QMMP
GuiAppBinary=qmmp
GuiAppRun= A=`ps -ef|grep qmmp|grep -v grep`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi
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);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Make(var,cover_size,by_value,);\
	Make(var,qmmp_mpris,by_value,NOMPRIS);\
	Make(var,qmmp_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get qmmp_layout`;\
	     if [ "x$V" = "x" ]; then echo "3x4"; else echo $V; fi);

(Exit)=\
	Exec($(CfgDir)/Utils/registry.sh $(TmpDir) set qmmp_layout $(qmmp_layout));

(Connect)=\
	Make(mode,qmmp_$(qmmp_layout));

[Mode]=qmmp_general

VOL_DOWN=\
	Exec(qmmp --volume-dec)

TOGGLE_MUTE=\
	Macro(TOGGLE_MUTE_$(qmmp_mpris));

VOL_UP=\
	Exec(qmmp --volume-inc)

RWD=\
	Macro(RWD_$(qmmp_mpris));

PLAY=\
	Macro(PLAY_$(qmmp_mpris));

FF=\
	Macro(FF_$(qmmp_mpris));

PREV=\
	Macro(PREV_$(qmmp_mpris));

NEXT=\
	Macro(NEXT_$(qmmp_mpris));

STOP=\
	Macro(STOP_$(qmmp_mpris));

PAUSE=\
	Macro(PAUSE_$(qmmp_mpris));

APP_QUIT=\
	Macro(QUIT_$(qmmp_mpris));

QMMP_TIMER_START=\
	Macro(QMMP_TITLE);\
	Timer(QMMP_TITLE,$(UpdateTimeout),0);
	
QMMP_TIMER_CANCEL=\
	Timer(QMMP_TITLE,cancel);\
	Set(title,);\
	Set(status,);

%
% MPRIS versions
%

% VolumeGet add \n to value. Use some tricks to strip it
TOGGLE_MUTE_MPRIS=\
	Make(var,qmmp_volume,\
                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);\
	Make(var,qmmp_volume,echo "$(qmmp_volume) an-trick-to-strip-new-line"|tr -d '\n'|cut -f 1 -d ' ');

RWD_MPRIS=\
	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);

PLAY_ONLY_MPRIS=\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause);

PLAY_MPRIS=\
	Macro(PLAY_ONLY_MPRIS);\
	Macro(QMMP_TIMER_START);

FF_MPRIS=\
	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);

PREV_MPRIS=\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous);\
	Macro(QMMP_SETTITLE);

PAUSE_MPRIS=\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause);\
	Macro(QMMP_SETTITLE);

NEXT_MPRIS=\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next);\
	Macro(QMMP_SETTITLE);

STOP_MPRIS=\
	Macro(QMMP_TIMER_CANCEL);\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop);

QUIT_MPRIS=\
	Macro(QMMP_TIMER_CANCEL);\
	Exec(qdbus org.mpris.MediaPlayer2.qmmp /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Quit);
%
% NO-MPRIS versions
%

TOGGLE_MUTE_NOMPRIS=\
	Macro(DEFAULT_TOGGLE_MUTE)

RWD_NOMPRIS=\
	Exec(qmmp --previous > /dev/null);
	
PLAY_NOMPRIS=\
	Exec(qmmp --play-pause > /dev/null);
	
FF_NOMPRIS=\
	Exec(qmmp --next > /dev/null);

PREV_NOMPRIS=\
	Exec(qmmp --previous > /dev/null);

PAUSE_NOMPRIS=\
	Exec(qmmp --pause > /dev/null);

NEXT_NOMPRIS=\
	Exec(qmmp --next > /dev/null);

STOP_NOMPRIS=\
	Exec(qmmp --stop > /dev/null);

QUIT_NOMPRIS=\
	Macro(QMMP_TIMER_CANCEL);\
	Exec(killall qmmp > /dev/null);
%
%
%
QMMP_POS=\
	Make(var,qmmp_pos,qdbus org.mpris.qmmp /Player PositionGet 2> /dev/null);

QMMP_TITLE_GENERAL=\
	Make(var,artist,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);\
	Make(var,title,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);\
	Set(title,$(artist) - $(title));

%	Make(var,album,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);\
%

RUN_IF_NEED=\
	Exec($(CfgDir)/Utils/run-or-switch-to.sh qmmp);

RUN_OR_QUIT=\
	Exec(A=`ps -ef|grep qmmp|grep -v grep|grep -v edit`; \
	     if [ "x$A" = "x" ]; then qmmp& else killall qmmp; fi);\
	Macro(QMMP_TIMER_CANCEL);

Playlist=\
	Make(mode,qmmp_playlist);

Repeat=\
	Make(mode,qmmp_repeat);

Random=\
	Make(mode,qmmp_random);

APP_PLAY_STATUS=\
	Make(var,qmmp_status,qdbus --literal org.mpris.MediaPlayer2.qmmp /Player org.freedesktop.MediaPlayer.GetStatus|tr -d ','|tr -s ' '|cut -f 3 -d ' ');\
	Make(var,app_status,if [ "x$(qmmp_status)" = "x0" ]; then echo playing; else echo paused; fi);

Layout 3x4=\
	Make(mode,qmmp_3x4);

Layout 7x1=\
	Make(mode,qmmp_7x1);

Layout 5x1=\
	Make(mode,qmmp_5x1);

[ModeEnd]

[Mode]=qmmp_3x4 : qmmp_general,default_player

(EnterMode)=\
	Macro(APP_PRE_SETUP);\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(RUN_IF_NEED);\
	Macro(SETUP_QMMP_PLAYER_$(qmmp_mpris));\
	Macro(APP_MENU);\
	Macro(APP_POST_SETUP);

SETUP_QMMP_PLAYER_MPRIS=\
	Macro(SETUP_DEFAULT_PLAYER);

SETUP_QMMP_PLAYER_NOMPRIS=\
	Macro(SETUP_DEFAULT_PLAYER_NO_FF_FWD);

* *=Macro(QMMP_HELP_$(qmmp_mpris));

QMMP_HELP_MPRIS=\
	Macro(DEFAULT_HELP);

QMMP_HELP_NOMPRIS=\
        Set(text,replace,Help,1 Vol-\n2 Mute\n3 Vol+\n5 Play\n7 Prew\n8 Stop\n9 Next\n* * Help\n* # Run/Quit $(player_name)\n0 Pause\n# Quit $(player_name)\nPlease enable MPRIS plugin to support advanced features)
	
% PowerMgmt by Utils/aliases-server.cfg
APP_MENU=\
	Macro(APP_MENU_$(qmmp_mpris));

APP_MENU_NOMPRIS=\
	Set(menu,replace,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_MENU_MPRIS=\
	Set(menu,replace,Playlist,Repeat,Random,Layout 5x1,Layout 7x1,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,qmmp_layout,by_value,3x4);\
	Make(var,qmmp_volume,by_value,-1);\
	Timer(QMMP_CHECK_MPRIS,5,6);\
	Macro(QMMP_CHECK_MPRIS);

% Run check every 5 seconds for 6 times
QMMP_CHECK_MPRIS=\
        Make(var,qmmp_use_mpris,Q=`qdbus|grep org.mpris.qmmp`;if [ "x$Q" = "x" ]; then echo QMMP_DONTUSE_MPRIS; else echo QMMP_USE_MPRIS; fi);\
	Macro($(qmmp_use_mpris));

%
% Switch to use MPRIS version
%		
QMMP_USE_MPRIS=\
	Make(var,qmmp_mpris,by_value,MPRIS);\
	Timer(QMMP_CHECK_MPRIS,cancel);\
	Macro(SETUP_QMMP_PLAYER_MPRIS);\
	Macro(APP_MENU_MPRIS);

QMMP_DONTUSE_MPRIS=\
	Make(none);

QMMP_TITLE=\
	Macro(QMMP_TITLE_GENERAL);

[ModeEnd]

[Mode]=qmmp_cover

QMMP_TITLE=\
	Macro(QMMP_TITLE_GENERAL);\
	Macro(APP_COVER, if [ "x$(cover_size)" = "x" ]; then echo 1; else echo 0; fi);

APP_COVER=\
	Make(var,media_file,qdbus org.mpris.MediaPlayer2.qmmp /Player org.freedesktop.MediaPlayer.GetMetadata|grep 'location: file'|sed "s/.*\/\/\//\//;s/'/'\\\''/g;s/\//\'\/\'/g;s/$/\'/g;s/^'\//\//");\
	Macro(DEFAULT_COVER);

Mute=\
	Macro(TOGGLE_MUTE_MPRIS);

[ModeEnd]

% Use this layout only if MPRIS plugin is on in QMMP
[Mode]=qmmp_7x1 : qmmp_general,qmmp_cover,default_player_7x1

APP_MENU=\
	Set(menu,replace,Playlist,Mute,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,qmmp_layout,by_value,7x1);

[ModeEnd]

% Use this layout only if MPRIS plugin is on in QMMP
[Mode]=qmmp_5x1 : qmmp_general,qmmp_cover,default_player_5x1

APP_MENU=\
	Set(menu,replace,Playlist,Mute,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,qmmp_layout,by_value,5x1);

APP_TITLE=\
	Macro(QMMP_TITLE);

PLAY=\
	Macro(PLAY_ONLY_MPRIS);

[ModeEnd]

[Mode]=qmmp_playlist

% Found no way to choose track from playlist -- MPRIS support seems to be incompete

(EnterMode)=\
	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($$)=\
	Exec(dbus-send --type=signal --dest=org.mpris.MediaPlayer2.qmmp /TrackList org.freedesktop.MediaPlayer.TrackListChange int32:$(Index));\
	Macro(APP_MENU);

Back($$)=\
	Set(list,close);\
	Make(mode,qmmp_$(qmmp_layout));

[ModeEnd]

[Mode]=qmmp_repeat

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

Back($$)=\
	Set(list,close);\
	Make(mode,qmmp_$(qmmp_layout));
	
Push($$)=\
	Macro(Choose($$));
	
Choose($$)=\
	Exec(qdbus org.mpris.qmmp /TrackList SetLoop `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Set(list,close);\
	Make(mode,qmmp_$(qmmp_layout));

[ModeEnd]

[Mode]=qmmp_random

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

Back($$)=\
	Set(list,close);\
	Make(mode,qmmp_$(qmmp_layout));
	
Push($$)=\
	Macro(Choose($$));
	
Choose($$)=\
	Exec(qdbus org.mpris.qmmp /TrackList SetRandom `echo $(Param)|sed 's/Yes/true/;s/No/false/'`);\
	Set(list,close);\
	Make(mode,qmmp_$(qmmp_layout));

[ModeEnd]

%

%qdbus --literal org.mpris.MediaPlayer2.qmmp /Player  org.freedesktop.MediaPlayer.GetStatus
%dbus-send --type=signal --dest=org.mpris.MediaPlayer2.qmmp /TrackList org.freedesktop.MediaPlayer.TrackListChange int32:1

[End]
