%
% anyremote configuration file for amarok management. (Server-mode)
%
% Made by Carlos Sanchez (chukysoria@gmail.com)
% --- modified to run with amarok/kde4 (johann bauer, johann.bauer1@gmail.com )
% --- --- modified (Dmirty Ashkadov, dmitry.ashkadov@gmail.com)

% STATUS stable
% XTEST no
% SOUND app
% ENV no
% ALL_IN_ONE 

GuiAppName=Amarok v4
GuiAppBinary=amarok
GuiAppVersion=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
GuiAppRun=A=`pgrep amarok`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi
GuiAppIcon=amarok.png
GuiAppType=Application
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 supports cover art. This configuration file is for Amarok/KDE4.

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Include($(CfgDir)/Utils/aliases-server-fbrowser-v3i.cfg);\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(FBROWSER_IC_INIT);\
 	Make(var, tmp_dir,by_value,/tmp);\
	Macro(TEST_IMAGEMAGIC);\
	Make(var,kde4_app,by_value,amarok);\
	Macro(TEST_KDE4_APP);\
	Macro(INIT_VARIABLES);

(Exit)=\
	Macro(FBROWSER_IC_EXIT);

INIT_VARIABLES=\
	Make(var,cvDir,by_value,$(tmp_dir)/amarok_covers/);\
	Exec(if [ ! -d $(cvDir) ]; then mkdir $(cvDir); fi);\
	Make(var,am_track_index,by_value,0);\
	Make(var,am_track_index_saved,by_value,-2);\
	Make(var,am_num_of_tracks,by_value,-2);\
	Make(var,am_saved_mode,by_value,amarok-v4);\
	Make(var,am_track_pos,by_value,0);\
	Make(var,am_track_pos_old,by_value,0);\
	Make(var,am_track_length,by_value,1);\
	Make(var,am_volume,by_value,0);\
	Make(var,am_cover,by_value,);\
	Make(var,am_cover_old,by_value,);

MODE_SAVE=\
	Make(var,am_saved_mode,by_value,$(Mode));

MODE_LOAD=\
	Make(mode,$(am_saved_mode));

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

(Connect)=\
	Macro(AMAROK_V2_RUN_IF);\
	Make(var,cover_size,by_value,128);\
	Get(cover_size);\
	Make(mode,amarok-v4);

(Disconnect)=Macro(AMAROK_V2_STOP_TIMER);
	
InCall($$)=\
	Exec(amarok --pause);\
	Macro(AMAROK_V2_STOP_TIMER);\
	Make(remote,off);
	
EndCall($$)=\
	Make(remote,on);\
	Exec(amarok --play);\
	Macro(AMAROK_V2_START_TIMER);

AMAROK_V2_SETINFO=\
	Macro(AMAROK_V2_GET_TRACK_INDEX);\
	Macro(AMAROK_V2_GET_POSITION);\
	Macro(AMAROK_V2_GET_TRACK_LENGTH);\
	Macro(AMAROK_V2_UPDATE_STATUS);\
	Macro(AMAROK_V2_UPDATE_TITLE, expr $(am_track_index) == $(am_track_index_saved));\
	Macro(AMAROK_V2_UPDATE_COVER, expr $(am_track_index) == $(am_track_index_saved));\
	Make(var,am_track_index_saved,by_value,$(am_track_index));

% avoid
%	Macro(AMAROK_V2_UPDATE_PROGRESSBAR);\   -- Android client show volume as temporary floating window
%	Macro(AMAROK_V2_UPDATE_PLAY_ICON);

AMAROK_V2_START_TIMER=Timer(AMAROK_V2_SETINFO,$(UpdateTimeout),0);
AMAROK_V2_STOP_TIMER =Timer(AMAROK_V2_SETINFO,cancel);

