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.

3081 lines
74KB

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