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.

2548 lines
58KB

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