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.

130 lines
3.2KB

  1. /*
  2. * JUCE LV2 *.ttl generator
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #define TTL_GENERATOR_WINDOWS
  9. #else
  10. #include <dlfcn.h>
  11. #endif
  12. #ifndef nullptr
  13. #define nullptr (0)
  14. #endif
  15. // Replicating some of the LV2 header here so that we don't have to set up any
  16. // custom include paths for this file.
  17. // Normally this would be a bad idea, but the LV2 API has to keep these definitions
  18. // in order to remain backwards-compatible.
  19. extern "C" {
  20. typedef struct LV2_Descriptor {
  21. const void* a;
  22. const void* b;
  23. const void* c;
  24. const void* d;
  25. const void* e;
  26. const void* f;
  27. const void* g;
  28. const void* (*extension_data)(const char* uri);
  29. } LV2_Descriptor;
  30. typedef struct RecallFeature {
  31. int (*doRecall)(const char*);
  32. } RecallFeature;
  33. }
  34. typedef void (*TTL_Generator_Function)(const char* basename);
  35. typedef const LV2_Descriptor* (*LV2_Descriptor_Function)(unsigned index);
  36. int main(int argc, char* argv[])
  37. {
  38. if (argc != 2)
  39. {
  40. printf("usage: %s /path/to/plugin-DLL\n", argv[0]);
  41. return 1;
  42. }
  43. #ifdef TTL_GENERATOR_WINDOWS
  44. const HMODULE handle = LoadLibraryA(argv[1]);
  45. #else
  46. void* const handle = dlopen(argv[1], RTLD_LAZY);
  47. #endif
  48. if (! handle)
  49. {
  50. #ifdef TTL_GENERATOR_WINDOWS
  51. printf("Failed to open plugin DLL\n");
  52. #else
  53. printf("Failed to open plugin DLL, error was:\n%s\n", dlerror());
  54. #endif
  55. return 2;
  56. }
  57. #ifdef TTL_GENERATOR_WINDOWS
  58. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)GetProcAddress(handle, "lv2_generate_ttl");
  59. const LV2_Descriptor_Function lv2Fn = (LV2_Descriptor_Function)GetProcAddress(handle, "lv2_descriptor");
  60. #else
  61. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)dlsym(handle, "lv2_generate_ttl");
  62. const LV2_Descriptor_Function lv2Fn = (LV2_Descriptor_Function)dlsym(handle, "lv2_descriptor");
  63. #endif
  64. if (ttlFn != NULL)
  65. {
  66. char basename[strlen(argv[1])+1];
  67. #ifdef TTL_GENERATOR_WINDOWS
  68. if (char* base2 = strrchr(argv[1], '\\'))
  69. #else
  70. if (char* base2 = strrchr(argv[1], '/'))
  71. #endif
  72. {
  73. strcpy(basename, base2+1);
  74. basename[strrchr(base2, '.')-base2-1] = '\0';
  75. }
  76. else
  77. {
  78. #ifdef TTL_GENERATOR_WINDOWS
  79. // Fix when running through wine
  80. if (char* base2 = strrchr(argv[1], '/'))
  81. {
  82. strcpy(basename, base2+1);
  83. basename[strrchr(base2, '.')-base2-1] = '\0';
  84. }
  85. else
  86. #endif
  87. {
  88. strcpy(basename, argv[1]);
  89. }
  90. }
  91. printf("Generate ttl data for '%s', basename: '%s'\n", argv[1], basename);
  92. ttlFn(basename);
  93. }
  94. else if (lv2Fn != nullptr)
  95. {
  96. if (const LV2_Descriptor* const descriptor = lv2Fn(0))
  97. if (const RecallFeature* const recallFeature = (const RecallFeature*)descriptor->extension_data("https://lv2-extensions.juce.com/turtle_recall"))
  98. recallFeature->doRecall(argv[1]);
  99. }
  100. else
  101. {
  102. printf("Failed to find 'lv2_generate_ttl' function\n");
  103. }
  104. #ifdef TTL_GENERATOR_WINDOWS
  105. FreeLibrary(handle);
  106. #else
  107. dlclose(handle);
  108. #endif
  109. return 0;
  110. }