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.

1185 lines
43KB

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