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.

1228 lines
46KB

  1. /*
  2. * Carla Bridge UI, LV2 version
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaBridgeUI.hpp"
  18. #include "CarlaLibUtils.hpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "LinkedList.hpp"
  22. #include "juce_core.h"
  23. #define URI_CARLA_ATOM_WORKER "http://kxstudio.sf.net/ns/carla/atomWorker"
  24. using juce::File;
  25. CARLA_BRIDGE_START_NAMESPACE
  26. // -----------------------------------------------------
  27. static int gBufferSize = 1024;
  28. static double gSampleRate = 44100.0;
  29. // Maximum default buffer size
  30. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  31. // LV2 URI Map Ids
  32. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  33. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  34. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  35. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  36. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  37. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  38. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  39. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  40. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  41. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  42. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  43. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  44. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  45. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  46. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  50. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  51. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  52. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  53. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  56. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  57. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  58. const uint32_t CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH = 26;
  59. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 27;
  60. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 28;
  61. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 29;
  62. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 30;
  63. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 31;
  64. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 32; // base type
  65. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 33; // values
  66. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 34;
  67. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 35;
  68. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 36;
  69. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 37;
  70. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 38;
  71. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 39;
  72. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 40;
  73. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 41;
  74. const uint32_t CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT = 42;
  75. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 43;
  76. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 44;
  77. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 45;
  78. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 46;
  79. const uint32_t CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID = 47;
  80. const uint32_t CARLA_URI_MAP_ID_COUNT = 48;
  81. // LV2 Feature Ids
  82. const uint32_t kFeatureIdLogs = 0;
  83. const uint32_t kFeatureIdOptions = 1;
  84. const uint32_t kFeatureIdPrograms = 2;
  85. const uint32_t kFeatureIdStateMakePath = 3;
  86. const uint32_t kFeatureIdStateMapPath = 4;
  87. const uint32_t kFeatureIdUriMap = 5;
  88. const uint32_t kFeatureIdUridMap = 6;
  89. const uint32_t kFeatureIdUridUnmap = 7;
  90. const uint32_t kFeatureIdUiIdleInterface = 8;
  91. const uint32_t kFeatureIdUiFixedSize = 9;
  92. const uint32_t kFeatureIdUiMakeResident = 10;
  93. const uint32_t kFeatureIdUiNoUserResize = 11;
  94. const uint32_t kFeatureIdUiParent = 12;
  95. const uint32_t kFeatureIdUiPortMap = 13;
  96. const uint32_t kFeatureIdUiPortSubscribe = 14;
  97. const uint32_t kFeatureIdUiResize = 15;
  98. const uint32_t kFeatureIdUiTouch = 16;
  99. const uint32_t kFeatureCount = 17;
  100. // -------------------------------------------------------------------------
  101. struct Lv2PluginOptions {
  102. enum OptIndex {
  103. MaxBlockLenth = 0,
  104. MinBlockLenth,
  105. NominalBlockLenth,
  106. SequenceSize,
  107. SampleRate,
  108. FrontendWinId,
  109. //WindowTitle,
  110. Null,
  111. Count
  112. };
  113. int maxBufferSize;
  114. int minBufferSize;
  115. int nominalBufferSize;
  116. int sequenceSize;
  117. double sampleRate;
  118. int64_t frontendWinId;
  119. const char* windowTitle;
  120. LV2_Options_Option opts[Count];
  121. Lv2PluginOptions() noexcept
  122. : maxBufferSize(0),
  123. minBufferSize(0),
  124. nominalBufferSize(0),
  125. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  126. sampleRate(0.0),
  127. frontendWinId(0),
  128. windowTitle(nullptr)
  129. {
  130. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  131. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  132. optMaxBlockLenth.subject = 0;
  133. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  134. optMaxBlockLenth.size = sizeof(int);
  135. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  136. optMaxBlockLenth.value = &maxBufferSize;
  137. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  138. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  139. optMinBlockLenth.subject = 0;
  140. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  141. optMinBlockLenth.size = sizeof(int);
  142. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  143. optMinBlockLenth.value = &minBufferSize;
  144. LV2_Options_Option& optNominalBlockLenth(opts[NominalBlockLenth]);
  145. optNominalBlockLenth.context = LV2_OPTIONS_INSTANCE;
  146. optNominalBlockLenth.subject = 0;
  147. optNominalBlockLenth.key = CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  148. optNominalBlockLenth.size = sizeof(int);
  149. optNominalBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  150. optNominalBlockLenth.value = &nominalBufferSize;
  151. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  152. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  153. optSequenceSize.subject = 0;
  154. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  155. optSequenceSize.size = sizeof(int);
  156. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  157. optSequenceSize.value = &sequenceSize;
  158. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  159. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  160. optSampleRate.subject = 0;
  161. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  162. optSampleRate.size = sizeof(double);
  163. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  164. optSampleRate.value = &sampleRate;
  165. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  166. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  167. optFrontendWinId.subject = 0;
  168. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  169. optFrontendWinId.size = sizeof(int64_t);
  170. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  171. optFrontendWinId.value = &frontendWinId;
  172. /*
  173. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  174. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  175. optWindowTitle.subject = 0;
  176. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  177. optWindowTitle.size = 0;
  178. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  179. optWindowTitle.value = nullptr;
  180. */
  181. LV2_Options_Option& optNull(opts[Null]);
  182. optNull.context = LV2_OPTIONS_INSTANCE;
  183. optNull.subject = 0;
  184. optNull.key = CARLA_URI_MAP_ID_NULL;
  185. optNull.size = 0;
  186. optNull.type = CARLA_URI_MAP_ID_NULL;
  187. optNull.value = nullptr;
  188. }
  189. };
  190. // -------------------------------------------------------------------------
  191. class CarlaLv2Client : public CarlaBridgeUI
  192. {
  193. public:
  194. CarlaLv2Client()
  195. : CarlaBridgeUI(),
  196. fHandle(nullptr),
  197. fWidget(nullptr),
  198. fDescriptor(nullptr),
  199. fRdfDescriptor(nullptr),
  200. fRdfUiDescriptor(nullptr),
  201. fLv2Options(),
  202. fUiOptions(),
  203. fCustomURIDs(),
  204. fExt()
  205. {
  206. carla_zeroPointers(fFeatures, kFeatureCount+1);
  207. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  208. fCustomURIDs.append(nullptr);
  209. // ---------------------------------------------------------------
  210. // initialize options
  211. fLv2Options.minBufferSize = gBufferSize;
  212. fLv2Options.maxBufferSize = gBufferSize;
  213. fLv2Options.nominalBufferSize = gBufferSize;
  214. fLv2Options.sampleRate = gSampleRate;
  215. // ---------------------------------------------------------------
  216. // initialize features (part 1)
  217. LV2_Log_Log* const logFt = new LV2_Log_Log;
  218. logFt->handle = this;
  219. logFt->printf = carla_lv2_log_printf;
  220. logFt->vprintf = carla_lv2_log_vprintf;
  221. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  222. stateMakePathFt->handle = this;
  223. stateMakePathFt->path = carla_lv2_state_make_path;
  224. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  225. stateMapPathFt->handle = this;
  226. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  227. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  228. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  229. programsFt->handle = this;
  230. programsFt->program_changed = carla_lv2_program_changed;
  231. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  232. uriMapFt->callback_data = this;
  233. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  234. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  235. uridMapFt->handle = this;
  236. uridMapFt->map = carla_lv2_urid_map;
  237. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  238. uridUnmapFt->handle = this;
  239. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  240. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  241. uiPortMapFt->handle = this;
  242. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  243. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  244. uiResizeFt->handle = this;
  245. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  246. // ---------------------------------------------------------------
  247. // initialize features (part 2)
  248. for (uint32_t i=0; i < kFeatureCount; ++i)
  249. fFeatures[i] = new LV2_Feature;
  250. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  251. fFeatures[kFeatureIdLogs]->data = logFt;
  252. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  253. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  254. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  255. fFeatures[kFeatureIdPrograms]->data = programsFt;
  256. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  257. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  258. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  259. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  260. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  261. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  262. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  263. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  264. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  265. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  266. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  267. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  268. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  269. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  270. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  271. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  272. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  273. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  274. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  275. fFeatures[kFeatureIdUiParent]->data = nullptr;
  276. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  277. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  278. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  279. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  280. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  281. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  282. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  283. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  284. }
  285. ~CarlaLv2Client() override
  286. {
  287. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->cleanup != nullptr)
  288. {
  289. fDescriptor->cleanup(fHandle);
  290. fHandle = nullptr;
  291. }
  292. if (fRdfDescriptor != nullptr)
  293. {
  294. delete fRdfDescriptor;
  295. fRdfDescriptor = nullptr;
  296. }
  297. fRdfUiDescriptor = nullptr;
  298. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  299. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  300. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  301. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  302. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  303. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  304. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  305. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  306. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  307. for (uint32_t i=0; i < kFeatureCount; ++i)
  308. {
  309. if (fFeatures[i] != nullptr)
  310. {
  311. delete fFeatures[i];
  312. fFeatures[i] = nullptr;
  313. }
  314. }
  315. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin2(); it.valid(); it.next())
  316. {
  317. const char* const uri(it.getValue(nullptr));
  318. if (uri != nullptr)
  319. delete[] uri;
  320. }
  321. fCustomURIDs.clear();
  322. }
  323. // ---------------------------------------------------------------------
  324. // UI initialization
  325. bool init(const int argc, const char* argv[]) override
  326. {
  327. const char* pluginURI = argv[1];
  328. const char* uiURI = argv[2];
  329. // -----------------------------------------------------------------
  330. // load plugin
  331. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  332. lv2World.initIfNeeded(std::getenv("LV2_PATH"));
  333. //Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, uiBundle));
  334. //CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(), false);
  335. //CarlaString sBundle(bundleNode.as_uri());
  336. //if (! sBundle.endsWith("/"))
  337. // sBundle += "/";
  338. //lv2World.load_bundle(sBundle);
  339. // -----------------------------------------------------------------
  340. // get plugin from lv2_rdf (lilv)
  341. fRdfDescriptor = lv2_rdf_new(pluginURI, true);
  342. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  343. // -----------------------------------------------------------------
  344. // find requested UI
  345. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  346. {
  347. if (std::strcmp(fRdfDescriptor->UIs[i].URI, uiURI) == 0)
  348. {
  349. fRdfUiDescriptor = &fRdfDescriptor->UIs[i];
  350. break;
  351. }
  352. }
  353. CARLA_SAFE_ASSERT_RETURN(fRdfUiDescriptor != nullptr, false);
  354. // -----------------------------------------------------------
  355. // check if not resizable
  356. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  357. // embed UIs can only be resizable if they provide resize extension
  358. fUiOptions.isResizable = false;
  359. // TODO: put this trick into main carla
  360. for (uint32_t i=0; i < fRdfUiDescriptor->ExtensionCount; ++i)
  361. {
  362. carla_stdout("Test UI extension %s", fRdfUiDescriptor->Extensions[i]);
  363. if (std::strcmp(fRdfUiDescriptor->Extensions[i], LV2_UI__resize) == 0)
  364. {
  365. fUiOptions.isResizable = true;
  366. break;
  367. }
  368. }
  369. #endif
  370. for (uint32_t i=0; i < fRdfUiDescriptor->FeatureCount; ++i)
  371. {
  372. carla_stdout("Test UI feature %s", fRdfUiDescriptor->Features[i].URI);
  373. if (std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__fixedSize ) == 0 ||
  374. std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  375. {
  376. fUiOptions.isResizable = false;
  377. break;
  378. }
  379. }
  380. carla_stdout("Is resizable => %s", bool2str(fUiOptions.isResizable));
  381. // -----------------------------------------------------------------
  382. // init UI
  383. if (! CarlaBridgeUI::init(argc, argv))
  384. return false;
  385. // -----------------------------------------------------------------
  386. // open DLL
  387. if (! libOpen(fRdfUiDescriptor->Binary))
  388. {
  389. carla_stderr("Failed to load UI binary, error was:\n%s", libError());
  390. return false;
  391. }
  392. // -----------------------------------------------------------------
  393. // get DLL main entry
  394. const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)libSymbol("lv2ui_descriptor");
  395. if (ui_descFn == nullptr)
  396. return false;
  397. // -----------------------------------------------------------
  398. // get descriptor that matches URI
  399. uint32_t i = 0;
  400. while ((fDescriptor = ui_descFn(i++)))
  401. {
  402. if (std::strcmp(fDescriptor->URI, uiURI) == 0)
  403. break;
  404. }
  405. if (fDescriptor == nullptr)
  406. {
  407. carla_stderr("Failed to find UI descriptor");
  408. return false;
  409. }
  410. // -----------------------------------------------------------
  411. // initialize UI
  412. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  413. fFeatures[kFeatureIdUiParent]->data = fToolkit->getContainerId();
  414. #endif
  415. fHandle = fDescriptor->instantiate(fDescriptor, fRdfDescriptor->URI, fRdfUiDescriptor->Bundle, carla_lv2_ui_write_function, this, &fWidget, fFeatures);
  416. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  417. // -----------------------------------------------------------
  418. // check for known extensions
  419. if (fDescriptor->extension_data != nullptr)
  420. {
  421. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  422. fExt.programs = (const LV2_Programs_UI_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__UIInterface);
  423. fExt.idle = (const LV2UI_Idle_Interface*)fDescriptor->extension_data(LV2_UI__idleInterface);
  424. fExt.resize = (const LV2UI_Resize*)fDescriptor->extension_data(LV2_UI__resize);
  425. // check if invalid
  426. if (fExt.programs != nullptr && fExt.programs->select_program == nullptr)
  427. fExt.programs = nullptr;
  428. if (fExt.idle != nullptr && fExt.idle->idle == nullptr)
  429. fExt.idle = nullptr;
  430. if (fExt.resize != nullptr && fExt.resize->ui_resize == nullptr)
  431. fExt.resize = nullptr;
  432. }
  433. return true;
  434. }
  435. void idleUI() override
  436. {
  437. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  438. if (fHandle != nullptr && fExt.idle != nullptr)
  439. fExt.idle->idle(fHandle);
  440. #endif
  441. }
  442. // ---------------------------------------------------------------------
  443. // UI management
  444. void* getWidget() const noexcept override
  445. {
  446. return fWidget;
  447. }
  448. const Options& getOptions() const noexcept override
  449. {
  450. return fUiOptions;
  451. }
  452. // ---------------------------------------------------------------------
  453. // DSP Callbacks
  454. void dspParameterChanged(const uint32_t index, const float value) override
  455. {
  456. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  457. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  458. if (fDescriptor->port_event == nullptr)
  459. return;
  460. fDescriptor->port_event(fHandle, index, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  461. }
  462. void dspProgramChanged(const uint32_t) override
  463. {
  464. }
  465. void dspMidiProgramChanged(const uint32_t bank, const uint32_t program) override
  466. {
  467. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  468. if (fExt.programs == nullptr)
  469. return;
  470. fExt.programs->select_program(fHandle, bank, program);
  471. }
  472. void dspStateChanged(const char* const, const char* const) override
  473. {
  474. }
  475. void dspNoteReceived(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) override
  476. {
  477. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  478. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  479. if (fDescriptor->port_event == nullptr)
  480. return;
  481. LV2_Atom_MidiEvent midiEv;
  482. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  483. midiEv.atom.size = 3;
  484. midiEv.data[0] = uint8_t((onOff ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (channel & MIDI_CHANNEL_BIT));
  485. midiEv.data[1] = note;
  486. midiEv.data[2] = velocity;
  487. fDescriptor->port_event(fHandle, /* TODO */ 0, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  488. }
  489. void dspAtomReceived(const uint32_t portIndex, const LV2_Atom* const atom) override
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  492. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  493. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  494. if (fDescriptor->port_event == nullptr)
  495. return;
  496. fDescriptor->port_event(fHandle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  497. }
  498. void dspURIDReceived(const LV2_URID urid, const char* const uri)
  499. {
  500. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  501. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  502. fCustomURIDs.append(carla_strdup(uri));
  503. }
  504. void uiOptionsChanged(const double sampleRate, const bool useTheme, const bool useThemeColors, const char* const windowTitle, uintptr_t transientWindowId) override
  505. {
  506. carla_debug("CarlaLv2Client::uiOptionsChanged(%g, %s, %s, \"%s\", " P_UINTPTR ")", sampleRate, bool2str(useTheme), bool2str(useThemeColors), windowTitle, transientWindowId);
  507. fLv2Options.sampleRate = gSampleRate = sampleRate;
  508. fUiOptions.useTheme = useTheme;
  509. fUiOptions.useThemeColors = useThemeColors;
  510. fUiOptions.windowTitle = windowTitle;
  511. fUiOptions.transientWindowId = transientWindowId;
  512. }
  513. void uiResized(const uint width, const uint height) override
  514. {
  515. if (fHandle != nullptr && fExt.resize != nullptr)
  516. fExt.resize->ui_resize(fHandle, static_cast<int>(width), static_cast<int>(height));
  517. }
  518. // ---------------------------------------------------------------------
  519. LV2_URID getCustomURID(const char* const uri)
  520. {
  521. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  522. carla_debug("CarlaLv2Client::getCustomURID(\"%s\")", uri);
  523. LV2_URID urid = CARLA_URI_MAP_ID_NULL;
  524. for (uint32_t i=0, count=static_cast<uint32_t>(fCustomURIDs.count()); i<count; ++i)
  525. {
  526. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  527. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  528. {
  529. urid = i;
  530. break;
  531. }
  532. }
  533. if (urid == CARLA_URI_MAP_ID_NULL)
  534. {
  535. urid = static_cast<LV2_URID>(fCustomURIDs.count());
  536. fCustomURIDs.append(carla_strdup(uri));
  537. }
  538. if (isPipeRunning())
  539. writeLv2UridMessage(urid, uri);
  540. return urid;
  541. }
  542. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  543. {
  544. static const char* const sFallback = "urn:null";
  545. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, sFallback);
  546. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), sFallback);
  547. carla_debug("CarlaLv2Client::getCustomURIDString(%i)", urid);
  548. return fCustomURIDs.getAt(urid, sFallback);
  549. }
  550. // ---------------------------------------------------------------------
  551. void handleProgramChanged(const int32_t /*index*/)
  552. {
  553. if (isPipeRunning())
  554. writeConfigureMessage("reloadprograms", "");
  555. }
  556. uint32_t handleUiPortMap(const char* const symbol)
  557. {
  558. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  559. carla_debug("CarlaLv2Client::handleUiPortMap(\"%s\")", symbol);
  560. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  561. {
  562. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  563. return i;
  564. }
  565. return LV2UI_INVALID_PORT_INDEX;
  566. }
  567. int handleUiResize(const int width, const int height)
  568. {
  569. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, 1);
  570. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  571. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  572. carla_debug("CarlaLv2Client::handleUiResize(%i, %i)", width, height);
  573. fToolkit->setSize(static_cast<uint>(width), static_cast<uint>(height));
  574. return 0;
  575. }
  576. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  577. {
  578. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  579. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  580. carla_debug("CarlaLv2Client::handleUiWrite(%i, %i, %i, %p)", portIndex, bufferSize, format, buffer);
  581. if (format == 0)
  582. {
  583. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  584. const float value(*(const float*)buffer);
  585. if (isPipeRunning())
  586. writeControlMessage(portIndex, value);
  587. }
  588. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  589. {
  590. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  591. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  592. if (isPipeRunning())
  593. writeLv2AtomMessage(portIndex, atom);
  594. }
  595. else
  596. {
  597. carla_stdout("CarlaLv2Client::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", portIndex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  598. }
  599. }
  600. // ---------------------------------------------------------------------
  601. private:
  602. LV2UI_Handle fHandle;
  603. LV2UI_Widget fWidget;
  604. LV2_Feature* fFeatures[kFeatureCount+1];
  605. const LV2UI_Descriptor* fDescriptor;
  606. const LV2_RDF_Descriptor* fRdfDescriptor;
  607. const LV2_RDF_UI* fRdfUiDescriptor;
  608. Lv2PluginOptions fLv2Options;
  609. Options fUiOptions;
  610. LinkedList<const char*> fCustomURIDs;
  611. struct Extensions {
  612. const LV2_Options_Interface* options;
  613. const LV2_Programs_UI_Interface* programs;
  614. const LV2UI_Idle_Interface* idle;
  615. const LV2UI_Resize* resize;
  616. Extensions()
  617. : options(nullptr),
  618. programs(nullptr),
  619. idle(nullptr),
  620. resize(nullptr) {}
  621. } fExt;
  622. // -------------------------------------------------------------------
  623. // Logs Feature
  624. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  625. {
  626. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  627. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  628. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  629. #ifndef DEBUG
  630. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  631. return 0;
  632. #endif
  633. va_list args;
  634. va_start(args, fmt);
  635. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  636. va_end(args);
  637. return ret;
  638. }
  639. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  642. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  643. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  644. #ifndef DEBUG
  645. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  646. return 0;
  647. #endif
  648. int ret = 0;
  649. switch (type)
  650. {
  651. case CARLA_URI_MAP_ID_LOG_ERROR:
  652. std::fprintf(stderr, "\x1b[31m");
  653. ret = std::vfprintf(stderr, fmt, ap);
  654. std::fprintf(stderr, "\x1b[0m");
  655. break;
  656. case CARLA_URI_MAP_ID_LOG_NOTE:
  657. ret = std::vfprintf(stdout, fmt, ap);
  658. break;
  659. case CARLA_URI_MAP_ID_LOG_TRACE:
  660. #ifdef DEBUG
  661. std::fprintf(stdout, "\x1b[30;1m");
  662. ret = std::vfprintf(stdout, fmt, ap);
  663. std::fprintf(stdout, "\x1b[0m");
  664. #endif
  665. break;
  666. case CARLA_URI_MAP_ID_LOG_WARNING:
  667. ret = std::vfprintf(stderr, fmt, ap);
  668. break;
  669. default:
  670. break;
  671. }
  672. return ret;
  673. }
  674. // -------------------------------------------------------------------
  675. // Programs Feature
  676. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  677. {
  678. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  679. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  680. ((CarlaLv2Client*)handle)->handleProgramChanged(index);
  681. }
  682. // -------------------------------------------------------------------
  683. // State Feature
  684. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  685. {
  686. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  687. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  688. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  689. File file;
  690. if (File::isAbsolutePath(path))
  691. file = File(path);
  692. else
  693. file = File::getCurrentWorkingDirectory().getChildFile(path);
  694. file.getParentDirectory().createDirectory();
  695. return strdup(file.getFullPathName().toRawUTF8());
  696. }
  697. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  700. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  701. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  702. // may already be an abstract path
  703. if (! File::isAbsolutePath(absolute_path))
  704. return strdup(absolute_path);
  705. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  706. }
  707. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  708. {
  709. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  710. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  711. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  712. // may already be an absolute path
  713. if (File::isAbsolutePath(abstract_path))
  714. return strdup(abstract_path);
  715. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  716. }
  717. // -------------------------------------------------------------------
  718. // URI-Map Feature
  719. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  720. {
  721. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  722. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  723. // unused
  724. (void)map;
  725. }
  726. // -------------------------------------------------------------------
  727. // URID Feature
  728. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  729. {
  730. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  731. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  732. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  733. // Atom types
  734. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  735. return CARLA_URI_MAP_ID_ATOM_BLANK;
  736. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  737. return CARLA_URI_MAP_ID_ATOM_BOOL;
  738. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  739. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  740. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  741. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  742. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  743. return CARLA_URI_MAP_ID_ATOM_EVENT;
  744. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  745. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  746. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  747. return CARLA_URI_MAP_ID_ATOM_INT;
  748. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  749. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  750. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  751. return CARLA_URI_MAP_ID_ATOM_LONG;
  752. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  753. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  754. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  755. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  756. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  757. return CARLA_URI_MAP_ID_ATOM_PATH;
  758. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  759. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  760. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  761. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  762. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  763. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  764. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  765. return CARLA_URI_MAP_ID_ATOM_SOUND;
  766. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  767. return CARLA_URI_MAP_ID_ATOM_STRING;
  768. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  769. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  770. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  771. return CARLA_URI_MAP_ID_ATOM_URI;
  772. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  773. return CARLA_URI_MAP_ID_ATOM_URID;
  774. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  775. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  776. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  777. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  778. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  779. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  780. // BufSize types
  781. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  782. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  783. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  784. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  785. if (std::strcmp(uri, LV2_BUF_SIZE__nominalBlockLength) == 0)
  786. return CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  787. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  788. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  789. // Log types
  790. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  791. return CARLA_URI_MAP_ID_LOG_ERROR;
  792. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  793. return CARLA_URI_MAP_ID_LOG_NOTE;
  794. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  795. return CARLA_URI_MAP_ID_LOG_TRACE;
  796. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  797. return CARLA_URI_MAP_ID_LOG_WARNING;
  798. // Time types
  799. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  800. return CARLA_URI_MAP_ID_TIME_POSITION;
  801. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  802. return CARLA_URI_MAP_ID_TIME_BAR;
  803. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  804. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  805. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  806. return CARLA_URI_MAP_ID_TIME_BEAT;
  807. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  808. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  809. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  810. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  811. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  812. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  813. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  814. return CARLA_URI_MAP_ID_TIME_FRAME;
  815. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  816. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  817. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  818. return CARLA_URI_MAP_ID_TIME_SPEED;
  819. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  820. return CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT;
  821. // Others
  822. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  823. return CARLA_URI_MAP_ID_MIDI_EVENT;
  824. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  825. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  826. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  827. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  828. // Custom
  829. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  830. return CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  831. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  832. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  833. // Custom types
  834. return ((CarlaLv2Client*)handle)->getCustomURID(uri);
  835. }
  836. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  837. {
  838. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  839. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  840. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  841. // Atom types
  842. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  843. return LV2_ATOM__Blank;
  844. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  845. return LV2_ATOM__Bool;
  846. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  847. return LV2_ATOM__Chunk;
  848. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  849. return LV2_ATOM__Double;
  850. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  851. return LV2_ATOM__Event;
  852. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  853. return LV2_ATOM__Float;
  854. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  855. return LV2_ATOM__Int;
  856. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  857. return LV2_ATOM__Literal;
  858. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  859. return LV2_ATOM__Long;
  860. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  861. return LV2_ATOM__Number;
  862. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  863. return LV2_ATOM__Object;
  864. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  865. return LV2_ATOM__Path;
  866. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  867. return LV2_ATOM__Property;
  868. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  869. return LV2_ATOM__Resource;
  870. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  871. return LV2_ATOM__Sequence;
  872. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  873. return LV2_ATOM__Sound;
  874. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  875. return LV2_ATOM__String;
  876. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  877. return LV2_ATOM__Tuple;
  878. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  879. return LV2_ATOM__URI;
  880. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  881. return LV2_ATOM__URID;
  882. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  883. return LV2_ATOM__Vector;
  884. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  885. return LV2_ATOM__atomTransfer;
  886. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  887. return LV2_ATOM__eventTransfer;
  888. // BufSize types
  889. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  890. return LV2_BUF_SIZE__maxBlockLength;
  891. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  892. return LV2_BUF_SIZE__minBlockLength;
  893. if (urid == CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH)
  894. return LV2_BUF_SIZE__nominalBlockLength;
  895. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  896. return LV2_BUF_SIZE__sequenceSize;
  897. // Log types
  898. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  899. return LV2_LOG__Error;
  900. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  901. return LV2_LOG__Note;
  902. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  903. return LV2_LOG__Trace;
  904. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  905. return LV2_LOG__Warning;
  906. // Time types
  907. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  908. return LV2_TIME__Position;
  909. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  910. return LV2_TIME__bar;
  911. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  912. return LV2_TIME__barBeat;
  913. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  914. return LV2_TIME__beat;
  915. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  916. return LV2_TIME__beatUnit;
  917. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  918. return LV2_TIME__beatsPerBar;
  919. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  920. return LV2_TIME__beatsPerMinute;
  921. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  922. return LV2_TIME__frame;
  923. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  924. return LV2_TIME__framesPerSecond;
  925. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  926. return LV2_TIME__speed;
  927. if (urid == CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT)
  928. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  929. // Others
  930. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  931. return LV2_MIDI__MidiEvent;
  932. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  933. return LV2_PARAMETERS__sampleRate;
  934. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  935. return LV2_UI__windowTitle;
  936. // Custom
  937. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  938. return URI_CARLA_ATOM_WORKER;
  939. if (urid == CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID)
  940. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  941. // Custom types
  942. return ((CarlaLv2Client*)handle)->getCustomURIDString(urid);
  943. }
  944. // -------------------------------------------------------------------
  945. // UI Port-Map Feature
  946. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  947. {
  948. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  949. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  950. return ((CarlaLv2Client*)handle)->handleUiPortMap(symbol);
  951. }
  952. // -------------------------------------------------------------------
  953. // UI Resize Feature
  954. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  955. {
  956. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  957. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  958. return ((CarlaLv2Client*)handle)->handleUiResize(width, height);
  959. }
  960. // -------------------------------------------------------------------
  961. // UI Extension
  962. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  963. {
  964. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  965. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  966. ((CarlaLv2Client*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  967. }
  968. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLv2Client)
  969. };
  970. // -----------------------------------------------------------------------
  971. CARLA_BRIDGE_END_NAMESPACE
  972. // -----------------------------------------------------------------------
  973. int main(int argc, const char* argv[])
  974. {
  975. CARLA_BRIDGE_USE_NAMESPACE
  976. if (argc < 3)
  977. {
  978. carla_stderr("usage: %s <plugin-uri> <ui-uri>", argv[0]);
  979. return 1;
  980. }
  981. const bool testingModeOnly = (argc != 7);
  982. // try to get sampleRate value
  983. if (const char* const sampleRateStr = std::getenv("CARLA_SAMPLE_RATE"))
  984. gSampleRate = std::atof(sampleRateStr);
  985. // Init LV2 client
  986. CarlaLv2Client client;
  987. // Load UI
  988. int ret;
  989. if (client.init(argc, argv))
  990. {
  991. client.exec(testingModeOnly);
  992. ret = 0;
  993. }
  994. else
  995. {
  996. ret = 1;
  997. }
  998. return ret;
  999. }
  1000. // -----------------------------------------------------------------------