1 /* ncmpc (Ncurses MPD Client) 2 * (c) 2004-2020 The Music Player Daemon Project 3 * Project homepage: http://musicpd.org 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 #ifndef PLUGIN_H 21 #define PLUGIN_H 22 23 #include <vector> 24 #include <string> 25 26 class EventLoop; 27 28 /** 29 * When a plugin cycle is finished, one method of this class is called. In any 30 * case, plugin_stop() has to be called to free all memory. 31 */ 32 class PluginResponseHandler { 33 public: 34 /** 35 * @param plugin_name the name of the plugin which succeeded 36 * @param result the plugin's output (stdout) 37 */ 38 virtual void OnPluginSuccess(const char *plugin_name, 39 std::string result) noexcept = 0; 40 41 virtual void OnPluginError(std::string error) noexcept = 0; 42 }; 43 44 /** 45 * A list of registered plugins. 46 */ 47 struct PluginList { 48 std::vector<std::string> plugins; 49 }; 50 51 /** 52 * This object represents a cycle through all available plugins, until 53 * a plugin returns a positive result. 54 */ 55 struct PluginCycle; 56 57 /** 58 * Load all plugins (executables) in a directory. 59 */ 60 PluginList 61 plugin_list_load_directory(const char *path) noexcept; 62 63 /** 64 * Run plugins in this list, until one returns success (or until the 65 * plugin list is exhausted). 66 * 67 * @param list the plugin list 68 * @param args nullptr terminated command line arguments passed to the 69 * plugin programs; they must remain valid while the plugin runs 70 * @param handler the handler which will be called when a result is 71 * available 72 */ 73 PluginCycle * 74 plugin_run(EventLoop &event_loop, 75 PluginList *list, const char *const*args, 76 PluginResponseHandler &handler) noexcept; 77 78 /** 79 * Stops the plugin cycle and frees resources. This can be called to 80 * abort the current cycle, or after the plugin_callback_t has been 81 * invoked. 82 */ 83 void 84 plugin_stop(PluginCycle *invocation) noexcept; 85 86 #endif 87