123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /*
- * PluginHost.h
- *
- * Created on: 17 déc. 2023
- * Author: fanch
- */
- #ifndef SRC_CORE_PLUGININDEX_H_
- #define SRC_CORE_PLUGININDEX_H_
- #include <ladspa.h>
- #include <string>
- #include <vector>
- #include <utility>
- #include <cstdio>
- #include <dlfcn.h>
- struct DllEntry {
- std::string path;
- int count;
- void* ptr;
- };
- typedef std::vector<DllEntry> DllTable;
- class DllManager;
- class DllManager {
- public:
- DllManager(){
- }
- virtual ~DllManager(){
- }
- void* _open(const std::string& path){
- for(size_t i=0; i<m_table.size(); i++){
- if(m_table[i].path == path){
- m_table[i].count++;
- return m_table[i].ptr;
- }
- }
- DllEntry entry;
- entry.path = path;
- entry.count = 1;
- entry.ptr = dlopen(path.c_str(), RTLD_LAZY);
- m_table.push_back(entry);
- return entry.ptr;
- }
- void _close(void* ptr){
- for(size_t i=0; i<m_table.size(); i++){
- if(m_table[i].ptr == ptr){
- m_table[i].count--;
- if(m_table[i].count<=0){
- printf("Close %s\n", m_table[i].path.c_str());
- dlclose(m_table[i].ptr);
- m_table.erase(m_table.begin()+i);
- }
- return;
- }
- }
- }
- static void* open(const std::string& path){
- return DllManager::m_instance->_open(path);
- }
- static void close(void* ptr){
- return DllManager::m_instance->_close(ptr);
- }
- protected:
- DllTable m_table;
- static DllManager* m_instance;
- };
- struct PluginPort {
- PluginPort() : index(0), is_input(false), is_output(false),
- is_audio(false), is_control(false){
- }
- virtual ~PluginPort(){
- }
- std::string name;
- int index;
- bool is_input;
- bool is_output;
- bool is_audio;
- bool is_control;
- std::string& to_string(std::string& out) const {
- out += "[" + name +"] ";
- if(is_input) out+="AUDIO ";
- if(is_output) out+="OUTPUT ";
- if(is_audio) out+="AUDIO ";
- if(is_control) out+="CONTROL ";
- out+="\n";
- return out;
- }
- };
- typedef std::vector<PluginPort> PluginPorts;
- class PluginEntry {
- public:
- PluginEntry(const std::string& plugin_path){
- set_path(plugin_path);
- }
- PluginEntry(){
- _clear();
- }
- PluginEntry(const PluginEntry& other){
- m_loaded = other.m_loaded;
- m_name = other.m_name;
- m_path = other.m_path;
- m_error = other.m_error;
- m_is_error = other.m_is_error;
- m_label = other.m_label;
- m_id = other.m_id;
- m_ports = other.m_ports;
- }
- virtual ~PluginEntry(){
- }
- void set_path(const std::string& path){
- if(path == m_path) return;
- m_loaded = false;
- this->m_path = path;
- }
- void load();
- const std::string& path() const{
- return m_path;
- }
- std::string& to_string(std::string& out) const {
- char tmp[1024];
- sprintf(tmp, "=== [%s] %s%s===\n id=%d\n name=%s\n label=%s\n ports:\n",
- m_path.c_str(),
- (m_is_error?"Erreur: ":""),
- (m_is_error?m_error.c_str():""),
- m_id, m_name.c_str(), m_label.c_str());
- out+=tmp;
- for(size_t i=0; i<m_ports.size(); i++){
- out+=" ";
- m_ports[i].to_string(out);
- }
- return out;
- }
- void print() const {
- std::string out;
- to_string(out);
- printf("%s\n", out.c_str());
- }
- const PluginPorts& ports() const {
- return m_ports;
- }
- protected:
- void _clear(){
- m_loaded = false;
- }
- void _read_info(){
- m_loaded = true;
- }
- bool m_loaded;
- std::string m_path;
- std::string m_name;
- std::string m_error;
- bool m_is_error;
- std::string m_label;
- long m_id;
- PluginPorts m_ports;
- friend class PluginInstance;
- };
- class PluginIndex {
- public:
- PluginIndex(){
- }
- virtual ~PluginIndex(){
- }
- void add_path(const std::string& path){
- m_plugin_pathes.push_back(path);
- }
- void list() const;
- void update();
- PluginEntry& load_from_path(const std::string& path);
- void print() const{
- printf("%d plugins\n", m_plugins.size());
- for(size_t i=0; i<m_plugins.size(); i++){
- printf("Plugin '%s'\n", m_plugins[i].path().c_str());
- }
- }
- protected:
- void _update_dir(const std::string& dir);
- std::vector<std::string> m_plugin_pathes;
- std::vector<PluginEntry> m_plugins;
- };
- #endif /* SRC_CORE_PLUGININDEX_H_ */
|