%
% anyremote Server-mode configuration file for Noise (the official audio player of elementary OS) management.
% https://launchpad.net/noise
% 

% STATUS stable
% SOUND mixer
% ENV no
% IPC app
% DEF_PLAYER   
% ALL_IN_ONE
%      (Fixed) Quit issue https://bugs.launchpad.net/noise/+bug/1081493
%      (Fixed) Stop/Play issue https://bugs.launchpad.net/noise/+bug/1081497

GuiAppName=Noise
GuiAppBinary=noise
GuiAppRun= P=`ps -ef|grep noise|grep -v grep|grep -v anyremote|grep -v edit`; if [ "x$P" = "x" ]; then echo NOK; else echo OK; fi
GuiAppIcon=noise.svg
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=Noise is the official audio player of elementary OS

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
 	Macro(SETUP_DEFAULT_MIXER);\
	Macro(TEST_DBUS_SEND);\
	Make(var,player_name,by_value,noise);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Make(var,noise_cmd,by_value,dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2);\
	Make(var,noise_cmd_get_prop,by_value,dbus-send --type=method_call --print-reply --dest=org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get);\
	Make(var,noise_cmd_set_prop,by_value,dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Set);\
	Make(var,cover_size,by_value,);\
	Make(var,noise_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get noise_layout`;\
	     if [ "x$V" = "x" ]; then echo "5x1"; else echo $V; fi);

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

(Connect)=\
	Make(mode,noise_$(noise_layout));

[Mode]=noise_general

PLAY=\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.Play);\
	Macro(NOISE_CHECK_TIMER);

NOISE_CHECK_TIMER=\
	Macro(NOISE_STATUS);\
	Make(var,timer_action,if [ "x$(noise_status)" = "xPlaying" ]; then echo NOISE_START; else echo NOISE_CANCEL; fi);\
	Macro($(timer_action));

PLAYPAUSE=\
        Make(var,dummy,$(noise_cmd) org.mpris.MediaPlayer2.Player.PlayPause);
	
%qdbus org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.SetPosition a `expr $(noise_pos) - 5000000`
%qdbus org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.SetPosition a `expr $(noise_pos) + 5000000`

RWD=\
	Macro(NOISE_GETPOS);\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.SetPosition string:'a' int64:`expr $(noise_pos) - 5000000`);
	
FF=\
	Macro(NOISE_GETPOS);\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.SetPosition string:'a' int64:`expr $(noise_pos) + 5000000`);

PREV=\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.Previous);\
	Macro(NOISE_TITLE);

STOP=\
	Timer(NOISE_TITLE,cancel);\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.Stop);\
	Set(title,);\
	Set(status,);
	
NEXT=\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.Next);\
	Macro(NOISE_TITLE);
	
PAUSE=\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Player.Pause);

RUN_IF_NEED=\
	Exec($(CfgDir)/Utils/run-or-switch-to.sh noise);
	
RUN_OR_QUIT=\
	Make(var,noise_run_or_quit,\
	     P=`pgrep noise`;\
	     if [ "x$P" = "x" ]; then echo NOISE_TITLE; noise & else pkill noise; echo NOISE_CANCEL; fi);\
	Macro($(noise_run_or_quit));

NOISE_CANCEL=\
	Timer(NOISE_TITLE,cancel);

NOISE_START=\
	Macro(NOISE_TITLE);\
	Timer(NOISE_TITLE,$(UpdateTimeout),0);

APP_QUIT=\
	Timer(NOISE_TITLE,cancel);\
	Exec($(noise_cmd) org.mpris.MediaPlayer2.Quit);\
	Exec(pkill noise);

NOISE_TITLE_GENERAL=\
	Make(var,art,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:Metadata|awk '/.*xesam.albumArtist.*/{getline;getline;print}'|cut -f 2 -d '"');\
	Make(var,tit,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:Metadata|awk '/.*xesam.title.*/{getline;print}'|cut -f 2 -d '"');\
	Set(title,$(art) - $(tit));

NOISE_STATUS=\
	Make(var,noise_status,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:PlaybackStatus|grep variant|cut -f 2 -d '"');

NOISE_GETPOS=\
	Make(var,noise_pos,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:Position|grep variant|tr -s ' '|cut -f 4 -d ' ');

%qdbus org.mpris.MediaPlayer2.noise /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus
APP_PLAY_STATUS=\
	Make(var,noise_status,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:PlaybackStatus|tail -1|cut -d '"' -f 2);\
	Make(var,app_status,if [ "x$(noise_status)" = "xPlaying" ]; then echo playing; else echo paused; fi);

Shuffle=\
	Make(var,shuffle_val,\
	     P=`$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:Shuffle|grep variant|tr -s ' '|cut -f 4 -d ' '`;\
	     if [ "x$P" = "xtrue" ]; then echo false; else echo true; fi);\
        Exec($(noise_cmd_set_prop) string:org.mpris.MediaPlayer2.Player string:Shuffle variant:boolean:$(shuffle_val));

Repeat=\
	Make(var,repeat_val,\
	     P=`$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:LoopStatus|grep variant|cut -f 2 -d '"'`;\
	     if [ "x$P" = "xNone" ]; then echo Playlist; else echo None; fi);\
        Exec($(noise_cmd_set_prop) string:org.mpris.MediaPlayer2.Player string:LoopStatus variant:string:$(repeat_val));

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

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

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

[ModeEnd]

[Mode]=noise_3x4 : noise_general,default_player

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

APP_POST_SETUP=\
	Make(var,noise_layout,by_value,3x4);\
	Macro(NOISE_CHECK_TIMER);

NOISE_TITLE=\
	Macro(NOISE_TITLE_GENERAL);

[ModeEnd]

[Mode]=noise_cover

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

APP_COVER=\
	Make(var,media_file,$(noise_cmd_get_prop) string:org.mpris.MediaPlayer2.Player string:Metadata|awk '/.*artUrl.*/{getline;print}'|cut -f 2 -d '"'|cut -f 2 -d ':'|sed 's/"$//;s/^\/\/\//\//');\
	Macro(DEFAULT_METADATA_COVER);

[ModeEnd]

[Mode]=noise_7x1 : noise_general,default_player_7x1,noise_cover

APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Mute,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);

APP_POST_SETUP=\
	Make(var,noise_layout,by_value,7x1);\
	Macro(NOISE_CHECK_TIMER);

[ModeEnd]

[Mode]=noise_5x1 : noise_general,default_player_5x1,noise_cover

APP_MENU=\
	Set(menu,replace,Shuffle,Repeat,Mute,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);

APP_POST_SETUP=\
	Make(var,noise_layout,by_value,5x1);
	
APP_TITLE=\
	Macro(NOISE_TITLE);

PLAY=\
	Macro(PLAYPAUSE);

[ModeEnd]

[End]