AMAROK_V2_UPDATE_STATUS=\
	ExecAndSet(status,min=`expr $(am_track_length) / 60`; sec=`expr $(am_track_length) % 60`; pos_min=`expr $(am_track_pos) / 60`; pos_sec=`expr $(am_track_pos) % 60`; echo "$pos_min:`if [ $pos_sec \< 10 ]; then echo 0; fi`$pos_sec / $min:`if [ $sec \< 10 ]; then echo 0; fi`$sec");

%AMAROK_V2_UPDATE_PROGRESSBAR=\
%	ExecAndSet(volume, if [ $(am_track_length) = 0 ]; then echo 0; else expr $(am_track_pos) \* 100 \/ $(am_track_length); fi);
%
%AMAROK_V2_UPDATE_PLAY_ICON=\
%	Make(var,am_play_icon, if [ $(am_track_pos) = $(am_track_pos_old) ]; then echo 'play'; else echo 'pause'; fi);\
%	Make(var,am_track_pos_old,by_value,$(am_track_pos));\
%	Set(icons,SAME,2,$(am_play_icon));

AMAROK_V2_UPDATE_TITLE=\
	ExecAndSet(title,T=`qdbus org.kde.amarok /TrackList GetMetadata $(am_track_index)|grep title:|cut -f 2 -d ":"`; A=`qdbus org.kde.amarok /TrackList GetMetadata $(am_track_index)|grep artist:|cut -f 2 -d ":"`;echo "`expr $(am_track_index) + 1`. $T" by"$A");

AMAROK_V2_UPDATE_COVER=\
	Make(var,am_cover, qdbus org.kde.amarok /TrackList GetMetadata $(am_track_index)|grep arturl:|cut -f 2 -d " "|sed "s/%20/ /g"|cut -f 2 -d ":");\
	Macro(AMAROK_V2_UPDATE_COVER_SUB, if [ "x$(am_cover)" = "x$(am_cover_old)" ]; then echo 1; else echo 0; fi);

AMAROK_V2_UPDATE_COVER_SUB=\
	ExecAndSet(cover,noname,F=`basename "$(am_cover)"`; if [ "x$F" = "x" ]; then echo ""; else convert -resize $(cover_size)x$(cover_size) -depth 8 -background transparent "$(am_cover)" "$(cvDir)$F"; echo "$(cvDir)$F"; fi);\
	Make(var,am_cover_old,by_value,$(am_cover));

AMAROK_V2_GET_TRACK_INDEX=\
	Make(var,am_track_index, qdbus org.kde.amarok /TrackList GetCurrentTrack | tr -d " ");
	
AMAROK_V2_GET_POSITION=\
	Make(var,am_track_pos, expr `qdbus org.kde.amarok /Player PositionGet` / 1000);
	
AMAROK_V2_GET_TRACK_LENGTH=\
	Make(var,am_track_length,if [ $(am_track_index) \< 0 ]; then echo 0; else expr `qdbus org.kde.amarok /TrackList GetMetadata $(am_track_index)|grep "mtime"|cut -f 2 -d ":"` / 1000; fi);

AMAROK_V2_GET_VOLUME=\
	Make(var,am_volume,qdbus org.kde.amarok /Player VolumeGet);

AMAROK_V2_GET_NUM_OF_TRACKS=\
	Make(var,am_num_of_tracks,qdbus org.kde.amarok /TrackList GetLength);

CoverSize($$)=\
	Make(var,cover_size,by_value,$(Index));

% Main mode ======================================================================

[Mode]=amarok-v4

