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.

3023 lines
70KB

  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_client_core_wait (jack_client_t* client)
  1502. {
  1503. jack_client_control_t *control = client->control;
  1504. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1505. "event_fd and graph_wait_fd..." :
  1506. "event_fd only");
  1507. while (1) {
  1508. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1509. if (errno == EINTR) {
  1510. continue;
  1511. }
  1512. jack_error ("poll failed in client (%s)",
  1513. strerror (errno));
  1514. return -1;
  1515. }
  1516. pthread_testcancel();
  1517. #ifndef JACK_USE_MACH_THREADS
  1518. /* get an accurate timestamp on waking from poll for a
  1519. * process() cycle.
  1520. */
  1521. if (client->graph_wait_fd >= 0
  1522. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1523. control->awake_at = jack_get_microseconds();
  1524. }
  1525. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1526. client->pollfd[EVENT_POLL_INDEX].revents,
  1527. client->pollfd[WAIT_POLL_INDEX].revents);
  1528. if (client->graph_wait_fd >= 0 &&
  1529. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1530. /* our upstream "wait" connection
  1531. closed, which either means that
  1532. an intermediate client exited, or
  1533. jackd exited, or jackd zombified
  1534. us.
  1535. we can discover the zombification
  1536. via client->control->dead, but
  1537. the other two possibilities are
  1538. impossible to identify just from
  1539. this situation. so we have to
  1540. check what we are connected to,
  1541. and act accordingly.
  1542. */
  1543. if (client->upstream_is_jackd) {
  1544. DEBUG ("WE DIE\n");
  1545. return 0;
  1546. } else {
  1547. DEBUG ("WE PUNT\n");
  1548. /* don't poll on the wait fd
  1549. * again until we get a
  1550. * GraphReordered event.
  1551. */
  1552. client->graph_wait_fd = -1;
  1553. client->pollmax = 1;
  1554. }
  1555. }
  1556. #endif
  1557. if (jack_client_process_events (client)) {
  1558. DEBUG ("event processing failed\n");
  1559. return 0;
  1560. }
  1561. #ifndef JACK_USE_MACH_THREADS
  1562. if (client->graph_wait_fd >= 0 &&
  1563. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1564. DEBUG ("time to run process()\n");
  1565. break;
  1566. }
  1567. #endif
  1568. }
  1569. if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1570. DEBUG ("client appears dead or event pollfd has error status\n");
  1571. return -1;
  1572. }
  1573. return 0;
  1574. }
  1575. static int
  1576. jack_wake_next_client (jack_client_t* client)
  1577. {
  1578. struct pollfd pfds[1];
  1579. int pret = 0;
  1580. char c = 0;
  1581. if (write (client->graph_next_fd, &c, sizeof (c))
  1582. != sizeof (c)) {
  1583. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1584. jack_error ("cannot continue execution of the "
  1585. "processing graph (%s)",
  1586. strerror(errno));
  1587. return -1;
  1588. }
  1589. DEBUG ("client sent message to next stage by %" PRIu64 "",
  1590. jack_get_microseconds());
  1591. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1592. /* "upstream client went away? readability is checked in
  1593. * jack_client_core_wait(), but that's almost a whole cycle
  1594. * before we get here.
  1595. */
  1596. if (client->graph_wait_fd >= 0) {
  1597. pfds[0].fd = client->graph_wait_fd;
  1598. pfds[0].events = POLLIN;
  1599. /* 0 timeout, don't actually wait */
  1600. pret = poll(pfds, 1, 0);
  1601. }
  1602. if (pret > 0 && (pfds[0].revents & POLLIN)) {
  1603. if (read (client->graph_wait_fd, &c, sizeof (c))
  1604. != sizeof (c)) {
  1605. jack_error ("cannot complete execution of the "
  1606. "processing graph (%s)", strerror(errno));
  1607. return -1;
  1608. }
  1609. } else {
  1610. DEBUG("cleanup byte from pipe %d not available?\n",
  1611. client->graph_wait_fd);
  1612. }
  1613. return 0;
  1614. }
  1615. static jack_nframes_t
  1616. jack_thread_first_wait (jack_client_t* client)
  1617. {
  1618. if (jack_client_core_wait (client)) {
  1619. return 0;
  1620. }
  1621. return client->control->nframes;
  1622. }
  1623. jack_nframes_t
  1624. jack_thread_wait (jack_client_t* client, int status)
  1625. {
  1626. client->control->last_status = status;
  1627. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1628. /* housekeeping/cleanup after data processing */
  1629. if (status == 0 && client->control->timebase_cb_cbset) {
  1630. jack_call_timebase_master (client);
  1631. }
  1632. /* end preemption checking */
  1633. CHECK_PREEMPTION (client->engine, FALSE);
  1634. client->control->finished_at = jack_get_microseconds();
  1635. /* wake the next client in the chain (could be the server),
  1636. and check if we were killed during the process
  1637. cycle.
  1638. */
  1639. if (jack_wake_next_client (client)) {
  1640. DEBUG("client cannot wake next, or is dead\n");
  1641. return 0;
  1642. }
  1643. if (status || client->control->dead || !client->engine->engine_ok) {
  1644. return 0;
  1645. }
  1646. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1647. if (jack_client_core_wait (client)) {
  1648. return 0;
  1649. }
  1650. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1651. /* Time to do data processing */
  1652. client->control->state = Running;
  1653. /* begin preemption checking */
  1654. CHECK_PREEMPTION (client->engine, TRUE);
  1655. if (client->control->sync_cb_cbset)
  1656. jack_call_sync_client (client);
  1657. return client->control->nframes;
  1658. }
  1659. jack_nframes_t jack_cycle_wait (jack_client_t* client)
  1660. {
  1661. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1662. if (jack_client_core_wait (client)) {
  1663. return 0;
  1664. }
  1665. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1666. /* Time to do data processing */
  1667. client->control->state = Running;
  1668. /* begin preemption checking */
  1669. CHECK_PREEMPTION (client->engine, TRUE);
  1670. if (client->control->sync_cb_cbset)
  1671. jack_call_sync_client (client);
  1672. return client->control->nframes;
  1673. }
  1674. void jack_cycle_signal(jack_client_t* client, int status)
  1675. {
  1676. client->control->last_status = status;
  1677. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1678. /* housekeeping/cleanup after data processing */
  1679. if (status == 0 && client->control->timebase_cb_cbset) {
  1680. jack_call_timebase_master (client);
  1681. }
  1682. /* end preemption checking */
  1683. CHECK_PREEMPTION (client->engine, FALSE);
  1684. client->control->finished_at = jack_get_microseconds();
  1685. /* wake the next client in the chain (could be the server),
  1686. and check if we were killed during the process
  1687. cycle.
  1688. */
  1689. if (jack_wake_next_client (client)) {
  1690. DEBUG("client cannot wake next, or is dead\n");
  1691. jack_client_thread_suicide (client);
  1692. /*NOTREACHED*/
  1693. }
  1694. if (status || client->control->dead || !client->engine->engine_ok) {
  1695. jack_client_thread_suicide (client);
  1696. /*NOTREACHED*/
  1697. }
  1698. }
  1699. static void
  1700. jack_client_thread_aux (void *arg)
  1701. {
  1702. jack_client_t *client = (jack_client_t *) arg;
  1703. jack_client_control_t *control = client->control;
  1704. pthread_mutex_lock (&client_lock);
  1705. client->thread_ok = TRUE;
  1706. client->thread_id = pthread_self();
  1707. pthread_cond_signal (&client_ready);
  1708. pthread_mutex_unlock (&client_lock);
  1709. control->pid = getpid();
  1710. control->pgrp = getpgrp();
  1711. DEBUG ("client thread is now running");
  1712. if (control->thread_init_cbset) {
  1713. DEBUG ("calling client thread init callback");
  1714. client->thread_init (client->thread_init_arg);
  1715. }
  1716. /* wait for first wakeup from server */
  1717. if (jack_thread_first_wait (client) == control->nframes) {
  1718. /* now run till we're done */
  1719. if (control->process_cbset) {
  1720. /* run process callback, then wait... ad-infinitum */
  1721. while (1) {
  1722. DEBUG("client calls process()");
  1723. int status = (client->process (control->nframes,
  1724. client->process_arg) ==
  1725. control->nframes);
  1726. control->state = Finished;
  1727. DEBUG("client leaves process(), re-enters wait");
  1728. if (!jack_thread_wait (client, status)) {
  1729. break;
  1730. }
  1731. DEBUG("client done with wait");
  1732. }
  1733. } else {
  1734. /* no process handling but still need to process events */
  1735. while (jack_thread_wait (client, 0) == control->nframes)
  1736. ;
  1737. }
  1738. }
  1739. jack_client_thread_suicide (client);
  1740. }
  1741. static void*
  1742. jack_client_thread (void *arg)
  1743. {
  1744. jack_client_t *client = (jack_client_t *) arg;
  1745. jack_client_control_t *control = client->control;
  1746. if (client->control->thread_cb_cbset) {
  1747. pthread_mutex_lock (&client_lock);
  1748. client->thread_ok = TRUE;
  1749. client->thread_id = pthread_self();
  1750. pthread_cond_signal (&client_ready);
  1751. pthread_mutex_unlock (&client_lock);
  1752. control->pid = getpid();
  1753. control->pgrp = getpgrp();
  1754. client->thread_cb(client->thread_cb_arg);
  1755. jack_client_thread_suicide(client);
  1756. } else {
  1757. jack_client_thread_aux(arg);
  1758. }
  1759. /*NOTREACHED*/
  1760. return (void *) 0;
  1761. }
  1762. #ifdef JACK_USE_MACH_THREADS
  1763. /* real-time thread : separated from the normal client thread, it will
  1764. * communicate with the server using fast mach RPC mechanism */
  1765. static void *
  1766. jack_client_process_thread (void *arg)
  1767. {
  1768. jack_client_t *client = (jack_client_t *) arg;
  1769. jack_client_control_t *control = client->control;
  1770. int err = 0;
  1771. if (client->control->thread_init_cbset) {
  1772. /* this means that the init callback will be called twice -taybin*/
  1773. DEBUG ("calling client thread init callback");
  1774. client->thread_init (client->thread_init_arg);
  1775. }
  1776. client->control->pid = getpid();
  1777. DEBUG ("client process thread is now running");
  1778. client->rt_thread_ok = TRUE;
  1779. while (err == 0) {
  1780. if (jack_client_suspend(client) < 0) {
  1781. jack_error ("jack_client_process_thread :resume error");
  1782. goto zombie;
  1783. }
  1784. control->awake_at = jack_get_microseconds();
  1785. DEBUG ("client resumed");
  1786. control->state = Running;
  1787. if (control->sync_cb_cbset)
  1788. jack_call_sync_client (client);
  1789. if (control->process_cbset) {
  1790. if (client->process (control->nframes,
  1791. client->process_arg) == 0) {
  1792. control->state = Finished;
  1793. }
  1794. } else {
  1795. control->state = Finished;
  1796. }
  1797. if (control->timebase_cb_cbset)
  1798. jack_call_timebase_master (client);
  1799. control->finished_at = jack_get_microseconds();
  1800. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1801. control->finished_at,
  1802. ((float)(control->finished_at - control->awake_at)));
  1803. /* check if we were killed during the process cycle
  1804. * (or whatever) */
  1805. if (client->control->dead) {
  1806. jack_error ("jack_client_process_thread: "
  1807. "client->control->dead");
  1808. goto zombie;
  1809. }
  1810. DEBUG("process cycle fully complete\n");
  1811. }
  1812. return (void *) ((intptr_t)err);
  1813. zombie:
  1814. jack_error ("jack_client_process_thread : zombified");
  1815. client->rt_thread_ok = FALSE;
  1816. if (client->on_info_shutdown) {
  1817. jack_error ("zombified - calling shutdown handler");
  1818. client->on_info_shutdown (JackClientZombie, "Zombified", client->on_info_shutdown_arg);
  1819. } else if (client->on_shutdown) {
  1820. jack_error ("zombified - calling shutdown handler");
  1821. client->on_shutdown (client->on_shutdown_arg);
  1822. } else {
  1823. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1824. /* Need a fix : possibly make client crash if
  1825. * zombified without shutdown handler */
  1826. jack_client_close_aux (client);
  1827. }
  1828. pthread_exit (0);
  1829. /*NOTREACHED*/
  1830. return 0;
  1831. }
  1832. #endif /* JACK_USE_MACH_THREADS */
  1833. static int
  1834. jack_start_thread (jack_client_t *client)
  1835. {
  1836. if (client->engine->real_time) {
  1837. #ifdef USE_MLOCK
  1838. if (client->engine->do_mlock
  1839. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1840. jack_error ("cannot lock down memory for RT thread "
  1841. "(%s)", strerror (errno));
  1842. #ifdef ENSURE_MLOCK
  1843. return -1;
  1844. #endif /* ENSURE_MLOCK */
  1845. }
  1846. if (client->engine->do_munlock) {
  1847. cleanup_mlock ();
  1848. }
  1849. #endif /* USE_MLOCK */
  1850. }
  1851. #ifdef JACK_USE_MACH_THREADS
  1852. /* Stephane Letz : letz@grame.fr
  1853. On MacOSX, the normal thread does not need to be real-time.
  1854. */
  1855. if (jack_client_create_thread (client,
  1856. &client->thread,
  1857. client->engine->client_priority,
  1858. FALSE,
  1859. jack_client_thread, client)) {
  1860. return -1;
  1861. }
  1862. #else
  1863. if (jack_client_create_thread (client,
  1864. &client->thread,
  1865. client->engine->client_priority,
  1866. client->engine->real_time,
  1867. jack_client_thread, client)) {
  1868. return -1;
  1869. }
  1870. #endif
  1871. #ifdef JACK_USE_MACH_THREADS
  1872. /* a secondary thread that runs the process callback and uses
  1873. ultra-fast Mach primitives for inter-thread signalling.
  1874. XXX in a properly structured JACK, there would be no
  1875. need for this, because we would have client wake up
  1876. methods that encapsulated the underlying mechanism
  1877. used.
  1878. */
  1879. if (jack_client_create_thread(client,
  1880. &client->process_thread,
  1881. client->engine->client_priority,
  1882. client->engine->real_time,
  1883. jack_client_process_thread, client)) {
  1884. return -1;
  1885. }
  1886. #endif /* JACK_USE_MACH_THREADS */
  1887. return 0;
  1888. }
  1889. int
  1890. jack_activate (jack_client_t *client)
  1891. {
  1892. jack_request_t req;
  1893. if (client->control->type == ClientInternal ||
  1894. client->control->type == ClientDriver) {
  1895. goto startit;
  1896. }
  1897. /* get the pid of the client process to pass it to engine */
  1898. client->control->pid = getpid ();
  1899. #ifdef USE_CAPABILITIES
  1900. if (client->engine->has_capabilities != 0 &&
  1901. client->control->pid != 0 && client->engine->real_time != 0) {
  1902. /* we need to ask the engine for realtime capabilities
  1903. before trying to start the realtime thread
  1904. */
  1905. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1906. req.type = SetClientCapabilities;
  1907. req.x.client_id = client->control->id;
  1908. req.x.cap_pid = client->control->pid;
  1909. jack_client_deliver_request (client, &req);
  1910. if (req.status) {
  1911. /* what to do? engine is running realtime, it
  1912. is using capabilities and has them
  1913. (otherwise we would not get an error
  1914. return) but for some reason it could not
  1915. give the client the required capabilities.
  1916. For now, leave the client so that it
  1917. still runs, albeit non-realtime.
  1918. */
  1919. jack_error ("could not receive realtime capabilities, "
  1920. "client will run non-realtime");
  1921. }
  1922. }
  1923. #endif /* USE_CAPABILITIES */
  1924. if (client->first_active) {
  1925. pthread_mutex_init (&client_lock, NULL);
  1926. pthread_cond_init (&client_ready, NULL);
  1927. pthread_mutex_lock (&client_lock);
  1928. if (jack_start_thread (client)) {
  1929. pthread_mutex_unlock (&client_lock);
  1930. return -1;
  1931. }
  1932. pthread_cond_wait (&client_ready, &client_lock);
  1933. pthread_mutex_unlock (&client_lock);
  1934. if (!client->thread_ok) {
  1935. jack_error ("could not start client thread");
  1936. return -1;
  1937. }
  1938. client->first_active = FALSE;
  1939. }
  1940. startit:
  1941. req.type = ActivateClient;
  1942. req.x.client_id = client->control->id;
  1943. return jack_client_deliver_request (client, &req);
  1944. }
  1945. static int
  1946. jack_deactivate_aux (jack_client_t *client)
  1947. {
  1948. jack_request_t req;
  1949. int rc = ESRCH; /* already shut down */
  1950. if (client && client->control) { /* not shut down? */
  1951. rc = 0;
  1952. if (client->control->active) { /* still active? */
  1953. VALGRIND_MEMSET (&req, 0, sizeof (req));
  1954. req.type = DeactivateClient;
  1955. req.x.client_id = client->control->id;
  1956. rc = jack_client_deliver_request (client, &req);
  1957. }
  1958. }
  1959. return rc;
  1960. }
  1961. int
  1962. jack_deactivate (jack_client_t *client)
  1963. {
  1964. return jack_deactivate_aux(client);
  1965. }
  1966. static int
  1967. jack_client_close_aux (jack_client_t *client)
  1968. {
  1969. JSList *node;
  1970. void *status;
  1971. int rc;
  1972. rc = jack_deactivate_aux (client);
  1973. if (rc == ESRCH) { /* already shut down? */
  1974. return rc;
  1975. }
  1976. if (client->control->type == ClientExternal) {
  1977. #if JACK_USE_MACH_THREADS
  1978. if (client->rt_thread_ok) {
  1979. // MacOSX pthread_cancel not implemented in
  1980. // Darwin 5.5, 6.4
  1981. mach_port_t machThread =
  1982. pthread_mach_thread_np (client->process_thread);
  1983. thread_terminate (machThread);
  1984. }
  1985. #endif
  1986. /* stop the thread that communicates with the jack
  1987. * server, only if it was actually running
  1988. */
  1989. if (client->thread_ok){
  1990. pthread_cancel (client->thread);
  1991. pthread_join (client->thread, &status);
  1992. }
  1993. if (client->control) {
  1994. jack_release_shm (&client->control_shm);
  1995. client->control = NULL;
  1996. }
  1997. if (client->engine) {
  1998. jack_release_shm (&client->engine_shm);
  1999. client->engine = NULL;
  2000. }
  2001. if (client->port_segment) {
  2002. jack_port_type_id_t ptid;
  2003. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  2004. jack_release_shm (&client->port_segment[ptid]);
  2005. }
  2006. free (client->port_segment);
  2007. client->port_segment = NULL;
  2008. }
  2009. #ifndef JACK_USE_MACH_THREADS
  2010. if (client->graph_wait_fd >= 0) {
  2011. close (client->graph_wait_fd);
  2012. }
  2013. if (client->graph_next_fd >= 0) {
  2014. close (client->graph_next_fd);
  2015. }
  2016. #endif
  2017. close (client->event_fd);
  2018. if (shutdown (client->request_fd, SHUT_RDWR)) {
  2019. jack_error ("could not shutdown client request socket");
  2020. }
  2021. close (client->request_fd);
  2022. }
  2023. for (node = client->ports; node; node = jack_slist_next (node)) {
  2024. free (node->data);
  2025. }
  2026. jack_slist_free (client->ports);
  2027. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  2028. free (node->data);
  2029. }
  2030. jack_slist_free (client->ports_ext);
  2031. jack_client_free (client);
  2032. jack_messagebuffer_exit ();
  2033. return rc;
  2034. }
  2035. int
  2036. jack_client_close (jack_client_t *client)
  2037. {
  2038. return jack_client_close_aux(client);
  2039. }
  2040. int
  2041. jack_is_realtime (jack_client_t *client)
  2042. {
  2043. return client->engine->real_time;
  2044. }
  2045. jack_nframes_t
  2046. jack_get_buffer_size (jack_client_t *client)
  2047. {
  2048. return client->engine->buffer_size;
  2049. }
  2050. int
  2051. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  2052. {
  2053. #ifdef DO_BUFFER_RESIZE
  2054. jack_request_t req;
  2055. VALGRIND_MEMSET (&req, 0, sizeof (req));
  2056. req.type = SetBufferSize;
  2057. req.x.nframes = nframes;
  2058. return jack_client_deliver_request (client, &req);
  2059. #else
  2060. return ENOSYS;
  2061. #endif /* DO_BUFFER_RESIZE */
  2062. }
  2063. int
  2064. jack_connect (jack_client_t *client, const char *source_port,
  2065. const char *destination_port)
  2066. {
  2067. jack_request_t req;
  2068. VALGRIND_MEMSET (&req, 0, sizeof (req));
  2069. req.type = ConnectPorts;
  2070. snprintf (req.x.connect.source_port,
  2071. sizeof (req.x.connect.source_port), "%s", source_port);
  2072. snprintf (req.x.connect.destination_port,
  2073. sizeof (req.x.connect.destination_port),
  2074. "%s", destination_port);
  2075. return jack_client_deliver_request (client, &req);
  2076. }
  2077. int
  2078. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  2079. {
  2080. jack_request_t req;
  2081. pthread_mutex_lock (&port->connection_lock);
  2082. if (port->connections == NULL) {
  2083. pthread_mutex_unlock (&port->connection_lock);
  2084. return 0;
  2085. }
  2086. pthread_mutex_unlock (&port->connection_lock);
  2087. VALGRIND_MEMSET (&req, 0, sizeof (req));
  2088. req.type = DisconnectPort;
  2089. req.x.port_info.port_id = port->shared->id;
  2090. return jack_client_deliver_request (client, &req);
  2091. }
  2092. int
  2093. jack_disconnect (jack_client_t *client, const char *source_port,
  2094. const char *destination_port)
  2095. {
  2096. jack_request_t req;
  2097. VALGRIND_MEMSET (&req, 0, sizeof (req));
  2098. req.type = DisconnectPorts;
  2099. snprintf (req.x.connect.source_port,
  2100. sizeof (req.x.connect.source_port), "%s", source_port);
  2101. snprintf (req.x.connect.destination_port,
  2102. sizeof (req.x.connect.destination_port),
  2103. "%s", destination_port);
  2104. return jack_client_deliver_request (client, &req);
  2105. }
  2106. void
  2107. jack_set_error_function (void (*func) (const char *))
  2108. {
  2109. jack_error_callback = func;
  2110. }
  2111. void
  2112. jack_set_info_function (void (*func) (const char *))
  2113. {
  2114. jack_info_callback = func;
  2115. }
  2116. int
  2117. jack_set_graph_order_callback (jack_client_t *client,
  2118. JackGraphOrderCallback callback, void *arg)
  2119. {
  2120. if (client->control->active) {
  2121. jack_error ("You cannot set callbacks on an active client.");
  2122. return -1;
  2123. }
  2124. client->graph_order = callback;
  2125. client->graph_order_arg = arg;
  2126. client->control->graph_order_cbset = (callback != NULL);
  2127. return 0;
  2128. }
  2129. int
  2130. jack_set_latency_callback (jack_client_t *client,
  2131. JackLatencyCallback callback, void *arg)
  2132. {
  2133. if (client->control->active) {
  2134. jack_error ("You cannot set callbacks on an active client.");
  2135. return -1;
  2136. }
  2137. client->latency_cb = callback;
  2138. client->latency_cb_arg = arg;
  2139. client->control->latency_cbset = (callback != NULL);
  2140. return 0;
  2141. }
  2142. int jack_set_xrun_callback (jack_client_t *client,
  2143. JackXRunCallback callback, void *arg)
  2144. {
  2145. if (client->control->active) {
  2146. jack_error ("You cannot set callbacks on an active client.");
  2147. return -1;
  2148. }
  2149. client->xrun = callback;
  2150. client->xrun_arg = arg;
  2151. client->control->xrun_cbset = (callback != NULL);
  2152. return 0;
  2153. }
  2154. int
  2155. jack_set_process_callback (jack_client_t *client,
  2156. JackProcessCallback callback, void *arg)
  2157. {
  2158. if (client->control->active) {
  2159. jack_error ("You cannot set callbacks on an active client.");
  2160. return -1;
  2161. }
  2162. if (client->control->thread_cb_cbset) {
  2163. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  2164. return -1;
  2165. }
  2166. client->process_arg = arg;
  2167. client->process = callback;
  2168. client->control->process_cbset = (callback != NULL);
  2169. return 0;
  2170. }
  2171. int
  2172. jack_set_thread_init_callback (jack_client_t *client,
  2173. JackThreadInitCallback callback, void *arg)
  2174. {
  2175. if (client->control->active) {
  2176. jack_error ("You cannot set callbacks on an active client.");
  2177. return -1;
  2178. }
  2179. client->thread_init_arg = arg;
  2180. client->thread_init = callback;
  2181. client->control->thread_init_cbset = (callback != NULL);
  2182. /* make sure that the message buffer thread is initialized too */
  2183. jack_messagebuffer_thread_init (callback, arg);
  2184. return 0;
  2185. }
  2186. int
  2187. jack_set_freewheel_callback (jack_client_t *client,
  2188. JackFreewheelCallback callback, void *arg)
  2189. {
  2190. if (client->control->active) {
  2191. jack_error ("You cannot set callbacks on an active client.");
  2192. return -1;
  2193. }
  2194. client->freewheel_arg = arg;
  2195. client->freewheel_cb = callback;
  2196. client->control->freewheel_cb_cbset = (callback != NULL);
  2197. return 0;
  2198. }
  2199. int
  2200. jack_set_buffer_size_callback (jack_client_t *client,
  2201. JackBufferSizeCallback callback, void *arg)
  2202. {
  2203. client->bufsize_arg = arg;
  2204. client->bufsize = callback;
  2205. client->control->bufsize_cbset = (callback != NULL);
  2206. return 0;
  2207. }
  2208. int
  2209. jack_set_port_registration_callback(jack_client_t *client,
  2210. JackPortRegistrationCallback callback,
  2211. void *arg)
  2212. {
  2213. if (client->control->active) {
  2214. jack_error ("You cannot set callbacks on an active client.");
  2215. return -1;
  2216. }
  2217. client->port_register_arg = arg;
  2218. client->port_register = callback;
  2219. client->control->port_register_cbset = (callback != NULL);
  2220. return 0;
  2221. }
  2222. int
  2223. jack_set_port_connect_callback(jack_client_t *client,
  2224. JackPortConnectCallback callback,
  2225. void *arg)
  2226. {
  2227. if (client->control->active) {
  2228. jack_error ("You cannot set callbacks on an active client.");
  2229. return -1;
  2230. }
  2231. client->port_connect_arg = arg;
  2232. client->port_connect = callback;
  2233. client->control->port_connect_cbset = (callback != NULL);
  2234. return 0;
  2235. }
  2236. int
  2237. jack_set_client_registration_callback(jack_client_t *client,
  2238. JackClientRegistrationCallback callback,
  2239. void *arg)
  2240. {
  2241. if (client->control->active) {
  2242. jack_error ("You cannot set callbacks on an active client.");
  2243. return -1;
  2244. }
  2245. client->client_register_arg = arg;
  2246. client->client_register = callback;
  2247. client->control->client_register_cbset = (callback != NULL);
  2248. return 0;
  2249. }
  2250. int
  2251. jack_set_process_thread(jack_client_t* client, JackThreadCallback callback, void *arg)
  2252. {
  2253. if (client->control->active) {
  2254. jack_error ("You cannot set callbacks on an active client.");
  2255. return -1;
  2256. }
  2257. if (client->control->process_cbset) {
  2258. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  2259. return -1;
  2260. }
  2261. client->thread_cb_arg = arg;
  2262. client->thread_cb = callback;
  2263. client->control->thread_cb_cbset = (callback != NULL);
  2264. return 0;
  2265. }
  2266. int
  2267. jack_set_session_callback(jack_client_t* client, JackSessionCallback callback, void *arg)
  2268. {
  2269. if (client->control->active) {
  2270. jack_error ("You cannot set callbacks on an active client.");
  2271. return -1;
  2272. }
  2273. client->session_cb_arg = arg;
  2274. client->session_cb = callback;
  2275. client->control->session_cbset = (callback != NULL);
  2276. return 0;
  2277. }
  2278. int
  2279. jack_get_process_done_fd (jack_client_t *client)
  2280. {
  2281. return client->graph_next_fd;
  2282. }
  2283. void
  2284. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  2285. {
  2286. client->on_shutdown = function;
  2287. client->on_shutdown_arg = arg;
  2288. }
  2289. void
  2290. jack_on_info_shutdown (jack_client_t *client, void (*function)(jack_status_t, const char*, void *arg), void *arg)
  2291. {
  2292. client->on_info_shutdown = function;
  2293. client->on_info_shutdown_arg = arg;
  2294. }
  2295. char *
  2296. jack_get_client_name_by_uuid( jack_client_t *client, const char *uuid )
  2297. {
  2298. jack_request_t request;
  2299. char *end_ptr;
  2300. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2301. if( *end_ptr != '\0' ) {
  2302. return NULL;
  2303. }
  2304. VALGRIND_MEMSET (&request, 0, sizeof (request));
  2305. request.type = GetClientByUUID;
  2306. request.x.client_id = uuid_int;
  2307. if( jack_client_deliver_request( client, &request ) )
  2308. return NULL;
  2309. return strdup( request.x.port_info.name );
  2310. }
  2311. char *
  2312. jack_client_get_uuid( jack_client_t *client )
  2313. {
  2314. char retval[16];
  2315. snprintf( retval, sizeof(retval), "%d", client->control->uid );
  2316. return strdup(retval);
  2317. }
  2318. int
  2319. jack_reserve_client_name( jack_client_t *client, const char *name, const char *uuid )
  2320. {
  2321. jack_request_t request;
  2322. char *end_ptr;
  2323. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2324. if( *end_ptr != '\0' ) {
  2325. return -1;
  2326. }
  2327. VALGRIND_MEMSET (&request, 0, sizeof (request));
  2328. request.type = ReserveName;
  2329. snprintf( request.x.reservename.name, sizeof( request.x.reservename.name ),
  2330. "%s", name );
  2331. request.x.reservename.uuid = uuid_int;
  2332. return jack_client_deliver_request( client, &request );
  2333. }
  2334. const char **
  2335. jack_get_ports (jack_client_t *client,
  2336. const char *port_name_pattern,
  2337. const char *type_name_pattern,
  2338. unsigned long flags)
  2339. {
  2340. jack_control_t *engine;
  2341. const char **matching_ports;
  2342. unsigned long match_cnt;
  2343. jack_port_shared_t *psp;
  2344. unsigned long i;
  2345. regex_t port_regex;
  2346. regex_t type_regex;
  2347. int matching;
  2348. engine = client->engine;
  2349. if (port_name_pattern && port_name_pattern[0]) {
  2350. regcomp (&port_regex, port_name_pattern,
  2351. REG_EXTENDED|REG_NOSUB);
  2352. }
  2353. if (type_name_pattern && type_name_pattern[0]) {
  2354. regcomp (&type_regex, type_name_pattern,
  2355. REG_EXTENDED|REG_NOSUB);
  2356. }
  2357. psp = engine->ports;
  2358. match_cnt = 0;
  2359. if ((matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max)) == NULL) {
  2360. return NULL;
  2361. }
  2362. for (i = 0; i < engine->port_max; i++) {
  2363. matching = 1;
  2364. if (!psp[i].in_use) {
  2365. continue;
  2366. }
  2367. if (flags) {
  2368. if ((psp[i].flags & flags) != flags) {
  2369. matching = 0;
  2370. }
  2371. }
  2372. if (matching && port_name_pattern && port_name_pattern[0]) {
  2373. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  2374. matching = 0;
  2375. }
  2376. }
  2377. if (matching && type_name_pattern && type_name_pattern[0]) {
  2378. jack_port_type_id_t ptid = psp[i].ptype_id;
  2379. if (regexec (&type_regex,
  2380. engine->port_types[ptid].type_name,
  2381. 0, NULL, 0)) {
  2382. matching = 0;
  2383. }
  2384. }
  2385. if (matching) {
  2386. matching_ports[match_cnt++] = psp[i].name;
  2387. }
  2388. }
  2389. if (port_name_pattern && port_name_pattern[0]) {
  2390. regfree (&port_regex);
  2391. }
  2392. if (type_name_pattern && type_name_pattern[0]) {
  2393. regfree (&type_regex);
  2394. }
  2395. matching_ports[match_cnt] = 0;
  2396. if (match_cnt == 0) {
  2397. free (matching_ports);
  2398. matching_ports = 0;
  2399. }
  2400. return matching_ports;
  2401. }
  2402. float
  2403. jack_cpu_load (jack_client_t *client)
  2404. {
  2405. return client->engine->cpu_load;
  2406. }
  2407. float
  2408. jack_get_xrun_delayed_usecs (jack_client_t *client)
  2409. {
  2410. return client->engine->xrun_delayed_usecs;
  2411. }
  2412. float
  2413. jack_get_max_delayed_usecs (jack_client_t *client)
  2414. {
  2415. return client->engine->max_delayed_usecs;
  2416. }
  2417. void
  2418. jack_reset_max_delayed_usecs (jack_client_t *client)
  2419. {
  2420. client->engine->max_delayed_usecs = 0.0f;
  2421. }
  2422. pthread_t
  2423. jack_client_thread_id (jack_client_t *client)
  2424. {
  2425. return client->thread_id;
  2426. }
  2427. int
  2428. jack_client_name_size(void)
  2429. {
  2430. return JACK_CLIENT_NAME_SIZE;
  2431. }
  2432. int
  2433. jack_port_name_size(void)
  2434. {
  2435. return JACK_PORT_NAME_SIZE;
  2436. }
  2437. int
  2438. jack_port_type_size(void)
  2439. {
  2440. return JACK_PORT_TYPE_SIZE;
  2441. }
  2442. void
  2443. jack_free (void* ptr)
  2444. {
  2445. free (ptr);
  2446. }