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.

907 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. #include <regex.h>
  23. namespace Jack
  24. {
  25. static void AssertBufferSize(jack_nframes_t buffer_size)
  26. {
  27. if (buffer_size > BUFFER_SIZE_MAX) {
  28. jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
  29. assert(buffer_size <= BUFFER_SIZE_MAX);
  30. }
  31. }
  32. void JackGraphManager::AssertPort(jack_port_id_t port_index)
  33. {
  34. if (port_index >= fPortMax) {
  35. jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
  36. assert(port_index < fPortMax);
  37. }
  38. }
  39. JackGraphManager* JackGraphManager::Allocate(int port_max)
  40. {
  41. // Using "Placement" new
  42. void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
  43. return new(shared_ptr) JackGraphManager(port_max);
  44. }
  45. void JackGraphManager::Destroy(JackGraphManager* manager)
  46. {
  47. // "Placement" new was used
  48. manager->~JackGraphManager();
  49. JackShmMem::operator delete(manager);
  50. }
  51. JackGraphManager::JackGraphManager(int port_max)
  52. {
  53. assert(port_max <= PORT_NUM_MAX);
  54. for (int i = 0; i < port_max; i++) {
  55. fPortArray[i].Release();
  56. }
  57. fPortMax = port_max;
  58. }
  59. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  60. {
  61. AssertPort(port_index);
  62. return &fPortArray[port_index];
  63. }
  64. jack_default_audio_sample_t* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  65. {
  66. return fPortArray[port_index].GetBuffer();
  67. }
  68. // Server
  69. void JackGraphManager::InitRefNum(int refnum)
  70. {
  71. JackConnectionManager* manager = WriteNextStateStart();
  72. manager->InitRefNum(refnum);
  73. WriteNextStateStop();
  74. }
  75. // RT
  76. void JackGraphManager::RunCurrentGraph()
  77. {
  78. JackConnectionManager* manager = ReadCurrentState();
  79. manager->ResetGraph(fClientTiming);
  80. }
  81. // RT
  82. bool JackGraphManager::RunNextGraph()
  83. {
  84. bool res;
  85. JackConnectionManager* manager = TrySwitchState(&res);
  86. manager->ResetGraph(fClientTiming);
  87. return res;
  88. }
  89. // RT
  90. bool JackGraphManager::IsFinishedGraph()
  91. {
  92. JackConnectionManager* manager = ReadCurrentState();
  93. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  94. }
  95. // RT
  96. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
  97. {
  98. JackConnectionManager* manager = ReadCurrentState();
  99. return manager->ResumeRefNum(control, table, fClientTiming);
  100. }
  101. // RT
  102. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
  103. {
  104. JackConnectionManager* manager = ReadCurrentState();
  105. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  106. }
  107. void JackGraphManager::TopologicalSort(std::vector<jack_int_t>& sorted)
  108. {
  109. UInt16 cur_index;
  110. UInt16 next_index;
  111. do {
  112. cur_index = GetCurrentIndex();
  113. sorted.clear();
  114. ReadCurrentState()->TopologicalSort(sorted);
  115. next_index = GetCurrentIndex();
  116. } while (cur_index != next_index); // Until a coherent state has been read
  117. }
  118. // Server
  119. void JackGraphManager::DirectConnect(int ref1, int ref2)
  120. {
  121. JackConnectionManager* manager = WriteNextStateStart();
  122. manager->DirectConnect(ref1, ref2);
  123. jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  124. WriteNextStateStop();
  125. }
  126. // Server
  127. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  128. {
  129. JackConnectionManager* manager = WriteNextStateStart();
  130. manager->DirectDisconnect(ref1, ref2);
  131. jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  132. WriteNextStateStop();
  133. }
  134. // Server
  135. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  136. {
  137. JackConnectionManager* manager = ReadCurrentState();
  138. return manager->IsDirectConnection(ref1, ref2);
  139. }
  140. // RT
  141. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size, bool nulled)
  142. {
  143. AssertPort(port_index);
  144. AssertBufferSize(buffer_size);
  145. JackConnectionManager* manager = ReadCurrentState();
  146. JackPort* port = GetPort(port_index);
  147. // This happens when a port has just been unregistered and is still used by the RT code
  148. if (!port->IsUsed()) {
  149. jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
  150. return GetBuffer(0); // port_index 0 is not used
  151. }
  152. jack_int_t len = manager->Connections(port_index);
  153. // Output port
  154. if (port->fFlags & JackPortIsOutput) {
  155. if (port->fTied != NO_PORT) {
  156. return GetBuffer(port->fTied, buffer_size);
  157. } else {
  158. return (len == 0 && nulled) ? NULL : GetBuffer(port_index);
  159. }
  160. }
  161. // No connections : return a zero-filled buffer
  162. if (len == 0) {
  163. port->ClearBuffer(buffer_size);
  164. return (nulled) ? NULL : 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. return 0;
  260. }
  261. void JackGraphManager::RecalculateLatencyAux(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  262. {
  263. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  264. JackPort* port = GetPort(port_index);
  265. jack_latency_range_t latency = { UINT32_MAX, 0 };
  266. jack_port_id_t dst_index;
  267. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  268. AssertPort(dst_index);
  269. JackPort* dst_port = GetPort(dst_index);
  270. jack_latency_range_t other_latency;
  271. dst_port->GetLatencyRange(mode, &other_latency);
  272. if (other_latency.max > latency.max)
  273. latency.max = other_latency.max;
  274. if (other_latency.min < latency.min)
  275. latency.min = other_latency.min;
  276. }
  277. if (latency.min == UINT32_MAX)
  278. latency.min = 0;
  279. port->SetLatencyRange(mode, &latency);
  280. }
  281. void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  282. {
  283. UInt16 cur_index;
  284. UInt16 next_index;
  285. do {
  286. cur_index = GetCurrentIndex();
  287. RecalculateLatencyAux(port_index, mode);
  288. next_index = GetCurrentIndex();
  289. } while (cur_index != next_index); // Until a coherent state has been read
  290. jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
  291. }
  292. // Server
  293. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  294. {
  295. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  296. jack_port_id_t port_index;
  297. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  298. JackPort* port = GetPort(port_index);
  299. if (port->IsUsed())
  300. port->ClearBuffer(buffer_size);
  301. }
  302. }
  303. // Server
  304. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  305. {
  306. jack_port_id_t port_index;
  307. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  308. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  309. JackPort* port = GetPort(port_index);
  310. if (!port->IsUsed()) {
  311. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  312. if (!port->Allocate(refnum, port_name, port_type, flags))
  313. return NO_PORT;
  314. break;
  315. }
  316. }
  317. return (port_index < fPortMax) ? port_index : NO_PORT;
  318. }
  319. // Server
  320. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  321. {
  322. JackConnectionManager* manager = WriteNextStateStart();
  323. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  324. if (port_index != NO_PORT) {
  325. JackPort* port = GetPort(port_index);
  326. assert(port);
  327. port->ClearBuffer(buffer_size);
  328. int res;
  329. if (flags & JackPortIsOutput) {
  330. res = manager->AddOutputPort(refnum, port_index);
  331. } else {
  332. res = manager->AddInputPort(refnum, port_index);
  333. }
  334. // Insertion failure
  335. if (res < 0) {
  336. port->Release();
  337. port_index = NO_PORT;
  338. }
  339. }
  340. WriteNextStateStop();
  341. return port_index;
  342. }
  343. // Server
  344. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  345. {
  346. JackConnectionManager* manager = WriteNextStateStart();
  347. JackPort* port = GetPort(port_index);
  348. int res;
  349. if (port->fFlags & JackPortIsOutput) {
  350. DisconnectAllOutput(port_index);
  351. res = manager->RemoveOutputPort(refnum, port_index);
  352. } else {
  353. DisconnectAllInput(port_index);
  354. res = manager->RemoveInputPort(refnum, port_index);
  355. }
  356. port->Release();
  357. WriteNextStateStop();
  358. return res;
  359. }
  360. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  361. {
  362. JackConnectionManager* manager = WriteNextStateStart();
  363. const jack_int_t* input = manager->GetInputPorts(refnum);
  364. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  365. WriteNextStateStop();
  366. }
  367. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  368. {
  369. JackConnectionManager* manager = WriteNextStateStart();
  370. const jack_int_t* output = manager->GetOutputPorts(refnum);
  371. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  372. WriteNextStateStop();
  373. }
  374. // Server
  375. void JackGraphManager::RemoveAllPorts(int refnum)
  376. {
  377. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  378. JackConnectionManager* manager = WriteNextStateStart();
  379. jack_port_id_t port_index;
  380. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  381. const jack_int_t* input = manager->GetInputPorts(refnum);
  382. while ((port_index = input[0]) != EMPTY) {
  383. int res = ReleasePort(refnum, port_index);
  384. if (res < 0) {
  385. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  386. assert(true);
  387. break;
  388. }
  389. }
  390. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  391. const jack_int_t* output = manager->GetOutputPorts(refnum);
  392. while ((port_index = output[0]) != EMPTY) {
  393. int res = ReleasePort(refnum, port_index);
  394. if (res < 0) {
  395. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  396. assert(true);
  397. break;
  398. }
  399. }
  400. WriteNextStateStop();
  401. }
  402. // Server
  403. void JackGraphManager::DisconnectAllPorts(int refnum)
  404. {
  405. int i;
  406. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  407. JackConnectionManager* manager = WriteNextStateStart();
  408. const jack_int_t* input = manager->GetInputPorts(refnum);
  409. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  410. DisconnectAllInput(input[i]);
  411. }
  412. const jack_int_t* output = manager->GetOutputPorts(refnum);
  413. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  414. DisconnectAllOutput(output[i]);
  415. }
  416. WriteNextStateStop();
  417. }
  418. // Server
  419. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  420. {
  421. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  422. JackConnectionManager* manager = WriteNextStateStart();
  423. for (unsigned int i = 0; i < fPortMax; i++) {
  424. if (manager->IsConnected(i, port_index)) {
  425. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  426. Disconnect(i, port_index);
  427. }
  428. }
  429. WriteNextStateStop();
  430. }
  431. // Server
  432. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  433. {
  434. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  435. JackConnectionManager* manager = WriteNextStateStart();
  436. const jack_int_t* connections = manager->GetConnections(port_index);
  437. while (connections[0] != EMPTY) {
  438. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  439. }
  440. WriteNextStateStop();
  441. }
  442. // Server
  443. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  444. {
  445. AssertPort(port_index);
  446. JackPort* port = GetPort(port_index);
  447. if (port->fFlags & JackPortIsOutput) {
  448. DisconnectAllOutput(port_index);
  449. } else {
  450. DisconnectAllInput(port_index);
  451. }
  452. return 0;
  453. }
  454. // Server
  455. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  456. {
  457. JackConnectionManager* manager = WriteNextStateStart();
  458. const jack_int_t* connections = manager->GetConnections(port_index);
  459. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  460. WriteNextStateStop();
  461. }
  462. // Server
  463. void JackGraphManager::Activate(int refnum)
  464. {
  465. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  466. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  467. }
  468. /*
  469. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  470. (thus unactivated) state may happen where the client is still checked for its end.
  471. */
  472. // Server
  473. void JackGraphManager::Deactivate(int refnum)
  474. {
  475. // Disconnect only when needed
  476. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  477. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  478. } else {
  479. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  480. }
  481. // Disconnect only when needed
  482. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  483. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  484. } else {
  485. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  486. }
  487. }
  488. // Server
  489. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  490. {
  491. AssertPort(port_index);
  492. JackConnectionManager* manager = WriteNextStateStart();
  493. int res = manager->GetInputRefNum(port_index);
  494. WriteNextStateStop();
  495. return res;
  496. }
  497. // Server
  498. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  499. {
  500. AssertPort(port_index);
  501. JackConnectionManager* manager = WriteNextStateStart();
  502. int res = manager->GetOutputRefNum(port_index);
  503. WriteNextStateStop();
  504. return res;
  505. }
  506. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  507. {
  508. JackConnectionManager* manager = WriteNextStateStart();
  509. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  510. JackPort* src = GetPort(port_src);
  511. JackPort* dst = GetPort(port_dst);
  512. int res = 0;
  513. if (!src->fInUse || !dst->fInUse) {
  514. if (!src->fInUse)
  515. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  516. if (!dst->fInUse)
  517. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  518. res = -1;
  519. goto end;
  520. }
  521. if (src->fTypeId != dst->fTypeId) {
  522. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  523. res = -1;
  524. goto end;
  525. }
  526. if (manager->IsConnected(port_src, port_dst)) {
  527. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  528. res = EEXIST;
  529. goto end;
  530. }
  531. res = manager->Connect(port_src, port_dst);
  532. if (res < 0) {
  533. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  534. goto end;
  535. }
  536. res = manager->Connect(port_dst, port_src);
  537. if (res < 0) {
  538. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  539. goto end;
  540. }
  541. if (manager->IsLoopPath(port_src, port_dst)) {
  542. jack_log("JackGraphManager::Connect: LOOP detected");
  543. manager->IncFeedbackConnection(port_src, port_dst);
  544. } else {
  545. manager->IncDirectConnection(port_src, port_dst);
  546. }
  547. end:
  548. WriteNextStateStop();
  549. return res;
  550. }
  551. // Server
  552. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  553. {
  554. JackConnectionManager* manager = WriteNextStateStart();
  555. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  556. bool in_use_src = GetPort(port_src)->fInUse;
  557. bool in_use_dst = GetPort(port_dst)->fInUse;
  558. int res = 0;
  559. if (!in_use_src || !in_use_dst) {
  560. if (!in_use_src)
  561. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  562. if (!in_use_dst)
  563. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  564. res = -1;
  565. goto end;
  566. }
  567. if (!manager->IsConnected(port_src, port_dst)) {
  568. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  569. res = -1;
  570. goto end;
  571. }
  572. res = manager->Disconnect(port_src, port_dst);
  573. if (res < 0) {
  574. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  575. goto end;
  576. }
  577. res = manager->Disconnect(port_dst, port_src);
  578. if (res < 0) {
  579. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  580. goto end;
  581. }
  582. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  583. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  584. manager->DecFeedbackConnection(port_src, port_dst);
  585. } else {
  586. manager->DecDirectConnection(port_src, port_dst);
  587. }
  588. end:
  589. WriteNextStateStop();
  590. return res;
  591. }
  592. // Client
  593. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  594. {
  595. JackConnectionManager* manager = ReadCurrentState();
  596. return manager->IsConnected(port_src, port_dst);
  597. }
  598. // Server
  599. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  600. {
  601. JackPort* src = GetPort(port_src);
  602. JackPort* dst = GetPort(port_dst);
  603. if ((dst->fFlags & JackPortIsInput) == 0) {
  604. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  605. return -1;
  606. }
  607. if ((src->fFlags & JackPortIsOutput) == 0) {
  608. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  609. return -1;
  610. }
  611. return 0;
  612. }
  613. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  614. {
  615. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  616. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  617. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  618. return -1;
  619. }
  620. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  621. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  622. return -1;
  623. }
  624. return 0;
  625. }
  626. // Client : port array
  627. jack_port_id_t JackGraphManager::GetPort(const char* name)
  628. {
  629. for (unsigned int i = 0; i < fPortMax; i++) {
  630. JackPort* port = GetPort(i);
  631. if (port->IsUsed() && port->NameEquals(name))
  632. return i;
  633. }
  634. return NO_PORT;
  635. }
  636. /*!
  637. \brief Get the connection port name array.
  638. */
  639. // Client
  640. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  641. {
  642. const jack_int_t* connections = manager->GetConnections(port_index);
  643. jack_int_t index;
  644. int i;
  645. // Cleanup connection array
  646. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  647. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  648. JackPort* port = GetPort(index);
  649. res[i] = port->fName;
  650. }
  651. res[i] = NULL;
  652. }
  653. /*
  654. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  655. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  656. read to loop forever.
  657. */
  658. // Client
  659. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  660. {
  661. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  662. UInt16 cur_index, next_index;
  663. if (!res)
  664. return NULL;
  665. do {
  666. cur_index = GetCurrentIndex();
  667. GetConnectionsAux(ReadCurrentState(), res, port_index);
  668. next_index = GetCurrentIndex();
  669. } while (cur_index != next_index); // Until a coherent state has been read
  670. if (res[0]) { // At least one connection
  671. return res;
  672. } else { // Empty array, should return NULL
  673. free(res);
  674. return NULL;
  675. }
  676. }
  677. // Client
  678. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  679. {
  680. int match_cnt = 0;
  681. regex_t port_regex, type_regex;
  682. if (port_name_pattern && port_name_pattern[0]) {
  683. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  684. }
  685. if (type_name_pattern && type_name_pattern[0]) {
  686. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  687. }
  688. // Cleanup port array
  689. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  690. for (unsigned int i = 0; i < fPortMax; i++) {
  691. bool matching = true;
  692. JackPort* port = GetPort(i);
  693. if (port->IsUsed()) {
  694. if (flags) {
  695. if ((port->fFlags & flags) != flags) {
  696. matching = false;
  697. }
  698. }
  699. if (matching && port_name_pattern && port_name_pattern[0]) {
  700. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  701. matching = false;
  702. }
  703. }
  704. if (matching && type_name_pattern && type_name_pattern[0]) {
  705. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  706. matching = false;
  707. }
  708. }
  709. if (matching) {
  710. matching_ports[match_cnt++] = port->fName;
  711. }
  712. }
  713. }
  714. matching_ports[match_cnt] = 0;
  715. if (port_name_pattern && port_name_pattern[0]) {
  716. regfree(&port_regex);
  717. }
  718. if (type_name_pattern && type_name_pattern[0]) {
  719. regfree(&type_regex);
  720. }
  721. }
  722. // Client
  723. /*
  724. Check that the state was not changed during the read operation.
  725. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  726. read to loop forever.
  727. */
  728. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  729. {
  730. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  731. UInt16 cur_index, next_index;
  732. if (!res)
  733. return NULL;
  734. do {
  735. cur_index = GetCurrentIndex();
  736. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  737. next_index = GetCurrentIndex();
  738. } while (cur_index != next_index); // Until a coherent state has been read
  739. if (res[0]) { // At least one port
  740. return res;
  741. } else {
  742. free(res); // Empty array, should return NULL
  743. return NULL;
  744. }
  745. }
  746. // Server
  747. void JackGraphManager::Save(JackConnectionManager* dst)
  748. {
  749. JackConnectionManager* manager = WriteNextStateStart();
  750. memcpy(dst, manager, sizeof(JackConnectionManager));
  751. WriteNextStateStop();
  752. }
  753. // Server
  754. void JackGraphManager::Restore(JackConnectionManager* src)
  755. {
  756. JackConnectionManager* manager = WriteNextStateStart();
  757. memcpy(manager, src, sizeof(JackConnectionManager));
  758. WriteNextStateStop();
  759. }
  760. } // end of namespace