AM_V2_LAYOUT=\
	Set(icons,Amarok,1,prev,2,play,3,next,4,rewind,5,stop,6,forward,7,mute,8,vol_down,9,vol_up,*,question,0,fullscreen,#,folder);\
	Set(font,small);\
	Set(layout,7x1,choose,2,volume,ticker,up,Vol+,down,Vol-);

AM_V2_MENU=\
	Set(menu,replace,Shuffle,Repeat,Playlist,Toggle Fullscreen,Track Info,Run Player,Show Player,Hide Player,Quit Player,PowerMgmt,Help);\
	Macro(ALLIN1MENU);

(EnterMode)=\
	Macro(AM_V2_LAYOUT);\
	Macro(AMAROK_V2_SETINFO);\
	Macro(AM_V2_MENU);\
	Macro(AMAROK_V2_START_TIMER);

(ExitMode)=\
	Macro(AMAROK_V2_STOP_TIMER);\
	Make(var,am_track_index_saved,by_value,-2);

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

Playlist=\
	Macro(MODE_SAVE);\
	Make(mode,amarok_playlist_v4);

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));

Vol+=Exec(qdbus org.kde.amarok /Player VolumeUp 5);\
	Macro(AMAROK_V2_GET_VOLUME);\
	ExecAndSet(status, echo 'Volume: $(am_volume) %');

Vol-=Exec(qdbus org.kde.amarok /Player VolumeDown 5);\
	Macro(AMAROK_V2_GET_VOLUME);\
	ExecAndSet(status, echo 'Volume: $(am_volume) %');

% Android support
VOL-=Macro(Vol-);
VOL+=Macro(Vol+);

Toggle Fullscreen=\
	Macro(ToggleFullscreen);

ToggleFullscreen=\
	Set(fullscreen,toggle);\
	Set(cover,clear);\
	Get(cover_size);

Track Info=\
	Macro(MODE_SAVE);\
	Macro(AMAROK_V2_GET_TRACK_INDEX);\
	Make(mode,TrackInfo);

CoverSize($$)=\
	Make(var,cover_size,by_value,$(Index));\
	Macro(AMAROK_V2_UPDATE_COVER_SUB);
	
Run Player=\
	Macro(AMAROK_V2_RUN_IF);

Quit Player=\
	Exec(qdbus org.kde.amarok /MainApplication quit);\
	Macro(AMAROK_V2_STOP_TIMER);\
	Set(title,);\
	Set(status,);\
	Set(cover,clear);
	
%	Set(volume,0);

Show Player=Exec(qdbus org.kde.amarok /amarok/MainWindow show);
Hide Player=Exec(qdbus org.kde.amarok /amarok/MainWindow hide);
	
Help=Macro(MODE_SAVE);Make(mode,Help);


1=Exec(qdbus org.kde.amarok /Player Prev);Macro(AMAROK_V2_SETINFO);
* 1=Exec(qdbus org.kde.amarok /Player Backward 300000);Macro(AMAROK_V2_SETINFO);

2=Exec(qdbus org.kde.amarok /Player PlayPause);Macro(AMAROK_V2_SETINFO);

3=Exec(qdbus org.kde.amarok /Player Next);Macro(AMAROK_V2_SETINFO);
* 3=Exec(qdbus org.kde.amarok /Player Forward 300000);Macro(AMAROK_V2_SETINFO);

4=Exec(qdbus org.kde.amarok /Player Backward 10000);Macro(AMAROK_V2_SETINFO);
* 4=Exec(qdbus org.kde.amarok /Player Backward 60000);Macro(AMAROK_V2_SETINFO);

5=Exec(qdbus org.kde.amarok /Player Stop);Macro(AMAROK_V2_SETINFO);

6=Exec(qdbus org.kde.amarok /Player Forward 10000);Macro(AMAROK_V2_SETINFO);
* 6=Exec(qdbus org.kde.amarok /Player Forward 60000);Macro(AMAROK_V2_SETINFO);

7=Exec(qdbus org.kde.amarok /Player Mute);
8=Macro(Vol-);
9=Macro(Vol+);

#=Macro(Playlist);
0=Macro(ToggleFullscreen);
* *=Macro(Help);

[ModeEnd]



% =================================================================================
[Mode]=amarok_playlist_v4

