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.

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