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.

262 lines
6.5KB

  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::strcmp(groupNameToId.name, name) != 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. void clear() noexcept
  59. {
  60. lastId = 0;
  61. list.clear();
  62. }
  63. uint getGroupId(const char* const groupName) const noexcept
  64. {
  65. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', 0);
  66. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  67. {
  68. const GroupNameToId& groupNameToId(it.getValue());
  69. if (std::strcmp(groupNameToId.name, groupName) == 0)
  70. return groupNameToId.group;
  71. }
  72. return 0;
  73. }
  74. const char* getGroupName(const uint groupId) const noexcept
  75. {
  76. static const char fallback[] = { '\0' };
  77. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  78. {
  79. const GroupNameToId& groupNameToId(it.getValue());
  80. if (groupNameToId.group == groupId)
  81. return groupNameToId.name;
  82. }
  83. return fallback;
  84. }
  85. };
  86. // -----------------------------------------------------------------------
  87. struct PortNameToId {
  88. uint group;
  89. uint port;
  90. char name[STR_MAX+1]; // locally unique
  91. char fullName[STR_MAX+1]; // globally unique
  92. void clear() noexcept
  93. {
  94. group = 0;
  95. port = 0;
  96. name[0] = '\0';
  97. fullName[0] = '\0';
  98. }
  99. void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
  100. {
  101. group = g;
  102. port = p;
  103. rename(n, fn);
  104. }
  105. void rename(const char n[], const char fn[]) noexcept
  106. {
  107. std::strncpy(name, n, STR_MAX);
  108. name[STR_MAX] = '\0';
  109. std::strncpy(fullName, fn, STR_MAX);
  110. fullName[STR_MAX] = '\0';
  111. }
  112. bool operator==(const PortNameToId& portNameToId) noexcept
  113. {
  114. if (portNameToId.group != group || portNameToId.port != port)
  115. return false;
  116. if (std::strcmp(portNameToId.name, name) != 0)
  117. return false;
  118. if (std::strcmp(portNameToId.fullName, fullName) != 0)
  119. return false;
  120. return true;
  121. }
  122. bool operator!=(const PortNameToId& portNameToId) noexcept
  123. {
  124. return !operator==(portNameToId);
  125. }
  126. };
  127. struct PatchbayPortList {
  128. uint lastId;
  129. LinkedList<PortNameToId> list;
  130. PatchbayPortList() noexcept
  131. : lastId(0) {}
  132. void clear() noexcept
  133. {
  134. lastId = 0;
  135. list.clear();
  136. }
  137. const char* getFullPortName(const uint groupId, const uint portId) const noexcept
  138. {
  139. static const char fallback[] = { '\0' };
  140. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  141. {
  142. const PortNameToId& portNameToId(it.getValue());
  143. if (portNameToId.group == groupId && portNameToId.port == portId)
  144. return portNameToId.fullName;
  145. }
  146. return fallback;
  147. }
  148. const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept
  149. {
  150. static const PortNameToId fallback = { 0, 0, { '\0' }, { '\0' } };
  151. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', fallback);
  152. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  153. {
  154. const PortNameToId& portNameToId(it.getValue());
  155. if (std::strcmp(portNameToId.fullName, fullPortName) == 0)
  156. return portNameToId;
  157. }
  158. return fallback;
  159. }
  160. };
  161. // -----------------------------------------------------------------------
  162. struct ConnectionToId {
  163. uint id;
  164. uint groupA, portA;
  165. uint groupB, portB;
  166. void clear() noexcept
  167. {
  168. id = 0;
  169. groupA = 0;
  170. portA = 0;
  171. groupB = 0;
  172. portB = 0;
  173. }
  174. void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
  175. {
  176. id = i;
  177. groupA = gA;
  178. portA = pA;
  179. groupB = gB;
  180. portB = pB;
  181. }
  182. bool operator==(const ConnectionToId& connectionToId) const noexcept
  183. {
  184. if (connectionToId.id != id)
  185. return false;
  186. if (connectionToId.groupA != groupA || connectionToId.portA != portA)
  187. return false;
  188. if (connectionToId.groupB != groupB || connectionToId.portB != portB)
  189. return false;
  190. return true;
  191. }
  192. bool operator!=(const ConnectionToId& connectionToId) const noexcept
  193. {
  194. return !operator==(connectionToId);
  195. }
  196. };
  197. struct PatchbayConnectionList {
  198. uint lastId;
  199. LinkedList<ConnectionToId> list;
  200. PatchbayConnectionList() noexcept
  201. : lastId(0) {}
  202. void clear() noexcept
  203. {
  204. lastId = 0;
  205. list.clear();
  206. }
  207. };
  208. // -----------------------------------------------------------------------
  209. #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED