%
% 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

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_QDBUS);\
	Macro(TEST_DBUS_SEND);\
	Make(var,player_name,by_value,Guayadeque);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);

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

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

GUAYADEQUE_HELP=\
	Set(text,replace,Help, 1 Vol-\n 2 Mute\n 3 Vol+\n 4 Rewind\n5 Play\n 6 FF\n 7 Prew\n 8 Stop\n 9 Next\n * Refresh\n 0 Pause\n # Quit $(player_name));

GUAYADEQUE_TITLE=\
	Make(var,alb,qdbus org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.GetMetadata|grep album:|sed 's/album://');\
	Make(var,art,qdbus org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.GetMetadata|grep artist:|sed 's/artist://');\
	Make(var,tit,qdbus org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.GetMetadata|grep title:|sed 's/title://');\
	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,5,0);

[Mode]=guayadeque

(EnterMode)=\
	Make(var,guaya_dbus,if [ "x$(Dbus)" = "xyes" ]; then echo guayadeque_dbus; else echo guayadeque_nodbus; fi);\
	Make(mode,$(guaya_dbus));

[ModeEnd]

%
% Will be used if compiled with dbus support
%
[Mode]=guayadeque_dbus : default_player

(EnterMode)=\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(RUN_IF_NEED);\
	Macro(SETUP_DEFAULT_PLAYER);\
	Macro(APP_MENU);

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,*,refresh,0,pause,#,no);\
	Dbus(connect,guaya,org.mpris.guayadeque,/Player,org.freedesktop.MediaPlayer);\
	Dbus(connect,guaya2,org.mpris.guayadeque,/,org.freedesktop.MediaPlayer);\
	Set(font,small);\
	Macro(GUAYADEQUE_SET_VOL);\
	Macro(GUAYADEQUE_TITLE);

APP_MENU=\
	Set(menu,replace,Help,Power Off,Suspend);\
	Macro(ALLIN1MENU);

% Power Off, Suspend by Utils/aliases-server-player.cfg
Help=Macro(GUAYADEQUE_HELP);

% 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);
*=Macro(GUAYADEQUE_TITLE);

GETPOS=Dbus(get,guaya,PositionGet,int,guaya_pos);

% Sound volume control by Utils/aliases-server-player.cfg

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

PLAY=Dbus(set,guaya,Play);Macro(GUAYADEQUE_TIMER);

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

PREV=Dbus(set,guaya,Prev);Macro(GUAYADEQUE_TITLE);
STOP=Dbus(set,guaya,Stop);Set(title,);Set(status,);Timer(GUAYADEQUE_TITLE,cancel);
NEXT=Dbus(set,guaya,Next);Macro(GUAYADEQUE_TITLE);

PAUSE=Dbus(set,guaya, Pause);

RUN_IF_NEED=Exec($(CfgDir)/Utils/run-or-switch-to.sh guayadeque);
RUN_OR_QUIT=\
	Exec(P=`pgrep -u `id -u` 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,);
QUIT=Dbus(set,guaya2,Quit);\
	Timer(GUAYADEQUE_TITLE,cancel);\
	Set(title,);\
	Set(status,);

[ModeEnd]

%
% Will be used if compiled without dbus support
%
[Mode]=guayadeque_nodbus : default_player

(EnterMode)=\
	Macro(SETUP_DEFAULT_POWER_MANAGEMENT);\
	Macro(RUN_IF_NEED);\
	Macro(SETUP_DEFAULT_PLAYER);\
	Macro(APP_MENU);
	
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,*,refresh,0,pause,#,no);\
	Set(font,small);\
	Macro(GUAYADEQUE_SET_VOL);\
	Macro(GUAYADEQUE_TITLE);

% Found no way to choose track from playlist -- MPRIS support seems to be incompete
%APP_MENU=Set(menu,replace,Help,Power Off,Suspend);Macro(ALLIN1MENU);
APP_MENU=Set(menu,replace,Playlist,Help,Power Off,Suspend);Macro(ALLIN1MENU);

% Power Off, Suspend by Utils/aliases-server-player.cfg
Help=Macro(GUAYADEQUE_HELP);

% 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);
*=Macro(GUAYADEQUE_TITLE);

GETPOS=Make(var,guaya_pos,dbus-send --print-reply --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.PositionGet|grep int32|tr -d ' '|sed 's/int32//');

% Sound volume control by Utils/aliases-server-player.cfg
   
PLAY=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.Play);Macro(GUAYADEQUE_TIMER);

RWD=Macro(GETPOS);\
	Make(var,guaya_pos,expr $(guaya_pos) - 10000);\
	Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.PositionSet int32:$(guaya_pos));

FF=Macro(GETPOS);\
	Make(var,guaya_pos,expr $(guaya_pos) + 10000);\
	Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.PositionSet int32:$(guaya_pos));

PREV=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.Prev);Macro(GUAYADEQUE_TITLE);
STOP=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.Stop);Timer(GUAYADEQUE_TITLE,cancel);Set(title,);Set(status,);
NEXT=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.Next);Macro(GUAYADEQUE_TITLE);
PAUSE=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque /Player org.freedesktop.MediaPlayer.Pause);

RUN_IF_NEED=Exec($(CfgDir)/Utils/run-or-switch-to.sh guayadeque);
RUN_OR_QUIT=\
	Exec(P=`pgrep -u `id -u` 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,);
QUIT=Exec(dbus-send --type=method_call --dest=org.mpris.guayadeque / org.freedesktop.MediaPlayer.Quit);\
	Timer(GUAYADEQUE_TITLE,cancel);\
	Set(title,);\
	Set(status,);

% Found no way to choose track from playlist -- MPRIS support seems to be incompete
%Playlist=\
%	ExecAndSet(list,replace,Playlist,$(CfgDir)/Utils/getPlayList.sh org.mpris.guayadeque);\
%  	ExecAndSet(list,select,expr 1 + `qdbus org.mpris.guayadeque /TrackList org.freedesktop.MediaPlayer.GetCurrentTrack`);\
%	Set(menu,replace,Choose);
%	
%Push($$)=\
%	Macro(Choose($$))
%	
%Choose($$)=\
%	Exec(qdbus org.mpris.guayadeque /TrackList PlayTrack `expr $(Index) - 1`);

[ModeEnd]

[End]
