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.

libjack_ports.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2022 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 "libjack.hpp"
  18. CARLA_BACKEND_USE_NAMESPACE
  19. // --------------------------------------------------------------------------------------------------------------------
  20. static uint32_t gPortId = JackPortState::kPortIdOffsetUser;
  21. CARLA_PLUGIN_EXPORT
  22. jack_port_t* jack_port_register(jack_client_t* client, const char* port_name, const char* port_type,
  23. unsigned long flags, unsigned long buffer_size)
  24. {
  25. carla_debug("%s(%p, %s, %s, 0x%lx, %lu)", __FUNCTION__, client, port_name, port_type, flags, buffer_size);
  26. JackClientState* const jclient = (JackClientState*)client;
  27. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, nullptr);
  28. CARLA_SAFE_ASSERT_RETURN(port_name != nullptr && port_name[0] != '\0', nullptr);
  29. CARLA_SAFE_ASSERT_RETURN(port_type != nullptr && port_type[0] != '\0', nullptr);
  30. const JackServerState& jserver(jclient->server);
  31. if (std::strcmp(port_type, JACK_DEFAULT_AUDIO_TYPE) == 0)
  32. {
  33. if (flags & JackPortIsInput)
  34. {
  35. const std::size_t index = jclient->audioIns.count();
  36. const uint gid = ++gPortId;
  37. JackPortState* const port = new JackPortState(jclient->name, port_name,
  38. static_cast<uint>(index),
  39. static_cast<int>(flags),
  40. gid,
  41. false, false, index < jserver.numAudioIns);
  42. {
  43. const CarlaMutexLocker cms(jclient->mutex);
  44. jclient->audioIns.append(port);
  45. }
  46. jclient->portIdMapping[gid] = port;
  47. jclient->portNameMapping[port->fullname] = port;
  48. return (jack_port_t*)port;
  49. }
  50. if (flags & JackPortIsOutput)
  51. {
  52. const std::size_t index = jclient->audioOuts.count();
  53. const uint gid = ++gPortId;
  54. JackPortState* const port = new JackPortState(jclient->name, port_name,
  55. static_cast<uint>(index),
  56. static_cast<int>(flags),
  57. gid,
  58. false, false, index < jserver.numAudioOuts);
  59. {
  60. const CarlaMutexLocker cms(jclient->mutex);
  61. jclient->audioOuts.append(port);
  62. }
  63. jclient->portIdMapping[gid] = port;
  64. jclient->portNameMapping[port->fullname] = port;
  65. return (jack_port_t*)port;
  66. }
  67. carla_stderr2("jack_port_register: invalid port flags '%x'", flags);
  68. return nullptr;
  69. }
  70. if (std::strcmp(port_type, JACK_DEFAULT_MIDI_TYPE) == 0)
  71. {
  72. if (flags & JackPortIsInput)
  73. {
  74. const std::size_t index = jclient->midiIns.count();
  75. const uint gid = ++gPortId;
  76. JackPortState* const port = new JackPortState(jclient->name, port_name,
  77. static_cast<uint>(index),
  78. static_cast<int>(flags),
  79. gid,
  80. true, false, index < jserver.numMidiIns);
  81. {
  82. const CarlaMutexLocker cms(jclient->mutex);
  83. jclient->midiIns.append(port);
  84. }
  85. jclient->portIdMapping[gid] = port;
  86. jclient->portNameMapping[port->fullname] = port;
  87. return (jack_port_t*)port;
  88. }
  89. if (flags & JackPortIsOutput)
  90. {
  91. const std::size_t index = jclient->midiOuts.count();
  92. const uint gid = ++gPortId;
  93. JackPortState* const port = new JackPortState(jclient->name, port_name,
  94. static_cast<uint>(index),
  95. static_cast<int>(flags),
  96. gid,
  97. true, false, index < jserver.numMidiOuts);
  98. {
  99. const CarlaMutexLocker cms(jclient->mutex);
  100. jclient->midiOuts.append(port);
  101. }
  102. jclient->portIdMapping[gid] = port;
  103. jclient->portNameMapping[port->fullname] = port;
  104. return (jack_port_t*)port;
  105. }
  106. carla_stderr2("jack_port_register: invalid port flags '%x'", flags);
  107. return nullptr;
  108. }
  109. carla_stderr2("jack_port_register: invalid port type '%s'", port_type);
  110. return nullptr;
  111. // unused
  112. (void)buffer_size;
  113. }
  114. CARLA_PLUGIN_EXPORT
  115. int jack_port_unregister(jack_client_t* client, jack_port_t* port)
  116. {
  117. carla_debug("%s(%p, %p)", __FUNCTION__, client, port);
  118. JackClientState* const jclient = (JackClientState*)client;
  119. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, EINVAL);
  120. JackPortState* const jport = (JackPortState*)port;
  121. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, EINVAL);
  122. CARLA_SAFE_ASSERT_RETURN(! jport->isSystem, EINVAL);
  123. {
  124. const CarlaMutexLocker cms(jclient->mutex);
  125. if (jport->isMidi)
  126. {
  127. if (jport->flags & JackPortIsInput)
  128. {
  129. CARLA_SAFE_ASSERT_RETURN(jclient->midiIns.removeOne(jport), ENOENT);
  130. jclient->portIdMapping.erase(jport->gid);
  131. jclient->portNameMapping.erase(jport->fullname);
  132. return 0;
  133. }
  134. if (jport->flags & JackPortIsOutput)
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(jclient->midiOuts.removeOne(jport), ENOENT);
  137. jclient->portIdMapping.erase(jport->gid);
  138. jclient->portNameMapping.erase(jport->fullname);
  139. return 0;
  140. }
  141. }
  142. else
  143. {
  144. if (jport->flags & JackPortIsInput)
  145. {
  146. CARLA_SAFE_ASSERT_RETURN(jclient->audioIns.removeOne(jport), ENOENT);
  147. jclient->portIdMapping.erase(jport->gid);
  148. jclient->portNameMapping.erase(jport->fullname);
  149. return 0;
  150. }
  151. if (jport->flags & JackPortIsOutput)
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(jclient->audioOuts.removeOne(jport), ENOENT);
  154. jclient->portIdMapping.erase(jport->gid);
  155. jclient->portNameMapping.erase(jport->fullname);
  156. return 0;
  157. }
  158. }
  159. }
  160. carla_stderr2("jack_port_register: invalid port '%s'", jport->name);
  161. return EINVAL;
  162. }
  163. CARLA_PLUGIN_EXPORT
  164. void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t)
  165. {
  166. JackPortState* const jport = (JackPortState*)port;
  167. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  168. return jport->buffer;
  169. }
  170. // --------------------------------------------------------------------------------------------------------------------
  171. CARLA_PLUGIN_EXPORT
  172. jack_uuid_t jack_port_uuid(const jack_port_t* port)
  173. {
  174. carla_debug("%s(%p)", __FUNCTION__, port);
  175. const JackPortState* const jport = (const JackPortState*)port;
  176. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  177. return jport->uuid;
  178. }
  179. CARLA_PLUGIN_EXPORT
  180. const char* jack_port_name(const jack_port_t* port)
  181. {
  182. carla_debug("%s(%p)", __FUNCTION__, port);
  183. const JackPortState* const jport = (const JackPortState*)port;
  184. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  185. return jport->fullname;
  186. }
  187. CARLA_PLUGIN_EXPORT
  188. const char* jack_port_short_name(const jack_port_t* port)
  189. {
  190. carla_debug("%s(%p)", __FUNCTION__, port);
  191. const JackPortState* const jport = (const JackPortState*)port;
  192. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  193. return jport->name;
  194. }
  195. CARLA_PLUGIN_EXPORT
  196. int jack_port_flags(const jack_port_t* port)
  197. {
  198. carla_debug("%s(%p)", __FUNCTION__, port);
  199. const JackPortState* const jport = (const JackPortState*)port;
  200. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  201. return jport->flags;
  202. }
  203. CARLA_PLUGIN_EXPORT
  204. const char* jack_port_type(const jack_port_t* port)
  205. {
  206. carla_debug("%s(%p)", __FUNCTION__, port);
  207. const JackPortState* const jport = (const JackPortState*)port;
  208. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, nullptr);
  209. static const char* const kAudioType = JACK_DEFAULT_AUDIO_TYPE;
  210. static const char* const kMidiType = JACK_DEFAULT_MIDI_TYPE;
  211. return jport->isMidi ? kMidiType : kAudioType;
  212. }
  213. CARLA_PLUGIN_EXPORT
  214. uint32_t jack_port_type_id(const jack_port_t* port)
  215. {
  216. carla_debug("%s(%p)", __FUNCTION__, port);
  217. const JackPortState* const jport = (const JackPortState*)port;
  218. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  219. return jport->isMidi ? 1 : 0;
  220. }
  221. // --------------------------------------------------------------------------------------------------------------------
  222. CARLA_PLUGIN_EXPORT
  223. int jack_port_is_mine(const jack_client_t*, const jack_port_t* port)
  224. {
  225. const JackPortState* const jport = (const JackPortState*)port;
  226. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  227. return jport->isSystem ? 0 : 1;
  228. }
  229. CARLA_PLUGIN_EXPORT
  230. int jack_port_connected(const jack_port_t* port)
  231. {
  232. const JackPortState* const jport = (const JackPortState*)port;
  233. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  234. return jport->isConnected ? 1 : 0;
  235. }
  236. CARLA_PLUGIN_EXPORT
  237. int jack_port_connected_to(const jack_port_t* port, const char* port_name)
  238. {
  239. carla_stderr2("%s(%p, %s) WIP", __FUNCTION__, port, port_name);
  240. const JackPortState* const jport = (const JackPortState*)port;
  241. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, 0);
  242. if (! jport->isConnected)
  243. return 0;
  244. // TODO
  245. return 1;
  246. }
  247. // --------------------------------------------------------------------------------------------------------------------
  248. CARLA_PLUGIN_EXPORT
  249. int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  250. {
  251. carla_debug("%s(%p, %p)", __FUNCTION__, src, dst);
  252. return ENOSYS;
  253. // unused
  254. (void)src;
  255. (void)dst;
  256. }
  257. CARLA_PLUGIN_EXPORT
  258. int jack_port_untie(jack_port_t* port)
  259. {
  260. carla_debug("%s(%p)", __FUNCTION__, port);
  261. return ENOSYS;
  262. // unused
  263. (void)port;
  264. }
  265. // --------------------------------------------------------------------------------------------------------------------
  266. CARLA_PLUGIN_EXPORT
  267. int jack_port_set_name(jack_port_t *port, const char *port_name)
  268. {
  269. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, port_name);
  270. return ENOSYS;
  271. }
  272. CARLA_PLUGIN_EXPORT
  273. int jack_port_rename(jack_client_t* client, jack_port_t *port, const char *port_name)
  274. {
  275. carla_stderr2("%s(%p, %p, %s) WIP", __FUNCTION__, client, port, port_name);
  276. JackClientState* const jclient = (JackClientState*)client;
  277. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, EINVAL);
  278. CARLA_SAFE_ASSERT_RETURN(port_name != nullptr && port_name[0] != '\0', EINVAL);
  279. JackPortState* const jport = (JackPortState*)port;
  280. CARLA_SAFE_ASSERT_RETURN(jport != nullptr, EINVAL);
  281. CARLA_SAFE_ASSERT_RETURN(! jport->isSystem, EINVAL);
  282. // TODO: verify uniqueness
  283. char* const fullname = (char*)std::malloc(STR_MAX);
  284. CARLA_SAFE_ASSERT_RETURN(fullname != nullptr, ENOMEM);
  285. std::snprintf(fullname, STR_MAX, "%s:%s", jclient->name, port_name);
  286. fullname[STR_MAX-1] = '\0';
  287. jclient->portNameMapping.erase(jport->fullname);
  288. jclient->portNameMapping[fullname] = jport;
  289. std::free(jport->name);
  290. std::free(jport->fullname);
  291. jport->name = strdup(port_name);
  292. jport->fullname = fullname;
  293. // TODO: port rename callback
  294. return 0;
  295. }
  296. CARLA_PLUGIN_EXPORT
  297. int jack_port_set_alias(jack_port_t* port, const char* alias)
  298. {
  299. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  300. return ENOSYS;
  301. }
  302. CARLA_PLUGIN_EXPORT
  303. int jack_port_unset_alias(jack_port_t* port, const char* alias)
  304. {
  305. carla_stderr2("%s(%p, %s)", __FUNCTION__, port, alias);
  306. return ENOSYS;
  307. }
  308. CARLA_PLUGIN_EXPORT
  309. int jack_port_get_aliases(const jack_port_t*, char* const aliases[2])
  310. {
  311. *aliases[0] = '\0';
  312. *aliases[1] = '\0';
  313. return 0;
  314. }
  315. // --------------------------------------------------------------------------------------------------------------------
  316. CARLA_PLUGIN_EXPORT
  317. int jack_port_request_monitor(jack_port_t* port, int onoff)
  318. {
  319. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  320. return ENOSYS;
  321. }
  322. CARLA_PLUGIN_EXPORT
  323. int jack_port_request_monitor_by_name(jack_client_t* client, const char* port_name, int onoff)
  324. {
  325. carla_stderr2("%s(%p, %s, %i)", __FUNCTION__, client, port_name, onoff);
  326. return ENOSYS;
  327. }
  328. CARLA_PLUGIN_EXPORT
  329. int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  330. {
  331. carla_stderr2("%s(%p, %i)", __FUNCTION__, port, onoff);
  332. return ENOSYS;
  333. }
  334. CARLA_PLUGIN_EXPORT
  335. int jack_port_monitoring_input(jack_port_t* port)
  336. {
  337. carla_stderr2("%s(%p)", __FUNCTION__, port);
  338. return 0;
  339. }
  340. // --------------------------------------------------------------------------------------------------------------------
  341. CARLA_PLUGIN_EXPORT
  342. int jack_connect(jack_client_t* client, const char* a, const char* b)
  343. {
  344. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  345. return 0;
  346. }
  347. CARLA_PLUGIN_EXPORT
  348. int jack_disconnect(jack_client_t* client, const char* a, const char* b)
  349. {
  350. carla_stderr2("%s(%p, %s. %s)", __FUNCTION__, client, a, b);
  351. return 0;
  352. }
  353. CARLA_PLUGIN_EXPORT
  354. int jack_port_disconnect(jack_client_t* client, jack_port_t* port)
  355. {
  356. carla_stderr2("%s(%p, %p)", __FUNCTION__, client, port);
  357. return 0;
  358. }
  359. // --------------------------------------------------------------------------------------------------------------------
  360. CARLA_PLUGIN_EXPORT
  361. int jack_port_name_size(void)
  362. {
  363. return STR_MAX;
  364. }
  365. CARLA_PLUGIN_EXPORT
  366. int jack_port_type_size(void)
  367. {
  368. return STR_MAX;
  369. }
  370. CARLA_PLUGIN_EXPORT
  371. size_t jack_port_type_get_buffer_size(jack_client_t* client, const char* port_type)
  372. {
  373. carla_debug("%s(%p, %s)", __FUNCTION__, client, port_type);
  374. CARLA_SAFE_ASSERT_RETURN(port_type != nullptr && port_type[0] != '\0', 0);
  375. if (std::strcmp(port_type, JACK_DEFAULT_MIDI_TYPE) == 0)
  376. return JackMidiPortBufferBase::kMaxEventSize;
  377. return 0;
  378. // unused
  379. (void)client;
  380. }
  381. // --------------------------------------------------------------------------------------------------------------------