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.

2716 lines
62KB

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