PluginIndex.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * PluginHost.h
  3. *
  4. * Created on: 17 déc. 2023
  5. * Author: fanch
  6. */
  7. #ifndef SRC_CORE_PLUGININDEX_H_
  8. #define SRC_CORE_PLUGININDEX_H_
  9. #include <ladspa.h>
  10. #include <string>
  11. #include <vector>
  12. #include <utility>
  13. #include <cstdio>
  14. #include <dlfcn.h>
  15. struct DllEntry {
  16. std::string path;
  17. int count;
  18. void* ptr;
  19. };
  20. typedef std::vector<DllEntry> DllTable;
  21. class DllManager;
  22. class DllManager {
  23. public:
  24. DllManager(){
  25. }
  26. virtual ~DllManager(){
  27. }
  28. void* _open(const std::string& path){
  29. for(size_t i=0; i<m_table.size(); i++){
  30. if(m_table[i].path == path){
  31. m_table[i].count++;
  32. return m_table[i].ptr;
  33. }
  34. }
  35. DllEntry entry;
  36. entry.path = path;
  37. entry.count = 1;
  38. entry.ptr = dlopen(path.c_str(), RTLD_LAZY);
  39. m_table.push_back(entry);
  40. return entry.ptr;
  41. }
  42. void _close(void* ptr){
  43. for(size_t i=0; i<m_table.size(); i++){
  44. if(m_table[i].ptr == ptr){
  45. m_table[i].count--;
  46. if(m_table[i].count<=0){
  47. printf("Close %s\n", m_table[i].path.c_str());
  48. dlclose(m_table[i].ptr);
  49. m_table.erase(m_table.begin()+i);
  50. }
  51. return;
  52. }
  53. }
  54. }
  55. static void* open(const std::string& path){
  56. return DllManager::m_instance->_open(path);
  57. }
  58. static void close(void* ptr){
  59. return DllManager::m_instance->_close(ptr);
  60. }
  61. protected:
  62. DllTable m_table;
  63. static DllManager* m_instance;
  64. };
  65. struct PluginPort {
  66. PluginPort() : index(0), is_input(false), is_output(false),
  67. is_audio(false), is_control(false){
  68. }
  69. virtual ~PluginPort(){
  70. }
  71. std::string name;
  72. int index;
  73. bool is_input;
  74. bool is_output;
  75. bool is_audio;
  76. bool is_control;
  77. std::string& to_string(std::string& out) const {
  78. out += "[" + name +"] ";
  79. if(is_input) out+="AUDIO ";
  80. if(is_output) out+="OUTPUT ";
  81. if(is_audio) out+="AUDIO ";
  82. if(is_control) out+="CONTROL ";
  83. out+="\n";
  84. return out;
  85. }
  86. };
  87. typedef std::vector<PluginPort> PluginPorts;
  88. class PluginEntry {
  89. public:
  90. PluginEntry(const std::string& plugin_path){
  91. set_path(plugin_path);
  92. }
  93. PluginEntry(){
  94. _clear();
  95. }
  96. PluginEntry(const PluginEntry& other){
  97. m_loaded = other.m_loaded;
  98. m_name = other.m_name;
  99. m_path = other.m_path;
  100. m_error = other.m_error;
  101. m_is_error = other.m_is_error;
  102. m_label = other.m_label;
  103. m_id = other.m_id;
  104. m_ports = other.m_ports;
  105. }
  106. virtual ~PluginEntry(){
  107. }
  108. void set_path(const std::string& path){
  109. if(path == m_path) return;
  110. m_loaded = false;
  111. this->m_path = path;
  112. }
  113. void load();
  114. const std::string& path() const{
  115. return m_path;
  116. }
  117. std::string& to_string(std::string& out) const {
  118. char tmp[1024];
  119. sprintf(tmp, "=== [%s] %s%s===\n id=%d\n name=%s\n label=%s\n ports:\n",
  120. m_path.c_str(),
  121. (m_is_error?"Erreur: ":""),
  122. (m_is_error?m_error.c_str():""),
  123. m_id, m_name.c_str(), m_label.c_str());
  124. out+=tmp;
  125. for(size_t i=0; i<m_ports.size(); i++){
  126. out+=" ";
  127. m_ports[i].to_string(out);
  128. }
  129. return out;
  130. }
  131. void print() const {
  132. std::string out;
  133. to_string(out);
  134. printf("%s\n", out.c_str());
  135. }
  136. const PluginPorts& ports() const {
  137. return m_ports;
  138. }
  139. protected:
  140. void _clear(){
  141. m_loaded = false;
  142. }
  143. void _read_info(){
  144. m_loaded = true;
  145. }
  146. bool m_loaded;
  147. std::string m_path;
  148. std::string m_name;
  149. std::string m_error;
  150. bool m_is_error;
  151. std::string m_label;
  152. long m_id;
  153. PluginPorts m_ports;
  154. friend class PluginInstance;
  155. };
  156. class PluginIndex {
  157. public:
  158. PluginIndex(){
  159. }
  160. virtual ~PluginIndex(){
  161. }
  162. void add_path(const std::string& path){
  163. m_plugin_pathes.push_back(path);
  164. }
  165. void list() const;
  166. void update();
  167. PluginEntry& load_from_path(const std::string& path);
  168. void print() const{
  169. printf("%d plugins\n", m_plugins.size());
  170. for(size_t i=0; i<m_plugins.size(); i++){
  171. printf("Plugin '%s'\n", m_plugins[i].path().c_str());
  172. }
  173. }
  174. protected:
  175. void _update_dir(const std::string& dir);
  176. std::vector<std::string> m_plugin_pathes;
  177. std::vector<PluginEntry> m_plugins;
  178. };
  179. #endif /* SRC_CORE_PLUGININDEX_H_ */