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.

1848 lines
41KB

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