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.

2858 lines
66KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2005 Jussi Laako
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. #include <limits.h>
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <regex.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. #include <sys/mman.h>
  33. #include <jack/internal.h>
  34. #include <jack/jack.h>
  35. #include <jack/engine.h>
  36. #include <jack/pool.h>
  37. #include <jack/jslist.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/unlock.h>
  41. #include <jack/thread.h>
  42. #include <jack/varargs.h>
  43. #include <jack/intsimd.h>
  44. #include <jack/messagebuffer.h>
  45. #include <sysdeps/time.h>
  46. #include "local.h"
  47. #include <sysdeps/poll.h>
  48. #include <sysdeps/ipc.h>
  49. #include <sysdeps/cycles.h>
  50. #ifdef JACK_USE_MACH_THREADS
  51. #include <sysdeps/pThreadUtilities.h>
  52. #endif
  53. static pthread_mutex_t client_lock;
  54. static pthread_cond_t client_ready;
  55. static int
  56. jack_client_close_aux (jack_client_t *client);
  57. #define EVENT_POLL_INDEX 0
  58. #define WAIT_POLL_INDEX 1
  59. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  60. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
  61. typedef struct {
  62. int status;
  63. struct _jack_client *client;
  64. const char *client_name;
  65. } client_info;
  66. #ifdef USE_DYNSIMD
  67. #ifdef ARCH_X86
  68. int cpu_type = 0;
  69. static void
  70. init_cpu ()
  71. {
  72. cpu_type = ((have_3dnow() << 8) | have_sse());
  73. if (ARCH_X86_HAVE_3DNOW(cpu_type))
  74. jack_info("Enhanced3DNow! detected");
  75. if (ARCH_X86_HAVE_SSE2(cpu_type))
  76. jack_info("SSE2 detected");
  77. if ((!ARCH_X86_HAVE_3DNOW(cpu_type)) && (!ARCH_X86_HAVE_SSE2(cpu_type)))
  78. jack_info("No supported SIMD instruction sets detected");
  79. jack_port_set_funcs();
  80. }
  81. #else /* ARCH_X86 */
  82. static void
  83. init_cpu ()
  84. {
  85. jack_port_set_funcs();
  86. }
  87. #endif /* ARCH_X86 */
  88. #endif /* USE_DYNSIMD */
  89. char *jack_tmpdir = DEFAULT_TMP_DIR;
  90. static int
  91. jack_get_tmpdir ()
  92. {
  93. FILE* in;
  94. size_t len;
  95. char buf[PATH_MAX+2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  96. char *pathenv;
  97. char *pathcopy;
  98. char *p;
  99. /* some implementations of popen(3) close a security loophole by
  100. resetting PATH for the exec'd command. since we *want* to
  101. use the user's PATH setting to locate jackd, we have to
  102. do it ourselves.
  103. */
  104. if ((pathenv = getenv ("PATH")) == 0) {
  105. return -1;
  106. }
  107. /* don't let strtok(3) mess with the real environment variable */
  108. if ((pathcopy = strdup (pathenv)) == NULL) {
  109. return -1;
  110. }
  111. p = strtok (pathcopy, ":");
  112. while (p) {
  113. char jackd[PATH_MAX+1];
  114. char command[PATH_MAX+4];
  115. snprintf (jackd, sizeof (jackd), "%s/jackd", p);
  116. if (access (jackd, X_OK) == 0) {
  117. snprintf (command, sizeof (command), "%s -l", jackd);
  118. if ((in = popen (command, "r")) != NULL) {
  119. break;
  120. }
  121. }
  122. p = strtok (NULL, ":");
  123. }
  124. if (p == NULL) {
  125. /* no command successfully started */
  126. free (pathcopy);
  127. return -1;
  128. }
  129. if (fgets (buf, sizeof (buf), in) == NULL) {
  130. fclose (in);
  131. free (pathcopy);
  132. return -1;
  133. }
  134. len = strlen (buf);
  135. if (buf[len-1] != '\n') {
  136. /* didn't get a whole line */
  137. fclose (in);
  138. free (pathcopy);
  139. return -1;
  140. }
  141. if ((jack_tmpdir = (char *) malloc (len)) == NULL) {
  142. free (pathcopy);
  143. return -1;
  144. }
  145. memcpy (jack_tmpdir, buf, len-1);
  146. jack_tmpdir[len-1] = '\0';
  147. fclose (in);
  148. free (pathcopy);
  149. return 0;
  150. }
  151. void
  152. jack_error (const char *fmt, ...)
  153. {
  154. va_list ap;
  155. char buffer[300];
  156. va_start (ap, fmt);
  157. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  158. jack_error_callback (buffer);
  159. va_end (ap);
  160. }
  161. void
  162. default_jack_error_callback (const char *desc)
  163. {
  164. #ifdef DEBUG_ENABLED
  165. DEBUG("%s", desc);
  166. #else
  167. fprintf(stderr, "%s\n", desc);
  168. fflush(stderr);
  169. #endif
  170. }
  171. void
  172. default_jack_info_callback (const char *desc)
  173. {
  174. fprintf(stdout, "%s\n", desc);
  175. fflush(stdout);
  176. }
  177. void
  178. silent_jack_error_callback (const char *desc)
  179. {
  180. }
  181. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  182. void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;
  183. void
  184. jack_info (const char *fmt, ...)
  185. {
  186. va_list ap;
  187. char buffer[300];
  188. va_start (ap, fmt);
  189. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  190. jack_info_callback (buffer);
  191. va_end (ap);
  192. }
  193. static int
  194. oop_client_deliver_request (void *ptr, jack_request_t *req)
  195. {
  196. int wok, rok;
  197. jack_client_t *client = (jack_client_t*) ptr;
  198. wok = (write (client->request_fd, req, sizeof (*req))
  199. == sizeof (*req));
  200. rok = (read (client->request_fd, req, sizeof (*req))
  201. == sizeof (*req));
  202. if (wok && rok) { /* everything OK? */
  203. return req->status;
  204. }
  205. req->status = -1; /* request failed */
  206. /* check for server shutdown */
  207. if (client->engine->engine_ok == 0)
  208. return req->status;
  209. /* otherwise report errors */
  210. if (!wok)
  211. jack_error ("cannot send request type %d to server",
  212. req->type);
  213. if (!rok)
  214. jack_error ("cannot read result for request type %d from"
  215. " server (%s)", req->type, strerror (errno));
  216. return req->status;
  217. }
  218. int
  219. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  220. {
  221. /* indirect through the function pointer that was set either
  222. * by jack_client_open() or by jack_new_client_request() in
  223. * the server.
  224. */
  225. return client->deliver_request (client->deliver_arg,
  226. req);
  227. }
  228. #if JACK_USE_MACH_THREADS
  229. jack_client_t *
  230. jack_client_alloc ()
  231. {
  232. jack_client_t *client;
  233. if ((client = (jack_client_t *) malloc (sizeof (jack_client_t))) == NULL) {
  234. return NULL;
  235. }
  236. if ((client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 1)) == NULL) {
  237. free (client);
  238. return NULL;
  239. }
  240. client->pollmax = 1;
  241. client->request_fd = -1;
  242. client->event_fd = -1;
  243. client->upstream_is_jackd = 0;
  244. client->graph_next_fd = -1;
  245. client->ports = NULL;
  246. client->ports_ext = NULL;
  247. client->engine = NULL;
  248. client->control = NULL;
  249. client->thread_ok = FALSE;
  250. client->rt_thread_ok = FALSE;
  251. client->first_active = TRUE;
  252. client->on_shutdown = NULL;
  253. client->on_info_shutdown = NULL;
  254. client->n_port_types = 0;
  255. client->port_segment = NULL;
  256. #ifdef USE_DYNSIMD
  257. init_cpu();
  258. #endif /* USE_DYNSIMD */
  259. return client;
  260. }
  261. #else
  262. jack_client_t *
  263. jack_client_alloc ()
  264. {
  265. jack_client_t *client;
  266. if ((client = (jack_client_t *) malloc (sizeof (jack_client_t))) == NULL) {
  267. return NULL;
  268. }
  269. if ((client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2)) == NULL) {
  270. free (client);
  271. return NULL;
  272. }
  273. client->pollmax = 2;
  274. client->request_fd = -1;
  275. client->event_fd = -1;
  276. client->upstream_is_jackd = 0;
  277. client->graph_wait_fd = -1;
  278. client->graph_next_fd = -1;
  279. client->ports = NULL;
  280. client->ports_ext = NULL;
  281. client->engine = NULL;
  282. client->control = NULL;
  283. client->thread_ok = FALSE;
  284. client->first_active = TRUE;
  285. client->on_shutdown = NULL;
  286. client->on_info_shutdown = NULL;
  287. client->n_port_types = 0;
  288. client->port_segment = NULL;
  289. #ifdef USE_DYNSIMD
  290. init_cpu();
  291. #endif /* USE_DYNSIMD */
  292. return client;
  293. }
  294. #endif
  295. /*
  296. * Build the jack_client_t structure for an internal client.
  297. */
  298. jack_client_t *
  299. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  300. {
  301. jack_client_t* client;
  302. client = jack_client_alloc ();
  303. client->control = cc;
  304. client->engine = engine->control;
  305. client->n_port_types = client->engine->n_port_types;
  306. client->port_segment = &engine->port_segment[0];
  307. return client;
  308. }
  309. static void
  310. jack_client_free (jack_client_t *client)
  311. {
  312. if (client->pollfd) {
  313. free (client->pollfd);
  314. }
  315. free (client);
  316. }
  317. void
  318. jack_client_fix_port_buffers (jack_client_t *client)
  319. {
  320. JSList *node;
  321. jack_port_t *port;
  322. /* This releases all local memory owned by input ports
  323. and sets the buffer pointer to NULL. This will cause
  324. jack_port_get_buffer() to reallocate space for the
  325. buffer on the next call (if there is one).
  326. */
  327. for (node = client->ports; node; node = jack_slist_next (node)) {
  328. port = (jack_port_t *) node->data;
  329. if (port->shared->flags & JackPortIsInput) {
  330. if (port->mix_buffer) {
  331. size_t buffer_size =
  332. jack_port_type_buffer_size( port->type_info,
  333. client->engine->buffer_size );
  334. jack_pool_release (port->mix_buffer);
  335. port->mix_buffer = NULL;
  336. pthread_mutex_lock (&port->connection_lock);
  337. if (jack_slist_length (port->connections) > 1) {
  338. port->mix_buffer = jack_pool_alloc (buffer_size);
  339. port->fptr.buffer_init (port->mix_buffer,
  340. buffer_size,
  341. client->engine->buffer_size);
  342. }
  343. pthread_mutex_unlock (&port->connection_lock);
  344. }
  345. }
  346. }
  347. }
  348. int
  349. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  350. {
  351. jack_port_t *control_port;
  352. jack_port_t *other = 0;
  353. JSList *node;
  354. int need_free = FALSE;
  355. if (client->engine->ports[event->x.self_id].client_id == client->control->id ||
  356. client->engine->ports[event->y.other_id].client_id == client->control->id) {
  357. /* its one of ours */
  358. switch (event->type) {
  359. case PortConnected:
  360. other = jack_port_new (client, event->y.other_id,
  361. client->engine);
  362. /* jack_port_by_id_int() always returns an internal
  363. * port that does not need to be deallocated
  364. */
  365. control_port = jack_port_by_id_int (client, event->x.self_id,
  366. &need_free);
  367. pthread_mutex_lock (&control_port->connection_lock);
  368. if ((control_port->shared->flags & JackPortIsInput)
  369. && (control_port->connections != NULL)
  370. && (control_port->mix_buffer == NULL) ) {
  371. size_t buffer_size =
  372. jack_port_type_buffer_size( control_port->type_info,
  373. client->engine->buffer_size );
  374. control_port->mix_buffer = jack_pool_alloc (buffer_size);
  375. control_port->fptr.buffer_init (control_port->mix_buffer,
  376. buffer_size,
  377. client->engine->buffer_size);
  378. }
  379. control_port->connections =
  380. jack_slist_prepend (control_port->connections,
  381. (void *) other);
  382. pthread_mutex_unlock (&control_port->connection_lock);
  383. break;
  384. case PortDisconnected:
  385. /* jack_port_by_id_int() always returns an internal
  386. * port that does not need to be deallocated
  387. */
  388. control_port = jack_port_by_id_int (client, event->x.self_id,
  389. &need_free);
  390. pthread_mutex_lock (&control_port->connection_lock);
  391. for (node = control_port->connections; node;
  392. node = jack_slist_next (node)) {
  393. other = (jack_port_t *) node->data;
  394. if (other->shared->id == event->y.other_id) {
  395. control_port->connections =
  396. jack_slist_remove_link (
  397. control_port->connections,
  398. node);
  399. jack_slist_free_1 (node);
  400. free (other);
  401. break;
  402. }
  403. }
  404. pthread_mutex_unlock (&control_port->connection_lock);
  405. break;
  406. default:
  407. /* impossible */
  408. break;
  409. }
  410. }
  411. if (client->control->port_connect_cbset) {
  412. client->port_connect (event->x.self_id, event->y.other_id,
  413. (event->type == PortConnected ? 1 : 0),
  414. client->port_connect_arg);
  415. }
  416. return 0;
  417. }
  418. int
  419. jack_client_handle_session_callback (jack_client_t *client, jack_event_t *event)
  420. {
  421. char prefix[32];
  422. jack_session_event_t *s_event;
  423. if (! client->control->session_cbset) {
  424. return -1;
  425. }
  426. snprintf( prefix, sizeof(prefix), "%d", client->control->uid );
  427. s_event = malloc( sizeof(jack_session_event_t) );
  428. s_event->type = event->y.n;
  429. s_event->session_dir = strdup( event->x.name );
  430. s_event->client_uuid = strdup( prefix );
  431. s_event->command_line = NULL;
  432. s_event->future = 0;
  433. client->session_cb_immediate_reply = 0;
  434. client->session_cb ( s_event, client->session_cb_arg);
  435. if (client->session_cb_immediate_reply) {
  436. return 2;
  437. }
  438. return 1;
  439. }
  440. #if JACK_USE_MACH_THREADS
  441. static int
  442. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  443. {
  444. client->pollmax = 1;
  445. /* If the client registered its own callback for graph order events,
  446. execute it now.
  447. */
  448. if (client->control->graph_order_cbset) {
  449. client->graph_order (client->graph_order_arg);
  450. }
  451. return 0;
  452. }
  453. #else
  454. static int
  455. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  456. {
  457. char path[PATH_MAX+1];
  458. DEBUG ("graph reorder\n");
  459. if (client->graph_wait_fd >= 0) {
  460. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  461. close (client->graph_wait_fd);
  462. client->graph_wait_fd = -1;
  463. }
  464. if (client->graph_next_fd >= 0) {
  465. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  466. close (client->graph_next_fd);
  467. client->graph_next_fd = -1;
  468. }
  469. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  470. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  471. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  472. path, strerror (errno));
  473. return -1;
  474. }
  475. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  476. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  477. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  478. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  479. path, strerror (errno));
  480. return -1;
  481. }
  482. client->upstream_is_jackd = event->y.n;
  483. client->pollmax = 2;
  484. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  485. client->graph_next_fd, path,
  486. client->upstream_is_jackd);
  487. /* If the client registered its own callback for graph order events,
  488. execute it now.
  489. */
  490. if (client->control->graph_order_cbset) {
  491. client->graph_order (client->graph_order_arg);
  492. }
  493. return 0;
  494. }
  495. #endif
  496. static int
  497. server_connect (const char *server_name)
  498. {
  499. int fd;
  500. struct sockaddr_un addr;
  501. int which = 0;
  502. char server_dir[PATH_MAX+1] = "";
  503. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  504. jack_error ("cannot create client socket (%s)",
  505. strerror (errno));
  506. return -1;
  507. }
  508. //JOQ: temporary debug message
  509. //jack_info ("DEBUG: connecting to `%s' server", server_name);
  510. addr.sun_family = AF_UNIX;
  511. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  512. jack_server_dir (server_name, server_dir) , which);
  513. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  514. close (fd);
  515. return -1;
  516. }
  517. return fd;
  518. }
  519. static int
  520. server_event_connect (jack_client_t *client, const char *server_name)
  521. {
  522. int fd;
  523. struct sockaddr_un addr;
  524. jack_client_connect_ack_request_t req;
  525. jack_client_connect_ack_result_t res;
  526. char server_dir[PATH_MAX+1] = "";
  527. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  528. jack_error ("cannot create client event socket (%s)",
  529. strerror (errno));
  530. return -1;
  531. }
  532. addr.sun_family = AF_UNIX;
  533. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  534. jack_server_dir (server_name,server_dir));
  535. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  536. jack_error ("cannot connect to jack server for events",
  537. strerror (errno));
  538. close (fd);
  539. return -1;
  540. }
  541. req.client_id = client->control->id;
  542. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  543. jack_error ("cannot write event connect request to server (%s)",
  544. strerror (errno));
  545. close (fd);
  546. return -1;
  547. }
  548. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  549. jack_error ("cannot read event connect result from server (%s)",
  550. strerror (errno));
  551. close (fd);
  552. return -1;
  553. }
  554. if (res.status != 0) {
  555. jack_error ("cannot connect to server for event stream (%s)",
  556. strerror (errno));
  557. close (fd);
  558. return -1;
  559. }
  560. return fd;
  561. }
  562. /* Exec the JACK server in this process. Does not return. */
  563. static void
  564. _start_server (const char *server_name)
  565. {
  566. FILE* fp = 0;
  567. char filename[255];
  568. char arguments[255];
  569. char buffer[255];
  570. char* command = 0;
  571. size_t pos = 0;
  572. size_t result = 0;
  573. char** argv = 0;
  574. int i = 0;
  575. int good = 0;
  576. int ret;
  577. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  578. fp = fopen(filename, "r");
  579. if (!fp) {
  580. fp = fopen("/etc/jackdrc", "r");
  581. }
  582. /* if still not found, check old config name for backwards compatability */
  583. if (!fp) {
  584. fp = fopen("/etc/jackd.conf", "r");
  585. }
  586. if (fp) {
  587. arguments[0] = '\0';
  588. ret = fscanf(fp, "%s", buffer);
  589. while(ret != 0 && ret != EOF) {
  590. strcat(arguments, buffer);
  591. strcat(arguments, " ");
  592. ret = fscanf(fp, "%s", buffer);
  593. }
  594. if (strlen(arguments) > 0) {
  595. good = 1;
  596. }
  597. }
  598. if (!good) {
  599. #if defined(USE_CAPABILITIES)
  600. command = JACK_LOCATION "/jackstart";
  601. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  602. JACK_DEFAULT_DRIVER " -p 512", 255);
  603. #else /* !USE_CAPABILITIES */
  604. command = JACK_LOCATION "/jackd";
  605. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  606. JACK_DEFAULT_DRIVER, 255);
  607. #endif /* USE_CAPABILITIES */
  608. } else {
  609. result = strcspn(arguments, " ");
  610. if ((command = (char *) malloc(result+1)) == NULL) {
  611. goto failure;
  612. }
  613. strncpy(command, arguments, result);
  614. command[result] = '\0';
  615. }
  616. if ((argv = (char **) malloc (255)) == NULL) {
  617. goto failure;
  618. }
  619. while(1) {
  620. /* insert -T and -nserver_name in front of arguments */
  621. if (i == 1) {
  622. argv[i] = (char *) malloc(strlen ("-T") + 1);
  623. strcpy (argv[i++], "-T");
  624. if (server_name) {
  625. size_t optlen = strlen ("-n");
  626. char *buf =
  627. malloc (optlen
  628. + strlen (server_name) + 1);
  629. strcpy (buf, "-n");
  630. strcpy (buf+optlen, server_name);
  631. argv[i++] = buf;
  632. }
  633. }
  634. result = strcspn(arguments+pos, " ");
  635. if (result == 0) {
  636. break;
  637. }
  638. argv[i] = (char*)malloc(result+1);
  639. strncpy(argv[i], arguments+pos, result);
  640. argv[i][result] = '\0';
  641. pos += result+1;
  642. ++i;
  643. }
  644. argv[i] = 0;
  645. #if 0
  646. fprintf (stderr, "execing JACK using %s\n", command);
  647. for (_xx = 0; argv[_xx]; ++_xx) {
  648. fprintf (stderr, "\targv[%d] = %s\n", _xx, argv[_xx]);
  649. }
  650. #endif
  651. execv (command, argv);
  652. failure:
  653. /* If execv() succeeds, it does not return. There's no point
  654. * in calling jack_error() here in the child process. */
  655. fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
  656. }
  657. int
  658. start_server (const char *server_name, jack_options_t options)
  659. {
  660. if ((options & JackNoStartServer)
  661. || getenv("JACK_NO_START_SERVER")) {
  662. return 1;
  663. }
  664. /* The double fork() forces the server to become a child of
  665. * init, which will always clean up zombie process state on
  666. * termination. This even works in cases where the server
  667. * terminates but this client does not.
  668. *
  669. * Since fork() is usually implemented using copy-on-write
  670. * virtual memory tricks, the overhead of the second fork() is
  671. * probably relatively small.
  672. */
  673. switch (fork()) {
  674. case 0: /* child process */
  675. switch (fork()) {
  676. case 0: /* grandchild process */
  677. _start_server(server_name);
  678. _exit (99); /* exec failed */
  679. case -1:
  680. _exit (98);
  681. default:
  682. _exit (0);
  683. }
  684. case -1: /* fork() error */
  685. return 1; /* failed to start server */
  686. }
  687. /* only the original parent process goes here */
  688. return 0; /* (probably) successful */
  689. }
  690. static int
  691. jack_request_client (ClientType type,
  692. const char* client_name, jack_options_t options,
  693. jack_status_t *status, jack_varargs_t *va,
  694. jack_client_connect_result_t *res, int *req_fd)
  695. {
  696. jack_client_connect_request_t req;
  697. *req_fd = -1;
  698. memset (&req, 0, sizeof (req));
  699. req.options = options;
  700. if (strlen (client_name) >= sizeof (req.name)) {
  701. jack_error ("\"%s\" is too long to be used as a JACK client"
  702. " name.\n"
  703. "Please use %lu characters or less.",
  704. client_name, sizeof (req.name));
  705. return -1;
  706. }
  707. if (va->load_name
  708. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  709. jack_error ("\"%s\" is too long to be used as a JACK shared"
  710. " object name.\n"
  711. "Please use %lu characters or less.",
  712. va->load_name, sizeof (req.object_path) - 1);
  713. return -1;
  714. }
  715. if (va->load_init
  716. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  717. jack_error ("\"%s\" is too long to be used as a JACK shared"
  718. " object data string.\n"
  719. "Please use %lu characters or less.",
  720. va->load_init, sizeof (req.object_data) - 1);
  721. return -1;
  722. }
  723. if ((*req_fd = server_connect (va->server_name)) < 0) {
  724. int trys;
  725. if (start_server(va->server_name, options)) {
  726. *status |= (JackFailure|JackServerFailed);
  727. goto fail;
  728. }
  729. trys = 5;
  730. do {
  731. sleep(1);
  732. if (--trys < 0) {
  733. *status |= (JackFailure|JackServerFailed);
  734. goto fail;
  735. }
  736. } while ((*req_fd = server_connect (va->server_name)) < 0);
  737. *status |= JackServerStarted;
  738. }
  739. /* format connection request */
  740. if( va->sess_uuid )
  741. req.uuid = atoi( va->sess_uuid );
  742. else
  743. req.uuid = 0;
  744. req.protocol_v = jack_protocol_version;
  745. req.load = TRUE;
  746. req.type = type;
  747. snprintf (req.name, sizeof (req.name),
  748. "%s", client_name);
  749. snprintf (req.object_path, sizeof (req.object_path),
  750. "%s", va->load_name);
  751. snprintf (req.object_data, sizeof (req.object_data),
  752. "%s", va->load_init);
  753. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  754. jack_error ("cannot send request to jack server (%s)",
  755. strerror (errno));
  756. *status |= (JackFailure|JackServerError);
  757. goto fail;
  758. }
  759. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  760. if (errno == 0) {
  761. /* server shut the socket */
  762. jack_error ("could not attach as client");
  763. *status |= (JackFailure|JackServerError);
  764. goto fail;
  765. }
  766. if (errno == ECONNRESET) {
  767. jack_error ("could not attach as JACK client "
  768. "(server has exited)");
  769. *status |= (JackFailure|JackServerError);
  770. goto fail;
  771. }
  772. jack_error ("cannot read response from jack server (%s)",
  773. strerror (errno));
  774. *status |= (JackFailure|JackServerError);
  775. goto fail;
  776. }
  777. *status |= res->status; /* return server status bits */
  778. if (*status & JackFailure) {
  779. if (*status & JackVersionError) {
  780. jack_error ("client linked with incompatible libjack"
  781. " version.");
  782. }
  783. jack_error ("could not attach to JACK server");
  784. *status |= JackServerError;
  785. goto fail;
  786. }
  787. switch (type) {
  788. case ClientDriver:
  789. case ClientInternal:
  790. close (*req_fd);
  791. *req_fd = -1;
  792. break;
  793. default:
  794. break;
  795. }
  796. return 0;
  797. fail:
  798. if (*req_fd >= 0) {
  799. close (*req_fd);
  800. *req_fd = -1;
  801. }
  802. return -1;
  803. }
  804. int
  805. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  806. {
  807. /* Lookup, attach and register the port/buffer segments in use
  808. * right now.
  809. */
  810. if (client->control->type != ClientExternal) {
  811. jack_error("Only external clients need attach port segments");
  812. abort();
  813. }
  814. /* make sure we have space to store the port
  815. segment information.
  816. */
  817. if (ptid >= client->n_port_types) {
  818. client->port_segment = (jack_shm_info_t*)
  819. realloc (client->port_segment,
  820. sizeof (jack_shm_info_t) * (ptid+1));
  821. memset (&client->port_segment[client->n_port_types],
  822. 0,
  823. sizeof (jack_shm_info_t) *
  824. (ptid - client->n_port_types));
  825. client->n_port_types = ptid + 1;
  826. } else {
  827. /* release any previous segment */
  828. jack_release_shm (&client->port_segment[ptid]);
  829. }
  830. /* get the index into the shm registry */
  831. client->port_segment[ptid].index =
  832. client->engine->port_types[ptid].shm_registry_index;
  833. /* attach the relevant segment */
  834. if (jack_attach_shm (&client->port_segment[ptid])) {
  835. jack_error ("cannot attach port segment shared memory"
  836. " (%s)", strerror (errno));
  837. return -1;
  838. }
  839. return 0;
  840. }
  841. jack_client_t *
  842. jack_client_open_aux (const char *client_name,
  843. jack_options_t options,
  844. jack_status_t *status, va_list ap)
  845. {
  846. /* optional arguments: */
  847. jack_varargs_t va; /* variable arguments */
  848. int req_fd = -1;
  849. int ev_fd = -1;
  850. jack_client_connect_result_t res;
  851. jack_client_t *client;
  852. jack_port_type_id_t ptid;
  853. jack_status_t my_status;
  854. jack_messagebuffer_init ();
  855. if (status == NULL) /* no status from caller? */
  856. status = &my_status; /* use local status word */
  857. *status = 0;
  858. /* validate parameters */
  859. if ((options & ~JackOpenOptions)) {
  860. *status |= (JackFailure|JackInvalidOption);
  861. jack_messagebuffer_exit ();
  862. return NULL;
  863. }
  864. /* parse variable arguments */
  865. jack_varargs_parse(options, ap, &va);
  866. /* External clients need to know where the tmpdir used for
  867. communication with the server lives
  868. */
  869. if (jack_get_tmpdir ()) {
  870. *status |= JackFailure;
  871. jack_messagebuffer_exit ();
  872. return NULL;
  873. }
  874. /* External clients need this initialized. It is already set
  875. * up in the server's address space for internal clients.
  876. */
  877. jack_init_time ();
  878. if (jack_request_client (ClientExternal, client_name, options, status,
  879. &va, &res, &req_fd)) {
  880. jack_messagebuffer_exit ();
  881. return NULL;
  882. }
  883. /* Allocate the jack_client_t structure in local memory.
  884. * Shared memory is not accessible yet. */
  885. client = jack_client_alloc ();
  886. strcpy (client->name, res.name);
  887. strcpy (client->fifo_prefix, res.fifo_prefix);
  888. client->request_fd = req_fd;
  889. client->pollfd[EVENT_POLL_INDEX].events =
  890. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  891. #ifndef JACK_USE_MACH_THREADS
  892. client->pollfd[WAIT_POLL_INDEX].events =
  893. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  894. #endif
  895. /* Don't access shared memory until server connected. */
  896. if (jack_initialize_shm (va.server_name)) {
  897. jack_error ("Unable to initialize shared memory.");
  898. *status |= (JackFailure|JackShmFailure);
  899. goto fail;
  900. }
  901. /* attach the engine control/info block */
  902. client->engine_shm.index = res.engine_shm_index;
  903. if (jack_attach_shm (&client->engine_shm)) {
  904. jack_error ("cannot attached engine control shared memory"
  905. " segment");
  906. goto fail;
  907. }
  908. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  909. /* initialize clock source as early as possible */
  910. jack_set_clock_source (client->engine->clock_source);
  911. /* now attach the client control block */
  912. client->control_shm.index = res.client_shm_index;
  913. if (jack_attach_shm (&client->control_shm)) {
  914. jack_error ("cannot attached client control shared memory"
  915. " segment");
  916. goto fail;
  917. }
  918. client->control = (jack_client_control_t *)
  919. jack_shm_addr (&client->control_shm);
  920. /* Nobody else needs to access this shared memory any more, so
  921. * destroy it. Because we have it attached, it won't vanish
  922. * till we exit (and release it).
  923. */
  924. jack_destroy_shm (&client->control_shm);
  925. client->n_port_types = client->engine->n_port_types;
  926. if ((client->port_segment = (jack_shm_info_t *) malloc (sizeof (jack_shm_info_t) * client->n_port_types)) == NULL) {
  927. goto fail;
  928. }
  929. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  930. client->port_segment[ptid].index =
  931. client->engine->port_types[ptid].shm_registry_index;
  932. client->port_segment[ptid].attached_at = MAP_FAILED;
  933. jack_attach_port_segment (client, ptid);
  934. }
  935. /* set up the client so that it does the right thing for an
  936. * external client
  937. */
  938. client->deliver_request = oop_client_deliver_request;
  939. client->deliver_arg = client;
  940. if( va.sess_uuid )
  941. client->control->uid = atoi( va.sess_uuid );
  942. else
  943. client->control->uid = 0U;
  944. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  945. goto fail;
  946. }
  947. client->event_fd = ev_fd;
  948. #ifdef JACK_USE_MACH_THREADS
  949. /* specific resources for server/client real-time thread
  950. * communication */
  951. client->clienttask = mach_task_self();
  952. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  953. jack_error ("Can't find bootstrap port");
  954. goto fail;
  955. }
  956. if (allocate_mach_clientport(client, res.portnum) < 0) {
  957. jack_error("Can't allocate mach port");
  958. goto fail;
  959. };
  960. #endif /* JACK_USE_MACH_THREADS */
  961. return client;
  962. fail:
  963. jack_messagebuffer_exit ();
  964. if (client->engine) {
  965. jack_release_shm (&client->engine_shm);
  966. client->engine = 0;
  967. }
  968. if (client->control) {
  969. jack_release_shm (&client->control_shm);
  970. client->control = 0;
  971. }
  972. if (req_fd >= 0) {
  973. close (req_fd);
  974. }
  975. if (ev_fd >= 0) {
  976. close (ev_fd);
  977. }
  978. free (client);
  979. return NULL;
  980. }
  981. jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  982. {
  983. va_list ap;
  984. va_start(ap, status);
  985. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  986. va_end(ap);
  987. return res;
  988. }
  989. jack_client_t *
  990. jack_client_new (const char *client_name)
  991. {
  992. jack_options_t options = JackUseExactName;
  993. if (getenv("JACK_START_SERVER") == NULL)
  994. options |= JackNoStartServer;
  995. return jack_client_open (client_name, options, NULL);
  996. }
  997. char *
  998. jack_get_client_name (jack_client_t *client)
  999. {
  1000. return client->name;
  1001. }
  1002. int
  1003. jack_internal_client_new (const char *client_name,
  1004. const char *so_name, const char *so_data)
  1005. {
  1006. jack_client_connect_result_t res;
  1007. int req_fd;
  1008. jack_varargs_t va;
  1009. jack_status_t status;
  1010. jack_options_t options = JackUseExactName;
  1011. if (getenv("JACK_START_SERVER") == NULL)
  1012. options |= JackNoStartServer;
  1013. jack_varargs_init (&va);
  1014. va.load_name = (char *) so_name;
  1015. va.load_init = (char *) so_data;
  1016. return jack_request_client (ClientInternal, client_name,
  1017. options, &status, &va, &res, &req_fd);
  1018. }
  1019. char *
  1020. jack_default_server_name (void)
  1021. {
  1022. char *server_name;
  1023. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  1024. server_name = "default";
  1025. return server_name;
  1026. }
  1027. /* returns the name of the per-user subdirectory of jack_tmpdir */
  1028. char *
  1029. jack_user_dir (void)
  1030. {
  1031. static char user_dir[PATH_MAX+1] = "";
  1032. /* format the path name on the first call */
  1033. if (user_dir[0] == '\0') {
  1034. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  1035. snprintf (user_dir, sizeof (user_dir), "%s/jack",
  1036. jack_tmpdir);
  1037. } else {
  1038. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  1039. jack_tmpdir, getuid ());
  1040. }
  1041. }
  1042. return user_dir;
  1043. }
  1044. /* returns the name of the per-server subdirectory of jack_user_dir() */
  1045. char *
  1046. jack_server_dir (const char *server_name, char *server_dir)
  1047. {
  1048. /* format the path name into the suppled server_dir char array,
  1049. * assuming that server_dir is at least as large as PATH_MAX+1 */
  1050. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  1051. jack_user_dir (), server_name);
  1052. return server_dir;
  1053. }
  1054. void
  1055. jack_internal_client_close (const char *client_name)
  1056. {
  1057. jack_client_connect_request_t req;
  1058. int fd;
  1059. char *server_name = jack_default_server_name ();
  1060. req.load = FALSE;
  1061. snprintf (req.name, sizeof (req.name), "%s", client_name);
  1062. if ((fd = server_connect (server_name)) < 0) {
  1063. return;
  1064. }
  1065. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  1066. jack_error ("cannot deliver ClientUnload request to JACK "
  1067. "server.");
  1068. }
  1069. /* no response to this request */
  1070. close (fd);
  1071. return;
  1072. }
  1073. int
  1074. jack_recompute_total_latencies (jack_client_t* client)
  1075. {
  1076. jack_request_t request;
  1077. VALGRIND_MEMSET (&request, 0, sizeof (request));
  1078. request.type = RecomputeTotalLatencies;
  1079. return jack_client_deliver_request (client, &request);
  1080. }
  1081. int
  1082. jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
  1083. {
  1084. jack_request_t request;
  1085. VALGRIND_MEMSET (&request, 0, sizeof (request));
  1086. request.type = RecomputeTotalLatency;
  1087. request.x.port_info.port_id = port->shared->id;
  1088. return jack_client_deliver_request (client, &request);
  1089. }
  1090. int
  1091. jack_set_freewheel (jack_client_t* client, int onoff)
  1092. {
  1093. jack_request_t request;
  1094. VALGRIND_MEMSET (&request, 0, sizeof (request));
  1095. request.type = onoff ? FreeWheel : StopFreeWheel;
  1096. request.x.client_id = client->control->id;
  1097. return jack_client_deliver_request (client, &request);
  1098. }
  1099. int
  1100. jack_session_reply (jack_client_t *client, jack_session_event_t *event )
  1101. {
  1102. int retval = 0;
  1103. if (event->command_line) {
  1104. snprintf ((char *)client->control->session_command,
  1105. sizeof(client->control->session_command),
  1106. "%s", event->command_line);
  1107. client->control->session_flags = event->flags;
  1108. } else {
  1109. retval = -1;
  1110. }
  1111. if (pthread_self() == client->thread_id) {
  1112. client->session_cb_immediate_reply = 1;
  1113. } else {
  1114. jack_request_t request;
  1115. VALGRIND_MEMSET (&request, 0, sizeof (request));
  1116. request.type = SessionReply;
  1117. request.x.client_id = client->control->id;
  1118. retval = jack_client_deliver_request(client, &request);
  1119. }
  1120. return retval;
  1121. }
  1122. void
  1123. jack_session_event_free (jack_session_event_t *event)
  1124. {
  1125. if (event->command_line)
  1126. free (event->command_line);
  1127. free ((char *)event->session_dir);
  1128. free ((char *)event->client_uuid);
  1129. free (event);
  1130. }
  1131. void
  1132. jack_session_commands_free (jack_session_command_t *cmds)
  1133. {
  1134. int i=0;
  1135. while(1) {
  1136. if (cmds[i].client_name)
  1137. free ((char *)cmds[i].client_name);
  1138. if (cmds[i].command)
  1139. free ((char *)cmds[i].command);
  1140. if (cmds[i].uuid)
  1141. free ((char *)cmds[i].uuid);
  1142. else
  1143. break;
  1144. i += 1;
  1145. }
  1146. free(cmds);
  1147. }
  1148. jack_session_command_t *
  1149. jack_session_notify (jack_client_t* client, const char *target, jack_session_event_type_t code, const char *path )
  1150. {
  1151. jack_request_t request;
  1152. jack_session_command_t *retval = NULL;
  1153. int num_replies = 0;
  1154. VALGRIND_MEMSET (&request, 0, sizeof (request));
  1155. request.type = SessionNotify;
  1156. if( path )
  1157. snprintf( request.x.session.path, sizeof( request.x.session.path ), "%s", path );
  1158. else
  1159. request.x.session.path[0] = '\0';
  1160. if( target )
  1161. snprintf( request.x.session.target, sizeof( request.x.session.target ), "%s", target );
  1162. else
  1163. request.x.session.target[0] = '\0';
  1164. request.x.session.type = code;
  1165. if( (write (client->request_fd, &request, sizeof (request))
  1166. != sizeof (request)) ) {
  1167. jack_error ("cannot send request type %d to server",
  1168. request.type);
  1169. goto out;
  1170. }
  1171. while( 1 ) {
  1172. jack_client_id_t uid;
  1173. if (read (client->request_fd, &uid, sizeof (uid)) != sizeof (uid)) {
  1174. jack_error ("cannot read result for request type %d from"
  1175. " server (%s)", request.type, strerror (errno));
  1176. goto out;
  1177. }
  1178. num_replies += 1;
  1179. retval = realloc( retval, (num_replies)*sizeof(jack_session_command_t) );
  1180. retval[num_replies-1].client_name = malloc (JACK_CLIENT_NAME_SIZE);
  1181. retval[num_replies-1].command = malloc (JACK_PORT_NAME_SIZE);
  1182. retval[num_replies-1].uuid = malloc (16);
  1183. if ( (retval[num_replies-1].client_name == NULL)
  1184. ||(retval[num_replies-1].command == NULL)
  1185. ||(retval[num_replies-1].uuid == NULL) )
  1186. goto out;
  1187. if( uid == 0 )
  1188. break;
  1189. if (read (client->request_fd, (char *)retval[num_replies-1].client_name, JACK_CLIENT_NAME_SIZE)
  1190. != JACK_CLIENT_NAME_SIZE) {
  1191. jack_error ("cannot read result for request type %d from"
  1192. " server (%s)", request.type, strerror (errno));
  1193. goto out;
  1194. }
  1195. if (read (client->request_fd, (char *)retval[num_replies-1].command, JACK_PORT_NAME_SIZE)
  1196. != JACK_PORT_NAME_SIZE) {
  1197. jack_error ("cannot read result for request type %d from"
  1198. " server (%s)", request.type, strerror (errno));
  1199. goto out;
  1200. }
  1201. if (read (client->request_fd, & retval[num_replies-1].flags, sizeof(retval[num_replies-1].flags) )
  1202. != sizeof(retval[num_replies-1].flags) ) {
  1203. jack_error ("cannot read result for request type %d from"
  1204. " server (%s)", request.type, strerror (errno));
  1205. goto out;
  1206. }
  1207. snprintf( (char *)retval[num_replies-1].uuid, 16, "%d", uid );
  1208. }
  1209. free((char *)retval[num_replies-1].uuid);
  1210. retval[num_replies-1].uuid = NULL;
  1211. retval[num_replies-1].client_name = NULL;
  1212. retval[num_replies-1].command = NULL;
  1213. return retval;
  1214. out:
  1215. if( retval )
  1216. jack_session_commands_free(retval);
  1217. return NULL;
  1218. }
  1219. void
  1220. jack_start_freewheel (jack_client_t* client)
  1221. {
  1222. jack_client_control_t *control = client->control;
  1223. if (client->engine->real_time) {
  1224. #if JACK_USE_MACH_THREADS
  1225. jack_drop_real_time_scheduling (client->process_thread);
  1226. #else
  1227. jack_drop_real_time_scheduling (client->thread);
  1228. #endif
  1229. }
  1230. if (control->freewheel_cb_cbset) {
  1231. client->freewheel_cb (1, client->freewheel_arg);
  1232. }
  1233. }
  1234. void
  1235. jack_stop_freewheel (jack_client_t* client)
  1236. {
  1237. jack_client_control_t *control = client->control;
  1238. if (client->engine->real_time) {
  1239. #if JACK_USE_MACH_THREADS
  1240. jack_acquire_real_time_scheduling (client->process_thread,
  1241. client->engine->client_priority);
  1242. #else
  1243. jack_acquire_real_time_scheduling (client->thread,
  1244. client->engine->client_priority);
  1245. #endif
  1246. }
  1247. if (control->freewheel_cb_cbset) {
  1248. client->freewheel_cb (0, client->freewheel_arg);
  1249. }
  1250. }
  1251. static void
  1252. jack_client_thread_suicide (jack_client_t* client)
  1253. {
  1254. if (client->on_info_shutdown) {
  1255. jack_error ("zombified - calling shutdown handler");
  1256. client->on_info_shutdown (JackClientZombie, "Zombified", client->on_info_shutdown_arg);
  1257. } else if (client->on_shutdown) {
  1258. jack_error ("zombified - calling shutdown handler");
  1259. client->on_shutdown (client->on_shutdown_arg);
  1260. } else {
  1261. jack_error ("jack_client_thread zombified - exiting from JACK");
  1262. jack_client_close_aux (client);
  1263. /* Need a fix : possibly make client crash if
  1264. * zombified without shutdown handler
  1265. */
  1266. }
  1267. pthread_exit (0);
  1268. /*NOTREACHED*/
  1269. }
  1270. static int
  1271. jack_client_process_events (jack_client_t* client)
  1272. {
  1273. jack_event_t event;
  1274. char status = 0;
  1275. jack_client_control_t *control = client->control;
  1276. JSList *node;
  1277. jack_port_t* port;
  1278. DEBUG ("process events");
  1279. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1280. DEBUG ("client receives an event, "
  1281. "now reading on event fd");
  1282. /* server has sent us an event. process the
  1283. * event and reply */
  1284. if (read (client->event_fd, &event, sizeof (event))
  1285. != sizeof (event)) {
  1286. jack_error ("cannot read server event (%s)",
  1287. strerror (errno));
  1288. return -1;
  1289. }
  1290. status = 0;
  1291. switch (event.type) {
  1292. case PortRegistered:
  1293. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1294. port = node->data;
  1295. if (port->shared->id == event.x.port_id) { // Found port, update port type
  1296. port->type_info = &client->engine->port_types[port->shared->ptype_id];
  1297. }
  1298. }
  1299. if (control->port_register_cbset) {
  1300. client->port_register
  1301. (event.x.port_id, TRUE,
  1302. client->port_register_arg);
  1303. }
  1304. break;
  1305. case PortUnregistered:
  1306. if (control->port_register_cbset) {
  1307. client->port_register
  1308. (event.x.port_id, FALSE,
  1309. client->port_register_arg);
  1310. }
  1311. break;
  1312. case ClientRegistered:
  1313. if (control->client_register_cbset) {
  1314. client->client_register
  1315. (event.x.name, TRUE,
  1316. client->client_register_arg);
  1317. }
  1318. break;
  1319. case ClientUnregistered:
  1320. if (control->client_register_cbset) {
  1321. client->client_register
  1322. (event.x.name, FALSE,
  1323. client->client_register_arg);
  1324. }
  1325. break;
  1326. case GraphReordered:
  1327. status = jack_handle_reorder (client, &event);
  1328. break;
  1329. case PortConnected:
  1330. case PortDisconnected:
  1331. status = jack_client_handle_port_connection
  1332. (client, &event);
  1333. break;
  1334. case BufferSizeChange:
  1335. jack_client_fix_port_buffers (client);
  1336. if (control->bufsize_cbset) {
  1337. status = client->bufsize
  1338. (control->nframes,
  1339. client->bufsize_arg);
  1340. }
  1341. break;
  1342. case SampleRateChange:
  1343. if (control->srate_cbset) {
  1344. status = client->srate
  1345. (control->nframes,
  1346. client->srate_arg);
  1347. }
  1348. break;
  1349. case XRun:
  1350. if (control->xrun_cbset) {
  1351. status = client->xrun
  1352. (client->xrun_arg);
  1353. }
  1354. break;
  1355. case AttachPortSegment:
  1356. jack_attach_port_segment (client, event.y.ptid);
  1357. break;
  1358. case StartFreewheel:
  1359. jack_start_freewheel (client);
  1360. break;
  1361. case StopFreewheel:
  1362. jack_stop_freewheel (client);
  1363. break;
  1364. case SaveSession:
  1365. status = jack_client_handle_session_callback (client, &event );
  1366. break;
  1367. }
  1368. DEBUG ("client has dealt with the event, writing "
  1369. "response on event fd");
  1370. if (write (client->event_fd, &status, sizeof (status))
  1371. != sizeof (status)) {
  1372. jack_error ("cannot send event response to "
  1373. "engine (%s)", strerror (errno));
  1374. return -1;
  1375. }
  1376. }
  1377. return 0;
  1378. }
  1379. static int
  1380. jack_client_core_wait (jack_client_t* client)
  1381. {
  1382. jack_client_control_t *control = client->control;
  1383. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1384. "event_fd and graph_wait_fd..." :
  1385. "event_fd only");
  1386. while (1) {
  1387. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1388. if (errno == EINTR) {
  1389. continue;
  1390. }
  1391. jack_error ("poll failed in client (%s)",
  1392. strerror (errno));
  1393. return -1;
  1394. }
  1395. pthread_testcancel();
  1396. #ifndef JACK_USE_MACH_THREADS
  1397. /* get an accurate timestamp on waking from poll for a
  1398. * process() cycle.
  1399. */
  1400. if (client->graph_wait_fd >= 0
  1401. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1402. control->awake_at = jack_get_microseconds();
  1403. }
  1404. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1405. client->pollfd[EVENT_POLL_INDEX].revents,
  1406. client->pollfd[WAIT_POLL_INDEX].revents);
  1407. if (client->graph_wait_fd >= 0 &&
  1408. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1409. /* our upstream "wait" connection
  1410. closed, which either means that
  1411. an intermediate client exited, or
  1412. jackd exited, or jackd zombified
  1413. us.
  1414. we can discover the zombification
  1415. via client->control->dead, but
  1416. the other two possibilities are
  1417. impossible to identify just from
  1418. this situation. so we have to
  1419. check what we are connected to,
  1420. and act accordingly.
  1421. */
  1422. if (client->upstream_is_jackd) {
  1423. DEBUG ("WE DIE\n");
  1424. return 0;
  1425. } else {
  1426. DEBUG ("WE PUNT\n");
  1427. /* don't poll on the wait fd
  1428. * again until we get a
  1429. * GraphReordered event.
  1430. */
  1431. client->graph_wait_fd = -1;
  1432. client->pollmax = 1;
  1433. }
  1434. }
  1435. #endif
  1436. if (jack_client_process_events (client)) {
  1437. DEBUG ("event processing failed\n");
  1438. return 0;
  1439. }
  1440. #ifndef JACK_USE_MACH_THREADS
  1441. if (client->graph_wait_fd >= 0 &&
  1442. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1443. DEBUG ("time to run process()\n");
  1444. break;
  1445. }
  1446. #endif
  1447. }
  1448. if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1449. DEBUG ("client appears dead or event pollfd has error status\n");
  1450. return -1;
  1451. }
  1452. return 0;
  1453. }
  1454. static int
  1455. jack_wake_next_client (jack_client_t* client)
  1456. {
  1457. struct pollfd pfds[1];
  1458. int pret = 0;
  1459. char c = 0;
  1460. if (write (client->graph_next_fd, &c, sizeof (c))
  1461. != sizeof (c)) {
  1462. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1463. jack_error ("cannot continue execution of the "
  1464. "processing graph (%s)",
  1465. strerror(errno));
  1466. return -1;
  1467. }
  1468. DEBUG ("client sent message to next stage by %" PRIu64 "",
  1469. jack_get_microseconds());
  1470. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1471. /* "upstream client went away? readability is checked in
  1472. * jack_client_core_wait(), but that's almost a whole cycle
  1473. * before we get here.
  1474. */
  1475. if (client->graph_wait_fd >= 0) {
  1476. pfds[0].fd = client->graph_wait_fd;
  1477. pfds[0].events = POLLIN;
  1478. /* 0 timeout, don't actually wait */
  1479. pret = poll(pfds, 1, 0);
  1480. }
  1481. if (pret > 0 && (pfds[0].revents & POLLIN)) {
  1482. if (read (client->graph_wait_fd, &c, sizeof (c))
  1483. != sizeof (c)) {
  1484. jack_error ("cannot complete execution of the "
  1485. "processing graph (%s)", strerror(errno));
  1486. return -1;
  1487. }
  1488. } else {
  1489. DEBUG("cleanup byte from pipe %d not available?\n",
  1490. client->graph_wait_fd);
  1491. }
  1492. return 0;
  1493. }
  1494. static jack_nframes_t
  1495. jack_thread_first_wait (jack_client_t* client)
  1496. {
  1497. if (jack_client_core_wait (client)) {
  1498. return 0;
  1499. }
  1500. return client->control->nframes;
  1501. }
  1502. jack_nframes_t
  1503. jack_thread_wait (jack_client_t* client, int status)
  1504. {
  1505. client->control->last_status = status;
  1506. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1507. /* housekeeping/cleanup after data processing */
  1508. if (status == 0 && client->control->timebase_cb_cbset) {
  1509. jack_call_timebase_master (client);
  1510. }
  1511. /* end preemption checking */
  1512. CHECK_PREEMPTION (client->engine, FALSE);
  1513. client->control->finished_at = jack_get_microseconds();
  1514. /* wake the next client in the chain (could be the server),
  1515. and check if we were killed during the process
  1516. cycle.
  1517. */
  1518. if (jack_wake_next_client (client)) {
  1519. DEBUG("client cannot wake next, or is dead\n");
  1520. return 0;
  1521. }
  1522. if (status || client->control->dead || !client->engine->engine_ok) {
  1523. return 0;
  1524. }
  1525. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1526. if (jack_client_core_wait (client)) {
  1527. return 0;
  1528. }
  1529. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1530. /* Time to do data processing */
  1531. client->control->state = Running;
  1532. /* begin preemption checking */
  1533. CHECK_PREEMPTION (client->engine, TRUE);
  1534. if (client->control->sync_cb_cbset)
  1535. jack_call_sync_client (client);
  1536. return client->control->nframes;
  1537. }
  1538. jack_nframes_t jack_cycle_wait (jack_client_t* client)
  1539. {
  1540. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1541. if (jack_client_core_wait (client)) {
  1542. return 0;
  1543. }
  1544. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1545. /* Time to do data processing */
  1546. client->control->state = Running;
  1547. /* begin preemption checking */
  1548. CHECK_PREEMPTION (client->engine, TRUE);
  1549. if (client->control->sync_cb_cbset)
  1550. jack_call_sync_client (client);
  1551. return client->control->nframes;
  1552. }
  1553. void jack_cycle_signal(jack_client_t* client, int status)
  1554. {
  1555. client->control->last_status = status;
  1556. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1557. /* housekeeping/cleanup after data processing */
  1558. if (status == 0 && client->control->timebase_cb_cbset) {
  1559. jack_call_timebase_master (client);
  1560. }
  1561. /* end preemption checking */
  1562. CHECK_PREEMPTION (client->engine, FALSE);
  1563. client->control->finished_at = jack_get_microseconds();
  1564. /* wake the next client in the chain (could be the server),
  1565. and check if we were killed during the process
  1566. cycle.
  1567. */
  1568. if (jack_wake_next_client (client)) {
  1569. DEBUG("client cannot wake next, or is dead\n");
  1570. jack_client_thread_suicide (client);
  1571. /*NOTREACHED*/
  1572. }
  1573. if (status || client->control->dead || !client->engine->engine_ok) {
  1574. jack_client_thread_suicide (client);
  1575. /*NOTREACHED*/
  1576. }
  1577. }
  1578. static void
  1579. jack_client_thread_aux (void *arg)
  1580. {
  1581. jack_client_t *client = (jack_client_t *) arg;
  1582. jack_client_control_t *control = client->control;
  1583. pthread_mutex_lock (&client_lock);
  1584. client->thread_ok = TRUE;
  1585. client->thread_id = pthread_self();
  1586. pthread_cond_signal (&client_ready);
  1587. pthread_mutex_unlock (&client_lock);
  1588. control->pid = getpid();
  1589. control->pgrp = getpgrp();
  1590. DEBUG ("client thread is now running");
  1591. if (control->thread_init_cbset) {
  1592. DEBUG ("calling client thread init callback");
  1593. client->thread_init (client->thread_init_arg);
  1594. }
  1595. /* wait for first wakeup from server */
  1596. if (jack_thread_first_wait (client) == control->nframes) {
  1597. /* now run till we're done */
  1598. if (control->process_cbset) {
  1599. /* run process callback, then wait... ad-infinitum */
  1600. while (1) {
  1601. DEBUG("client calls process()");
  1602. int status = (client->process (control->nframes,
  1603. client->process_arg) ==
  1604. control->nframes);
  1605. control->state = Finished;
  1606. DEBUG("client leaves process(), re-enters wait");
  1607. if (!jack_thread_wait (client, status)) {
  1608. break;
  1609. }
  1610. DEBUG("client done with wait");
  1611. }
  1612. } else {
  1613. /* no process handling but still need to process events */
  1614. while (jack_thread_wait (client, 0) == control->nframes)
  1615. ;
  1616. }
  1617. }
  1618. jack_client_thread_suicide (client);
  1619. }
  1620. static void*
  1621. jack_client_thread (void *arg)
  1622. {
  1623. jack_client_t *client = (jack_client_t *) arg;
  1624. jack_client_control_t *control = client->control;
  1625. if (client->control->thread_cb_cbset) {
  1626. pthread_mutex_lock (&client_lock);
  1627. client->thread_ok = TRUE;
  1628. client->thread_id = pthread_self();
  1629. pthread_cond_signal (&client_ready);
  1630. pthread_mutex_unlock (&client_lock);
  1631. control->pid = getpid();
  1632. control->pgrp = getpgrp();
  1633. client->thread_cb(client->thread_cb_arg);
  1634. jack_client_thread_suicide(client);
  1635. } else {
  1636. jack_client_thread_aux(arg);
  1637. }
  1638. /*NOTREACHED*/
  1639. return (void *) 0;
  1640. }
  1641. #ifdef JACK_USE_MACH_THREADS
  1642. /* real-time thread : separated from the normal client thread, it will
  1643. * communicate with the server using fast mach RPC mechanism */
  1644. static void *
  1645. jack_client_process_thread (void *arg)
  1646. {
  1647. jack_client_t *client = (jack_client_t *) arg;
  1648. jack_client_control_t *control = client->control;
  1649. int err = 0;
  1650. if (client->control->thread_init_cbset) {
  1651. /* this means that the init callback will be called twice -taybin*/
  1652. DEBUG ("calling client thread init callback");
  1653. client->thread_init (client->thread_init_arg);
  1654. }
  1655. client->control->pid = getpid();
  1656. DEBUG ("client process thread is now running");
  1657. client->rt_thread_ok = TRUE;
  1658. while (err == 0) {
  1659. if (jack_client_suspend(client) < 0) {
  1660. jack_error ("jack_client_process_thread :resume error");
  1661. goto zombie;
  1662. }
  1663. control->awake_at = jack_get_microseconds();
  1664. DEBUG ("client resumed");
  1665. control->state = Running;
  1666. if (control->sync_cb_cbset)
  1667. jack_call_sync_client (client);
  1668. if (control->process_cbset) {
  1669. if (client->process (control->nframes,
  1670. client->process_arg) == 0) {
  1671. control->state = Finished;
  1672. }
  1673. } else {
  1674. control->state = Finished;
  1675. }
  1676. if (control->timebase_cb_cbset)
  1677. jack_call_timebase_master (client);
  1678. control->finished_at = jack_get_microseconds();
  1679. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1680. control->finished_at,
  1681. ((float)(control->finished_at - control->awake_at)));
  1682. /* check if we were killed during the process cycle
  1683. * (or whatever) */
  1684. if (client->control->dead) {
  1685. jack_error ("jack_client_process_thread: "
  1686. "client->control->dead");
  1687. goto zombie;
  1688. }
  1689. DEBUG("process cycle fully complete\n");
  1690. }
  1691. return (void *) ((intptr_t)err);
  1692. zombie:
  1693. jack_error ("jack_client_process_thread : zombified");
  1694. client->rt_thread_ok = FALSE;
  1695. if (client->on_info_shutdown) {
  1696. jack_error ("zombified - calling shutdown handler");
  1697. client->on_info_shutdown (JackClientZombie, "Zombified", client->on_info_shutdown_arg);
  1698. } else if (client->on_shutdown) {
  1699. jack_error ("zombified - calling shutdown handler");
  1700. client->on_shutdown (client->on_shutdown_arg);
  1701. } else {
  1702. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1703. /* Need a fix : possibly make client crash if
  1704. * zombified without shutdown handler */
  1705. jack_client_close_aux (client);
  1706. }
  1707. pthread_exit (0);
  1708. /*NOTREACHED*/
  1709. return 0;
  1710. }
  1711. #endif /* JACK_USE_MACH_THREADS */
  1712. static int
  1713. jack_start_thread (jack_client_t *client)
  1714. {
  1715. if (client->engine->real_time) {
  1716. #ifdef USE_MLOCK
  1717. if (client->engine->do_mlock
  1718. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1719. jack_error ("cannot lock down memory for RT thread "
  1720. "(%s)", strerror (errno));
  1721. #ifdef ENSURE_MLOCK
  1722. return -1;
  1723. #endif /* ENSURE_MLOCK */
  1724. }
  1725. if (client->engine->do_munlock) {
  1726. cleanup_mlock ();
  1727. }
  1728. #endif /* USE_MLOCK */
  1729. }
  1730. #ifdef JACK_USE_MACH_THREADS
  1731. /* Stephane Letz : letz@grame.fr
  1732. On MacOSX, the normal thread does not need to be real-time.
  1733. */
  1734. if (jack_client_create_thread (client,
  1735. &client->thread,
  1736. client->engine->client_priority,
  1737. FALSE,
  1738. jack_client_thread, client)) {
  1739. return -1;
  1740. }
  1741. #else
  1742. if (jack_client_create_thread (client,
  1743. &client->thread,
  1744. client->engine->client_priority,
  1745. client->engine->real_time,
  1746. jack_client_thread, client)) {
  1747. return -1;
  1748. }
  1749. #endif
  1750. #ifdef JACK_USE_MACH_THREADS
  1751. /* a secondary thread that runs the process callback and uses
  1752. ultra-fast Mach primitives for inter-thread signalling.
  1753. XXX in a properly structured JACK, there would be no
  1754. need for this, because we would have client wake up
  1755. methods that encapsulated the underlying mechanism
  1756. used.
  1757. */
  1758. if (jack_client_create_thread(client,
  1759. &client->process_thread,
  1760. client->engine->client_priority,
  1761. client->engine->real_time,
  1762. jack_client_process_thread, client)) {
  1763. return -1;
  1764. }
  1765. #endif /* JACK_USE_MACH_THREADS */
  1766. return 0;
  1767. }
  1768. int
  1769. jack_activate (jack_client_t *client)
  1770. {
  1771. jack_request_t req;
  1772. if (client->control->type == ClientInternal ||
  1773. client->control->type == ClientDriver) {
  1774. goto startit;
  1775. }
  1776. /* get the pid of the client process to pass it to engine */
  1777. client->control->pid = getpid ();
  1778. #ifdef USE_CAPABILITIES
  1779. if (client->engine->has_capabilities != 0 &&
  1780. client->control->pid != 0 && client->engine->real_time != 0) {
  1781. /* we need to ask the engine for realtime capabilities
  1782. before trying to start the realtime thread
  1783. */
  1784. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1785. req.type = SetClientCapabilities;
  1786. req.x.client_id = client->control->id;
  1787. req.x.cap_pid = client->control->pid;
  1788. jack_client_deliver_request (client, &req);
  1789. if (req.status) {
  1790. /* what to do? engine is running realtime, it
  1791. is using capabilities and has them
  1792. (otherwise we would not get an error
  1793. return) but for some reason it could not
  1794. give the client the required capabilities.
  1795. For now, leave the client so that it
  1796. still runs, albeit non-realtime.
  1797. */
  1798. jack_error ("could not receive realtime capabilities, "
  1799. "client will run non-realtime");
  1800. }
  1801. }
  1802. #endif /* USE_CAPABILITIES */
  1803. if (client->first_active) {
  1804. pthread_mutex_init (&client_lock, NULL);
  1805. pthread_cond_init (&client_ready, NULL);
  1806. pthread_mutex_lock (&client_lock);
  1807. if (jack_start_thread (client)) {
  1808. pthread_mutex_unlock (&client_lock);
  1809. return -1;
  1810. }
  1811. pthread_cond_wait (&client_ready, &client_lock);
  1812. pthread_mutex_unlock (&client_lock);
  1813. if (!client->thread_ok) {
  1814. jack_error ("could not start client thread");
  1815. return -1;
  1816. }
  1817. client->first_active = FALSE;
  1818. }
  1819. startit:
  1820. req.type = ActivateClient;
  1821. req.x.client_id = client->control->id;
  1822. return jack_client_deliver_request (client, &req);
  1823. }
  1824. static int
  1825. jack_deactivate_aux (jack_client_t *client)
  1826. {
  1827. jack_request_t req;
  1828. int rc = ESRCH; /* already shut down */
  1829. if (client && client->control) { /* not shut down? */
  1830. rc = 0;
  1831. if (client->control->active) { /* still active? */
  1832. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1833. req.type = DeactivateClient;
  1834. req.x.client_id = client->control->id;
  1835. rc = jack_client_deliver_request (client, &req);
  1836. }
  1837. }
  1838. return rc;
  1839. }
  1840. int
  1841. jack_deactivate (jack_client_t *client)
  1842. {
  1843. return jack_deactivate_aux(client);
  1844. }
  1845. static int
  1846. jack_client_close_aux (jack_client_t *client)
  1847. {
  1848. JSList *node;
  1849. void *status;
  1850. int rc;
  1851. rc = jack_deactivate_aux (client);
  1852. if (rc == ESRCH) { /* already shut down? */
  1853. return rc;
  1854. }
  1855. if (client->control->type == ClientExternal) {
  1856. #if JACK_USE_MACH_THREADS
  1857. if (client->rt_thread_ok) {
  1858. // MacOSX pthread_cancel not implemented in
  1859. // Darwin 5.5, 6.4
  1860. mach_port_t machThread =
  1861. pthread_mach_thread_np (client->process_thread);
  1862. thread_terminate (machThread);
  1863. }
  1864. #endif
  1865. /* stop the thread that communicates with the jack
  1866. * server, only if it was actually running
  1867. */
  1868. if (client->thread_ok){
  1869. pthread_cancel (client->thread);
  1870. pthread_join (client->thread, &status);
  1871. }
  1872. if (client->control) {
  1873. jack_release_shm (&client->control_shm);
  1874. client->control = NULL;
  1875. }
  1876. if (client->engine) {
  1877. jack_release_shm (&client->engine_shm);
  1878. client->engine = NULL;
  1879. }
  1880. if (client->port_segment) {
  1881. jack_port_type_id_t ptid;
  1882. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1883. jack_release_shm (&client->port_segment[ptid]);
  1884. }
  1885. free (client->port_segment);
  1886. client->port_segment = NULL;
  1887. }
  1888. #ifndef JACK_USE_MACH_THREADS
  1889. if (client->graph_wait_fd >= 0) {
  1890. close (client->graph_wait_fd);
  1891. }
  1892. if (client->graph_next_fd >= 0) {
  1893. close (client->graph_next_fd);
  1894. }
  1895. #endif
  1896. close (client->event_fd);
  1897. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1898. jack_error ("could not shutdown client request socket");
  1899. }
  1900. close (client->request_fd);
  1901. }
  1902. for (node = client->ports; node; node = jack_slist_next (node)) {
  1903. free (node->data);
  1904. }
  1905. jack_slist_free (client->ports);
  1906. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1907. free (node->data);
  1908. }
  1909. jack_slist_free (client->ports_ext);
  1910. jack_client_free (client);
  1911. jack_messagebuffer_exit ();
  1912. return rc;
  1913. }
  1914. int
  1915. jack_client_close (jack_client_t *client)
  1916. {
  1917. return jack_client_close_aux(client);
  1918. }
  1919. int
  1920. jack_is_realtime (jack_client_t *client)
  1921. {
  1922. return client->engine->real_time;
  1923. }
  1924. jack_nframes_t
  1925. jack_get_buffer_size (jack_client_t *client)
  1926. {
  1927. return client->engine->buffer_size;
  1928. }
  1929. int
  1930. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1931. {
  1932. #ifdef DO_BUFFER_RESIZE
  1933. jack_request_t req;
  1934. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1935. req.type = SetBufferSize;
  1936. req.x.nframes = nframes;
  1937. return jack_client_deliver_request (client, &req);
  1938. #else
  1939. return ENOSYS;
  1940. #endif /* DO_BUFFER_RESIZE */
  1941. }
  1942. int
  1943. jack_connect (jack_client_t *client, const char *source_port,
  1944. const char *destination_port)
  1945. {
  1946. jack_request_t req;
  1947. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1948. req.type = ConnectPorts;
  1949. snprintf (req.x.connect.source_port,
  1950. sizeof (req.x.connect.source_port), "%s", source_port);
  1951. snprintf (req.x.connect.destination_port,
  1952. sizeof (req.x.connect.destination_port),
  1953. "%s", destination_port);
  1954. return jack_client_deliver_request (client, &req);
  1955. }
  1956. int
  1957. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1958. {
  1959. jack_request_t req;
  1960. pthread_mutex_lock (&port->connection_lock);
  1961. if (port->connections == NULL) {
  1962. pthread_mutex_unlock (&port->connection_lock);
  1963. return 0;
  1964. }
  1965. pthread_mutex_unlock (&port->connection_lock);
  1966. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1967. req.type = DisconnectPort;
  1968. req.x.port_info.port_id = port->shared->id;
  1969. return jack_client_deliver_request (client, &req);
  1970. }
  1971. int
  1972. jack_disconnect (jack_client_t *client, const char *source_port,
  1973. const char *destination_port)
  1974. {
  1975. jack_request_t req;
  1976. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1977. req.type = DisconnectPorts;
  1978. snprintf (req.x.connect.source_port,
  1979. sizeof (req.x.connect.source_port), "%s", source_port);
  1980. snprintf (req.x.connect.destination_port,
  1981. sizeof (req.x.connect.destination_port),
  1982. "%s", destination_port);
  1983. return jack_client_deliver_request (client, &req);
  1984. }
  1985. void
  1986. jack_set_error_function (void (*func) (const char *))
  1987. {
  1988. jack_error_callback = func;
  1989. }
  1990. void
  1991. jack_set_info_function (void (*func) (const char *))
  1992. {
  1993. jack_info_callback = func;
  1994. }
  1995. int
  1996. jack_set_graph_order_callback (jack_client_t *client,
  1997. JackGraphOrderCallback callback, void *arg)
  1998. {
  1999. if (client->control->active) {
  2000. jack_error ("You cannot set callbacks on an active client.");
  2001. return -1;
  2002. }
  2003. client->graph_order = callback;
  2004. client->graph_order_arg = arg;
  2005. client->control->graph_order_cbset = (callback != NULL);
  2006. return 0;
  2007. }
  2008. int jack_set_xrun_callback (jack_client_t *client,
  2009. JackXRunCallback callback, void *arg)
  2010. {
  2011. if (client->control->active) {
  2012. jack_error ("You cannot set callbacks on an active client.");
  2013. return -1;
  2014. }
  2015. client->xrun = callback;
  2016. client->xrun_arg = arg;
  2017. client->control->xrun_cbset = (callback != NULL);
  2018. return 0;
  2019. }
  2020. int
  2021. jack_set_process_callback (jack_client_t *client,
  2022. JackProcessCallback callback, void *arg)
  2023. {
  2024. if (client->control->active) {
  2025. jack_error ("You cannot set callbacks on an active client.");
  2026. return -1;
  2027. }
  2028. if (client->control->thread_cb_cbset) {
  2029. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  2030. return -1;
  2031. }
  2032. client->process_arg = arg;
  2033. client->process = callback;
  2034. client->control->process_cbset = (callback != NULL);
  2035. return 0;
  2036. }
  2037. int
  2038. jack_set_thread_init_callback (jack_client_t *client,
  2039. JackThreadInitCallback callback, void *arg)
  2040. {
  2041. if (client->control->active) {
  2042. jack_error ("You cannot set callbacks on an active client.");
  2043. return -1;
  2044. }
  2045. client->thread_init_arg = arg;
  2046. client->thread_init = callback;
  2047. client->control->thread_init_cbset = (callback != NULL);
  2048. /* make sure that the message buffer thread is initialized too */
  2049. jack_messagebuffer_thread_init (callback, arg);
  2050. return 0;
  2051. }
  2052. int
  2053. jack_set_freewheel_callback (jack_client_t *client,
  2054. JackFreewheelCallback callback, void *arg)
  2055. {
  2056. if (client->control->active) {
  2057. jack_error ("You cannot set callbacks on an active client.");
  2058. return -1;
  2059. }
  2060. client->freewheel_arg = arg;
  2061. client->freewheel_cb = callback;
  2062. client->control->freewheel_cb_cbset = (callback != NULL);
  2063. return 0;
  2064. }
  2065. int
  2066. jack_set_buffer_size_callback (jack_client_t *client,
  2067. JackBufferSizeCallback callback, void *arg)
  2068. {
  2069. client->bufsize_arg = arg;
  2070. client->bufsize = callback;
  2071. client->control->bufsize_cbset = (callback != NULL);
  2072. return 0;
  2073. }
  2074. int
  2075. jack_set_port_registration_callback(jack_client_t *client,
  2076. JackPortRegistrationCallback callback,
  2077. void *arg)
  2078. {
  2079. if (client->control->active) {
  2080. jack_error ("You cannot set callbacks on an active client.");
  2081. return -1;
  2082. }
  2083. client->port_register_arg = arg;
  2084. client->port_register = callback;
  2085. client->control->port_register_cbset = (callback != NULL);
  2086. return 0;
  2087. }
  2088. int
  2089. jack_set_port_connect_callback(jack_client_t *client,
  2090. JackPortConnectCallback callback,
  2091. void *arg)
  2092. {
  2093. if (client->control->active) {
  2094. jack_error ("You cannot set callbacks on an active client.");
  2095. return -1;
  2096. }
  2097. client->port_connect_arg = arg;
  2098. client->port_connect = callback;
  2099. client->control->port_connect_cbset = (callback != NULL);
  2100. return 0;
  2101. }
  2102. int
  2103. jack_set_client_registration_callback(jack_client_t *client,
  2104. JackClientRegistrationCallback callback,
  2105. void *arg)
  2106. {
  2107. if (client->control->active) {
  2108. jack_error ("You cannot set callbacks on an active client.");
  2109. return -1;
  2110. }
  2111. client->client_register_arg = arg;
  2112. client->client_register = callback;
  2113. client->control->client_register_cbset = (callback != NULL);
  2114. return 0;
  2115. }
  2116. int
  2117. jack_set_process_thread(jack_client_t* client, JackThreadCallback callback, void *arg)
  2118. {
  2119. if (client->control->active) {
  2120. jack_error ("You cannot set callbacks on an active client.");
  2121. return -1;
  2122. }
  2123. if (client->control->process_cbset) {
  2124. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  2125. return -1;
  2126. }
  2127. client->thread_cb_arg = arg;
  2128. client->thread_cb = callback;
  2129. client->control->thread_cb_cbset = (callback != NULL);
  2130. return 0;
  2131. }
  2132. int
  2133. jack_set_session_callback(jack_client_t* client, JackSessionCallback callback, void *arg)
  2134. {
  2135. if (client->control->active) {
  2136. jack_error ("You cannot set callbacks on an active client.");
  2137. return -1;
  2138. }
  2139. client->session_cb_arg = arg;
  2140. client->session_cb = callback;
  2141. client->control->session_cbset = (callback != NULL);
  2142. return 0;
  2143. }
  2144. int
  2145. jack_get_process_done_fd (jack_client_t *client)
  2146. {
  2147. return client->graph_next_fd;
  2148. }
  2149. void
  2150. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  2151. {
  2152. client->on_shutdown = function;
  2153. client->on_shutdown_arg = arg;
  2154. }
  2155. void
  2156. jack_on_info_shutdown (jack_client_t *client, void (*function)(jack_status_t, const char*, void *arg), void *arg)
  2157. {
  2158. client->on_info_shutdown = function;
  2159. client->on_info_shutdown_arg = arg;
  2160. }
  2161. char *
  2162. jack_get_client_name_by_uuid( jack_client_t *client, const char *uuid )
  2163. {
  2164. jack_request_t request;
  2165. char *end_ptr;
  2166. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2167. if( *end_ptr != '\0' ) {
  2168. return NULL;
  2169. }
  2170. VALGRIND_MEMSET (&request, 0, sizeof (request));
  2171. request.type = GetClientByUUID;
  2172. request.x.client_id = uuid_int;
  2173. if( jack_client_deliver_request( client, &request ) )
  2174. return NULL;
  2175. return strdup( request.x.port_info.name );
  2176. }
  2177. char *
  2178. jack_client_get_uuid( jack_client_t *client )
  2179. {
  2180. char retval[16];
  2181. snprintf( retval, sizeof(retval), "%d", client->control->uid );
  2182. return strdup(retval);
  2183. }
  2184. int
  2185. jack_reserve_client_name( jack_client_t *client, const char *name, const char *uuid )
  2186. {
  2187. jack_request_t request;
  2188. char *end_ptr;
  2189. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2190. if( *end_ptr != '\0' ) {
  2191. return -1;
  2192. }
  2193. VALGRIND_MEMSET (&request, 0, sizeof (request));
  2194. request.type = ReserveName;
  2195. snprintf( request.x.reservename.name, sizeof( request.x.reservename.name ),
  2196. "%s", name );
  2197. request.x.reservename.uuid = uuid_int;
  2198. return jack_client_deliver_request( client, &request );
  2199. }
  2200. const char **
  2201. jack_get_ports (jack_client_t *client,
  2202. const char *port_name_pattern,
  2203. const char *type_name_pattern,
  2204. unsigned long flags)
  2205. {
  2206. jack_control_t *engine;
  2207. const char **matching_ports;
  2208. unsigned long match_cnt;
  2209. jack_port_shared_t *psp;
  2210. unsigned long i;
  2211. regex_t port_regex;
  2212. regex_t type_regex;
  2213. int matching;
  2214. engine = client->engine;
  2215. if (port_name_pattern && port_name_pattern[0]) {
  2216. regcomp (&port_regex, port_name_pattern,
  2217. REG_EXTENDED|REG_NOSUB);
  2218. }
  2219. if (type_name_pattern && type_name_pattern[0]) {
  2220. regcomp (&type_regex, type_name_pattern,
  2221. REG_EXTENDED|REG_NOSUB);
  2222. }
  2223. psp = engine->ports;
  2224. match_cnt = 0;
  2225. if ((matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max)) == NULL) {
  2226. return NULL;
  2227. }
  2228. for (i = 0; i < engine->port_max; i++) {
  2229. matching = 1;
  2230. if (!psp[i].in_use) {
  2231. continue;
  2232. }
  2233. if (flags) {
  2234. if ((psp[i].flags & flags) != flags) {
  2235. matching = 0;
  2236. }
  2237. }
  2238. if (matching && port_name_pattern && port_name_pattern[0]) {
  2239. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  2240. matching = 0;
  2241. }
  2242. }
  2243. if (matching && type_name_pattern && type_name_pattern[0]) {
  2244. jack_port_type_id_t ptid = psp[i].ptype_id;
  2245. if (regexec (&type_regex,
  2246. engine->port_types[ptid].type_name,
  2247. 0, NULL, 0)) {
  2248. matching = 0;
  2249. }
  2250. }
  2251. if (matching) {
  2252. matching_ports[match_cnt++] = psp[i].name;
  2253. }
  2254. }
  2255. if (port_name_pattern && port_name_pattern[0]) {
  2256. regfree (&port_regex);
  2257. }
  2258. if (type_name_pattern && type_name_pattern[0]) {
  2259. regfree (&type_regex);
  2260. }
  2261. matching_ports[match_cnt] = 0;
  2262. if (match_cnt == 0) {
  2263. free (matching_ports);
  2264. matching_ports = 0;
  2265. }
  2266. return matching_ports;
  2267. }
  2268. float
  2269. jack_cpu_load (jack_client_t *client)
  2270. {
  2271. return client->engine->cpu_load;
  2272. }
  2273. float
  2274. jack_get_xrun_delayed_usecs (jack_client_t *client)
  2275. {
  2276. return client->engine->xrun_delayed_usecs;
  2277. }
  2278. float
  2279. jack_get_max_delayed_usecs (jack_client_t *client)
  2280. {
  2281. return client->engine->max_delayed_usecs;
  2282. }
  2283. void
  2284. jack_reset_max_delayed_usecs (jack_client_t *client)
  2285. {
  2286. client->engine->max_delayed_usecs = 0.0f;
  2287. }
  2288. pthread_t
  2289. jack_client_thread_id (jack_client_t *client)
  2290. {
  2291. return client->thread_id;
  2292. }
  2293. int
  2294. jack_client_name_size(void)
  2295. {
  2296. return JACK_CLIENT_NAME_SIZE;
  2297. }
  2298. int
  2299. jack_port_name_size(void)
  2300. {
  2301. return JACK_PORT_NAME_SIZE;
  2302. }
  2303. int
  2304. jack_port_type_size(void)
  2305. {
  2306. return JACK_PORT_TYPE_SIZE;
  2307. }
  2308. void
  2309. jack_free (void* ptr)
  2310. {
  2311. free (ptr);
  2312. }