(EnterMode)=\
	Set(list,font,small);\
	Set(list,bg,50,70,130);\
	Set(list,fg,145,165,230);\
	Macro(AMAROK_V2_GET_NUM_OF_TRACKS);\
	Macro(AMAROK_V2_GET_TRACK_INDEX);\
	Macro(SET_LIST_IF_NOT_EMPTY);\
	Macro(SET_LIST_ITEM, expr $(am_track_index) \< 0);

% collision for Push() event
%	Macro(SET_LIST_IF_EMPTY, expr $(am_num_of_tracks) != 0);\
%	Macro(SET_LIST_IF_NOT_EMPTY, expr $(am_num_of_tracks) == 0);\
%	
	
SET_LIST_ITEM=\
	ExecAndSet(list,select, expr $(am_track_index) + 1);
	
%SET_LIST_IF_EMPTY=\
%	Set(menu,replace,Add,Refresh);Set(list,replace,Playlist, -- empty --);

SET_LIST_IF_NOT_EMPTY=\
	Set(menu,replace,Play,Add,Remove,Info,Refresh,Clear);\
	ExecAndSet(list,replace,Playlist,$(CfgDir)/Utils/getPlayList.sh);

(ExitMode)=Set(list,close);

Back($$)=\
	Make(mode,amarok-v4);

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

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

Info($$)=\
	Make(var,am_track_index,expr $(Index) - 1);\
	Macro(MODE_SAVE);\
	Make(mode,TrackInfo);

Remove($$)=\
	Exec(qdbus org.kde.amarok /TrackList DelTrack `expr $(Index) - 1`);\
	Make(mode,amarok_playlist_v4);

Add($$)=\
	Macro(MODE_SAVE);\
	Make(mode,FileBrowser);

Clear($$)=\
	Macro(AMAROK_V2_GET_NUM_OF_TRACKS);\
	Exec(for (( i=0; i<$(am_num_of_tracks); ++i)) do qdbus org.kde.amarok /TrackList DelTrack 0; done);\
	Set(menu,replace,Add,Refresh);

%	Macro(SET_LIST_IF_EMPTY);

Refresh($$)=\
	Make(mode,amarok_playlist_v4);

[ModeEnd]

% =================================================================================

[Mode]=TrackInfo

(EnterMode)=\
	ExecAndSet(text,replace,Track Info,qdbus org.kde.amarok /TrackList GetMetadata $(am_track_index) | sed "s/%20/ /g");

(ExitMode)=\
	Set(text,close);
	
Back=Macro(MODE_LOAD);

[ModeEnd]

% ==================================================================================

[Mode]=FileBrowser

(EnterMode)=\
	Macro(FBROWSER_IC_FILL_LIST);\
	Set(menu,replace,GoTo,Add,Go Home);\
	Macro(ALLIN1MENU);

(ExitMode)=\
	Set(iconlist,close);

Back($$)=\
	Macro(MODE_LOAD);

Add($$)=\
	Exec(cd "$(fbrowser_dir)"; amarok -a "$(Param)");

GoTo($$)=\
	Macro(FBROWSER_IC_GO_TO);

Go Home($$)=\
	Macro(FBROWSER_IC_GO_HOME);

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

[ModeEnd]


% ==================================================================================

[Mode]=Help

(EnterMode)=\
	Set(text,replace,Help,Manage player by joystick, by keypad or from menu.\n \n 1 - Prev. track\n 2 - Play/Pause\n 3 - Next track\n 4 - Rewind\n 5 - Stop\n 6 - Forward\n 7 - Mute\n 8 - Volume down\n 9 - Volume up\n 0 - Fullscreen\n # - Playlist\n * 1 - Rewind 5 min\n * 3 - Forward 5 min\n * 4 - Rewind 1 min\n * 6 - Forward 1 min\n * * - Help);

(ExitMode)=\
	Set(text,close);

Back=Macro(MODE_LOAD);

[ModeEnd]

[End]

