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.

2031 lines
47KB

  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 (const char *server_name)
  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. #if defined(USE_CAPABILITIES)
  374. command = JACK_LOCATION "/jackstart";
  375. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  376. JACK_DEFAULT_DRIVER " -p 512", 255);
  377. #else /* !USE_CAPABILITIES */
  378. command = JACK_LOCATION "/jackd";
  379. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  380. JACK_DEFAULT_DRIVER, 255);
  381. #endif /* USE_CAPABILITIES */
  382. } else {
  383. result = strcspn(arguments, " ");
  384. command = (char *) malloc(result+1);
  385. strncpy(command, arguments, result);
  386. command[result] = '\0';
  387. }
  388. argv = (char **) malloc (255);
  389. while(1) {
  390. /* insert -T and -nserver_name in front of arguments */
  391. if (i == 1) {
  392. argv[i] = (char *) malloc(strlen ("-T") + 1);
  393. strcpy (argv[i++], "-T");
  394. if (server_name) {
  395. size_t optlen = strlen ("-n");
  396. char *buf =
  397. malloc (optlen
  398. + strlen (server_name) + 1);
  399. strcpy (buf, "-n");
  400. strcpy (buf+optlen, server_name);
  401. argv[i++] = buf;
  402. }
  403. }
  404. result = strcspn(arguments+pos, " ");
  405. if (result == 0) {
  406. break;
  407. }
  408. argv[i] = (char*)malloc(result+1);
  409. strncpy(argv[i], arguments+pos, result);
  410. argv[i][result] = '\0';
  411. pos += result+1;
  412. ++i;
  413. }
  414. argv[i] = 0;
  415. execv (command, argv);
  416. /* If execv() succeeds, it does not return. There's no point
  417. * in calling jack_error() here in the child process. */
  418. perror ("exec of JACK server failed");
  419. }
  420. int
  421. start_server (const char *server_name, jack_options_t options)
  422. {
  423. if ((options & JackNoStartServer)
  424. || getenv("JACK_NO_START_SERVER")) {
  425. return 1;
  426. }
  427. /* The double fork() forces the server to become a child of
  428. * init, which will always clean up zombie process state on
  429. * termination. This even works in cases where the server
  430. * terminates but this client does not.
  431. *
  432. * Since fork() is usually implemented using copy-on-write
  433. * virtual memory tricks, the overhead of the second fork() is
  434. * probably relatively small.
  435. */
  436. switch (fork()) {
  437. case 0: /* child process */
  438. switch (fork()) {
  439. case 0: /* grandchild process */
  440. _start_server(server_name);
  441. _exit (99); /* exec failed */
  442. case -1:
  443. _exit (98);
  444. default:
  445. _exit (0);
  446. }
  447. case -1: /* fork() error */
  448. return 1; /* failed to start server */
  449. }
  450. /* only the original parent process goes here */
  451. return 0; /* (probably) successful */
  452. }
  453. static int
  454. jack_request_client (ClientType type,
  455. const char* client_name, jack_options_t options,
  456. jack_status_t *status, jack_varargs_t *va,
  457. jack_client_connect_result_t *res, int *req_fd)
  458. {
  459. jack_client_connect_request_t req;
  460. *req_fd = -1;
  461. memset (&req, 0, sizeof (req));
  462. req.options = options;
  463. if (strlen (client_name) >= sizeof (req.name)) {
  464. jack_error ("\"%s\" is too long to be used as a JACK client"
  465. " name.\n"
  466. "Please use %lu characters or less.",
  467. client_name, sizeof (req.name));
  468. return -1;
  469. }
  470. if (va->load_name
  471. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  472. jack_error ("\"%s\" is too long to be used as a JACK shared"
  473. " object name.\n"
  474. "Please use %lu characters or less.",
  475. va->load_name, sizeof (req.object_path) - 1);
  476. return -1;
  477. }
  478. if (va->load_init
  479. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  480. jack_error ("\"%s\" is too long to be used as a JACK shared"
  481. " object data string.\n"
  482. "Please use %lu characters or less.",
  483. va->load_init, sizeof (req.object_data) - 1);
  484. return -1;
  485. }
  486. if ((*req_fd = server_connect (va->server_name)) < 0) {
  487. int trys;
  488. if (start_server(va->server_name, options)) {
  489. *status |= (JackFailure|JackServerFailed);
  490. goto fail;
  491. }
  492. trys = 5;
  493. do {
  494. sleep(1);
  495. if (--trys < 0) {
  496. *status |= (JackFailure|JackServerFailed);
  497. goto fail;
  498. }
  499. } while ((*req_fd = server_connect (va->server_name)) < 0);
  500. *status |= JackServerStarted;
  501. }
  502. /* format connection request */
  503. req.protocol_v = jack_protocol_version;
  504. req.load = TRUE;
  505. req.type = type;
  506. snprintf (req.name, sizeof (req.name),
  507. "%s", client_name);
  508. snprintf (req.object_path, sizeof (req.object_path),
  509. "%s", va->load_name);
  510. snprintf (req.object_data, sizeof (req.object_data),
  511. "%s", va->load_init);
  512. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  513. jack_error ("cannot send request to jack server (%s)",
  514. strerror (errno));
  515. *status |= (JackFailure|JackServerError);
  516. goto fail;
  517. }
  518. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  519. if (errno == 0) {
  520. /* server shut the socket */
  521. jack_error ("could not attach as client");
  522. *status |= (JackFailure|JackServerError);
  523. goto fail;
  524. }
  525. if (errno == ECONNRESET) {
  526. jack_error ("could not attach as JACK client "
  527. "(server has exited)");
  528. *status |= (JackFailure|JackServerError);
  529. goto fail;
  530. }
  531. jack_error ("cannot read response from jack server (%s)",
  532. strerror (errno));
  533. *status |= (JackFailure|JackServerError);
  534. goto fail;
  535. }
  536. *status |= res->status; /* return server status bits */
  537. if (*status & JackFailure) {
  538. if (*status & JackVersionError) {
  539. jack_error ("client linked with incompatible libjack"
  540. " version.");
  541. }
  542. jack_error ("could not attach to JACK server");
  543. *status |= JackServerError;
  544. goto fail;
  545. }
  546. switch (type) {
  547. case ClientDriver:
  548. case ClientInternal:
  549. close (*req_fd);
  550. *req_fd = -1;
  551. break;
  552. default:
  553. break;
  554. }
  555. return 0;
  556. fail:
  557. if (*req_fd >= 0) {
  558. close (*req_fd);
  559. *req_fd = -1;
  560. }
  561. return -1;
  562. }
  563. int
  564. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  565. {
  566. /* Lookup, attach and register the port/buffer segments in use
  567. * right now.
  568. */
  569. if (client->control->type != ClientExternal) {
  570. jack_error("Only external clients need attach port segments");
  571. abort();
  572. }
  573. /* make sure we have space to store the port
  574. segment information.
  575. */
  576. if (ptid >= client->n_port_types) {
  577. client->port_segment = (jack_shm_info_t*)
  578. realloc (client->port_segment,
  579. sizeof (jack_shm_info_t) * (ptid+1));
  580. memset (&client->port_segment[client->n_port_types],
  581. 0,
  582. sizeof (jack_shm_info_t) *
  583. (ptid - client->n_port_types));
  584. client->n_port_types = ptid + 1;
  585. } else {
  586. /* release any previous segment */
  587. jack_release_shm (&client->port_segment[ptid]);
  588. }
  589. /* get the index into the shm registry */
  590. client->port_segment[ptid].index =
  591. client->engine->port_types[ptid].shm_registry_index;
  592. /* attach the relevant segment */
  593. if (jack_attach_shm (&client->port_segment[ptid])) {
  594. jack_error ("cannot attach port segment shared memory"
  595. " (%s)", strerror (errno));
  596. return -1;
  597. }
  598. /* The first chunk of the audio port segment will be set by
  599. * the engine to be a zero-filled buffer. This hasn't been
  600. * done yet, but it will happen before the process cycle
  601. * (re)starts.
  602. */
  603. if (ptid == JACK_AUDIO_PORT_TYPE) {
  604. jack_zero_filled_buffer =
  605. jack_shm_addr (&client->port_segment[ptid]);
  606. }
  607. return 0;
  608. }
  609. jack_client_t *
  610. jack_client_open (const char *client_name,
  611. jack_options_t options,
  612. jack_status_t *status, ...)
  613. {
  614. /* optional arguments: */
  615. va_list ap; /* variable argument pointer */
  616. jack_varargs_t va; /* variable arguments */
  617. int req_fd = -1;
  618. int ev_fd = -1;
  619. jack_client_connect_result_t res;
  620. jack_client_t *client;
  621. jack_port_type_id_t ptid;
  622. jack_status_t my_status;
  623. if (status == NULL) /* no status from caller? */
  624. status = &my_status; /* use local status word */
  625. *status = 0;
  626. /* validate parameters */
  627. if ((options & ~JackOpenOptions)) {
  628. *status |= (JackFailure|JackInvalidOption);
  629. return NULL;
  630. }
  631. /* parse variable arguments */
  632. va_start (ap, status);
  633. jack_varargs_parse (options, ap, &va);
  634. va_end (ap);
  635. /* External clients need this initialized. It is already set
  636. * up in the server's address space for internal clients.
  637. */
  638. jack_init_time ();
  639. if (jack_request_client (ClientExternal, client_name, options, status,
  640. &va, &res, &req_fd)) {
  641. return NULL;
  642. }
  643. /* Allocate the jack_client_t structure in local memory.
  644. * Shared memory is not accessible yet. */
  645. client = jack_client_alloc ();
  646. strcpy (client->name, res.name);
  647. strcpy (client->fifo_prefix, res.fifo_prefix);
  648. client->request_fd = req_fd;
  649. client->pollfd[EVENT_POLL_INDEX].events =
  650. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  651. client->pollfd[WAIT_POLL_INDEX].events =
  652. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  653. /* Don't access shared memory until server connected. */
  654. if (jack_initialize_shm (va.server_name)) {
  655. jack_error ("Unable to initialize shared memory.");
  656. *status |= (JackFailure|JackShmFailure);
  657. goto fail;
  658. }
  659. /* attach the engine control/info block */
  660. client->engine_shm = res.engine_shm;
  661. if (jack_attach_shm (&client->engine_shm)) {
  662. jack_error ("cannot attached engine control shared memory"
  663. " segment");
  664. goto fail;
  665. }
  666. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  667. /* now attach the client control block */
  668. client->control_shm = res.client_shm;
  669. if (jack_attach_shm (&client->control_shm)) {
  670. jack_error ("cannot attached client control shared memory"
  671. " segment");
  672. goto fail;
  673. }
  674. client->control = (jack_client_control_t *)
  675. jack_shm_addr (&client->control_shm);
  676. /* Nobody else needs to access this shared memory any more, so
  677. * destroy it. Because we have it attached, it won't vanish
  678. * till we exit (and release it).
  679. */
  680. jack_destroy_shm (&client->control_shm);
  681. client->n_port_types = client->engine->n_port_types;
  682. client->port_segment = (jack_shm_info_t *)
  683. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  684. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  685. client->port_segment[ptid].index =
  686. client->engine->port_types[ptid].shm_registry_index;
  687. client->port_segment[ptid].attached_at = MAP_FAILED;
  688. jack_attach_port_segment (client, ptid);
  689. }
  690. /* set up the client so that it does the right thing for an
  691. * external client
  692. */
  693. client->control->deliver_request = oop_client_deliver_request;
  694. client->control->deliver_arg = client;
  695. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  696. goto fail;
  697. }
  698. client->event_fd = ev_fd;
  699. #ifdef JACK_USE_MACH_THREADS
  700. /* specific resources for server/client real-time thread
  701. * communication */
  702. client->clienttask = mach_task_self();
  703. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  704. jack_error ("Can't find bootstrap port");
  705. goto fail;
  706. }
  707. if (allocate_mach_clientport(client, res.portnum) < 0) {
  708. jack_error("Can't allocate mach port");
  709. goto fail;
  710. };
  711. #endif /* JACK_USE_MACH_THREADS */
  712. return client;
  713. fail:
  714. if (client->engine) {
  715. jack_release_shm (&client->engine_shm);
  716. client->engine = 0;
  717. }
  718. if (client->control) {
  719. jack_release_shm (&client->control_shm);
  720. client->control = 0;
  721. }
  722. if (req_fd >= 0) {
  723. close (req_fd);
  724. }
  725. if (ev_fd >= 0) {
  726. close (ev_fd);
  727. }
  728. free (client);
  729. return NULL;
  730. }
  731. jack_client_t *
  732. jack_client_new (const char *client_name)
  733. {
  734. jack_options_t options = JackUseExactName;
  735. if (getenv("JACK_START_SERVER") == NULL)
  736. options |= JackNoStartServer;
  737. return jack_client_open (client_name, options, NULL);
  738. }
  739. char *
  740. jack_get_client_name (jack_client_t *client)
  741. {
  742. return client->name;
  743. }
  744. int
  745. jack_internal_client_new (const char *client_name,
  746. const char *so_name, const char *so_data)
  747. {
  748. jack_client_connect_result_t res;
  749. int req_fd;
  750. jack_varargs_t va;
  751. jack_status_t status;
  752. jack_options_t options = JackUseExactName;
  753. if (getenv("JACK_START_SERVER") == NULL)
  754. options |= JackNoStartServer;
  755. jack_varargs_init (&va);
  756. va.load_name = (char *) so_name;
  757. va.load_init = (char *) so_data;
  758. return jack_request_client (ClientInternal, client_name,
  759. options, &status, &va, &res, &req_fd);
  760. }
  761. char *
  762. jack_default_server_name (void)
  763. {
  764. char *server_name;
  765. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  766. server_name = "default";
  767. return server_name;
  768. }
  769. char *jack_tmpdir = DEFAULT_TMP_DIR;
  770. /* returns the name of the per-user subdirectory of jack_tmpdir */
  771. char *
  772. jack_user_dir (void)
  773. {
  774. static char user_dir[PATH_MAX] = "";
  775. /* format the path name on the first call */
  776. if (user_dir[0] == '\0') {
  777. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  778. jack_tmpdir, getuid ());
  779. }
  780. return user_dir;
  781. }
  782. /* returns the name of the per-server subdirectory of jack_user_dir() */
  783. char *
  784. jack_server_dir (const char *server_name)
  785. {
  786. static char server_dir[PATH_MAX] = "";
  787. /* format the path name on the first call */
  788. if (server_dir[0] == '\0') {
  789. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  790. jack_user_dir (), server_name);
  791. }
  792. return server_dir;
  793. }
  794. void
  795. jack_internal_client_close (const char *client_name)
  796. {
  797. jack_client_connect_request_t req;
  798. int fd;
  799. char *server_name = jack_default_server_name ();
  800. req.load = FALSE;
  801. snprintf (req.name, sizeof (req.name), "%s", client_name);
  802. if ((fd = server_connect (server_name)) < 0) {
  803. return;
  804. }
  805. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  806. jack_error ("cannot deliver ClientUnload request to JACK "
  807. "server.");
  808. }
  809. /* no response to this request */
  810. close (fd);
  811. return;
  812. }
  813. int
  814. jack_set_freewheel (jack_client_t* client, int onoff)
  815. {
  816. jack_request_t request;
  817. request.type = onoff ? FreeWheel : StopFreeWheel;
  818. return jack_client_deliver_request (client, &request);
  819. }
  820. void
  821. jack_start_freewheel (jack_client_t* client)
  822. {
  823. jack_client_control_t *control = client->control;
  824. if (client->engine->real_time) {
  825. #if JACK_USE_MACH_THREADS
  826. jack_drop_real_time_scheduling (client->process_thread);
  827. #else
  828. jack_drop_real_time_scheduling (client->thread);
  829. #endif
  830. }
  831. if (control->freewheel_cb) {
  832. control->freewheel_cb (1, control->freewheel_arg);
  833. }
  834. }
  835. void
  836. jack_stop_freewheel (jack_client_t* client)
  837. {
  838. jack_client_control_t *control = client->control;
  839. if (control->freewheel_cb) {
  840. control->freewheel_cb (0, control->freewheel_arg);
  841. }
  842. if (client->engine->real_time) {
  843. #if JACK_USE_MACH_THREADS
  844. jack_acquire_real_time_scheduling (
  845. client->process_thread,
  846. client->engine->client_priority);
  847. #else
  848. jack_acquire_real_time_scheduling (
  849. client->thread,
  850. client->engine->client_priority);
  851. #endif
  852. }
  853. }
  854. static void *
  855. jack_client_thread (void *arg)
  856. {
  857. jack_client_t *client = (jack_client_t *) arg;
  858. jack_client_control_t *control = client->control;
  859. jack_event_t event;
  860. char status = 0;
  861. char c = 0;
  862. int err = 0;
  863. pthread_mutex_lock (&client_lock);
  864. client->thread_ok = TRUE;
  865. client->thread_id = pthread_self();
  866. pthread_cond_signal (&client_ready);
  867. pthread_mutex_unlock (&client_lock);
  868. client->control->pid = getpid();
  869. client->control->pgrp = getpgrp();
  870. DEBUG ("client thread is now running");
  871. if (client->control->thread_init) {
  872. DEBUG ("calling client thread init callback");
  873. client->control->thread_init (client->control->thread_init_arg);
  874. }
  875. while (err == 0) {
  876. if (client->engine->engine_ok == 0) {
  877. if (client->on_shutdown)
  878. client->on_shutdown (client->on_shutdown_arg);
  879. else
  880. jack_error ("engine unexpectedly shutdown; "
  881. "thread exiting\n");
  882. pthread_exit (0);
  883. }
  884. DEBUG ("client polling on %s", client->pollmax == 2 ?
  885. "event_fd and graph_wait_fd..." :
  886. "event_fd only");
  887. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  888. if (errno == EINTR) {
  889. continue;
  890. }
  891. jack_error ("poll failed in client (%s)",
  892. strerror (errno));
  893. status = -1;
  894. break;
  895. }
  896. /* get an accurate timestamp on waking from poll for a
  897. * process() cycle.
  898. */
  899. if (client->graph_wait_fd >= 0
  900. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  901. control->awake_at = jack_get_microseconds();
  902. }
  903. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  904. client->pollfd[EVENT_POLL_INDEX].revents,
  905. client->pollfd[WAIT_POLL_INDEX].revents);
  906. pthread_testcancel();
  907. if (client->graph_wait_fd >= 0 &&
  908. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  909. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR,"
  910. " ZOMBIE\n\n\n\n\n");
  911. /* our upstream "wait" connection
  912. closed, which either means that
  913. an intermediate client exited, or
  914. jackd exited, or jackd zombified
  915. us.
  916. we can discover the zombification
  917. via client->control->dead, but
  918. the other two possibilities are
  919. impossible to identify just from
  920. this situation. so we have to
  921. check what we are connected to,
  922. and act accordingly.
  923. */
  924. if (client->upstream_is_jackd) {
  925. DEBUG ("WE DIE\n");
  926. goto zombie;
  927. } else {
  928. DEBUG ("WE PUNT\n");
  929. /* don't poll on the wait fd
  930. * again until we get a
  931. * GraphReordered event.
  932. */
  933. client->graph_wait_fd = -1;
  934. client->pollmax = 1;
  935. }
  936. }
  937. if (client->control->dead) {
  938. goto zombie;
  939. }
  940. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  941. /* jackd shutdown */
  942. goto zombie;
  943. }
  944. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  945. DEBUG ("client receives an event, "
  946. "now reading on event fd");
  947. /* server has sent us an event. process the
  948. * event and reply */
  949. if (read (client->event_fd, &event, sizeof (event))
  950. != sizeof (event)) {
  951. jack_error ("cannot read server event (%s)",
  952. strerror (errno));
  953. err++;
  954. break;
  955. }
  956. status = 0;
  957. switch (event.type) {
  958. case PortRegistered:
  959. if (control->port_register) {
  960. control->port_register
  961. (event.x.port_id, TRUE,
  962. control->port_register_arg);
  963. }
  964. break;
  965. case PortUnregistered:
  966. if (control->port_register) {
  967. control->port_register
  968. (event.x.port_id, FALSE,
  969. control->port_register_arg);
  970. }
  971. break;
  972. case GraphReordered:
  973. status = jack_handle_reorder (client, &event);
  974. break;
  975. case PortConnected:
  976. case PortDisconnected:
  977. status = jack_client_handle_port_connection
  978. (client, &event);
  979. break;
  980. case BufferSizeChange:
  981. jack_client_invalidate_port_buffers (client);
  982. if (control->bufsize) {
  983. status = control->bufsize
  984. (control->nframes,
  985. control->bufsize_arg);
  986. }
  987. break;
  988. case SampleRateChange:
  989. if (control->srate) {
  990. status = control->srate
  991. (control->nframes,
  992. control->srate_arg);
  993. }
  994. break;
  995. case XRun:
  996. if (control->xrun) {
  997. status = control->xrun
  998. (control->xrun_arg);
  999. }
  1000. break;
  1001. case AttachPortSegment:
  1002. jack_attach_port_segment (client, event.y.ptid);
  1003. break;
  1004. case StartFreewheel:
  1005. jack_start_freewheel (client);
  1006. break;
  1007. case StopFreewheel:
  1008. jack_stop_freewheel (client);
  1009. break;
  1010. }
  1011. DEBUG ("client has dealt with the event, writing "
  1012. "response on event fd");
  1013. if (write (client->event_fd, &status, sizeof (status))
  1014. != sizeof (status)) {
  1015. jack_error ("cannot send event response to "
  1016. "engine (%s)", strerror (errno));
  1017. err++;
  1018. break;
  1019. }
  1020. }
  1021. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1022. #ifdef WITH_TIMESTAMPS
  1023. jack_reset_timestamps ();
  1024. #endif
  1025. DEBUG ("client %d signalled at %" PRIu64
  1026. ", awake for process at %" PRIu64
  1027. " (delay = %" PRIu64
  1028. " usecs) (wakeup on graph_wait_fd==%d)",
  1029. getpid(),
  1030. control->signalled_at,
  1031. control->awake_at,
  1032. control->awake_at - control->signalled_at,
  1033. client->pollfd[WAIT_POLL_INDEX].fd);
  1034. control->state = Running;
  1035. /* begin preemption checking */
  1036. CHECK_PREEMPTION (client->engine, TRUE);
  1037. if (control->sync_cb)
  1038. jack_call_sync_client (client);
  1039. if (control->process) {
  1040. if (control->process (control->nframes,
  1041. control->process_arg)
  1042. == 0) {
  1043. control->state = Finished;
  1044. }
  1045. } else {
  1046. control->state = Finished;
  1047. }
  1048. if (control->timebase_cb)
  1049. jack_call_timebase_master (client);
  1050. /* end preemption checking */
  1051. CHECK_PREEMPTION (client->engine, FALSE);
  1052. control->finished_at = jack_get_microseconds();
  1053. #ifdef WITH_TIMESTAMPS
  1054. jack_timestamp ("finished");
  1055. #endif
  1056. /* pass the execution token along */
  1057. DEBUG ("client finished processing at %" PRIu64
  1058. " (elapsed = %" PRIu64
  1059. " usecs), writing on graph_next_fd==%d",
  1060. control->finished_at,
  1061. control->finished_at - control->awake_at,
  1062. client->graph_next_fd);
  1063. if (write (client->graph_next_fd, &c, sizeof (c))
  1064. != sizeof (c)) {
  1065. jack_error ("cannot continue execution of the "
  1066. "processing graph (%s)",
  1067. strerror(errno));
  1068. err++;
  1069. break;
  1070. }
  1071. DEBUG ("client sent message to next stage by %" PRIu64
  1072. ", client reading on graph_wait_fd==%d",
  1073. jack_get_microseconds(), client->graph_wait_fd);
  1074. #ifdef WITH_TIMESTAMPS
  1075. jack_timestamp ("read pending byte from wait");
  1076. #endif
  1077. DEBUG("reading cleanup byte from pipe\n");
  1078. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1079. != sizeof (c))) {
  1080. DEBUG ("WARNING: READ FAILED!");
  1081. #if 0
  1082. jack_error ("cannot complete execution of the "
  1083. "processing graph (%s)",
  1084. strerror(errno));
  1085. err++;
  1086. break;
  1087. #endif
  1088. }
  1089. /* check if we were killed during the process
  1090. * cycle (or whatever) */
  1091. if (client->control->dead) {
  1092. goto zombie;
  1093. }
  1094. DEBUG("process cycle fully complete\n");
  1095. #ifdef WITH_TIMESTAMPS
  1096. jack_timestamp ("read done");
  1097. jack_dump_timestamps (stdout);
  1098. #endif
  1099. }
  1100. }
  1101. return (void *) ((intptr_t)err);
  1102. zombie:
  1103. if (client->on_shutdown) {
  1104. jack_error ("zombified - calling shutdown handler");
  1105. client->on_shutdown (client->on_shutdown_arg);
  1106. } else {
  1107. jack_error ("zombified - exiting from JACK");
  1108. jack_client_close (client);
  1109. /* Need a fix : possibly make client crash if
  1110. * zombified without shutdown handler
  1111. */
  1112. }
  1113. pthread_exit (0);
  1114. /*NOTREACHED*/
  1115. return 0;
  1116. }
  1117. #ifdef JACK_USE_MACH_THREADS
  1118. /* real-time thread : separated from the normal client thread, it will
  1119. * communicate with the server using fast mach RPC mechanism */
  1120. static void *
  1121. jack_client_process_thread (void *arg)
  1122. {
  1123. jack_client_t *client = (jack_client_t *) arg;
  1124. jack_client_control_t *control = client->control;
  1125. int err = 0;
  1126. if (client->control->thread_init) {
  1127. /* this means that the init callback will be called twice -taybin*/
  1128. DEBUG ("calling client thread init callback");
  1129. client->control->thread_init (client->control->thread_init_arg);
  1130. }
  1131. client->control->pid = getpid();
  1132. DEBUG ("client process thread is now running");
  1133. client->rt_thread_ok = TRUE;
  1134. while (err == 0) {
  1135. if (jack_client_suspend(client) < 0) {
  1136. jack_error ("jack_client_process_thread :resume error");
  1137. goto zombie;
  1138. }
  1139. control->awake_at = jack_get_microseconds();
  1140. DEBUG ("client resumed");
  1141. control->state = Running;
  1142. if (control->sync_cb)
  1143. jack_call_sync_client (client);
  1144. if (control->process) {
  1145. if (control->process (control->nframes,
  1146. control->process_arg) == 0) {
  1147. control->state = Finished;
  1148. }
  1149. } else {
  1150. control->state = Finished;
  1151. }
  1152. if (control->timebase_cb)
  1153. jack_call_timebase_master (client);
  1154. control->finished_at = jack_get_microseconds();
  1155. #ifdef WITH_TIMESTAMPS
  1156. jack_timestamp ("finished");
  1157. #endif
  1158. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1159. control->finished_at,
  1160. ((float)(control->finished_at - control->awake_at)));
  1161. /* check if we were killed during the process cycle
  1162. * (or whatever) */
  1163. if (client->control->dead) {
  1164. jack_error ("jack_client_process_thread: "
  1165. "client->control->dead");
  1166. goto zombie;
  1167. }
  1168. DEBUG("process cycle fully complete\n");
  1169. }
  1170. return (void *) ((intptr_t)err);
  1171. zombie:
  1172. jack_error ("jack_client_process_thread : zombified");
  1173. client->rt_thread_ok = FALSE;
  1174. if (client->on_shutdown) {
  1175. jack_error ("zombified - calling shutdown handler");
  1176. client->on_shutdown (client->on_shutdown_arg);
  1177. } else {
  1178. jack_error ("zombified - exiting from JACK");
  1179. /* Need a fix : possibly make client crash if
  1180. * zombified without shutdown handler */
  1181. jack_client_close (client);
  1182. }
  1183. pthread_exit (0);
  1184. /*NOTREACHED*/
  1185. return 0;
  1186. }
  1187. #endif /* JACK_USE_MACH_THREADS */
  1188. static int
  1189. jack_start_thread (jack_client_t *client)
  1190. {
  1191. if (client->engine->real_time) {
  1192. #ifdef USE_MLOCK
  1193. if (client->engine->do_mlock
  1194. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1195. jack_error ("cannot lock down memory for RT thread "
  1196. "(%s)", strerror (errno));
  1197. #ifdef ENSURE_MLOCK
  1198. return -1;
  1199. #endif /* ENSURE_MLOCK */
  1200. }
  1201. if (client->engine->do_munlock) {
  1202. cleanup_mlock ();
  1203. }
  1204. #endif /* USE_MLOCK */
  1205. }
  1206. #ifdef JACK_USE_MACH_THREADS
  1207. /* Stephane Letz : letz@grame.fr
  1208. On MacOSX, the normal thread does not need to be real-time.
  1209. */
  1210. if (jack_create_thread (&client->thread,
  1211. client->engine->client_priority,
  1212. 0,
  1213. jack_client_thread, client)) {
  1214. return -1;
  1215. }
  1216. #else
  1217. if (jack_create_thread (&client->thread,
  1218. client->engine->client_priority,
  1219. client->engine->real_time,
  1220. jack_client_thread, client)) {
  1221. return -1;
  1222. }
  1223. #endif
  1224. #ifdef JACK_USE_MACH_THREADS
  1225. /* a secondary thread that runs the process callback and uses
  1226. ultra-fast Mach primitives for inter-thread signalling.
  1227. XXX in a properly structured JACK, there would be no
  1228. need for this, because we would have client wake up
  1229. methods that encapsulated the underlying mechanism
  1230. used.
  1231. */
  1232. if (jack_create_thread(&client->process_thread,
  1233. client->engine->client_priority,
  1234. client->engine->real_time,
  1235. jack_client_process_thread, client)) {
  1236. return -1;
  1237. }
  1238. #endif /* JACK_USE_MACH_THREADS */
  1239. return 0;
  1240. }
  1241. int
  1242. jack_activate (jack_client_t *client)
  1243. {
  1244. jack_request_t req;
  1245. /* we need to scribble on our stack to ensure that its memory
  1246. * pages are actually mapped (more important for mlockall(2)
  1247. * usage in jack_start_thread())
  1248. */
  1249. char buf[JACK_THREAD_STACK_TOUCH];
  1250. int i;
  1251. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1252. buf[i] = (char) (i & 0xff);
  1253. }
  1254. if (client->control->type == ClientInternal ||
  1255. client->control->type == ClientDriver) {
  1256. goto startit;
  1257. }
  1258. /* get the pid of the client process to pass it to engine */
  1259. client->control->pid = getpid ();
  1260. #ifdef USE_CAPABILITIES
  1261. if (client->engine->has_capabilities != 0 &&
  1262. client->control->pid != 0 && client->engine->real_time != 0) {
  1263. /* we need to ask the engine for realtime capabilities
  1264. before trying to start the realtime thread
  1265. */
  1266. req.type = SetClientCapabilities;
  1267. req.x.client_id = client->control->id;
  1268. jack_client_deliver_request (client, &req);
  1269. if (req.status) {
  1270. /* what to do? engine is running realtime, it
  1271. is using capabilities and has them
  1272. (otherwise we would not get an error
  1273. return) but for some reason it could not
  1274. give the client the required capabilities,
  1275. so for now downgrade the client so that it
  1276. still runs, albeit non-realtime - nando
  1277. */
  1278. jack_error ("could not receive realtime capabilities, "
  1279. "client will run non-realtime");
  1280. /* XXX wrong, this is a property of the engine
  1281. client->engine->real_time = 0;
  1282. */
  1283. }
  1284. }
  1285. #endif /* USE_CAPABILITIES */
  1286. if (client->first_active) {
  1287. pthread_mutex_init (&client_lock, NULL);
  1288. pthread_cond_init (&client_ready, NULL);
  1289. pthread_mutex_lock (&client_lock);
  1290. if (jack_start_thread (client)) {
  1291. pthread_mutex_unlock (&client_lock);
  1292. return -1;
  1293. }
  1294. pthread_cond_wait (&client_ready, &client_lock);
  1295. pthread_mutex_unlock (&client_lock);
  1296. if (!client->thread_ok) {
  1297. jack_error ("could not start client thread");
  1298. return -1;
  1299. }
  1300. client->first_active = FALSE;
  1301. }
  1302. startit:
  1303. req.type = ActivateClient;
  1304. req.x.client_id = client->control->id;
  1305. return jack_client_deliver_request (client, &req);
  1306. }
  1307. int
  1308. jack_deactivate (jack_client_t *client)
  1309. {
  1310. jack_request_t req;
  1311. int rc = ESRCH; /* already shut down */
  1312. if (client && client->control) { /* not shut down? */
  1313. rc = 0;
  1314. if (client->control->active) { /* still active? */
  1315. req.type = DeactivateClient;
  1316. req.x.client_id = client->control->id;
  1317. rc = jack_client_deliver_request (client, &req);
  1318. }
  1319. }
  1320. return rc;
  1321. }
  1322. int
  1323. jack_client_close (jack_client_t *client)
  1324. {
  1325. JSList *node;
  1326. void *status;
  1327. int rc;
  1328. rc = jack_deactivate (client);
  1329. if (rc == ESRCH) { /* already shut down? */
  1330. return rc;
  1331. }
  1332. if (client->control->type == ClientExternal) {
  1333. #if JACK_USE_MACH_THREADS
  1334. if (client->rt_thread_ok) {
  1335. // MacOSX pthread_cancel not implemented in
  1336. // Darwin 5.5, 6.4
  1337. mach_port_t machThread =
  1338. pthread_mach_thread_np (client->process_thread);
  1339. thread_terminate (machThread);
  1340. }
  1341. #endif
  1342. /* stop the thread that communicates with the jack
  1343. * server, only if it was actually running
  1344. */
  1345. if (client->thread_ok){
  1346. pthread_cancel (client->thread);
  1347. pthread_join (client->thread, &status);
  1348. }
  1349. if (client->control) {
  1350. jack_release_shm (&client->control_shm);
  1351. client->control = NULL;
  1352. }
  1353. if (client->engine) {
  1354. jack_release_shm (&client->engine_shm);
  1355. client->engine = NULL;
  1356. }
  1357. if (client->port_segment) {
  1358. jack_port_type_id_t ptid;
  1359. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1360. jack_release_shm (&client->port_segment[ptid]);
  1361. }
  1362. free (client->port_segment);
  1363. client->port_segment = NULL;
  1364. }
  1365. if (client->graph_wait_fd) {
  1366. close (client->graph_wait_fd);
  1367. }
  1368. if (client->graph_next_fd) {
  1369. close (client->graph_next_fd);
  1370. }
  1371. close (client->event_fd);
  1372. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1373. jack_error ("could not shutdown client request socket");
  1374. }
  1375. close (client->request_fd);
  1376. }
  1377. for (node = client->ports; node; node = jack_slist_next (node)) {
  1378. free (node->data);
  1379. }
  1380. jack_slist_free (client->ports);
  1381. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1382. free (node->data);
  1383. }
  1384. jack_slist_free (client->ports_ext);
  1385. jack_client_free (client);
  1386. return rc;
  1387. }
  1388. int
  1389. jack_is_realtime (jack_client_t *client)
  1390. {
  1391. return client->engine->real_time;
  1392. }
  1393. jack_nframes_t
  1394. jack_get_buffer_size (jack_client_t *client)
  1395. {
  1396. return client->engine->buffer_size;
  1397. }
  1398. int
  1399. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1400. {
  1401. #ifdef DO_BUFFER_RESIZE
  1402. jack_request_t req;
  1403. req.type = SetBufferSize;
  1404. req.x.nframes = nframes;
  1405. return jack_client_deliver_request (client, &req);
  1406. #else
  1407. return ENOSYS;
  1408. #endif /* DO_BUFFER_RESIZE */
  1409. }
  1410. int
  1411. jack_connect (jack_client_t *client, const char *source_port,
  1412. const char *destination_port)
  1413. {
  1414. jack_request_t req;
  1415. req.type = ConnectPorts;
  1416. snprintf (req.x.connect.source_port,
  1417. sizeof (req.x.connect.source_port), "%s", source_port);
  1418. snprintf (req.x.connect.destination_port,
  1419. sizeof (req.x.connect.destination_port),
  1420. "%s", destination_port);
  1421. return jack_client_deliver_request (client, &req);
  1422. }
  1423. int
  1424. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1425. {
  1426. jack_request_t req;
  1427. pthread_mutex_lock (&port->connection_lock);
  1428. if (port->connections == NULL) {
  1429. pthread_mutex_unlock (&port->connection_lock);
  1430. return 0;
  1431. }
  1432. pthread_mutex_unlock (&port->connection_lock);
  1433. req.type = DisconnectPort;
  1434. req.x.port_info.port_id = port->shared->id;
  1435. return jack_client_deliver_request (client, &req);
  1436. }
  1437. int
  1438. jack_disconnect (jack_client_t *client, const char *source_port,
  1439. const char *destination_port)
  1440. {
  1441. jack_request_t req;
  1442. req.type = DisconnectPorts;
  1443. snprintf (req.x.connect.source_port,
  1444. sizeof (req.x.connect.source_port), "%s", source_port);
  1445. snprintf (req.x.connect.destination_port,
  1446. sizeof (req.x.connect.destination_port),
  1447. "%s", destination_port);
  1448. return jack_client_deliver_request (client, &req);
  1449. }
  1450. void
  1451. jack_set_error_function (void (*func) (const char *))
  1452. {
  1453. jack_error_callback = func;
  1454. }
  1455. int
  1456. jack_set_graph_order_callback (jack_client_t *client,
  1457. JackGraphOrderCallback callback, void *arg)
  1458. {
  1459. if (client->control->active) {
  1460. jack_error ("You cannot set callbacks on an active client.");
  1461. return -1;
  1462. }
  1463. client->control->graph_order = callback;
  1464. client->control->graph_order_arg = arg;
  1465. return 0;
  1466. }
  1467. int jack_set_xrun_callback (jack_client_t *client,
  1468. JackXRunCallback callback, void *arg)
  1469. {
  1470. if (client->control->active) {
  1471. jack_error ("You cannot set callbacks on an active client.");
  1472. return -1;
  1473. }
  1474. client->control->xrun = callback;
  1475. client->control->xrun_arg = arg;
  1476. return 0;
  1477. }
  1478. int
  1479. jack_set_process_callback (jack_client_t *client,
  1480. JackProcessCallback callback, void *arg)
  1481. {
  1482. if (client->control->active) {
  1483. jack_error ("You cannot set callbacks on an active client.");
  1484. return -1;
  1485. }
  1486. client->control->process_arg = arg;
  1487. client->control->process = callback;
  1488. return 0;
  1489. }
  1490. int
  1491. jack_set_thread_init_callback (jack_client_t *client,
  1492. JackThreadInitCallback callback, void *arg)
  1493. {
  1494. if (client->control->active) {
  1495. jack_error ("You cannot set callbacks on an active client.");
  1496. return -1;
  1497. }
  1498. client->control->thread_init_arg = arg;
  1499. client->control->thread_init = callback;
  1500. return 0;
  1501. }
  1502. int
  1503. jack_set_freewheel_callback (jack_client_t *client,
  1504. JackFreewheelCallback callback, void *arg)
  1505. {
  1506. if (client->control->active) {
  1507. jack_error ("You cannot set callbacks on an active client.");
  1508. return -1;
  1509. }
  1510. client->control->freewheel_arg = arg;
  1511. client->control->freewheel_cb = callback;
  1512. return 0;
  1513. }
  1514. int
  1515. jack_set_buffer_size_callback (jack_client_t *client,
  1516. JackBufferSizeCallback callback, void *arg)
  1517. {
  1518. client->control->bufsize_arg = arg;
  1519. client->control->bufsize = callback;
  1520. return 0;
  1521. }
  1522. int
  1523. jack_set_port_registration_callback(jack_client_t *client,
  1524. JackPortRegistrationCallback callback,
  1525. void *arg)
  1526. {
  1527. if (client->control->active) {
  1528. jack_error ("You cannot set callbacks on an active client.");
  1529. return -1;
  1530. }
  1531. client->control->port_register_arg = arg;
  1532. client->control->port_register = callback;
  1533. return 0;
  1534. }
  1535. int
  1536. jack_get_process_done_fd (jack_client_t *client)
  1537. {
  1538. return client->graph_next_fd;
  1539. }
  1540. void
  1541. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1542. {
  1543. client->on_shutdown = function;
  1544. client->on_shutdown_arg = arg;
  1545. }
  1546. const char **
  1547. jack_get_ports (jack_client_t *client,
  1548. const char *port_name_pattern,
  1549. const char *type_name_pattern,
  1550. unsigned long flags)
  1551. {
  1552. jack_control_t *engine;
  1553. const char **matching_ports;
  1554. unsigned long match_cnt;
  1555. jack_port_shared_t *psp;
  1556. unsigned long i;
  1557. regex_t port_regex;
  1558. regex_t type_regex;
  1559. int matching;
  1560. engine = client->engine;
  1561. if (port_name_pattern && port_name_pattern[0]) {
  1562. regcomp (&port_regex, port_name_pattern,
  1563. REG_EXTENDED|REG_NOSUB);
  1564. }
  1565. if (type_name_pattern && type_name_pattern[0]) {
  1566. regcomp (&type_regex, type_name_pattern,
  1567. REG_EXTENDED|REG_NOSUB);
  1568. }
  1569. psp = engine->ports;
  1570. match_cnt = 0;
  1571. matching_ports = (const char **)
  1572. malloc (sizeof (char *) * engine->port_max);
  1573. for (i = 0; i < engine->port_max; i++) {
  1574. matching = 1;
  1575. if (!psp[i].in_use) {
  1576. continue;
  1577. }
  1578. if (flags) {
  1579. if ((psp[i].flags & flags) != flags) {
  1580. matching = 0;
  1581. }
  1582. }
  1583. if (matching && port_name_pattern && port_name_pattern[0]) {
  1584. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1585. matching = 0;
  1586. }
  1587. }
  1588. if (matching && type_name_pattern && type_name_pattern[0]) {
  1589. jack_port_type_id_t ptid = psp[i].ptype_id;
  1590. if (regexec (&type_regex,
  1591. engine->port_types[ptid].type_name,
  1592. 0, NULL, 0)) {
  1593. matching = 0;
  1594. }
  1595. }
  1596. if (matching) {
  1597. matching_ports[match_cnt++] = psp[i].name;
  1598. }
  1599. }
  1600. if (port_name_pattern && port_name_pattern[0]) {
  1601. regfree (&port_regex);
  1602. }
  1603. if (type_name_pattern && type_name_pattern[0]) {
  1604. regfree (&type_regex);
  1605. }
  1606. matching_ports[match_cnt] = 0;
  1607. if (match_cnt == 0) {
  1608. free (matching_ports);
  1609. matching_ports = 0;
  1610. }
  1611. return matching_ports;
  1612. }
  1613. float
  1614. jack_cpu_load (jack_client_t *client)
  1615. {
  1616. return client->engine->cpu_load;
  1617. }
  1618. float
  1619. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1620. {
  1621. return client->engine->xrun_delayed_usecs;
  1622. }
  1623. float
  1624. jack_get_max_delayed_usecs (jack_client_t *client)
  1625. {
  1626. return client->engine->max_delayed_usecs;
  1627. }
  1628. void
  1629. jack_reset_max_delayed_usecs (jack_client_t *client)
  1630. {
  1631. client->engine->max_delayed_usecs = 0.0f;
  1632. }
  1633. pthread_t
  1634. jack_client_thread_id (jack_client_t *client)
  1635. {
  1636. return client->thread_id;
  1637. }
  1638. int
  1639. jack_client_name_size(void)
  1640. {
  1641. return JACK_CLIENT_NAME_SIZE;
  1642. }
  1643. int
  1644. jack_port_name_size(void)
  1645. {
  1646. return JACK_PORT_NAME_SIZE;
  1647. }
  1648. int
  1649. jack_port_type_size(void)
  1650. {
  1651. return JACK_PORT_TYPE_SIZE;
  1652. }