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.

1909 lines
44KB

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