jack1 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.

1799 lines
40KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. $Id$
  15. */
  16. #include <sys/socket.h>
  17. #include <sys/un.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/ipc.h>
  23. #include <sys/shm.h>
  24. #include <sys/mman.h>
  25. #include <sys/poll.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <regex.h>
  29. #include <math.h>
  30. #include <asm/msr.h>
  31. #include <jack/jack.h>
  32. #include <jack/internal.h>
  33. #include <jack/engine.h>
  34. #include <jack/pool.h>
  35. #include <jack/error.h>
  36. char *jack_temp_dir = "/tmp";
  37. void
  38. jack_set_temp_dir (const char *path)
  39. {
  40. jack_temp_dir = strdup (path);
  41. }
  42. static jack_port_t *jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control);
  43. static pthread_mutex_t client_lock;
  44. static pthread_cond_t client_ready;
  45. void *jack_zero_filled_buffer = 0;
  46. static void jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes);
  47. jack_port_type_info_t builtin_port_types[] = {
  48. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  49. { "", NULL }
  50. };
  51. struct _jack_client {
  52. jack_control_t *engine;
  53. jack_client_control_t *control;
  54. struct pollfd *pollfd;
  55. int pollmax;
  56. int graph_next_fd;
  57. int request_fd;
  58. GSList *port_segments;
  59. GSList *ports;
  60. pthread_t thread;
  61. char fifo_prefix[PATH_MAX+1];
  62. void (*on_shutdown)(void *arg);
  63. void *on_shutdown_arg;
  64. char thread_ok : 1;
  65. char first_active : 1;
  66. };
  67. #define event_fd pollfd[0].fd
  68. #define graph_wait_fd pollfd[1].fd
  69. typedef struct {
  70. int status;
  71. struct _jack_client *client;
  72. const char *client_name;
  73. } client_info;
  74. static void
  75. default_jack_error (const char *fmt, ...)
  76. {
  77. va_list ap;
  78. va_start (ap, fmt);
  79. vfprintf (stderr, fmt, ap);
  80. va_end (ap);
  81. fputc ('\n', stderr);
  82. }
  83. void (*jack_error)(const char *fmt, ...) = &default_jack_error;
  84. jack_client_t *
  85. jack_client_alloc ()
  86. {
  87. jack_client_t *client;
  88. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  89. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  90. client->pollmax = 2;
  91. client->request_fd = -1;
  92. client->event_fd = -1;
  93. client->graph_wait_fd = -1;
  94. client->graph_next_fd = -1;
  95. client->port_segments = NULL;
  96. client->ports = NULL;
  97. client->engine = NULL;
  98. client->control = 0;
  99. client->thread_ok = FALSE;
  100. client->first_active = TRUE;
  101. client->on_shutdown = NULL;
  102. return client;
  103. }
  104. static jack_port_t *
  105. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  106. {
  107. GSList *node;
  108. for (node = client->ports; node; node = g_slist_next (node)) {
  109. if (((jack_port_t *) node->data)->shared->id == id) {
  110. return (jack_port_t *) node->data;
  111. }
  112. }
  113. return NULL;
  114. }
  115. jack_port_t *
  116. jack_port_by_name (jack_client_t *client, const char *port_name)
  117. {
  118. unsigned long i, limit;
  119. jack_port_shared_t *port;
  120. limit = client->engine->port_max;
  121. port = &client->engine->ports[0];
  122. for (i = 0; i < limit; i++) {
  123. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  124. return jack_port_new (client, port[i].id, client->engine);
  125. }
  126. }
  127. return NULL;
  128. }
  129. static void
  130. jack_client_invalidate_port_buffers (jack_client_t *client)
  131. {
  132. GSList *node;
  133. jack_port_t *port;
  134. /* This releases all local memory owned by input ports
  135. and sets the buffer pointer to NULL. This will cause
  136. jack_port_get_buffer() to reallocate space for the
  137. buffer on the next call (if there is one).
  138. */
  139. for (node = client->ports; node; node = g_slist_next (node)) {
  140. port = (jack_port_t *) node->data;
  141. if (port->shared->flags & JackPortIsInput) {
  142. if (port->client_segment_base == 0) {
  143. jack_pool_release ((void *) port->shared->offset);
  144. port->client_segment_base = 0;
  145. port->shared->offset = 0;
  146. }
  147. }
  148. }
  149. }
  150. int
  151. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  152. {
  153. jack_port_t *control_port;
  154. jack_port_t *other;
  155. GSList *node;
  156. switch (event->type) {
  157. case PortConnected:
  158. other = jack_port_new (client, event->y.other_id, client->engine);
  159. control_port = jack_port_by_id (client, event->x.self_id);
  160. pthread_mutex_lock (&control_port->connection_lock);
  161. control_port->connections = g_slist_prepend (control_port->connections, other);
  162. pthread_mutex_unlock (&control_port->connection_lock);
  163. break;
  164. case PortDisconnected:
  165. control_port = jack_port_by_id (client, event->x.self_id);
  166. pthread_mutex_lock (&control_port->connection_lock);
  167. for (node = control_port->connections; node; node = g_slist_next (node)) {
  168. other = (jack_port_t *) node->data;
  169. if (other->shared->id == event->y.other_id) {
  170. control_port->connections = g_slist_remove_link (control_port->connections, node);
  171. g_slist_free_1 (node);
  172. free (other);
  173. break;
  174. }
  175. }
  176. pthread_mutex_unlock (&control_port->connection_lock);
  177. break;
  178. default:
  179. /* impossible */
  180. break;
  181. }
  182. return 0;
  183. }
  184. static int
  185. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  186. {
  187. char path[PATH_MAX+1];
  188. if (client->graph_wait_fd >= 0) {
  189. close (client->graph_wait_fd);
  190. client->graph_wait_fd = -1;
  191. }
  192. if (client->graph_next_fd >= 0) {
  193. close (client->graph_next_fd);
  194. client->graph_next_fd = -1;
  195. }
  196. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  197. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) <= 0) {
  198. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  199. return -1;
  200. }
  201. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  202. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  203. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  204. return -1;
  205. }
  206. /* If the client registered its own callback for graph order events,
  207. execute it now.
  208. */
  209. if (client->control->graph_order) {
  210. client->control->graph_order (client->control->graph_order_arg);
  211. }
  212. return 0;
  213. }
  214. static int
  215. server_connect (int which)
  216. {
  217. int fd;
  218. struct sockaddr_un addr;
  219. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  220. jack_error ("cannot create client socket (%s)", strerror (errno));
  221. return -1;
  222. }
  223. addr.sun_family = AF_UNIX;
  224. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, which);
  225. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  226. jack_error ("cannot connect to jack server", strerror (errno));
  227. close (fd);
  228. return -1;
  229. }
  230. return fd;
  231. }
  232. static int
  233. server_event_connect (jack_client_t *client)
  234. {
  235. int fd;
  236. struct sockaddr_un addr;
  237. jack_client_connect_ack_request_t req;
  238. jack_client_connect_ack_result_t res;
  239. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  240. jack_error ("cannot create client event socket (%s)", strerror (errno));
  241. return -1;
  242. }
  243. addr.sun_family = AF_UNIX;
  244. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_temp_dir);
  245. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  246. jack_error ("cannot connect to jack server for events", strerror (errno));
  247. close (fd);
  248. return -1;
  249. }
  250. req.client_id = client->control->id;
  251. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  252. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  253. close (fd);
  254. return -1;
  255. }
  256. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  257. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  258. close (fd);
  259. return -1;
  260. }
  261. if (res.status != 0) {
  262. close (fd);
  263. return -1;
  264. }
  265. return fd;
  266. }
  267. jack_client_t *
  268. jack_client_new (const char *client_name)
  269. {
  270. int req_fd = -1;
  271. int ev_fd = -1;
  272. void *addr;
  273. jack_client_connect_request_t req;
  274. jack_client_connect_result_t res;
  275. jack_port_segment_info_t *si;
  276. jack_client_t *client;
  277. int client_shm_id;
  278. int control_shm_id;
  279. int port_segment_shm_id;
  280. int n;
  281. if (strlen (client_name) > sizeof (req.name) - 1) {
  282. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  283. "Please use %lu characters or less.",
  284. sizeof (req.name) - 1);
  285. return NULL;
  286. }
  287. if ((req_fd = server_connect (0)) < 0) {
  288. jack_error ("cannot connect to default JACK server");
  289. return NULL;
  290. }
  291. req.type = ClientOutOfProcess;
  292. strncpy (req.name, client_name, sizeof (req.name) - 1);
  293. if (write (req_fd, &req, sizeof (req)) != sizeof (req)) {
  294. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  295. close (req_fd);
  296. return NULL;
  297. }
  298. if ((n = read (req_fd, &res, sizeof (res))) != sizeof (res)) {
  299. if (errno == 0) {
  300. /* server shut the socket */
  301. jack_error ("could not attach as client (duplicate client name?)");
  302. close (req_fd);
  303. return NULL;
  304. }
  305. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  306. close (req_fd);
  307. return NULL;
  308. }
  309. if (res.status) {
  310. close (req_fd);
  311. jack_error ("could not attach as client (duplicate client name?)");
  312. return NULL;
  313. }
  314. client = jack_client_alloc ();
  315. strcpy (client->fifo_prefix, res.fifo_prefix);
  316. client->request_fd = req_fd;
  317. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  318. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  319. /* Lookup, attach and register the port/buffer segments in use
  320. right now.
  321. */
  322. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  323. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  324. goto fail;
  325. }
  326. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  327. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  328. goto fail;
  329. }
  330. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  331. si->shm_key = res.port_segment_key;
  332. si->address = addr;
  333. /* the first chunk of the first port segment is always set by the engine
  334. to be a conveniently-sized, zero-filled lump of memory.
  335. */
  336. if (client->port_segments == NULL) {
  337. jack_zero_filled_buffer = si->address;
  338. }
  339. client->port_segments = g_slist_prepend (client->port_segments, si);
  340. /* attach the engine control/info block */
  341. if ((control_shm_id = shmget (res.control_key, 0, 0)) < 0) {
  342. jack_error ("cannot determine shared memory segment for control key 0x%x", res.control_key);
  343. goto fail;
  344. }
  345. if ((addr = shmat (control_shm_id, 0, 0)) == (void *) -1) {
  346. jack_error ("cannot attached engine control shared memory segment");
  347. goto fail;
  348. }
  349. client->engine = (jack_control_t *) addr;
  350. /* now attach the client control block */
  351. if ((client_shm_id = shmget (res.client_key, 0, 0)) < 0) {
  352. jack_error ("cannot determine shared memory segment for client key 0x%x", res.client_key);
  353. goto fail;
  354. }
  355. if ((addr = shmat (client_shm_id, 0, 0)) == (void *) -1) {
  356. jack_error ("cannot attached client control shared memory segment");
  357. goto fail;
  358. }
  359. client->control = (jack_client_control_t *) addr;
  360. if ((ev_fd = server_event_connect (client)) < 0) {
  361. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  362. goto fail;
  363. }
  364. client->event_fd = ev_fd;
  365. return client;
  366. fail:
  367. if (client->engine) {
  368. shmdt (client->engine);
  369. }
  370. if (client->control) {
  371. shmdt ((char *) client->control);
  372. }
  373. if (req_fd >= 0) {
  374. close (req_fd);
  375. }
  376. if (ev_fd >= 0) {
  377. close (ev_fd);
  378. }
  379. return 0;
  380. }
  381. static void *
  382. jack_client_thread (void *arg)
  383. {
  384. jack_client_t *client = (jack_client_t *) arg;
  385. jack_client_control_t *control = client->control;
  386. jack_event_t event;
  387. char status = 0;
  388. char c;
  389. int err = 0;
  390. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  391. pthread_mutex_lock (&client_lock);
  392. client->thread_ok = TRUE;
  393. pthread_cond_signal (&client_ready);
  394. pthread_mutex_unlock (&client_lock);
  395. while (err == 0) {
  396. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  397. if (errno == EINTR) {
  398. printf ("poll interrupted\n");
  399. continue;
  400. }
  401. jack_error ("poll failed in client (%s)", strerror (errno));
  402. status = -1;
  403. break;
  404. }
  405. if (client->pollfd[0].revents & ~POLLIN) {
  406. jack_error ("engine has shut down socket; thread exiting");
  407. if (client->on_shutdown) {
  408. client->on_shutdown (client->on_shutdown_arg);
  409. }
  410. pthread_exit (0);
  411. }
  412. if (client->pollfd[0].revents & POLLIN) {
  413. /* server has sent us an event. process the event and reply */
  414. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  415. jack_error ("cannot read server event (%s)", strerror (errno));
  416. err++;
  417. break;
  418. }
  419. status = 0;
  420. switch (event.type) {
  421. case PortRegistered:
  422. if (control->port_register) {
  423. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  424. }
  425. break;
  426. case PortUnregistered:
  427. if (control->port_register) {
  428. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  429. }
  430. break;
  431. case GraphReordered:
  432. status = jack_handle_reorder (client, &event);
  433. break;
  434. case PortConnected:
  435. case PortDisconnected:
  436. status = jack_client_handle_port_connection (client, &event);
  437. break;
  438. case BufferSizeChange:
  439. jack_client_invalidate_port_buffers (client);
  440. if (control->bufsize) {
  441. status = control->bufsize (control->nframes, control->bufsize_arg);
  442. }
  443. break;
  444. case SampleRateChange:
  445. if (control->srate) {
  446. status = control->srate (control->nframes, control->srate_arg);
  447. }
  448. break;
  449. case XRun:
  450. if (control->xrun) {
  451. status = control->xrun (control->xrun_arg);
  452. }
  453. break;
  454. case NewPortBufferSegment:
  455. break;
  456. }
  457. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  458. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  459. err++;
  460. break;
  461. }
  462. }
  463. if (client->pollfd[1].revents & POLLIN) {
  464. rdtscll (control->signalled_at);
  465. control->state = Running;
  466. if (control->process) {
  467. if (control->process (control->nframes, control->process_arg) == 0) {
  468. control->state = Finished;
  469. }
  470. } else {
  471. control->state = Finished;
  472. }
  473. rdtscll (control->finished_at);
  474. /* pass the execution token along */
  475. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  476. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  477. err++;
  478. break;
  479. }
  480. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  481. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  482. err++;
  483. break;
  484. }
  485. }
  486. }
  487. return (void *) err;
  488. }
  489. static int
  490. jack_start_thread (jack_client_t *client)
  491. {
  492. pthread_attr_t *attributes = 0;
  493. if (client->engine->real_time) {
  494. /* Get the client thread to run as an RT-FIFO
  495. scheduled thread of appropriate priority.
  496. */
  497. struct sched_param rt_param;
  498. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  499. pthread_attr_init (attributes);
  500. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  501. jack_error ("cannot set FIFO scheduling class for RT thread");
  502. return -1;
  503. }
  504. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  505. jack_error ("Cannot set scheduling scope for RT thread");
  506. return -1;
  507. }
  508. memset (&rt_param, 0, sizeof (rt_param));
  509. rt_param.sched_priority = client->engine->client_priority;
  510. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  511. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  512. return -1;
  513. }
  514. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  515. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  516. return -1;
  517. }
  518. }
  519. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  520. return -1;
  521. }
  522. return 0;
  523. }
  524. int
  525. jack_activate (jack_client_t *client)
  526. {
  527. jack_request_t req;
  528. #define BIG_ENOUGH_STACK 1048576
  529. char buf[BIG_ENOUGH_STACK];
  530. int i;
  531. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  532. buf[i] = (char) (i & 0xff);
  533. }
  534. #undef BIG_ENOUGH_STACK
  535. if (client->control->type == ClientOutOfProcess && client->first_active) {
  536. pthread_mutex_init (&client_lock, NULL);
  537. pthread_cond_init (&client_ready, NULL);
  538. pthread_mutex_lock (&client_lock);
  539. if (jack_start_thread (client)) {
  540. pthread_mutex_unlock (&client_lock);
  541. return -1;
  542. }
  543. pthread_cond_wait (&client_ready, &client_lock);
  544. pthread_mutex_unlock (&client_lock);
  545. if (!client->thread_ok) {
  546. jack_error ("could not start client thread");
  547. return -1;
  548. }
  549. client->first_active = FALSE;
  550. }
  551. req.type = ActivateClient;
  552. req.x.client_id = client->control->id;
  553. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  554. jack_error ("cannot send activate client request to server");
  555. return -1;
  556. }
  557. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  558. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  559. return -1;
  560. }
  561. return req.status;
  562. }
  563. int
  564. jack_deactivate (jack_client_t *client)
  565. {
  566. jack_request_t req;
  567. req.type = DeactivateClient;
  568. req.x.client_id = client->control->id;
  569. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  570. jack_error ("cannot send activate client request to server");
  571. return -1;
  572. }
  573. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  574. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  575. return -1;
  576. }
  577. return req.status;
  578. }
  579. int
  580. jack_client_close (jack_client_t *client)
  581. {
  582. GSList *node;
  583. void *status;
  584. /* stop the thread that communicates with the jack server */
  585. pthread_cancel (client->thread);
  586. pthread_join (client->thread, &status);
  587. shmdt ((char *) client->control);
  588. shmdt (client->engine);
  589. for (node = client->port_segments; node; node = g_slist_next (node)) {
  590. shmdt (((jack_port_segment_info_t *) node->data)->address);
  591. free (node->data);
  592. }
  593. g_slist_free (client->port_segments);
  594. for (node = client->ports; node; node = g_slist_next (node)) {
  595. free (node->data);
  596. }
  597. g_slist_free (client->ports);
  598. if (client->graph_wait_fd) {
  599. close (client->graph_wait_fd);
  600. }
  601. if (client->graph_next_fd) {
  602. close (client->graph_next_fd);
  603. }
  604. close (client->event_fd);
  605. close (client->request_fd);
  606. free (client->pollfd);
  607. free (client);
  608. return 0;
  609. }
  610. int
  611. jack_load_client (const char *client_name, const char *path_to_so)
  612. {
  613. int fd;
  614. jack_client_connect_request_t req;
  615. jack_client_connect_result_t res;
  616. if ((fd = server_connect (0)) < 0) {
  617. jack_error ("cannot connect to jack server");
  618. return 0;
  619. }
  620. req.type = ClientDynamic;
  621. strncpy (req.name, client_name, sizeof (req.name) - 1);
  622. req.name[sizeof(req.name)-1] = '\0';
  623. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  624. req.object_path[sizeof(req.object_path)-1] = '\0';
  625. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  626. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  627. close (fd);
  628. return 0;
  629. }
  630. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  631. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  632. close (fd);
  633. return 0;
  634. }
  635. close (fd);
  636. return res.status;
  637. }
  638. jack_client_t *
  639. jack_driver_become_client (const char *client_name)
  640. {
  641. int fd;
  642. jack_client_connect_request_t req;
  643. jack_client_connect_result_t res;
  644. jack_client_t *client = 0;
  645. int port_segment_shm_id;
  646. jack_port_segment_info_t *si;
  647. void *addr;
  648. if ((fd = server_connect (0)) < 0) {
  649. jack_error ("cannot connect to jack server");
  650. return 0;
  651. }
  652. req.type = ClientDriver;
  653. strncpy (req.name, client_name, sizeof (req.name) - 1);
  654. req.name[sizeof(req.name)-1] = '\0';
  655. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  656. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  657. close (fd);
  658. return 0;
  659. }
  660. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  661. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  662. close (fd);
  663. return 0;
  664. }
  665. if (res.status) {
  666. return 0;
  667. }
  668. client = jack_client_alloc ();
  669. client->request_fd = fd;
  670. client->control = res.client_control;
  671. client->engine = res.engine_control;
  672. /* Lookup, attach and register the port/buffer segments in use
  673. right now.
  674. */
  675. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  676. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  677. return NULL;
  678. }
  679. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  680. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  681. return NULL;
  682. }
  683. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  684. si->shm_key = res.port_segment_key;
  685. si->address = addr;
  686. /* the first chunk of the first port segment is always set by the engine
  687. to be a conveniently-sized, zero-filled lump of memory.
  688. */
  689. if (client->port_segments == NULL) {
  690. jack_zero_filled_buffer = si->address;
  691. }
  692. client->port_segments = g_slist_prepend (client->port_segments, si);
  693. /* allow the engine to act on the client's behalf
  694. when dealing with in-process clients.
  695. */
  696. client->control->private_internal_client = client;
  697. return client;
  698. }
  699. unsigned long jack_get_buffer_size (jack_client_t *client)
  700. {
  701. return client->engine->buffer_size;
  702. }
  703. unsigned long jack_get_sample_rate (jack_client_t *client)
  704. {
  705. return client->engine->time.frame_rate;
  706. }
  707. static jack_port_t *
  708. jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  709. {
  710. jack_port_t *port;
  711. jack_port_shared_t *shared;
  712. jack_port_segment_info_t *si;
  713. GSList *node;
  714. shared = &control->ports[port_id];
  715. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  716. port->client_segment_base = 0;
  717. port->shared = shared;
  718. pthread_mutex_init (&port->connection_lock, NULL);
  719. port->connections = 0;
  720. port->tied = NULL;
  721. si = NULL;
  722. for (node = client->port_segments; node; node = g_slist_next (node)) {
  723. si = (jack_port_segment_info_t *) node->data;
  724. if (si->shm_key == port->shared->shm_key) {
  725. break;
  726. }
  727. }
  728. if (si == NULL) {
  729. jack_error ("cannot find port segment to match newly registered port\n");
  730. return NULL;
  731. }
  732. port->client_segment_base = si->address;
  733. return port;
  734. }
  735. jack_port_t *
  736. jack_port_register (jack_client_t *client,
  737. const char *port_name,
  738. const char *port_type,
  739. unsigned long flags,
  740. unsigned long buffer_size)
  741. {
  742. jack_request_t req;
  743. jack_port_t *port = 0;
  744. jack_port_type_info_t *type_info;
  745. int n;
  746. req.type = RegisterPort;
  747. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  748. strcat ((char *) req.x.port_info.name, ":");
  749. strcat ((char *) req.x.port_info.name, port_name);
  750. strncpy (req.x.port_info.type, port_type, sizeof (req.x.port_info.type) - 1);
  751. req.x.port_info.flags = flags;
  752. req.x.port_info.buffer_size = buffer_size;
  753. req.x.port_info.client_id = client->control->id;
  754. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  755. jack_error ("cannot send port registration request to server");
  756. return 0;
  757. }
  758. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  759. jack_error ("cannot read port registration result from server");
  760. return 0;
  761. }
  762. if (req.status != 0) {
  763. return NULL;
  764. }
  765. port = jack_port_new (client, req.x.port_info.port_id, client->engine);
  766. type_info = NULL;
  767. for (n = 0; builtin_port_types[n].type_name[0]; n++) {
  768. if (strcmp (req.x.port_info.type, builtin_port_types[n].type_name) == 0) {
  769. type_info = &builtin_port_types[n];
  770. break;
  771. }
  772. }
  773. if (type_info == NULL) {
  774. /* not a builtin type, so allocate a new type_info structure,
  775. and fill it appropriately.
  776. */
  777. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  778. snprintf ((char *) type_info->type_name, sizeof (type_info->type_name), req.x.port_info.type);
  779. type_info->mixdown = NULL; /* we have no idea how to mix this */
  780. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  781. }
  782. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  783. client->ports = g_slist_prepend (client->ports, port);
  784. return port;
  785. }
  786. int
  787. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  788. {
  789. jack_request_t req;
  790. req.type = UnRegisterPort;
  791. req.x.port_info.port_id = port->shared->id;
  792. req.x.port_info.client_id = client->control->id;
  793. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  794. jack_error ("cannot send port registration request to server");
  795. return -1;
  796. }
  797. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  798. jack_error ("cannot read port registration result from server");
  799. return -1;
  800. }
  801. return req.status;
  802. }
  803. int
  804. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  805. {
  806. jack_request_t req;
  807. req.type = ConnectPorts;
  808. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  809. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  810. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  811. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  812. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  813. jack_error ("cannot send port connection request to server");
  814. return -1;
  815. }
  816. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  817. jack_error ("cannot read port connection result from server");
  818. return -1;
  819. }
  820. return req.status;
  821. }
  822. int
  823. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  824. {
  825. jack_request_t req;
  826. pthread_mutex_lock (&port->connection_lock);
  827. if (port->connections == NULL) {
  828. pthread_mutex_unlock (&port->connection_lock);
  829. return 0;
  830. }
  831. pthread_mutex_unlock (&port->connection_lock);
  832. req.type = DisconnectPort;
  833. req.x.port_info.port_id = port->shared->id;
  834. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  835. jack_error ("cannot send port disconnect request to server");
  836. return -1;
  837. }
  838. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  839. jack_error ("cannot read port disconnect result from server");
  840. return -1;
  841. }
  842. return req.status;
  843. }
  844. int
  845. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  846. {
  847. jack_request_t req;
  848. req.type = DisconnectPorts;
  849. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  850. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  851. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  852. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  853. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  854. jack_error ("cannot send port connection request to server");
  855. return -1;
  856. }
  857. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  858. jack_error ("cannot read port connection result from server");
  859. return -1;
  860. }
  861. return req.status;
  862. }
  863. int
  864. jack_engine_takeover_timebase (jack_client_t *client)
  865. {
  866. jack_request_t req;
  867. req.type = SetTimeBaseClient;
  868. req.x.client_id = client->control->id;
  869. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  870. jack_error ("cannot send set time base request to server");
  871. return -1;
  872. }
  873. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  874. jack_error ("cannot read set time base result from server");
  875. return -1;
  876. }
  877. return req.status;
  878. }
  879. void
  880. jack_set_error_function (void (*func) (const char *, ...))
  881. {
  882. jack_error = func;
  883. }
  884. nframes_t
  885. jack_port_get_latency (jack_port_t *port)
  886. {
  887. return port->shared->latency;
  888. }
  889. void
  890. jack_port_set_latency (jack_port_t *port, nframes_t nframes)
  891. {
  892. port->shared->latency = nframes;
  893. }
  894. void *
  895. jack_port_get_buffer (jack_port_t *port, nframes_t nframes)
  896. {
  897. GSList *node, *next;
  898. /* Output port. The buffer was assigned by the engine
  899. when the port was registered.
  900. */
  901. if (port->shared->flags & JackPortIsOutput) {
  902. if (port->tied) {
  903. return jack_port_get_buffer (port->tied, nframes);
  904. }
  905. return jack_port_buffer (port);
  906. }
  907. /* Input port.
  908. */
  909. /* since this can only be called from the process() callback,
  910. and since no connections can be made/broken during this
  911. phase (enforced by the jack server), there is no need
  912. to take the connection lock here
  913. */
  914. if ((node = port->connections) == NULL) {
  915. /* no connections; return a zero-filled buffer */
  916. return jack_zero_filled_buffer;
  917. }
  918. if ((next = g_slist_next (node)) == NULL) {
  919. /* one connection: use zero-copy mode - just pass
  920. the buffer of the connected (output) port.
  921. */
  922. return jack_port_buffer (((jack_port_t *) node->data));
  923. }
  924. /* multiple connections. use a local buffer and mixdown
  925. the incoming data to that buffer. we have already
  926. established the existence of a mixdown function
  927. during the connection process.
  928. no port can have an offset of 0 - that offset refers
  929. to the zero-filled area at the start of a shared port
  930. segment area. so, use the offset to store the location
  931. of a locally allocated buffer, and reset the client_segment_base
  932. so that the jack_port_buffer() computation works correctly.
  933. */
  934. if (port->shared->offset == 0) {
  935. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  936. sizeof (sample_t) * nframes);
  937. port->client_segment_base = 0;
  938. }
  939. port->shared->type_info.mixdown (port, nframes);
  940. return (sample_t *) port->shared->offset;
  941. }
  942. int
  943. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  944. {
  945. if (dst->shared->client_id != src->shared->client_id) {
  946. jack_error ("cannot tie ports not owned by the same client");
  947. return -1;
  948. }
  949. if (dst->shared->flags & JackPortIsOutput) {
  950. jack_error ("cannot tie an input port");
  951. return -1;
  952. }
  953. dst->tied = src;
  954. return 0;
  955. }
  956. int
  957. jack_port_untie (jack_port_t *port)
  958. {
  959. if (port->tied == NULL) {
  960. jack_error ("port \"%s\" is not tied", port->shared->name);
  961. return -1;
  962. }
  963. port->tied = NULL;
  964. return 0;
  965. }
  966. int
  967. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  968. {
  969. if (client->control->active) {
  970. return -1;
  971. }
  972. client->control->graph_order = callback;
  973. client->control->graph_order_arg = arg;
  974. return 0;
  975. }
  976. int
  977. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  978. {
  979. if (client->control->active) {
  980. return -1;
  981. }
  982. client->control->process_arg = arg;
  983. client->control->process = callback;
  984. return 0;
  985. }
  986. int
  987. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  988. {
  989. if (client->control->active) {
  990. return -1;
  991. }
  992. client->control->bufsize_arg = arg;
  993. client->control->bufsize = callback;
  994. /* Now invoke it */
  995. callback (client->engine->buffer_size, arg);
  996. return 0;
  997. }
  998. int
  999. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  1000. {
  1001. if (client->control->active) {
  1002. return -1;
  1003. }
  1004. client->control->srate_arg = arg;
  1005. client->control->srate = callback;
  1006. /* Now invoke it */
  1007. callback (client->engine->time.frame_rate, arg);
  1008. return 0;
  1009. }
  1010. int
  1011. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1012. {
  1013. if (client->control->active) {
  1014. return -1;
  1015. }
  1016. client->control->port_register_arg = arg;
  1017. client->control->port_register = callback;
  1018. return 0;
  1019. }
  1020. int
  1021. jack_get_process_start_fd (jack_client_t *client)
  1022. {
  1023. /* once this has been called, the client thread
  1024. does not sleep on the graph wait fd.
  1025. */
  1026. client->pollmax = 1;
  1027. return client->graph_wait_fd;
  1028. }
  1029. int
  1030. jack_get_process_done_fd (jack_client_t *client)
  1031. {
  1032. return client->graph_next_fd;
  1033. }
  1034. int
  1035. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  1036. {
  1037. jack_port_t *port;
  1038. unsigned long i, limit;
  1039. jack_port_shared_t *ports;
  1040. limit = client->engine->port_max;
  1041. ports = &client->engine->ports[0];
  1042. for (i = 0; i < limit; i++) {
  1043. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  1044. port = jack_port_new (client, ports[i].id, client->engine);
  1045. return jack_port_request_monitor (port, onoff);
  1046. free (port);
  1047. return 0;
  1048. }
  1049. }
  1050. return -1;
  1051. }
  1052. int
  1053. jack_port_request_monitor (jack_port_t *port, int onoff)
  1054. {
  1055. if (onoff) {
  1056. port->shared->monitor_requests++;
  1057. } else if (port->shared->monitor_requests) {
  1058. port->shared->monitor_requests--;
  1059. }
  1060. if ((port->shared->flags & JackPortIsOutput) == 0) {
  1061. GSList *node;
  1062. /* this port is for input, so recurse over each of the
  1063. connected ports.
  1064. */
  1065. pthread_mutex_lock (&port->connection_lock);
  1066. for (node = port->connections; node; node = g_slist_next (node)) {
  1067. /* drop the lock because if there is a feedback loop,
  1068. we will deadlock. XXX much worse things will
  1069. happen if there is a feedback loop !!!
  1070. */
  1071. pthread_mutex_unlock (&port->connection_lock);
  1072. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  1073. pthread_mutex_lock (&port->connection_lock);
  1074. }
  1075. pthread_mutex_unlock (&port->connection_lock);
  1076. }
  1077. return 0;
  1078. }
  1079. int
  1080. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  1081. {
  1082. if (yn) {
  1083. if (port->shared->monitor_requests == 0) {
  1084. port->shared->monitor_requests++;
  1085. }
  1086. } else {
  1087. if (port->shared->monitor_requests == 1) {
  1088. port->shared->monitor_requests--;
  1089. }
  1090. }
  1091. return 0;
  1092. }
  1093. int
  1094. jack_port_monitoring_input (jack_port_t *port)
  1095. {
  1096. return port->shared->monitor_requests > 0;
  1097. }
  1098. const char *
  1099. jack_port_name (const jack_port_t *port)
  1100. {
  1101. return port->shared->name;
  1102. }
  1103. const char *
  1104. jack_port_short_name (const jack_port_t *port)
  1105. {
  1106. /* we know there is always a colon, because we put
  1107. it there ...
  1108. */
  1109. return strchr (port->shared->name, ':') + 1;
  1110. }
  1111. int
  1112. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  1113. {
  1114. return port->shared->client_id == client->control->id;
  1115. }
  1116. int
  1117. jack_port_flags (const jack_port_t *port)
  1118. {
  1119. return port->shared->flags;
  1120. }
  1121. const char *
  1122. jack_port_type (const jack_port_t *port)
  1123. {
  1124. return port->shared->type_info.type_name;
  1125. }
  1126. int
  1127. jack_port_set_name (jack_port_t *port, const char *new_name)
  1128. {
  1129. char *colon;
  1130. int len;
  1131. colon = strchr (port->shared->name, ':');
  1132. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  1133. snprintf (colon+1, len, "%s", new_name);
  1134. return 0;
  1135. }
  1136. void
  1137. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1138. {
  1139. client->on_shutdown = function;
  1140. client->on_shutdown_arg = arg;
  1141. }
  1142. const char **
  1143. jack_get_ports (jack_client_t *client,
  1144. const char *port_name_pattern,
  1145. const char *type_name_pattern,
  1146. unsigned long flags)
  1147. {
  1148. jack_control_t *engine;
  1149. const char **matching_ports;
  1150. unsigned long match_cnt;
  1151. jack_port_shared_t *psp;
  1152. unsigned long i;
  1153. regex_t port_regex;
  1154. regex_t type_regex;
  1155. int matching;
  1156. engine = client->engine;
  1157. if (port_name_pattern && port_name_pattern[0]) {
  1158. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1159. }
  1160. if (type_name_pattern && type_name_pattern[0]) {
  1161. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1162. }
  1163. psp = engine->ports;
  1164. match_cnt = 0;
  1165. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1166. for (i = 0; i < engine->port_max; i++) {
  1167. matching = 1;
  1168. if (!psp[i].in_use) {
  1169. continue;
  1170. }
  1171. if (flags) {
  1172. if ((psp[i].flags & flags) != flags) {
  1173. matching = 0;
  1174. }
  1175. }
  1176. if (matching && port_name_pattern && port_name_pattern[0]) {
  1177. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1178. matching = 0;
  1179. }
  1180. }
  1181. if (matching && type_name_pattern && type_name_pattern[0]) {
  1182. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1183. matching = 0;
  1184. }
  1185. }
  1186. if (matching) {
  1187. matching_ports[match_cnt++] = psp[i].name;
  1188. }
  1189. }
  1190. matching_ports[match_cnt] = 0;
  1191. if (match_cnt == 0) {
  1192. free (matching_ports);
  1193. matching_ports = 0;
  1194. }
  1195. return matching_ports;
  1196. }
  1197. nframes_t
  1198. jack_frames_since_cycle_start (jack_client_t *client)
  1199. {
  1200. struct timeval now;
  1201. float usecs;
  1202. gettimeofday (&now, NULL);
  1203. usecs = ((now.tv_sec * 1000000) + now.tv_usec) - client->engine->time.microseconds;
  1204. return (nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1205. }
  1206. int
  1207. jack_port_lock (jack_client_t *client, jack_port_t *port)
  1208. {
  1209. if (port) {
  1210. port->shared->locked = 1;
  1211. return 0;
  1212. }
  1213. return -1;
  1214. }
  1215. int
  1216. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  1217. {
  1218. if (port) {
  1219. port->shared->locked = 0;
  1220. return 0;
  1221. }
  1222. return -1;
  1223. }
  1224. static void
  1225. jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes)
  1226. {
  1227. GSList *node;
  1228. jack_port_t *input;
  1229. nframes_t n;
  1230. sample_t *buffer;
  1231. sample_t *dst, *src;
  1232. /* by the time we've called this, we've already established
  1233. the existence of more than 1 connection to this input port.
  1234. */
  1235. /* no need to take connection lock, since this is called
  1236. from the process() callback, and the jack server
  1237. ensures that no changes to connections happen
  1238. during this time.
  1239. */
  1240. node = port->connections;
  1241. input = (jack_port_t *) node->data;
  1242. buffer = jack_port_buffer (port);
  1243. memcpy (buffer, jack_port_buffer (input), sizeof (sample_t) * nframes);
  1244. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1245. input = (jack_port_t *) node->data;
  1246. n = nframes;
  1247. dst = buffer;
  1248. src = jack_port_buffer (input);
  1249. while (n--) {
  1250. *dst++ += *src++;
  1251. }
  1252. }
  1253. }
  1254. const char **
  1255. jack_port_get_connections (const jack_port_t *port)
  1256. {
  1257. const char **ret;
  1258. GSList *node;
  1259. unsigned int n;
  1260. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1261. if (port->connections == NULL) {
  1262. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1263. return NULL;
  1264. }
  1265. ret = (const char **) malloc (sizeof (char *) * (g_slist_length (port->connections) + 1));
  1266. for (n = 0, node = port->connections; node; node = g_slist_next (node), n++) {
  1267. ret[n] = ((jack_port_t *) node->data)->shared->name;
  1268. }
  1269. ret[n] = NULL;
  1270. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1271. return ret;
  1272. }
  1273. int
  1274. jack_port_connected (const jack_port_t *port)
  1275. {
  1276. return port->connections != NULL;
  1277. }
  1278. int
  1279. jack_port_connected_to (const jack_port_t *port, const char *portname)
  1280. {
  1281. GSList *node;
  1282. int ret = FALSE;
  1283. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1284. for (node = port->connections; node; node = g_slist_next (node)) {
  1285. jack_port_t *other_port = (jack_port_t *) node->data;
  1286. if (strcmp (other_port->shared->name, portname) == 0) {
  1287. ret = TRUE;
  1288. break;
  1289. }
  1290. }
  1291. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1292. return ret;
  1293. }
  1294. int
  1295. jack_port_connected_to_port (const jack_port_t *port, const jack_port_t *other_port)
  1296. {
  1297. GSList *node;
  1298. int ret = FALSE;
  1299. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1300. for (node = port->connections; node; node = g_slist_next (node)) {
  1301. jack_port_t *this_port = (jack_port_t *) node->data;
  1302. if (other_port->shared == this_port->shared) {
  1303. ret = TRUE;
  1304. break;
  1305. }
  1306. }
  1307. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1308. return ret;
  1309. }
  1310. /* TRANSPORT CONTROL */
  1311. int
  1312. jack_get_transport_info (jack_client_t *client,
  1313. jack_transport_info_t *info)
  1314. {
  1315. jack_time_info_t *time_info = &client->engine->time;
  1316. if (info->valid & JackTransportState) {
  1317. info->state = time_info->transport_state;
  1318. }
  1319. if (info->valid & JackTransportPosition) {
  1320. info->position = time_info->frame;
  1321. }
  1322. if (info->valid & JackTransportLoop) {
  1323. info->loop_start = time_info->loop_start;
  1324. info->loop_end = time_info->loop_end;
  1325. }
  1326. return 0;
  1327. }
  1328. int
  1329. jack_set_transport_info (jack_client_t *client,
  1330. jack_transport_info_t *info)
  1331. {
  1332. jack_time_info_t *time_info = &client->engine->time;
  1333. if (info->valid & JackTransportState) {
  1334. time_info->transport_state = info->state;
  1335. }
  1336. if (info->valid & JackTransportPosition) {
  1337. time_info->frame = info->position;
  1338. }
  1339. if (info->valid & JackTransportLoop) {
  1340. time_info->loop_start = info->loop_start;
  1341. time_info->loop_end = info->loop_end;
  1342. }
  1343. return 0;
  1344. }
  1345. nframes_t
  1346. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  1347. {
  1348. return port->shared->total_latency;
  1349. }
  1350. int
  1351. jack_get_mhz (void)
  1352. {
  1353. FILE *f = fopen("/proc/cpuinfo", "r");
  1354. if (f == 0)
  1355. {
  1356. perror("can't open /proc/cpuinfo\n");
  1357. exit(1);
  1358. }
  1359. for ( ; ; )
  1360. {
  1361. int mhz;
  1362. int ret;
  1363. char buf[1000];
  1364. if (fgets(buf, sizeof(buf), f) == NULL)
  1365. {
  1366. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1367. exit(1);
  1368. }
  1369. ret = sscanf(buf, "cpu MHz : %d", &mhz);
  1370. if (ret == 1)
  1371. {
  1372. fclose(f);
  1373. return mhz;
  1374. }
  1375. }
  1376. }
  1377. float
  1378. jack_cpu_load (jack_client_t *client)
  1379. {
  1380. return client->engine->cpu_load;
  1381. }