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.

4049 lines
96KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <math.h>
  17. #include <unistd.h>
  18. #include <sys/socket.h>
  19. #if defined(__APPLE__) && defined(__POWERPC__)
  20. #include "fakepoll.h"
  21. #include "ipc.h"
  22. #else
  23. #include <sys/poll.h>
  24. #endif
  25. #include <sys/un.h>
  26. #include <sys/stat.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <stdint.h>
  32. #include <dirent.h>
  33. #include <sys/ipc.h>
  34. #include <signal.h>
  35. #include <sys/types.h>
  36. #include <sys/mman.h>
  37. #include <string.h>
  38. #include <limits.h>
  39. #include <config.h>
  40. #include <jack/internal.h>
  41. #include <jack/engine.h>
  42. #include <jack/driver.h>
  43. #include <jack/time.h>
  44. #include <jack/version.h>
  45. #include <jack/shm.h>
  46. #ifdef USE_CAPABILITIES
  47. /* capgetp and capsetp are linux only extensions, not posix */
  48. #undef _POSIX_SOURCE
  49. #include <sys/capability.h>
  50. #endif
  51. #include "transengine.h"
  52. #define JACK_ERROR_WITH_SOCKETS 10000000
  53. typedef struct {
  54. jack_port_internal_t *source;
  55. jack_port_internal_t *destination;
  56. } jack_connection_internal_t;
  57. typedef struct _jack_driver_info {
  58. jack_driver_t *(*initialize)(jack_client_t*, int, char**);
  59. void (*finish);
  60. char (*client_name);
  61. dlhandle handle;
  62. } jack_driver_info_t;
  63. static int jack_port_assign_buffer (jack_engine_t *,
  64. jack_port_internal_t *);
  65. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *,
  66. const char *name);
  67. static void jack_zombify_client (jack_engine_t *engine,
  68. jack_client_internal_t *client);
  69. static void jack_remove_client (jack_engine_t *engine,
  70. jack_client_internal_t *client);
  71. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  72. static jack_client_internal_t
  73. *jack_client_internal_new (jack_engine_t *engine, int fd,
  74. jack_client_connect_request_t *);
  75. static void jack_sort_graph (jack_engine_t *engine);
  76. static int jack_rechain_graph (jack_engine_t *engine);
  77. static int jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo);
  78. static void jack_clear_fifos (jack_engine_t *engine);
  79. static int jack_port_do_connect (jack_engine_t *engine,
  80. const char *source_port,
  81. const char *destination_port);
  82. static int jack_port_do_disconnect (jack_engine_t *engine,
  83. const char *source_port,
  84. const char *destination_port);
  85. static int jack_port_do_disconnect_all (jack_engine_t *engine,
  86. jack_port_id_t);
  87. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  88. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  89. static int jack_do_get_port_connections (jack_engine_t *engine,
  90. jack_request_t *req, int reply_fd);
  91. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  92. static void jack_port_clear_connections (jack_engine_t *engine,
  93. jack_port_internal_t *port);
  94. static int jack_port_disconnect_internal (jack_engine_t *engine,
  95. jack_port_internal_t *src,
  96. jack_port_internal_t *dst,
  97. int sort_graph);
  98. static void jack_port_registration_notify (jack_engine_t *,
  99. jack_port_id_t, int);
  100. static int jack_send_connection_notification (jack_engine_t *,
  101. jack_client_id_t,
  102. jack_port_id_t,
  103. jack_port_id_t, int);
  104. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *,
  105. jack_event_t *);
  106. static void jack_deliver_event_to_all (jack_engine_t *engine,
  107. jack_event_t *event);
  108. static void jack_engine_post_process (jack_engine_t *);
  109. static int internal_client_request (void*, jack_request_t *);
  110. static int jack_use_driver (jack_engine_t *engine, jack_driver_t *driver);
  111. static int jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  112. float delayed_usecs);
  113. static char *client_state_names[] = {
  114. "Not triggered",
  115. "Triggered",
  116. "Running",
  117. "Finished"
  118. };
  119. static inline int
  120. jack_client_is_internal (jack_client_internal_t *client)
  121. {
  122. return (client->control->type == ClientInternal) ||
  123. (client->control->type == ClientDriver);
  124. }
  125. static inline void
  126. jack_engine_reset_rolling_usecs (jack_engine_t *engine)
  127. {
  128. memset (engine->rolling_client_usecs, 0,
  129. sizeof (engine->rolling_client_usecs));
  130. engine->rolling_client_usecs_index = 0;
  131. engine->rolling_client_usecs_cnt = 0;
  132. if (engine->driver) {
  133. engine->rolling_interval = (int)
  134. floor (JACK_ENGINE_ROLLING_INTERVAL * 1000.0f
  135. / engine->driver->period_usecs);
  136. } else {
  137. engine->rolling_interval = JACK_ENGINE_ROLLING_INTERVAL;
  138. }
  139. engine->spare_usecs = 0;
  140. }
  141. static inline jack_port_type_info_t *
  142. jack_global_port_type_info (jack_engine_t *engine, jack_port_internal_t *port)
  143. {
  144. /* this returns a pointer to the engine's private port type
  145. information, rather than the copy that the port uses. we
  146. need it for buffer allocation, because we need the mutex
  147. that protects the free buffer list for this port type, and
  148. that requires accessing the single copy owned by the
  149. engine.
  150. */
  151. return &engine->control->port_types[port->shared->type_info.type_id];
  152. }
  153. static int
  154. make_sockets (int fd[2])
  155. {
  156. struct sockaddr_un addr;
  157. int i;
  158. /* First, the master server socket */
  159. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  160. jack_error ("cannot create server socket (%s)",
  161. strerror (errno));
  162. return -1;
  163. }
  164. addr.sun_family = AF_UNIX;
  165. for (i = 0; i < 999; i++) {
  166. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  167. "%s/jack_%d", jack_server_dir, i);
  168. if (access (addr.sun_path, F_OK) != 0) {
  169. break;
  170. }
  171. }
  172. if (i == 999) {
  173. jack_error ("all possible server socket names in use!!!");
  174. close (fd[0]);
  175. return -1;
  176. }
  177. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  178. jack_error ("cannot bind server to socket (%s)",
  179. strerror (errno));
  180. close (fd[0]);
  181. return -1;
  182. }
  183. if (listen (fd[0], 1) < 0) {
  184. jack_error ("cannot enable listen on server socket (%s)",
  185. strerror (errno));
  186. close (fd[0]);
  187. return -1;
  188. }
  189. /* Now the client/server event ack server socket */
  190. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  191. jack_error ("cannot create event ACK socket (%s)",
  192. strerror (errno));
  193. close (fd[0]);
  194. return -1;
  195. }
  196. addr.sun_family = AF_UNIX;
  197. for (i = 0; i < 999; i++) {
  198. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  199. "%s/jack_ack_%d", jack_server_dir, i);
  200. if (access (addr.sun_path, F_OK) != 0) {
  201. break;
  202. }
  203. }
  204. if (i == 999) {
  205. jack_error ("all possible server ACK socket names in use!!!");
  206. close (fd[0]);
  207. close (fd[1]);
  208. return -1;
  209. }
  210. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  211. jack_error ("cannot bind server to socket (%s)",
  212. strerror (errno));
  213. close (fd[0]);
  214. close (fd[1]);
  215. return -1;
  216. }
  217. if (listen (fd[1], 1) < 0) {
  218. jack_error ("cannot enable listen on server socket (%s)",
  219. strerror (errno));
  220. close (fd[0]);
  221. close (fd[1]);
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. void
  227. jack_cleanup_files ()
  228. {
  229. DIR *dir;
  230. struct dirent *dirent;
  231. /* its important that we remove all files that jackd creates
  232. because otherwise subsequent attempts to start jackd will
  233. believe that an instance is already running.
  234. */
  235. if ((dir = opendir (jack_server_dir)) == NULL) {
  236. fprintf (stderr, "jack(%d): cannot open jack FIFO directory "
  237. "(%s)\n", getpid(), strerror (errno));
  238. return;
  239. }
  240. while ((dirent = readdir (dir)) != NULL) {
  241. if (strncmp (dirent->d_name, "jack-", 5) == 0 ||
  242. strncmp (dirent->d_name, "jack_", 5) == 0) {
  243. char fullpath[PATH_MAX+1];
  244. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  245. jack_server_dir, dirent->d_name);
  246. unlink (fullpath);
  247. }
  248. }
  249. closedir (dir);
  250. }
  251. static int
  252. jack_resize_port_segment (jack_engine_t *engine,
  253. jack_port_type_info_t *port_type,
  254. jack_nframes_t buffer_size,
  255. unsigned long nports)
  256. {
  257. jack_event_t event;
  258. char *addr;
  259. size_t offset;
  260. size_t one_buffer;
  261. size_t size;
  262. int shmid;
  263. int perm;
  264. if (port_type->buffer_scale_factor < 0) {
  265. one_buffer = port_type->buffer_size;
  266. } else {
  267. one_buffer = sizeof (jack_default_audio_sample_t)
  268. * port_type->buffer_scale_factor
  269. * engine->control->buffer_size;
  270. }
  271. size = nports * one_buffer;
  272. #if defined(__APPLE__) && defined(__POWERPC__)
  273. /* using O_TRUNC option does not work on Darwin */
  274. perm = O_RDWR|O_CREAT;
  275. #else
  276. perm = O_RDWR|O_CREAT|O_TRUNC;
  277. #endif
  278. if (port_type->shm_info.size == 0) {
  279. snprintf (port_type->shm_info.shm_name,
  280. sizeof(port_type->shm_info.shm_name),
  281. "/jck-[%s]", port_type->type_name);
  282. if ((addr = jack_get_shm (port_type->shm_info.shm_name, size,
  283. perm, 0666, PROT_READ|PROT_WRITE,
  284. &shmid)) == MAP_FAILED) {
  285. jack_error ("cannot create new port segment of %d"
  286. " bytes, shm_name = %s (%s)",
  287. size, port_type->shm_info.shm_name,
  288. strerror (errno));
  289. return -1;
  290. }
  291. jack_register_shm (port_type->shm_info.shm_name, addr, shmid);
  292. port_type->shm_info.address = addr;
  293. } else {
  294. if ((addr = jack_resize_shm (port_type->shm_info.shm_name,
  295. size, perm, 0666,
  296. PROT_READ|PROT_WRITE))
  297. == MAP_FAILED) {
  298. jack_error ("cannot resize port segment to %d bytes,"
  299. " shm_name = %s (%s)", size,
  300. port_type->shm_info.shm_name,
  301. strerror (errno));
  302. return -1;
  303. }
  304. }
  305. port_type->shm_info.size = size;
  306. port_type->shm_info.address = addr;
  307. pthread_mutex_lock (&port_type->buffer_lock);
  308. offset = 0;
  309. while (offset < port_type->shm_info.size) {
  310. jack_port_buffer_info_t *bi;
  311. bi = (jack_port_buffer_info_t *)
  312. malloc (sizeof (jack_port_buffer_info_t));
  313. bi->shm_name = port_type->shm_info.shm_name;
  314. bi->offset = offset;
  315. /* we append because we want the list to be in
  316. * memory-address order */
  317. port_type->buffer_freelist =
  318. jack_slist_append (port_type->buffer_freelist, bi);
  319. offset += one_buffer;
  320. }
  321. pthread_mutex_unlock (&port_type->buffer_lock);
  322. /* tell everybody about it */
  323. event.type = NewPortType;
  324. strcpy (event.x.shm_name, port_type->shm_info.shm_name);
  325. event.y.addr = addr;
  326. jack_deliver_event_to_all (engine, &event);
  327. return 0;
  328. }
  329. static int
  330. jack_set_buffer_size (jack_engine_t *engine, jack_nframes_t nframes)
  331. {
  332. int i;
  333. jack_event_t event;
  334. engine->control->buffer_size = nframes;
  335. for (i = 0; i < engine->control->n_port_types; ++i) {
  336. jack_resize_port_segment (engine,
  337. &engine->control->port_types[i],
  338. nframes, engine->control->port_max);
  339. }
  340. /* convert the first chunk of the audio port buffer segment
  341. * into a zero-filled area */
  342. if (engine->silent_buffer == NULL) {
  343. jack_port_type_info_t *port_type =
  344. &engine->control->port_types[0];
  345. engine->silent_buffer = (jack_port_buffer_info_t *)
  346. port_type->buffer_freelist->data;
  347. port_type->buffer_freelist =
  348. jack_slist_remove_link (port_type->buffer_freelist,
  349. port_type->buffer_freelist);
  350. memset (port_type->shm_info.address +
  351. engine->silent_buffer->offset, 0,
  352. sizeof (jack_default_audio_sample_t) * nframes);
  353. }
  354. event.type = BufferSizeChange;
  355. jack_deliver_event_to_all (engine, &event);
  356. return 0;
  357. }
  358. static JSList *
  359. jack_process_internal(jack_engine_t *engine, JSList *node,
  360. jack_nframes_t nframes)
  361. {
  362. jack_client_internal_t *client;
  363. jack_client_control_t *ctl;
  364. client = (jack_client_internal_t *) node->data;
  365. ctl = client->control;
  366. /* internal client ("plugin") */
  367. DEBUG ("invoking an internal client's callbacks");
  368. ctl->state = Running;
  369. engine->current_client = client;
  370. /* XXX how to time out an internal client? */
  371. if (ctl->sync_cb)
  372. jack_call_sync_client (ctl->private_client);
  373. if (ctl->process)
  374. if (ctl->process (nframes, ctl->process_arg)) {
  375. jack_error ("internal client %s failed", ctl->name);
  376. engine->process_errors++;
  377. }
  378. if (ctl->timebase_cb)
  379. jack_call_timebase_master (ctl->private_client);
  380. ctl->state = Finished;
  381. if (engine->process_errors)
  382. return NULL; /* will stop the loop */
  383. else
  384. return jack_slist_next (node);
  385. }
  386. #if defined(__APPLE__) && defined(__POWERPC__)
  387. static JSList *
  388. jack_process_external(jack_engine_t *engine, JSList *node)
  389. {
  390. jack_client_internal_t * client = (jack_client_internal_t *) node->data;
  391. jack_client_control_t *ctl;
  392. client = (jack_client_internal_t *) node->data;
  393. ctl = client->control;
  394. engine->current_client = client;
  395. // a race exists if we do this after the write(2)
  396. ctl->state = Triggered;
  397. ctl->signalled_at = jack_get_microseconds();
  398. ctl->awake_at = 0;
  399. ctl->finished_at = 0;
  400. jack_client_resume(client);
  401. return jack_slist_next (node);
  402. }
  403. #else
  404. static JSList *
  405. jack_process_external(jack_engine_t *engine, JSList *node)
  406. {
  407. int status;
  408. char c;
  409. float delayed_usecs;
  410. jack_client_internal_t *client;
  411. jack_client_control_t *ctl;
  412. jack_time_t now, then;
  413. client = (jack_client_internal_t *) node->data;
  414. ctl = client->control;
  415. /* external subgraph */
  416. /* a race exists if we do this after the write(2) */
  417. ctl->state = Triggered;
  418. ctl->signalled_at = jack_get_microseconds();
  419. ctl->awake_at = 0;
  420. ctl->finished_at = 0;
  421. engine->current_client = client;
  422. DEBUG ("calling process() on an external subgraph, fd==%d",
  423. client->subgraph_start_fd);
  424. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  425. jack_error ("cannot initiate graph processing (%s)",
  426. strerror (errno));
  427. engine->process_errors++;
  428. return NULL; /* will stop the loop */
  429. }
  430. then = jack_get_microseconds ();
  431. if (engine->asio_mode) {
  432. engine->driver->wait (engine->driver, client->subgraph_wait_fd,
  433. &status, &delayed_usecs);
  434. } else {
  435. struct pollfd pfd[1];
  436. int poll_timeout = (engine->control->real_time == 0 ?
  437. engine->client_timeout_msecs :
  438. engine->driver->period_usecs/1000);
  439. pfd[0].fd = client->subgraph_wait_fd;
  440. pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  441. DEBUG ("waiting on fd==%d for process() subgraph to finish",
  442. client->subgraph_wait_fd);
  443. if (poll (pfd, 1, poll_timeout) < 0) {
  444. jack_error ("poll on subgraph processing failed (%s)",
  445. strerror (errno));
  446. status = -1;
  447. }
  448. if (pfd[0].revents & ~POLLIN) {
  449. jack_error ("subgraph starting at %s lost client",
  450. client->control->name);
  451. status = -2;
  452. }
  453. if (pfd[0].revents & POLLIN) {
  454. status = 0;
  455. } else {
  456. jack_error ("subgraph starting at %s timed out "
  457. "(subgraph_wait_fd=%d, status = %d, "
  458. "state = %s)",
  459. client->control->name,
  460. client->subgraph_wait_fd, status,
  461. client_state_names[client->control->state]);
  462. status = 1;
  463. }
  464. }
  465. now = jack_get_microseconds ();
  466. if (status != 0) {
  467. if (engine->verbose) {
  468. fprintf (stderr, "at %" PRIu64
  469. " client waiting on %d took %" PRIu64
  470. " usecs, status = %d sig = %" PRIu64
  471. " awa = %" PRIu64 " fin = %" PRIu64
  472. " dur=%" PRIu64 "\n",
  473. now,
  474. client->subgraph_wait_fd,
  475. now - then,
  476. status,
  477. ctl->signalled_at,
  478. ctl->awake_at,
  479. ctl->finished_at,
  480. ctl->finished_at? (ctl->finished_at -
  481. ctl->signalled_at): 0);
  482. }
  483. /* we can only consider the timeout a client error if
  484. * it actually woke up. its possible that the kernel
  485. * scheduler screwed us up and never woke up the
  486. * client in time. sigh.
  487. */
  488. if (ctl->awake_at > 0) {
  489. ctl->timed_out++;
  490. }
  491. engine->process_errors++;
  492. return NULL; /* will stop the loop */
  493. } else {
  494. DEBUG ("reading byte from subgraph_wait_fd==%d",
  495. client->subgraph_wait_fd);
  496. if (read (client->subgraph_wait_fd, &c, sizeof(c))
  497. != sizeof (c)) {
  498. jack_error ("pp: cannot clean up byte from graph wait "
  499. "fd (%s)", strerror (errno));
  500. client->error++;
  501. return NULL; /* will stop the loop */
  502. }
  503. }
  504. /* Move to next internal client (or end of client list) */
  505. while (node) {
  506. if (jack_client_is_internal ((jack_client_internal_t *)
  507. node->data)) {
  508. break;
  509. }
  510. node = jack_slist_next (node);
  511. }
  512. return node;
  513. }
  514. #endif
  515. static int
  516. jack_engine_process (jack_engine_t *engine, jack_nframes_t nframes)
  517. {
  518. jack_client_internal_t *client;
  519. jack_client_control_t *ctl;
  520. JSList *node;
  521. engine->process_errors = 0;
  522. for (node = engine->clients; node; node = jack_slist_next (node)) {
  523. ctl = ((jack_client_internal_t *) node->data)->control;
  524. ctl->state = NotTriggered;
  525. ctl->nframes = nframes;
  526. ctl->timed_out = 0;
  527. }
  528. for (node = engine->clients; engine->process_errors == 0 && node; ) {
  529. client = (jack_client_internal_t *) node->data;
  530. DEBUG ("considering client %s for processing",
  531. client->control->name);
  532. if (!client->control->active || client->control->dead) {
  533. node = jack_slist_next (node);
  534. } else if (jack_client_is_internal (client)) {
  535. node = jack_process_internal (engine, node, nframes);
  536. } else {
  537. node = jack_process_external (engine, node);
  538. }
  539. }
  540. return engine->process_errors > 0;
  541. }
  542. static void
  543. jack_calc_cpu_load(jack_engine_t *engine)
  544. {
  545. jack_time_t cycle_end = jack_get_microseconds ();
  546. /* store the execution time for later averaging */
  547. engine->rolling_client_usecs[engine->rolling_client_usecs_index++] =
  548. cycle_end - engine->control->current_time.usecs;
  549. if (engine->rolling_client_usecs_index >= JACK_ENGINE_ROLLING_COUNT) {
  550. engine->rolling_client_usecs_index = 0;
  551. }
  552. /* every so often, recompute the current maximum use over the
  553. last JACK_ENGINE_ROLLING_COUNT client iterations.
  554. */
  555. if (++engine->rolling_client_usecs_cnt
  556. % engine->rolling_interval == 0) {
  557. float max_usecs = 0.0f;
  558. int i;
  559. for (i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  560. if (engine->rolling_client_usecs[i] > max_usecs) {
  561. max_usecs = engine->rolling_client_usecs[i];
  562. }
  563. }
  564. if (max_usecs < engine->driver->period_usecs) {
  565. engine->spare_usecs =
  566. engine->driver->period_usecs - max_usecs;
  567. } else {
  568. engine->spare_usecs = 0;
  569. }
  570. engine->control->cpu_load =
  571. (1.0f - (engine->spare_usecs /
  572. engine->driver->period_usecs)) * 50.0f
  573. + (engine->control->cpu_load * 0.5f);
  574. if (engine->verbose) {
  575. fprintf (stderr, "load = %.4f max usecs: %.3f, "
  576. "spare = %.3f\n", engine->control->cpu_load,
  577. max_usecs, engine->spare_usecs);
  578. }
  579. }
  580. }
  581. static void
  582. jack_remove_clients (jack_engine_t* engine)
  583. {
  584. JSList *tmp, *node;
  585. int need_sort = FALSE;
  586. jack_client_internal_t *client;
  587. /* remove all dead clients */
  588. for (node = engine->clients; node; ) {
  589. tmp = jack_slist_next (node);
  590. client = (jack_client_internal_t *) node->data;
  591. if (client->error) {
  592. /* if we have a communication problem with the
  593. client, remove it. otherwise, turn it into
  594. a zombie. the client will/should realize
  595. this and will close its sockets. then
  596. we'll end up back here again and will
  597. finally remove the client.
  598. */
  599. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  600. if (engine->verbose) {
  601. fprintf (stderr, "removing failed "
  602. "client %s state = %s errors"
  603. " = %d\n",
  604. client->control->name,
  605. client_state_names[
  606. client->control->state
  607. ],
  608. client->error);
  609. }
  610. jack_remove_client (engine,
  611. (jack_client_internal_t *)
  612. node->data);
  613. } else {
  614. if (engine->verbose) {
  615. fprintf (stderr, "zombifying failed "
  616. "client %s state = %s errors"
  617. " = %d\n",
  618. client->control->name,
  619. client_state_names[
  620. client->control->state
  621. ],
  622. client->error);
  623. }
  624. jack_zombify_client (engine,
  625. (jack_client_internal_t *)
  626. node->data);
  627. client->error = 0;
  628. }
  629. need_sort = TRUE;
  630. }
  631. node = tmp;
  632. }
  633. if (need_sort) {
  634. jack_sort_graph (engine);
  635. }
  636. jack_engine_reset_rolling_usecs (engine);
  637. }
  638. static void
  639. jack_engine_post_process (jack_engine_t *engine)
  640. {
  641. /* precondition: caller holds the graph lock. */
  642. jack_client_control_t *ctl;
  643. jack_client_internal_t *client;
  644. JSList *node;
  645. int need_remove = FALSE;
  646. jack_transport_cycle_end (engine);
  647. /* find any clients that need removal due to timeouts, etc. */
  648. for (node = engine->clients; node; node = jack_slist_next (node) ) {
  649. client = (jack_client_internal_t *) node->data;
  650. ctl = client->control;
  651. /* this check is invalid for internal clients and
  652. external clients with no process callback.
  653. */
  654. if (!jack_client_is_internal (client) && ctl->process) {
  655. if (ctl->awake_at != 0 &&
  656. ctl->state > NotTriggered &&
  657. ctl->state != Finished &&
  658. ctl->timed_out++) {
  659. fprintf (stderr, "client %s error: awake_at = %"
  660. PRIu64
  661. " state = %d timed_out = %d\n",
  662. ctl->name,
  663. ctl->awake_at,
  664. ctl->state,
  665. ctl->timed_out);
  666. client->error++;
  667. }
  668. }
  669. if (client->error) {
  670. need_remove = TRUE;
  671. }
  672. }
  673. if (need_remove) {
  674. jack_remove_clients (engine);
  675. }
  676. jack_calc_cpu_load (engine);
  677. }
  678. static int
  679. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client,
  680. const char *so_name)
  681. {
  682. const char *errstr;
  683. char path_to_so[PATH_MAX+1];
  684. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  685. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  686. if (client->handle == 0) {
  687. if ((errstr = dlerror ()) != 0) {
  688. jack_error ("can't load \"%s\": %s", path_to_so,
  689. errstr);
  690. } else {
  691. jack_error ("bizarre error loading shared object %s",
  692. so_name);
  693. }
  694. return -1;
  695. }
  696. client->initialize = dlsym (client->handle, "jack_initialize");
  697. if ((errstr = dlerror ()) != 0) {
  698. jack_error ("no initialize function in shared object %s\n",
  699. so_name);
  700. dlclose (client->handle);
  701. client->handle = 0;
  702. return -1;
  703. }
  704. client->finish = (void (*)(void *)) dlsym (client->handle,
  705. "jack_finish");
  706. if ((errstr = dlerror ()) != 0) {
  707. jack_error ("no finish function in in shared object %s",
  708. so_name);
  709. dlclose (client->handle);
  710. client->handle = 0;
  711. return -1;
  712. }
  713. return 0;
  714. }
  715. static void
  716. jack_client_unload (jack_client_internal_t *client)
  717. {
  718. if (client->handle) {
  719. if (client->finish) {
  720. client->finish (client->control->process_arg);
  721. }
  722. dlclose (client->handle);
  723. }
  724. }
  725. static jack_client_internal_t *
  726. setup_client (jack_engine_t *engine, int client_fd,
  727. jack_client_connect_request_t *req,
  728. jack_client_connect_result_t *res)
  729. {
  730. JSList *node;
  731. jack_client_internal_t *client = NULL;
  732. for (node = engine->clients; node; node = jack_slist_next (node)) {
  733. client = (jack_client_internal_t *) node->data;
  734. if (strncmp(req->name, (char*)client->control->name,
  735. sizeof(req->name)) == 0) {
  736. jack_error ("cannot create new client; %s already"
  737. " exists", client->control->name);
  738. return NULL;
  739. }
  740. }
  741. if ((client = jack_client_internal_new (engine, client_fd, req))
  742. == NULL) {
  743. jack_error ("cannot create new client object");
  744. return 0;
  745. }
  746. if (engine->verbose) {
  747. fprintf (stderr, "new client: %s, id = %" PRIu32
  748. " type %d @ %p fd = %d\n",
  749. client->control->name, client->control->id,
  750. req->type, client->control, client_fd);
  751. }
  752. res->protocol_v = jack_protocol_version;
  753. strcpy (res->client_shm_name, client->shm_name);
  754. strcpy (res->control_shm_name, engine->control_shm_name);
  755. res->control_size = engine->control_size;
  756. res->realtime = engine->control->real_time;
  757. res->realtime_priority = engine->rtpriority - 1;
  758. res->n_port_types = engine->control->n_port_types;
  759. #if defined(__APPLE__) && defined(__POWERPC__)
  760. /* specific resources for server/client real-time thread
  761. * communication */
  762. res->portnum = client->portnum;
  763. #endif
  764. if (jack_client_is_internal(client)) {
  765. /* set up the pointers necessary for the request system
  766. to work.
  767. */
  768. client->control->deliver_request = internal_client_request;
  769. client->control->deliver_arg = engine;
  770. /* the client is in the same address space */
  771. res->client_control = client->control;
  772. res->engine_control = engine->control;
  773. } else {
  774. strcpy (res->fifo_prefix, engine->fifo_prefix);
  775. }
  776. jack_lock_graph (engine);
  777. engine->clients = jack_slist_prepend (engine->clients, client);
  778. jack_engine_reset_rolling_usecs (engine);
  779. switch (client->control->type) {
  780. case ClientDriver:
  781. case ClientInternal:
  782. /* an internal client still needs to be able to make
  783. regular JACK API calls, which need a jack_client_t
  784. structure. create one here for it.
  785. */
  786. client->control->private_client =
  787. jack_client_alloc_internal (client->control,
  788. engine->control);
  789. jack_unlock_graph (engine);
  790. /* call its initialization function */
  791. if (client->control->type == ClientInternal) {
  792. unsigned long i;
  793. /* tell it about current port types and their
  794. * shared memory information */
  795. for (i = 0; i < engine->control->n_port_types; ++i) {
  796. jack_client_handle_new_port_type (
  797. client->control->private_client,
  798. engine->control->port_types[i]
  799. .shm_info.shm_name,
  800. engine->control->port_types[i]
  801. .shm_info.size,
  802. engine->control->port_types[i]
  803. .shm_info.address);
  804. }
  805. if (client->initialize (client->control->private_client,
  806. req->object_data)) {
  807. jack_client_delete (engine, client);
  808. return 0;
  809. }
  810. }
  811. /* its good to go */
  812. break;
  813. default:
  814. if (engine->pfd_max >= engine->pfd_size) {
  815. engine->pfd = (struct pollfd *)
  816. realloc (engine->pfd, sizeof (struct pollfd)
  817. * engine->pfd_size + 16);
  818. engine->pfd_size += 16;
  819. }
  820. engine->pfd[engine->pfd_max].fd = client->request_fd;
  821. engine->pfd[engine->pfd_max].events =
  822. POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  823. engine->pfd_max++;
  824. jack_unlock_graph (engine);
  825. break;
  826. }
  827. return client;
  828. }
  829. static jack_driver_info_t *
  830. jack_load_driver (jack_engine_t *engine, char *so_name)
  831. {
  832. const char *errstr;
  833. char path_to_so[PATH_MAX+1];
  834. jack_driver_info_t *info;
  835. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  836. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/jack_%s.so",
  837. so_name);
  838. info->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  839. if (info->handle == NULL) {
  840. if ((errstr = dlerror ()) != 0) {
  841. jack_error ("can't load \"%s\": %s", path_to_so,
  842. errstr);
  843. } else {
  844. jack_error ("bizarre error loading driver shared "
  845. "object %s", path_to_so);
  846. }
  847. goto fail;
  848. }
  849. info->initialize = dlsym (info->handle, "driver_initialize");
  850. if ((errstr = dlerror ()) != 0) {
  851. jack_error ("no initialize function in shared object %s\n",
  852. path_to_so);
  853. goto fail;
  854. }
  855. info->finish = dlsym (info->handle, "driver_finish");
  856. if ((errstr = dlerror ()) != 0) {
  857. jack_error ("no finish function in in shared driver object %s",
  858. path_to_so);
  859. goto fail;
  860. }
  861. info->client_name = (char *) dlsym (info->handle, "driver_client_name");
  862. if ((errstr = dlerror ()) != 0) {
  863. jack_error ("no client name in in shared driver object %s",
  864. path_to_so);
  865. goto fail;
  866. }
  867. return info;
  868. fail:
  869. if (info->handle) {
  870. dlclose (info->handle);
  871. }
  872. free (info);
  873. return NULL;
  874. }
  875. void
  876. jack_driver_unload (jack_driver_t *driver)
  877. {
  878. driver->finish (driver);
  879. dlclose (driver->handle);
  880. }
  881. int
  882. jack_engine_load_driver (jack_engine_t *engine, int argc, char *argv[])
  883. {
  884. jack_client_connect_request_t req;
  885. jack_client_connect_result_t res;
  886. jack_client_internal_t *client;
  887. jack_driver_t *driver;
  888. jack_driver_info_t *info;
  889. if ((info = jack_load_driver (engine, argv[0])) == NULL) {
  890. return -1;
  891. }
  892. req.type = ClientDriver;
  893. snprintf (req.name, sizeof (req.name), "%s", info->client_name);
  894. if ((client = setup_client (engine, -1, &req, &res)) == NULL) {
  895. return -1;
  896. }
  897. if ((driver = info->initialize (client->control->private_client,
  898. argc, argv)) != 0) {
  899. driver->handle = info->handle;
  900. driver->finish = info->finish;
  901. }
  902. free (info);
  903. if (jack_use_driver (engine, driver)) {
  904. jack_driver_unload (driver);
  905. jack_client_delete (engine, client);
  906. return -1;
  907. }
  908. return 0;
  909. }
  910. static int
  911. handle_unload_client (jack_engine_t *engine, int client_fd,
  912. jack_client_connect_request_t *req)
  913. {
  914. JSList *node;
  915. jack_client_connect_result_t res;
  916. res.status = -1;
  917. if (engine->verbose) {
  918. fprintf (stderr, "unloading client \"%s\"\n", req->name);
  919. }
  920. jack_lock_graph (engine);
  921. for (node = engine->clients; node; node = jack_slist_next (node)) {
  922. if (strcmp ((char *) ((jack_client_internal_t *)
  923. node->data)->control->name,
  924. req->name) == 0) {
  925. jack_remove_client (engine, (jack_client_internal_t *)
  926. node->data);
  927. res.status = 0;
  928. break;
  929. }
  930. }
  931. jack_unlock_graph (engine);
  932. return 0;
  933. }
  934. static int
  935. handle_new_client (jack_engine_t *engine, int client_fd)
  936. {
  937. jack_client_internal_t *client;
  938. jack_client_connect_request_t req;
  939. jack_client_connect_result_t res;
  940. unsigned long i;
  941. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  942. jack_error ("cannot read connection request from client");
  943. return -1;
  944. }
  945. if (!req.load) {
  946. return handle_unload_client (engine, client_fd, &req);
  947. }
  948. if ((client = setup_client (engine, client_fd, &req, &res)) == NULL) {
  949. return -1;
  950. }
  951. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  952. jack_error ("cannot write connection response to client");
  953. jack_client_delete (engine, client);
  954. return -1;
  955. }
  956. /* give external clients a chance to find out about all known
  957. * port types */
  958. switch (client->control->type) {
  959. case ClientDriver:
  960. case ClientInternal:
  961. close (client_fd);
  962. break;
  963. default:
  964. for (i = 0; i < engine->control->n_port_types; ++i) {
  965. if (write (client->request_fd,
  966. &engine->control->port_types[i],
  967. sizeof (jack_port_type_info_t))
  968. != sizeof (jack_port_type_info_t)) {
  969. jack_error ("cannot send port type information"
  970. " to new client");
  971. jack_client_delete (engine, client);
  972. }
  973. }
  974. break;
  975. }
  976. return 0;
  977. }
  978. static int
  979. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  980. {
  981. jack_client_internal_t *client;
  982. jack_client_connect_ack_request_t req;
  983. jack_client_connect_ack_result_t res;
  984. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  985. jack_error ("cannot read ACK connection request from client");
  986. return -1;
  987. }
  988. if ((client = jack_client_internal_by_id (engine, req.client_id))
  989. == NULL) {
  990. jack_error ("unknown client ID in ACK connection request");
  991. return -1;
  992. }
  993. client->event_fd = client_fd;
  994. res.status = 0;
  995. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  996. jack_error ("cannot write ACK connection response to client");
  997. return -1;
  998. }
  999. return 0;
  1000. }
  1001. #ifdef USE_CAPABILITIES
  1002. static int check_capabilities (jack_engine_t *engine)
  1003. {
  1004. cap_t caps = cap_init();
  1005. cap_flag_value_t cap;
  1006. pid_t pid;
  1007. int have_all_caps = 1;
  1008. if (caps == NULL) {
  1009. if (engine->verbose) {
  1010. fprintf (stderr, "check: could not allocate capability"
  1011. " working storage\n");
  1012. }
  1013. return 0;
  1014. }
  1015. pid = getpid ();
  1016. cap_clear (caps);
  1017. if (capgetp (pid, caps)) {
  1018. if (engine->verbose) {
  1019. fprintf (stderr, "check: could not get capabilities "
  1020. "for process %d\n", pid);
  1021. }
  1022. return 0;
  1023. }
  1024. /* check that we are able to give capabilites to other processes */
  1025. cap_get_flag(caps, CAP_SETPCAP, CAP_EFFECTIVE, &cap);
  1026. if (cap == CAP_CLEAR) {
  1027. have_all_caps = 0;
  1028. goto done;
  1029. }
  1030. /* check that we have the capabilities we want to transfer */
  1031. cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &cap);
  1032. if (cap == CAP_CLEAR) {
  1033. have_all_caps = 0;
  1034. goto done;
  1035. }
  1036. cap_get_flag(caps, CAP_SYS_RESOURCE, CAP_EFFECTIVE, &cap);
  1037. if (cap == CAP_CLEAR) {
  1038. have_all_caps = 0;
  1039. goto done;
  1040. }
  1041. cap_get_flag(caps, CAP_IPC_LOCK, CAP_EFFECTIVE, &cap);
  1042. if (cap == CAP_CLEAR) {
  1043. have_all_caps = 0;
  1044. goto done;
  1045. }
  1046. done:
  1047. cap_free (caps);
  1048. return have_all_caps;
  1049. }
  1050. static int give_capabilities (jack_engine_t *engine, pid_t pid)
  1051. {
  1052. cap_t caps = cap_init();
  1053. const unsigned caps_size = 3;
  1054. cap_value_t cap_list[] = {CAP_SYS_NICE, CAP_SYS_RESOURCE, CAP_IPC_LOCK};
  1055. if (caps == NULL) {
  1056. if (engine->verbose) {
  1057. fprintf (stderr, "give: could not allocate capability"
  1058. " working storage\n");
  1059. }
  1060. return -1;
  1061. }
  1062. cap_clear(caps);
  1063. if (capgetp (pid, caps)) {
  1064. if (engine->verbose) {
  1065. fprintf (stderr, "give: could not get current "
  1066. "capabilities for process %d\n", pid);
  1067. }
  1068. cap_clear(caps);
  1069. }
  1070. cap_set_flag(caps, CAP_EFFECTIVE, caps_size, cap_list , CAP_SET);
  1071. cap_set_flag(caps, CAP_INHERITABLE, caps_size, cap_list , CAP_SET);
  1072. cap_set_flag(caps, CAP_PERMITTED, caps_size, cap_list , CAP_SET);
  1073. if (capsetp (pid, caps)) {
  1074. cap_free (caps);
  1075. return -1;
  1076. }
  1077. cap_free (caps);
  1078. return 0;
  1079. }
  1080. static int
  1081. jack_set_client_capabilities (jack_engine_t *engine, jack_client_id_t id)
  1082. {
  1083. JSList *node;
  1084. int ret = -1;
  1085. jack_lock_graph (engine);
  1086. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1087. jack_client_internal_t *client =
  1088. (jack_client_internal_t *) node->data;
  1089. if (client->control->id == id) {
  1090. /* before sending this request the client has
  1091. already checked that the engine has
  1092. realtime capabilities, that it is running
  1093. realtime and that the pid is defined
  1094. */
  1095. ret = give_capabilities (engine, client->control->pid);
  1096. if (ret) {
  1097. jack_error ("could not give capabilities to "
  1098. "process %d\n",
  1099. client->control->pid);
  1100. } else {
  1101. if (engine->verbose) {
  1102. fprintf (stderr, "gave capabilities to"
  1103. " process %d\n",
  1104. client->control->pid);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. jack_unlock_graph (engine);
  1110. return ret;
  1111. }
  1112. #endif
  1113. static int
  1114. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  1115. {
  1116. jack_client_internal_t *client;
  1117. JSList *node;
  1118. int ret = -1;
  1119. jack_lock_graph (engine);
  1120. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1121. if (((jack_client_internal_t *) node->data)->control->id
  1122. == id) {
  1123. client = (jack_client_internal_t *) node->data;
  1124. client->control->active = TRUE;
  1125. /* we call this to make sure the
  1126. FIFO is built+ready by the time
  1127. the client needs it. we don't
  1128. care about the return value at
  1129. this point.
  1130. */
  1131. jack_get_fifo_fd (engine,
  1132. ++engine->external_client_cnt);
  1133. jack_sort_graph (engine);
  1134. ret = 0;
  1135. break;
  1136. }
  1137. }
  1138. jack_unlock_graph (engine);
  1139. return ret;
  1140. }
  1141. static int
  1142. jack_client_do_deactivate (jack_engine_t *engine,
  1143. jack_client_internal_t *client, int sort_graph)
  1144. {
  1145. /* called must hold engine->client_lock and must have checked
  1146. for and/or cleared all connections held by client.
  1147. */
  1148. client->control->active = FALSE;
  1149. if (!jack_client_is_internal (client) &&
  1150. engine->external_client_cnt > 0) {
  1151. engine->external_client_cnt--;
  1152. }
  1153. if (sort_graph) {
  1154. jack_sort_graph (engine);
  1155. }
  1156. return 0;
  1157. }
  1158. static void
  1159. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  1160. {
  1161. JSList *node;
  1162. jack_port_internal_t *port;
  1163. /* call tree **** MUST HOLD *** engine->client_lock */
  1164. for (node = client->ports; node; node = jack_slist_next (node)) {
  1165. port = (jack_port_internal_t *) node->data;
  1166. jack_port_clear_connections (engine, port);
  1167. jack_port_release (engine, port);
  1168. }
  1169. jack_slist_free (client->ports);
  1170. jack_slist_free (client->fed_by);
  1171. client->fed_by = 0;
  1172. client->ports = 0;
  1173. }
  1174. static int
  1175. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  1176. {
  1177. JSList *node;
  1178. int ret = -1;
  1179. jack_lock_graph (engine);
  1180. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1181. jack_client_internal_t *client =
  1182. (jack_client_internal_t *) node->data;
  1183. if (client->control->id == id) {
  1184. JSList *portnode;
  1185. jack_port_internal_t *port;
  1186. jack_transport_client_exit (engine, client);
  1187. for (portnode = client->ports; portnode;
  1188. portnode = jack_slist_next (portnode)) {
  1189. port = (jack_port_internal_t *) portnode->data;
  1190. jack_port_clear_connections (engine, port);
  1191. }
  1192. ret = jack_client_do_deactivate (engine, node->data,
  1193. TRUE);
  1194. break;
  1195. }
  1196. }
  1197. jack_unlock_graph (engine);
  1198. return ret;
  1199. }
  1200. static int
  1201. handle_client_socket_error (jack_engine_t *engine, int fd)
  1202. {
  1203. jack_client_internal_t *client = 0;
  1204. JSList *node;
  1205. #ifndef DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD
  1206. jack_lock_graph (engine);
  1207. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1208. if (jack_client_is_internal((jack_client_internal_t *)
  1209. node->data)) {
  1210. continue;
  1211. }
  1212. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1213. client = (jack_client_internal_t *) node->data;
  1214. break;
  1215. }
  1216. }
  1217. if (client) {
  1218. if (engine->verbose) {
  1219. fprintf (stderr, "removing failed client %s state = "
  1220. "%s errors = %d\n", client->control->name,
  1221. client_state_names[client->control->state],
  1222. client->error);
  1223. }
  1224. jack_remove_client(engine, client);
  1225. jack_sort_graph (engine);
  1226. }
  1227. jack_unlock_graph (engine);
  1228. #else
  1229. jack_lock_graph (engine);
  1230. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1231. if (jack_client_is_internal((jack_client_internal_t *)
  1232. node->data)) {
  1233. continue;
  1234. }
  1235. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1236. client = (jack_client_internal_t *) node->data;
  1237. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  1238. client->error += JACK_ERROR_WITH_SOCKETS;
  1239. }
  1240. break;
  1241. }
  1242. }
  1243. jack_unlock_graph (engine);
  1244. #endif
  1245. return 0;
  1246. }
  1247. static void
  1248. do_request (jack_engine_t *engine, jack_request_t *req, int *reply_fd)
  1249. {
  1250. pthread_mutex_lock (&engine->request_lock);
  1251. DEBUG ("got a request of type %d", req->type);
  1252. switch (req->type) {
  1253. case RegisterPort:
  1254. req->status = jack_port_do_register (engine, req);
  1255. break;
  1256. case UnRegisterPort:
  1257. req->status = jack_port_do_unregister (engine, req);
  1258. break;
  1259. case ConnectPorts:
  1260. req->status = jack_port_do_connect
  1261. (engine, req->x.connect.source_port,
  1262. req->x.connect.destination_port);
  1263. break;
  1264. case DisconnectPort:
  1265. req->status = jack_port_do_disconnect_all
  1266. (engine, req->x.port_info.port_id);
  1267. break;
  1268. case DisconnectPorts:
  1269. req->status = jack_port_do_disconnect
  1270. (engine, req->x.connect.source_port,
  1271. req->x.connect.destination_port);
  1272. break;
  1273. case ActivateClient:
  1274. req->status = jack_client_activate (engine, req->x.client_id);
  1275. break;
  1276. case DeactivateClient:
  1277. req->status = jack_client_deactivate (engine, req->x.client_id);
  1278. break;
  1279. case SetTimeBaseClient:
  1280. req->status = jack_timebase_set (engine,
  1281. req->x.timebase.client_id,
  1282. req->x.timebase.conditional);
  1283. break;
  1284. case ResetTimeBaseClient:
  1285. req->status = jack_timebase_reset (engine, req->x.client_id);
  1286. break;
  1287. case SetSyncClient:
  1288. req->status =
  1289. jack_transport_client_set_sync (engine,
  1290. req->x.client_id);
  1291. break;
  1292. case ResetSyncClient:
  1293. req->status =
  1294. jack_transport_client_reset_sync (engine,
  1295. req->x.client_id);
  1296. break;
  1297. case SetSyncTimeout:
  1298. req->status = jack_transport_set_sync_timeout (engine,
  1299. req->x.timeout);
  1300. break;
  1301. #ifdef USE_CAPABILITIES
  1302. case SetClientCapabilities:
  1303. req->status = jack_set_client_capabilities (engine,
  1304. req->x.client_id);
  1305. break;
  1306. #endif
  1307. case GetPortConnections:
  1308. case GetPortNConnections:
  1309. if ((req->status =
  1310. jack_do_get_port_connections (engine, req, *reply_fd))
  1311. == 0) {
  1312. /* we have already replied, don't do it again */
  1313. *reply_fd = -1;
  1314. }
  1315. break;
  1316. default:
  1317. /* some requests are handled entirely on the client
  1318. * side, by adjusting the shared memory area(s) */
  1319. break;
  1320. }
  1321. pthread_mutex_unlock (&engine->request_lock);
  1322. DEBUG ("status of request: %d", req->status);
  1323. }
  1324. static int
  1325. internal_client_request (void* ptr, jack_request_t *request)
  1326. {
  1327. do_request ((jack_engine_t*) ptr, request, 0);
  1328. return request->status;
  1329. }
  1330. static int
  1331. handle_external_client_request (jack_engine_t *engine, int fd)
  1332. {
  1333. jack_request_t req;
  1334. jack_client_internal_t *client = 0;
  1335. int reply_fd;
  1336. JSList *node;
  1337. ssize_t r;
  1338. DEBUG ("HIT: before lock");
  1339. jack_lock_graph (engine);
  1340. DEBUG ("HIT: before for");
  1341. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1342. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1343. DEBUG ("HIT: in for");
  1344. client = (jack_client_internal_t *) node->data;
  1345. break;
  1346. }
  1347. }
  1348. DEBUG ("HIT: after for");
  1349. jack_unlock_graph (engine);
  1350. if (client == NULL) {
  1351. jack_error ("client input on unknown fd %d!", fd);
  1352. return -1;
  1353. }
  1354. if ((r = read (client->request_fd, &req, sizeof (req)))
  1355. < (ssize_t) sizeof (req)) {
  1356. jack_error ("cannot read request from client (%d/%d/%s)",
  1357. r, sizeof(req), strerror (errno));
  1358. client->error++;
  1359. return -1;
  1360. }
  1361. reply_fd = client->request_fd;
  1362. do_request (engine, &req, &reply_fd);
  1363. if (reply_fd >= 0) {
  1364. DEBUG ("replying to client");
  1365. if (write (reply_fd, &req, sizeof (req))
  1366. < (ssize_t) sizeof (req)) {
  1367. jack_error ("cannot write request result to client");
  1368. return -1;
  1369. }
  1370. } else {
  1371. DEBUG ("*not* replying to client");
  1372. }
  1373. return 0;
  1374. }
  1375. static void *
  1376. jack_server_thread (void *arg)
  1377. {
  1378. jack_engine_t *engine = (jack_engine_t *) arg;
  1379. struct sockaddr_un client_addr;
  1380. socklen_t client_addrlen;
  1381. struct pollfd *pfd;
  1382. int client_socket;
  1383. int done = 0;
  1384. int i;
  1385. int max;
  1386. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1387. engine->pfd[0].fd = engine->fds[0];
  1388. engine->pfd[0].events = POLLIN|POLLERR;
  1389. engine->pfd[1].fd = engine->fds[1];
  1390. engine->pfd[1].events = POLLIN|POLLERR;
  1391. engine->pfd_max = 2;
  1392. while (!done) {
  1393. DEBUG ("start while");
  1394. /* XXX race here with new external clients
  1395. causing engine->pfd to be reallocated.
  1396. I don't know how to solve this
  1397. short of copying the entire
  1398. contents of the pfd struct. Ick.
  1399. */
  1400. max = engine->pfd_max;
  1401. pfd = engine->pfd;
  1402. if (poll (pfd, max, 10000) < 0) {
  1403. if (errno == EINTR) {
  1404. continue;
  1405. }
  1406. jack_error ("poll failed (%s)", strerror (errno));
  1407. break;
  1408. }
  1409. /* check each client socket before handling other request*/
  1410. for (i = 2; i < max; i++) {
  1411. if (pfd[i].fd < 0) {
  1412. continue;
  1413. }
  1414. if (pfd[i].revents & ~POLLIN) {
  1415. handle_client_socket_error (engine, pfd[i].fd);
  1416. } else if (pfd[i].revents & POLLIN) {
  1417. if (handle_external_client_request
  1418. (engine, pfd[i].fd)) {
  1419. jack_error ("could not handle external"
  1420. " client request");
  1421. #if defined(__APPLE__) && defined(__POWERPC__)
  1422. /* poll is implemented using
  1423. select (see the fakepool
  1424. code). When the socket is closed
  1425. select does not return any error,
  1426. POLLIN is true and the next read
  1427. will return 0 bytes. This
  1428. behaviour is diffrent from the
  1429. Linux poll behaviour. Thus we use
  1430. this condition as a socket error
  1431. and remove the client.
  1432. */
  1433. handle_client_socket_error(engine,
  1434. pfd[i].fd);
  1435. #endif
  1436. }
  1437. }
  1438. }
  1439. /* check the master server socket */
  1440. if (pfd[0].revents & POLLERR) {
  1441. jack_error ("error on server socket");
  1442. break;
  1443. }
  1444. if (pfd[0].revents & POLLIN) {
  1445. DEBUG ("pfd[0].revents & POLLIN");
  1446. memset (&client_addr, 0, sizeof (client_addr));
  1447. client_addrlen = sizeof (client_addr);
  1448. if ((client_socket =
  1449. accept (engine->fds[0],
  1450. (struct sockaddr *) &client_addr,
  1451. &client_addrlen)) < 0) {
  1452. jack_error ("cannot accept new connection (%s)",
  1453. strerror (errno));
  1454. } else if (handle_new_client (engine, client_socket)
  1455. < 0) {
  1456. jack_error ("cannot complete new client "
  1457. "connection process");
  1458. close (client_socket);
  1459. }
  1460. }
  1461. /* check the ACK server socket */
  1462. if (pfd[1].revents & POLLERR) {
  1463. jack_error ("error on server ACK socket");
  1464. break;
  1465. }
  1466. if (pfd[1].revents & POLLIN) {
  1467. DEBUG ("pfd[1].revents & POLLIN");
  1468. memset (&client_addr, 0, sizeof (client_addr));
  1469. client_addrlen = sizeof (client_addr);
  1470. if ((client_socket =
  1471. accept (engine->fds[1],
  1472. (struct sockaddr *) &client_addr,
  1473. &client_addrlen)) < 0) {
  1474. jack_error ("cannot accept new ACK connection"
  1475. " (%s)", strerror (errno));
  1476. } else if (handle_client_ack_connection
  1477. (engine, client_socket)) {
  1478. jack_error ("cannot complete client ACK "
  1479. "connection process");
  1480. close (client_socket);
  1481. }
  1482. }
  1483. }
  1484. return 0;
  1485. }
  1486. jack_engine_t *
  1487. jack_engine_new (int realtime, int rtpriority, int verbose, int client_timeout)
  1488. {
  1489. jack_engine_t *engine;
  1490. void *addr;
  1491. unsigned int i;
  1492. int shmid;
  1493. int perm;
  1494. #ifdef USE_CAPABILITIES
  1495. uid_t uid = getuid ();
  1496. uid_t euid = geteuid ();
  1497. #endif
  1498. jack_init_time ();
  1499. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1500. engine->driver = 0;
  1501. engine->set_sample_rate = jack_set_sample_rate;
  1502. engine->set_buffer_size = jack_set_buffer_size;
  1503. engine->run_cycle = jack_run_cycle;
  1504. engine->transport_cycle_start = jack_transport_cycle_start;
  1505. engine->client_timeout_msecs = client_timeout;
  1506. engine->next_client_id = 1;
  1507. engine->port_max = 128;
  1508. engine->rtpriority = rtpriority;
  1509. engine->silent_buffer = 0;
  1510. engine->verbose = verbose;
  1511. engine->asio_mode = FALSE;
  1512. jack_engine_reset_rolling_usecs (engine);
  1513. pthread_mutex_init (&engine->client_lock, 0);
  1514. pthread_mutex_init (&engine->port_lock, 0);
  1515. pthread_mutex_init (&engine->request_lock, 0);
  1516. engine->clients = 0;
  1517. engine->pfd_size = 16;
  1518. engine->pfd_max = 0;
  1519. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1520. * engine->pfd_size);
  1521. engine->fifo_size = 16;
  1522. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1523. for (i = 0; i < engine->fifo_size; i++) {
  1524. engine->fifo[i] = -1;
  1525. }
  1526. engine->external_client_cnt = 0;
  1527. srandom (time ((time_t *) 0));
  1528. snprintf (engine->control_shm_name,
  1529. sizeof (engine->control_shm_name), "/jack-engine");
  1530. engine->control_size = sizeof (jack_control_t) +
  1531. (sizeof (jack_port_shared_t) * engine->port_max);
  1532. if (jack_initialize_shm (engine)) {
  1533. return 0;
  1534. }
  1535. #if defined(__APPLE__) && defined(__POWERPC__)
  1536. /* using O_TRUNC option does not work on Darwin */
  1537. perm = O_RDWR|O_CREAT;
  1538. #else
  1539. perm = O_RDWR|O_CREAT|O_TRUNC;
  1540. #endif
  1541. if ((addr = jack_get_shm (engine->control_shm_name,
  1542. engine->control_size,
  1543. perm, 0644, PROT_READ|PROT_WRITE, &shmid))
  1544. == MAP_FAILED) {
  1545. jack_error ("cannot create engine control shared memory "
  1546. "segment (%s)", strerror (errno));
  1547. return 0;
  1548. }
  1549. jack_register_shm (engine->control_shm_name, addr, shmid);
  1550. engine->control = (jack_control_t *) addr;
  1551. /* Setup port type information from builtins. buffer space is
  1552. * allocated when the driver calls jack_driver_buffer_size().
  1553. */
  1554. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1555. memcpy (&engine->control->port_types[i],
  1556. &jack_builtin_port_types[i],
  1557. sizeof (jack_port_type_info_t));
  1558. /* set offset into port_types array */
  1559. engine->control->port_types[i].type_id = i;
  1560. /* be sure to initialize mutex correctly */
  1561. pthread_mutex_init (&engine->control->port_types[i].buffer_lock,
  1562. NULL);
  1563. /* indicate no shared memory allocation for this port type */
  1564. engine->control->port_types[i].shm_info.size = 0;
  1565. }
  1566. engine->control->n_port_types = i;
  1567. /* Mark all ports as available */
  1568. for (i = 0; i < engine->port_max; i++) {
  1569. engine->control->ports[i].in_use = 0;
  1570. engine->control->ports[i].id = i;
  1571. }
  1572. /* allocate internal port structures so that we can keep track
  1573. * of port connections.
  1574. */
  1575. engine->internal_ports = (jack_port_internal_t *)
  1576. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1577. for (i = 0; i < engine->port_max; i++) {
  1578. engine->internal_ports[i].connections = 0;
  1579. }
  1580. if (make_sockets (engine->fds) < 0) {
  1581. jack_error ("cannot create server sockets");
  1582. return 0;
  1583. }
  1584. engine->control->port_max = engine->port_max;
  1585. engine->control->real_time = realtime;
  1586. engine->control->client_priority = engine->rtpriority - 1;
  1587. engine->control->cpu_load = 0;
  1588. engine->control->buffer_size = 0;
  1589. jack_set_sample_rate (engine, 0);
  1590. jack_transport_init (engine);
  1591. engine->control->internal = 0;
  1592. engine->control->has_capabilities = 0;
  1593. #if defined(__APPLE__) && defined(__POWERPC__)
  1594. /* specific ressources for server/client real-time thread
  1595. * communication */
  1596. engine->servertask = mach_task_self();
  1597. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1598. jack_error("Jackd: Can't find bootstrap mach port");
  1599. return 0;
  1600. }
  1601. engine->portnum = 0;
  1602. #endif
  1603. #ifdef USE_CAPABILITIES
  1604. if (uid == 0 || euid == 0) {
  1605. if (engine->verbose) {
  1606. fprintf (stderr, "running with uid=%d and euid=%d, "
  1607. "will not try to use capabilites\n",
  1608. uid, euid);
  1609. }
  1610. } else {
  1611. /* only try to use capabilities if not running as root */
  1612. engine->control->has_capabilities = check_capabilities (engine);
  1613. if (engine->control->has_capabilities == 0) {
  1614. if (engine->verbose) {
  1615. fprintf (stderr, "required capabilities not "
  1616. "available\n");
  1617. }
  1618. }
  1619. if (engine->verbose) {
  1620. size_t size;
  1621. cap_t cap = cap_init();
  1622. capgetp(0, cap);
  1623. fprintf (stderr, "capabilities: %s\n",
  1624. cap_to_text(cap, &size));
  1625. }
  1626. }
  1627. #endif
  1628. engine->control->engine_ok = 1;
  1629. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1630. "%s/jack-ack-fifo-%d", jack_server_dir, getpid());
  1631. (void) jack_get_fifo_fd (engine, 0);
  1632. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  1633. pthread_detach (engine->server_thread);
  1634. return engine;
  1635. }
  1636. static int
  1637. jack_become_real_time (pthread_t thread, int priority)
  1638. {
  1639. struct sched_param rtparam;
  1640. int x;
  1641. memset (&rtparam, 0, sizeof (rtparam));
  1642. rtparam.sched_priority = priority;
  1643. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  1644. jack_error ("cannot set thread to real-time priority (FIFO/%d)"
  1645. " (%d: %s)", rtparam.sched_priority, x,
  1646. strerror (errno));
  1647. return -1;
  1648. }
  1649. #if defined(__APPLE__) && defined(__POWERPC__)
  1650. // To be implemented
  1651. #else
  1652. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  1653. jack_error ("cannot lock down memory for RT thread (%s)",
  1654. strerror (errno));
  1655. return -1;
  1656. }
  1657. #endif
  1658. return 0;
  1659. }
  1660. #ifdef HAVE_ON_EXIT
  1661. static void
  1662. cancel_cleanup (int status, void *arg)
  1663. {
  1664. jack_engine_t *engine = (jack_engine_t *) arg;
  1665. engine->control->engine_ok = 0;
  1666. engine->driver->stop (engine->driver);
  1667. engine->driver->finish (engine->driver);
  1668. }
  1669. #else
  1670. #ifdef HAVE_ATEXIT
  1671. jack_engine_t *global_engine;
  1672. static void
  1673. cancel_cleanup (void)
  1674. {
  1675. jack_engine_t *engine = global_engine;
  1676. engine->driver->stop (engine->driver);
  1677. engine->driver->finish (engine->driver);
  1678. }
  1679. #else
  1680. #error "Don't know how to make an exit handler"
  1681. #endif /* HAVE_ATEXIT */
  1682. #endif /* HAVE_ON_EXIT */
  1683. static void *
  1684. watchdog_thread (void *arg)
  1685. {
  1686. jack_engine_t *engine = (jack_engine_t *) arg;
  1687. int watchdog_priority = (engine->rtpriority) > 89 ? 99 : engine->rtpriority + 10;
  1688. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1689. if (jack_become_real_time (pthread_self(), watchdog_priority)) {
  1690. return 0;
  1691. }
  1692. engine->watchdog_check = 0;
  1693. while (1) {
  1694. usleep (5000000);
  1695. if (engine->watchdog_check == 0) {
  1696. jack_error ("jackd watchdog: timeout - killing jackd");
  1697. /* kill the process group of the current client */
  1698. kill (-engine->current_client->control->pid, SIGKILL);
  1699. /* kill our process group */
  1700. kill (-getpgrp(), SIGKILL);
  1701. /*NOTREACHED*/
  1702. exit (1);
  1703. }
  1704. engine->watchdog_check = 0;
  1705. }
  1706. }
  1707. static int
  1708. jack_start_watchdog (jack_engine_t *engine)
  1709. {
  1710. pthread_t watchdog;
  1711. if (pthread_create (&watchdog, 0, watchdog_thread, engine)) {
  1712. jack_error ("cannot start watchdog thread");
  1713. return -1;
  1714. }
  1715. pthread_detach (watchdog);
  1716. return 0;
  1717. }
  1718. static void
  1719. jack_engine_notify_clients_about_delay (jack_engine_t *engine)
  1720. {
  1721. JSList *node;
  1722. jack_event_t event;
  1723. event.type = XRun;
  1724. jack_lock_graph (engine);
  1725. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1726. jack_deliver_event (engine,
  1727. (jack_client_internal_t *) node->data,
  1728. &event);
  1729. }
  1730. jack_unlock_graph (engine);
  1731. }
  1732. static inline void
  1733. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t amount)
  1734. {
  1735. jack_frame_timer_t *time = &engine->control->frame_timer;
  1736. // atomic_inc (&time->guard1, 1);
  1737. // really need a memory barrier here
  1738. time->guard1++;
  1739. time->frames += amount;
  1740. time->stamp = engine->driver->last_wait_ust;
  1741. // atomic_inc (&time->guard2, 1);
  1742. // might need a memory barrier here
  1743. time->guard2++;
  1744. }
  1745. static int
  1746. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1747. float delayed_usecs)
  1748. {
  1749. int restart = 0;
  1750. jack_driver_t* driver = engine->driver;
  1751. int ret = -1;
  1752. static int consecutive_excessive_delays = 0;
  1753. engine->watchdog_check = 1;
  1754. #define WORK_SCALE 1.0f
  1755. if (engine->control->real_time &&
  1756. engine->spare_usecs &&
  1757. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1758. fprintf (stderr, "delay of %.3f usecs exceeds estimated spare"
  1759. " time of %.3f; restart ...\n",
  1760. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1761. if (++consecutive_excessive_delays > 10) {
  1762. jack_error ("too many consecutive interrupt delays "
  1763. "... engine pausing");
  1764. return -1; /* will exit the thread loop */
  1765. }
  1766. if (driver->stop (driver)) {
  1767. jack_error ("cannot stop current driver");
  1768. return -1; /* will exit the thread loop */
  1769. }
  1770. jack_engine_notify_clients_about_delay (engine);
  1771. if (driver->start (driver)) {
  1772. jack_error ("cannot restart current driver after "
  1773. "delay");
  1774. return -1; /* will exit the thread loop */
  1775. }
  1776. return 0;
  1777. } else {
  1778. consecutive_excessive_delays = 0;
  1779. }
  1780. if (jack_try_lock_graph (engine)) {
  1781. /* engine can't run. just throw away an entire cycle */
  1782. driver->null_cycle (driver, nframes);
  1783. return 0;
  1784. }
  1785. if (driver->read (driver, nframes)) {
  1786. goto unlock;
  1787. }
  1788. if (jack_engine_process (engine, nframes)) {
  1789. driver->stop (driver);
  1790. restart = 1;
  1791. } else {
  1792. if (driver->write (driver, nframes)) {
  1793. goto unlock;
  1794. }
  1795. }
  1796. jack_engine_post_process (engine);
  1797. jack_inc_frame_time (engine, nframes);
  1798. ret = 0;
  1799. unlock:
  1800. jack_unlock_graph (engine);
  1801. if (restart) {
  1802. driver->start (driver);
  1803. }
  1804. return ret;
  1805. }
  1806. static void *
  1807. jack_main_thread (void *arg)
  1808. {
  1809. jack_engine_t *engine = (jack_engine_t *) arg;
  1810. jack_driver_t *driver = engine->driver;
  1811. int wait_status;
  1812. jack_nframes_t nframes;
  1813. float delayed_usecs;
  1814. if (engine->control->real_time) {
  1815. if (jack_start_watchdog (engine)) {
  1816. pthread_exit (0);
  1817. }
  1818. if (jack_become_real_time (pthread_self(),
  1819. engine->rtpriority)) {
  1820. engine->control->real_time = 0;
  1821. }
  1822. }
  1823. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1824. engine->watchdog_check = 1;
  1825. while (1) {
  1826. if ((nframes = driver->wait (driver, -1, &wait_status,
  1827. &delayed_usecs)) == 0) {
  1828. /* the driver detected an xrun and restarted */
  1829. jack_engine_notify_clients_about_delay (engine);
  1830. continue;
  1831. }
  1832. if (wait_status == 0) {
  1833. jack_nframes_t left, b_size =
  1834. engine->control->buffer_size;
  1835. if (engine->verbose) {
  1836. if (nframes != b_size) {
  1837. fprintf(stderr, "late driver wakeup: "
  1838. "nframes to process = %lu.\n",
  1839. (unsigned long int)nframes);
  1840. }
  1841. }
  1842. for(left = nframes; left >= b_size; left -= b_size) {
  1843. if (jack_run_cycle (engine, b_size,
  1844. delayed_usecs)) {
  1845. jack_error ("cycle execution failure,"
  1846. " exiting");
  1847. break;
  1848. }
  1849. }
  1850. } else if (wait_status < 0) {
  1851. break;
  1852. } else {
  1853. /* driver restarted, just continue */
  1854. }
  1855. }
  1856. pthread_exit (0);
  1857. return 0;
  1858. }
  1859. int
  1860. jack_run (jack_engine_t *engine)
  1861. {
  1862. #ifdef HAVE_ON_EXIT
  1863. on_exit (cancel_cleanup, engine);
  1864. #else
  1865. #ifdef HAVE_ATEXIT
  1866. global_engine = engine;
  1867. atexit (cancel_cleanup);
  1868. #else
  1869. #error "Don't know how to install an exit handler"
  1870. #endif /* HAVE_ATEXIT */
  1871. #endif /* HAVE_ON_EXIT */
  1872. if (engine->driver == NULL) {
  1873. jack_error ("engine driver not set; cannot start");
  1874. return -1;
  1875. }
  1876. if (engine->driver->start (engine->driver)) {
  1877. jack_error ("cannot start driver");
  1878. return -1;
  1879. }
  1880. #if defined(__APPLE__) && defined(__POWERPC__)
  1881. return 0;
  1882. #else
  1883. return pthread_create (&engine->main_thread, 0,
  1884. jack_main_thread, engine);
  1885. #endif
  1886. }
  1887. #if defined(__APPLE__) && defined(__POWERPC__)
  1888. int
  1889. jack_wait (jack_engine_t *engine)
  1890. {
  1891. while(1) sleep(1);
  1892. }
  1893. #else
  1894. int
  1895. jack_wait (jack_engine_t *engine)
  1896. {
  1897. void *ret = 0;
  1898. int err;
  1899. if ((err = pthread_join (engine->main_thread, &ret)) != 0) {
  1900. switch (err) {
  1901. case EINVAL:
  1902. jack_error ("cannot join with audio thread (thread "
  1903. "detached, or another thread is waiting)");
  1904. break;
  1905. case ESRCH:
  1906. jack_error ("cannot join with audio thread (thread "
  1907. "no longer exists)");
  1908. break;
  1909. case EDEADLK:
  1910. jack_error ("programming error: jack_wait() called by"
  1911. " audio thread");
  1912. break;
  1913. default:
  1914. jack_error ("cannot join with audio thread (%s)",
  1915. strerror (errno));
  1916. }
  1917. }
  1918. return (int) ((intptr_t)ret);
  1919. }
  1920. #endif
  1921. int
  1922. jack_engine_delete (jack_engine_t *engine)
  1923. {
  1924. if (engine) {
  1925. #if defined(__APPLE__) && defined(__POWERPC__)
  1926. /* the jack_run_cycle function is directly called from
  1927. * the CoreAudo audio callback */
  1928. return 0;
  1929. #else
  1930. return pthread_cancel (engine->main_thread);
  1931. #endif
  1932. }
  1933. return 0;
  1934. }
  1935. static jack_client_internal_t *
  1936. jack_client_internal_new (jack_engine_t *engine, int fd,
  1937. jack_client_connect_request_t *req)
  1938. {
  1939. jack_client_internal_t *client;
  1940. shm_name_t shm_name;
  1941. int shmid;
  1942. int perm;
  1943. void *addr = 0;
  1944. switch (req->type) {
  1945. case ClientInternal:
  1946. case ClientDriver:
  1947. break;
  1948. case ClientExternal:
  1949. #if defined(__APPLE__) && defined(__POWERPC__)
  1950. /* using O_TRUNC option does not work on Darwin */
  1951. perm = O_RDWR|O_CREAT;
  1952. #else
  1953. perm = O_RDWR|O_CREAT|O_TRUNC;
  1954. #endif
  1955. snprintf (shm_name, sizeof (shm_name), "/jack-c-%s", req->name);
  1956. if ((addr = jack_get_shm (shm_name,
  1957. sizeof (jack_client_control_t),
  1958. perm, 0666, PROT_READ|PROT_WRITE,
  1959. &shmid)) == MAP_FAILED) {
  1960. jack_error ("cannot create client control block for %s",
  1961. req->name);
  1962. return 0;
  1963. }
  1964. jack_register_shm (shm_name, addr, shmid);
  1965. break;
  1966. }
  1967. client = (jack_client_internal_t *)
  1968. malloc (sizeof (jack_client_internal_t));
  1969. client->request_fd = fd;
  1970. client->event_fd = -1;
  1971. client->ports = 0;
  1972. client->fed_by = 0;
  1973. client->execution_order = UINT_MAX;
  1974. client->next_client = NULL;
  1975. client->handle = NULL;
  1976. client->finish = NULL;
  1977. client->error = 0;
  1978. if (req->type != ClientExternal) {
  1979. client->control = (jack_client_control_t *)
  1980. malloc (sizeof (jack_client_control_t));
  1981. } else {
  1982. strcpy (client->shm_name, shm_name);
  1983. client->control = (jack_client_control_t *) addr;
  1984. }
  1985. client->control->type = req->type;
  1986. client->control->active = 0;
  1987. client->control->dead = FALSE;
  1988. client->control->timed_out = 0;
  1989. client->control->id = engine->next_client_id++;
  1990. strcpy ((char *) client->control->name, req->name);
  1991. client->subgraph_start_fd = -1;
  1992. client->subgraph_wait_fd = -1;
  1993. client->control->process = NULL;
  1994. client->control->process_arg = NULL;
  1995. client->control->bufsize = NULL;
  1996. client->control->bufsize_arg = NULL;
  1997. client->control->srate = NULL;
  1998. client->control->srate_arg = NULL;
  1999. client->control->xrun = NULL;
  2000. client->control->xrun_arg = NULL;
  2001. client->control->port_register = NULL;
  2002. client->control->port_register_arg = NULL;
  2003. client->control->graph_order = NULL;
  2004. client->control->graph_order_arg = NULL;
  2005. jack_transport_client_new (client);
  2006. #if defined(__APPLE__) && defined(__POWERPC__)
  2007. /* specific resources for server/client real-time thread
  2008. * communication */
  2009. allocate_mach_serverport(engine, client);
  2010. client->running = FALSE;
  2011. #endif
  2012. if (req->type == ClientInternal) {
  2013. if (jack_load_client (engine, client, req->object_path)) {
  2014. jack_error ("cannot dynamically load client from"
  2015. " \"%s\"", req->object_path);
  2016. jack_client_delete (engine, client);
  2017. return 0;
  2018. }
  2019. }
  2020. return client;
  2021. }
  2022. static void
  2023. jack_port_clear_connections (jack_engine_t *engine,
  2024. jack_port_internal_t *port)
  2025. {
  2026. JSList *node, *next;
  2027. for (node = port->connections; node; ) {
  2028. next = jack_slist_next (node);
  2029. jack_port_disconnect_internal (
  2030. engine, ((jack_connection_internal_t *)
  2031. node->data)->source,
  2032. ((jack_connection_internal_t *)
  2033. node->data)->destination,
  2034. FALSE);
  2035. node = next;
  2036. }
  2037. jack_slist_free (port->connections);
  2038. port->connections = 0;
  2039. }
  2040. static void
  2041. jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client)
  2042. {
  2043. VERBOSE (engine, "*&*&*&*&** senor %s - you are a ZOMBIE\n",
  2044. client->control->name);
  2045. /* caller must hold the client_lock */
  2046. /* this stops jack_deliver_event() from doing anything */
  2047. client->control->dead = TRUE;
  2048. jack_transport_client_exit (engine, client);
  2049. jack_client_disconnect (engine, client);
  2050. jack_client_do_deactivate (engine, client, FALSE);
  2051. }
  2052. static void
  2053. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  2054. {
  2055. unsigned int i;
  2056. JSList *node;
  2057. /* caller must hold the client_lock */
  2058. if (engine->verbose) {
  2059. fprintf (stderr, "adios senor %s\n", client->control->name);
  2060. }
  2061. /* if its not already a zombie, make it so */
  2062. if (!client->control->dead) {
  2063. jack_zombify_client (engine, client);
  2064. }
  2065. /* try to force the server thread to return from poll */
  2066. close (client->event_fd);
  2067. close (client->request_fd);
  2068. if (client->control->type == ClientExternal) {
  2069. /* rearrange the pollfd array so that things work right the
  2070. next time we go into poll(2).
  2071. */
  2072. for (i = 0; i < engine->pfd_max; i++) {
  2073. if (engine->pfd[i].fd == client->request_fd) {
  2074. if (i+1 < engine->pfd_max) {
  2075. memmove (&engine->pfd[i],
  2076. &engine->pfd[i+1],
  2077. sizeof (struct pollfd)
  2078. * (engine->pfd_max - i));
  2079. }
  2080. engine->pfd_max--;
  2081. }
  2082. }
  2083. }
  2084. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2085. if (((jack_client_internal_t *) node->data)->control->id
  2086. == client->control->id) {
  2087. engine->clients =
  2088. jack_slist_remove_link (engine->clients, node);
  2089. jack_slist_free_1 (node);
  2090. break;
  2091. }
  2092. }
  2093. jack_client_delete (engine, client);
  2094. }
  2095. static void
  2096. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  2097. {
  2098. if (jack_client_is_internal (client)) {
  2099. jack_client_unload (client);
  2100. free ((char *) client->control);
  2101. } else {
  2102. jack_destroy_shm (client->shm_name);
  2103. jack_release_shm ((char*)client->control,
  2104. sizeof (jack_client_control_t));
  2105. }
  2106. free (client);
  2107. }
  2108. jack_client_internal_t *
  2109. jack_client_by_name (jack_engine_t *engine, const char *name)
  2110. {
  2111. jack_client_internal_t *client = NULL;
  2112. JSList *node;
  2113. jack_lock_graph (engine);
  2114. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2115. if (strcmp ((const char *) ((jack_client_internal_t *)
  2116. node->data)->control->name,
  2117. name) == 0) {
  2118. client = (jack_client_internal_t *) node->data;
  2119. break;
  2120. }
  2121. }
  2122. jack_unlock_graph (engine);
  2123. return client;
  2124. }
  2125. jack_client_internal_t *
  2126. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  2127. {
  2128. jack_client_internal_t *client = NULL;
  2129. JSList *node;
  2130. /* call tree ***MUST HOLD*** the graph lock */
  2131. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2132. if (((jack_client_internal_t *) node->data)->control->id
  2133. == id) {
  2134. client = (jack_client_internal_t *) node->data;
  2135. break;
  2136. }
  2137. }
  2138. return client;
  2139. }
  2140. static void
  2141. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  2142. {
  2143. JSList *node;
  2144. jack_lock_graph (engine);
  2145. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2146. jack_deliver_event (engine,
  2147. (jack_client_internal_t *) node->data,
  2148. event);
  2149. }
  2150. jack_unlock_graph (engine);
  2151. }
  2152. static int
  2153. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  2154. jack_event_t *event)
  2155. {
  2156. char status;
  2157. /* caller must hold the graph lock */
  2158. DEBUG ("delivering event (type %d)", event->type);
  2159. if (client->control->dead) {
  2160. return 0;
  2161. }
  2162. if (jack_client_is_internal (client)) {
  2163. switch (event->type) {
  2164. case PortConnected:
  2165. case PortDisconnected:
  2166. jack_client_handle_port_connection
  2167. (client->control->private_client, event);
  2168. break;
  2169. case BufferSizeChange:
  2170. jack_client_invalidate_port_buffers
  2171. (client->control->private_client);
  2172. if (client->control->bufsize) {
  2173. client->control->bufsize
  2174. (event->x.n,
  2175. client->control->bufsize_arg);
  2176. }
  2177. break;
  2178. case SampleRateChange:
  2179. if (client->control->srate) {
  2180. client->control->srate (
  2181. event->x.n,
  2182. client->control->bufsize_arg);
  2183. }
  2184. break;
  2185. case GraphReordered:
  2186. if (client->control->graph_order) {
  2187. client->control->graph_order
  2188. (client->control->graph_order_arg);
  2189. }
  2190. break;
  2191. case XRun:
  2192. if (client->control->xrun) {
  2193. client->control->xrun
  2194. (client->control->xrun_arg);
  2195. }
  2196. break;
  2197. case NewPortType:
  2198. jack_client_handle_new_port_type (
  2199. client->control->private_client,
  2200. event->x.shm_name, event->z.size,
  2201. event->y.addr);
  2202. break;
  2203. default:
  2204. /* internal clients don't need to know */
  2205. break;
  2206. }
  2207. } else {
  2208. if (client->control->active) {
  2209. /* there's a thread waiting for events, so
  2210. * it's worth telling the client */
  2211. DEBUG ("engine writing on event fd");
  2212. if (write (client->event_fd, event, sizeof (*event))
  2213. != sizeof (*event)) {
  2214. jack_error ("cannot send event to client [%s]"
  2215. " (%s)", client->control->name,
  2216. strerror (errno));
  2217. client->error++;
  2218. }
  2219. DEBUG ("engine reading from event fd");
  2220. if (!client->error &&
  2221. (read (client->event_fd, &status, sizeof (status))
  2222. != sizeof (status))) {
  2223. jack_error ("cannot read event response from "
  2224. "client [%s] (%s)",
  2225. client->control->name,
  2226. strerror (errno));
  2227. client->error++;
  2228. }
  2229. if (status != 0) {
  2230. jack_error ("bad status for client event "
  2231. "handling (type = %d)",
  2232. event->type);
  2233. client->error++;
  2234. }
  2235. }
  2236. }
  2237. DEBUG ("event delivered");
  2238. return 0;
  2239. }
  2240. int
  2241. jack_rechain_graph (jack_engine_t *engine)
  2242. {
  2243. JSList *node, *next;
  2244. unsigned long n;
  2245. int err = 0;
  2246. jack_client_internal_t *client, *subgraph_client, *next_client;
  2247. jack_event_t event;
  2248. jack_clear_fifos (engine);
  2249. subgraph_client = 0;
  2250. if (engine->verbose) {
  2251. fprintf(stderr, "++ jack_rechain_graph():\n");
  2252. }
  2253. event.type = GraphReordered;
  2254. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  2255. next = jack_slist_next (node);
  2256. if (((jack_client_internal_t *) node->data)->control->active) {
  2257. client = (jack_client_internal_t *) node->data;
  2258. /* find the next active client. its ok for
  2259. * this to be NULL */
  2260. while (next) {
  2261. if (((jack_client_internal_t *)
  2262. next->data)->control->active) {
  2263. break;
  2264. }
  2265. next = jack_slist_next (next);
  2266. };
  2267. if (next == NULL) {
  2268. next_client = NULL;
  2269. } else {
  2270. next_client = (jack_client_internal_t *)
  2271. next->data;
  2272. }
  2273. client->execution_order = n;
  2274. client->next_client = next_client;
  2275. if (jack_client_is_internal (client)) {
  2276. /* break the chain for the current
  2277. * subgraph. the server will wait for
  2278. * chain on the nth FIFO, and will
  2279. * then execute this internal
  2280. * client. */
  2281. if (subgraph_client) {
  2282. subgraph_client->subgraph_wait_fd =
  2283. jack_get_fifo_fd (engine, n);
  2284. if (engine->verbose) {
  2285. fprintf(stderr,
  2286. "client %s: wait_fd="
  2287. "%d, execution_order="
  2288. "%lu.\n",
  2289. subgraph_client->
  2290. control->name,
  2291. subgraph_client->
  2292. subgraph_wait_fd, n);
  2293. }
  2294. n++;
  2295. }
  2296. if (engine->verbose) {
  2297. fprintf(stderr, "client %s: internal "
  2298. "client, execution_order="
  2299. "%lu.\n",
  2300. client->control->name, n);
  2301. }
  2302. /* this does the right thing for
  2303. * internal clients too */
  2304. jack_deliver_event (engine, client, &event);
  2305. subgraph_client = 0;
  2306. } else {
  2307. if (subgraph_client == NULL) {
  2308. /* start a new subgraph. the
  2309. * engine will start the chain
  2310. * by writing to the nth
  2311. * FIFO. */
  2312. subgraph_client = client;
  2313. subgraph_client->subgraph_start_fd =
  2314. jack_get_fifo_fd (engine, n);
  2315. if (engine->verbose) {
  2316. fprintf(stderr, "client %s: "
  2317. "start_fd=%d, execution"
  2318. "_order=%lu.\n",
  2319. subgraph_client->
  2320. control->name,
  2321. subgraph_client->
  2322. subgraph_start_fd, n);
  2323. }
  2324. }
  2325. else {
  2326. if (engine->verbose) {
  2327. fprintf(stderr, "client %s: in"
  2328. " subgraph after %s, "
  2329. "execution_order="
  2330. "%lu.\n",
  2331. client->control->name,
  2332. subgraph_client->
  2333. control->name, n);
  2334. }
  2335. subgraph_client->subgraph_wait_fd = -1;
  2336. }
  2337. /* make sure fifo for 'n + 1' exists
  2338. * before issuing client reorder
  2339. */
  2340. (void) jack_get_fifo_fd(
  2341. engine, client->execution_order + 1);
  2342. event.x.n = client->execution_order;
  2343. jack_deliver_event (engine, client, &event);
  2344. n++;
  2345. }
  2346. }
  2347. }
  2348. if (subgraph_client) {
  2349. subgraph_client->subgraph_wait_fd =
  2350. jack_get_fifo_fd (engine, n);
  2351. if (engine->verbose) {
  2352. fprintf(stderr, "client %s: wait_fd=%d, "
  2353. "execution_order=%lu (last client).\n",
  2354. subgraph_client->control->name,
  2355. subgraph_client->subgraph_wait_fd, n);
  2356. }
  2357. }
  2358. if (engine->verbose) {
  2359. fprintf(stderr, "-- jack_rechain_graph()\n");
  2360. }
  2361. return err;
  2362. }
  2363. static void
  2364. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  2365. {
  2366. jack_client_internal_t *c2;
  2367. /* make a copy of the existing list of routes that feed
  2368. c1. this provides us with an atomic snapshot of c1's
  2369. "fed-by" state, which will be modified as we progress ...
  2370. */
  2371. JSList *existing;
  2372. JSList *node;
  2373. if (c1->fed_by == NULL) {
  2374. return;
  2375. }
  2376. existing = jack_slist_copy (c1->fed_by);
  2377. /* for each route that feeds c1, recurse, marking it as
  2378. feeding rbase as well.
  2379. */
  2380. for (node = existing; node; node = jack_slist_next (node)) {
  2381. c2 = (jack_client_internal_t *) node->data;
  2382. /* c2 is a route that feeds c1 which somehow feeds
  2383. base. mark base as being fed by c2, but don't do it
  2384. more than once.
  2385. */
  2386. if (c2 != rbase && c2 != c1) {
  2387. if (jack_slist_find (rbase->fed_by, c2) == NULL) {
  2388. rbase->fed_by =
  2389. jack_slist_prepend (rbase->fed_by, c2);
  2390. }
  2391. /* FIXME: if c2->fed_by is not up-to-date, we
  2392. may end up recursing infinitely
  2393. (kaiv)
  2394. */
  2395. if (jack_slist_find (c2->fed_by, c1) == NULL) {
  2396. /* now recurse, so that we can mark
  2397. base as being fed by all routes
  2398. that feed c2
  2399. */
  2400. jack_trace_terminal (c2, rbase);
  2401. }
  2402. }
  2403. }
  2404. jack_slist_free (existing);
  2405. }
  2406. static int
  2407. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2408. {
  2409. if (jack_slist_find (a->fed_by, b)) {
  2410. if (jack_slist_find (b->fed_by, a)) {
  2411. /* feedback loop: if `a' is the driver
  2412. client, let that execute first.
  2413. */
  2414. if (a->control->type == ClientDriver) {
  2415. /* b comes after a */
  2416. return -1;
  2417. }
  2418. }
  2419. /* a comes after b */
  2420. return 1;
  2421. } else if (jack_slist_find (b->fed_by, a)) {
  2422. if (jack_slist_find (a->fed_by, b)) {
  2423. /* feedback loop: if `b' is the driver
  2424. client, let that execute first.
  2425. */
  2426. if (b->control->type == ClientDriver) {
  2427. /* b comes before a */
  2428. return 1;
  2429. }
  2430. }
  2431. /* b comes after a */
  2432. return -1;
  2433. } else {
  2434. /* we don't care */
  2435. return 0;
  2436. }
  2437. }
  2438. static int
  2439. jack_client_feeds (jack_client_internal_t *might,
  2440. jack_client_internal_t *target)
  2441. {
  2442. JSList *pnode, *cnode;
  2443. /* Check every port of `might' for an outbound connection to
  2444. * `target' */
  2445. for (pnode = might->ports; pnode; pnode = jack_slist_next (pnode)) {
  2446. jack_port_internal_t *port;
  2447. port = (jack_port_internal_t *) pnode->data;
  2448. for (cnode = port->connections; cnode;
  2449. cnode = jack_slist_next (cnode)) {
  2450. jack_connection_internal_t *c;
  2451. c = (jack_connection_internal_t *) cnode->data;
  2452. if (c->source->shared->client_id
  2453. == might->control->id &&
  2454. c->destination->shared->client_id
  2455. == target->control->id) {
  2456. return 1;
  2457. }
  2458. }
  2459. }
  2460. return 0;
  2461. }
  2462. static jack_nframes_t
  2463. jack_get_port_total_latency (jack_engine_t *engine,
  2464. jack_port_internal_t *port, int hop_count,
  2465. int toward_port)
  2466. {
  2467. JSList *node;
  2468. jack_nframes_t latency;
  2469. jack_nframes_t max_latency = 0;
  2470. /* call tree must hold engine->client_lock. */
  2471. latency = port->shared->latency;
  2472. /* we don't prevent cyclic graphs, so we have to do something
  2473. to bottom out in the event that they are created.
  2474. */
  2475. if (hop_count > 8) {
  2476. return latency;
  2477. }
  2478. for (node = port->connections; node; node = jack_slist_next (node)) {
  2479. jack_nframes_t this_latency;
  2480. jack_connection_internal_t *connection;
  2481. connection = (jack_connection_internal_t *) node->data;
  2482. if ((toward_port &&
  2483. (connection->source->shared == port->shared)) ||
  2484. (!toward_port &&
  2485. (connection->destination->shared == port->shared))) {
  2486. continue;
  2487. }
  2488. /* if we're a destination in the connection, recurse
  2489. on the source to get its total latency
  2490. */
  2491. if (connection->destination == port) {
  2492. if (connection->source->shared->flags
  2493. & JackPortIsTerminal) {
  2494. this_latency = connection->source->
  2495. shared->latency;
  2496. } else {
  2497. this_latency =
  2498. jack_get_port_total_latency (
  2499. engine, connection->source,
  2500. hop_count + 1,
  2501. toward_port);
  2502. }
  2503. } else {
  2504. /* "port" is the source, so get the latency of
  2505. * the destination */
  2506. if (connection->destination->shared->flags
  2507. & JackPortIsTerminal) {
  2508. this_latency = connection->destination->
  2509. shared->latency;
  2510. } else {
  2511. this_latency =
  2512. jack_get_port_total_latency (
  2513. engine,
  2514. connection->destination,
  2515. hop_count + 1,
  2516. toward_port);
  2517. }
  2518. }
  2519. if (this_latency > max_latency) {
  2520. max_latency = this_latency;
  2521. }
  2522. }
  2523. return latency + max_latency;
  2524. }
  2525. static void
  2526. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2527. {
  2528. jack_port_shared_t *shared = engine->control->ports;
  2529. unsigned int i;
  2530. int toward_port;
  2531. for (i = 0; i < engine->control->port_max; i++) {
  2532. if (shared[i].in_use) {
  2533. if (shared[i].flags & JackPortIsOutput) {
  2534. toward_port = FALSE;
  2535. } else {
  2536. toward_port = TRUE;
  2537. }
  2538. shared[i].total_latency =
  2539. jack_get_port_total_latency (
  2540. engine, &engine->internal_ports[i],
  2541. 0, toward_port);
  2542. }
  2543. }
  2544. }
  2545. /**
  2546. * Sorts the network of clients using the following
  2547. * algorithm:
  2548. *
  2549. * 1) figure out who is connected to whom:
  2550. *
  2551. * foreach client1
  2552. * foreach input port
  2553. * foreach client2
  2554. * foreach output port
  2555. * if client1->input port connected to client2->output port
  2556. * mark client1 fed by client 2
  2557. *
  2558. * 2) trace the connections as terminal arcs in the graph so that
  2559. * if client A feeds client B who feeds client C, mark client C
  2560. * as fed by client A as well as client B, and so forth.
  2561. *
  2562. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  2563. * if the condition is true, client2 must execute before client1
  2564. *
  2565. */
  2566. static void
  2567. jack_sort_graph (jack_engine_t *engine)
  2568. {
  2569. JSList *node, *onode;
  2570. jack_client_internal_t *client;
  2571. jack_client_internal_t *oclient;
  2572. /* called, obviously, must hold engine->client_lock */
  2573. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2574. client = (jack_client_internal_t *) node->data;
  2575. jack_slist_free (client->fed_by);
  2576. client->fed_by = 0;
  2577. for (onode = engine->clients; onode;
  2578. onode = jack_slist_next (onode)) {
  2579. oclient = (jack_client_internal_t *) onode->data;
  2580. if (jack_client_feeds (oclient, client)) {
  2581. client->fed_by =
  2582. jack_slist_prepend (client->fed_by,
  2583. oclient);
  2584. }
  2585. }
  2586. }
  2587. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2588. jack_trace_terminal ((jack_client_internal_t *) node->data,
  2589. (jack_client_internal_t *) node->data);
  2590. }
  2591. engine->clients = jack_slist_sort (engine->clients,
  2592. (JCompareFunc) jack_client_sort);
  2593. jack_compute_all_port_total_latencies (engine);
  2594. jack_rechain_graph (engine);
  2595. }
  2596. /**
  2597. * Dumps current engine configuration to stderr.
  2598. */
  2599. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2600. {
  2601. JSList *clientnode, *portnode, *connectionnode;
  2602. jack_client_internal_t *client;
  2603. jack_client_control_t *ctl;
  2604. jack_port_internal_t *port;
  2605. jack_connection_internal_t* connection;
  2606. int n, m, o;
  2607. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2608. if (take_lock) {
  2609. jack_lock_graph (engine);
  2610. }
  2611. for (n = 0, clientnode = engine->clients; clientnode;
  2612. clientnode = jack_slist_next (clientnode)) {
  2613. client = (jack_client_internal_t *) clientnode->data;
  2614. ctl = client->control;
  2615. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed"
  2616. " by %d clients) start=%d wait=%d\n",
  2617. ++n,
  2618. ctl->name,
  2619. ctl->type,
  2620. ctl->process ? "yes" : "no",
  2621. jack_slist_length(client->fed_by),
  2622. client->subgraph_start_fd,
  2623. client->subgraph_wait_fd);
  2624. for(m = 0, portnode = client->ports; portnode;
  2625. portnode = jack_slist_next (portnode)) {
  2626. port = (jack_port_internal_t *) portnode->data;
  2627. fprintf(stderr, "\t port #%d: %s\n", ++m,
  2628. port->shared->name);
  2629. for(o = 0, connectionnode = port->connections;
  2630. connectionnode;
  2631. connectionnode =
  2632. jack_slist_next (connectionnode)) {
  2633. connection = (jack_connection_internal_t *)
  2634. connectionnode->data;
  2635. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2636. ++o,
  2637. (port->shared->flags
  2638. & JackPortIsInput)? "<-": "->",
  2639. (port->shared->flags & JackPortIsInput)?
  2640. connection->source->shared->name:
  2641. connection->destination->shared->name);
  2642. }
  2643. }
  2644. }
  2645. if (take_lock) {
  2646. jack_unlock_graph (engine);
  2647. }
  2648. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2649. }
  2650. static int
  2651. jack_port_do_connect (jack_engine_t *engine,
  2652. const char *source_port,
  2653. const char *destination_port)
  2654. {
  2655. jack_connection_internal_t *connection;
  2656. jack_port_internal_t *srcport, *dstport;
  2657. jack_port_id_t src_id, dst_id;
  2658. jack_client_internal_t *client;
  2659. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2660. jack_error ("unknown source port in attempted connection [%s]",
  2661. source_port);
  2662. return -1;
  2663. }
  2664. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2665. == NULL) {
  2666. jack_error ("unknown destination port in attempted connection"
  2667. " [%s]", destination_port);
  2668. return -1;
  2669. }
  2670. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2671. jack_error ("destination port in attempted connection of"
  2672. " %s and %s is not an input port",
  2673. source_port, destination_port);
  2674. return -1;
  2675. }
  2676. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2677. jack_error ("source port in attempted connection of %s and"
  2678. " %s is not an output port",
  2679. source_port, destination_port);
  2680. return -1;
  2681. }
  2682. if (srcport->shared->locked) {
  2683. jack_error ("source port %s is locked against connection"
  2684. " changes", source_port);
  2685. return -1;
  2686. }
  2687. if (dstport->shared->locked) {
  2688. jack_error ("destination port %s is locked against connection"
  2689. " changes", destination_port);
  2690. return -1;
  2691. }
  2692. if (srcport->shared->type_info.type_id
  2693. != dstport->shared->type_info.type_id) {
  2694. jack_error ("ports used in attemped connection are not of "
  2695. "the same data type");
  2696. return -1;
  2697. }
  2698. if ((client = jack_client_internal_by_id (engine,
  2699. srcport->shared->client_id))
  2700. == 0) {
  2701. jack_error ("unknown client set as owner of port - "
  2702. "cannot connect");
  2703. return -1;
  2704. }
  2705. if (!client->control->active) {
  2706. jack_error ("cannot connect ports owned by inactive clients;"
  2707. " \"%s\" is not active", client->control->name);
  2708. return -1;
  2709. }
  2710. if ((client = jack_client_internal_by_id (engine,
  2711. dstport->shared->client_id))
  2712. == 0) {
  2713. jack_error ("unknown client set as owner of port - cannot "
  2714. "connect");
  2715. return -1;
  2716. }
  2717. if (!client->control->active) {
  2718. jack_error ("cannot connect ports owned by inactive clients;"
  2719. " \"%s\" is not active", client->control->name);
  2720. return -1;
  2721. }
  2722. connection = (jack_connection_internal_t *)
  2723. malloc (sizeof (jack_connection_internal_t));
  2724. connection->source = srcport;
  2725. connection->destination = dstport;
  2726. src_id = srcport->shared->id;
  2727. dst_id = dstport->shared->id;
  2728. jack_lock_graph (engine);
  2729. if (dstport->connections &&
  2730. dstport->shared->type_info.mixdown == NULL) {
  2731. jack_error ("cannot make multiple connections to a port of"
  2732. " type [%s]",
  2733. dstport->shared->type_info.type_name);
  2734. free (connection);
  2735. jack_unlock_graph (engine);
  2736. return -1;
  2737. } else {
  2738. if (engine->verbose) {
  2739. fprintf (stderr, "connect %s and %s\n",
  2740. srcport->shared->name,
  2741. dstport->shared->name);
  2742. }
  2743. dstport->connections =
  2744. jack_slist_prepend (dstport->connections, connection);
  2745. srcport->connections =
  2746. jack_slist_prepend (srcport->connections, connection);
  2747. jack_sort_graph (engine);
  2748. DEBUG ("actually sorted the graph...");
  2749. jack_send_connection_notification (engine,
  2750. srcport->shared->client_id,
  2751. src_id, dst_id, TRUE);
  2752. jack_send_connection_notification (engine,
  2753. dstport->shared->client_id,
  2754. dst_id, src_id, TRUE);
  2755. }
  2756. jack_unlock_graph (engine);
  2757. return 0;
  2758. }
  2759. int
  2760. jack_port_disconnect_internal (jack_engine_t *engine,
  2761. jack_port_internal_t *srcport,
  2762. jack_port_internal_t *dstport,
  2763. int sort_graph)
  2764. {
  2765. JSList *node;
  2766. jack_connection_internal_t *connect;
  2767. int ret = -1;
  2768. jack_port_id_t src_id, dst_id;
  2769. /* call tree **** MUST HOLD **** engine->client_lock. */
  2770. for (node = srcport->connections; node;
  2771. node = jack_slist_next (node)) {
  2772. connect = (jack_connection_internal_t *) node->data;
  2773. if (connect->source == srcport &&
  2774. connect->destination == dstport) {
  2775. if (engine->verbose) {
  2776. fprintf (stderr, "DIS-connect %s and %s\n",
  2777. srcport->shared->name,
  2778. dstport->shared->name);
  2779. }
  2780. srcport->connections =
  2781. jack_slist_remove (srcport->connections,
  2782. connect);
  2783. dstport->connections =
  2784. jack_slist_remove (dstport->connections,
  2785. connect);
  2786. src_id = srcport->shared->id;
  2787. dst_id = dstport->shared->id;
  2788. /* this is a bit harsh, but it basically says
  2789. that if we actually do a disconnect, and
  2790. its the last one, then make sure that any
  2791. input monitoring is turned off on the
  2792. srcport. this isn't ideal for all
  2793. situations, but it works better for most of
  2794. them.
  2795. */
  2796. if (srcport->connections == NULL) {
  2797. srcport->shared->monitor_requests = 0;
  2798. }
  2799. jack_send_connection_notification (
  2800. engine, srcport->shared->client_id, src_id,
  2801. dst_id, FALSE);
  2802. jack_send_connection_notification (
  2803. engine, dstport->shared->client_id, dst_id,
  2804. src_id, FALSE);
  2805. free (connect);
  2806. ret = 0;
  2807. break;
  2808. }
  2809. }
  2810. if (sort_graph) {
  2811. jack_sort_graph (engine);
  2812. }
  2813. return ret;
  2814. }
  2815. static int
  2816. jack_port_do_disconnect_all (jack_engine_t *engine,
  2817. jack_port_id_t port_id)
  2818. {
  2819. if (port_id >= engine->control->port_max) {
  2820. jack_error ("illegal port ID in attempted disconnection [%"
  2821. PRIu32 "]", port_id);
  2822. return -1;
  2823. }
  2824. if (engine->verbose) {
  2825. fprintf (stderr, "clear connections for %s\n",
  2826. engine->internal_ports[port_id].shared->name);
  2827. }
  2828. jack_lock_graph (engine);
  2829. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2830. jack_sort_graph (engine);
  2831. jack_unlock_graph (engine);
  2832. return 0;
  2833. }
  2834. static int
  2835. jack_port_do_disconnect (jack_engine_t *engine,
  2836. const char *source_port,
  2837. const char *destination_port)
  2838. {
  2839. jack_port_internal_t *srcport, *dstport;
  2840. int ret = -1;
  2841. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2842. jack_error ("unknown source port in attempted disconnection"
  2843. " [%s]", source_port);
  2844. return -1;
  2845. }
  2846. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2847. == NULL) {
  2848. jack_error ("unknown destination port in attempted connection"
  2849. " [%s]", destination_port);
  2850. return -1;
  2851. }
  2852. jack_lock_graph (engine);
  2853. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  2854. jack_unlock_graph (engine);
  2855. return ret;
  2856. }
  2857. static int
  2858. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2859. {
  2860. /* caller must hold client_lock */
  2861. char path[PATH_MAX+1];
  2862. struct stat statbuf;
  2863. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2864. which_fifo);
  2865. DEBUG ("%s", path);
  2866. if (stat (path, &statbuf)) {
  2867. if (errno == ENOENT) {
  2868. #if defined(__APPLE__) && defined(__POWERPC__)
  2869. if (mkfifo(path,0666) < 0){
  2870. #else
  2871. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  2872. #endif
  2873. jack_error ("cannot create inter-client FIFO"
  2874. " [%s] (%s)\n", path,
  2875. strerror (errno));
  2876. return -1;
  2877. }
  2878. } else {
  2879. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2880. return -1;
  2881. }
  2882. } else {
  2883. if (!S_ISFIFO(statbuf.st_mode)) {
  2884. jack_error ("FIFO %d (%s) already exists, but is not"
  2885. " a FIFO!\n", which_fifo, path);
  2886. return -1;
  2887. }
  2888. }
  2889. if (which_fifo >= engine->fifo_size) {
  2890. unsigned int i;
  2891. engine->fifo = (int *)
  2892. realloc (engine->fifo,
  2893. sizeof (int) * engine->fifo_size + 16);
  2894. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2895. engine->fifo[i] = -1;
  2896. }
  2897. engine->fifo_size += 16;
  2898. }
  2899. if (engine->fifo[which_fifo] < 0) {
  2900. if ((engine->fifo[which_fifo] =
  2901. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2902. jack_error ("cannot open fifo [%s] (%s)", path,
  2903. strerror (errno));
  2904. return -1;
  2905. }
  2906. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  2907. which_fifo, engine->fifo[which_fifo], path);
  2908. }
  2909. return engine->fifo[which_fifo];
  2910. }
  2911. static void
  2912. jack_clear_fifos (jack_engine_t *engine)
  2913. {
  2914. /* caller must hold client_lock */
  2915. unsigned int i;
  2916. char buf[16];
  2917. /* this just drains the existing FIFO's of any data left in
  2918. them by aborted clients, etc. there is only ever going to
  2919. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2920. */
  2921. for (i = 0; i < engine->fifo_size; i++) {
  2922. if (engine->fifo[i] >= 0) {
  2923. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2924. if (nread < 0 && errno != EAGAIN) {
  2925. jack_error ("clear fifo[%d] error: %s",
  2926. i, strerror (errno));
  2927. }
  2928. }
  2929. }
  2930. }
  2931. static int
  2932. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2933. {
  2934. if (engine->driver) {
  2935. engine->driver->detach (engine->driver, engine);
  2936. engine->driver = 0;
  2937. }
  2938. if (driver) {
  2939. if (driver->attach (driver, engine))
  2940. return -1;
  2941. engine->rolling_interval = (int)
  2942. floor ((JACK_ENGINE_ROLLING_INTERVAL * 1000.0f)
  2943. / driver->period_usecs);
  2944. }
  2945. engine->driver = driver;
  2946. return 0;
  2947. }
  2948. /* PORT RELATED FUNCTIONS */
  2949. static jack_port_id_t
  2950. jack_get_free_port (jack_engine_t *engine)
  2951. {
  2952. jack_port_id_t i;
  2953. pthread_mutex_lock (&engine->port_lock);
  2954. for (i = 0; i < engine->port_max; i++) {
  2955. if (engine->control->ports[i].in_use == 0) {
  2956. engine->control->ports[i].in_use = 1;
  2957. break;
  2958. }
  2959. }
  2960. pthread_mutex_unlock (&engine->port_lock);
  2961. if (i == engine->port_max) {
  2962. return (jack_port_id_t) -1;
  2963. }
  2964. return i;
  2965. }
  2966. static void
  2967. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2968. {
  2969. pthread_mutex_lock (&engine->port_lock);
  2970. port->shared->in_use = 0;
  2971. if (port->buffer_info) {
  2972. jack_port_type_info_t *info =
  2973. jack_global_port_type_info (engine, port);
  2974. pthread_mutex_lock (&info->buffer_lock);
  2975. info->buffer_freelist =
  2976. jack_slist_prepend (info->buffer_freelist,
  2977. port->buffer_info);
  2978. port->buffer_info = NULL;
  2979. pthread_mutex_unlock (&info->buffer_lock);
  2980. }
  2981. pthread_mutex_unlock (&engine->port_lock);
  2982. }
  2983. jack_port_internal_t *
  2984. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2985. {
  2986. jack_port_id_t id;
  2987. pthread_mutex_lock (&engine->port_lock);
  2988. for (id = 0; id < engine->port_max; id++) {
  2989. if (strcmp (engine->control->ports[id].name, name) == 0) {
  2990. break;
  2991. }
  2992. }
  2993. pthread_mutex_unlock (&engine->port_lock);
  2994. if (id != engine->port_max) {
  2995. return &engine->internal_ports[id];
  2996. } else {
  2997. return NULL;
  2998. }
  2999. }
  3000. int
  3001. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  3002. {
  3003. jack_port_id_t port_id;
  3004. jack_port_shared_t *shared;
  3005. jack_port_internal_t *port;
  3006. jack_client_internal_t *client;
  3007. unsigned long i;
  3008. for (i = 0; i < engine->control->n_port_types; ++i) {
  3009. if (strcmp (req->x.port_info.type,
  3010. engine->control->port_types[i].type_name) == 0) {
  3011. break;
  3012. }
  3013. }
  3014. if (i == engine->control->n_port_types) {
  3015. jack_error ("cannot register a port of type \"%s\"",
  3016. req->x.port_info.type);
  3017. return -1;
  3018. }
  3019. jack_lock_graph (engine);
  3020. if ((client = jack_client_internal_by_id (engine,
  3021. req->x.port_info.client_id))
  3022. == NULL) {
  3023. jack_error ("unknown client id in port registration request");
  3024. return -1;
  3025. }
  3026. jack_unlock_graph (engine);
  3027. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  3028. jack_error ("no ports available!");
  3029. return -1;
  3030. }
  3031. shared = &engine->control->ports[port_id];
  3032. strcpy (shared->name, req->x.port_info.name);
  3033. memcpy (&shared->type_info, &engine->control->port_types[i],
  3034. sizeof (jack_port_type_info_t));
  3035. shared->client_id = req->x.port_info.client_id;
  3036. shared->flags = req->x.port_info.flags;
  3037. shared->latency = 0;
  3038. shared->monitor_requests = 0;
  3039. shared->locked = 0;
  3040. port = &engine->internal_ports[port_id];
  3041. port->shared = shared;
  3042. port->connections = 0;
  3043. if (jack_port_assign_buffer (engine, port)) {
  3044. jack_error ("cannot assign buffer for port");
  3045. return -1;
  3046. }
  3047. jack_lock_graph (engine);
  3048. client->ports = jack_slist_prepend (client->ports, port);
  3049. jack_port_registration_notify (engine, port_id, TRUE);
  3050. jack_unlock_graph (engine);
  3051. if (engine->verbose) {
  3052. fprintf (stderr, "registered port %s, offset = %u\n",
  3053. shared->name, (unsigned int)shared->offset);
  3054. }
  3055. req->x.port_info.port_id = port_id;
  3056. return 0;
  3057. }
  3058. int
  3059. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  3060. {
  3061. jack_client_internal_t *client;
  3062. jack_port_shared_t *shared;
  3063. jack_port_internal_t *port;
  3064. if (req->x.port_info.port_id < 0 ||
  3065. req->x.port_info.port_id > engine->port_max) {
  3066. jack_error ("invalid port ID %" PRIu32
  3067. " in unregister request",
  3068. req->x.port_info.port_id);
  3069. return -1;
  3070. }
  3071. shared = &engine->control->ports[req->x.port_info.port_id];
  3072. if (shared->client_id != req->x.port_info.client_id) {
  3073. jack_error ("Client %" PRIu32
  3074. " is not allowed to remove port %s",
  3075. req->x.port_info.client_id, shared->name);
  3076. return -1;
  3077. }
  3078. jack_lock_graph (engine);
  3079. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  3080. == NULL) {
  3081. jack_error ("unknown client id in port registration request");
  3082. jack_unlock_graph (engine);
  3083. return -1;
  3084. }
  3085. port = &engine->internal_ports[req->x.port_info.port_id];
  3086. jack_port_clear_connections (engine, port);
  3087. jack_port_release (engine,
  3088. &engine->internal_ports[req->x.port_info.port_id]);
  3089. client->ports = jack_slist_remove (client->ports, port);
  3090. jack_port_registration_notify (engine, req->x.port_info.port_id,
  3091. FALSE);
  3092. jack_unlock_graph (engine);
  3093. return 0;
  3094. }
  3095. int
  3096. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  3097. int reply_fd)
  3098. {
  3099. jack_port_internal_t *port;
  3100. JSList *node;
  3101. unsigned int i;
  3102. int ret = -1;
  3103. int internal = FALSE;
  3104. jack_lock_graph (engine);
  3105. port = &engine->internal_ports[req->x.port_info.port_id];
  3106. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  3107. req->x.port_connections.nports = jack_slist_length (port->connections);
  3108. req->status = 0;
  3109. /* figure out if this is an internal or external client */
  3110. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3111. if (((jack_client_internal_t *) node->data)->request_fd
  3112. == reply_fd) {
  3113. internal = jack_client_is_internal(
  3114. (jack_client_internal_t *) node->data);
  3115. break;
  3116. }
  3117. }
  3118. if (!internal) {
  3119. if (write (reply_fd, req, sizeof (*req))
  3120. < (ssize_t) sizeof (req)) {
  3121. jack_error ("cannot write GetPortConnections result "
  3122. "to client via fd = %d (%s)",
  3123. reply_fd, strerror (errno));
  3124. goto out;
  3125. }
  3126. } else {
  3127. req->x.port_connections.ports = (const char **)
  3128. malloc (sizeof (char *)
  3129. * req->x.port_connections.nports);
  3130. }
  3131. if (req->type == GetPortConnections) {
  3132. for (i = 0, node = port->connections; node;
  3133. node = jack_slist_next (node), ++i) {
  3134. jack_port_id_t port_id;
  3135. if (((jack_connection_internal_t *) node->data)->source
  3136. == port) {
  3137. port_id = ((jack_connection_internal_t *)
  3138. node->data)->destination->shared->id;
  3139. } else {
  3140. port_id = ((jack_connection_internal_t *)
  3141. node->data)->source->shared->id;
  3142. }
  3143. if (internal) {
  3144. /* internal client asking for
  3145. * names. store in malloc'ed space,
  3146. * client frees
  3147. */
  3148. req->x.port_connections.ports[i] =
  3149. engine->control->ports[port_id].name;
  3150. } else {
  3151. /* external client asking for
  3152. * names. we write the port id's to
  3153. * the reply fd.
  3154. */
  3155. if (write (reply_fd, &port_id,
  3156. sizeof (port_id))
  3157. < (ssize_t) sizeof (port_id)) {
  3158. jack_error ("cannot write port id "
  3159. "to client");
  3160. goto out;
  3161. }
  3162. }
  3163. }
  3164. }
  3165. ret = 0;
  3166. out:
  3167. req->status = ret;
  3168. jack_unlock_graph (engine);
  3169. return ret;
  3170. }
  3171. void
  3172. jack_port_registration_notify (jack_engine_t *engine,
  3173. jack_port_id_t port_id, int yn)
  3174. {
  3175. jack_event_t event;
  3176. jack_client_internal_t *client;
  3177. JSList *node;
  3178. event.type = (yn ? PortRegistered : PortUnregistered);
  3179. event.x.port_id = port_id;
  3180. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3181. client = (jack_client_internal_t *) node->data;
  3182. if (!client->control->active) {
  3183. continue;
  3184. }
  3185. if (client->control->port_register) {
  3186. if (jack_deliver_event (engine, client, &event)) {
  3187. jack_error ("cannot send port registration"
  3188. " notification to %s (%s)",
  3189. client->control->name,
  3190. strerror (errno));
  3191. }
  3192. }
  3193. }
  3194. }
  3195. int
  3196. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  3197. {
  3198. jack_port_type_info_t *port_type =
  3199. jack_global_port_type_info (engine, port);
  3200. jack_port_buffer_info_t *bi;
  3201. if (port->shared->flags & JackPortIsInput) {
  3202. port->shared->offset = 0;
  3203. return 0;
  3204. }
  3205. pthread_mutex_lock (&port_type->buffer_lock);
  3206. if (port_type->buffer_freelist == NULL) {
  3207. jack_error ("all %s port buffers in use!",
  3208. port_type->type_name);
  3209. pthread_mutex_unlock (&port_type->buffer_lock);
  3210. return -1;
  3211. }
  3212. bi = (jack_port_buffer_info_t *) port_type->buffer_freelist->data;
  3213. port_type->buffer_freelist =
  3214. jack_slist_remove (port_type->buffer_freelist, bi);
  3215. port->shared->offset = bi->offset;
  3216. port->buffer_info = bi;
  3217. pthread_mutex_unlock (&port_type->buffer_lock);
  3218. return 0;
  3219. }
  3220. static jack_port_internal_t *
  3221. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  3222. {
  3223. jack_port_id_t id;
  3224. /* Note the potential race on "in_use". Other design
  3225. elements prevent this from being a problem.
  3226. */
  3227. for (id = 0; id < engine->port_max; id++) {
  3228. if (engine->control->ports[id].in_use &&
  3229. strcmp (engine->control->ports[id].name, name) == 0) {
  3230. return &engine->internal_ports[id];
  3231. }
  3232. }
  3233. return NULL;
  3234. }
  3235. static int
  3236. jack_send_connection_notification (jack_engine_t *engine,
  3237. jack_client_id_t client_id,
  3238. jack_port_id_t self_id,
  3239. jack_port_id_t other_id, int connected)
  3240. {
  3241. jack_client_internal_t *client;
  3242. jack_event_t event;
  3243. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  3244. jack_error ("no such client %" PRIu32
  3245. " during connection notification", client_id);
  3246. return -1;
  3247. }
  3248. if (client->control->active) {
  3249. event.type = (connected ? PortConnected : PortDisconnected);
  3250. event.x.self_id = self_id;
  3251. event.y.other_id = other_id;
  3252. if (jack_deliver_event (engine, client, &event)) {
  3253. jack_error ("cannot send port connection notification"
  3254. " to client %s (%s)",
  3255. client->control->name, strerror (errno));
  3256. return -1;
  3257. }
  3258. }
  3259. return 0;
  3260. }
  3261. void
  3262. jack_set_asio_mode (jack_engine_t *engine, int yn)
  3263. {
  3264. engine->asio_mode = yn;
  3265. }