From 8ac1a382131d88ebc37f790c6515da89dd5d62cd Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 16 Sep 2012 04:33:52 +0200 Subject: [PATCH] bindings: add support for action "require_screen" --- doc/bindings | 7 +++++++ src/Makefile.am | 1 + src/bindings.cpp | 10 ++++++++++ src/macro_utilities.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/macro_utilities.h | 21 ++++++++++++++++----- 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/macro_utilities.cpp diff --git a/doc/bindings b/doc/bindings index 07c32b7b..d6c231cf 100644 --- a/doc/bindings +++ b/doc/bindings @@ -88,6 +88,13 @@ ## before we stuff characters into input queue, so if condition ## is not met, whole chain is aborted and we're fine. ## +## - require_screen "screen" - checks whether given screen is +## currently active. accepted values: browser, clock, help, +## media_library, outputs, playlist, playlist_editor, +## search_engine, tag_editor, visualizer, last_fm, lyrics, +## selected_items_adder, server_info, song_info, +## sort_playlist_dialog, tiny_tag_editor. +## ## Note: Both 'backspace' and 'backspace_2' are used because some ## terminals interpret backspace using keycode of 'backspace' ## and some the one of 'backspace_2'. You can get away with diff --git a/src/Makefile.am b/src/Makefile.am index 2efb5a32..4b4fc919 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,7 @@ ncmpcpp_SOURCES = \ lastfm_service.cpp \ lyrics.cpp \ lyrics_fetcher.cpp \ + macro_utilities.cpp \ media_library.cpp \ mpdpp.cpp \ mutable_song.cpp \ diff --git a/src/bindings.cpp b/src/bindings.cpp index 16ddf594..f6e6c592 100644 --- a/src/bindings.cpp +++ b/src/bindings.cpp @@ -127,6 +127,16 @@ Action *parseActionLine(const std::string &line, F error) else error() << "empty argument passed to push_characters\n"; } + else if (action_name == "require_screen") + { + // require screen of given type + std::string arg = getEnclosedString(line, '"', '"', 0); + ScreenType screen_type = stringToScreenType(arg); + if (screen_type != ScreenType::Unknown) + result = new RequireScreen(screen_type); + else + error() << "unknown screen passed to require_screen: '" << arg << "'\n"; + } else if (action_name == "require_runnable") { // require that given action is runnable diff --git a/src/macro_utilities.cpp b/src/macro_utilities.cpp new file mode 100644 index 00000000..a2f69c90 --- /dev/null +++ b/src/macro_utilities.cpp @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (C) 2008-2012 by Andrzej Rybczak * + * electricityispower@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "global.h" +#include "macro_utilities.h" + +void PushCharacters::Run() +{ + for (auto it = m_queue.begin(); it != m_queue.end(); ++it) + (*m_window)->pushChar(*it); +} + +bool RequireRunnable::canBeRun() const +{ + return m_action->canBeRun(); +} + +bool RequireScreen::canBeRun() const +{ + return Global::myScreen->type() == m_screen_type; +} diff --git a/src/macro_utilities.h b/src/macro_utilities.h index 4ce0678a..966ab0de 100644 --- a/src/macro_utilities.h +++ b/src/macro_utilities.h @@ -23,6 +23,7 @@ #include #include "actions.h" +#include "screen_type.h" struct PushCharacters : public Action { @@ -30,10 +31,7 @@ struct PushCharacters : public Action : Action(aMacroUtility, ""), m_window(w), m_queue(queue) { } protected: - virtual void Run() { - for (auto it = m_queue.begin(); it != m_queue.end(); ++it) - (*m_window)->pushChar(*it); - } + virtual void Run(); private: NC::Window **m_window; @@ -46,11 +44,24 @@ struct RequireRunnable : public Action : Action(aMacroUtility, ""), m_action(action) { assert(action); } protected: - virtual bool canBeRun() const { return m_action->canBeRun(); } + virtual bool canBeRun() const; virtual void Run() { } private: Action *m_action; }; +struct RequireScreen : public Action +{ + RequireScreen(ScreenType screen_type) + : Action(aMacroUtility, ""), m_screen_type(screen_type) { } + +protected: + virtual bool canBeRun() const; + virtual void Run() { } + +private: + ScreenType m_screen_type; +}; + #endif // _MACRO_UTILITIES