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.

1910 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. jack_status_t status;
  717. return jack_request_client (ClientInternal, client_name, so_name,
  718. so_data, &res, &req_fd,
  719. 0, &status, NULL, NULL);
  720. }
  721. void
  722. jack_internal_client_close (const char *client_name)
  723. {
  724. jack_client_connect_request_t req;
  725. int fd;
  726. req.load = FALSE;
  727. snprintf (req.name, sizeof (req.name), "%s", client_name);
  728. if ((fd = server_connect (NULL)) < 0) {
  729. return;
  730. }
  731. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  732. jack_error ("cannot deliver ClientUnload request to JACK server.");
  733. }
  734. /* no response to this request */
  735. close (fd);
  736. return;
  737. }
  738. int
  739. jack_set_freewheel (jack_client_t* client, int onoff)
  740. {
  741. jack_request_t request;
  742. request.type = onoff ? FreeWheel : StopFreeWheel;
  743. return jack_client_deliver_request (client, &request);
  744. }
  745. void
  746. jack_start_freewheel (jack_client_t* client)
  747. {
  748. jack_client_control_t *control = client->control;
  749. if (client->engine->real_time) {
  750. #if JACK_USE_MACH_THREADS
  751. jack_drop_real_time_scheduling (client->process_thread);
  752. #else
  753. jack_drop_real_time_scheduling (client->thread);
  754. #endif
  755. }
  756. if (control->freewheel_cb) {
  757. control->freewheel_cb (1, control->freewheel_arg);
  758. }
  759. }
  760. void
  761. jack_stop_freewheel (jack_client_t* client)
  762. {
  763. jack_client_control_t *control = client->control;
  764. if (control->freewheel_cb) {
  765. control->freewheel_cb (0, control->freewheel_arg);
  766. }
  767. if (client->engine->real_time) {
  768. #if JACK_USE_MACH_THREADS
  769. jack_acquire_real_time_scheduling (client->process_thread,
  770. client->engine->client_priority);
  771. #else
  772. jack_acquire_real_time_scheduling (client->thread,
  773. client->engine->client_priority);
  774. #endif
  775. }
  776. }
  777. static void *
  778. jack_client_thread (void *arg)
  779. {
  780. jack_client_t *client = (jack_client_t *) arg;
  781. jack_client_control_t *control = client->control;
  782. jack_event_t event;
  783. char status = 0;
  784. char c = 0;
  785. int err = 0;
  786. pthread_mutex_lock (&client_lock);
  787. client->thread_ok = TRUE;
  788. client->thread_id = pthread_self();
  789. pthread_cond_signal (&client_ready);
  790. pthread_mutex_unlock (&client_lock);
  791. client->control->pid = getpid();
  792. client->control->pgrp = getpgrp();
  793. DEBUG ("client thread is now running");
  794. if (client->control->thread_init) {
  795. DEBUG ("calling client thread init callback");
  796. client->control->thread_init (client->control->thread_init_arg);
  797. }
  798. while (err == 0) {
  799. if (client->engine->engine_ok == 0) {
  800. if (client->on_shutdown)
  801. client->on_shutdown (client->on_shutdown_arg);
  802. else
  803. jack_error ("engine unexpectedly shutdown; "
  804. "thread exiting\n");
  805. pthread_exit (0);
  806. }
  807. DEBUG ("client polling on %s", client->pollmax == 2 ?
  808. "event_fd and graph_wait_fd..." :
  809. "event_fd only");
  810. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  811. if (errno == EINTR) {
  812. continue;
  813. }
  814. jack_error ("poll failed in client (%s)",
  815. strerror (errno));
  816. status = -1;
  817. break;
  818. }
  819. /* get an accurate timestamp on waking from poll for a
  820. * process() cycle.
  821. */
  822. if (client->graph_wait_fd >= 0 && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  823. control->awake_at = jack_get_microseconds();
  824. }
  825. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  826. client->pollfd[EVENT_POLL_INDEX].revents,
  827. client->pollfd[WAIT_POLL_INDEX].revents);
  828. pthread_testcancel();
  829. if (client->graph_wait_fd >= 0 &&
  830. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  831. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR, ZOMBIE\n\n\n\n\n");
  832. /* our upstream "wait" connection
  833. closed, which either means that
  834. an intermediate client exited, or
  835. jackd exited, or jackd zombified
  836. us.
  837. we can discover the zombification
  838. via client->control->dead, but
  839. the other two possibilities are
  840. impossible to identify just from
  841. this situation. so we have to
  842. check what we are connected to,
  843. and act accordingly.
  844. */
  845. if (client->upstream_is_jackd) {
  846. DEBUG ("WE DIE\n");
  847. goto zombie;
  848. } else {
  849. DEBUG ("WE PUNT\n");
  850. /* don't poll on the wait fd
  851. * again until we get a
  852. * GraphReordered event.
  853. */
  854. client->graph_wait_fd = -1;
  855. client->pollmax = 1;
  856. }
  857. }
  858. if (client->control->dead) {
  859. goto zombie;
  860. }
  861. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  862. /* jackd shutdown */
  863. goto zombie;
  864. }
  865. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  866. DEBUG ("client receives an event, "
  867. "now reading on event fd");
  868. /* server has sent us an event. process the
  869. * event and reply */
  870. if (read (client->event_fd, &event, sizeof (event))
  871. != sizeof (event)) {
  872. jack_error ("cannot read server event (%s)",
  873. strerror (errno));
  874. err++;
  875. break;
  876. }
  877. status = 0;
  878. switch (event.type) {
  879. case PortRegistered:
  880. if (control->port_register) {
  881. control->port_register
  882. (event.x.port_id, TRUE,
  883. control->port_register_arg);
  884. }
  885. break;
  886. case PortUnregistered:
  887. if (control->port_register) {
  888. control->port_register
  889. (event.x.port_id, FALSE,
  890. control->port_register_arg);
  891. }
  892. break;
  893. case GraphReordered:
  894. status = jack_handle_reorder (client, &event);
  895. break;
  896. case PortConnected:
  897. case PortDisconnected:
  898. status = jack_client_handle_port_connection
  899. (client, &event);
  900. break;
  901. case BufferSizeChange:
  902. jack_client_invalidate_port_buffers (client);
  903. if (control->bufsize) {
  904. status = control->bufsize
  905. (control->nframes,
  906. control->bufsize_arg);
  907. }
  908. break;
  909. case SampleRateChange:
  910. if (control->srate) {
  911. status = control->srate
  912. (control->nframes,
  913. control->srate_arg);
  914. }
  915. break;
  916. case XRun:
  917. if (control->xrun) {
  918. status = control->xrun
  919. (control->xrun_arg);
  920. }
  921. break;
  922. case AttachPortSegment:
  923. jack_attach_port_segment (client, event.y.ptid);
  924. break;
  925. case StartFreewheel:
  926. jack_start_freewheel (client);
  927. break;
  928. case StopFreewheel:
  929. jack_stop_freewheel (client);
  930. break;
  931. }
  932. DEBUG ("client has dealt with the event, writing "
  933. "response on event fd");
  934. if (write (client->event_fd, &status, sizeof (status))
  935. != sizeof (status)) {
  936. jack_error ("cannot send event response to "
  937. "engine (%s)", strerror (errno));
  938. err++;
  939. break;
  940. }
  941. }
  942. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  943. #ifdef WITH_TIMESTAMPS
  944. jack_reset_timestamps ();
  945. #endif
  946. DEBUG ("client %d signalled at %" PRIu64
  947. ", awake for process at %" PRIu64
  948. " (delay = %" PRIu64
  949. " usecs) (wakeup on graph_wait_fd==%d)",
  950. getpid(),
  951. control->signalled_at,
  952. control->awake_at,
  953. control->awake_at - control->signalled_at,
  954. client->pollfd[WAIT_POLL_INDEX].fd);
  955. control->state = Running;
  956. if (control->sync_cb)
  957. jack_call_sync_client (client);
  958. if (control->process) {
  959. if (control->process (control->nframes,
  960. control->process_arg)
  961. == 0) {
  962. control->state = Finished;
  963. }
  964. } else {
  965. control->state = Finished;
  966. }
  967. if (control->timebase_cb)
  968. jack_call_timebase_master (client);
  969. control->finished_at = jack_get_microseconds();
  970. #ifdef WITH_TIMESTAMPS
  971. jack_timestamp ("finished");
  972. #endif
  973. /* pass the execution token along */
  974. DEBUG ("client finished processing at %" PRIu64
  975. " (elapsed = %" PRIu64
  976. " usecs), writing on graph_next_fd==%d",
  977. control->finished_at,
  978. control->finished_at - control->awake_at,
  979. client->graph_next_fd);
  980. if (write (client->graph_next_fd, &c, sizeof (c))
  981. != sizeof (c)) {
  982. jack_error ("cannot continue execution of the "
  983. "processing graph (%s)",
  984. strerror(errno));
  985. err++;
  986. break;
  987. }
  988. DEBUG ("client sent message to next stage by %" PRIu64
  989. ", client reading on graph_wait_fd==%d",
  990. jack_get_microseconds(), client->graph_wait_fd);
  991. #ifdef WITH_TIMESTAMPS
  992. jack_timestamp ("read pending byte from wait");
  993. #endif
  994. DEBUG("reading cleanup byte from pipe\n");
  995. if ((read (client->graph_wait_fd, &c, sizeof (c))
  996. != sizeof (c))) {
  997. DEBUG ("WARNING: READ FAILED!");
  998. #if 0
  999. jack_error ("cannot complete execution of the "
  1000. "processing graph (%s)",
  1001. strerror(errno));
  1002. err++;
  1003. break;
  1004. #endif
  1005. }
  1006. /* check if we were killed during the process
  1007. * cycle (or whatever) */
  1008. if (client->control->dead) {
  1009. goto zombie;
  1010. }
  1011. DEBUG("process cycle fully complete\n");
  1012. #ifdef WITH_TIMESTAMPS
  1013. jack_timestamp ("read done");
  1014. jack_dump_timestamps (stdout);
  1015. #endif
  1016. }
  1017. }
  1018. return (void *) ((intptr_t)err);
  1019. zombie:
  1020. if (client->on_shutdown) {
  1021. jack_error ("zombified - calling shutdown handler");
  1022. client->on_shutdown (client->on_shutdown_arg);
  1023. } else {
  1024. jack_error ("zombified - exiting from JACK");
  1025. jack_client_close (client);
  1026. /* Need a fix : possibly make client crash if
  1027. * zombified without shutdown handler
  1028. */
  1029. }
  1030. pthread_exit (0);
  1031. /*NOTREACHED*/
  1032. return 0;
  1033. }
  1034. #ifdef JACK_USE_MACH_THREADS
  1035. /* real-time thread : separated from the normal client thread, it will
  1036. * communicate with the server using fast mach RPC mechanism */
  1037. static void *
  1038. jack_client_process_thread (void *arg)
  1039. {
  1040. jack_client_t *client = (jack_client_t *) arg;
  1041. jack_client_control_t *control = client->control;
  1042. int err = 0;
  1043. if (client->control->thread_init) {
  1044. /* this means that the init callback will be called twice -taybin*/
  1045. DEBUG ("calling client thread init callback");
  1046. client->control->thread_init (client->control->thread_init_arg);
  1047. }
  1048. client->control->pid = getpid();
  1049. DEBUG ("client process thread is now running");
  1050. client->rt_thread_ok = TRUE;
  1051. while (err == 0) {
  1052. if (jack_client_suspend(client) < 0) {
  1053. jack_error ("jack_client_process_thread : resume error");
  1054. goto zombie;
  1055. }
  1056. control->awake_at = jack_get_microseconds();
  1057. DEBUG ("client resumed");
  1058. control->state = Running;
  1059. if (control->sync_cb)
  1060. jack_call_sync_client (client);
  1061. if (control->process) {
  1062. if (control->process (control->nframes,
  1063. control->process_arg) == 0) {
  1064. control->state = Finished;
  1065. }
  1066. } else {
  1067. control->state = Finished;
  1068. }
  1069. if (control->timebase_cb)
  1070. jack_call_timebase_master (client);
  1071. control->finished_at = jack_get_microseconds();
  1072. #ifdef WITH_TIMESTAMPS
  1073. jack_timestamp ("finished");
  1074. #endif
  1075. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1076. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  1077. /* check if we were killed during the process cycle (or whatever) */
  1078. if (client->control->dead) {
  1079. jack_error ("jack_client_process_thread : client->control->dead");
  1080. goto zombie;
  1081. }
  1082. DEBUG("process cycle fully complete\n");
  1083. }
  1084. return (void *) ((intptr_t)err);
  1085. zombie:
  1086. jack_error ("jack_client_process_thread : zombified");
  1087. client->rt_thread_ok = FALSE;
  1088. if (client->on_shutdown) {
  1089. jack_error ("zombified - calling shutdown handler");
  1090. client->on_shutdown (client->on_shutdown_arg);
  1091. } else {
  1092. jack_error ("zombified - exiting from JACK");
  1093. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  1094. }
  1095. pthread_exit (0);
  1096. /*NOTREACHED*/
  1097. return 0;
  1098. }
  1099. #endif /* JACK_USE_MACH_THREADS */
  1100. static int
  1101. jack_start_thread (jack_client_t *client)
  1102. {
  1103. if (client->engine->real_time) {
  1104. #ifdef USE_MLOCK
  1105. if (client->engine->do_mlock
  1106. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1107. jack_error ("cannot lock down memory for RT thread (%s)",
  1108. strerror (errno));
  1109. #ifdef ENSURE_MLOCK
  1110. return -1;
  1111. #endif /* ENSURE_MLOCK */
  1112. }
  1113. if (client->engine->do_munlock) {
  1114. cleanup_mlock ();
  1115. }
  1116. #endif /* USE_MLOCK */
  1117. }
  1118. #ifdef JACK_USE_MACH_THREADS
  1119. /* Stephane Letz : letz@grame.fr
  1120. On MacOSX, the normal thread does not need to be real-time.
  1121. */
  1122. if (jack_create_thread (&client->thread,
  1123. client->engine->client_priority,
  1124. 0,
  1125. jack_client_thread, client)) {
  1126. return -1;
  1127. }
  1128. #else
  1129. if (jack_create_thread (&client->thread,
  1130. client->engine->client_priority,
  1131. client->engine->real_time,
  1132. jack_client_thread, client)) {
  1133. return -1;
  1134. }
  1135. #endif
  1136. #ifdef JACK_USE_MACH_THREADS
  1137. /* a secondary thread that runs the process callback and uses
  1138. ultra-fast Mach primitives for inter-thread signalling.
  1139. XXX in a properly structured JACK, there would be no
  1140. need for this, because we would have client wake up
  1141. methods that encapsulated the underlying mechanism
  1142. used.
  1143. */
  1144. if (jack_create_thread(&client->process_thread,
  1145. client->engine->client_priority,
  1146. client->engine->real_time,
  1147. jack_client_process_thread, client)) {
  1148. return -1;
  1149. }
  1150. #endif /* JACK_USE_MACH_THREADS */
  1151. return 0;
  1152. }
  1153. int
  1154. jack_activate (jack_client_t *client)
  1155. {
  1156. jack_request_t req;
  1157. /* we need to scribble on our stack to ensure that its memory
  1158. * pages are actually mapped (more important for mlockall(2)
  1159. * usage in jack_start_thread())
  1160. */
  1161. char buf[JACK_THREAD_STACK_TOUCH];
  1162. int i;
  1163. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1164. buf[i] = (char) (i & 0xff);
  1165. }
  1166. if (client->control->type == ClientInternal ||
  1167. client->control->type == ClientDriver) {
  1168. goto startit;
  1169. }
  1170. /* get the pid of the client process to pass it to engine */
  1171. client->control->pid = getpid ();
  1172. #ifdef USE_CAPABILITIES
  1173. if (client->engine->has_capabilities != 0 &&
  1174. client->control->pid != 0 && client->engine->real_time != 0) {
  1175. /* we need to ask the engine for realtime capabilities
  1176. before trying to start the realtime thread
  1177. */
  1178. req.type = SetClientCapabilities;
  1179. req.x.client_id = client->control->id;
  1180. jack_client_deliver_request (client, &req);
  1181. if (req.status) {
  1182. /* what to do? engine is running realtime, it
  1183. is using capabilities and has them
  1184. (otherwise we would not get an error
  1185. return) but for some reason it could not
  1186. give the client the required capabilities,
  1187. so for now downgrade the client so that it
  1188. still runs, albeit non-realtime - nando
  1189. */
  1190. jack_error ("could not receive realtime capabilities, "
  1191. "client will run non-realtime");
  1192. /* XXX wrong, this is a property of the engine
  1193. client->engine->real_time = 0;
  1194. */
  1195. }
  1196. }
  1197. #endif /* USE_CAPABILITIES */
  1198. if (client->first_active) {
  1199. pthread_mutex_init (&client_lock, NULL);
  1200. pthread_cond_init (&client_ready, NULL);
  1201. pthread_mutex_lock (&client_lock);
  1202. if (jack_start_thread (client)) {
  1203. pthread_mutex_unlock (&client_lock);
  1204. return -1;
  1205. }
  1206. pthread_cond_wait (&client_ready, &client_lock);
  1207. pthread_mutex_unlock (&client_lock);
  1208. if (!client->thread_ok) {
  1209. jack_error ("could not start client thread");
  1210. return -1;
  1211. }
  1212. client->first_active = FALSE;
  1213. }
  1214. startit:
  1215. req.type = ActivateClient;
  1216. req.x.client_id = client->control->id;
  1217. return jack_client_deliver_request (client, &req);
  1218. }
  1219. int
  1220. jack_deactivate (jack_client_t *client)
  1221. {
  1222. jack_request_t req;
  1223. req.type = DeactivateClient;
  1224. req.x.client_id = client->control->id;
  1225. return jack_client_deliver_request (client, &req);
  1226. }
  1227. int
  1228. jack_client_close (jack_client_t *client)
  1229. {
  1230. JSList *node;
  1231. void *status;
  1232. if (client->control->active) {
  1233. jack_deactivate (client);
  1234. }
  1235. if (client->control->type == ClientExternal) {
  1236. #if JACK_USE_MACH_THREADS
  1237. if (client->rt_thread_ok) {
  1238. // MacOSX pthread_cancel not implemented in
  1239. // Darwin 5.5, 6.4
  1240. mach_port_t machThread =
  1241. pthread_mach_thread_np (client->process_thread);
  1242. thread_terminate (machThread);
  1243. }
  1244. #endif
  1245. /* stop the thread that communicates with the jack
  1246. * server, only if it was actually running
  1247. */
  1248. if (client->thread_ok){
  1249. pthread_cancel (client->thread);
  1250. pthread_join (client->thread, &status);
  1251. }
  1252. if (client->control) {
  1253. jack_release_shm (&client->control_shm);
  1254. client->control = NULL;
  1255. }
  1256. if (client->engine) {
  1257. jack_release_shm (&client->engine_shm);
  1258. client->engine = NULL;
  1259. }
  1260. if (client->port_segment) {
  1261. jack_port_type_id_t ptid;
  1262. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1263. jack_release_shm (&client->port_segment[ptid]);
  1264. }
  1265. free (client->port_segment);
  1266. client->port_segment = NULL;
  1267. }
  1268. if (client->graph_wait_fd) {
  1269. close (client->graph_wait_fd);
  1270. }
  1271. if (client->graph_next_fd) {
  1272. close (client->graph_next_fd);
  1273. }
  1274. close (client->event_fd);
  1275. close (client->request_fd);
  1276. }
  1277. for (node = client->ports; node; node = jack_slist_next (node)) {
  1278. free (node->data);
  1279. }
  1280. jack_slist_free (client->ports);
  1281. jack_client_free (client);
  1282. return 0;
  1283. }
  1284. int
  1285. jack_is_realtime (jack_client_t *client)
  1286. {
  1287. return client->engine->real_time;
  1288. }
  1289. jack_nframes_t
  1290. jack_get_buffer_size (jack_client_t *client)
  1291. {
  1292. return client->engine->buffer_size;
  1293. }
  1294. int
  1295. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1296. {
  1297. #ifdef DO_BUFFER_RESIZE
  1298. jack_request_t req;
  1299. req.type = SetBufferSize;
  1300. req.x.nframes = nframes;
  1301. return jack_client_deliver_request (client, &req);
  1302. #else
  1303. return ENOSYS;
  1304. #endif /* DO_BUFFER_RESIZE */
  1305. }
  1306. int
  1307. jack_connect (jack_client_t *client, const char *source_port,
  1308. const char *destination_port)
  1309. {
  1310. jack_request_t req;
  1311. req.type = ConnectPorts;
  1312. snprintf (req.x.connect.source_port,
  1313. sizeof (req.x.connect.source_port), "%s", source_port);
  1314. snprintf (req.x.connect.destination_port,
  1315. sizeof (req.x.connect.destination_port),
  1316. "%s", destination_port);
  1317. return jack_client_deliver_request (client, &req);
  1318. }
  1319. int
  1320. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1321. {
  1322. jack_request_t req;
  1323. pthread_mutex_lock (&port->connection_lock);
  1324. if (port->connections == NULL) {
  1325. pthread_mutex_unlock (&port->connection_lock);
  1326. return 0;
  1327. }
  1328. pthread_mutex_unlock (&port->connection_lock);
  1329. req.type = DisconnectPort;
  1330. req.x.port_info.port_id = port->shared->id;
  1331. return jack_client_deliver_request (client, &req);
  1332. }
  1333. int
  1334. jack_disconnect (jack_client_t *client, const char *source_port,
  1335. const char *destination_port)
  1336. {
  1337. jack_request_t req;
  1338. req.type = DisconnectPorts;
  1339. snprintf (req.x.connect.source_port,
  1340. sizeof (req.x.connect.source_port), "%s", source_port);
  1341. snprintf (req.x.connect.destination_port,
  1342. sizeof (req.x.connect.destination_port),
  1343. "%s", destination_port);
  1344. return jack_client_deliver_request (client, &req);
  1345. }
  1346. void
  1347. jack_set_error_function (void (*func) (const char *))
  1348. {
  1349. jack_error_callback = func;
  1350. }
  1351. int
  1352. jack_set_graph_order_callback (jack_client_t *client,
  1353. JackGraphOrderCallback callback, void *arg)
  1354. {
  1355. if (client->control->active) {
  1356. jack_error ("You cannot set callbacks on an active client.");
  1357. return -1;
  1358. }
  1359. client->control->graph_order = callback;
  1360. client->control->graph_order_arg = arg;
  1361. return 0;
  1362. }
  1363. int jack_set_xrun_callback (jack_client_t *client,
  1364. JackXRunCallback callback, void *arg)
  1365. {
  1366. if (client->control->active) {
  1367. jack_error ("You cannot set callbacks on an active client.");
  1368. return -1;
  1369. }
  1370. client->control->xrun = callback;
  1371. client->control->xrun_arg = arg;
  1372. return 0;
  1373. }
  1374. int
  1375. jack_set_process_callback (jack_client_t *client,
  1376. JackProcessCallback callback, void *arg)
  1377. {
  1378. if (client->control->active) {
  1379. jack_error ("You cannot set callbacks on an active client.");
  1380. return -1;
  1381. }
  1382. client->control->process_arg = arg;
  1383. client->control->process = callback;
  1384. return 0;
  1385. }
  1386. int
  1387. jack_set_thread_init_callback (jack_client_t *client,
  1388. JackThreadInitCallback callback, void *arg)
  1389. {
  1390. if (client->control->active) {
  1391. jack_error ("You cannot set callbacks on an active client.");
  1392. return -1;
  1393. }
  1394. client->control->thread_init_arg = arg;
  1395. client->control->thread_init = callback;
  1396. return 0;
  1397. }
  1398. int
  1399. jack_set_freewheel_callback (jack_client_t *client,
  1400. JackFreewheelCallback callback, void *arg)
  1401. {
  1402. if (client->control->active) {
  1403. jack_error ("You cannot set callbacks on an active client.");
  1404. return -1;
  1405. }
  1406. client->control->freewheel_arg = arg;
  1407. client->control->freewheel_cb = callback;
  1408. return 0;
  1409. }
  1410. int
  1411. jack_set_buffer_size_callback (jack_client_t *client,
  1412. JackBufferSizeCallback callback, void *arg)
  1413. {
  1414. client->control->bufsize_arg = arg;
  1415. client->control->bufsize = callback;
  1416. return 0;
  1417. }
  1418. int
  1419. jack_set_port_registration_callback(jack_client_t *client,
  1420. JackPortRegistrationCallback callback,
  1421. void *arg)
  1422. {
  1423. if (client->control->active) {
  1424. jack_error ("You cannot set callbacks on an active client.");
  1425. return -1;
  1426. }
  1427. client->control->port_register_arg = arg;
  1428. client->control->port_register = callback;
  1429. return 0;
  1430. }
  1431. int
  1432. jack_get_process_done_fd (jack_client_t *client)
  1433. {
  1434. return client->graph_next_fd;
  1435. }
  1436. void
  1437. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1438. {
  1439. client->on_shutdown = function;
  1440. client->on_shutdown_arg = arg;
  1441. }
  1442. const char **
  1443. jack_get_ports (jack_client_t *client,
  1444. const char *port_name_pattern,
  1445. const char *type_name_pattern,
  1446. unsigned long flags)
  1447. {
  1448. jack_control_t *engine;
  1449. const char **matching_ports;
  1450. unsigned long match_cnt;
  1451. jack_port_shared_t *psp;
  1452. unsigned long i;
  1453. regex_t port_regex;
  1454. regex_t type_regex;
  1455. int matching;
  1456. engine = client->engine;
  1457. if (port_name_pattern && port_name_pattern[0]) {
  1458. regcomp (&port_regex, port_name_pattern,
  1459. REG_EXTENDED|REG_NOSUB);
  1460. }
  1461. if (type_name_pattern && type_name_pattern[0]) {
  1462. regcomp (&type_regex, type_name_pattern,
  1463. REG_EXTENDED|REG_NOSUB);
  1464. }
  1465. psp = engine->ports;
  1466. match_cnt = 0;
  1467. matching_ports = (const char **)
  1468. malloc (sizeof (char *) * engine->port_max);
  1469. for (i = 0; i < engine->port_max; i++) {
  1470. matching = 1;
  1471. if (!psp[i].in_use) {
  1472. continue;
  1473. }
  1474. if (flags) {
  1475. if ((psp[i].flags & flags) != flags) {
  1476. matching = 0;
  1477. }
  1478. }
  1479. if (matching && port_name_pattern && port_name_pattern[0]) {
  1480. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1481. matching = 0;
  1482. }
  1483. }
  1484. if (matching && type_name_pattern && type_name_pattern[0]) {
  1485. jack_port_type_id_t ptid = psp[i].ptype_id;
  1486. if (regexec (&type_regex,
  1487. engine->port_types[ptid].type_name,
  1488. 0, NULL, 0)) {
  1489. matching = 0;
  1490. }
  1491. }
  1492. if (matching) {
  1493. matching_ports[match_cnt++] = psp[i].name;
  1494. }
  1495. }
  1496. if (port_name_pattern && port_name_pattern[0]) {
  1497. regfree (&port_regex);
  1498. }
  1499. if (type_name_pattern && type_name_pattern[0]) {
  1500. regfree (&type_regex);
  1501. }
  1502. matching_ports[match_cnt] = 0;
  1503. if (match_cnt == 0) {
  1504. free (matching_ports);
  1505. matching_ports = 0;
  1506. }
  1507. return matching_ports;
  1508. }
  1509. float
  1510. jack_cpu_load (jack_client_t *client)
  1511. {
  1512. return client->engine->cpu_load;
  1513. }
  1514. float
  1515. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1516. {
  1517. return client->engine->xrun_delayed_usecs;
  1518. }
  1519. pthread_t
  1520. jack_client_thread_id (jack_client_t *client)
  1521. {
  1522. return client->thread_id;
  1523. }
  1524. int
  1525. jack_client_name_size(void)
  1526. {
  1527. return JACK_CLIENT_NAME_SIZE;
  1528. }
  1529. int
  1530. jack_port_name_size(void)
  1531. {
  1532. return JACK_PORT_NAME_SIZE;
  1533. }
  1534. int
  1535. jack_port_type_size(void)
  1536. {
  1537. return JACK_PORT_TYPE_SIZE;
  1538. }