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.

275 lines
7.2KB

  1. /*
  2. * Carla patchbay utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  18. #define CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  19. #include "LinkedList.hpp"
  20. #define STR_MAX 0xFF
  21. // -----------------------------------------------------------------------
  22. struct GroupNameToId {
  23. uint group;
  24. char name[STR_MAX+1]; // globally unique
  25. void clear() noexcept
  26. {
  27. group = 0;
  28. name[0] = '\0';
  29. }
  30. void setData(const uint g, const char n[]) noexcept
  31. {
  32. group = g;
  33. rename(n);
  34. }
  35. void rename(const char n[]) noexcept
  36. {
  37. std::strncpy(name, n, STR_MAX);
  38. name[STR_MAX] = '\0';
  39. }
  40. bool operator==(const GroupNameToId& groupNameToId) const noexcept
  41. {
  42. if (groupNameToId.group != group)
  43. return false;
  44. if (std::strncmp(groupNameToId.name, name, STR_MAX) != 0)
  45. return false;
  46. return true;
  47. }
  48. bool operator!=(const GroupNameToId& groupNameToId) const noexcept
  49. {
  50. return !operator==(groupNameToId);
  51. }
  52. };
  53. struct PatchbayGroupList {
  54. uint lastId;
  55. LinkedList<GroupNameToId> list;
  56. PatchbayGroupList() noexcept
  57. : lastId(0),
  58. list() {}
  59. void clear() noexcept
  60. {
  61. lastId = 0;
  62. list.clear();
  63. }
  64. uint getGroupId(const char* const groupName) const noexcept
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', 0);
  67. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  68. {
  69. static const GroupNameToId groupNameFallback = { 0, { '\0' } };
  70. const GroupNameToId& groupNameToId(it.getValue(groupNameFallback));
  71. CARLA_SAFE_ASSERT_CONTINUE(groupNameToId.group != 0);
  72. if (std::strncmp(groupNameToId.name, groupName, STR_MAX) == 0)
  73. return groupNameToId.group;
  74. }
  75. return 0;
  76. }
  77. const char* getGroupName(const uint groupId) const noexcept
  78. {
  79. static const char fallback[] = { '\0' };
  80. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  81. {
  82. static const GroupNameToId groupNameFallback = { 0, { '\0' } };
  83. const GroupNameToId& groupNameToId(it.getValue(groupNameFallback));
  84. CARLA_SAFE_ASSERT_CONTINUE(groupNameToId.group != 0);
  85. if (groupNameToId.group == groupId)
  86. return groupNameToId.name;
  87. }
  88. return fallback;
  89. }
  90. };
  91. // -----------------------------------------------------------------------
  92. struct PortNameToId {
  93. uint group;
  94. uint port;
  95. char name[STR_MAX+1]; // locally unique (within the same group)
  96. char fullName[STR_MAX+1]; // globally unique
  97. void clear() noexcept
  98. {
  99. group = 0;
  100. port = 0;
  101. name[0] = '\0';
  102. fullName[0] = '\0';
  103. }
  104. void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
  105. {
  106. group = g;
  107. port = p;
  108. rename(n, fn);
  109. }
  110. void rename(const char n[], const char fn[]) noexcept
  111. {
  112. std::strncpy(name, n, STR_MAX);
  113. name[STR_MAX] = '\0';
  114. std::strncpy(fullName, fn, STR_MAX);
  115. fullName[STR_MAX] = '\0';
  116. }
  117. bool operator==(const PortNameToId& portNameToId) noexcept
  118. {
  119. if (portNameToId.group != group || portNameToId.port != port)
  120. return false;
  121. if (std::strncmp(portNameToId.name, name, STR_MAX) != 0)
  122. return false;
  123. if (std::strncmp(portNameToId.fullName, fullName, STR_MAX) != 0)
  124. return false;
  125. return true;
  126. }
  127. bool operator!=(const PortNameToId& portNameToId) noexcept
  128. {
  129. return !operator==(portNameToId);
  130. }
  131. };
  132. struct PatchbayPortList {
  133. uint lastId;
  134. LinkedList<PortNameToId> list;
  135. PatchbayPortList() noexcept
  136. : lastId(0),
  137. list() {}
  138. void clear() noexcept
  139. {
  140. lastId = 0;
  141. list.clear();
  142. }
  143. const char* getFullPortName(const uint groupId, const uint portId) const noexcept
  144. {
  145. static const char fallback[] = { '\0' };
  146. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  147. {
  148. static const PortNameToId portNameFallback = { 0, 0, { '\0' }, { '\0' } };
  149. const PortNameToId& portNameToId(it.getValue(portNameFallback));
  150. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  151. if (portNameToId.group == groupId && portNameToId.port == portId)
  152. return portNameToId.fullName;
  153. }
  154. return fallback;
  155. }
  156. const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept
  157. {
  158. static const PortNameToId fallback = { 0, 0, { '\0' }, { '\0' } };
  159. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', fallback);
  160. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  161. {
  162. const PortNameToId& portNameToId(it.getValue(fallback));
  163. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  164. if (std::strncmp(portNameToId.fullName, fullPortName, STR_MAX) == 0)
  165. return portNameToId;
  166. }
  167. return fallback;
  168. }
  169. };
  170. // -----------------------------------------------------------------------
  171. struct ConnectionToId {
  172. uint id;
  173. uint groupA, portA;
  174. uint groupB, portB;
  175. void clear() noexcept
  176. {
  177. id = 0;
  178. groupA = 0;
  179. portA = 0;
  180. groupB = 0;
  181. portB = 0;
  182. }
  183. void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
  184. {
  185. id = i;
  186. groupA = gA;
  187. portA = pA;
  188. groupB = gB;
  189. portB = pB;
  190. }
  191. bool operator==(const ConnectionToId& connectionToId) const noexcept
  192. {
  193. if (connectionToId.id != id)
  194. return false;
  195. if (connectionToId.groupA != groupA || connectionToId.portA != portA)
  196. return false;
  197. if (connectionToId.groupB != groupB || connectionToId.portB != portB)
  198. return false;
  199. return true;
  200. }
  201. bool operator!=(const ConnectionToId& connectionToId) const noexcept
  202. {
  203. return !operator==(connectionToId);
  204. }
  205. };
  206. struct PatchbayConnectionList {
  207. uint lastId;
  208. LinkedList<ConnectionToId> list;
  209. PatchbayConnectionList() noexcept
  210. : lastId(0),
  211. list() {}
  212. void clear() noexcept
  213. {
  214. lastId = 0;
  215. list.clear();
  216. }
  217. };
  218. // -----------------------------------------------------------------------
  219. #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED