%
% anyremote configuration file for guayadeque management. (Server-mode)
%
% made by Fabian Frank (onlinezeug@gmx.net)
% ---- based on amarok.cfg (initial version) by Carlos Sanchez

% STATUS non-stable
% XTEST no
% SOUND mixer
% ENV no
% DEF_PLAYER
% ALL_IN_ONE
% MEDIACENTER audio

GuiAppName=Guayadeque
GuiAppBinary=guayadeque
GuiAppVersion=
GuiAppRun=echo 'A=`pgrep guayadeque`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi'|sh -s
GuiAppIcon=guayadeque.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=Guayadeque Music Player

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(SETUP_DEFAULT_MIXER);\
	Macro(TEST_DBUS_SEND);\
	Make(var,player_name,by_value,Guayadeque);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Make(var,cover_size,by_value,);\
	Make(var,guayadeque_dbus,if [ "x$(Dbus)" = "xyes" ]; then echo DBUS; else echo NODBUS; fi);\
	Make(var,guayadeque_exec,by_value,\
	     dbus-send --type=method_call --dest=org.mpris.guayadeque /Player);\
	Make(var,guayadeque_get,by_value,\
	     dbus-send --print-reply --type=method_call --dest=org.mpris.guayadeque /Player);\
	Make(var,guayadeque_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get guayadeque_layout`;\
	     if [ "x$V" = "x" ]; then echo "5x1"; else echo $V; fi);

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

(Connect)=\
	Make(mode,guayadeque_$(guayadeque_layout));

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

[Mode]=guayadeque_general

GUAYADEQUE_TITLE_GENERAL=\
	Make(var,alb,$(guayadeque_get) org.freedesktop.MediaPlayer.GetMetadata|awk '/.*album".*/{getline;print}'|grep string|cut -f 2 -d '"');\
	Make(var,art,$(guayadeque_get) org.freedesktop.MediaPlayer.GetMetadata|awk '/.*artist".*/{getline;print}'|grep string|cut -f 2 -d '"');\
	Make(var,tit,$(guayadeque_get) org.freedesktop.MediaPlayer.GetMetadata|awk '/.*title".*/{getline;print}'|grep string|cut -f 2 -d '"');\
	Set(title,$(alb) - $(tit));\
	Set(status,$(art));

GUAYADEQUE_SET_VOL=\
	Make(var,dummy,sleep 1;echo 0);\
	Macro(DEFAULT_VOLUME_GET);\
	Set(volume,$(default_mixer_value));
    
GUAYADEQUE_TIMER=\
	Macro(GUAYADEQUE_TITLE);\
	Timer(GUAYADEQUE_TITLE,$(UpdateTimeout),0);
	
GUAYADEQUE_TIMER_CANCEL=\
	Timer(GUAYADEQUE_TITLE,cancel);\
	Set(title,);\
	Set(status,);

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

RUN_OR_QUIT=\
	Exec(ID=`id -u`;P=`pgrep -u $ID guayadeque`; \
	     if [ "x$P" = "x" ]; then \
	         guayadeque 2> /dev/null&; \
             else \
	         dbus-send --type=method_call --dest=org.mpris.guayadeque / org.freedesktop.MediaPlayer.Quit; \
	     fi);\
	Set(title,);\
	Set(status,);

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

PLAY_ONLY=\
	Macro(PLAY_$(guayadeque_dbus));

PLAY=\
	Macro(PLAY_ONLY);\
	Macro(GUAYADEQUE_TIMER);

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

PREV=\
	Macro(PREV_$(guayadeque_dbus));\
	Macro(GUAYADEQUE_TITLE);

NEXT=\
	Macro(NEXT_$(guayadeque_dbus));\
	Macro(GUAYADEQUE_TITLE);

STOP=\
	Macro(GUAYADEQUE_TIMER_CANCEL);\
	Macro(STOP_$(guayadeque_dbus));

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

APP_QUIT=\
	Macro(GUAYADEQUE_TIMER_CANCEL);\
	Macro(QUIT_$(guayadeque_dbus));

%
% DBUS versions
%
GUAYADEQUE_SETUP_DBUS=\
	Dbus(connect,guaya,org.mpris.guayadeque,/Player,org.freedesktop.MediaPlayer);\
	Dbus(connect,guaya2,org.mpris.guayadeque,/,org.freedesktop.MediaPlayer);

GETPOS_DBUS=\
	Dbus(get,guaya,PositionGet,int,guaya_pos);

RWD_DBUS=\
	Macro(GETPOS_DBUS);\
	Make(var,guaya_pos,expr $(guaya_pos) - 10000);\
	Dbus(set,guaya,PositionSet,int,$(guaya_pos));

PLAY_DBUS=\
	Dbus(set,guaya,Play);

FF_DBUS=\
	Macro(GETPOS_DBUS);\
	Make(var,guaya_pos,expr $(guaya_pos) + 10000);\
	Dbus(set,guaya,PositionSet,int,$(guaya_pos));

PREV_DBUS=\
	Dbus(set,guaya,Prev);

STOP_DBUS=\
	Dbus(set,guaya,Stop);

NEXT_DBUS=\
	Dbus(set,guaya,Next);

PAUSE_DBUS=\
	Dbus(set,guaya, Pause);

QUIT_DBUS=\
	Dbus(set,guaya2,Quit);

%
% No-DBUS versions
%
GUAYADEQUE_SETUP_NODBUS=\
	Make(none);

GETPOS_NODBUS=\
	Make(var,guaya_pos,$(guayadeque_get) org.freedesktop.MediaPlayer.PositionGet|grep int32|tr -d ' '|sed 's/int32//');
   
PLAY_NODBUS=\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.Play);

RWD_NODBUS=\
	Macro(GETPOS_NODBUS);\
	Make(var,guaya_pos,expr $(guaya_pos) - 10000);\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.PositionSet int32:$(guaya_pos));

FF_NODBUS=\
	Macro(GETPOS_NODBUS);\
	Make(var,guaya_pos,expr $(guaya_pos) + 10000);\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.PositionSet int32:$(guaya_pos));

PREV_NODBUS=\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.Prev);

STOP_NODBUS=\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.Stop);

NEXT_NODBUS=\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.Next);

PAUSE_NODBUS=\
	Exec($(guayadeque_exec) org.freedesktop.MediaPlayer.Pause);

QUIT_NODBUS=\
	Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque / org.freedesktop.MediaPlayer.Quit);

APP_PLAY_STATUS=\
	Make(var,guayadeque_status,$(guayadeque_get) org.freedesktop.MediaPlayer.GetStatus|awk '/.*struct.*/{getline;print}'|sed 's/int32//'|tr -d ' ');\
	Make(var,app_status,if [ "x$(guayadeque_status)" = "x0" ]; then echo playing; else echo paused; fi);

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

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

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

[ModeEnd]

[Mode]=guayadeque_3x4 : guayadeque_general, default_player

SETUP_DEFAULT_PLAYER=\
	Set(layout,default,ticker,volume);\
	Set(status,);\
	Set(title,);\
	Set(icons,$(player_name),1,vol_down,2,mute,3,vol_up,4,rewind,5,play,6,forward,7,prev,8,stop,9,next,*,question,0,pause,#,no);\
	Macro(GUAYADEQUE_SETUP_$(guayadeque_dbus));\
	Set(font,small);\
	Macro(GUAYADEQUE_SET_VOL);\
	Macro(GUAYADEQUE_TITLE);

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

APP_POST_SETUP=\
	Make(var,guayadeque_layout,by_value,3x4);

% Override
1=Macro(DEFAULT_VOLUME_DOWN);\
	Macro(GUAYADEQUE_SET_VOL);

2=Macro(DEFAULT_TOGGLE_MUTE);\
	Macro(GUAYADEQUE_SET_VOL);

3=Macro(DEFAULT_VOLUME_UP);\
	Macro(GUAYADEQUE_SET_VOL);

% PowerMgmt by Utils/aliases-server.cfg
Help=\
	Set(text,replace,Help, 1 Vol-\n 2 Mute\n 3 Vol+\n 4 Rewind\n5 Play\n* 5 Refresh title\n 6 FF\n 7 Prew\n 8 Stop\n 9 Next\n* * Help\n* # Run/Quit $(player_name)\n 0 Pause\n # Quit $(player_name));

% Force title update
* 5=Macro(GUAYADEQUE_TITLE);

GUAYADEQUE_TITLE=\
	Macro(GUAYADEQUE_TITLE_GENERAL);

[ModeEnd]

[Mode]=guayadeque_cover

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

APP_COVER=\
	Make(var,media_file, $(guayadeque_get)  org.freedesktop.MediaPlayer.GetMetadata|\
	                     awk '/.*arturl".*/{getline;print}'|grep string|cut -f 2 -d '"'|cut -f 2 -d ':'|sed 's/"$//;s/^\/\/\//\//');\
	Macro(DEFAULT_METADATA_COVER);

[ModeEnd]

[Mode]=guayadeque_7x1 : guayadeque_general, default_player_7x1, guayadeque_cover

APP_MENU=\
	Set(menu,replace,Mute,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(GUAYADEQUE_SETUP_$(guayadeque_dbus));\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,guayadeque_layout,by_value,7x1);
	
[ModeEnd]

[Mode]=guayadeque_5x1 : guayadeque_general, default_player_5x1, guayadeque_cover

APP_MENU=\
	Set(menu,replace,Mute,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(GUAYADEQUE_SETUP_$(guayadeque_dbus));\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

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

APP_TITLE=\
	Macro(GUAYADEQUE_TITLE);

PLAY=\
	Macro(PLAY_ONLY);
	
[ModeEnd]

%
% NOT USED
%
[Mode]=guayadeque_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.guayadeque);\
	Make(var,guayadeque_track, dbus-send --print-reply --type=method_call --dest=org.mpris.guayadeque /TrackList org.freedesktop.MediaPlayer.GetCurrentTrack|grep int32|tr -d ' '|sed 's/int32//');\
  	ExecAndSet(list,select,expr 1 + $(guayadeque_track));\
	Set(menu,replace,Choose);
	
Push($$)=\
	Macro(Choose($$))

% no such method	
Choose($$)=\
	Exec(qdbus org.mpris.guayadeque /TrackList PlayTrack `expr $(Index) - 1`);

Back($$)=\
	Set(list,close);\
	Make(mode,guayadeque_$(guayadeque_layout));

[ModeEnd]

[End]
