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.

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