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.

3133 lines
73KB

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