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.

2961 lines
69KB

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