%
% anyremote configuration file for Amarok/KDE4 management. (Server-mode for Command Fusion iViewer )
% prerequisites: Iviewer client installed on Ipod touch/Iphone 2.x and Iviewer free winamp GUI
%

% STATUS unknown
% XTEST no
% SOUND app
% ENV no

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=iViewer
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]=iViewer

(Init)=\
	Make(var,IViewer,by_value,true);\
	Exec(P=`which dbus-send|grep dbus-send|grep -v no|wc -l|tr -d " "`;if [ "x$P" = "x1" ]; then true; else $(CfgDir)/Utils/message.sh "ERROR: dbus-send is not found"; fi);\
	Exec(P=`which qdbus|grep qdbus|grep -v no|wc -l|tr -d " "`; if [ "x$P" = "x1" ]; then true; else $(CfgDir)/Utils/message.sh "ERROR: qdbus is not found"; fi);\
	Exec(V=`amarok -v|grep KDE|tr -d " "|cut -f 2 -d ":"|cut -f 1 -d "."`; if [ "x$V" = "x3" ]; then $(CfgDir)/Utils/message.sh "ERROR: this configuration file is for KDE4"; fi);

%a1 = volume
%a2 = needs a constent update to. its the seek bar

%s1 = artist
%s2 = track
%s3 = album
%s5 = current playback in min:sec
%s6 = song length min:sec.

s10001($$)=Exec(echo "s10001=$(Param)");
s10002($$)=Exec(echo "s10002=$(Param)");
n(Player)=Exec(echo "n=Player");
m(portrait)=Exec(echo "m=portrait");

% play
d1(1)=Exec(amarok --play);Exec(echo "d1=1");

% play
d1(0)=Exec(echo "d1=0");

% stop
d30(1)=\
	Exec(amarok --stop);\
	Macro(AM_RESET_COVER);\
	Exec(echo "d30=1");
	
d30(0)=Exec(echo "d30=0");

% pause
d31(1)=Exec(amarok --pause);Exec(echo "d31=1");

% next
d5(1)=\
	Exec(amarok --next);\
	Macro(TITLE);\
	Macro(PLAYBACK);

% prev
d6(1)=\
	Exec(amarok --previous);\
	Macro(TITLE);\
	Macro(PLAYBACK);

% toggle repeat
d12(1)=Exec(echo "Toggle repeat: Oops")

% toggle shuffle
d9(1)=Exec(echo "Toggle shuffle: Oops")

% volume contol
a1($$)=\
    Make(var,change_vol,echo "$(Index)*100/65535"|bc);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.VolumeSet int32:$(change_vol));\
	Macro(VOLUME);

% seek >> and << 
a2($$)=\
    Make(var,song_len,qdbus org.kde.amarok /Player GetMetadata|grep mtime|sed 's/mtime://'|tr -d ' ');\
	Make(var,song_pos,echo "$(Index)*$(song_len)/65535"|bc);\
	Exec(dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.PositionSet int32:$(song_pos));\
	Macro(PLAYBACK);

% Feedback
PLAYBACK=\
	Make(var,song_pos,qdbus org.kde.amarok /Player PositionGet);\
	ExecAndSend(string,echo $(song_pos)|awk '{min=int($0/60000);sec=int(($0-min*60000)/1000);if (sec < 10) {sec="0"sec;}; printf("s5=%s:%s",min,sec);}');\
	Send(bytes,3);\
	Make(var,playback_pos,P=`qdbus org.kde.amarok /Player GetMetadata|grep mtime|sed "s/mtime://"|tr -d " "`; if [ "x$P" = "x" ]; then echo ""; else echo "65535*$(song_pos)/$P"|bc; fi);\
	Send(string,a2=$(playback_pos));\
	Send(bytes,3);

% get volume: 0 - 65535
VOLUME=\
	Make(var,vol_value,echo "65535*"`qdbus org.kde.amarok /Player VolumeGet`"/100"|bc);\
	Send(string,a1=$(vol_value));\
	Send(bytes,3);

% Set Track, Album, Artist and song length (mm:ss)
TITLE=\
	ExecAndSend(string,echo "s1=`qdbus org.kde.amarok /Player GetMetadata|grep artist:|sed 's/artist://'`");\
	Send(bytes,3);\
	ExecAndSend(string,echo "s2=`qdbus org.kde.amarok /Player GetMetadata|grep title:|sed 's/title://'`");\
	Send(bytes,3);\
	ExecAndSend(string,echo "s3=`qdbus org.kde.amarok /Player GetMetadata|grep album:|sed 's/album://'`");\
	Send(bytes,3);\
	ExecAndSend(string,qdbus org.kde.amarok /Player GetMetadata|grep mtime|sed 's/mtime://'|tr -d ' '|awk '{min=int($0/60000);sec=int(($0-min*60000)/1000);if (sec < 10) {sec="0"sec;}; printf("s6=%s:%s",min,sec);}');\
	Send(bytes,3);\
	Macro(AM_COVER);

d5(0)=Macro(TITLE);
d6(0)=Macro(TITLE);

% Init
i(1)=Macro(TITLE);

% Password p(PASSWORD)"
p($$)=Send(string,p=ok);Send(bytes,3);

% Heartbeat
DISC=Macro(STOP_TIMERS);Make(disconnect);

h(0)=Timer(DISC,reset);

% no needs to send "h=1\03" replay directly because this will be done 
% automatically - that controlled by $(IViewer) variable
% If $(IViewer) variable is not set it needs to use the following command:
%h(0)=Send(bytes,104,61,49,3);Timer(DISC,reset);

STOP_TIMERS=\
	Timer(PLAYBACK,cancel);\
	Timer(VOLUME,cancel);\
	Timer(TITLE,cancel);

% run these timers a bit postponed
START_TIMERS=\
	Timer(DISC,10,1);\
	Timer(PLAYBACK,3,0);\
	Timer(VOLUME,10,0);\
	Timer(TITLE,5,0);
	
% Event timers
(Connect)=\
	Exec(A=`pgrep amarok`; if [ "x$A" = "x" ]; then amarok 2> /dev/null; fi);\
	Timer(START_TIMERS,10,1);

(Disconnect)=\
	Timer(DISC,cancel);\
	Macro(STOP_TIMERS);

AM_COVER=\
	Make(var,coverencode,qdbus org.kde.amarok /Player GetMetadata|grep arturl:|sed "s/arturl: //;s/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 -f -s);\
	Make(var,coverconverted, F=`basename "$(coverdecode)"`;if [ "x$F" = "x" ]; then echo img_coverart-300.png;else convert "$(coverdecode)" -resize 300 $HOME/.anyRemote/generated_cover;echo generated_cover; fi);\
	Send(string,s4=$(coverconverted));\
	Send(bytes,3);

AM_RESET_COVER=\
	Send(string,s4=img_coverart-300.png);\
	Send(bytes,3);

[End]

