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.

2523 lines
57KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2005 Jussi Laako
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <limits.h>
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <regex.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. #include <sys/mman.h>
  33. #include <jack/internal.h>
  34. #include <jack/jack.h>
  35. #include <jack/engine.h>
  36. #include <jack/pool.h>
  37. #include <jack/jslist.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/unlock.h>
  41. #include <jack/thread.h>
  42. #include <jack/varargs.h>
  43. #include <jack/intsimd.h>
  44. #include <sysdeps/time.h>
  45. #include "local.h"
  46. #include <sysdeps/poll.h>
  47. #include <sysdeps/ipc.h>
  48. #include <sysdeps/cycles.h>
  49. #ifdef JACK_USE_MACH_THREADS
  50. #include <sysdeps/pThreadUtilities.h>
  51. #endif
  52. static pthread_mutex_t client_lock;
  53. static pthread_cond_t client_ready;
  54. static int
  55. jack_client_close_aux (jack_client_t *client);
  56. #define EVENT_POLL_INDEX 0
  57. #define WAIT_POLL_INDEX 1
  58. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  59. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
  60. typedef struct {
  61. int status;
  62. struct _jack_client *client;
  63. const char *client_name;
  64. } client_info;
  65. #ifdef USE_DYNSIMD
  66. #ifdef ARCH_X86
  67. int cpu_type = 0;
  68. static void
  69. init_cpu ()
  70. {
  71. cpu_type = ((have_3dnow() << 8) | have_sse());
  72. if (ARCH_X86_HAVE_3DNOW(cpu_type))
  73. jack_info("Enhanced3DNow! detected");
  74. if (ARCH_X86_HAVE_SSE2(cpu_type))
  75. jack_info("SSE2 detected");
  76. if ((!ARCH_X86_HAVE_3DNOW(cpu_type)) && (!ARCH_X86_HAVE_SSE2(cpu_type)))
  77. jack_info("No supported SIMD instruction sets detected");
  78. jack_port_set_funcs();
  79. }
  80. #else /* ARCH_X86 */
  81. static void
  82. init_cpu ()
  83. {
  84. jack_port_set_funcs();
  85. }
  86. #endif /* ARCH_X86 */
  87. #endif /* USE_DYNSIMD */
  88. char *jack_tmpdir = DEFAULT_TMP_DIR;
  89. static int
  90. jack_get_tmpdir ()
  91. {
  92. FILE* in;
  93. size_t len;
  94. char buf[PATH_MAX+2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  95. char *pathenv;
  96. char *pathcopy;
  97. char *p;
  98. /* some implementations of popen(3) close a security loophole by
  99. resetting PATH for the exec'd command. since we *want* to
  100. use the user's PATH setting to locate jackd, we have to
  101. do it ourselves.
  102. */
  103. if ((pathenv = getenv ("PATH")) == 0) {
  104. return -1;
  105. }
  106. /* don't let strtok(3) mess with the real environment variable */
  107. pathcopy = strdup (pathenv);
  108. p = strtok (pathcopy, ":");
  109. while (p) {
  110. char jackd[PATH_MAX+1];
  111. char command[PATH_MAX+4];
  112. snprintf (jackd, sizeof (jackd), "%s/jackd", p);
  113. if (access (jackd, X_OK) == 0) {
  114. snprintf (command, sizeof (command), "%s -l", jackd);
  115. if ((in = popen (command, "r")) != NULL) {
  116. break;
  117. }
  118. }
  119. p = strtok (NULL, ":");
  120. }
  121. if (p == NULL) {
  122. /* no command successfully started */
  123. free (pathcopy);
  124. return -1;
  125. }
  126. if (fgets (buf, sizeof (buf), in) == NULL) {
  127. fclose (in);
  128. free (pathcopy);
  129. return -1;
  130. }
  131. len = strlen (buf);
  132. if (buf[len-1] != '\n') {
  133. /* didn't get a whole line */
  134. fclose (in);
  135. free (pathcopy);
  136. return -1;
  137. }
  138. jack_tmpdir = (char *) malloc (len);
  139. memcpy (jack_tmpdir, buf, len-1);
  140. jack_tmpdir[len-1] = '\0';
  141. fclose (in);
  142. free (pathcopy);
  143. return 0;
  144. }
  145. void
  146. jack_error (const char *fmt, ...)
  147. {
  148. va_list ap;
  149. char buffer[300];
  150. va_start (ap, fmt);
  151. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  152. jack_error_callback (buffer);
  153. va_end (ap);
  154. }
  155. void
  156. default_jack_error_callback (const char *desc)
  157. {
  158. #ifdef DEBUG_ENABLED
  159. DEBUG("%s", desc);
  160. #else
  161. fprintf(stderr, "%s\n", desc);
  162. fflush(stderr);
  163. #endif
  164. }
  165. void
  166. default_jack_info_callback (const char *desc)
  167. {
  168. fprintf(stdout, "%s\n", desc);
  169. fflush(stdout);
  170. }
  171. void
  172. silent_jack_error_callback (const char *desc)
  173. {
  174. }
  175. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  176. void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;
  177. void
  178. jack_info (const char *fmt, ...)
  179. {
  180. va_list ap;
  181. char buffer[300];
  182. va_start (ap, fmt);
  183. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  184. jack_info_callback (buffer);
  185. va_end (ap);
  186. }
  187. static int
  188. oop_client_deliver_request (void *ptr, jack_request_t *req)
  189. {
  190. int wok, rok;
  191. jack_client_t *client = (jack_client_t*) ptr;
  192. wok = (write (client->request_fd, req, sizeof (*req))
  193. == sizeof (*req));
  194. rok = (read (client->request_fd, req, sizeof (*req))
  195. == sizeof (*req));
  196. if (wok && rok) { /* everything OK? */
  197. return req->status;
  198. }
  199. req->status = -1; /* request failed */
  200. /* check for server shutdown */
  201. if (client->engine->engine_ok == 0)
  202. return req->status;
  203. /* otherwise report errors */
  204. if (!wok)
  205. jack_error ("cannot send request type %d to server",
  206. req->type);
  207. if (!rok)
  208. jack_error ("cannot read result for request type %d from"
  209. " server (%s)", req->type, strerror (errno));
  210. return req->status;
  211. }
  212. int
  213. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  214. {
  215. /* indirect through the function pointer that was set either
  216. * by jack_client_open() or by jack_new_client_request() in
  217. * the server.
  218. */
  219. return client->deliver_request (client->deliver_arg,
  220. req);
  221. }
  222. #if JACK_USE_MACH_THREADS
  223. jack_client_t *
  224. jack_client_alloc ()
  225. {
  226. jack_client_t *client;
  227. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  228. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 1);
  229. client->pollmax = 1;
  230. client->request_fd = -1;
  231. client->event_fd = -1;
  232. client->upstream_is_jackd = 0;
  233. client->graph_wait_fd = -1;
  234. client->graph_next_fd = -1;
  235. client->ports = NULL;
  236. client->ports_ext = NULL;
  237. client->engine = NULL;
  238. client->control = NULL;
  239. client->thread_ok = FALSE;
  240. client->rt_thread_ok = FALSE;
  241. client->first_active = TRUE;
  242. client->on_shutdown = NULL;
  243. client->n_port_types = 0;
  244. client->port_segment = NULL;
  245. #ifdef USE_DYNSIMD
  246. init_cpu();
  247. #endif /* USE_DYNSIMD */
  248. return client;
  249. }
  250. #else
  251. jack_client_t *
  252. jack_client_alloc ()
  253. {
  254. jack_client_t *client;
  255. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  256. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  257. client->pollmax = 2;
  258. client->request_fd = -1;
  259. client->event_fd = -1;
  260. client->upstream_is_jackd = 0;
  261. client->graph_wait_fd = -1;
  262. client->graph_next_fd = -1;
  263. client->ports = NULL;
  264. client->ports_ext = NULL;
  265. client->engine = NULL;
  266. client->control = NULL;
  267. client->thread_ok = FALSE;
  268. client->first_active = TRUE;
  269. client->on_shutdown = NULL;
  270. client->n_port_types = 0;
  271. client->port_segment = NULL;
  272. #ifdef USE_DYNSIMD
  273. init_cpu();
  274. #endif /* USE_DYNSIMD */
  275. return client;
  276. }
  277. #endif
  278. /*
  279. * Build the jack_client_t structure for an internal client.
  280. */
  281. jack_client_t *
  282. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  283. {
  284. jack_client_t* client;
  285. client = jack_client_alloc ();
  286. client->control = cc;
  287. client->engine = engine->control;
  288. client->n_port_types = client->engine->n_port_types;
  289. client->port_segment = &engine->port_segment[0];
  290. return client;
  291. }
  292. static void
  293. jack_client_free (jack_client_t *client)
  294. {
  295. if (client->pollfd) {
  296. free (client->pollfd);
  297. }
  298. free (client);
  299. }
  300. void
  301. jack_client_invalidate_port_buffers (jack_client_t *client)
  302. {
  303. JSList *node;
  304. jack_port_t *port;
  305. /* This releases all local memory owned by input ports
  306. and sets the buffer pointer to NULL. This will cause
  307. jack_port_get_buffer() to reallocate space for the
  308. buffer on the next call (if there is one).
  309. */
  310. for (node = client->ports; node; node = jack_slist_next (node)) {
  311. port = (jack_port_t *) node->data;
  312. if (port->shared->flags & JackPortIsInput) {
  313. if (port->mix_buffer) {
  314. jack_pool_release (port->mix_buffer);
  315. port->mix_buffer = NULL;
  316. }
  317. }
  318. }
  319. }
  320. int
  321. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  322. {
  323. jack_port_t *control_port;
  324. jack_port_t *other = 0;
  325. JSList *node;
  326. int need_free = FALSE;
  327. if (client->engine->ports[event->x.self_id].client_id == client->control->id ||
  328. client->engine->ports[event->y.other_id].client_id == client->control->id) {
  329. /* its one of ours */
  330. switch (event->type) {
  331. case PortConnected:
  332. other = jack_port_new (client, event->y.other_id,
  333. client->engine);
  334. /* jack_port_by_id_int() always returns an internal
  335. * port that does not need to be deallocated
  336. */
  337. control_port = jack_port_by_id_int (client, event->x.self_id,
  338. &need_free);
  339. pthread_mutex_lock (&control_port->connection_lock);
  340. control_port->connections =
  341. jack_slist_prepend (control_port->connections,
  342. (void *) other);
  343. pthread_mutex_unlock (&control_port->connection_lock);
  344. break;
  345. case PortDisconnected:
  346. /* jack_port_by_id_int() always returns an internal
  347. * port that does not need to be deallocated
  348. */
  349. control_port = jack_port_by_id_int (client, event->x.self_id,
  350. &need_free);
  351. pthread_mutex_lock (&control_port->connection_lock);
  352. for (node = control_port->connections; node;
  353. node = jack_slist_next (node)) {
  354. other = (jack_port_t *) node->data;
  355. if (other->shared->id == event->y.other_id) {
  356. control_port->connections =
  357. jack_slist_remove_link (
  358. control_port->connections,
  359. node);
  360. jack_slist_free_1 (node);
  361. free (other);
  362. break;
  363. }
  364. }
  365. pthread_mutex_unlock (&control_port->connection_lock);
  366. break;
  367. default:
  368. /* impossible */
  369. break;
  370. }
  371. }
  372. if (client->control->port_connect_cbset) {
  373. client->port_connect (event->x.self_id, event->y.other_id,
  374. (event->type == PortConnected ? 1 : 0),
  375. client->port_connect_arg);
  376. }
  377. return 0;
  378. }
  379. #if JACK_USE_MACH_THREADS
  380. static int
  381. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  382. {
  383. client->pollmax = 1;
  384. /* If the client registered its own callback for graph order events,
  385. execute it now.
  386. */
  387. if (client->control->graph_order_cbset) {
  388. client->graph_order (client->graph_order_arg);
  389. }
  390. return 0;
  391. }
  392. #else
  393. static int
  394. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  395. {
  396. char path[PATH_MAX+1];
  397. DEBUG ("graph reorder\n");
  398. if (client->graph_wait_fd >= 0) {
  399. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  400. close (client->graph_wait_fd);
  401. client->graph_wait_fd = -1;
  402. }
  403. if (client->graph_next_fd >= 0) {
  404. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  405. close (client->graph_next_fd);
  406. client->graph_next_fd = -1;
  407. }
  408. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  409. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  410. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  411. path, strerror (errno));
  412. return -1;
  413. }
  414. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  415. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  416. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  417. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  418. path, strerror (errno));
  419. return -1;
  420. }
  421. client->upstream_is_jackd = event->y.n;
  422. client->pollmax = 2;
  423. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  424. client->graph_next_fd, path,
  425. client->upstream_is_jackd);
  426. /* If the client registered its own callback for graph order events,
  427. execute it now.
  428. */
  429. if (client->control->graph_order_cbset) {
  430. client->graph_order (client->graph_order_arg);
  431. }
  432. return 0;
  433. }
  434. #endif
  435. static int
  436. server_connect (const char *server_name)
  437. {
  438. int fd;
  439. struct sockaddr_un addr;
  440. int which = 0;
  441. char server_dir[PATH_MAX+1] = "";
  442. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  443. jack_error ("cannot create client socket (%s)",
  444. strerror (errno));
  445. return -1;
  446. }
  447. //JOQ: temporary debug message
  448. //jack_info ("DEBUG: connecting to `%s' server", server_name);
  449. addr.sun_family = AF_UNIX;
  450. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  451. jack_server_dir (server_name, server_dir) , which);
  452. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  453. close (fd);
  454. return -1;
  455. }
  456. return fd;
  457. }
  458. static int
  459. server_event_connect (jack_client_t *client, const char *server_name)
  460. {
  461. int fd;
  462. struct sockaddr_un addr;
  463. jack_client_connect_ack_request_t req;
  464. jack_client_connect_ack_result_t res;
  465. char server_dir[PATH_MAX+1] = "";
  466. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  467. jack_error ("cannot create client event socket (%s)",
  468. strerror (errno));
  469. return -1;
  470. }
  471. addr.sun_family = AF_UNIX;
  472. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  473. jack_server_dir (server_name,server_dir));
  474. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  475. jack_error ("cannot connect to jack server for events",
  476. strerror (errno));
  477. close (fd);
  478. return -1;
  479. }
  480. req.client_id = client->control->id;
  481. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  482. jack_error ("cannot write event connect request to server (%s)",
  483. strerror (errno));
  484. close (fd);
  485. return -1;
  486. }
  487. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  488. jack_error ("cannot read event connect result from server (%s)",
  489. strerror (errno));
  490. close (fd);
  491. return -1;
  492. }
  493. if (res.status != 0) {
  494. jack_error ("cannot connect to server for event stream (%s)",
  495. strerror (errno));
  496. close (fd);
  497. return -1;
  498. }
  499. return fd;
  500. }
  501. /* Exec the JACK server in this process. Does not return. */
  502. static void
  503. _start_server (const char *server_name)
  504. {
  505. FILE* fp = 0;
  506. char filename[255];
  507. char arguments[255];
  508. char buffer[255];
  509. char* command = 0;
  510. size_t pos = 0;
  511. size_t result = 0;
  512. char** argv = 0;
  513. int i = 0;
  514. int good = 0;
  515. int ret;
  516. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  517. fp = fopen(filename, "r");
  518. if (!fp) {
  519. fp = fopen("/etc/jackdrc", "r");
  520. }
  521. /* if still not found, check old config name for backwards compatability */
  522. if (!fp) {
  523. fp = fopen("/etc/jackd.conf", "r");
  524. }
  525. if (fp) {
  526. arguments[0] = '\0';
  527. ret = fscanf(fp, "%s", buffer);
  528. while(ret != 0 && ret != EOF) {
  529. strcat(arguments, buffer);
  530. strcat(arguments, " ");
  531. ret = fscanf(fp, "%s", buffer);
  532. }
  533. if (strlen(arguments) > 0) {
  534. good = 1;
  535. }
  536. }
  537. if (!good) {
  538. #if defined(USE_CAPABILITIES)
  539. command = JACK_LOCATION "/jackstart";
  540. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  541. JACK_DEFAULT_DRIVER " -p 512", 255);
  542. #else /* !USE_CAPABILITIES */
  543. command = JACK_LOCATION "/jackd";
  544. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  545. JACK_DEFAULT_DRIVER, 255);
  546. #endif /* USE_CAPABILITIES */
  547. } else {
  548. result = strcspn(arguments, " ");
  549. command = (char *) malloc(result+1);
  550. strncpy(command, arguments, result);
  551. command[result] = '\0';
  552. }
  553. argv = (char **) malloc (255);
  554. while(1) {
  555. /* insert -T and -nserver_name in front of arguments */
  556. if (i == 1) {
  557. argv[i] = (char *) malloc(strlen ("-T") + 1);
  558. strcpy (argv[i++], "-T");
  559. if (server_name) {
  560. size_t optlen = strlen ("-n");
  561. char *buf =
  562. malloc (optlen
  563. + strlen (server_name) + 1);
  564. strcpy (buf, "-n");
  565. strcpy (buf+optlen, server_name);
  566. argv[i++] = buf;
  567. }
  568. }
  569. result = strcspn(arguments+pos, " ");
  570. if (result == 0) {
  571. break;
  572. }
  573. argv[i] = (char*)malloc(result+1);
  574. strncpy(argv[i], arguments+pos, result);
  575. argv[i][result] = '\0';
  576. pos += result+1;
  577. ++i;
  578. }
  579. argv[i] = 0;
  580. #if 0
  581. fprintf (stderr, "execing JACK using %s\n", command);
  582. for (_xx = 0; argv[_xx]; ++_xx) {
  583. fprintf (stderr, "\targv[%d] = %s\n", _xx, argv[_xx]);
  584. }
  585. #endif
  586. execv (command, argv);
  587. /* If execv() succeeds, it does not return. There's no point
  588. * in calling jack_error() here in the child process. */
  589. fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
  590. }
  591. int
  592. start_server (const char *server_name, jack_options_t options)
  593. {
  594. if ((options & JackNoStartServer)
  595. || getenv("JACK_NO_START_SERVER")) {
  596. return 1;
  597. }
  598. /* The double fork() forces the server to become a child of
  599. * init, which will always clean up zombie process state on
  600. * termination. This even works in cases where the server
  601. * terminates but this client does not.
  602. *
  603. * Since fork() is usually implemented using copy-on-write
  604. * virtual memory tricks, the overhead of the second fork() is
  605. * probably relatively small.
  606. */
  607. switch (fork()) {
  608. case 0: /* child process */
  609. switch (fork()) {
  610. case 0: /* grandchild process */
  611. _start_server(server_name);
  612. _exit (99); /* exec failed */
  613. case -1:
  614. _exit (98);
  615. default:
  616. _exit (0);
  617. }
  618. case -1: /* fork() error */
  619. return 1; /* failed to start server */
  620. }
  621. /* only the original parent process goes here */
  622. return 0; /* (probably) successful */
  623. }
  624. static int
  625. jack_request_client (ClientType type,
  626. const char* client_name, jack_options_t options,
  627. jack_status_t *status, jack_varargs_t *va,
  628. jack_client_connect_result_t *res, int *req_fd)
  629. {
  630. jack_client_connect_request_t req;
  631. *req_fd = -1;
  632. memset (&req, 0, sizeof (req));
  633. req.options = options;
  634. if (strlen (client_name) >= sizeof (req.name)) {
  635. jack_error ("\"%s\" is too long to be used as a JACK client"
  636. " name.\n"
  637. "Please use %lu characters or less.",
  638. client_name, sizeof (req.name));
  639. return -1;
  640. }
  641. if (va->load_name
  642. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  643. jack_error ("\"%s\" is too long to be used as a JACK shared"
  644. " object name.\n"
  645. "Please use %lu characters or less.",
  646. va->load_name, sizeof (req.object_path) - 1);
  647. return -1;
  648. }
  649. if (va->load_init
  650. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  651. jack_error ("\"%s\" is too long to be used as a JACK shared"
  652. " object data string.\n"
  653. "Please use %lu characters or less.",
  654. va->load_init, sizeof (req.object_data) - 1);
  655. return -1;
  656. }
  657. if ((*req_fd = server_connect (va->server_name)) < 0) {
  658. int trys;
  659. if (start_server(va->server_name, options)) {
  660. *status |= (JackFailure|JackServerFailed);
  661. goto fail;
  662. }
  663. trys = 5;
  664. do {
  665. sleep(1);
  666. if (--trys < 0) {
  667. *status |= (JackFailure|JackServerFailed);
  668. goto fail;
  669. }
  670. } while ((*req_fd = server_connect (va->server_name)) < 0);
  671. *status |= JackServerStarted;
  672. }
  673. /* format connection request */
  674. req.protocol_v = jack_protocol_version;
  675. req.load = TRUE;
  676. req.type = type;
  677. snprintf (req.name, sizeof (req.name),
  678. "%s", client_name);
  679. snprintf (req.object_path, sizeof (req.object_path),
  680. "%s", va->load_name);
  681. snprintf (req.object_data, sizeof (req.object_data),
  682. "%s", va->load_init);
  683. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  684. jack_error ("cannot send request to jack server (%s)",
  685. strerror (errno));
  686. *status |= (JackFailure|JackServerError);
  687. goto fail;
  688. }
  689. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  690. if (errno == 0) {
  691. /* server shut the socket */
  692. jack_error ("could not attach as client");
  693. *status |= (JackFailure|JackServerError);
  694. goto fail;
  695. }
  696. if (errno == ECONNRESET) {
  697. jack_error ("could not attach as JACK client "
  698. "(server has exited)");
  699. *status |= (JackFailure|JackServerError);
  700. goto fail;
  701. }
  702. jack_error ("cannot read response from jack server (%s)",
  703. strerror (errno));
  704. *status |= (JackFailure|JackServerError);
  705. goto fail;
  706. }
  707. *status |= res->status; /* return server status bits */
  708. if (*status & JackFailure) {
  709. if (*status & JackVersionError) {
  710. jack_error ("client linked with incompatible libjack"
  711. " version.");
  712. }
  713. jack_error ("could not attach to JACK server");
  714. *status |= JackServerError;
  715. goto fail;
  716. }
  717. switch (type) {
  718. case ClientDriver:
  719. case ClientInternal:
  720. close (*req_fd);
  721. *req_fd = -1;
  722. break;
  723. default:
  724. break;
  725. }
  726. return 0;
  727. fail:
  728. if (*req_fd >= 0) {
  729. close (*req_fd);
  730. *req_fd = -1;
  731. }
  732. return -1;
  733. }
  734. int
  735. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  736. {
  737. /* Lookup, attach and register the port/buffer segments in use
  738. * right now.
  739. */
  740. if (client->control->type != ClientExternal) {
  741. jack_error("Only external clients need attach port segments");
  742. abort();
  743. }
  744. /* make sure we have space to store the port
  745. segment information.
  746. */
  747. if (ptid >= client->n_port_types) {
  748. client->port_segment = (jack_shm_info_t*)
  749. realloc (client->port_segment,
  750. sizeof (jack_shm_info_t) * (ptid+1));
  751. memset (&client->port_segment[client->n_port_types],
  752. 0,
  753. sizeof (jack_shm_info_t) *
  754. (ptid - client->n_port_types));
  755. client->n_port_types = ptid + 1;
  756. } else {
  757. /* release any previous segment */
  758. jack_release_shm (&client->port_segment[ptid]);
  759. }
  760. /* get the index into the shm registry */
  761. client->port_segment[ptid].index =
  762. client->engine->port_types[ptid].shm_registry_index;
  763. /* attach the relevant segment */
  764. if (jack_attach_shm (&client->port_segment[ptid])) {
  765. jack_error ("cannot attach port segment shared memory"
  766. " (%s)", strerror (errno));
  767. return -1;
  768. }
  769. return 0;
  770. }
  771. jack_client_t *
  772. jack_client_open_aux (const char *client_name,
  773. jack_options_t options,
  774. jack_status_t *status, va_list ap)
  775. {
  776. /* optional arguments: */
  777. jack_varargs_t va; /* variable arguments */
  778. int req_fd = -1;
  779. int ev_fd = -1;
  780. jack_client_connect_result_t res;
  781. jack_client_t *client;
  782. jack_port_type_id_t ptid;
  783. jack_status_t my_status;
  784. jack_messagebuffer_init ();
  785. if (status == NULL) /* no status from caller? */
  786. status = &my_status; /* use local status word */
  787. *status = 0;
  788. /* validate parameters */
  789. if ((options & ~JackOpenOptions)) {
  790. *status |= (JackFailure|JackInvalidOption);
  791. return NULL;
  792. }
  793. /* parse variable arguments */
  794. jack_varargs_parse(options, ap, &va);
  795. /* External clients need to know where the tmpdir used for
  796. communication with the server lives
  797. */
  798. if (jack_get_tmpdir ()) {
  799. *status |= JackFailure;
  800. return NULL;
  801. }
  802. /* External clients need this initialized. It is already set
  803. * up in the server's address space for internal clients.
  804. */
  805. jack_init_time ();
  806. if (jack_request_client (ClientExternal, client_name, options, status,
  807. &va, &res, &req_fd)) {
  808. return NULL;
  809. }
  810. /* Allocate the jack_client_t structure in local memory.
  811. * Shared memory is not accessible yet. */
  812. client = jack_client_alloc ();
  813. strcpy (client->name, res.name);
  814. strcpy (client->fifo_prefix, res.fifo_prefix);
  815. client->request_fd = req_fd;
  816. client->pollfd[EVENT_POLL_INDEX].events =
  817. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  818. #ifndef JACK_USE_MACH_THREADS
  819. client->pollfd[WAIT_POLL_INDEX].events =
  820. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  821. #endif
  822. /* Don't access shared memory until server connected. */
  823. if (jack_initialize_shm (va.server_name)) {
  824. jack_error ("Unable to initialize shared memory.");
  825. *status |= (JackFailure|JackShmFailure);
  826. goto fail;
  827. }
  828. /* attach the engine control/info block */
  829. client->engine_shm.index = res.engine_shm_index;
  830. if (jack_attach_shm (&client->engine_shm)) {
  831. jack_error ("cannot attached engine control shared memory"
  832. " segment");
  833. goto fail;
  834. }
  835. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  836. /* initialize clock source as early as possible */
  837. jack_set_clock_source (client->engine->clock_source);
  838. /* now attach the client control block */
  839. client->control_shm.index = res.client_shm_index;
  840. if (jack_attach_shm (&client->control_shm)) {
  841. jack_error ("cannot attached client control shared memory"
  842. " segment");
  843. goto fail;
  844. }
  845. client->control = (jack_client_control_t *)
  846. jack_shm_addr (&client->control_shm);
  847. /* Nobody else needs to access this shared memory any more, so
  848. * destroy it. Because we have it attached, it won't vanish
  849. * till we exit (and release it).
  850. */
  851. jack_destroy_shm (&client->control_shm);
  852. client->n_port_types = client->engine->n_port_types;
  853. client->port_segment = (jack_shm_info_t *)
  854. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  855. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  856. client->port_segment[ptid].index =
  857. client->engine->port_types[ptid].shm_registry_index;
  858. client->port_segment[ptid].attached_at = MAP_FAILED;
  859. jack_attach_port_segment (client, ptid);
  860. }
  861. /* set up the client so that it does the right thing for an
  862. * external client
  863. */
  864. client->deliver_request = oop_client_deliver_request;
  865. client->deliver_arg = client;
  866. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  867. goto fail;
  868. }
  869. client->event_fd = ev_fd;
  870. #ifdef JACK_USE_MACH_THREADS
  871. /* specific resources for server/client real-time thread
  872. * communication */
  873. client->clienttask = mach_task_self();
  874. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  875. jack_error ("Can't find bootstrap port");
  876. goto fail;
  877. }
  878. if (allocate_mach_clientport(client, res.portnum) < 0) {
  879. jack_error("Can't allocate mach port");
  880. goto fail;
  881. };
  882. #endif /* JACK_USE_MACH_THREADS */
  883. return client;
  884. fail:
  885. if (client->engine) {
  886. jack_release_shm (&client->engine_shm);
  887. client->engine = 0;
  888. }
  889. if (client->control) {
  890. jack_release_shm (&client->control_shm);
  891. client->control = 0;
  892. }
  893. if (req_fd >= 0) {
  894. close (req_fd);
  895. }
  896. if (ev_fd >= 0) {
  897. close (ev_fd);
  898. }
  899. free (client);
  900. return NULL;
  901. }
  902. jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  903. {
  904. va_list ap;
  905. va_start(ap, status);
  906. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  907. va_end(ap);
  908. return res;
  909. }
  910. jack_client_t *
  911. jack_client_new (const char *client_name)
  912. {
  913. jack_options_t options = JackUseExactName;
  914. if (getenv("JACK_START_SERVER") == NULL)
  915. options |= JackNoStartServer;
  916. return jack_client_open (client_name, options, NULL);
  917. }
  918. char *
  919. jack_get_client_name (jack_client_t *client)
  920. {
  921. return client->name;
  922. }
  923. int
  924. jack_internal_client_new (const char *client_name,
  925. const char *so_name, const char *so_data)
  926. {
  927. jack_client_connect_result_t res;
  928. int req_fd;
  929. jack_varargs_t va;
  930. jack_status_t status;
  931. jack_options_t options = JackUseExactName;
  932. if (getenv("JACK_START_SERVER") == NULL)
  933. options |= JackNoStartServer;
  934. jack_varargs_init (&va);
  935. va.load_name = (char *) so_name;
  936. va.load_init = (char *) so_data;
  937. return jack_request_client (ClientInternal, client_name,
  938. options, &status, &va, &res, &req_fd);
  939. }
  940. char *
  941. jack_default_server_name (void)
  942. {
  943. char *server_name;
  944. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  945. server_name = "default";
  946. return server_name;
  947. }
  948. /* returns the name of the per-user subdirectory of jack_tmpdir */
  949. char *
  950. jack_user_dir (void)
  951. {
  952. static char user_dir[PATH_MAX+1] = "";
  953. /* format the path name on the first call */
  954. if (user_dir[0] == '\0') {
  955. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  956. snprintf (user_dir, sizeof (user_dir), "%s/jack",
  957. jack_tmpdir);
  958. } else {
  959. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  960. jack_tmpdir, getuid ());
  961. }
  962. }
  963. return user_dir;
  964. }
  965. /* returns the name of the per-server subdirectory of jack_user_dir() */
  966. char *
  967. jack_server_dir (const char *server_name, char *server_dir)
  968. {
  969. /* format the path name into the suppled server_dir char array,
  970. * assuming that server_dir is at least as large as PATH_MAX+1 */
  971. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  972. jack_user_dir (), server_name);
  973. return server_dir;
  974. }
  975. void
  976. jack_internal_client_close (const char *client_name)
  977. {
  978. jack_client_connect_request_t req;
  979. int fd;
  980. char *server_name = jack_default_server_name ();
  981. req.load = FALSE;
  982. snprintf (req.name, sizeof (req.name), "%s", client_name);
  983. if ((fd = server_connect (server_name)) < 0) {
  984. return;
  985. }
  986. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  987. jack_error ("cannot deliver ClientUnload request to JACK "
  988. "server.");
  989. }
  990. /* no response to this request */
  991. close (fd);
  992. return;
  993. }
  994. int
  995. jack_recompute_total_latencies (jack_client_t* client)
  996. {
  997. jack_request_t request;
  998. request.type = RecomputeTotalLatencies;
  999. return jack_client_deliver_request (client, &request);
  1000. }
  1001. int
  1002. jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
  1003. {
  1004. jack_request_t request;
  1005. request.type = RecomputeTotalLatency;
  1006. request.x.port_info.port_id = port->shared->id;
  1007. return jack_client_deliver_request (client, &request);
  1008. }
  1009. int
  1010. jack_set_freewheel (jack_client_t* client, int onoff)
  1011. {
  1012. jack_request_t request;
  1013. request.type = onoff ? FreeWheel : StopFreeWheel;
  1014. return jack_client_deliver_request (client, &request);
  1015. }
  1016. void
  1017. jack_start_freewheel (jack_client_t* client)
  1018. {
  1019. jack_client_control_t *control = client->control;
  1020. if (client->engine->real_time) {
  1021. #if JACK_USE_MACH_THREADS
  1022. jack_drop_real_time_scheduling (client->process_thread);
  1023. #else
  1024. jack_drop_real_time_scheduling (client->thread);
  1025. #endif
  1026. }
  1027. if (control->freewheel_cb_cbset) {
  1028. client->freewheel_cb (1, client->freewheel_arg);
  1029. }
  1030. }
  1031. void
  1032. jack_stop_freewheel (jack_client_t* client)
  1033. {
  1034. jack_client_control_t *control = client->control;
  1035. if (client->engine->real_time) {
  1036. #if JACK_USE_MACH_THREADS
  1037. jack_acquire_real_time_scheduling (client->process_thread,
  1038. client->engine->client_priority);
  1039. #else
  1040. jack_acquire_real_time_scheduling (client->thread,
  1041. client->engine->client_priority);
  1042. #endif
  1043. }
  1044. if (control->freewheel_cb_cbset) {
  1045. client->freewheel_cb (0, client->freewheel_arg);
  1046. }
  1047. }
  1048. static void
  1049. jack_client_thread_suicide (jack_client_t* client)
  1050. {
  1051. if (client->on_shutdown) {
  1052. jack_error ("zombified - calling shutdown handler");
  1053. client->on_shutdown (client->on_shutdown_arg);
  1054. } else {
  1055. jack_error ("jack_client_thread zombified - exiting from JACK");
  1056. jack_client_close_aux (client);
  1057. /* Need a fix : possibly make client crash if
  1058. * zombified without shutdown handler
  1059. */
  1060. }
  1061. pthread_exit (0);
  1062. /*NOTREACHED*/
  1063. }
  1064. static int
  1065. jack_client_process_events (jack_client_t* client)
  1066. {
  1067. jack_event_t event;
  1068. char status = 0;
  1069. jack_client_control_t *control = client->control;
  1070. JSList *node;
  1071. jack_port_t* port;
  1072. DEBUG ("process events");
  1073. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1074. DEBUG ("client receives an event, "
  1075. "now reading on event fd");
  1076. /* server has sent us an event. process the
  1077. * event and reply */
  1078. if (read (client->event_fd, &event, sizeof (event))
  1079. != sizeof (event)) {
  1080. jack_error ("cannot read server event (%s)",
  1081. strerror (errno));
  1082. return -1;
  1083. }
  1084. status = 0;
  1085. switch (event.type) {
  1086. case PortRegistered:
  1087. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1088. port = node->data;
  1089. if (port->shared->id == event.x.port_id) { // Found port, update port type
  1090. port->type_info = &client->engine->port_types[port->shared->ptype_id];
  1091. }
  1092. }
  1093. if (control->port_register_cbset) {
  1094. client->port_register
  1095. (event.x.port_id, TRUE,
  1096. client->port_register_arg);
  1097. }
  1098. break;
  1099. case PortUnregistered:
  1100. if (control->port_register_cbset) {
  1101. client->port_register
  1102. (event.x.port_id, FALSE,
  1103. client->port_register_arg);
  1104. }
  1105. break;
  1106. case ClientRegistered:
  1107. if (control->client_register_cbset) {
  1108. client->client_register
  1109. (event.x.name, TRUE,
  1110. client->client_register_arg);
  1111. }
  1112. break;
  1113. case ClientUnregistered:
  1114. if (control->client_register_cbset) {
  1115. client->client_register
  1116. (event.x.name, FALSE,
  1117. client->client_register_arg);
  1118. }
  1119. break;
  1120. case GraphReordered:
  1121. status = jack_handle_reorder (client, &event);
  1122. break;
  1123. case PortConnected:
  1124. case PortDisconnected:
  1125. status = jack_client_handle_port_connection
  1126. (client, &event);
  1127. break;
  1128. case BufferSizeChange:
  1129. jack_client_invalidate_port_buffers (client);
  1130. if (control->bufsize_cbset) {
  1131. status = client->bufsize
  1132. (control->nframes,
  1133. client->bufsize_arg);
  1134. }
  1135. break;
  1136. case SampleRateChange:
  1137. if (control->srate_cbset) {
  1138. status = client->srate
  1139. (control->nframes,
  1140. client->srate_arg);
  1141. }
  1142. break;
  1143. case XRun:
  1144. if (control->xrun_cbset) {
  1145. status = client->xrun
  1146. (client->xrun_arg);
  1147. }
  1148. break;
  1149. case AttachPortSegment:
  1150. jack_attach_port_segment (client, event.y.ptid);
  1151. break;
  1152. case StartFreewheel:
  1153. jack_start_freewheel (client);
  1154. break;
  1155. case StopFreewheel:
  1156. jack_stop_freewheel (client);
  1157. break;
  1158. }
  1159. DEBUG ("client has dealt with the event, writing "
  1160. "response on event fd");
  1161. if (write (client->event_fd, &status, sizeof (status))
  1162. != sizeof (status)) {
  1163. jack_error ("cannot send event response to "
  1164. "engine (%s)", strerror (errno));
  1165. return -1;
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. static int
  1171. jack_client_core_wait (jack_client_t* client)
  1172. {
  1173. jack_client_control_t *control = client->control;
  1174. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1175. "event_fd and graph_wait_fd..." :
  1176. "event_fd only");
  1177. while (1) {
  1178. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1179. if (errno == EINTR) {
  1180. continue;
  1181. }
  1182. jack_error ("poll failed in client (%s)",
  1183. strerror (errno));
  1184. return -1;
  1185. }
  1186. pthread_testcancel();
  1187. #ifndef JACK_USE_MACH_THREADS
  1188. /* get an accurate timestamp on waking from poll for a
  1189. * process() cycle.
  1190. */
  1191. if (client->graph_wait_fd >= 0
  1192. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1193. control->awake_at = jack_get_microseconds();
  1194. }
  1195. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1196. client->pollfd[EVENT_POLL_INDEX].revents,
  1197. client->pollfd[WAIT_POLL_INDEX].revents);
  1198. if (client->graph_wait_fd >= 0 &&
  1199. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1200. /* our upstream "wait" connection
  1201. closed, which either means that
  1202. an intermediate client exited, or
  1203. jackd exited, or jackd zombified
  1204. us.
  1205. we can discover the zombification
  1206. via client->control->dead, but
  1207. the other two possibilities are
  1208. impossible to identify just from
  1209. this situation. so we have to
  1210. check what we are connected to,
  1211. and act accordingly.
  1212. */
  1213. if (client->upstream_is_jackd) {
  1214. DEBUG ("WE DIE\n");
  1215. return 0;
  1216. } else {
  1217. DEBUG ("WE PUNT\n");
  1218. /* don't poll on the wait fd
  1219. * again until we get a
  1220. * GraphReordered event.
  1221. */
  1222. client->graph_wait_fd = -1;
  1223. client->pollmax = 1;
  1224. }
  1225. }
  1226. #endif
  1227. if (jack_client_process_events (client)) {
  1228. DEBUG ("event processing failed\n");
  1229. return 0;
  1230. }
  1231. if (client->graph_wait_fd >= 0 &&
  1232. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1233. DEBUG ("time to run process()\n");
  1234. break;
  1235. }
  1236. }
  1237. if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1238. DEBUG ("client appears dead or event pollfd has error status\n");
  1239. return -1;
  1240. }
  1241. return 0;
  1242. }
  1243. static int
  1244. jack_wake_next_client (jack_client_t* client)
  1245. {
  1246. struct pollfd pfds[1];
  1247. int pret = 0;
  1248. char c = 0;
  1249. if (write (client->graph_next_fd, &c, sizeof (c))
  1250. != sizeof (c)) {
  1251. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1252. jack_error ("cannot continue execution of the "
  1253. "processing graph (%s)",
  1254. strerror(errno));
  1255. return -1;
  1256. }
  1257. DEBUG ("client sent message to next stage by %" PRIu64 "",
  1258. jack_get_microseconds());
  1259. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1260. /* "upstream client went away? readability is checked in
  1261. * jack_client_core_wait(), but that's almost a whole cycle
  1262. * before we get here.
  1263. */
  1264. if (client->graph_wait_fd >= 0) {
  1265. pfds[0].fd = client->graph_wait_fd;
  1266. pfds[0].events = POLLIN;
  1267. /* 0 timeout, don't actually wait */
  1268. pret = poll(pfds, 1, 0);
  1269. }
  1270. if (pret > 0 && (pfds[0].revents & POLLIN)) {
  1271. if (read (client->graph_wait_fd, &c, sizeof (c))
  1272. != sizeof (c)) {
  1273. jack_error ("cannot complete execution of the "
  1274. "processing graph (%s)", strerror(errno));
  1275. return -1;
  1276. }
  1277. } else {
  1278. DEBUG("cleanup byte from pipe %d not available?\n",
  1279. client->graph_wait_fd);
  1280. }
  1281. return 0;
  1282. }
  1283. static jack_nframes_t
  1284. jack_thread_first_wait (jack_client_t* client)
  1285. {
  1286. if (jack_client_core_wait (client)) {
  1287. return 0;
  1288. }
  1289. return client->control->nframes;
  1290. }
  1291. jack_nframes_t
  1292. jack_thread_wait (jack_client_t* client, int status)
  1293. {
  1294. client->control->last_status = status;
  1295. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1296. /* housekeeping/cleanup after data processing */
  1297. if (status == 0 && client->control->timebase_cb_cbset) {
  1298. jack_call_timebase_master (client);
  1299. }
  1300. /* end preemption checking */
  1301. CHECK_PREEMPTION (client->engine, FALSE);
  1302. client->control->finished_at = jack_get_microseconds();
  1303. /* wake the next client in the chain (could be the server),
  1304. and check if we were killed during the process
  1305. cycle.
  1306. */
  1307. if (jack_wake_next_client (client)) {
  1308. DEBUG("client cannot wake next, or is dead\n");
  1309. return 0;
  1310. }
  1311. if (status || client->control->dead || !client->engine->engine_ok) {
  1312. return 0;
  1313. }
  1314. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1315. if (jack_client_core_wait (client)) {
  1316. return 0;
  1317. }
  1318. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1319. /* Time to do data processing */
  1320. client->control->state = Running;
  1321. /* begin preemption checking */
  1322. CHECK_PREEMPTION (client->engine, TRUE);
  1323. if (client->control->sync_cb_cbset)
  1324. jack_call_sync_client (client);
  1325. return client->control->nframes;
  1326. }
  1327. jack_nframes_t jack_cycle_wait (jack_client_t* client)
  1328. {
  1329. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1330. if (jack_client_core_wait (client)) {
  1331. return 0;
  1332. }
  1333. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1334. /* Time to do data processing */
  1335. client->control->state = Running;
  1336. /* begin preemption checking */
  1337. CHECK_PREEMPTION (client->engine, TRUE);
  1338. if (client->control->sync_cb_cbset)
  1339. jack_call_sync_client (client);
  1340. return client->control->nframes;
  1341. }
  1342. void jack_cycle_signal(jack_client_t* client, int status)
  1343. {
  1344. client->control->last_status = status;
  1345. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1346. /* housekeeping/cleanup after data processing */
  1347. if (status == 0 && client->control->timebase_cb_cbset) {
  1348. jack_call_timebase_master (client);
  1349. }
  1350. /* end preemption checking */
  1351. CHECK_PREEMPTION (client->engine, FALSE);
  1352. client->control->finished_at = jack_get_microseconds();
  1353. /* wake the next client in the chain (could be the server),
  1354. and check if we were killed during the process
  1355. cycle.
  1356. */
  1357. if (jack_wake_next_client (client)) {
  1358. DEBUG("client cannot wake next, or is dead\n");
  1359. jack_client_thread_suicide (client);
  1360. /*NOTREACHED*/
  1361. }
  1362. if (status || client->control->dead || !client->engine->engine_ok) {
  1363. jack_client_thread_suicide (client);
  1364. /*NOTREACHED*/
  1365. }
  1366. }
  1367. static void
  1368. jack_client_thread_aux (void *arg)
  1369. {
  1370. jack_client_t *client = (jack_client_t *) arg;
  1371. jack_client_control_t *control = client->control;
  1372. pthread_mutex_lock (&client_lock);
  1373. client->thread_ok = TRUE;
  1374. client->thread_id = pthread_self();
  1375. pthread_cond_signal (&client_ready);
  1376. pthread_mutex_unlock (&client_lock);
  1377. control->pid = getpid();
  1378. control->pgrp = getpgrp();
  1379. DEBUG ("client thread is now running");
  1380. if (control->thread_init_cbset) {
  1381. DEBUG ("calling client thread init callback");
  1382. client->thread_init (client->thread_init_arg);
  1383. }
  1384. /* wait for first wakeup from server */
  1385. if (jack_thread_first_wait (client) == control->nframes) {
  1386. /* now run till we're done */
  1387. if (control->process_cbset) {
  1388. /* run process callback, then wait... ad-infinitum */
  1389. while (1) {
  1390. DEBUG("client calls process()");
  1391. int status = (client->process (control->nframes,
  1392. client->process_arg) ==
  1393. control->nframes);
  1394. control->state = Finished;
  1395. DEBUG("client leaves process(), re-enters wait");
  1396. if (!jack_thread_wait (client, status)) {
  1397. break;
  1398. }
  1399. DEBUG("client done with wait");
  1400. }
  1401. } else {
  1402. /* no process handling but still need to process events */
  1403. while (jack_thread_wait (client, 0) == control->nframes)
  1404. ;
  1405. }
  1406. }
  1407. jack_client_thread_suicide (client);
  1408. }
  1409. static void *
  1410. jack_client_thread (void *arg)
  1411. {
  1412. jack_client_t *client = (jack_client_t *) arg;
  1413. jack_client_control_t *control = client->control;
  1414. if (client->control->thread_cb_cbset) {
  1415. pthread_mutex_lock (&client_lock);
  1416. client->thread_ok = TRUE;
  1417. client->thread_id = pthread_self();
  1418. pthread_cond_signal (&client_ready);
  1419. pthread_mutex_unlock (&client_lock);
  1420. control->pid = getpid();
  1421. control->pgrp = getpgrp();
  1422. client->thread_cb(client->thread_cb_arg);
  1423. jack_client_thread_suicide(client);
  1424. } else {
  1425. jack_client_thread_aux(arg);
  1426. }
  1427. /*NOTREACHED*/
  1428. return (void *) 0;
  1429. }
  1430. #ifdef JACK_USE_MACH_THREADS
  1431. /* real-time thread : separated from the normal client thread, it will
  1432. * communicate with the server using fast mach RPC mechanism */
  1433. static void *
  1434. jack_client_process_thread (void *arg)
  1435. {
  1436. jack_client_t *client = (jack_client_t *) arg;
  1437. jack_client_control_t *control = client->control;
  1438. int err = 0;
  1439. if (client->control->thread_init_cbset) {
  1440. /* this means that the init callback will be called twice -taybin*/
  1441. DEBUG ("calling client thread init callback");
  1442. client->thread_init (client->thread_init_arg);
  1443. }
  1444. client->control->pid = getpid();
  1445. DEBUG ("client process thread is now running");
  1446. client->rt_thread_ok = TRUE;
  1447. while (err == 0) {
  1448. if (jack_client_suspend(client) < 0) {
  1449. jack_error ("jack_client_process_thread :resume error");
  1450. goto zombie;
  1451. }
  1452. control->awake_at = jack_get_microseconds();
  1453. DEBUG ("client resumed");
  1454. control->state = Running;
  1455. if (control->sync_cb_cbset)
  1456. jack_call_sync_client (client);
  1457. if (control->process_cbset) {
  1458. if (client->process (control->nframes,
  1459. client->process_arg) == 0) {
  1460. control->state = Finished;
  1461. }
  1462. } else {
  1463. control->state = Finished;
  1464. }
  1465. if (control->timebase_cb_cbset)
  1466. jack_call_timebase_master (client);
  1467. control->finished_at = jack_get_microseconds();
  1468. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1469. control->finished_at,
  1470. ((float)(control->finished_at - control->awake_at)));
  1471. /* check if we were killed during the process cycle
  1472. * (or whatever) */
  1473. if (client->control->dead) {
  1474. jack_error ("jack_client_process_thread: "
  1475. "client->control->dead");
  1476. goto zombie;
  1477. }
  1478. DEBUG("process cycle fully complete\n");
  1479. }
  1480. return (void *) ((intptr_t)err);
  1481. zombie:
  1482. jack_error ("jack_client_process_thread : zombified");
  1483. client->rt_thread_ok = FALSE;
  1484. if (client->on_shutdown) {
  1485. jack_error ("zombified - calling shutdown handler");
  1486. client->on_shutdown (client->on_shutdown_arg);
  1487. } else {
  1488. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1489. /* Need a fix : possibly make client crash if
  1490. * zombified without shutdown handler */
  1491. jack_client_close_aux (client);
  1492. }
  1493. pthread_exit (0);
  1494. /*NOTREACHED*/
  1495. return 0;
  1496. }
  1497. #endif /* JACK_USE_MACH_THREADS */
  1498. static int
  1499. jack_start_thread (jack_client_t *client)
  1500. {
  1501. if (client->engine->real_time) {
  1502. #ifdef USE_MLOCK
  1503. if (client->engine->do_mlock
  1504. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1505. jack_error ("cannot lock down memory for RT thread "
  1506. "(%s)", strerror (errno));
  1507. #ifdef ENSURE_MLOCK
  1508. return -1;
  1509. #endif /* ENSURE_MLOCK */
  1510. }
  1511. if (client->engine->do_munlock) {
  1512. cleanup_mlock ();
  1513. }
  1514. #endif /* USE_MLOCK */
  1515. }
  1516. #ifdef JACK_USE_MACH_THREADS
  1517. /* Stephane Letz : letz@grame.fr
  1518. On MacOSX, the normal thread does not need to be real-time.
  1519. */
  1520. if (jack_client_create_thread (client,
  1521. &client->thread,
  1522. client->engine->client_priority,
  1523. FALSE,
  1524. jack_client_thread, client)) {
  1525. return -1;
  1526. }
  1527. #else
  1528. if (jack_client_create_thread (client,
  1529. &client->thread,
  1530. client->engine->client_priority,
  1531. client->engine->real_time,
  1532. jack_client_thread, client)) {
  1533. return -1;
  1534. }
  1535. #endif
  1536. #ifdef JACK_USE_MACH_THREADS
  1537. /* a secondary thread that runs the process callback and uses
  1538. ultra-fast Mach primitives for inter-thread signalling.
  1539. XXX in a properly structured JACK, there would be no
  1540. need for this, because we would have client wake up
  1541. methods that encapsulated the underlying mechanism
  1542. used.
  1543. */
  1544. if (jack_client_create_thread(client,
  1545. &client->process_thread,
  1546. client->engine->client_priority,
  1547. client->engine->real_time,
  1548. jack_client_process_thread, client)) {
  1549. return -1;
  1550. }
  1551. #endif /* JACK_USE_MACH_THREADS */
  1552. return 0;
  1553. }
  1554. int
  1555. jack_activate (jack_client_t *client)
  1556. {
  1557. jack_request_t req;
  1558. /* we need to scribble on our stack to ensure that its memory
  1559. * pages are actually mapped (more important for mlockall(2)
  1560. * usage in jack_start_thread())
  1561. */
  1562. char buf[JACK_THREAD_STACK_TOUCH];
  1563. int i;
  1564. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1565. buf[i] = (char) (i & 0xff);
  1566. }
  1567. if (client->control->type == ClientInternal ||
  1568. client->control->type == ClientDriver) {
  1569. goto startit;
  1570. }
  1571. /* get the pid of the client process to pass it to engine */
  1572. client->control->pid = getpid ();
  1573. #ifdef USE_CAPABILITIES
  1574. if (client->engine->has_capabilities != 0 &&
  1575. client->control->pid != 0 && client->engine->real_time != 0) {
  1576. /* we need to ask the engine for realtime capabilities
  1577. before trying to start the realtime thread
  1578. */
  1579. req.type = SetClientCapabilities;
  1580. req.x.client_id = client->control->id;
  1581. req.x.cap_pid = client->control->pid;
  1582. jack_client_deliver_request (client, &req);
  1583. if (req.status) {
  1584. /* what to do? engine is running realtime, it
  1585. is using capabilities and has them
  1586. (otherwise we would not get an error
  1587. return) but for some reason it could not
  1588. give the client the required capabilities.
  1589. For now, leave the client so that it
  1590. still runs, albeit non-realtime.
  1591. */
  1592. jack_error ("could not receive realtime capabilities, "
  1593. "client will run non-realtime");
  1594. }
  1595. }
  1596. #endif /* USE_CAPABILITIES */
  1597. if (client->first_active) {
  1598. pthread_mutex_init (&client_lock, NULL);
  1599. pthread_cond_init (&client_ready, NULL);
  1600. pthread_mutex_lock (&client_lock);
  1601. if (jack_start_thread (client)) {
  1602. pthread_mutex_unlock (&client_lock);
  1603. return -1;
  1604. }
  1605. pthread_cond_wait (&client_ready, &client_lock);
  1606. pthread_mutex_unlock (&client_lock);
  1607. if (!client->thread_ok) {
  1608. jack_error ("could not start client thread");
  1609. return -1;
  1610. }
  1611. client->first_active = FALSE;
  1612. }
  1613. startit:
  1614. req.type = ActivateClient;
  1615. req.x.client_id = client->control->id;
  1616. return jack_client_deliver_request (client, &req);
  1617. }
  1618. static int
  1619. jack_deactivate_aux (jack_client_t *client)
  1620. {
  1621. jack_request_t req;
  1622. int rc = ESRCH; /* already shut down */
  1623. if (client && client->control) { /* not shut down? */
  1624. rc = 0;
  1625. if (client->control->active) { /* still active? */
  1626. req.type = DeactivateClient;
  1627. req.x.client_id = client->control->id;
  1628. rc = jack_client_deliver_request (client, &req);
  1629. }
  1630. }
  1631. return rc;
  1632. }
  1633. int
  1634. jack_deactivate (jack_client_t *client)
  1635. {
  1636. return jack_deactivate_aux(client);
  1637. }
  1638. static int
  1639. jack_client_close_aux (jack_client_t *client)
  1640. {
  1641. JSList *node;
  1642. void *status;
  1643. int rc;
  1644. rc = jack_deactivate_aux (client);
  1645. if (rc == ESRCH) { /* already shut down? */
  1646. return rc;
  1647. }
  1648. if (client->control->type == ClientExternal) {
  1649. #if JACK_USE_MACH_THREADS
  1650. if (client->rt_thread_ok) {
  1651. // MacOSX pthread_cancel not implemented in
  1652. // Darwin 5.5, 6.4
  1653. mach_port_t machThread =
  1654. pthread_mach_thread_np (client->process_thread);
  1655. thread_terminate (machThread);
  1656. }
  1657. #endif
  1658. /* stop the thread that communicates with the jack
  1659. * server, only if it was actually running
  1660. */
  1661. if (client->thread_ok){
  1662. pthread_cancel (client->thread);
  1663. pthread_join (client->thread, &status);
  1664. }
  1665. if (client->control) {
  1666. jack_release_shm (&client->control_shm);
  1667. client->control = NULL;
  1668. }
  1669. if (client->engine) {
  1670. jack_release_shm (&client->engine_shm);
  1671. client->engine = NULL;
  1672. }
  1673. if (client->port_segment) {
  1674. jack_port_type_id_t ptid;
  1675. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1676. jack_release_shm (&client->port_segment[ptid]);
  1677. }
  1678. free (client->port_segment);
  1679. client->port_segment = NULL;
  1680. }
  1681. #ifndef JACK_USE_MACH_THREADS
  1682. if (client->graph_wait_fd >= 0) {
  1683. close (client->graph_wait_fd);
  1684. }
  1685. if (client->graph_next_fd >= 0) {
  1686. close (client->graph_next_fd);
  1687. }
  1688. #endif
  1689. close (client->event_fd);
  1690. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1691. jack_error ("could not shutdown client request socket");
  1692. }
  1693. close (client->request_fd);
  1694. }
  1695. for (node = client->ports; node; node = jack_slist_next (node)) {
  1696. free (node->data);
  1697. }
  1698. jack_slist_free (client->ports);
  1699. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1700. free (node->data);
  1701. }
  1702. jack_slist_free (client->ports_ext);
  1703. jack_client_free (client);
  1704. jack_messagebuffer_exit ();
  1705. return rc;
  1706. }
  1707. int
  1708. jack_client_close (jack_client_t *client)
  1709. {
  1710. return jack_client_close_aux(client);
  1711. }
  1712. int
  1713. jack_is_realtime (jack_client_t *client)
  1714. {
  1715. return client->engine->real_time;
  1716. }
  1717. jack_nframes_t
  1718. jack_get_buffer_size (jack_client_t *client)
  1719. {
  1720. return client->engine->buffer_size;
  1721. }
  1722. int
  1723. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1724. {
  1725. #ifdef DO_BUFFER_RESIZE
  1726. jack_request_t req;
  1727. req.type = SetBufferSize;
  1728. req.x.nframes = nframes;
  1729. return jack_client_deliver_request (client, &req);
  1730. #else
  1731. return ENOSYS;
  1732. #endif /* DO_BUFFER_RESIZE */
  1733. }
  1734. int
  1735. jack_connect (jack_client_t *client, const char *source_port,
  1736. const char *destination_port)
  1737. {
  1738. jack_request_t req;
  1739. req.type = ConnectPorts;
  1740. snprintf (req.x.connect.source_port,
  1741. sizeof (req.x.connect.source_port), "%s", source_port);
  1742. snprintf (req.x.connect.destination_port,
  1743. sizeof (req.x.connect.destination_port),
  1744. "%s", destination_port);
  1745. return jack_client_deliver_request (client, &req);
  1746. }
  1747. int
  1748. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1749. {
  1750. jack_request_t req;
  1751. pthread_mutex_lock (&port->connection_lock);
  1752. if (port->connections == NULL) {
  1753. pthread_mutex_unlock (&port->connection_lock);
  1754. return 0;
  1755. }
  1756. pthread_mutex_unlock (&port->connection_lock);
  1757. req.type = DisconnectPort;
  1758. req.x.port_info.port_id = port->shared->id;
  1759. return jack_client_deliver_request (client, &req);
  1760. }
  1761. int
  1762. jack_disconnect (jack_client_t *client, const char *source_port,
  1763. const char *destination_port)
  1764. {
  1765. jack_request_t req;
  1766. req.type = DisconnectPorts;
  1767. snprintf (req.x.connect.source_port,
  1768. sizeof (req.x.connect.source_port), "%s", source_port);
  1769. snprintf (req.x.connect.destination_port,
  1770. sizeof (req.x.connect.destination_port),
  1771. "%s", destination_port);
  1772. return jack_client_deliver_request (client, &req);
  1773. }
  1774. void
  1775. jack_set_error_function (void (*func) (const char *))
  1776. {
  1777. jack_error_callback = func;
  1778. }
  1779. void
  1780. jack_set_info_function (void (*func) (const char *))
  1781. {
  1782. jack_info_callback = func;
  1783. }
  1784. int
  1785. jack_set_graph_order_callback (jack_client_t *client,
  1786. JackGraphOrderCallback callback, void *arg)
  1787. {
  1788. if (client->control->active) {
  1789. jack_error ("You cannot set callbacks on an active client.");
  1790. return -1;
  1791. }
  1792. client->graph_order = callback;
  1793. client->graph_order_arg = arg;
  1794. client->control->graph_order_cbset = (callback != NULL);
  1795. return 0;
  1796. }
  1797. int jack_set_xrun_callback (jack_client_t *client,
  1798. JackXRunCallback callback, void *arg)
  1799. {
  1800. if (client->control->active) {
  1801. jack_error ("You cannot set callbacks on an active client.");
  1802. return -1;
  1803. }
  1804. client->xrun = callback;
  1805. client->xrun_arg = arg;
  1806. client->control->xrun_cbset = (callback != NULL);
  1807. return 0;
  1808. }
  1809. int
  1810. jack_set_process_callback (jack_client_t *client,
  1811. JackProcessCallback callback, void *arg)
  1812. {
  1813. if (client->control->active) {
  1814. jack_error ("You cannot set callbacks on an active client.");
  1815. return -1;
  1816. }
  1817. if (client->control->thread_cb_cbset) {
  1818. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  1819. return -1;
  1820. }
  1821. client->process_arg = arg;
  1822. client->process = callback;
  1823. client->control->process_cbset = (callback != NULL);
  1824. return 0;
  1825. }
  1826. int
  1827. jack_set_thread_init_callback (jack_client_t *client,
  1828. JackThreadInitCallback callback, void *arg)
  1829. {
  1830. if (client->control->active) {
  1831. jack_error ("You cannot set callbacks on an active client.");
  1832. return -1;
  1833. }
  1834. client->thread_init_arg = arg;
  1835. client->thread_init = callback;
  1836. client->control->thread_init_cbset = (callback != NULL);
  1837. return 0;
  1838. }
  1839. int
  1840. jack_set_freewheel_callback (jack_client_t *client,
  1841. JackFreewheelCallback callback, void *arg)
  1842. {
  1843. if (client->control->active) {
  1844. jack_error ("You cannot set callbacks on an active client.");
  1845. return -1;
  1846. }
  1847. client->freewheel_arg = arg;
  1848. client->freewheel_cb = callback;
  1849. client->control->freewheel_cb_cbset = (callback != NULL);
  1850. return 0;
  1851. }
  1852. int
  1853. jack_set_buffer_size_callback (jack_client_t *client,
  1854. JackBufferSizeCallback callback, void *arg)
  1855. {
  1856. client->bufsize_arg = arg;
  1857. client->bufsize = callback;
  1858. client->control->bufsize_cbset = (callback != NULL);
  1859. return 0;
  1860. }
  1861. int
  1862. jack_set_port_registration_callback(jack_client_t *client,
  1863. JackPortRegistrationCallback callback,
  1864. void *arg)
  1865. {
  1866. if (client->control->active) {
  1867. jack_error ("You cannot set callbacks on an active client.");
  1868. return -1;
  1869. }
  1870. client->port_register_arg = arg;
  1871. client->port_register = callback;
  1872. client->control->port_register_cbset = (callback != NULL);
  1873. return 0;
  1874. }
  1875. int
  1876. jack_set_port_connect_callback(jack_client_t *client,
  1877. JackPortConnectCallback callback,
  1878. void *arg)
  1879. {
  1880. if (client->control->active) {
  1881. jack_error ("You cannot set callbacks on an active client.");
  1882. return -1;
  1883. }
  1884. client->port_connect_arg = arg;
  1885. client->port_connect = callback;
  1886. client->control->port_connect_cbset = (callback != NULL);
  1887. return 0;
  1888. }
  1889. int
  1890. jack_set_client_registration_callback(jack_client_t *client,
  1891. JackClientRegistrationCallback callback,
  1892. void *arg)
  1893. {
  1894. if (client->control->active) {
  1895. jack_error ("You cannot set callbacks on an active client.");
  1896. return -1;
  1897. }
  1898. client->client_register_arg = arg;
  1899. client->client_register = callback;
  1900. client->control->client_register_cbset = (callback != NULL);
  1901. return 0;
  1902. }
  1903. int
  1904. jack_set_process_thread(jack_client_t* client, JackThreadCallback callback, void *arg)
  1905. {
  1906. if (client->control->active) {
  1907. jack_error ("You cannot set callbacks on an active client.");
  1908. return -1;
  1909. }
  1910. if (client->control->process_cbset) {
  1911. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  1912. return -1;
  1913. }
  1914. client->thread_cb_arg = arg;
  1915. client->thread_cb = callback;
  1916. client->control->thread_cb_cbset = (callback != NULL);
  1917. return 0;
  1918. }
  1919. int
  1920. jack_get_process_done_fd (jack_client_t *client)
  1921. {
  1922. return client->graph_next_fd;
  1923. }
  1924. void
  1925. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1926. {
  1927. client->on_shutdown = function;
  1928. client->on_shutdown_arg = arg;
  1929. }
  1930. const char **
  1931. jack_get_ports (jack_client_t *client,
  1932. const char *port_name_pattern,
  1933. const char *type_name_pattern,
  1934. unsigned long flags)
  1935. {
  1936. jack_control_t *engine;
  1937. const char **matching_ports;
  1938. unsigned long match_cnt;
  1939. jack_port_shared_t *psp;
  1940. unsigned long i;
  1941. regex_t port_regex;
  1942. regex_t type_regex;
  1943. int matching;
  1944. engine = client->engine;
  1945. if (port_name_pattern && port_name_pattern[0]) {
  1946. regcomp (&port_regex, port_name_pattern,
  1947. REG_EXTENDED|REG_NOSUB);
  1948. }
  1949. if (type_name_pattern && type_name_pattern[0]) {
  1950. regcomp (&type_regex, type_name_pattern,
  1951. REG_EXTENDED|REG_NOSUB);
  1952. }
  1953. psp = engine->ports;
  1954. match_cnt = 0;
  1955. matching_ports = (const char **)
  1956. malloc (sizeof (char *) * engine->port_max);
  1957. for (i = 0; i < engine->port_max; i++) {
  1958. matching = 1;
  1959. if (!psp[i].in_use) {
  1960. continue;
  1961. }
  1962. if (flags) {
  1963. if ((psp[i].flags & flags) != flags) {
  1964. matching = 0;
  1965. }
  1966. }
  1967. if (matching && port_name_pattern && port_name_pattern[0]) {
  1968. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1969. matching = 0;
  1970. }
  1971. }
  1972. if (matching && type_name_pattern && type_name_pattern[0]) {
  1973. jack_port_type_id_t ptid = psp[i].ptype_id;
  1974. if (regexec (&type_regex,
  1975. engine->port_types[ptid].type_name,
  1976. 0, NULL, 0)) {
  1977. matching = 0;
  1978. }
  1979. }
  1980. if (matching) {
  1981. matching_ports[match_cnt++] = psp[i].name;
  1982. }
  1983. }
  1984. if (port_name_pattern && port_name_pattern[0]) {
  1985. regfree (&port_regex);
  1986. }
  1987. if (type_name_pattern && type_name_pattern[0]) {
  1988. regfree (&type_regex);
  1989. }
  1990. matching_ports[match_cnt] = 0;
  1991. if (match_cnt == 0) {
  1992. free (matching_ports);
  1993. matching_ports = 0;
  1994. }
  1995. return matching_ports;
  1996. }
  1997. float
  1998. jack_cpu_load (jack_client_t *client)
  1999. {
  2000. return client->engine->cpu_load;
  2001. }
  2002. float
  2003. jack_get_xrun_delayed_usecs (jack_client_t *client)
  2004. {
  2005. return client->engine->xrun_delayed_usecs;
  2006. }
  2007. float
  2008. jack_get_max_delayed_usecs (jack_client_t *client)
  2009. {
  2010. return client->engine->max_delayed_usecs;
  2011. }
  2012. void
  2013. jack_reset_max_delayed_usecs (jack_client_t *client)
  2014. {
  2015. client->engine->max_delayed_usecs = 0.0f;
  2016. }
  2017. pthread_t
  2018. jack_client_thread_id (jack_client_t *client)
  2019. {
  2020. return client->thread_id;
  2021. }
  2022. int
  2023. jack_client_name_size(void)
  2024. {
  2025. return JACK_CLIENT_NAME_SIZE;
  2026. }
  2027. int
  2028. jack_port_name_size(void)
  2029. {
  2030. return JACK_PORT_NAME_SIZE;
  2031. }
  2032. int
  2033. jack_port_type_size(void)
  2034. {
  2035. return JACK_PORT_TYPE_SIZE;
  2036. }