jack2 codebase
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.

919 lines
28KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackGraphManager.h"
  17. #include "JackConstants.h"
  18. #include "JackError.h"
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <algorithm>
  22. #ifdef HAVE_TRE_REGEX_H
  23. #include <tre/regex.h>
  24. #else
  25. #include <regex.h>
  26. #endif
  27. namespace Jack
  28. {
  29. static void AssertBufferSize(jack_nframes_t buffer_size)
  30. {
  31. if (buffer_size > BUFFER_SIZE_MAX) {
  32. jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
  33. assert(buffer_size <= BUFFER_SIZE_MAX);
  34. }
  35. }
  36. void JackGraphManager::AssertPort(jack_port_id_t port_index)
  37. {
  38. if (port_index >= fPortMax) {
  39. jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
  40. assert(port_index < fPortMax);
  41. }
  42. }
  43. JackGraphManager* JackGraphManager::Allocate(int port_max)
  44. {
  45. // Using "Placement" new
  46. void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
  47. return new(shared_ptr) JackGraphManager(port_max);
  48. }
  49. void JackGraphManager::Destroy(JackGraphManager* manager)
  50. {
  51. // "Placement" new was used
  52. manager->~JackGraphManager();
  53. JackShmMem::operator delete(manager);
  54. }
  55. JackGraphManager::JackGraphManager(int port_max)
  56. {
  57. assert(port_max <= PORT_NUM_MAX);
  58. for (int i = 0; i < port_max; i++) {
  59. fPortArray[i].Release();
  60. }
  61. fPortMax = port_max;
  62. }
  63. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  64. {
  65. AssertPort(port_index);
  66. return &fPortArray[port_index];
  67. }
  68. jack_default_audio_sample_t* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  69. {
  70. return fPortArray[port_index].GetBuffer();
  71. }
  72. // Server
  73. void JackGraphManager::InitRefNum(int refnum)
  74. {
  75. JackConnectionManager* manager = WriteNextStateStart();
  76. manager->InitRefNum(refnum);
  77. WriteNextStateStop();
  78. }
  79. // RT
  80. void JackGraphManager::RunCurrentGraph()
  81. {
  82. JackConnectionManager* manager = ReadCurrentState();
  83. manager->ResetGraph(fClientTiming);
  84. }
  85. // RT
  86. bool JackGraphManager::RunNextGraph()
  87. {
  88. bool res;
  89. JackConnectionManager* manager = TrySwitchState(&res);
  90. manager->ResetGraph(fClientTiming);
  91. return res;
  92. }
  93. // RT
  94. bool JackGraphManager::IsFinishedGraph()
  95. {
  96. JackConnectionManager* manager = ReadCurrentState();
  97. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  98. }
  99. // RT
  100. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
  101. {
  102. JackConnectionManager* manager = ReadCurrentState();
  103. return manager->ResumeRefNum(control, table, fClientTiming);
  104. }
  105. // RT
  106. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
  107. {
  108. JackConnectionManager* manager = ReadCurrentState();
  109. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  110. }
  111. void JackGraphManager::TopologicalSort(std::vector<jack_int_t>& sorted)
  112. {
  113. UInt16 cur_index;
  114. UInt16 next_index;
  115. do {
  116. cur_index = GetCurrentIndex();
  117. sorted.clear();
  118. ReadCurrentState()->TopologicalSort(sorted);
  119. next_index = GetCurrentIndex();
  120. } while (cur_index != next_index); // Until a coherent state has been read
  121. }
  122. // Server
  123. void JackGraphManager::DirectConnect(int ref1, int ref2)
  124. {
  125. JackConnectionManager* manager = WriteNextStateStart();
  126. manager->DirectConnect(ref1, ref2);
  127. jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  128. WriteNextStateStop();
  129. }
  130. // Server
  131. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  132. {
  133. JackConnectionManager* manager = WriteNextStateStart();
  134. manager->DirectDisconnect(ref1, ref2);
  135. jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  136. WriteNextStateStop();
  137. }
  138. // Server
  139. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  140. {
  141. JackConnectionManager* manager = ReadCurrentState();
  142. return manager->IsDirectConnection(ref1, ref2);
  143. }
  144. // RT
  145. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
  146. {
  147. AssertPort(port_index);
  148. AssertBufferSize(buffer_size);
  149. JackConnectionManager* manager = ReadCurrentState();
  150. JackPort* port = GetPort(port_index);
  151. // This happens when a port has just been unregistered and is still used by the RT code
  152. if (!port->IsUsed()) {
  153. jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
  154. return GetBuffer(0); // port_index 0 is not used
  155. }
  156. jack_int_t len = manager->Connections(port_index);
  157. // Output port
  158. if (port->fFlags & JackPortIsOutput) {
  159. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  160. }
  161. // No connections : return a zero-filled buffer
  162. if (len == 0) {
  163. port->ClearBuffer(buffer_size);
  164. return port->GetBuffer();
  165. // One connection
  166. } else if (len == 1) {
  167. jack_port_id_t src_index = manager->GetPort(port_index, 0);
  168. // Ports in same client : copy the buffer
  169. if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
  170. void* buffers[1];
  171. buffers[0] = GetBuffer(src_index, buffer_size);
  172. port->MixBuffers(buffers, 1, buffer_size);
  173. return port->GetBuffer();
  174. // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
  175. } else {
  176. return GetBuffer(src_index, buffer_size);
  177. }
  178. // Multiple connections : mix all buffers
  179. } else {
  180. const jack_int_t* connections = manager->GetConnections(port_index);
  181. void* buffers[CONNECTION_NUM_FOR_PORT];
  182. jack_port_id_t src_index;
  183. int i;
  184. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  185. AssertPort(src_index);
  186. buffers[i] = GetBuffer(src_index, buffer_size);
  187. }
  188. port->MixBuffers(buffers, i, buffer_size);
  189. return port->GetBuffer();
  190. }
  191. }
  192. // Server
  193. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  194. {
  195. AssertPort(port_index);
  196. JackPort* port = GetPort(port_index);
  197. /**
  198. jackd.h
  199. * If @ref JackPortCanMonitor is set for this @a port, turn input
  200. * monitoring on or off. Otherwise, do nothing.
  201. if (!(fFlags & JackPortCanMonitor))
  202. return -1;
  203. */
  204. port->RequestMonitor(onoff);
  205. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  206. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  207. jack_port_id_t src_index;
  208. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  209. // XXX much worse things will happen if there is a feedback loop !!!
  210. RequestMonitor(src_index, onoff);
  211. }
  212. }
  213. return 0;
  214. }
  215. // Client
  216. jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  217. {
  218. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  219. jack_nframes_t max_latency = 0;
  220. jack_port_id_t dst_index;
  221. if (hop_count > 8)
  222. return GetPort(port_index)->GetLatency();
  223. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  224. if (src_port_index != dst_index) {
  225. AssertPort(dst_index);
  226. JackPort* dst_port = GetPort(dst_index);
  227. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  228. ? dst_port->GetLatency()
  229. : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  230. max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
  231. }
  232. }
  233. return max_latency + GetPort(port_index)->GetLatency();
  234. }
  235. // Client
  236. int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
  237. {
  238. UInt16 cur_index;
  239. UInt16 next_index;
  240. JackPort* port = GetPort(port_index);
  241. AssertPort(port_index);
  242. do {
  243. cur_index = GetCurrentIndex();
  244. port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  245. next_index = GetCurrentIndex();
  246. } while (cur_index != next_index); // Until a coherent state has been read
  247. jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
  248. return 0;
  249. }
  250. // Client
  251. int JackGraphManager::ComputeTotalLatencies()
  252. {
  253. jack_port_id_t port_index;
  254. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  255. JackPort* port = GetPort(port_index);
  256. if (port->IsUsed()) {
  257. ComputeTotalLatency(port_index);
  258. }
  259. }
  260. return 0;
  261. }
  262. void JackGraphManager::RecalculateLatencyAux(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  263. {
  264. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  265. JackPort* port = GetPort(port_index);
  266. jack_latency_range_t latency = { UINT32_MAX, 0 };
  267. jack_port_id_t dst_index;
  268. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  269. AssertPort(dst_index);
  270. JackPort* dst_port = GetPort(dst_index);
  271. jack_latency_range_t other_latency;
  272. dst_port->GetLatencyRange(mode, &other_latency);
  273. if (other_latency.max > latency.max) {
  274. latency.max = other_latency.max;
  275. }
  276. if (other_latency.min < latency.min) {
  277. latency.min = other_latency.min;
  278. }
  279. }
  280. if (latency.min == UINT32_MAX) {
  281. latency.min = 0;
  282. }
  283. port->SetLatencyRange(mode, &latency);
  284. }
  285. void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  286. {
  287. UInt16 cur_index;
  288. UInt16 next_index;
  289. do {
  290. cur_index = GetCurrentIndex();
  291. RecalculateLatencyAux(port_index, mode);
  292. next_index = GetCurrentIndex();
  293. } while (cur_index != next_index); // Until a coherent state has been read
  294. //jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
  295. }
  296. // Server
  297. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  298. {
  299. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  300. jack_port_id_t port_index;
  301. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  302. JackPort* port = GetPort(port_index);
  303. if (port->IsUsed()) {
  304. port->ClearBuffer(buffer_size);
  305. }
  306. }
  307. }
  308. // Server
  309. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  310. {
  311. jack_port_id_t port_index;
  312. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  313. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  314. JackPort* port = GetPort(port_index);
  315. if (!port->IsUsed()) {
  316. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  317. if (!port->Allocate(refnum, port_name, port_type, flags)) {
  318. return NO_PORT;
  319. }
  320. break;
  321. }
  322. }
  323. return (port_index < fPortMax) ? port_index : NO_PORT;
  324. }
  325. // Server
  326. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  327. {
  328. JackConnectionManager* manager = WriteNextStateStart();
  329. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  330. if (port_index != NO_PORT) {
  331. JackPort* port = GetPort(port_index);
  332. assert(port);
  333. port->ClearBuffer(buffer_size);
  334. int res;
  335. if (flags & JackPortIsOutput) {
  336. res = manager->AddOutputPort(refnum, port_index);
  337. } else {
  338. res = manager->AddInputPort(refnum, port_index);
  339. }
  340. // Insertion failure
  341. if (res < 0) {
  342. port->Release();
  343. port_index = NO_PORT;
  344. }
  345. }
  346. WriteNextStateStop();
  347. return port_index;
  348. }
  349. // Server
  350. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  351. {
  352. JackConnectionManager* manager = WriteNextStateStart();
  353. JackPort* port = GetPort(port_index);
  354. int res;
  355. if (port->fFlags & JackPortIsOutput) {
  356. DisconnectAllOutput(port_index);
  357. res = manager->RemoveOutputPort(refnum, port_index);
  358. } else {
  359. DisconnectAllInput(port_index);
  360. res = manager->RemoveInputPort(refnum, port_index);
  361. }
  362. port->Release();
  363. WriteNextStateStop();
  364. return res;
  365. }
  366. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  367. {
  368. JackConnectionManager* manager = WriteNextStateStart();
  369. const jack_int_t* input = manager->GetInputPorts(refnum);
  370. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  371. WriteNextStateStop();
  372. }
  373. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  374. {
  375. JackConnectionManager* manager = WriteNextStateStart();
  376. const jack_int_t* output = manager->GetOutputPorts(refnum);
  377. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  378. WriteNextStateStop();
  379. }
  380. // Server
  381. void JackGraphManager::RemoveAllPorts(int refnum)
  382. {
  383. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  384. JackConnectionManager* manager = WriteNextStateStart();
  385. jack_port_id_t port_index;
  386. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  387. const jack_int_t* input = manager->GetInputPorts(refnum);
  388. while ((port_index = input[0]) != EMPTY) {
  389. int res = ReleasePort(refnum, port_index);
  390. if (res < 0) {
  391. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  392. assert(true);
  393. break;
  394. }
  395. }
  396. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  397. const jack_int_t* output = manager->GetOutputPorts(refnum);
  398. while ((port_index = output[0]) != EMPTY) {
  399. int res = ReleasePort(refnum, port_index);
  400. if (res < 0) {
  401. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  402. assert(true);
  403. break;
  404. }
  405. }
  406. WriteNextStateStop();
  407. }
  408. // Server
  409. void JackGraphManager::DisconnectAllPorts(int refnum)
  410. {
  411. int i;
  412. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  413. JackConnectionManager* manager = WriteNextStateStart();
  414. const jack_int_t* input = manager->GetInputPorts(refnum);
  415. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  416. DisconnectAllInput(input[i]);
  417. }
  418. const jack_int_t* output = manager->GetOutputPorts(refnum);
  419. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  420. DisconnectAllOutput(output[i]);
  421. }
  422. WriteNextStateStop();
  423. }
  424. // Server
  425. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  426. {
  427. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  428. JackConnectionManager* manager = WriteNextStateStart();
  429. for (unsigned int i = 0; i < fPortMax; i++) {
  430. if (manager->IsConnected(i, port_index)) {
  431. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  432. Disconnect(i, port_index);
  433. }
  434. }
  435. WriteNextStateStop();
  436. }
  437. // Server
  438. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  439. {
  440. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  441. JackConnectionManager* manager = WriteNextStateStart();
  442. const jack_int_t* connections = manager->GetConnections(port_index);
  443. while (connections[0] != EMPTY) {
  444. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  445. }
  446. WriteNextStateStop();
  447. }
  448. // Server
  449. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  450. {
  451. AssertPort(port_index);
  452. JackPort* port = GetPort(port_index);
  453. if (port->fFlags & JackPortIsOutput) {
  454. DisconnectAllOutput(port_index);
  455. } else {
  456. DisconnectAllInput(port_index);
  457. }
  458. return 0;
  459. }
  460. // Server
  461. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  462. {
  463. JackConnectionManager* manager = WriteNextStateStart();
  464. const jack_int_t* connections = manager->GetConnections(port_index);
  465. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  466. WriteNextStateStop();
  467. }
  468. // Server
  469. void JackGraphManager::Activate(int refnum)
  470. {
  471. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  472. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  473. }
  474. /*
  475. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  476. (thus unactivated) state may happen where the client is still checked for its end.
  477. */
  478. // Server
  479. void JackGraphManager::Deactivate(int refnum)
  480. {
  481. // Disconnect only when needed
  482. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  483. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  484. } else {
  485. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  486. }
  487. // Disconnect only when needed
  488. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  489. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  490. } else {
  491. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  492. }
  493. }
  494. // Server
  495. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  496. {
  497. AssertPort(port_index);
  498. JackConnectionManager* manager = WriteNextStateStart();
  499. int res = manager->GetInputRefNum(port_index);
  500. WriteNextStateStop();
  501. return res;
  502. }
  503. // Server
  504. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  505. {
  506. AssertPort(port_index);
  507. JackConnectionManager* manager = WriteNextStateStart();
  508. int res = manager->GetOutputRefNum(port_index);
  509. WriteNextStateStop();
  510. return res;
  511. }
  512. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  513. {
  514. JackConnectionManager* manager = WriteNextStateStart();
  515. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  516. JackPort* src = GetPort(port_src);
  517. JackPort* dst = GetPort(port_dst);
  518. int res = 0;
  519. if (!src->fInUse || !dst->fInUse) {
  520. if (!src->fInUse)
  521. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  522. if (!dst->fInUse)
  523. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  524. res = -1;
  525. goto end;
  526. }
  527. if (src->fTypeId != dst->fTypeId) {
  528. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  529. res = -1;
  530. goto end;
  531. }
  532. if (manager->IsConnected(port_src, port_dst)) {
  533. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  534. res = EEXIST;
  535. goto end;
  536. }
  537. res = manager->Connect(port_src, port_dst);
  538. if (res < 0) {
  539. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  540. goto end;
  541. }
  542. res = manager->Connect(port_dst, port_src);
  543. if (res < 0) {
  544. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  545. goto end;
  546. }
  547. if (manager->IsLoopPath(port_src, port_dst)) {
  548. jack_log("JackGraphManager::Connect: LOOP detected");
  549. manager->IncFeedbackConnection(port_src, port_dst);
  550. } else {
  551. manager->IncDirectConnection(port_src, port_dst);
  552. }
  553. end:
  554. WriteNextStateStop();
  555. return res;
  556. }
  557. // Server
  558. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  559. {
  560. JackConnectionManager* manager = WriteNextStateStart();
  561. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  562. bool in_use_src = GetPort(port_src)->fInUse;
  563. bool in_use_dst = GetPort(port_dst)->fInUse;
  564. int res = 0;
  565. if (!in_use_src || !in_use_dst) {
  566. if (!in_use_src)
  567. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  568. if (!in_use_dst)
  569. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  570. res = -1;
  571. goto end;
  572. }
  573. if (!manager->IsConnected(port_src, port_dst)) {
  574. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  575. res = -1;
  576. goto end;
  577. }
  578. res = manager->Disconnect(port_src, port_dst);
  579. if (res < 0) {
  580. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  581. goto end;
  582. }
  583. res = manager->Disconnect(port_dst, port_src);
  584. if (res < 0) {
  585. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  586. goto end;
  587. }
  588. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  589. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  590. manager->DecFeedbackConnection(port_src, port_dst);
  591. } else {
  592. manager->DecDirectConnection(port_src, port_dst);
  593. }
  594. end:
  595. WriteNextStateStop();
  596. return res;
  597. }
  598. // Client
  599. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  600. {
  601. JackConnectionManager* manager = ReadCurrentState();
  602. return manager->IsConnected(port_src, port_dst);
  603. }
  604. // Server
  605. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  606. {
  607. JackPort* src = GetPort(port_src);
  608. JackPort* dst = GetPort(port_dst);
  609. if ((dst->fFlags & JackPortIsInput) == 0) {
  610. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  611. return -1;
  612. }
  613. if ((src->fFlags & JackPortIsOutput) == 0) {
  614. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  615. return -1;
  616. }
  617. return 0;
  618. }
  619. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  620. {
  621. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  622. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  623. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  624. return -1;
  625. }
  626. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  627. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  628. return -1;
  629. }
  630. return 0;
  631. }
  632. // Client : port array
  633. jack_port_id_t JackGraphManager::GetPort(const char* name)
  634. {
  635. for (unsigned int i = 0; i < fPortMax; i++) {
  636. JackPort* port = GetPort(i);
  637. if (port->IsUsed() && port->NameEquals(name)) {
  638. return i;
  639. }
  640. }
  641. return NO_PORT;
  642. }
  643. /*!
  644. \brief Get the connection port name array.
  645. */
  646. // Client
  647. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  648. {
  649. const jack_int_t* connections = manager->GetConnections(port_index);
  650. jack_int_t index;
  651. int i;
  652. // Cleanup connection array
  653. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  654. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  655. JackPort* port = GetPort(index);
  656. res[i] = port->fName;
  657. }
  658. res[i] = NULL;
  659. }
  660. /*
  661. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  662. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  663. read to loop forever.
  664. */
  665. // Client
  666. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  667. {
  668. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  669. UInt16 cur_index, next_index;
  670. if (!res)
  671. return NULL;
  672. do {
  673. cur_index = GetCurrentIndex();
  674. GetConnectionsAux(ReadCurrentState(), res, port_index);
  675. next_index = GetCurrentIndex();
  676. } while (cur_index != next_index); // Until a coherent state has been read
  677. if (res[0]) { // At least one connection
  678. return res;
  679. } else { // Empty array, should return NULL
  680. free(res);
  681. return NULL;
  682. }
  683. }
  684. // Client
  685. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  686. {
  687. // Cleanup port array
  688. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  689. int match_cnt = 0;
  690. regex_t port_regex, type_regex;
  691. if (port_name_pattern && port_name_pattern[0]) {
  692. if (regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB)!=0) {
  693. jack_log("JackGraphManager::GetPortsAux could not compile regex for port_name_pattern '%s'", port_name_pattern);
  694. return;
  695. }
  696. }
  697. if (type_name_pattern && type_name_pattern[0]) {
  698. if (regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB)!=0) {
  699. jack_log("JackGraphManager::GetPortsAux could not compile regex for type_name_pattern '%s'", type_name_pattern);
  700. return;
  701. }
  702. }
  703. for (unsigned int i = 0; i < fPortMax; i++) {
  704. bool matching = true;
  705. JackPort* port = GetPort(i);
  706. if (port->IsUsed()) {
  707. if (flags) {
  708. if ((port->fFlags & flags) != flags) {
  709. matching = false;
  710. }
  711. }
  712. if (matching && port_name_pattern && port_name_pattern[0]) {
  713. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  714. matching = false;
  715. }
  716. }
  717. if (matching && type_name_pattern && type_name_pattern[0]) {
  718. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  719. matching = false;
  720. }
  721. }
  722. if (matching) {
  723. matching_ports[match_cnt++] = port->fName;
  724. }
  725. }
  726. }
  727. matching_ports[match_cnt] = 0;
  728. if (port_name_pattern && port_name_pattern[0]) {
  729. regfree(&port_regex);
  730. }
  731. if (type_name_pattern && type_name_pattern[0]) {
  732. regfree(&type_regex);
  733. }
  734. }
  735. // Client
  736. /*
  737. Check that the state was not changed during the read operation.
  738. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  739. read to loop forever.
  740. */
  741. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  742. {
  743. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  744. UInt16 cur_index, next_index;
  745. if (!res)
  746. return NULL;
  747. do {
  748. cur_index = GetCurrentIndex();
  749. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  750. next_index = GetCurrentIndex();
  751. } while (cur_index != next_index); // Until a coherent state has been read
  752. if (res[0]) { // At least one port
  753. return res;
  754. } else {
  755. free(res); // Empty array, should return NULL
  756. return NULL;
  757. }
  758. }
  759. // Server
  760. void JackGraphManager::Save(JackConnectionManager* dst)
  761. {
  762. JackConnectionManager* manager = WriteNextStateStart();
  763. memcpy(dst, manager, sizeof(JackConnectionManager));
  764. WriteNextStateStop();
  765. }
  766. // Server
  767. void JackGraphManager::Restore(JackConnectionManager* src)
  768. {
  769. JackConnectionManager* manager = WriteNextStateStart();
  770. memcpy(manager, src, sizeof(JackConnectionManager));
  771. WriteNextStateStop();
  772. }
  773. } // end of namespace