%
% anyremote configuration file for amarok/KDE4 management. (Server-mode)
%
% Thanks to Carlos Sanchez, Johann Bauer and Dmirty Ashkadov
%

% STATUS stable
% XTEST no
% SOUND app
% ENV no
% DEF_PLAYER   
% ALL_IN_ONE 
% MEDIACENTER audio

GuiAppName=Amarok
GuiAppBinary=amarok
GuiAppVersion=echo 'V=`amarok -v|grep KDE|tr -d " "|cut -f 2 -d ":"|cut -f 1 -d "."`; if [ "x$V" = "x4" ]; then echo OK; else echo NOK; fi'|sh -s
GuiAppRun=echo 'A=`pgrep amarok`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi'|sh -s
GuiAppIcon=amarok.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=Amarok is the music player for Linux and Unix with an intuitive interface. Amarok makes playing the music you love easier than ever before - and looks good doing it. This configuration file is for Amarok/KDE4

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(TEST_QDBUS);\
	Macro(TEST_DBUS_SEND);\
	Macro(TEST_IMAGEMAGIC);\
	Make(var,kde4_app,by_value,amarok);\
	Macro(TEST_KDE4_APP);\
	Make(var,player_name,by_value,Amarok);\
	Make(var,cover_size,by_value,);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Macro(SETUP_DEFAULT_CALL_HANDLER);\
	Make(var,amarok_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get amarok_layout`;\
	     if [ "x$V" = "x" ]; then echo "5x1"; else echo $V; fi);

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

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

[Mode]=amarok
	
(EnterMode)=\
	Make(var,am_dbus,if [ "x$(Dbus)" = "xyes" ]; then echo dbus; else echo nodbus; fi);\
	Make(mode,amarok_$(am_dbus)_$(amarok_layout));

[ModeEnd]

% Common defines
[Mode]=amarok_general

(Disconnect)=\
	Timer(AMAROK_INFO,cancel);

InCall($$)=\
        Macro(DEFAULT_INCOMING_CALL);
	
EndCall($$)=\
        Macro(DEFAULT_END_CALL);

CHECK_PLAY_STATUS=\
	Make(var,amarok_play_status,qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus);\
	Make(var,incoming_call_handler,if  [ "x$(amarok_play_status)" = "xPlaying" ]; then echo PAUSE; else echo ""; fi);\
	Make(var,end_call_handler,if  [ "x$(amarok_play_status)" = "xPlaying" ]; then echo PLAY; else echo ""; fi);

Shuffle=\
	Make(var,shuffle_val,\
	     P=`qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Shuffle`;\
	     if [ "x$P" = "xtrue" ]; then echo 0; else echo 1; fi);\
        Exec(qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Shuffle $(shuffle_val));

Repeat=\
	Make(var,repeat_val,\
	     P=`qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.LoopStatus`;\
	     if [ "x$P" = "xNone" ]; then echo 1; else echo 0; fi);\
        Exec(qdbus org.kde.amarok /Player org.freedesktop.MediaPlayer.Repeat $(repeat_val));

Playlist=\
	Timer(AMAROK_INFO,cancel);\
	Make(mode,amarok_playlist);

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

RUN_OR_QUIT=\
	Exec(P=`pgrep amarok`;\
	     if [ "x$P" = "x" ]; then amarok 2> /dev/null; \
	     else dbus-send --type=method_call --dest=org.kde.amarok / org.freedesktop.MediaPlayer.Quit; fi);\
	Set(title,);\
	Set(status,);
	
APP_QUIT=\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok / org.freedesktop.MediaPlayer.Quit);\
	Timer(AMAROK_INFO,cancel);\
	Set(title,);\
	Set(status,);
	
AMAROK_INFO_GENERAL=\
	Make(var,alb,qdbus org.kde.amarok /Player GetMetadata|grep album:|sed 's/album: //');\
	Make(var,art,qdbus org.kde.amarok /Player GetMetadata|grep artist:|sed 's/.*artist: //'|tail -1);\
	Make(var,tit,qdbus org.kde.amarok /Player GetMetadata|grep title:|sed 's/title: //');\
	Set(title,$(alb) - $(tit));\
	Set(status,$(art));

% AMAROK_INFO must be defined in child modes

AMAROK_CHECK=\
	Macro(AMAROK_INFO,\
		P=`qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus`;\
		if  [ "x$P" = "xPlaying" ]; then echo 0; else echo 1; fi);

AMAROK_COVER=\
	Make(var,coverencode,qdbus org.kde.amarok /Player GetMetadata|grep arturl:|sed "s/arturl: file:\/\///");\
	Make(var,coverdecode,echo 'arg="$(coverencode)";i="0"; \
	                           while [ "$i" -lt ${#arg} ]; do c0=${arg:$i:1}; \
				       if [ "x$c0" = "x%" ]; then c1=${arg:$((i+1)):1}; c2=${arg:$((i+2)):1}; printf "\x$c1$c2"; i=$((i+3)); \
				       else echo -n "$c0"; i=$((i+1)); fi \
				   done'|bash -s);\
	Make(var,coverfile,echo "$(coverdecode)"|md5sum|cut -f 1 -d ' ');\
	Make(var,dummy,\
	     if  [ "x$(coverdecode)" != "x" ]; then \
	         convert -resize $(cover_size)x$(cover_size) -depth -background transparent \
		        "$(coverdecode)" $(TmpDir)/album_covers/$(coverfile)-$(cover_size).png;\
		 echo "$(TmpDir)/album_covers/$(coverfile)-$(cover_size).png";\
	     fi);\
	Set(cover,noname,$(TmpDir)/album_covers/$(coverfile)-$(cover_size).png);

APP_PLAY_STATUS=\
	Make(var,amarok_status,qdbus org.kde.amarok /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus);\
	Make(var,app_status,if [ "x$(amarok_status)" = "xPlaying" ]; then echo playing; else echo paused; fi);

[ModeEnd]


%
% Will be used if compiled with dbus support
%
[Mode]=amarok_general_dbus : amarok_general

APP_POST_SETUP_GENERAL=\
	Dbus(connect,am2,org.kde.amarok,/Player,org.freedesktop.MediaPlayer);\
	Dbus(connect,am2b,org.kde.amarok,/,org.freedesktop.MediaPlayer);\
	Macro(AMAROK_CHECK);\
	Macro(GETVOL);\
	Macro(GETPOS);\
	Make(var,am_mute,by_value,$(am_volume));

GETVOL=\
	Dbus(get,am2,VolumeGet,int,am_volume);
	
GETPOS=\
	Dbus(get,am2,PositionGet,int,am_pos);

VOL+=\
	Macro(GETVOL);\
	Make(var,am_volume,expr $(am_volume) + 5);\
	Dbus(set,am2,VolumeSet,int,$(am_volume));

VOL-=\
	Macro(GETVOL);\
	Make(var,am_volume,expr $(am_volume) - 5);\
	Dbus(set,am2,VolumeSet,int,$(am_volume));

TOGGLE_MUTE=\
	Make(var,am_mute,\
		if [ "x$(am_mute)" = "x-1" ]; then \
           		qdbus org.kde.amarok /Player VolumeGet; \
	   		dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:0; \
       		else \
           		dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:$(am_volume); \
	   		echo "-1"; \
       		fi);	

PLAY=\
	Dbus(set,am2,Play);\
	Macro(AMAROK_INFO);\
	Timer(AMAROK_INFO,$(UpdateTimeout),0);
	
RWD=\
	Macro(GETPOS);\
	Make(var,am_pos,expr $(am_pos) - 10000);\
	Dbus(set,am2,PositionSet,int,$(am_pos));
	
FF=\
	Macro(GETPOS);\
	Make(var,am_pos,expr $(am_pos) + 10000);\
	Dbus(set,am2,PositionSet,int,$(am_pos));
	
PREV=\
	Dbus(set,am2,Prev);\
	Macro(AMAROK_INFO);

STOP=\
	Dbus(set,am2,Stop);\
	Set(title,);\
	Set(status,);\
	Timer(AMAROK_INFO,cancel);

NEXT=\
	Dbus(set,am2,Next);\
	Macro(AMAROK_INFO);

PAUSE=\
	Dbus(set,am2, Pause);\
	Set(title,);\
	Set(status,);

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

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

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

[ModeEnd]

[Mode]=amarok_dbus_3x4 : amarok_general_dbus , default_player

% (EnterMode) by Utils/aliases-server-player.cfg

APP_POST_SETUP=\
        Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,dbus);\
	Make(var,amarok_layout,by_value,3x4);

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

AMAROK_INFO=\
	Macro(AMAROK_INFO_GENERAL);
	
% Custom volume control
1=Macro(VOL-);
2=Macro(TOGGLE_MUTE);
3=Macro(VOL+);

[ModeEnd]

[Mode]=amarok_dbus_7x1 : amarok_general_dbus,default_player_7x1,amarok_cover

APP_POST_SETUP=\
        Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,dbus);\
	Make(var,amarok_layout,by_value,7x1);


APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Playlist,Mute,Run/Quit,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

[ModeEnd]

[Mode]=amarok_dbus_5x1 : amarok_general_dbus,default_player_5x1,amarok_cover

APP_POST_SETUP=\
        Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,dbus);\
	Make(var,amarok_layout,by_value,5x1);


APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Playlist,Mute,Run/Quit,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

PLAY=\
	Dbus(set,am2,Play);\
	Macro(AMAROK_INFO);

APP_TITLE=\
	Macro(AMAROK_INFO);

[ModeEnd]

%
% Will be used if compiled without dbus support
%
[Mode]=amarok_general_nodbus : amarok_general

APP_POST_SETUP_GENERAL=\
	Macro(GETVOL);\
	Make(var,am_mute,by_value,$(am_volume));\
	Macro(AMAROK_CHECK);

GETVOL=\
	Make(var,am_volume,qdbus org.kde.amarok /Player VolumeGet);
	
GETPOS=\
	Make(var,am_pos,qdbus org.kde.amarok /Player PositionGet);

% Custom volume control
VOL-=\
	Macro(GETVOL);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:`expr $(am_volume) - 5`);

VOL+=\
	Macro(GETVOL);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:`expr $(am_volume) + 5`);

TOGGLE_MUTE=\
	Make(var,am_mute,\
		if [ "x$(am_mute)" = "x-1" ]; then \
			qdbus org.kde.amarok /Player VolumeGet; \
			dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:0; \
		else \
			dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:$(am_volume); \
			echo "-1"; \
		fi);

% Possible DBUS console commands
% dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Play
% dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Stop
% dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Next
% dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Prev
% dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Pause

PLAY=\
	Exec(amarok --play);\
	Macro(AMAROK_INFO);\
	Timer(AMAROK_INFO,$(UpdateTimeout),0);
	
RWD=\
	Macro(GETPOS);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.PositionSet int32:`expr $(am_pos) - 10000`);

FF=\
	Macro(GETPOS);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.PositionSet int32:`expr $(am_pos) + 10000`);

PREV=\
	Exec(amarok --previous);\
	Macro(AMAROK_INFO);

STOP=\
	Timer(AMAROK_INFO,cancel);\
	Exec(amarok --stop);\
	Set(title,);\
	Set(status,);

NEXT=\
	Exec(amarok --next);\
	Macro(AMAROK_INFO);

PAUSE=\
	Exec(amarok --pause);\
	Set(title,);\
	Set(status,);

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

Layout 7x1=\
	Make(mode,amarok_nodbus_7x1);
	
Layout 5x1=\
	Make(mode,amarok_nodbus_5x1);

[ModeEnd]

[Mode]=amarok_nodbus_3x4 : amarok_general_nodbus,default_player
	
APP_POST_SETUP=\
	Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,nodbus);\
	Make(var,amarok_layout,by_value,3x4);

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

AMAROK_INFO=\
	Macro(AMAROK_INFO_GENERAL);

% Custom volume control
1=Macro(VOL-);
2=Macro(TOGGLE_MUTE);
3=Macro(VOL+);

[ModeEnd]

[Mode]=amarok_cover

AMAROK_INFO=\
	Macro(AMAROK_INFO_GENERAL);\
	Macro(AMAROK_COVER, if [ "x$(cover_size)" = "x" ]; then echo 1; else echo 0; fi);

Mute=\
	Macro(TOGGLE_MUTE);

[ModeEnd]

[Mode]=amarok_nodbus_7x1 : amarok_general_nodbus,default_player_7x1,amarok_cover

APP_POST_SETUP=\
	Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,nodbus);\
	Make(var,amarok_layout,by_value,7x1);

APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Playlist,Mute,Run/Quit,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

[ModeEnd]

[Mode]=amarok_nodbus_5x1 : amarok_general_nodbus,default_player_5x1,amarok_cover

APP_POST_SETUP=\
	Macro(APP_POST_SETUP_GENERAL);\
	Make(var,am_dbus,by_value,nodbus);\
	Make(var,amarok_layout,by_value,5x1);

APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Playlist,Mute,Run/Quit,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

PLAY=\
	Exec(amarok --play);\
	Macro(AMAROK_INFO);

APP_TITLE=\
	Macro(AMAROK_INFO);

[ModeEnd]

[Mode]=amarok_playlist

(EnterMode)=\
	ExecAndSet(list,replace,Playlist,$(CfgDir)/Utils/getPlayList.sh);\
	Set(menu,replace,Play);
	
(ExitMode)=\
	Set(list,close);

Back($$)=\
	Make(mode,amarok_$(am_dbus)_$(amarok_layout));

Play($$)=\
	Exec(qdbus org.kde.amarok /TrackList org.freedesktop.MediaPlayer.PlayTrack `expr $(Index) - 1`);

Push($$)=\
	Macro(Play($$));

[ModeEnd]

[End]
