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.

230 lines
5.5KB

  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. void clear() noexcept
  78. {
  79. group = 0;
  80. port = 0;
  81. name[0] = '\0';
  82. fullName[0] = '\0';
  83. }
  84. void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
  85. {
  86. group = g;
  87. port = p;
  88. rename(n, fn);
  89. }
  90. void setFullName(const char fn[]) noexcept
  91. {
  92. std::strncpy(fullName, fn, STR_MAX-1);
  93. fullName[STR_MAX-1] = '\0';
  94. }
  95. void rename(const char n[], const char fn[]) noexcept
  96. {
  97. std::strncpy(name, n, STR_MAX-1);
  98. name[STR_MAX-1] = '\0';
  99. std::strncpy(fullName, fn, STR_MAX-1);
  100. fullName[STR_MAX-1] = '\0';
  101. }
  102. bool operator==(const PortNameToId& portNameToId) noexcept
  103. {
  104. if (portNameToId.group != group || portNameToId.port != port)
  105. return false;
  106. if (std::strncmp(portNameToId.name, name, STR_MAX-1) != 0)
  107. return false;
  108. if (std::strncmp(portNameToId.fullName, fullName, STR_MAX-1) != 0)
  109. return false;
  110. return true;
  111. }
  112. bool operator!=(const PortNameToId& portNameToId) noexcept
  113. {
  114. return !operator==(portNameToId);
  115. }
  116. };
  117. struct PatchbayPortList {
  118. uint lastId;
  119. LinkedList<PortNameToId> list;
  120. CarlaMutex mutex;
  121. PatchbayPortList() noexcept
  122. : lastId(0),
  123. list(),
  124. mutex() {}
  125. void clear() noexcept
  126. {
  127. lastId = 0;
  128. list.clear();
  129. }
  130. // always returns valid pointer (non-null)
  131. const char* getFullPortName(const uint groupId, const uint portId) const noexcept;
  132. const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept;
  133. };
  134. // -----------------------------------------------------------------------
  135. struct ConnectionToId {
  136. uint id;
  137. uint groupA, portA;
  138. uint groupB, portB;
  139. void clear() noexcept
  140. {
  141. // needed for apple GCC4.2
  142. this->id = 0;
  143. groupA = 0;
  144. portA = 0;
  145. groupB = 0;
  146. portB = 0;
  147. }
  148. void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
  149. {
  150. // needed for apple GCC4.2
  151. this->id = i;
  152. groupA = gA;
  153. portA = pA;
  154. groupB = gB;
  155. portB = pB;
  156. }
  157. bool operator==(const ConnectionToId& connectionToId) const noexcept
  158. {
  159. if (connectionToId.id != id)
  160. return false;
  161. if (connectionToId.groupA != groupA || connectionToId.portA != portA)
  162. return false;
  163. if (connectionToId.groupB != groupB || connectionToId.portB != portB)
  164. return false;
  165. return true;
  166. }
  167. bool operator!=(const ConnectionToId& connectionToId) const noexcept
  168. {
  169. return !operator==(connectionToId);
  170. }
  171. };
  172. struct PatchbayConnectionList {
  173. uint lastId;
  174. LinkedList<ConnectionToId> list;
  175. CarlaMutex mutex;
  176. PatchbayConnectionList() noexcept
  177. : lastId(0),
  178. list(),
  179. mutex() {}
  180. void clear() noexcept
  181. {
  182. lastId = 0;
  183. list.clear();
  184. }
  185. };
  186. // -----------------------------------------------------------------------
  187. #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED