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

%
% An important note:
%
% Heartbeat messages are sent every 3 seconds and the maximal waiting time is only 5 seconds.
% If no responces are comes in that short period of time, then iViewer will show the warning message.
% So, it is strongly suggested to write _fast_ and _effective_ commands. 
% Keep in mind timers which also will consume some amount of time.
%

% STATUS stable
% XTEST no
% SOUND mixer
% ENV no

GuiAppName=Template
GuiAppBinary=true
GuiAppRun= P=`ps -ef|grep my-app|grep -v grep|grep -v anyremote|grep -v edit`; if [ "x$P" = "x" ]; then echo NOK; else echo OK; fi
GuiAppIcon=file.png
GuiAppType=Example
GuiAppProtocols=iViewer
GuiAppDesc=This is template file for CommandFusion iViewer

[Protocol]=iViewer

%a1 = volume
%a2 = seek bar (needs to update constantly)

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

% play
d1(1)=\
	Exec(echo Play);\
	Macro(TITLE);

% stop
d30(1)=\
	Exec(echo Stop);\
	Macro(AM_RESET_COVER);

% pause
d31(1)=\
	Exec(echo Pause);

% next
d5(1)=\
	Exec(echo next);\
	Macro(TITLE);

% prev
d6(1)=\
	Exec(echo previous);\
	Macro(TITLE);

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

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

% volume contol
a1($$)=\
	Make(var,default_mixer_value,by_value,$(Index));\
	Macro(DEFAULT_VOLUME_SET);\
	Macro(VOLUME);

% seek >> and <<
a2($$)=\
	Make(var,seek,echo "$(Index)*100/65535"|bc);\
	Exec(echo "got $(Index)");\
	Exec(echo "set position to $(seek)%");\
	Macro(PLAYBACK);

% Feedback
PLAYBACK=\
	Send(string,s5=);ExecAndSend(string,echo "01:00");Send(bytes,3);\
	Make(var,playback_persent,echo "65535*"`echo 50`"/100"|bc);\
	Send(string,a2=$(playback_persent));Send(bytes,3);

% get volume: 0 - 65535
VOLUME=\
	Macro(DEFAULT_VOLUME_GET);\
	Send(string,a1=$(default_mixer_value));Send(bytes,3);

% Artist name
ARTIST=ExecAndSend(string,echo "Artist is ...");

% Track
TRACK=\
	ExecAndSend(string,echo "Track is ...");

% Song length mm:ss
SONG_LEN=\
	ExecAndSend(string,echo "04:00");

% cover image generation
IV_COVER=\
	Make(var,coverfile,echo "command to get file with cover image");\
	Make(var,coverconverted,F=`basename "$(coverfile)"`;if [ "x$F" = "x" ]; then echo img_coverart-300.png;else convert "$(coverfile)" -resize 300 -depth 8 $(TmpDir)/generated_cover;echo generated_cover; fi);\
	Send(string,s4=$(coverconverted));\
	Send(bytes,3);

% reset cover to default image (if playback is stopped)
IV_RESET_COVER=\
	Send(string,s4=img_coverart-300.png);\
	Send(bytes,3);

%
% The following is a common part. In general, it should not be changed.
%

TITLE=\
	Send(string,s1=);Macro(ARTIST);Send(bytes,3);\
	Send(string,s2=);Macro(TRACK);Send(bytes,3);\
	Send(string,s6=);Macro(SONG_LEN);Send(bytes,3);\
	Macro(IV_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 (no heartbeat == connection is lost)
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);

% Event timers
(Init)=\
	Include($(CfgDir)/Utils/aliases-iviewer.cfg);\
	Macro(SETUP_DEFAULT_MIXER);\
	Make(var,IViewer,by_value,true);

(Connect)=\
	Timer(DISC,10,1);\
	Timer(PLAYBACK,3,0);\
	Timer(VOLUME,10,0);\
	Timer(TITLE,5,0);

(Disconnect)=\
	Macro(STOP_TIMERS);
	
[End]

