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.

2004 lines
46KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  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. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #include <regex.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/un.h>
  31. #include <sys/mman.h>
  32. #include <jack/internal.h>
  33. #include <jack/jack.h>
  34. #include <jack/engine.h>
  35. #include <jack/pool.h>
  36. #include <jack/jslist.h>
  37. #include <jack/version.h>
  38. #include <jack/shm.h>
  39. #include <jack/unlock.h>
  40. #include <jack/thread.h>
  41. #include <jack/varargs.h>
  42. #include <sysdeps/time.h>
  43. JACK_TIME_GLOBAL_DECL; /* One instance per process. */
  44. #include "local.h"
  45. #include <sysdeps/poll.h>
  46. #include <sysdeps/ipc.h>
  47. #ifdef JACK_USE_MACH_THREADS
  48. #include <sysdeps/pThreadUtilities.h>
  49. #endif
  50. #ifdef WITH_TIMESTAMPS
  51. #include <jack/timestamps.h>
  52. #endif /* WITH_TIMESTAMPS */
  53. static pthread_mutex_t client_lock;
  54. static pthread_cond_t client_ready;
  55. void *jack_zero_filled_buffer = NULL;
  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. void
  66. jack_error (const char *fmt, ...)
  67. {
  68. va_list ap;
  69. char buffer[300];
  70. va_start (ap, fmt);
  71. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  72. jack_error_callback (buffer);
  73. va_end (ap);
  74. }
  75. void
  76. default_jack_error_callback (const char *desc)
  77. {
  78. fprintf(stderr, "%s\n", desc);
  79. }
  80. void
  81. silent_jack_error_callback (const char *desc)
  82. {
  83. }
  84. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  85. static int
  86. oop_client_deliver_request (void *ptr, jack_request_t *req)
  87. {
  88. int wok, rok;
  89. jack_client_t *client = (jack_client_t*) ptr;
  90. wok = (write (client->request_fd, req, sizeof (*req))
  91. == sizeof (*req));
  92. rok = (read (client->request_fd, req, sizeof (*req))
  93. == sizeof (*req));
  94. if (wok && rok) { /* everything OK? */
  95. return req->status;
  96. }
  97. req->status = -1; /* request failed */
  98. /* check for server shutdown */
  99. if (client->engine->engine_ok == 0)
  100. return req->status;
  101. /* otherwise report errors */
  102. if (!wok)
  103. jack_error ("cannot send request type %d to server",
  104. req->type);
  105. if (!rok)
  106. jack_error ("cannot read result for request type %d from"
  107. " server (%s)", req->type, strerror (errno));
  108. return req->status;
  109. }
  110. int
  111. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  112. {
  113. /* indirect through the function pointer that was set either
  114. * by jack_client_open() or by jack_new_client_request() in
  115. * the server.
  116. */
  117. return client->control->deliver_request (client->control->deliver_arg,
  118. req);
  119. }
  120. jack_client_t *
  121. jack_client_alloc ()
  122. {
  123. jack_client_t *client;
  124. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  125. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  126. client->pollmax = 2;
  127. client->request_fd = -1;
  128. client->event_fd = -1;
  129. client->upstream_is_jackd = 0;
  130. client->graph_wait_fd = -1;
  131. client->graph_next_fd = -1;
  132. client->ports = NULL;
  133. client->ports_ext = NULL;
  134. client->engine = NULL;
  135. client->control = NULL;
  136. client->thread_ok = FALSE;
  137. #if JACK_USE_MACH_THREADS
  138. client->rt_thread_ok = FALSE;
  139. #endif
  140. client->first_active = TRUE;
  141. client->on_shutdown = NULL;
  142. client->n_port_types = 0;
  143. client->port_segment = NULL;
  144. return client;
  145. }
  146. /*
  147. * Build the jack_client_t structure for an internal client.
  148. */
  149. jack_client_t *
  150. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  151. {
  152. jack_client_t* client;
  153. client = jack_client_alloc ();
  154. client->control = cc;
  155. client->engine = engine->control;
  156. client->n_port_types = client->engine->n_port_types;
  157. client->port_segment = &engine->port_segment[0];
  158. return client;
  159. }
  160. static void
  161. jack_client_free (jack_client_t *client)
  162. {
  163. if (client->pollfd) {
  164. free (client->pollfd);
  165. }
  166. free (client);
  167. }
  168. void
  169. jack_client_invalidate_port_buffers (jack_client_t *client)
  170. {
  171. JSList *node;
  172. jack_port_t *port;
  173. /* This releases all local memory owned by input ports
  174. and sets the buffer pointer to NULL. This will cause
  175. jack_port_get_buffer() to reallocate space for the
  176. buffer on the next call (if there is one).
  177. */
  178. for (node = client->ports; node; node = jack_slist_next (node)) {
  179. port = (jack_port_t *) node->data;
  180. if (port->shared->flags & JackPortIsInput) {
  181. if (port->mix_buffer) {
  182. jack_pool_release (port->mix_buffer);
  183. port->mix_buffer = NULL;
  184. }
  185. }
  186. }
  187. }
  188. int
  189. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  190. {
  191. jack_port_t *control_port;
  192. jack_port_t *other;
  193. JSList *node;
  194. int need_free = FALSE;
  195. switch (event->type) {
  196. case PortConnected:
  197. other = jack_port_new (client, event->y.other_id,
  198. client->engine);
  199. /* jack_port_by_id_int() always returns an internal
  200. * port that does not need to be deallocated */
  201. control_port = jack_port_by_id_int (client, event->x.self_id,
  202. &need_free);
  203. pthread_mutex_lock (&control_port->connection_lock);
  204. control_port->connections =
  205. jack_slist_prepend (control_port->connections,
  206. (void *) other);
  207. pthread_mutex_unlock (&control_port->connection_lock);
  208. break;
  209. case PortDisconnected:
  210. /* jack_port_by_id_int() always returns an internal
  211. * port that does not need to be deallocated */
  212. control_port = jack_port_by_id_int (client, event->x.self_id,
  213. &need_free);
  214. pthread_mutex_lock (&control_port->connection_lock);
  215. for (node = control_port->connections; node;
  216. node = jack_slist_next (node)) {
  217. other = (jack_port_t *) node->data;
  218. if (other->shared->id == event->y.other_id) {
  219. control_port->connections =
  220. jack_slist_remove_link (
  221. control_port->connections,
  222. node);
  223. jack_slist_free_1 (node);
  224. free (other);
  225. break;
  226. }
  227. }
  228. pthread_mutex_unlock (&control_port->connection_lock);
  229. break;
  230. default:
  231. /* impossible */
  232. break;
  233. }
  234. return 0;
  235. }
  236. static int
  237. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  238. {
  239. char path[PATH_MAX+1];
  240. if (client->graph_wait_fd >= 0) {
  241. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  242. close (client->graph_wait_fd);
  243. client->graph_wait_fd = -1;
  244. }
  245. if (client->graph_next_fd >= 0) {
  246. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  247. close (client->graph_next_fd);
  248. client->graph_next_fd = -1;
  249. }
  250. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  251. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  252. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  253. path, strerror (errno));
  254. return -1;
  255. }
  256. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  257. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  258. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  259. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  260. path, strerror (errno));
  261. return -1;
  262. }
  263. client->upstream_is_jackd = event->y.n;
  264. client->pollmax = 2;
  265. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  266. client->graph_next_fd, path,
  267. client->upstream_is_jackd);
  268. /* If the client registered its own callback for graph order events,
  269. execute it now.
  270. */
  271. if (client->control->graph_order) {
  272. client->control->graph_order (client->control->graph_order_arg);
  273. }
  274. return 0;
  275. }
  276. static int
  277. server_connect (const char *server_name)
  278. {
  279. int fd;
  280. struct sockaddr_un addr;
  281. int which = 0;
  282. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  283. jack_error ("cannot create client socket (%s)",
  284. strerror (errno));
  285. return -1;
  286. }
  287. //JOQ: temporary debug message
  288. //fprintf (stderr, "DEBUG: connecting to `%s' server\n", server_name);
  289. addr.sun_family = AF_UNIX;
  290. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  291. jack_server_dir (server_name), which);
  292. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  293. close (fd);
  294. return -1;
  295. }
  296. return fd;
  297. }
  298. static int
  299. server_event_connect (jack_client_t *client, const char *server_name)
  300. {
  301. int fd;
  302. struct sockaddr_un addr;
  303. jack_client_connect_ack_request_t req;
  304. jack_client_connect_ack_result_t res;
  305. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  306. jack_error ("cannot create client event socket (%s)",
  307. strerror (errno));
  308. return -1;
  309. }
  310. addr.sun_family = AF_UNIX;
  311. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  312. jack_server_dir (server_name));
  313. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  314. jack_error ("cannot connect to jack server for events",
  315. strerror (errno));
  316. close (fd);
  317. return -1;
  318. }
  319. req.client_id = client->control->id;
  320. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  321. jack_error ("cannot write event connect request to server (%s)",
  322. strerror (errno));
  323. close (fd);
  324. return -1;
  325. }
  326. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  327. jack_error ("cannot read event connect result from server (%s)",
  328. strerror (errno));
  329. close (fd);
  330. return -1;
  331. }
  332. if (res.status != 0) {
  333. jack_error ("cannot connect to server for event stream (%s)",
  334. strerror (errno));
  335. close (fd);
  336. return -1;
  337. }
  338. return fd;
  339. }
  340. /* Exec the JACK server in this process. Does not return. */
  341. static void
  342. _start_server (void)
  343. {
  344. FILE* fp = 0;
  345. char filename[255];
  346. char arguments[255];
  347. char buffer[255];
  348. char* command = 0;
  349. size_t pos = 0;
  350. size_t result = 0;
  351. char** argv = 0;
  352. int i = 0;
  353. int good = 0;
  354. int ret;
  355. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  356. fp = fopen(filename, "r");
  357. if (!fp) {
  358. fp = fopen("/etc/jackd.conf", "r");
  359. }
  360. if (fp) {
  361. arguments[0] = '\0';
  362. ret = fscanf(fp, "%s", buffer);
  363. while(ret != 0 && ret != EOF) {
  364. strcat(arguments, buffer);
  365. strcat(arguments, " ");
  366. ret = fscanf(fp, "%s", buffer);
  367. }
  368. if (strlen(arguments) > 0) {
  369. good = 1;
  370. }
  371. }
  372. if (!good) {
  373. #define JACK_LOCATION
  374. #if defined(USE_CAPABILITIES)
  375. command = JACK_LOCATION "/jackstart";
  376. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  377. JACK_DEFAULT_DRIVER " -p 512", 255);
  378. #else /* !USE_CAPABILITIES */
  379. command = JACK_LOCATION "/jackd";
  380. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  381. JACK_DEFAULT_DRIVER, 255);
  382. #endif /* USE_CAPABILITIES */
  383. } else {
  384. result = strcspn(arguments, " ");
  385. command = (char *) malloc(result+1);
  386. strncpy(command, arguments, result);
  387. command[result] = '\0';
  388. }
  389. argv = (char **) malloc (255);
  390. while(1) {
  391. /* insert -T into arguments */
  392. if (i == 1) {
  393. argv[i] = (char*)malloc(3);
  394. strncpy(argv[i], "-T", 2);
  395. argv[i][2] = '\0';
  396. ++i;
  397. }
  398. result = strcspn(arguments+pos, " ");
  399. if (result == 0) {
  400. break;
  401. }
  402. argv[i] = (char*)malloc(result+1);
  403. strncpy(argv[i], arguments+pos, result);
  404. argv[i][result] = '\0';
  405. pos += result+1;
  406. ++i;
  407. }
  408. argv[i] = 0;
  409. execv (command, argv);
  410. /* If execv() succeeds, it does not return. There's no point
  411. * in calling jack_error() here in the child process. */
  412. perror ("exec of JACK server failed");
  413. }
  414. int
  415. start_server (jack_options_t options)
  416. {
  417. if ((options & JackNoStartServer)
  418. || getenv("JACK_NO_START_SERVER")) {
  419. return 1;
  420. }
  421. /* The double fork() forces the server to become a child of
  422. * init, which will always clean up zombie process state on
  423. * termination. This even works in cases where the server
  424. * terminates but this client does not.
  425. *
  426. * Since fork() is usually implemented using copy-on-write
  427. * virtual memory tricks, the overhead of the second fork() is
  428. * probably relatively small.
  429. */
  430. switch (fork()) {
  431. case 0: /* child process */
  432. switch (fork()) {
  433. case 0: /* grandchild process */
  434. _start_server();
  435. _exit (99); /* exec failed */
  436. case -1:
  437. _exit (98);
  438. default:
  439. _exit (0);
  440. }
  441. case -1: /* fork() error */
  442. return 1; /* failed to start server */
  443. }
  444. /* only the original parent process goes here */
  445. return 0; /* (probably) successful */
  446. }
  447. static int
  448. jack_request_client (ClientType type,
  449. const char* client_name, jack_options_t options,
  450. jack_status_t *status, jack_varargs_t *va,
  451. jack_client_connect_result_t *res, int *req_fd)
  452. {
  453. jack_client_connect_request_t req;
  454. *req_fd = -1;
  455. memset (&req, 0, sizeof (req));
  456. req.options = options;
  457. if (strlen (client_name) >= sizeof (req.name)) {
  458. jack_error ("\"%s\" is too long to be used as a JACK client"
  459. " name.\n"
  460. "Please use %lu characters or less.",
  461. client_name, sizeof (req.name));
  462. return -1;
  463. }
  464. if (va->load_name
  465. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  466. jack_error ("\"%s\" is too long to be used as a JACK shared"
  467. " object name.\n"
  468. "Please use %lu characters or less.",
  469. va->load_name, sizeof (req.object_path) - 1);
  470. return -1;
  471. }
  472. if (va->load_init
  473. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  474. jack_error ("\"%s\" is too long to be used as a JACK shared"
  475. " object data string.\n"
  476. "Please use %lu characters or less.",
  477. va->load_init, sizeof (req.object_data) - 1);
  478. return -1;
  479. }
  480. if ((*req_fd = server_connect (va->server_name)) < 0) {
  481. int trys;
  482. if (start_server(options)) {
  483. *status |= (JackFailure|JackServerFailed);
  484. goto fail;
  485. }
  486. trys = 5;
  487. do {
  488. sleep(1);
  489. if (--trys < 0) {
  490. *status |= (JackFailure|JackServerFailed);
  491. goto fail;
  492. }
  493. } while ((*req_fd = server_connect (va->server_name)) < 0);
  494. *status |= JackServerStarted;
  495. }
  496. req.load = TRUE;
  497. req.type = type;
  498. snprintf (req.name, sizeof (req.name),
  499. "%s", client_name);
  500. snprintf (req.object_path, sizeof (req.object_path),
  501. "%s", va->load_name);
  502. snprintf (req.object_data, sizeof (req.object_data),
  503. "%s", va->load_init);
  504. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  505. jack_error ("cannot send request to jack server (%s)",
  506. strerror (errno));
  507. *status |= (JackFailure|JackServerError);
  508. goto fail;
  509. }
  510. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  511. if (errno == 0) {
  512. /* server shut the socket */
  513. jack_error ("could not attach as client");
  514. *status |= (JackFailure|JackServerError);
  515. goto fail;
  516. }
  517. if (errno == ECONNRESET) {
  518. jack_error ("could not attach as JACK client "
  519. "(server has exited)");
  520. *status |= (JackFailure|JackServerError);
  521. goto fail;
  522. }
  523. jack_error ("cannot read response from jack server (%s)",
  524. strerror (errno));
  525. *status |= (JackFailure|JackServerError);
  526. goto fail;
  527. }
  528. *status |= res->status; /* return server status bits */
  529. if (*status & JackFailure) {
  530. jack_error ("could not attach as client");
  531. *status |= JackServerError;
  532. goto fail;
  533. }
  534. if (res->protocol_v != jack_protocol_version){
  535. jack_error ("application linked against incompatible libjack"
  536. " version.");
  537. *status |= (JackFailure|JackServerError);
  538. goto fail;
  539. }
  540. switch (type) {
  541. case ClientDriver:
  542. case ClientInternal:
  543. close (*req_fd);
  544. *req_fd = -1;
  545. break;
  546. default:
  547. break;
  548. }
  549. return 0;
  550. fail:
  551. if (*req_fd >= 0) {
  552. close (*req_fd);
  553. *req_fd = -1;
  554. }
  555. return -1;
  556. }
  557. int
  558. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  559. {
  560. /* Lookup, attach and register the port/buffer segments in use
  561. * right now.
  562. */
  563. if (client->control->type != ClientExternal) {
  564. jack_error("Only external clients need attach port segments");
  565. abort();
  566. }
  567. /* make sure we have space to store the port
  568. segment information.
  569. */
  570. if (ptid >= client->n_port_types) {
  571. client->port_segment = (jack_shm_info_t*)
  572. realloc (client->port_segment,
  573. sizeof (jack_shm_info_t) * (ptid+1));
  574. memset (&client->port_segment[client->n_port_types],
  575. 0,
  576. sizeof (jack_shm_info_t) *
  577. (ptid - client->n_port_types));
  578. client->n_port_types = ptid + 1;
  579. } else {
  580. /* release any previous segment */
  581. #if JACK_USE_MACH_THREADS
  582. /* Stephane Letz : letz@grame.fr
  583. Need a fix : this crash on MacOSX : temporary removed
  584. jack_release_shm (&client->port_segment[ptid]);
  585. */
  586. #else
  587. jack_release_shm (&client->port_segment[ptid]);
  588. #endif
  589. }
  590. /* get the index into the shm registry */
  591. client->port_segment[ptid].index =
  592. client->engine->port_types[ptid].shm_registry_index;
  593. /* attach the relevant segment */
  594. if (jack_attach_shm (&client->port_segment[ptid])) {
  595. jack_error ("cannot attach port segment shared memory"
  596. " (%s)", strerror (errno));
  597. return -1;
  598. }
  599. /* The first chunk of the audio port segment will be set by
  600. * the engine to be a zero-filled buffer. This hasn't been
  601. * done yet, but it will happen before the process cycle
  602. * (re)starts.
  603. */
  604. if (ptid == JACK_AUDIO_PORT_TYPE) {
  605. jack_zero_filled_buffer =
  606. jack_shm_addr (&client->port_segment[ptid]);
  607. }
  608. return 0;
  609. }
  610. jack_client_t *
  611. jack_client_open (const char *client_name,
  612. jack_options_t options,
  613. jack_status_t *status, ...)
  614. {
  615. /* optional arguments: */
  616. va_list ap; /* variable argument pointer */
  617. jack_varargs_t va; /* variable arguments */
  618. int req_fd = -1;
  619. int ev_fd = -1;
  620. jack_client_connect_result_t res;
  621. jack_client_t *client;
  622. jack_port_type_id_t ptid;
  623. jack_status_t my_status;
  624. if (status == NULL) /* no status from caller? */
  625. status = &my_status; /* use local status word */
  626. *status = 0;
  627. /* validate parameters */
  628. if ((options & ~JackOpenOptions)) {
  629. *status |= (JackFailure|JackInvalidOption);
  630. return NULL;
  631. }
  632. /* parse variable arguments */
  633. va_start (ap, status);
  634. jack_varargs_parse (options, ap, &va);
  635. va_end (ap);
  636. /* External clients need this initialized. It is already set
  637. * up in the server's address space for internal clients.
  638. */
  639. jack_init_time ();
  640. if (jack_request_client (ClientExternal, client_name, options, status,
  641. &va, &res, &req_fd)) {
  642. return NULL;
  643. }
  644. /* don't access shared memory until server connected */
  645. if (jack_initialize_shm ()) {
  646. jack_error ("Unable to initialize shared memory.");
  647. return NULL;
  648. }
  649. client = jack_client_alloc ();
  650. strcpy (client->name, res.name);
  651. strcpy (client->fifo_prefix, res.fifo_prefix);
  652. client->request_fd = req_fd;
  653. client->pollfd[EVENT_POLL_INDEX].events =
  654. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  655. client->pollfd[WAIT_POLL_INDEX].events =
  656. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  657. /* attach the engine control/info block */
  658. client->engine_shm = res.engine_shm;
  659. if (jack_attach_shm (&client->engine_shm)) {
  660. jack_error ("cannot attached engine control shared memory"
  661. " segment");
  662. goto fail;
  663. }
  664. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  665. /* now attach the client control block */
  666. client->control_shm = res.client_shm;
  667. if (jack_attach_shm (&client->control_shm)) {
  668. jack_error ("cannot attached client control shared memory"
  669. " segment");
  670. goto fail;
  671. }
  672. client->control = (jack_client_control_t *)
  673. jack_shm_addr (&client->control_shm);
  674. /* nobody else needs to access this shared memory any more, so
  675. destroy it. because we have our own attachment to it, it won't
  676. vanish till we exit (and release it).
  677. */
  678. jack_destroy_shm (&client->control_shm);
  679. client->n_port_types = client->engine->n_port_types;
  680. client->port_segment = (jack_shm_info_t *)
  681. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  682. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  683. client->port_segment[ptid].index =
  684. client->engine->port_types[ptid].shm_registry_index;
  685. client->port_segment[ptid].attached_at = MAP_FAILED;
  686. jack_attach_port_segment (client, ptid);
  687. }
  688. /* set up the client so that it does the right thing for an
  689. * external client
  690. */
  691. client->control->deliver_request = oop_client_deliver_request;
  692. client->control->deliver_arg = client;
  693. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  694. goto fail;
  695. }
  696. client->event_fd = ev_fd;
  697. #ifdef JACK_USE_MACH_THREADS
  698. /* specific resources for server/client real-time thread
  699. * communication */
  700. client->clienttask = mach_task_self();
  701. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  702. jack_error ("Can't find bootstrap port");
  703. goto fail;
  704. }
  705. if (allocate_mach_clientport(client, res.portnum) < 0) {
  706. jack_error("Can't allocate mach port");
  707. goto fail;
  708. };
  709. #endif /* JACK_USE_MACH_THREADS */
  710. return client;
  711. fail:
  712. if (client->engine) {
  713. jack_release_shm (&client->engine_shm);
  714. client->engine = 0;
  715. }
  716. if (client->control) {
  717. jack_release_shm (&client->control_shm);
  718. client->control = 0;
  719. }
  720. if (req_fd >= 0) {
  721. close (req_fd);
  722. }
  723. if (ev_fd >= 0) {
  724. close (ev_fd);
  725. }
  726. return 0;
  727. }
  728. jack_client_t *
  729. jack_client_new (const char *client_name)
  730. {
  731. jack_options_t options = JackUseExactName;
  732. if (getenv("JACK_START_SERVER") == NULL)
  733. options |= JackNoStartServer;
  734. return jack_client_open (client_name, options, NULL);
  735. }
  736. char *
  737. jack_get_client_name (jack_client_t *client)
  738. {
  739. return client->name;
  740. }
  741. int
  742. jack_internal_client_new (const char *client_name,
  743. const char *so_name, const char *so_data)
  744. {
  745. jack_client_connect_result_t res;
  746. int req_fd;
  747. jack_varargs_t va;
  748. jack_status_t status;
  749. jack_options_t options = JackUseExactName;
  750. if (getenv("JACK_START_SERVER") == NULL)
  751. options |= JackNoStartServer;
  752. jack_varargs_init (&va);
  753. va.load_name = (char *) so_name;
  754. va.load_init = (char *) so_data;
  755. return jack_request_client (ClientInternal, client_name,
  756. options, &status, &va, &res, &req_fd);
  757. }
  758. char *
  759. jack_default_server_name (void)
  760. {
  761. char *server_name;
  762. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  763. server_name = "default";
  764. return server_name;
  765. }
  766. char *jack_tmpdir = DEFAULT_TMP_DIR;
  767. /* returns the name of the per-user subdirectory of jack_tmpdir */
  768. char *
  769. jack_user_dir (void)
  770. {
  771. static char user_dir[PATH_MAX] = "";
  772. /* format the path name on the first call */
  773. if (user_dir[0] == '\0') {
  774. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  775. jack_tmpdir, getuid ());
  776. }
  777. return user_dir;
  778. }
  779. /* returns the name of the per-server subdirectory of jack_user_dir() */
  780. char *
  781. jack_server_dir (const char *server_name)
  782. {
  783. static char server_dir[PATH_MAX] = "";
  784. /* format the path name on the first call */
  785. if (server_dir[0] == '\0') {
  786. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  787. jack_user_dir (), server_name);
  788. }
  789. return server_dir;
  790. }
  791. void
  792. jack_internal_client_close (const char *client_name)
  793. {
  794. jack_client_connect_request_t req;
  795. int fd;
  796. char *server_name = jack_default_server_name ();
  797. req.load = FALSE;
  798. snprintf (req.name, sizeof (req.name), "%s", client_name);
  799. if ((fd = server_connect (server_name)) < 0) {
  800. return;
  801. }
  802. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  803. jack_error ("cannot deliver ClientUnload request to JACK "
  804. "server.");
  805. }
  806. /* no response to this request */
  807. close (fd);
  808. return;
  809. }
  810. int
  811. jack_set_freewheel (jack_client_t* client, int onoff)
  812. {
  813. jack_request_t request;
  814. request.type = onoff ? FreeWheel : StopFreeWheel;
  815. return jack_client_deliver_request (client, &request);
  816. }
  817. void
  818. jack_start_freewheel (jack_client_t* client)
  819. {
  820. jack_client_control_t *control = client->control;
  821. if (client->engine->real_time) {
  822. #if JACK_USE_MACH_THREADS
  823. jack_drop_real_time_scheduling (client->process_thread);
  824. #else
  825. jack_drop_real_time_scheduling (client->thread);
  826. #endif
  827. }
  828. if (control->freewheel_cb) {
  829. control->freewheel_cb (1, control->freewheel_arg);
  830. }
  831. }
  832. void
  833. jack_stop_freewheel (jack_client_t* client)
  834. {
  835. jack_client_control_t *control = client->control;
  836. if (control->freewheel_cb) {
  837. control->freewheel_cb (0, control->freewheel_arg);
  838. }
  839. if (client->engine->real_time) {
  840. #if JACK_USE_MACH_THREADS
  841. jack_acquire_real_time_scheduling (
  842. client->process_thread,
  843. client->engine->client_priority);
  844. #else
  845. jack_acquire_real_time_scheduling (
  846. client->thread,
  847. client->engine->client_priority);
  848. #endif
  849. }
  850. }
  851. static void *
  852. jack_client_thread (void *arg)
  853. {
  854. jack_client_t *client = (jack_client_t *) arg;
  855. jack_client_control_t *control = client->control;
  856. jack_event_t event;
  857. char status = 0;
  858. char c = 0;
  859. int err = 0;
  860. pthread_mutex_lock (&client_lock);
  861. client->thread_ok = TRUE;
  862. client->thread_id = pthread_self();
  863. pthread_cond_signal (&client_ready);
  864. pthread_mutex_unlock (&client_lock);
  865. client->control->pid = getpid();
  866. client->control->pgrp = getpgrp();
  867. DEBUG ("client thread is now running");
  868. if (client->control->thread_init) {
  869. DEBUG ("calling client thread init callback");
  870. client->control->thread_init (client->control->thread_init_arg);
  871. }
  872. while (err == 0) {
  873. if (client->engine->engine_ok == 0) {
  874. if (client->on_shutdown)
  875. client->on_shutdown (client->on_shutdown_arg);
  876. else
  877. jack_error ("engine unexpectedly shutdown; "
  878. "thread exiting\n");
  879. pthread_exit (0);
  880. }
  881. DEBUG ("client polling on %s", client->pollmax == 2 ?
  882. "event_fd and graph_wait_fd..." :
  883. "event_fd only");
  884. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  885. if (errno == EINTR) {
  886. continue;
  887. }
  888. jack_error ("poll failed in client (%s)",
  889. strerror (errno));
  890. status = -1;
  891. break;
  892. }
  893. /* get an accurate timestamp on waking from poll for a
  894. * process() cycle.
  895. */
  896. if (client->graph_wait_fd >= 0
  897. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  898. control->awake_at = jack_get_microseconds();
  899. }
  900. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  901. client->pollfd[EVENT_POLL_INDEX].revents,
  902. client->pollfd[WAIT_POLL_INDEX].revents);
  903. pthread_testcancel();
  904. if (client->graph_wait_fd >= 0 &&
  905. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  906. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR,"
  907. " ZOMBIE\n\n\n\n\n");
  908. /* our upstream "wait" connection
  909. closed, which either means that
  910. an intermediate client exited, or
  911. jackd exited, or jackd zombified
  912. us.
  913. we can discover the zombification
  914. via client->control->dead, but
  915. the other two possibilities are
  916. impossible to identify just from
  917. this situation. so we have to
  918. check what we are connected to,
  919. and act accordingly.
  920. */
  921. if (client->upstream_is_jackd) {
  922. DEBUG ("WE DIE\n");
  923. goto zombie;
  924. } else {
  925. DEBUG ("WE PUNT\n");
  926. /* don't poll on the wait fd
  927. * again until we get a
  928. * GraphReordered event.
  929. */
  930. client->graph_wait_fd = -1;
  931. client->pollmax = 1;
  932. }
  933. }
  934. if (client->control->dead) {
  935. goto zombie;
  936. }
  937. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  938. /* jackd shutdown */
  939. goto zombie;
  940. }
  941. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  942. DEBUG ("client receives an event, "
  943. "now reading on event fd");
  944. /* server has sent us an event. process the
  945. * event and reply */
  946. if (read (client->event_fd, &event, sizeof (event))
  947. != sizeof (event)) {
  948. jack_error ("cannot read server event (%s)",
  949. strerror (errno));
  950. err++;
  951. break;
  952. }
  953. status = 0;
  954. switch (event.type) {
  955. case PortRegistered:
  956. if (control->port_register) {
  957. control->port_register
  958. (event.x.port_id, TRUE,
  959. control->port_register_arg);
  960. }
  961. break;
  962. case PortUnregistered:
  963. if (control->port_register) {
  964. control->port_register
  965. (event.x.port_id, FALSE,
  966. control->port_register_arg);
  967. }
  968. break;
  969. case GraphReordered:
  970. status = jack_handle_reorder (client, &event);
  971. break;
  972. case PortConnected:
  973. case PortDisconnected:
  974. status = jack_client_handle_port_connection
  975. (client, &event);
  976. break;
  977. case BufferSizeChange:
  978. jack_client_invalidate_port_buffers (client);
  979. if (control->bufsize) {
  980. status = control->bufsize
  981. (control->nframes,
  982. control->bufsize_arg);
  983. }
  984. break;
  985. case SampleRateChange:
  986. if (control->srate) {
  987. status = control->srate
  988. (control->nframes,
  989. control->srate_arg);
  990. }
  991. break;
  992. case XRun:
  993. if (control->xrun) {
  994. status = control->xrun
  995. (control->xrun_arg);
  996. }
  997. break;
  998. case AttachPortSegment:
  999. jack_attach_port_segment (client, event.y.ptid);
  1000. break;
  1001. case StartFreewheel:
  1002. jack_start_freewheel (client);
  1003. break;
  1004. case StopFreewheel:
  1005. jack_stop_freewheel (client);
  1006. break;
  1007. }
  1008. DEBUG ("client has dealt with the event, writing "
  1009. "response on event fd");
  1010. if (write (client->event_fd, &status, sizeof (status))
  1011. != sizeof (status)) {
  1012. jack_error ("cannot send event response to "
  1013. "engine (%s)", strerror (errno));
  1014. err++;
  1015. break;
  1016. }
  1017. }
  1018. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1019. #ifdef WITH_TIMESTAMPS
  1020. jack_reset_timestamps ();
  1021. #endif
  1022. DEBUG ("client %d signalled at %" PRIu64
  1023. ", awake for process at %" PRIu64
  1024. " (delay = %" PRIu64
  1025. " usecs) (wakeup on graph_wait_fd==%d)",
  1026. getpid(),
  1027. control->signalled_at,
  1028. control->awake_at,
  1029. control->awake_at - control->signalled_at,
  1030. client->pollfd[WAIT_POLL_INDEX].fd);
  1031. control->state = Running;
  1032. /* begin preemption checking */
  1033. CHECK_PREEMPTION (client->engine, TRUE);
  1034. if (control->sync_cb)
  1035. jack_call_sync_client (client);
  1036. if (control->process) {
  1037. if (control->process (control->nframes,
  1038. control->process_arg)
  1039. == 0) {
  1040. control->state = Finished;
  1041. }
  1042. } else {
  1043. control->state = Finished;
  1044. }
  1045. if (control->timebase_cb)
  1046. jack_call_timebase_master (client);
  1047. /* end preemption checking */
  1048. CHECK_PREEMPTION (client->engine, FALSE);
  1049. control->finished_at = jack_get_microseconds();
  1050. #ifdef WITH_TIMESTAMPS
  1051. jack_timestamp ("finished");
  1052. #endif
  1053. /* pass the execution token along */
  1054. DEBUG ("client finished processing at %" PRIu64
  1055. " (elapsed = %" PRIu64
  1056. " usecs), writing on graph_next_fd==%d",
  1057. control->finished_at,
  1058. control->finished_at - control->awake_at,
  1059. client->graph_next_fd);
  1060. if (write (client->graph_next_fd, &c, sizeof (c))
  1061. != sizeof (c)) {
  1062. jack_error ("cannot continue execution of the "
  1063. "processing graph (%s)",
  1064. strerror(errno));
  1065. err++;
  1066. break;
  1067. }
  1068. DEBUG ("client sent message to next stage by %" PRIu64
  1069. ", client reading on graph_wait_fd==%d",
  1070. jack_get_microseconds(), client->graph_wait_fd);
  1071. #ifdef WITH_TIMESTAMPS
  1072. jack_timestamp ("read pending byte from wait");
  1073. #endif
  1074. DEBUG("reading cleanup byte from pipe\n");
  1075. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1076. != sizeof (c))) {
  1077. DEBUG ("WARNING: READ FAILED!");
  1078. #if 0
  1079. jack_error ("cannot complete execution of the "
  1080. "processing graph (%s)",
  1081. strerror(errno));
  1082. err++;
  1083. break;
  1084. #endif
  1085. }
  1086. /* check if we were killed during the process
  1087. * cycle (or whatever) */
  1088. if (client->control->dead) {
  1089. goto zombie;
  1090. }
  1091. DEBUG("process cycle fully complete\n");
  1092. #ifdef WITH_TIMESTAMPS
  1093. jack_timestamp ("read done");
  1094. jack_dump_timestamps (stdout);
  1095. #endif
  1096. }
  1097. }
  1098. return (void *) ((intptr_t)err);
  1099. zombie:
  1100. if (client->on_shutdown) {
  1101. jack_error ("zombified - calling shutdown handler");
  1102. client->on_shutdown (client->on_shutdown_arg);
  1103. } else {
  1104. jack_error ("zombified - exiting from JACK");
  1105. jack_client_close (client);
  1106. /* Need a fix : possibly make client crash if
  1107. * zombified without shutdown handler
  1108. */
  1109. }
  1110. pthread_exit (0);
  1111. /*NOTREACHED*/
  1112. return 0;
  1113. }
  1114. #ifdef JACK_USE_MACH_THREADS
  1115. /* real-time thread : separated from the normal client thread, it will
  1116. * communicate with the server using fast mach RPC mechanism */
  1117. static void *
  1118. jack_client_process_thread (void *arg)
  1119. {
  1120. jack_client_t *client = (jack_client_t *) arg;
  1121. jack_client_control_t *control = client->control;
  1122. int err = 0;
  1123. if (client->control->thread_init) {
  1124. /* this means that the init callback will be called twice -taybin*/
  1125. DEBUG ("calling client thread init callback");
  1126. client->control->thread_init (client->control->thread_init_arg);
  1127. }
  1128. client->control->pid = getpid();
  1129. DEBUG ("client process thread is now running");
  1130. client->rt_thread_ok = TRUE;
  1131. while (err == 0) {
  1132. if (jack_client_suspend(client) < 0) {
  1133. jack_error ("jack_client_process_thread :resume error");
  1134. goto zombie;
  1135. }
  1136. control->awake_at = jack_get_microseconds();
  1137. DEBUG ("client resumed");
  1138. control->state = Running;
  1139. if (control->sync_cb)
  1140. jack_call_sync_client (client);
  1141. if (control->process) {
  1142. if (control->process (control->nframes,
  1143. control->process_arg) == 0) {
  1144. control->state = Finished;
  1145. }
  1146. } else {
  1147. control->state = Finished;
  1148. }
  1149. if (control->timebase_cb)
  1150. jack_call_timebase_master (client);
  1151. control->finished_at = jack_get_microseconds();
  1152. #ifdef WITH_TIMESTAMPS
  1153. jack_timestamp ("finished");
  1154. #endif
  1155. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1156. control->finished_at,
  1157. ((float)(control->finished_at - control->awake_at)));
  1158. /* check if we were killed during the process cycle
  1159. * (or whatever) */
  1160. if (client->control->dead) {
  1161. jack_error ("jack_client_process_thread: "
  1162. "client->control->dead");
  1163. goto zombie;
  1164. }
  1165. DEBUG("process cycle fully complete\n");
  1166. }
  1167. return (void *) ((intptr_t)err);
  1168. zombie:
  1169. jack_error ("jack_client_process_thread : zombified");
  1170. client->rt_thread_ok = FALSE;
  1171. if (client->on_shutdown) {
  1172. jack_error ("zombified - calling shutdown handler");
  1173. client->on_shutdown (client->on_shutdown_arg);
  1174. } else {
  1175. jack_error ("zombified - exiting from JACK");
  1176. /* Need a fix : possibly make client crash if
  1177. * zombified without shutdown handler */
  1178. jack_client_close (client);
  1179. }
  1180. pthread_exit (0);
  1181. /*NOTREACHED*/
  1182. return 0;
  1183. }
  1184. #endif /* JACK_USE_MACH_THREADS */
  1185. static int
  1186. jack_start_thread (jack_client_t *client)
  1187. {
  1188. if (client->engine->real_time) {
  1189. #ifdef USE_MLOCK
  1190. if (client->engine->do_mlock
  1191. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1192. jack_error ("cannot lock down memory for RT thread "
  1193. "(%s)", strerror (errno));
  1194. #ifdef ENSURE_MLOCK
  1195. return -1;
  1196. #endif /* ENSURE_MLOCK */
  1197. }
  1198. if (client->engine->do_munlock) {
  1199. cleanup_mlock ();
  1200. }
  1201. #endif /* USE_MLOCK */
  1202. }
  1203. #ifdef JACK_USE_MACH_THREADS
  1204. /* Stephane Letz : letz@grame.fr
  1205. On MacOSX, the normal thread does not need to be real-time.
  1206. */
  1207. if (jack_create_thread (&client->thread,
  1208. client->engine->client_priority,
  1209. 0,
  1210. jack_client_thread, client)) {
  1211. return -1;
  1212. }
  1213. #else
  1214. if (jack_create_thread (&client->thread,
  1215. client->engine->client_priority,
  1216. client->engine->real_time,
  1217. jack_client_thread, client)) {
  1218. return -1;
  1219. }
  1220. #endif
  1221. #ifdef JACK_USE_MACH_THREADS
  1222. /* a secondary thread that runs the process callback and uses
  1223. ultra-fast Mach primitives for inter-thread signalling.
  1224. XXX in a properly structured JACK, there would be no
  1225. need for this, because we would have client wake up
  1226. methods that encapsulated the underlying mechanism
  1227. used.
  1228. */
  1229. if (jack_create_thread(&client->process_thread,
  1230. client->engine->client_priority,
  1231. client->engine->real_time,
  1232. jack_client_process_thread, client)) {
  1233. return -1;
  1234. }
  1235. #endif /* JACK_USE_MACH_THREADS */
  1236. return 0;
  1237. }
  1238. int
  1239. jack_activate (jack_client_t *client)
  1240. {
  1241. jack_request_t req;
  1242. /* we need to scribble on our stack to ensure that its memory
  1243. * pages are actually mapped (more important for mlockall(2)
  1244. * usage in jack_start_thread())
  1245. */
  1246. char buf[JACK_THREAD_STACK_TOUCH];
  1247. int i;
  1248. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1249. buf[i] = (char) (i & 0xff);
  1250. }
  1251. if (client->control->type == ClientInternal ||
  1252. client->control->type == ClientDriver) {
  1253. goto startit;
  1254. }
  1255. /* get the pid of the client process to pass it to engine */
  1256. client->control->pid = getpid ();
  1257. #ifdef USE_CAPABILITIES
  1258. if (client->engine->has_capabilities != 0 &&
  1259. client->control->pid != 0 && client->engine->real_time != 0) {
  1260. /* we need to ask the engine for realtime capabilities
  1261. before trying to start the realtime thread
  1262. */
  1263. req.type = SetClientCapabilities;
  1264. req.x.client_id = client->control->id;
  1265. jack_client_deliver_request (client, &req);
  1266. if (req.status) {
  1267. /* what to do? engine is running realtime, it
  1268. is using capabilities and has them
  1269. (otherwise we would not get an error
  1270. return) but for some reason it could not
  1271. give the client the required capabilities,
  1272. so for now downgrade the client so that it
  1273. still runs, albeit non-realtime - nando
  1274. */
  1275. jack_error ("could not receive realtime capabilities, "
  1276. "client will run non-realtime");
  1277. /* XXX wrong, this is a property of the engine
  1278. client->engine->real_time = 0;
  1279. */
  1280. }
  1281. }
  1282. #endif /* USE_CAPABILITIES */
  1283. if (client->first_active) {
  1284. pthread_mutex_init (&client_lock, NULL);
  1285. pthread_cond_init (&client_ready, NULL);
  1286. pthread_mutex_lock (&client_lock);
  1287. if (jack_start_thread (client)) {
  1288. pthread_mutex_unlock (&client_lock);
  1289. return -1;
  1290. }
  1291. pthread_cond_wait (&client_ready, &client_lock);
  1292. pthread_mutex_unlock (&client_lock);
  1293. if (!client->thread_ok) {
  1294. jack_error ("could not start client thread");
  1295. return -1;
  1296. }
  1297. client->first_active = FALSE;
  1298. }
  1299. startit:
  1300. req.type = ActivateClient;
  1301. req.x.client_id = client->control->id;
  1302. return jack_client_deliver_request (client, &req);
  1303. }
  1304. int
  1305. jack_deactivate (jack_client_t *client)
  1306. {
  1307. jack_request_t req;
  1308. req.type = DeactivateClient;
  1309. req.x.client_id = client->control->id;
  1310. return jack_client_deliver_request (client, &req);
  1311. }
  1312. int
  1313. jack_client_close (jack_client_t *client)
  1314. {
  1315. JSList *node;
  1316. void *status;
  1317. if (client->control->active) {
  1318. jack_deactivate (client);
  1319. }
  1320. if (client->control->type == ClientExternal) {
  1321. #if JACK_USE_MACH_THREADS
  1322. if (client->rt_thread_ok) {
  1323. // MacOSX pthread_cancel not implemented in
  1324. // Darwin 5.5, 6.4
  1325. mach_port_t machThread =
  1326. pthread_mach_thread_np (client->process_thread);
  1327. thread_terminate (machThread);
  1328. }
  1329. #endif
  1330. /* stop the thread that communicates with the jack
  1331. * server, only if it was actually running
  1332. */
  1333. if (client->thread_ok){
  1334. pthread_cancel (client->thread);
  1335. pthread_join (client->thread, &status);
  1336. }
  1337. if (client->control) {
  1338. jack_release_shm (&client->control_shm);
  1339. client->control = NULL;
  1340. }
  1341. if (client->engine) {
  1342. jack_release_shm (&client->engine_shm);
  1343. client->engine = NULL;
  1344. }
  1345. if (client->port_segment) {
  1346. jack_port_type_id_t ptid;
  1347. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1348. jack_release_shm (&client->port_segment[ptid]);
  1349. }
  1350. free (client->port_segment);
  1351. client->port_segment = NULL;
  1352. }
  1353. if (client->graph_wait_fd) {
  1354. close (client->graph_wait_fd);
  1355. }
  1356. if (client->graph_next_fd) {
  1357. close (client->graph_next_fd);
  1358. }
  1359. close (client->event_fd);
  1360. close (client->request_fd);
  1361. }
  1362. for (node = client->ports; node; node = jack_slist_next (node)) {
  1363. free (node->data);
  1364. }
  1365. jack_slist_free (client->ports);
  1366. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1367. free (node->data);
  1368. }
  1369. jack_slist_free (client->ports_ext);
  1370. jack_client_free (client);
  1371. return 0;
  1372. }
  1373. int
  1374. jack_is_realtime (jack_client_t *client)
  1375. {
  1376. return client->engine->real_time;
  1377. }
  1378. jack_nframes_t
  1379. jack_get_buffer_size (jack_client_t *client)
  1380. {
  1381. return client->engine->buffer_size;
  1382. }
  1383. int
  1384. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1385. {
  1386. #ifdef DO_BUFFER_RESIZE
  1387. jack_request_t req;
  1388. req.type = SetBufferSize;
  1389. req.x.nframes = nframes;
  1390. return jack_client_deliver_request (client, &req);
  1391. #else
  1392. return ENOSYS;
  1393. #endif /* DO_BUFFER_RESIZE */
  1394. }
  1395. int
  1396. jack_connect (jack_client_t *client, const char *source_port,
  1397. const char *destination_port)
  1398. {
  1399. jack_request_t req;
  1400. req.type = ConnectPorts;
  1401. snprintf (req.x.connect.source_port,
  1402. sizeof (req.x.connect.source_port), "%s", source_port);
  1403. snprintf (req.x.connect.destination_port,
  1404. sizeof (req.x.connect.destination_port),
  1405. "%s", destination_port);
  1406. return jack_client_deliver_request (client, &req);
  1407. }
  1408. int
  1409. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1410. {
  1411. jack_request_t req;
  1412. pthread_mutex_lock (&port->connection_lock);
  1413. if (port->connections == NULL) {
  1414. pthread_mutex_unlock (&port->connection_lock);
  1415. return 0;
  1416. }
  1417. pthread_mutex_unlock (&port->connection_lock);
  1418. req.type = DisconnectPort;
  1419. req.x.port_info.port_id = port->shared->id;
  1420. return jack_client_deliver_request (client, &req);
  1421. }
  1422. int
  1423. jack_disconnect (jack_client_t *client, const char *source_port,
  1424. const char *destination_port)
  1425. {
  1426. jack_request_t req;
  1427. req.type = DisconnectPorts;
  1428. snprintf (req.x.connect.source_port,
  1429. sizeof (req.x.connect.source_port), "%s", source_port);
  1430. snprintf (req.x.connect.destination_port,
  1431. sizeof (req.x.connect.destination_port),
  1432. "%s", destination_port);
  1433. return jack_client_deliver_request (client, &req);
  1434. }
  1435. void
  1436. jack_set_error_function (void (*func) (const char *))
  1437. {
  1438. jack_error_callback = func;
  1439. }
  1440. int
  1441. jack_set_graph_order_callback (jack_client_t *client,
  1442. JackGraphOrderCallback callback, void *arg)
  1443. {
  1444. if (client->control->active) {
  1445. jack_error ("You cannot set callbacks on an active client.");
  1446. return -1;
  1447. }
  1448. client->control->graph_order = callback;
  1449. client->control->graph_order_arg = arg;
  1450. return 0;
  1451. }
  1452. int jack_set_xrun_callback (jack_client_t *client,
  1453. JackXRunCallback callback, void *arg)
  1454. {
  1455. if (client->control->active) {
  1456. jack_error ("You cannot set callbacks on an active client.");
  1457. return -1;
  1458. }
  1459. client->control->xrun = callback;
  1460. client->control->xrun_arg = arg;
  1461. return 0;
  1462. }
  1463. int
  1464. jack_set_process_callback (jack_client_t *client,
  1465. JackProcessCallback callback, void *arg)
  1466. {
  1467. if (client->control->active) {
  1468. jack_error ("You cannot set callbacks on an active client.");
  1469. return -1;
  1470. }
  1471. client->control->process_arg = arg;
  1472. client->control->process = callback;
  1473. return 0;
  1474. }
  1475. int
  1476. jack_set_thread_init_callback (jack_client_t *client,
  1477. JackThreadInitCallback callback, void *arg)
  1478. {
  1479. if (client->control->active) {
  1480. jack_error ("You cannot set callbacks on an active client.");
  1481. return -1;
  1482. }
  1483. client->control->thread_init_arg = arg;
  1484. client->control->thread_init = callback;
  1485. return 0;
  1486. }
  1487. int
  1488. jack_set_freewheel_callback (jack_client_t *client,
  1489. JackFreewheelCallback callback, void *arg)
  1490. {
  1491. if (client->control->active) {
  1492. jack_error ("You cannot set callbacks on an active client.");
  1493. return -1;
  1494. }
  1495. client->control->freewheel_arg = arg;
  1496. client->control->freewheel_cb = callback;
  1497. return 0;
  1498. }
  1499. int
  1500. jack_set_buffer_size_callback (jack_client_t *client,
  1501. JackBufferSizeCallback callback, void *arg)
  1502. {
  1503. client->control->bufsize_arg = arg;
  1504. client->control->bufsize = callback;
  1505. return 0;
  1506. }
  1507. int
  1508. jack_set_port_registration_callback(jack_client_t *client,
  1509. JackPortRegistrationCallback callback,
  1510. void *arg)
  1511. {
  1512. if (client->control->active) {
  1513. jack_error ("You cannot set callbacks on an active client.");
  1514. return -1;
  1515. }
  1516. client->control->port_register_arg = arg;
  1517. client->control->port_register = callback;
  1518. return 0;
  1519. }
  1520. int
  1521. jack_get_process_done_fd (jack_client_t *client)
  1522. {
  1523. return client->graph_next_fd;
  1524. }
  1525. void
  1526. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1527. {
  1528. client->on_shutdown = function;
  1529. client->on_shutdown_arg = arg;
  1530. }
  1531. const char **
  1532. jack_get_ports (jack_client_t *client,
  1533. const char *port_name_pattern,
  1534. const char *type_name_pattern,
  1535. unsigned long flags)
  1536. {
  1537. jack_control_t *engine;
  1538. const char **matching_ports;
  1539. unsigned long match_cnt;
  1540. jack_port_shared_t *psp;
  1541. unsigned long i;
  1542. regex_t port_regex;
  1543. regex_t type_regex;
  1544. int matching;
  1545. engine = client->engine;
  1546. if (port_name_pattern && port_name_pattern[0]) {
  1547. regcomp (&port_regex, port_name_pattern,
  1548. REG_EXTENDED|REG_NOSUB);
  1549. }
  1550. if (type_name_pattern && type_name_pattern[0]) {
  1551. regcomp (&type_regex, type_name_pattern,
  1552. REG_EXTENDED|REG_NOSUB);
  1553. }
  1554. psp = engine->ports;
  1555. match_cnt = 0;
  1556. matching_ports = (const char **)
  1557. malloc (sizeof (char *) * engine->port_max);
  1558. for (i = 0; i < engine->port_max; i++) {
  1559. matching = 1;
  1560. if (!psp[i].in_use) {
  1561. continue;
  1562. }
  1563. if (flags) {
  1564. if ((psp[i].flags & flags) != flags) {
  1565. matching = 0;
  1566. }
  1567. }
  1568. if (matching && port_name_pattern && port_name_pattern[0]) {
  1569. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1570. matching = 0;
  1571. }
  1572. }
  1573. if (matching && type_name_pattern && type_name_pattern[0]) {
  1574. jack_port_type_id_t ptid = psp[i].ptype_id;
  1575. if (regexec (&type_regex,
  1576. engine->port_types[ptid].type_name,
  1577. 0, NULL, 0)) {
  1578. matching = 0;
  1579. }
  1580. }
  1581. if (matching) {
  1582. matching_ports[match_cnt++] = psp[i].name;
  1583. }
  1584. }
  1585. if (port_name_pattern && port_name_pattern[0]) {
  1586. regfree (&port_regex);
  1587. }
  1588. if (type_name_pattern && type_name_pattern[0]) {
  1589. regfree (&type_regex);
  1590. }
  1591. matching_ports[match_cnt] = 0;
  1592. if (match_cnt == 0) {
  1593. free (matching_ports);
  1594. matching_ports = 0;
  1595. }
  1596. return matching_ports;
  1597. }
  1598. float
  1599. jack_cpu_load (jack_client_t *client)
  1600. {
  1601. return client->engine->cpu_load;
  1602. }
  1603. float
  1604. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1605. {
  1606. return client->engine->xrun_delayed_usecs;
  1607. }
  1608. pthread_t
  1609. jack_client_thread_id (jack_client_t *client)
  1610. {
  1611. return client->thread_id;
  1612. }
  1613. int
  1614. jack_client_name_size(void)
  1615. {
  1616. return JACK_CLIENT_NAME_SIZE;
  1617. }
  1618. int
  1619. jack_port_name_size(void)
  1620. {
  1621. return JACK_PORT_NAME_SIZE;
  1622. }
  1623. int
  1624. jack_port_type_size(void)
  1625. {
  1626. return JACK_PORT_TYPE_SIZE;
  1627. }