diff --git a/NEWS b/NEWS index a1eca260..9f6e4694 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,5 @@ ncmpcpp-0.8 (????-??-??) +* Configuration variable 'execute_on_player_state_change' was added. ncmpcpp 0.7.4 (2016-04-17) * Fetching lyrics from lyricwiki.org was fixed. diff --git a/doc/config b/doc/config index 29d4d642..3afbb760 100644 --- a/doc/config +++ b/doc/config @@ -286,6 +286,15 @@ ## #execute_on_song_change = "" # +## +## Note: Custom command that will be executed each +## time player state changes. The environment variable +## MPD_PLAYER_STATE is set to the current state (either +## unknown, play, pause, or stop) for its duration. +## +# +#execute_on_player_state_change = "" +# #playlist_show_mpd_host = no # #playlist_show_remaining_time = no diff --git a/doc/ncmpcpp.1 b/doc/ncmpcpp.1 index 17840ee0..33c94ea5 100644 --- a/doc/ncmpcpp.1 +++ b/doc/ncmpcpp.1 @@ -152,6 +152,11 @@ Format for songs' list displayed in columns. .B execute_on_song_change = COMMAND Shell command to execute on song change. .TP +.B execute_on_player_state_change = COMMAND +Shell command to execute on player state change. The environment variable +.B MPD_PLAYER_STATE +is set to the current state (either unknown, play, pause, or stop) for its duration. +.TP .B playlist_show_mpd_host = yes/no If enabled, current MPD host will be shown in playlist. .TP diff --git a/src/settings.cpp b/src/settings.cpp index 6f3daf52..bbc1e382 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -375,6 +375,9 @@ bool Configuration::read(const std::vector &config_paths, bool igno p.add("execute_on_song_change", assign_default( execute_on_song_change, "" )); + p.add("execute_on_player_state_change", assign_default( + execute_on_player_state_change, "" + )); p.add("playlist_show_mpd_host", yes_no( playlist_show_mpd_host, false )); diff --git a/src/settings.h b/src/settings.h index 5b244298..e468067b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -76,6 +76,7 @@ struct Configuration std::string external_editor; std::string system_encoding; std::string execute_on_song_change; + std::string execute_on_player_state_change; std::string lastfm_preferred_language; std::wstring progressbar; std::wstring visualizer_chars; @@ -199,4 +200,3 @@ struct Configuration extern Configuration Config; #endif // NCMPCPP_SETTINGS_H - diff --git a/src/status.cpp b/src/status.cpp index 54c68efa..3965eab1 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -246,7 +246,7 @@ void Status::update(int event) m_playlist_length = st.playlistLength(); m_total_time = st.totalTime(); m_volume = st.volume(); - + if (event & MPD_IDLE_DATABASE) Changes::database(); if (event & MPD_IDLE_STORED_PLAYLIST) @@ -432,10 +432,10 @@ void Status::Changes::playlist(unsigned previous_version) else // otherwise just add it to playlist myPlaylist->main().addItem(std::move(*s)); } - + myPlaylist->reloadTotalLength(); myPlaylist->reloadRemaining(); - + if (isVisible(myBrowser)) markSongsInPlaylist(myBrowser->main()); if (isVisible(mySearcher)) @@ -473,6 +473,24 @@ void Status::Changes::database() void Status::Changes::playerState() { + if (!Config.execute_on_player_state_change.empty()) + { + auto stateToEnv = [](MPD::PlayerState st) -> const char * { + switch (st) + { + case MPD::psPlay: return "play"; + case MPD::psStop: return "stop"; + case MPD::psPause: return "pause"; + case MPD::psUnknown: return "unknown"; + } + throw std::logic_error("unreachable"); + }; + GNUC_UNUSED int res; + setenv("MPD_PLAYER_STATE", stateToEnv(m_player_state), 1); + res = system(Config.execute_on_player_state_change.c_str()); + unsetenv("MPD_PLAYER_STATE"); + } + switch (m_player_state) { case MPD::psPlay: @@ -503,7 +521,7 @@ void Status::Changes::playerState() default: break; } - + std::string state = playerStateToString(m_player_state); if (Config.design == Design::Alternative) { @@ -518,7 +536,7 @@ void Status::Changes::playerState() else *wFooter << NC::Format::Bold << state << NC::Format::NoBold; } - + // needed for immediate display after starting // player from stopped state or seeking elapsedTime(false); @@ -680,7 +698,7 @@ void Status::Changes::flags() { if (!Config.header_visibility && Config.design == Design::Classic) return; - + std::string switch_state; switch (Config.design) { @@ -739,7 +757,7 @@ void Status::Changes::mixer() { if (!Config.display_volume_level || (!Config.header_visibility && Config.design == Design::Classic)) return; - + switch (Config.design) { case Design::Classic: