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.

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