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.

CarlaPatchbayUtils.hpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Carla patchbay utils
  3. * Copyright (C) 2011-2020 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. #ifndef CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  18. #define CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  19. #include "CarlaMutex.hpp"
  20. #include "LinkedList.hpp"
  21. #define STR_MAX 0xFF
  22. // -----------------------------------------------------------------------
  23. struct GroupNameToId {
  24. uint group;
  25. char name[STR_MAX]; // globally unique
  26. void clear() noexcept
  27. {
  28. group = 0;
  29. name[0] = '\0';
  30. }
  31. void setData(const uint g, const char n[]) noexcept
  32. {
  33. group = g;
  34. rename(n);
  35. }
  36. void rename(const char n[]) noexcept
  37. {
  38. std::strncpy(name, n, STR_MAX-1);
  39. name[STR_MAX-1] = '\0';
  40. }
  41. bool operator==(const GroupNameToId& groupNameToId) const noexcept
  42. {
  43. if (groupNameToId.group != group)
  44. return false;
  45. if (std::strncmp(groupNameToId.name, name, STR_MAX-1) != 0)
  46. return false;
  47. return true;
  48. }
  49. bool operator!=(const GroupNameToId& groupNameToId) const noexcept
  50. {
  51. return !operator==(groupNameToId);
  52. }
  53. };
  54. struct PatchbayGroupList {
  55. uint lastId;
  56. LinkedList<GroupNameToId> list;
  57. CarlaMutex mutex;
  58. PatchbayGroupList() noexcept
  59. : lastId(0),
  60. list(),
  61. mutex() {}
  62. void clear() noexcept
  63. {
  64. lastId = 0;
  65. list.clear();
  66. }
  67. uint getGroupId(const char* const groupName) const noexcept;
  68. // always returns valid pointer (non-null)
  69. const char* getGroupName(const uint groupId) const noexcept;
  70. };
  71. // -----------------------------------------------------------------------
  72. struct PortNameToId {
  73. uint group;
  74. uint port;
  75. char name[STR_MAX]; // locally unique (within the same group)
  76. char fullName[STR_MAX]; // globally unique
  77. char identifier[STR_MAX]; // globally unique, if used
  78. void clear() noexcept
  79. {
  80. group = 0;
  81. port = 0;
  82. name[0] = '\0';
  83. fullName[0] = '\0';
  84. identifier[0] = '\0';
  85. }
  86. void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
  87. {
  88. group = g;
  89. port = p;
  90. rename(n, fn);
  91. }
  92. void setDataWithIdentifier(const uint g, const uint p, const char n[], const char id[]) noexcept
  93. {
  94. group = g;
  95. port = p;
  96. std::strncpy(name, n, STR_MAX-1);
  97. name[STR_MAX-1] = '\0';
  98. fullName[0] = '\0';
  99. std::strncpy(identifier, id, STR_MAX-1);
  100. identifier[STR_MAX-1] = '\0';
  101. }
  102. void setFullName(const char fn[]) noexcept
  103. {
  104. std::strncpy(fullName, fn, STR_MAX-1);
  105. fullName[STR_MAX-1] = '\0';
  106. }
  107. void rename(const char n[], const char fn[]) noexcept
  108. {
  109. std::strncpy(name, n, STR_MAX-1);
  110. name[STR_MAX-1] = '\0';
  111. std::strncpy(fullName, fn, STR_MAX-1);
  112. fullName[STR_MAX-1] = '\0';
  113. }
  114. bool operator==(const PortNameToId& portNameToId) noexcept
  115. {
  116. if (portNameToId.group != group || portNameToId.port != port)
  117. return false;
  118. if (std::strncmp(portNameToId.name, name, STR_MAX-1) != 0)
  119. return false;
  120. if (std::strncmp(portNameToId.fullName, fullName, STR_MAX-1) != 0)
  121. return false;
  122. return true;
  123. }
  124. bool operator!=(const PortNameToId& portNameToId) noexcept
  125. {
  126. return !operator==(portNameToId);
  127. }
  128. };
  129. struct PatchbayPortList {
  130. uint lastId;
  131. LinkedList<PortNameToId> list;
  132. CarlaMutex mutex;
  133. PatchbayPortList() noexcept
  134. : lastId(0),
  135. list(),
  136. mutex() {}
  137. void clear() noexcept
  138. {
  139. lastId = 0;
  140. list.clear();
  141. }
  142. // always returns valid pointer (non-null)
  143. const char* getFullPortName(const uint groupId, const uint portId) const noexcept;
  144. const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept;
  145. };
  146. // -----------------------------------------------------------------------
  147. struct ConnectionToId {
  148. uint id;
  149. uint groupA, portA;
  150. uint groupB, portB;
  151. void clear() noexcept
  152. {
  153. // needed for apple GCC4.2
  154. this->id = 0;
  155. groupA = 0;
  156. portA = 0;
  157. groupB = 0;
  158. portB = 0;
  159. }
  160. void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
  161. {
  162. // needed for apple GCC4.2
  163. this->id = i;
  164. groupA = gA;
  165. portA = pA;
  166. groupB = gB;
  167. portB = pB;
  168. }
  169. bool operator==(const ConnectionToId& connectionToId) const noexcept
  170. {
  171. if (connectionToId.id != id)
  172. return false;
  173. if (connectionToId.groupA != groupA || connectionToId.portA != portA)
  174. return false;
  175. if (connectionToId.groupB != groupB || connectionToId.portB != portB)
  176. return false;
  177. return true;
  178. }
  179. bool operator!=(const ConnectionToId& connectionToId) const noexcept
  180. {
  181. return !operator==(connectionToId);
  182. }
  183. };
  184. struct PatchbayConnectionList {
  185. uint lastId;
  186. LinkedList<ConnectionToId> list;
  187. CarlaMutex mutex;
  188. PatchbayConnectionList() noexcept
  189. : lastId(0),
  190. list(),
  191. mutex() {}
  192. void clear() noexcept
  193. {
  194. lastId = 0;
  195. list.clear();
  196. }
  197. };
  198. // -----------------------------------------------------------------------
  199. #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED