Collection of tools useful for audio production
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.

906 lines
29KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifdef BRIDGE_LV2
  18. #include "carla_bridge_client.h"
  19. #include "carla_lv2.h"
  20. #include "carla_midi.h"
  21. #include <vector>
  22. #include <QtCore/QDir>
  23. #ifdef BRIDGE_LV2_X11
  24. # include <QtGui/QDialog>
  25. #endif
  26. CARLA_BRIDGE_START_NAMESPACE
  27. // -------------------------------------------------------------------------
  28. // feature ids
  29. const uint32_t lv2_feature_id_event = 0;
  30. const uint32_t lv2_feature_id_logs = 1;
  31. const uint32_t lv2_feature_id_programs = 2;
  32. const uint32_t lv2_feature_id_state_make_path = 3;
  33. const uint32_t lv2_feature_id_state_map_path = 4;
  34. const uint32_t lv2_feature_id_strict_bounds = 5;
  35. const uint32_t lv2_feature_id_uri_map = 6;
  36. const uint32_t lv2_feature_id_urid_map = 7;
  37. const uint32_t lv2_feature_id_urid_unmap = 8;
  38. const uint32_t lv2_feature_id_ui_parent = 9;
  39. const uint32_t lv2_feature_id_ui_port_map = 10;
  40. const uint32_t lv2_feature_id_ui_resize = 11;
  41. const uint32_t lv2_feature_count = 12;
  42. // pre-set uri[d] map ids
  43. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  44. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  45. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 2;
  46. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 3;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 4;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 5;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 6;
  50. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 7;
  51. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 8;
  52. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 9;
  53. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 10;
  54. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 11;
  55. const uint32_t CARLA_URI_MAP_ID_COUNT = 12;
  56. // -------------------------------------------------------------------------
  57. class CarlaLv2Client : public CarlaClient
  58. {
  59. public:
  60. CarlaLv2Client(CarlaToolkit* const toolkit)
  61. : CarlaClient(toolkit)
  62. {
  63. handle = nullptr;
  64. widget = nullptr;
  65. descriptor = nullptr;
  66. rdf_descriptor = nullptr;
  67. rdf_ui_descriptor = nullptr;
  68. programs = nullptr;
  69. #ifdef BRIDGE_LV2_X11
  70. m_resizable = false;
  71. x11_widget = new QDialog;
  72. #else
  73. m_resizable = true;
  74. #endif
  75. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; i++)
  76. customURIDs.push_back(nullptr);
  77. for (uint32_t i=0; i < lv2_feature_count+1; i++)
  78. features[i] = nullptr;
  79. // -----------------------------------------------------------------
  80. // initialize features
  81. LV2_Event_Feature* Event_Feature = new LV2_Event_Feature;
  82. Event_Feature->callback_data = this;
  83. Event_Feature->lv2_event_ref = carla_lv2_event_ref;
  84. Event_Feature->lv2_event_unref = carla_lv2_event_unref;
  85. LV2_Log_Log* Log_Feature = new LV2_Log_Log;
  86. Log_Feature->handle = this;
  87. Log_Feature->printf = carla_lv2_log_printf;
  88. Log_Feature->vprintf = carla_lv2_log_vprintf;
  89. LV2_Programs_Host* Programs_Feature = new LV2_Programs_Host;
  90. Programs_Feature->handle = this;
  91. Programs_Feature->program_changed = carla_lv2_program_changed;
  92. LV2_State_Make_Path* State_MakePath_Feature = new LV2_State_Make_Path;
  93. State_MakePath_Feature->handle = this;
  94. State_MakePath_Feature->path = carla_lv2_state_make_path;
  95. LV2_State_Map_Path* State_MapPath_Feature = new LV2_State_Map_Path;
  96. State_MapPath_Feature->handle = this;
  97. State_MapPath_Feature->abstract_path = carla_lv2_state_map_abstract_path;
  98. State_MapPath_Feature->absolute_path = carla_lv2_state_map_absolute_path;
  99. LV2_URI_Map_Feature* URI_Map_Feature = new LV2_URI_Map_Feature;
  100. URI_Map_Feature->callback_data = this;
  101. URI_Map_Feature->uri_to_id = carla_lv2_uri_to_id;
  102. LV2_URID_Map* URID_Map_Feature = new LV2_URID_Map;
  103. URID_Map_Feature->handle = this;
  104. URID_Map_Feature->map = carla_lv2_urid_map;
  105. LV2_URID_Unmap* URID_Unmap_Feature = new LV2_URID_Unmap;
  106. URID_Unmap_Feature->handle = this;
  107. URID_Unmap_Feature->unmap = carla_lv2_urid_unmap;
  108. LV2UI_Port_Map* UI_PortMap_Feature = new LV2UI_Port_Map;
  109. UI_PortMap_Feature->handle = this;
  110. UI_PortMap_Feature->port_index = carla_lv2_ui_port_map;
  111. LV2UI_Resize* UI_Resize_Feature = new LV2UI_Resize;
  112. UI_Resize_Feature->handle = this;
  113. UI_Resize_Feature->ui_resize = carla_lv2_ui_resize;
  114. features[lv2_feature_id_event] = new LV2_Feature;
  115. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  116. features[lv2_feature_id_event]->data = Event_Feature;
  117. features[lv2_feature_id_logs] = new LV2_Feature;
  118. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  119. features[lv2_feature_id_logs]->data = Log_Feature;
  120. features[lv2_feature_id_programs] = new LV2_Feature;
  121. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  122. features[lv2_feature_id_programs]->data = Programs_Feature;
  123. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  124. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  125. features[lv2_feature_id_state_make_path]->data = State_MakePath_Feature;
  126. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  127. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  128. features[lv2_feature_id_state_map_path]->data = State_MapPath_Feature;
  129. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  130. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  131. features[lv2_feature_id_strict_bounds]->data = nullptr;
  132. features[lv2_feature_id_uri_map] = new LV2_Feature;
  133. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  134. features[lv2_feature_id_uri_map]->data = URI_Map_Feature;
  135. features[lv2_feature_id_urid_map] = new LV2_Feature;
  136. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  137. features[lv2_feature_id_urid_map]->data = URID_Map_Feature;
  138. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  139. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  140. features[lv2_feature_id_urid_unmap]->data = URID_Unmap_Feature;
  141. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  142. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  143. #ifdef BRIDGE_LV2_X11
  144. features[lv2_feature_id_ui_parent]->data = (void*)x11_widget->winId();
  145. #else
  146. features[lv2_feature_id_ui_parent]->data = nullptr;
  147. #endif
  148. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  149. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  150. features[lv2_feature_id_ui_port_map]->data = UI_PortMap_Feature;
  151. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  152. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  153. features[lv2_feature_id_ui_resize]->data = UI_Resize_Feature;
  154. }
  155. ~CarlaLv2Client()
  156. {
  157. if (rdf_descriptor)
  158. lv2_rdf_free(rdf_descriptor);
  159. delete (LV2_Event_Feature*)features[lv2_feature_id_event]->data;
  160. delete (LV2_Log_Log*)features[lv2_feature_id_logs]->data;
  161. delete (LV2_Programs_Host*)features[lv2_feature_id_programs]->data;
  162. delete (LV2_State_Make_Path*)features[lv2_feature_id_state_make_path]->data;
  163. delete (LV2_State_Map_Path*)features[lv2_feature_id_state_map_path]->data;
  164. delete (LV2_URI_Map_Feature*)features[lv2_feature_id_uri_map]->data;
  165. delete (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  166. delete (LV2_URID_Unmap*)features[lv2_feature_id_urid_unmap]->data;
  167. delete (LV2UI_Port_Map*)features[lv2_feature_id_ui_port_map]->data;
  168. delete (LV2UI_Resize*)features[lv2_feature_id_ui_resize]->data;
  169. for (uint32_t i=0; i < lv2_feature_count; i++)
  170. {
  171. if (features[i])
  172. delete features[i];
  173. }
  174. for (size_t i=0; i < customURIDs.size(); i++)
  175. {
  176. if (customURIDs[i])
  177. free((void*)customURIDs[i]);
  178. }
  179. customURIDs.clear();
  180. }
  181. // ---------------------------------------------------------------------
  182. // ui initialization
  183. bool init(const char* plugin_uri, const char* ui_uri)
  184. {
  185. // -----------------------------------------------------------------
  186. // get plugin from lv2_rdf (lilv)
  187. Lv2World.init();
  188. rdf_descriptor = lv2_rdf_new(plugin_uri);
  189. if (! rdf_descriptor)
  190. return false;
  191. // -----------------------------------------------------------------
  192. // find requested UI
  193. for (uint32_t i=0; i < rdf_descriptor->UICount; i++)
  194. {
  195. if (strcmp(rdf_descriptor->UIs[i].URI, ui_uri) == 0)
  196. {
  197. rdf_ui_descriptor = &rdf_descriptor->UIs[i];
  198. break;
  199. }
  200. }
  201. if (! rdf_ui_descriptor)
  202. return false;
  203. // -----------------------------------------------------------------
  204. // open DLL
  205. if (! libOpen(rdf_ui_descriptor->Binary))
  206. return false;
  207. // -----------------------------------------------------------------
  208. // get DLL main entry
  209. LV2UI_DescriptorFunction const ui_descfn = (LV2UI_DescriptorFunction)libSymbol("lv2ui_descriptor");
  210. if (! ui_descfn)
  211. return false;
  212. // -----------------------------------------------------------
  213. // get descriptor that matches URI
  214. uint32_t i = 0;
  215. while ((descriptor = ui_descfn(i++)))
  216. {
  217. if (strcmp(descriptor->URI, ui_uri) == 0)
  218. break;
  219. }
  220. if (! descriptor)
  221. return false;
  222. // -----------------------------------------------------------
  223. // initialize UI
  224. handle = descriptor->instantiate(descriptor, plugin_uri, rdf_ui_descriptor->Bundle, carla_lv2_ui_write_function, this, &widget, features);
  225. if (! handle)
  226. return false;
  227. // -----------------------------------------------------------
  228. // check if not resizable
  229. #ifndef BRIDGE_LV2_X11
  230. for (uint32_t i=0; i < rdf_ui_descriptor->FeatureCount; i++)
  231. {
  232. if (strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  233. {
  234. m_resizable = false;
  235. break;
  236. }
  237. }
  238. #endif
  239. // -----------------------------------------------------------
  240. // check for known extensions
  241. for (uint32_t i=0; descriptor->extension_data && i < rdf_ui_descriptor->ExtensionCount; i++)
  242. {
  243. if (strcmp(rdf_ui_descriptor->Extensions[i], LV2_PROGRAMS__UIInterface) == 0)
  244. {
  245. programs = (LV2_Programs_UI_Interface*)descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  246. break;
  247. }
  248. }
  249. return true;
  250. }
  251. void close()
  252. {
  253. if (handle && descriptor && descriptor->cleanup)
  254. descriptor->cleanup(handle);
  255. libClose();
  256. }
  257. // ---------------------------------------------------------------------
  258. // processing
  259. void setParameter(int32_t rindex, double value)
  260. {
  261. Q_ASSERT(handle && descriptor);
  262. if (handle && descriptor && descriptor->port_event)
  263. {
  264. float fvalue = value;
  265. descriptor->port_event(handle, rindex, sizeof(float), 0, &fvalue);
  266. }
  267. }
  268. void setProgram(uint32_t)
  269. {
  270. }
  271. void setMidiProgram(uint32_t bank, uint32_t program)
  272. {
  273. Q_ASSERT(handle);
  274. if (handle && programs)
  275. programs->select_program(handle, bank, program);
  276. }
  277. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  278. {
  279. Q_ASSERT(handle && descriptor);
  280. if (handle && descriptor && descriptor->port_event)
  281. {
  282. uint8_t buf[3] = { uint8_t(0x90 + channel), note, velo };
  283. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_MIDI_EVENT, buf);
  284. }
  285. }
  286. void noteOff(const uint8_t channel, const uint8_t note)
  287. {
  288. Q_ASSERT(handle && descriptor);
  289. if (handle && descriptor && descriptor->port_event)
  290. {
  291. uint8_t buf[3] = { uint8_t(0x80 + channel), note, 0 };
  292. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_MIDI_EVENT, buf);
  293. }
  294. }
  295. // ---------------------------------------------------------------------
  296. // gui
  297. void* getWidget() const
  298. {
  299. #ifdef BRIDGE_LV2_X11
  300. return x11_widget;
  301. #else
  302. return widget;
  303. #endif
  304. }
  305. bool isResizable() const
  306. {
  307. return m_resizable;
  308. }
  309. bool needsReparent() const
  310. {
  311. #ifdef BRIDGE_LV2_X11
  312. return true;
  313. #else
  314. return false;
  315. #endif
  316. }
  317. // ---------------------------------------------------------------------
  318. uint32_t getCustomURID(const char* const uri)
  319. {
  320. qDebug("CarlaLv2Client::getCustomURID(%s)", uri);
  321. Q_ASSERT(uri);
  322. for (size_t i=0; i < customURIDs.size(); i++)
  323. {
  324. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  325. return i;
  326. }
  327. customURIDs.push_back(strdup(uri));
  328. return customURIDs.size()-1;
  329. }
  330. const char* getCustomURIString(LV2_URID urid)
  331. {
  332. qDebug("CarlaLv2Client::getCustomURIString(%i)", urid);
  333. Q_ASSERT(urid != 0);
  334. if (urid < customURIDs.size())
  335. return customURIDs[urid];
  336. return nullptr;
  337. }
  338. void handleTransferAtom(const char* const type, const char* const value)
  339. {
  340. qDebug("CarlaLv2Client::handleTransferEvent(%s, %s)", type, value);
  341. Q_ASSERT(type);
  342. Q_ASSERT(value);
  343. }
  344. void handleTransferEvent(const char* const type, const char* const value)
  345. {
  346. qDebug("CarlaLv2Client::handleTransferEvent(%s, %s)", type, value);
  347. Q_ASSERT(type);
  348. Q_ASSERT(value);
  349. if (handle && descriptor && descriptor->port_event)
  350. {
  351. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  352. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  353. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  354. Sratom* sratom = sratom_new(URID_Map);
  355. SerdChunk chunk = { nullptr, 0 };
  356. LV2_Atom_Forge forge;
  357. lv2_atom_forge_init(&forge, URID_Map);
  358. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  359. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  360. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  361. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  362. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  363. //lv2_atom_forge_property_head(&forge, getCustomURID(key), CARLA_URI_MAP_ID_NULL);
  364. if (strcmp(type, "string") == 0)
  365. lv2_atom_forge_string(&forge, value, strlen(value));
  366. else if (strcmp(type, "path") == 0)
  367. lv2_atom_forge_path(&forge, value, strlen(value));
  368. else if (strcmp(type, "chunk") == 0)
  369. lv2_atom_forge_literal(&forge, value, strlen(value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  370. //else
  371. // lv2_atom_forge_literal(&forge, value, strlen(value), getCustomURID(key), CARLA_URI_MAP_ID_NULL);
  372. lv2_atom_forge_pop(&forge, &bodyFrame);
  373. lv2_atom_forge_pop(&forge, &refFrame);
  374. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  375. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  376. free((void*)chunk.buf);
  377. sratom_free(sratom);
  378. }
  379. }
  380. void handleProgramChanged(int32_t /*index*/)
  381. {
  382. sendOscConfigure("reloadprograms", "");
  383. }
  384. uint32_t handleUiPortMap(const char* const symbol)
  385. {
  386. Q_ASSERT(symbol);
  387. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  388. {
  389. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  390. return i;
  391. }
  392. return LV2UI_INVALID_PORT_INDEX;
  393. }
  394. int handleUiResize(int width, int height)
  395. {
  396. Q_ASSERT(width > 0);
  397. Q_ASSERT(height > 0);
  398. quequeMessage(MESSAGE_RESIZE_GUI, width, height, 0.0);
  399. return 0;
  400. }
  401. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  402. {
  403. if (format == 0)
  404. {
  405. Q_ASSERT(bufferSize == sizeof(float));
  406. if (bufferSize == sizeof(float))
  407. {
  408. float value = *(float*)buffer;
  409. sendOscControl(portIndex, value);
  410. }
  411. }
  412. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  413. {
  414. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  415. QByteArray chunk((const char*)buffer, bufferSize);
  416. sendOscLv2TransferAtom(getCustomURIString(atom->type), chunk.toBase64().constData());
  417. if (descriptor && descriptor->port_event)
  418. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, atom);
  419. }
  420. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  421. {
  422. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  423. QByteArray chunk((const char*)buffer, bufferSize);
  424. sendOscLv2TransferEvent(getCustomURIString(atom->type), chunk.toBase64().constData());
  425. if (descriptor && descriptor->port_event)
  426. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  427. }
  428. }
  429. // ----------------- Event Feature ---------------------------------------------------
  430. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  431. {
  432. qDebug("CarlaLv2Client::carla_lv2_event_ref(%p, %p)", callback_data, event);
  433. Q_ASSERT(callback_data);
  434. Q_ASSERT(event);
  435. return 0;
  436. }
  437. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  438. {
  439. qDebug("CarlaLv2Client::carla_lv2_event_unref(%p, %p)", callback_data, event);
  440. Q_ASSERT(callback_data);
  441. Q_ASSERT(event);
  442. return 0;
  443. }
  444. // ----------------- Logs Feature ----------------------------------------------------
  445. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  446. {
  447. qDebug("CarlaLv2Client::carla_lv2_log_printf(%p, %i, %s, ...)", handle, type, fmt);
  448. Q_ASSERT(handle);
  449. Q_ASSERT(type > 0);
  450. #ifndef DEBUG
  451. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  452. return 0;
  453. #endif
  454. va_list args;
  455. va_start(args, fmt);
  456. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  457. va_end(args);
  458. return ret;
  459. }
  460. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  461. {
  462. qDebug("CarlaLv2Client::carla_lv2_log_vprintf(%p, %i, %s, ...)", handle, type, fmt);
  463. Q_ASSERT(handle);
  464. Q_ASSERT(type > 0);
  465. #ifndef DEBUG
  466. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  467. return 0;
  468. #endif
  469. char buf[8196];
  470. vsprintf(buf, fmt, ap);
  471. if (*buf == 0)
  472. return 0;
  473. switch (type)
  474. {
  475. case CARLA_URI_MAP_ID_LOG_ERROR:
  476. qCritical("%s", buf);
  477. break;
  478. case CARLA_URI_MAP_ID_LOG_NOTE:
  479. printf("%s\n", buf);
  480. break;
  481. case CARLA_URI_MAP_ID_LOG_TRACE:
  482. qDebug("%s", buf);
  483. break;
  484. case CARLA_URI_MAP_ID_LOG_WARNING:
  485. qWarning("%s", buf);
  486. break;
  487. default:
  488. break;
  489. }
  490. return strlen(buf);
  491. }
  492. // ----------------- Programs Feature ------------------------------------------------
  493. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  494. {
  495. qDebug("CarlaLv2Client::carla_lv2_program_changed(%p, %i)", handle, index);
  496. Q_ASSERT(handle);
  497. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  498. client->handleProgramChanged(index);
  499. }
  500. // ----------------- State Feature ---------------------------------------------------
  501. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  502. {
  503. qDebug("CarlaLv2Client::carla_lv2_state_make_path(%p, %p)", handle, path);
  504. Q_ASSERT(handle);
  505. Q_ASSERT(path);
  506. QDir dir;
  507. dir.mkpath(path);
  508. return strdup(path);
  509. }
  510. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  511. {
  512. qDebug("CarlaLv2Client::carla_lv2_state_map_abstract_path(%p, %p)", handle, absolute_path);
  513. Q_ASSERT(handle);
  514. Q_ASSERT(absolute_path);
  515. QDir dir(absolute_path);
  516. return strdup(dir.canonicalPath().toUtf8().constData());
  517. }
  518. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  519. {
  520. qDebug("CarlaLv2Client::carla_lv2_state_map_absolute_path(%p, %p)", handle, abstract_path);
  521. Q_ASSERT(handle);
  522. Q_ASSERT(abstract_path);
  523. QDir dir(abstract_path);
  524. return strdup(dir.absolutePath().toUtf8().constData());
  525. }
  526. // ----------------- URI-Map Feature ---------------------------------------
  527. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  528. {
  529. qDebug("CarlaLv2Client::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  530. return carla_lv2_urid_map(data, uri);
  531. }
  532. // ----------------- URID Feature ------------------------------------------
  533. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  534. {
  535. qDebug("CarlaLv2Client::carla_lv2_urid_map(%p, %s)", handle, uri);
  536. Q_ASSERT(handle);
  537. Q_ASSERT(uri);
  538. // Atom types
  539. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  540. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  541. if (strcmp(uri, LV2_ATOM__Path) == 0)
  542. return CARLA_URI_MAP_ID_ATOM_PATH;
  543. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  544. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  545. if (strcmp(uri, LV2_ATOM__String) == 0)
  546. return CARLA_URI_MAP_ID_ATOM_STRING;
  547. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  548. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  549. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  550. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  551. // Log types
  552. if (strcmp(uri, LV2_LOG__Error) == 0)
  553. return CARLA_URI_MAP_ID_LOG_ERROR;
  554. if (strcmp(uri, LV2_LOG__Note) == 0)
  555. return CARLA_URI_MAP_ID_LOG_NOTE;
  556. if (strcmp(uri, LV2_LOG__Trace) == 0)
  557. return CARLA_URI_MAP_ID_LOG_TRACE;
  558. if (strcmp(uri, LV2_LOG__Warning) == 0)
  559. return CARLA_URI_MAP_ID_LOG_WARNING;
  560. // Others
  561. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  562. return CARLA_URI_MAP_ID_MIDI_EVENT;
  563. // Custom types
  564. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  565. return client->getCustomURID(uri);
  566. }
  567. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  568. {
  569. qDebug("CarlaLv2Client::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  570. Q_ASSERT(handle);
  571. Q_ASSERT(urid > 0);
  572. // Atom types
  573. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  574. return LV2_ATOM__Chunk;
  575. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  576. return LV2_ATOM__Path;
  577. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  578. return LV2_ATOM__Sequence;
  579. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  580. return LV2_ATOM__String;
  581. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  582. return LV2_ATOM__atomTransfer;
  583. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  584. return LV2_ATOM__eventTransfer;
  585. // Log types
  586. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  587. return LV2_LOG__Error;
  588. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  589. return LV2_LOG__Note;
  590. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  591. return LV2_LOG__Trace;
  592. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  593. return LV2_LOG__Warning;
  594. // Others
  595. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  596. return LV2_MIDI__MidiEvent;
  597. // Custom types
  598. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  599. return client->getCustomURIString(urid);
  600. }
  601. // ----------------- UI Port-Map Feature ---------------------------------------------
  602. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  603. {
  604. qDebug("CarlaLv2Client::carla_lv2_ui_port_map(%p, %s)", handle, symbol);
  605. Q_ASSERT(handle);
  606. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  607. return client->handleUiPortMap(symbol);
  608. }
  609. // ----------------- UI Resize Feature -------------------------------------
  610. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  611. {
  612. qDebug("CarlaLv2Client::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  613. Q_ASSERT(handle);
  614. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  615. return client->handleUiResize(width, height);
  616. }
  617. // ----------------- UI Extension ------------------------------------------
  618. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  619. {
  620. qDebug("CarlaLv2Client::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  621. Q_ASSERT(controller);
  622. CarlaLv2Client* const client = (CarlaLv2Client*)controller;
  623. client->handleUiWrite(port_index, buffer_size, format, buffer);
  624. }
  625. private:
  626. LV2UI_Handle handle;
  627. LV2UI_Widget widget;
  628. const LV2UI_Descriptor* descriptor;
  629. LV2_Feature* features[lv2_feature_count+1];
  630. const LV2_RDF_Descriptor* rdf_descriptor;
  631. const LV2_RDF_UI* rdf_ui_descriptor;
  632. const LV2_Programs_UI_Interface* programs;
  633. #ifdef BRIDGE_LV2_X11
  634. QDialog* x11_widget;
  635. #endif
  636. bool m_resizable;
  637. std::vector<const char*> customURIDs;
  638. };
  639. int CarlaOsc::handleMsgLv2TransferAtom(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  640. {
  641. qDebug("CarlaOsc::handle_lv2_atom_transfer()");
  642. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  643. if (! client)
  644. return 1;
  645. const char* type = (const char*)&argv[0]->s;
  646. const char* value = (const char*)&argv[2]->s;
  647. CarlaLv2Client* const lv2client = (CarlaLv2Client*)client;
  648. lv2client->handleTransferAtom(type, value);
  649. return 0;
  650. }
  651. int CarlaOsc::handleMsgLv2TransferEvent(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  652. {
  653. qDebug("CarlaOsc::handle_lv2_event_transfer()");
  654. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  655. if (! client)
  656. return 1;
  657. const char* type = (const char*)&argv[0]->s;
  658. const char* value = (const char*)&argv[2]->s;
  659. CarlaLv2Client* const lv2client = (CarlaLv2Client*)client;
  660. lv2client->handleTransferEvent(type, value);
  661. return 0;
  662. }
  663. CARLA_BRIDGE_END_NAMESPACE
  664. int main(int argc, char* argv[])
  665. {
  666. if (argc != 5)
  667. {
  668. qCritical("%s: bad arguments", argv[0]);
  669. return 1;
  670. }
  671. const char* osc_url = argv[1];
  672. const char* plugin_uri = argv[2];
  673. const char* ui_uri = argv[3];
  674. const char* ui_title = argv[4];
  675. using namespace CarlaBridge;
  676. // Init toolkit
  677. CarlaToolkit* const toolkit = CarlaToolkit::createNew(ui_title);
  678. toolkit->init();
  679. // Init LV2-UI
  680. CarlaLv2Client client(toolkit);
  681. // Init OSC
  682. if (! client.oscInit(osc_url))
  683. {
  684. toolkit->quit();
  685. delete toolkit;
  686. return -1;
  687. }
  688. // Load UI
  689. int ret;
  690. if (client.init(plugin_uri, ui_uri))
  691. {
  692. toolkit->exec(&client);
  693. ret = 0;
  694. }
  695. else
  696. {
  697. qCritical("Failed to load LV2 UI");
  698. ret = 1;
  699. }
  700. // Close OSC
  701. client.sendOscExiting();
  702. client.oscClose();
  703. // Close LV2-UI
  704. client.close();
  705. // Close toolkit
  706. toolkit->quit();
  707. delete toolkit;
  708. return ret;
  709. }
  710. #endif