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.

3019 lines
71KB

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