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.

1193 lines
44KB

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