Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.c 3.0KB

12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2012-2016 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "lilv_internal.h"
  15. LilvLib*
  16. lilv_lib_open(LilvWorld* world,
  17. const LilvNode* uri,
  18. const char* bundle_path,
  19. const LV2_Feature*const* features)
  20. {
  21. ZixTreeIter* i = NULL;
  22. const LilvLib key = {
  23. world, (LilvNode*)uri, (char*)bundle_path, NULL, NULL, NULL, 0
  24. };
  25. if (!zix_tree_find(world->libs, &key, &i)) {
  26. LilvLib* llib = (LilvLib*)zix_tree_get(i);
  27. ++llib->refs;
  28. return llib;
  29. }
  30. const char* const lib_uri = lilv_node_as_uri(uri);
  31. char* const lib_path = lilv_file_uri_parse(lib_uri, NULL);
  32. if (!lib_path) {
  33. return NULL;
  34. }
  35. dlerror();
  36. void* lib = dlopen(lib_path, RTLD_NOW);
  37. if (!lib) {
  38. LILV_ERRORF("Failed to open library %s (%s)\n", lib_path, dlerror());
  39. lilv_free(lib_path);
  40. return NULL;
  41. }
  42. LV2_Descriptor_Function df = (LV2_Descriptor_Function)
  43. lilv_dlfunc(lib, "lv2_descriptor");
  44. LV2_Lib_Descriptor_Function ldf = (LV2_Lib_Descriptor_Function)
  45. lilv_dlfunc(lib, "lv2_lib_descriptor");
  46. const LV2_Lib_Descriptor* desc = NULL;
  47. if (ldf) {
  48. desc = ldf(bundle_path, features);
  49. if (!desc) {
  50. LILV_ERRORF("Call to %s:lv2_lib_descriptor failed\n", lib_path);
  51. dlclose(lib);
  52. lilv_free(lib_path);
  53. return NULL;
  54. }
  55. } else if (!df) {
  56. LILV_ERRORF("No `lv2_descriptor' or `lv2_lib_descriptor' in %s\n",
  57. lib_path);
  58. dlclose(lib);
  59. lilv_free(lib_path);
  60. return NULL;
  61. }
  62. lilv_free(lib_path);
  63. LilvLib* llib = (LilvLib*)malloc(sizeof(LilvLib));
  64. llib->world = world;
  65. llib->uri = lilv_node_duplicate(uri);
  66. llib->bundle_path = lilv_strdup(bundle_path);
  67. llib->lib = lib;
  68. llib->lv2_descriptor = df;
  69. llib->desc = desc;
  70. llib->refs = 1;
  71. zix_tree_insert(world->libs, llib, NULL);
  72. return llib;
  73. }
  74. const LV2_Descriptor*
  75. lilv_lib_get_plugin(LilvLib* lib, uint32_t index)
  76. {
  77. if (lib->lv2_descriptor) {
  78. return lib->lv2_descriptor(index);
  79. } else if (lib->desc) {
  80. return lib->desc->get_plugin(lib->desc->handle, index);
  81. }
  82. return NULL;
  83. }
  84. void
  85. lilv_lib_close(LilvLib* lib)
  86. {
  87. if (--lib->refs == 0) {
  88. dlclose(lib->lib);
  89. ZixTreeIter* i = NULL;
  90. if (lib->world->libs && !zix_tree_find(lib->world->libs, lib, &i)) {
  91. zix_tree_remove(lib->world->libs, i);
  92. }
  93. lilv_node_free(lib->uri);
  94. free(lib->bundle_path);
  95. free(lib);
  96. }
  97. }