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.

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