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.

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