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.

2474 lines
60KB

  1. /*
  2. Copyright (C) 2001 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 <unistd.h>
  17. #include <sys/socket.h>
  18. #include <sys/poll.h>
  19. #include <sys/un.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <sys/shm.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <dirent.h>
  27. #include <sys/ipc.h>
  28. #include <signal.h>
  29. #include <sys/types.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. #include <sys/mman.h>
  33. #include <asm/msr.h>
  34. #include <jack/internal.h>
  35. #include <jack/engine.h>
  36. #include <jack/driver.h>
  37. #define MAX_SHM_ID 256 /* likely use is more like 16 */
  38. typedef struct {
  39. jack_port_internal_t *source;
  40. jack_port_internal_t *destination;
  41. } jack_connection_internal_t;
  42. typedef struct _jack_client_internal {
  43. jack_client_control_t *control;
  44. int request_fd;
  45. int event_fd;
  46. int subgraph_start_fd;
  47. int subgraph_wait_fd;
  48. GSList *ports; /* protected by engine->graph_lock */
  49. GSList *fed_by; /* protected by engine->graph_lock */
  50. int shm_id;
  51. int shm_key;
  52. unsigned long rank;
  53. struct _jack_client_internal *next_client; /* not a linked list! */
  54. dlhandle handle;
  55. } jack_client_internal_t;
  56. static int jack_port_assign_buffer (jack_engine_t *, jack_port_internal_t *);
  57. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *, const char *name);
  58. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  59. static void jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client);
  60. static jack_client_internal_t *jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *);
  61. static jack_client_internal_t *jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  62. static void jack_sort_graph (jack_engine_t *engine, int take_lock);
  63. static int jack_rechain_graph (jack_engine_t *engine, int take_lock);
  64. static int jack_get_fifo_fd (jack_engine_t *engine, int which_fifo);
  65. static int jack_port_do_connect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  66. static int jack_port_do_disconnect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  67. static int jack_port_do_disconnect_all (jack_engine_t *engine, jack_port_id_t);
  68. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  69. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  70. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  71. static void jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port);
  72. static int jack_port_disconnect_internal (jack_engine_t *engine, jack_port_internal_t *src,
  73. jack_port_internal_t *dst, int sort_graph);
  74. static void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  75. static int jack_send_connection_notification (jack_engine_t *, jack_client_id_t, jack_port_id_t, jack_port_id_t, int);
  76. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *, jack_event_t *);
  77. static int jack_get_total_latency (jack_engine_t *engine, const char *portname, nframes_t *latency);
  78. static int jack_engine_process_lock (jack_engine_t *);
  79. static int jack_engine_process_unlock (jack_engine_t *);
  80. static int jack_engine_post_process (jack_engine_t *);
  81. static int *jack_shm_registry;
  82. static int jack_shm_id_cnt;
  83. static inline int
  84. jack_client_is_inprocess (jack_client_internal_t *client)
  85. {
  86. return (client->control->type == ClientDynamic) || (client->control->type == ClientDriver);
  87. }
  88. static int
  89. make_sockets (int fd[2])
  90. {
  91. struct sockaddr_un addr;
  92. int i;
  93. /* First, the master server socket */
  94. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  95. jack_error ("cannot create server socket (%s)", strerror (errno));
  96. return -1;
  97. }
  98. addr.sun_family = AF_UNIX;
  99. for (i = 0; i < 999; i++) {
  100. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, i);
  101. if (access (addr.sun_path, F_OK) != 0) {
  102. break;
  103. }
  104. }
  105. if (i == 999) {
  106. jack_error ("all possible server socket names in use!!!");
  107. close (fd[0]);
  108. return -1;
  109. }
  110. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  111. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  112. close (fd[0]);
  113. return -1;
  114. }
  115. if (listen (fd[0], 1) < 0) {
  116. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  117. close (fd[0]);
  118. return -1;
  119. }
  120. /* Now the client/server event ack server socket */
  121. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  122. jack_error ("cannot create event ACK socket (%s)", strerror (errno));
  123. close (fd[0]);
  124. return -1;
  125. }
  126. addr.sun_family = AF_UNIX;
  127. for (i = 0; i < 999; i++) {
  128. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_%d", jack_temp_dir, i);
  129. if (access (addr.sun_path, F_OK) != 0) {
  130. break;
  131. }
  132. }
  133. if (i == 999) {
  134. jack_error ("all possible server ACK socket names in use!!!");
  135. close (fd[0]);
  136. close (fd[1]);
  137. return -1;
  138. }
  139. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  140. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  141. close (fd[0]);
  142. close (fd[1]);
  143. return -1;
  144. }
  145. if (listen (fd[1], 1) < 0) {
  146. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  147. close (fd[0]);
  148. close (fd[1]);
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static void
  154. jack_cleanup_clients (jack_engine_t *engine)
  155. {
  156. jack_client_control_t *ctl;
  157. jack_client_internal_t *client;
  158. GSList *node, *tmp;
  159. pthread_mutex_lock (&engine->graph_lock);
  160. for (node = engine->clients; node; ) {
  161. tmp = g_slist_next (node);
  162. client = (jack_client_internal_t *) node->data;
  163. ctl = client->control;
  164. if (engine->verbose) {
  165. fprintf (stderr, "client %s state = %d\n", ctl->name, ctl->state);
  166. }
  167. if (ctl->state > NotTriggered && ctl->state != Finished) {
  168. if (engine->verbose) {
  169. fprintf (stderr, "removing failed client %s\n", ctl->name);
  170. }
  171. jack_remove_client (engine, (jack_client_internal_t *) node->data);
  172. }
  173. node = tmp;
  174. }
  175. pthread_mutex_unlock (&engine->graph_lock);
  176. }
  177. static int
  178. jack_initialize_shm ()
  179. {
  180. int shmid_id;
  181. void *addr;
  182. if (jack_shm_registry != NULL) {
  183. return 0;
  184. }
  185. /* grab a chunk of memory to store shm ids in. this is
  186. to allow our parent to clean up all such ids when
  187. if we exit. otherwise, they can get lost in crash
  188. or debugger driven exits.
  189. */
  190. if ((shmid_id = shmget (random(), sizeof(int) * MAX_SHM_ID, IPC_CREAT|0600)) < 0) {
  191. jack_error ("cannot create engine shm ID registry (%s)", strerror (errno));
  192. return -1;
  193. }
  194. if ((addr = shmat (shmid_id, 0, 0)) == (void *) -1) {
  195. jack_error ("cannot attach shm ID registry (%s)", strerror (errno));
  196. shmctl (shmid_id, IPC_RMID, 0);
  197. return -1;
  198. }
  199. if (shmctl (shmid_id, IPC_RMID, NULL)) {
  200. jack_error ("cannot mark shm ID registry as destroyed (%s)", strerror (errno));
  201. return -1;
  202. }
  203. jack_shm_registry = (int *) addr;
  204. jack_shm_id_cnt = 0;
  205. return 0;
  206. }
  207. static void
  208. jack_register_shm (int shmid)
  209. {
  210. if (jack_shm_id_cnt < MAX_SHM_ID) {
  211. jack_shm_registry[jack_shm_id_cnt++] = shmid;
  212. }
  213. }
  214. void
  215. jack_cleanup_shm ()
  216. {
  217. int i;
  218. for (i = 0; i < jack_shm_id_cnt; i++) {
  219. shmctl (jack_shm_registry[i], IPC_RMID, NULL);
  220. }
  221. }
  222. void
  223. jack_cleanup_files ()
  224. {
  225. DIR *dir;
  226. struct dirent *dirent;
  227. /* its important that we remove all files that jackd creates
  228. because otherwise subsequent attempts to start jackd will
  229. believe that an instance is already running.
  230. */
  231. if ((dir = opendir (jack_temp_dir)) == NULL) {
  232. fprintf (stderr, "jack(%d): cannot open jack FIFO directory (%s)\n", getpid(), strerror (errno));
  233. return;
  234. }
  235. while ((dirent = readdir (dir)) != NULL) {
  236. if (strncmp (dirent->d_name, "jack-", 5) == 0 || strncmp (dirent->d_name, "jack_", 5) == 0) {
  237. char fullpath[PATH_MAX+1];
  238. sprintf (fullpath, "%s/%s", jack_temp_dir, dirent->d_name);
  239. unlink (fullpath);
  240. }
  241. }
  242. closedir (dir);
  243. }
  244. static int
  245. jack_add_port_segment (jack_engine_t *engine, unsigned long nports)
  246. {
  247. jack_port_segment_info_t *si;
  248. key_t key;
  249. int id;
  250. char *addr;
  251. int offset;
  252. size_t size;
  253. size_t step;
  254. key = random();
  255. size = nports * sizeof (sample_t) * engine->control->buffer_size;
  256. if ((id = shmget (key, size, IPC_CREAT|0666)) < 0) {
  257. jack_error ("cannot create new port segment of %d bytes, key = 0x%x (%s)", size, key, strerror (errno));
  258. return -1;
  259. }
  260. jack_register_shm (id);
  261. if ((addr = shmat (id, 0, 0)) == (char *) -1) {
  262. jack_error ("cannot attach new port segment (%s)", strerror (errno));
  263. shmctl (id, IPC_RMID, 0);
  264. return -1;
  265. }
  266. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  267. si->shm_key = key;
  268. si->address = addr;
  269. engine->port_segments = g_slist_prepend (engine->port_segments, si);
  270. engine->port_segment_key = key; /* XXX fix me */
  271. engine->port_segment_address = addr; /* XXX fix me */
  272. pthread_mutex_lock (&engine->buffer_lock);
  273. offset = 0;
  274. step = engine->control->buffer_size * sizeof (sample_t);
  275. while (offset < size) {
  276. jack_port_buffer_info_t *bi;
  277. bi = (jack_port_buffer_info_t *) malloc (sizeof (jack_port_buffer_info_t));
  278. bi->shm_key = key;
  279. bi->offset = offset;
  280. /* we append because we want the list to be in memory-address order */
  281. engine->port_buffer_freelist = g_slist_append (engine->port_buffer_freelist, bi);
  282. offset += step;
  283. }
  284. /* convert the first chunk of the segment into a zero-filled area */
  285. if (engine->silent_buffer == 0) {
  286. engine->silent_buffer = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  287. engine->port_buffer_freelist = g_slist_remove_link (engine->port_buffer_freelist, engine->port_buffer_freelist);
  288. memset (engine->port_segment_address + engine->silent_buffer->offset, 0,
  289. sizeof (sample_t) * engine->control->buffer_size);
  290. }
  291. pthread_mutex_unlock (&engine->buffer_lock);
  292. /* XXX notify all clients of new segment */
  293. return 0;
  294. }
  295. static int
  296. jack_set_buffer_size (jack_engine_t *engine, nframes_t nframes)
  297. {
  298. /* XXX this is not really right, since it only works for
  299. audio ports. it also doesn't resize the zero filled
  300. area.
  301. */
  302. engine->control->buffer_size = nframes;
  303. jack_add_port_segment (engine, engine->control->port_max);
  304. return 0;
  305. }
  306. static int
  307. jack_set_sample_rate (jack_engine_t *engine, nframes_t nframes)
  308. {
  309. engine->control->time.frame_rate = nframes;
  310. return 0;
  311. }
  312. int
  313. jack_engine_process_lock (jack_engine_t *engine)
  314. {
  315. return pthread_mutex_trylock (&engine->graph_lock);
  316. }
  317. int
  318. jack_engine_process_unlock (jack_engine_t *engine)
  319. {
  320. return pthread_mutex_unlock (&engine->graph_lock);
  321. }
  322. static int
  323. jack_process (jack_engine_t *engine, nframes_t nframes)
  324. {
  325. int err = 0;
  326. jack_client_internal_t *client;
  327. jack_client_control_t *ctl;
  328. GSList *node;
  329. struct pollfd pollfd[1];
  330. char c;
  331. engine->process_errors = 0;
  332. for (node = engine->clients; node; node = g_slist_next (node)) {
  333. ctl = ((jack_client_internal_t *) node->data)->control;
  334. ctl->state = NotTriggered;
  335. ctl->nframes = nframes;
  336. }
  337. if (engine->timebase_client) {
  338. engine->control->time.frame = engine->timebase_client->control->frame_time;
  339. }
  340. for (node = engine->clients; err == 0 && node; ) {
  341. client = (jack_client_internal_t *) node->data;
  342. if (!client->control->active || client->control->dead) {
  343. node = g_slist_next (node);
  344. continue;
  345. }
  346. ctl = client->control;
  347. if (jack_client_is_inprocess (client)) {
  348. /* in-process client ("plugin") */
  349. ctl->state = Running;
  350. if (ctl->process (nframes, ctl->process_arg) == 0) {
  351. ctl->state = Finished;
  352. } else {
  353. jack_error ("in-process client %s failed", client->control->name);
  354. engine->process_errors++;
  355. break;
  356. }
  357. node = g_slist_next (node);
  358. } else {
  359. /* out of process subgraph */
  360. ctl->state = Triggered; // a race exists if we do this after the write(2)
  361. printf ("start subgraph\n");
  362. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  363. jack_error ("cannot initiate graph processing (%s)", strerror (errno));
  364. engine->process_errors++;
  365. break;
  366. }
  367. /* now wait for the result. use poll instead of read so that we
  368. can timeout effectively.
  369. */
  370. pollfd[0].fd = client->subgraph_wait_fd;
  371. pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  372. if (poll (pollfd, 1, engine->driver->period_interval) < 0) {
  373. if (errno == EINTR) {
  374. continue;
  375. }
  376. jack_error ("engine cannot poll for graph completion (%s)", strerror (errno));
  377. engine->process_errors++;
  378. break;
  379. }
  380. if (pollfd[0].revents == 0) {
  381. jack_error ("subgraph starting at %s timed out (state = %d)", client->control->name, client->control->state);
  382. engine->process_errors++;
  383. break;
  384. } else if (pollfd[0].revents & ~POLLIN) {
  385. jack_error ("error/hangup on graph wait fd");
  386. engine->process_errors++;
  387. break;
  388. } else {
  389. if (read (client->subgraph_wait_fd, &c, sizeof (c)) != sizeof (c)) {
  390. jack_error ("cannot clean up byte from graph wait fd (%s)", strerror (errno));
  391. engine->process_errors++;
  392. break;
  393. }
  394. }
  395. printf ("subgraph done\n");
  396. /* Move to next in-process client (or end of client list) */
  397. while (node) {
  398. if (jack_client_is_inprocess (((jack_client_internal_t *) node->data))) {
  399. break;
  400. }
  401. node = g_slist_next (node);
  402. }
  403. }
  404. }
  405. return engine->process_errors > 0;
  406. }
  407. int
  408. jack_engine_post_process (jack_engine_t *engine)
  409. {
  410. if (engine->process_errors) {
  411. jack_cleanup_clients (engine);
  412. }
  413. return 0;
  414. }
  415. static int
  416. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client, const char *path_to_so)
  417. {
  418. const char *errstr;
  419. dlhandle handle;
  420. handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  421. if (handle == 0) {
  422. if ((errstr = dlerror ()) != 0) {
  423. jack_error ("can't load \"%s\": %s", path_to_so, errstr);
  424. } else {
  425. jack_error ("bizarre error loading driver shared object %s", path_to_so);
  426. }
  427. return -1;
  428. }
  429. client->handle = handle;
  430. #if 0
  431. initialize = dlsym (handle, "client_initialize");
  432. if ((errstr = dlerror ()) != 0) {
  433. jack_error ("no initialize function in shared object %s\n", path_to_so);
  434. dlclose (handle);
  435. return -1;
  436. }
  437. finish = dlsym (handle, "client_finish");
  438. if ((errstr = dlerror ()) != 0) {
  439. jack_error ("no finish function in in shared driver object %s", path_to_so);
  440. dlclose (handle);
  441. return -1;
  442. }
  443. #endif
  444. return 0;
  445. }
  446. static void
  447. jack_client_unload (jack_client_internal_t *client)
  448. {
  449. if (client->handle) {
  450. // client->finish (client);
  451. dlclose (client->handle);
  452. }
  453. }
  454. static int
  455. handle_new_client (jack_engine_t *engine, int client_fd)
  456. {
  457. GSList *node;
  458. jack_client_internal_t *client = NULL;
  459. jack_client_connect_request_t req;
  460. jack_client_connect_result_t res;
  461. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  462. jack_error ("cannot read connection request from client");
  463. return -1;
  464. }
  465. res.status = 0;
  466. for (node = engine->clients; node; node = g_slist_next (node)) {
  467. client = (jack_client_internal_t *) node->data;
  468. if (strncmp(req.name, (char*)client->control->name, sizeof(req.name)) == 0) {
  469. jack_error ("cannot create new client; %s already exists", client->control->name);
  470. res.status = -1;
  471. }
  472. }
  473. if (res.status == 0) {
  474. if ((client = jack_client_internal_new (engine, client_fd, &req)) == NULL) {
  475. jack_error ("cannot create new client object");
  476. return -1;
  477. }
  478. if (engine->verbose) {
  479. fprintf (stderr, "new client: %s, id = %d type %d @ %p fd = %d\n",
  480. client->control->name, client->control->id,
  481. req.type, client->control, client_fd);
  482. }
  483. res.client_key = client->shm_key;
  484. res.control_key = engine->control_key;
  485. res.port_segment_key = engine->port_segment_key;
  486. res.realtime = engine->control->real_time;
  487. res.realtime_priority = engine->rtpriority - 1;
  488. if (jack_client_is_inprocess (client)) {
  489. res.client_control = client->control;
  490. res.engine_control = engine->control;
  491. } else {
  492. strcpy (res.fifo_prefix, engine->fifo_prefix);
  493. }
  494. }
  495. if (client == NULL) {
  496. return -1;
  497. }
  498. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  499. jack_error ("cannot write connection response to client");
  500. jack_client_delete (engine, client);
  501. return -1;
  502. }
  503. if (res.status) {
  504. return res.status;
  505. }
  506. pthread_mutex_lock (&engine->graph_lock);
  507. engine->clients = g_slist_prepend (engine->clients, client);
  508. if (client->control->type != ClientDynamic) {
  509. if (engine->pfd_max >= engine->pfd_size) {
  510. engine->pfd = (struct pollfd *) realloc (engine->pfd, sizeof (struct pollfd) * engine->pfd_size + 16);
  511. engine->pfd_size += 16;
  512. }
  513. engine->pfd[engine->pfd_max].fd = client->request_fd;
  514. engine->pfd[engine->pfd_max].events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  515. engine->pfd_max++;
  516. }
  517. pthread_mutex_unlock (&engine->graph_lock);
  518. return 0;
  519. }
  520. static int
  521. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  522. {
  523. jack_client_internal_t *client;
  524. jack_client_connect_ack_request_t req;
  525. jack_client_connect_ack_result_t res;
  526. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  527. jack_error ("cannot read ACK connection request from client");
  528. return -1;
  529. }
  530. printf ("ack message, id = %lu\n", req.client_id);
  531. if ((client = jack_client_internal_by_id (engine, req.client_id)) == NULL) {
  532. jack_error ("unknown client ID in ACK connection request");
  533. return -1;
  534. }
  535. client->event_fd = client_fd;
  536. res.status = 0;
  537. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  538. jack_error ("cannot write ACK connection response to client");
  539. return -1;
  540. }
  541. return 0;
  542. }
  543. static int
  544. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  545. {
  546. jack_client_internal_t *client;
  547. GSList *node;
  548. int ret = -1;
  549. pthread_mutex_lock (&engine->graph_lock);
  550. for (node = engine->clients; node; node = g_slist_next (node)) {
  551. if (((jack_client_internal_t *) node->data)->control->id == id) {
  552. client = (jack_client_internal_t *) node->data;
  553. client->control->active = TRUE;
  554. /* we call this to make sure the FIFO is built by the time
  555. the client needs it. we don't care about the return
  556. value at this point.
  557. */
  558. jack_get_fifo_fd (engine, ++engine->external_client_cnt);
  559. jack_rechain_graph (engine, FALSE);
  560. ret = 0;
  561. break;
  562. }
  563. }
  564. pthread_mutex_unlock (&engine->graph_lock);
  565. return ret;
  566. }
  567. static int
  568. jack_client_do_deactivate (jack_engine_t *engine, jack_client_internal_t *client)
  569. {
  570. /* called must hold engine->graph_lock and must have checked for and/or
  571. cleared all connections held by client.
  572. */
  573. client->control->active = FALSE;
  574. if (!jack_client_is_inprocess (client)) {
  575. engine->external_client_cnt--;
  576. }
  577. jack_sort_graph (engine, FALSE);
  578. return 0;
  579. }
  580. static void
  581. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  582. {
  583. GSList *node;
  584. jack_port_internal_t *port;
  585. /* call tree **** MUST HOLD *** engine->graph_lock */
  586. for (node = client->ports; node; node = g_slist_next (node)) {
  587. port = (jack_port_internal_t *) node->data;
  588. jack_port_clear_connections (engine, port);
  589. jack_port_release (engine, port);
  590. }
  591. g_slist_free (client->ports);
  592. g_slist_free (client->fed_by);
  593. client->fed_by = 0;
  594. client->ports = 0;
  595. }
  596. static int
  597. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id, int to_wait)
  598. {
  599. GSList *node;
  600. int ret = -1;
  601. pthread_mutex_lock (&engine->graph_lock);
  602. for (node = engine->clients; node; node = g_slist_next (node)) {
  603. jack_client_internal_t *client = (jack_client_internal_t *) node->data;
  604. if (client->control->id == id) {
  605. GSList *portnode;
  606. jack_port_internal_t *port;
  607. if (client == engine->timebase_client) {
  608. engine->timebase_client = 0;
  609. engine->control->time.frame = 0;
  610. }
  611. for (portnode = client->ports; portnode; portnode = g_slist_next (portnode)) {
  612. port = (jack_port_internal_t *) portnode->data;
  613. jack_port_clear_connections (engine, port);
  614. }
  615. ret = jack_client_do_deactivate (engine, node->data);
  616. break;
  617. }
  618. }
  619. pthread_mutex_unlock (&engine->graph_lock);
  620. return ret;
  621. }
  622. static int
  623. jack_set_timebase (jack_engine_t *engine, jack_client_id_t client)
  624. {
  625. int ret = -1;
  626. pthread_mutex_lock (&engine->graph_lock);
  627. if ((engine->timebase_client = jack_client_internal_by_id (engine, client)) != 0) {
  628. engine->control->time.frame = engine->timebase_client->control->frame_time;
  629. ret = 0;
  630. }
  631. pthread_mutex_unlock (&engine->graph_lock);
  632. return ret;
  633. }
  634. static int
  635. handle_client_jack_error (jack_engine_t *engine, int fd)
  636. {
  637. jack_client_internal_t *client = 0;
  638. GSList *node;
  639. pthread_mutex_lock (&engine->graph_lock);
  640. for (node = engine->clients; node; node = g_slist_next (node)) {
  641. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  642. client = (jack_client_internal_t *) node->data;
  643. break;
  644. }
  645. }
  646. if (client == 0) {
  647. jack_error ("no client found for fd %d\n", fd);
  648. pthread_mutex_unlock (&engine->graph_lock);
  649. return -1;
  650. }
  651. jack_remove_client (engine, client);
  652. pthread_mutex_unlock (&engine->graph_lock);
  653. return 0;
  654. }
  655. static int
  656. handle_client_io (jack_engine_t *engine, int fd)
  657. {
  658. jack_request_t req;
  659. jack_client_internal_t *client = 0;
  660. int reply_fd;
  661. GSList *node;
  662. pthread_mutex_lock (&engine->graph_lock);
  663. for (node = engine->clients; node; node = g_slist_next (node)) {
  664. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  665. client = (jack_client_internal_t *) node->data;
  666. break;
  667. }
  668. }
  669. pthread_mutex_unlock (&engine->graph_lock);
  670. if (client == 0) {
  671. jack_error ("client input on unknown fd %d!", fd);
  672. return -1;
  673. }
  674. if (read (client->request_fd, &req, sizeof (req)) < sizeof (req)) {
  675. jack_error ("cannot read request from client");
  676. /* XXX interlock problems with the driver thread here */
  677. jack_remove_client (engine, client);
  678. return -1;
  679. }
  680. reply_fd = client->request_fd;
  681. switch (req.type) {
  682. case RegisterPort:
  683. req.status = jack_port_do_register (engine, &req);
  684. break;
  685. case UnRegisterPort:
  686. req.status = jack_port_do_unregister (engine, &req);
  687. break;
  688. case ConnectPorts:
  689. req.status = jack_port_do_connect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  690. break;
  691. case DisconnectPort:
  692. req.status = jack_port_do_disconnect_all (engine, req.x.port_info.port_id);
  693. break;
  694. case DisconnectPorts:
  695. req.status = jack_port_do_disconnect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  696. break;
  697. case ActivateClient:
  698. req.status = jack_client_activate (engine, req.x.client_id);
  699. break;
  700. case DeactivateClient:
  701. req.status = jack_client_deactivate (engine, req.x.client_id, TRUE);
  702. break;
  703. case SetTimeBaseClient:
  704. req.status = jack_set_timebase (engine, req.x.client_id);
  705. break;
  706. case GetPortTotalLatency:
  707. req.status = jack_get_total_latency (engine, req.x.port_info.name, &req.x.nframes);
  708. break;
  709. default:
  710. /* some requests are handled entirely on the client side,
  711. by adjusting the shared memory area(s)
  712. */
  713. break;
  714. }
  715. if (reply_fd >= 0) {
  716. if (write (reply_fd, &req, sizeof (req)) < sizeof (req)) {
  717. jack_error ("cannot write request result to client");
  718. return -1;
  719. }
  720. }
  721. return 0;
  722. }
  723. static void *
  724. jack_server_thread (void *arg)
  725. {
  726. jack_engine_t *engine = (jack_engine_t *) arg;
  727. struct sockaddr_un client_addr;
  728. socklen_t client_addrlen;
  729. struct pollfd *pfd;
  730. int client_socket;
  731. int done = 0;
  732. int i;
  733. int max;
  734. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  735. engine->pfd[0].fd = engine->fds[0];
  736. engine->pfd[0].events = POLLIN|POLLERR;
  737. engine->pfd[1].fd = engine->fds[1];
  738. engine->pfd[1].events = POLLIN|POLLERR;
  739. engine->pfd_max = 2;
  740. while (!done) {
  741. /* XXX race here with new external clients
  742. causing engine->pfd to be reallocated.
  743. I don't know how to solve this
  744. short of copying the entire
  745. contents of the pfd struct. Ick.
  746. */
  747. max = engine->pfd_max;
  748. pfd = engine->pfd;
  749. if (poll (pfd, max, 10000) < 0) {
  750. if (errno == EINTR) {
  751. continue;
  752. }
  753. jack_error ("poll failed (%s)", strerror (errno));
  754. break;
  755. }
  756. /* check the master server socket */
  757. if (pfd[0].revents & POLLERR) {
  758. jack_error ("error on server socket");
  759. break;
  760. }
  761. if (pfd[0].revents & POLLIN) {
  762. memset (&client_addr, 0, sizeof (client_addr));
  763. client_addrlen = sizeof (client_addr);
  764. if ((client_socket = accept (engine->fds[0], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  765. jack_error ("cannot accept new connection (%s)", strerror (errno));
  766. } else if (handle_new_client (engine, client_socket) < 0) {
  767. jack_error ("cannot complete new client connection process");
  768. close (client_socket);
  769. }
  770. }
  771. /* check the ACK server socket */
  772. if (pfd[1].revents & POLLERR) {
  773. jack_error ("error on server ACK socket");
  774. break;
  775. }
  776. if (pfd[1].revents & POLLIN) {
  777. memset (&client_addr, 0, sizeof (client_addr));
  778. client_addrlen = sizeof (client_addr);
  779. if ((client_socket = accept (engine->fds[1], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  780. jack_error ("cannot accept new ACK connection (%s)", strerror (errno));
  781. } else if (handle_client_ack_connection (engine, client_socket)) {
  782. jack_error ("cannot complete client ACK connection process");
  783. close (client_socket);
  784. }
  785. }
  786. /* check each client socket */
  787. for (i = 2; i < max; i++) {
  788. if (pfd[i].fd < 0) {
  789. continue;
  790. }
  791. if (pfd[i].revents & ~POLLIN) {
  792. printf ("bad poll status on pfd[%d] (%d) = 0x%x\n", i, pfd[i].fd, pfd[i].revents);
  793. handle_client_jack_error (engine, pfd[i].fd);
  794. } else if (pfd[i].revents & POLLIN) {
  795. if (handle_client_io (engine, pfd[i].fd)) {
  796. jack_error ("bad hci\n");
  797. }
  798. }
  799. }
  800. }
  801. return 0;
  802. }
  803. static void
  804. jack_start_server (jack_engine_t *engine)
  805. {
  806. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  807. pthread_detach (engine->server_thread);
  808. }
  809. jack_engine_t *
  810. jack_engine_new (int realtime, int rtpriority, int verbose)
  811. {
  812. jack_engine_t *engine;
  813. size_t control_size;
  814. void *addr;
  815. int i;
  816. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  817. engine->driver = 0;
  818. engine->process = jack_process;
  819. engine->set_sample_rate = jack_set_sample_rate;
  820. engine->set_buffer_size = jack_set_buffer_size;
  821. engine->process_lock = jack_engine_process_lock;
  822. engine->process_unlock = jack_engine_process_unlock;
  823. engine->post_process = jack_engine_post_process;
  824. engine->next_client_id = 1;
  825. engine->timebase_client = 0;
  826. engine->port_max = 128;
  827. engine->rtpriority = rtpriority;
  828. engine->silent_buffer = 0;
  829. engine->verbose = verbose;
  830. pthread_mutex_init (&engine->graph_lock, 0);
  831. pthread_mutex_init (&engine->buffer_lock, 0);
  832. pthread_mutex_init (&engine->port_lock, 0);
  833. engine->clients = 0;
  834. engine->port_segments = 0;
  835. engine->port_buffer_freelist = 0;
  836. engine->pfd_size = 16;
  837. engine->pfd_max = 0;
  838. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * engine->pfd_size);
  839. engine->fifo_size = 16;
  840. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  841. for (i = 0; i < engine->fifo_size; i++) {
  842. engine->fifo[i] = -1;
  843. }
  844. engine->external_client_cnt = 0;
  845. srandom (time ((time_t *) 0));
  846. engine->control_key = random();
  847. control_size = sizeof (jack_control_t) + (sizeof (jack_port_shared_t) * engine->port_max);
  848. if (jack_initialize_shm (engine)) {
  849. return 0;
  850. }
  851. if ((engine->control_shm_id = shmget (engine->control_key, control_size, IPC_CREAT|0644)) < 0) {
  852. jack_error ("cannot create engine control shared memory segment (%s)", strerror (errno));
  853. return 0;
  854. }
  855. jack_register_shm (engine->control_shm_id);
  856. if ((addr = shmat (engine->control_shm_id, 0, 0)) == (void *) -1) {
  857. jack_error ("cannot attach control shared memory segment (%s)", strerror (errno));
  858. shmctl (engine->control_shm_id, IPC_RMID, 0);
  859. return 0;
  860. }
  861. engine->control = (jack_control_t *) addr;
  862. /* Mark all ports as available */
  863. for (i = 0; i < engine->port_max; i++) {
  864. engine->control->ports[i].in_use = 0;
  865. engine->control->ports[i].id = i;
  866. }
  867. /* allocate internal port structures so that we can keep
  868. track of port connections.
  869. */
  870. engine->internal_ports = (jack_port_internal_t *) malloc (sizeof (jack_port_internal_t) * engine->port_max);
  871. for (i = 0; i < engine->port_max; i++) {
  872. engine->internal_ports[i].connections = 0;
  873. }
  874. if (make_sockets (engine->fds) < 0) {
  875. jack_error ("cannot create server sockets");
  876. return 0;
  877. }
  878. engine->control->port_max = engine->port_max;
  879. engine->control->real_time = realtime;
  880. engine->control->client_priority = engine->rtpriority - 1;
  881. engine->control->buffer_size = 0;
  882. engine->control->time.frame_rate = 0;
  883. engine->control->time.frame = 0;
  884. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix), "%s/jack-ack-fifo-%d", jack_temp_dir, getpid());
  885. (void) jack_get_fifo_fd (engine, 0);
  886. jack_start_server (engine);
  887. return engine;
  888. }
  889. static int
  890. jack_become_real_time (pthread_t thread, int priority)
  891. {
  892. struct sched_param rtparam;
  893. int x;
  894. memset (&rtparam, 0, sizeof (rtparam));
  895. rtparam.sched_priority = priority;
  896. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  897. jack_error ("cannot set thread to real-time priority (FIFO/%d) (%d: %s)", rtparam.sched_priority, x, strerror (errno));
  898. return -1;
  899. }
  900. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  901. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  902. return -1;
  903. }
  904. return 0;
  905. }
  906. static void
  907. cancel_cleanup (int status, void *arg)
  908. {
  909. jack_engine_t *engine = (jack_engine_t *) arg;
  910. engine->driver->stop (engine->driver);
  911. engine->driver->finish (engine->driver);
  912. }
  913. static void *
  914. jack_main_thread (void *arg)
  915. {
  916. jack_engine_t *engine = (jack_engine_t *) arg;
  917. jack_driver_t *driver = engine->driver;
  918. if (engine->control->real_time) {
  919. if (jack_become_real_time (pthread_self(), engine->rtpriority)) {
  920. engine->control->real_time = 0;
  921. }
  922. }
  923. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  924. on_exit (cancel_cleanup, engine);
  925. if (driver->start (driver)) {
  926. jack_error ("cannot start driver");
  927. pthread_exit (0);
  928. }
  929. while (1) {
  930. switch (driver->wait (driver)) {
  931. case -1:
  932. jack_error ("driver wait function failed, exiting");
  933. pthread_exit (0);
  934. break;
  935. case 1:
  936. if (driver->start (driver)) {
  937. jack_error ("cannot restart driver");
  938. pthread_exit (0);
  939. }
  940. break;
  941. default:
  942. break;
  943. }
  944. }
  945. pthread_exit (0);
  946. }
  947. int
  948. jack_run (jack_engine_t *engine)
  949. {
  950. if (engine->driver == 0) {
  951. jack_error ("engine driver not set; cannot start");
  952. return -1;
  953. }
  954. return pthread_create (&engine->main_thread, 0, jack_main_thread, engine);
  955. }
  956. int
  957. jack_wait (jack_engine_t *engine)
  958. {
  959. void *ret = 0;
  960. int err;
  961. if ((err = pthread_join (engine->main_thread, &ret)) != 0) {
  962. switch (err) {
  963. case EINVAL:
  964. jack_error ("cannot join with audio thread (thread detached, or another thread is waiting)");
  965. break;
  966. case ESRCH:
  967. jack_error ("cannot join with audio thread (thread no longer exists)");
  968. break;
  969. case EDEADLK:
  970. jack_error ("programming error: jack_wait() called by audio thread");
  971. break;
  972. default:
  973. jack_error ("cannot join with audio thread (%s)", strerror (errno));
  974. }
  975. }
  976. return (int) ret;
  977. }
  978. int
  979. jack_engine_delete (jack_engine_t *engine)
  980. {
  981. if (engine) {
  982. return pthread_cancel (engine->main_thread);
  983. }
  984. return 0;
  985. }
  986. static jack_client_internal_t *
  987. jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *req)
  988. {
  989. jack_client_internal_t *client;
  990. key_t shm_key = 0;
  991. int shm_id = 0;
  992. void *addr = 0;
  993. switch (req->type) {
  994. case ClientDynamic:
  995. case ClientDriver:
  996. break;
  997. case ClientOutOfProcess:
  998. shm_key = random();
  999. if ((shm_id = shmget (shm_key, sizeof (jack_client_control_t), IPC_CREAT|0666)) < 0) {
  1000. jack_error ("cannot create client control block");
  1001. return 0;
  1002. }
  1003. jack_register_shm (shm_id);
  1004. if ((addr = shmat (shm_id, 0, 0)) == (void *) -1) {
  1005. jack_error ("cannot attach new client control block");
  1006. shmctl (shm_id, IPC_RMID, 0);
  1007. return 0;
  1008. }
  1009. break;
  1010. }
  1011. client = (jack_client_internal_t *) malloc (sizeof (jack_client_internal_t));
  1012. client->request_fd = fd;
  1013. client->event_fd = -1;
  1014. client->ports = 0;
  1015. client->fed_by = 0;
  1016. client->rank = UINT_MAX;
  1017. client->next_client = NULL;
  1018. client->handle = NULL;
  1019. if (req->type != ClientOutOfProcess) {
  1020. client->control = (jack_client_control_t *) malloc (sizeof (jack_client_control_t));
  1021. } else {
  1022. client->shm_id = shm_id;
  1023. client->shm_key = shm_key;
  1024. client->control = (jack_client_control_t *) addr;
  1025. }
  1026. client->control->type = req->type;
  1027. client->control->active = FALSE;
  1028. client->control->dead = FALSE;
  1029. client->control->id = engine->next_client_id++;
  1030. strcpy ((char *) client->control->name, req->name);
  1031. client->control->process = NULL;
  1032. client->control->process_arg = NULL;
  1033. client->control->bufsize = NULL;
  1034. client->control->bufsize_arg = NULL;
  1035. client->control->srate = NULL;
  1036. client->control->srate_arg = NULL;
  1037. client->control->port_register = NULL;
  1038. client->control->port_register_arg = NULL;
  1039. if (req->type == ClientDynamic) {
  1040. if (jack_load_client (engine, client, req->object_path)) {
  1041. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1042. jack_client_delete (engine, client);
  1043. return 0;
  1044. }
  1045. }
  1046. return client;
  1047. }
  1048. static void
  1049. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1050. {
  1051. GSList *node, *next;
  1052. for (node = port->connections; node; ) {
  1053. next = g_slist_next (node);
  1054. jack_port_disconnect_internal (engine,
  1055. ((jack_connection_internal_t *) node->data)->source,
  1056. ((jack_connection_internal_t *) node->data)->destination,
  1057. FALSE);
  1058. node = next;
  1059. }
  1060. g_slist_free (port->connections);
  1061. port->connections = 0;
  1062. }
  1063. static void
  1064. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1065. {
  1066. GSList *node;
  1067. int i;
  1068. /* caller must hold the graph_lock */
  1069. printf ("remove client\n");
  1070. /* these stop the process() loop from paying this client any attention,
  1071. as well as stopping jack_deliver_event() from bothering to try to
  1072. talk to the client.
  1073. */
  1074. client->control->dead = TRUE;
  1075. client->control->active = FALSE;
  1076. if (client == engine->timebase_client) {
  1077. engine->timebase_client = 0;
  1078. engine->control->time.frame = 0;
  1079. }
  1080. jack_client_disconnect (engine, client);
  1081. /* try to force the server thread to return from poll */
  1082. close (client->event_fd);
  1083. close (client->request_fd);
  1084. for (node = engine->clients; node; node = g_slist_next (node)) {
  1085. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1086. engine->clients = g_slist_remove_link (engine->clients, node);
  1087. g_slist_free_1 (node);
  1088. break;
  1089. }
  1090. }
  1091. jack_client_do_deactivate (engine, client);
  1092. /* rearrange the pollfd array so that things work right the
  1093. next time we go into poll(2).
  1094. */
  1095. for (i = 0; i < engine->pfd_max; i++) {
  1096. if (engine->pfd[i].fd == client->request_fd) {
  1097. if (i+1 < engine->pfd_max) {
  1098. memmove (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1099. }
  1100. engine->pfd_max--;
  1101. }
  1102. }
  1103. jack_client_delete (engine, client);
  1104. }
  1105. static void
  1106. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1107. {
  1108. jack_client_disconnect (engine, client);
  1109. if (jack_client_is_inprocess (client)) {
  1110. jack_client_unload (client);
  1111. free ((char *) client->control);
  1112. } else {
  1113. shmdt ((void *) client->control);
  1114. }
  1115. free (client);
  1116. }
  1117. jack_client_internal_t *
  1118. jack_client_by_name (jack_engine_t *engine, const char *name)
  1119. {
  1120. jack_client_internal_t *client = NULL;
  1121. GSList *node;
  1122. pthread_mutex_lock (&engine->graph_lock);
  1123. for (node = engine->clients; node; node = g_slist_next (node)) {
  1124. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1125. client = (jack_client_internal_t *) node->data;
  1126. break;
  1127. }
  1128. }
  1129. pthread_mutex_unlock (&engine->graph_lock);
  1130. return client;
  1131. }
  1132. jack_client_internal_t *
  1133. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1134. {
  1135. jack_client_internal_t *client = NULL;
  1136. GSList *node;
  1137. /* call tree ***MUST HOLD*** engine->graph_lock */
  1138. for (node = engine->clients; node; node = g_slist_next (node)) {
  1139. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1140. client = (jack_client_internal_t *) node->data;
  1141. break;
  1142. }
  1143. }
  1144. return client;
  1145. }
  1146. static int
  1147. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1148. {
  1149. char status;
  1150. int client_err = 0;
  1151. if (client->control->dead) {
  1152. return 0;
  1153. }
  1154. if (jack_client_is_inprocess (client)) {
  1155. switch (event->type) {
  1156. case PortConnected:
  1157. case PortDisconnected:
  1158. jack_client_handle_port_connection (client->control->private_internal_client, event);
  1159. break;
  1160. case BufferSizeChange:
  1161. if (client->control->bufsize) {
  1162. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1163. }
  1164. break;
  1165. case SampleRateChange:
  1166. if (client->control->srate) {
  1167. client->control->srate (event->x.n, client->control->bufsize_arg);
  1168. }
  1169. break;
  1170. default:
  1171. /* internal clients don't need to know */
  1172. break;
  1173. }
  1174. } else {
  1175. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1176. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1177. client_err++;
  1178. }
  1179. if (!client_err && (read (client->event_fd, &status, sizeof (status)) != sizeof (status))) {
  1180. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1181. client_err++;
  1182. }
  1183. if (client_err || status != 0) {
  1184. pthread_mutex_lock (&engine->graph_lock);
  1185. jack_remove_client (engine, client);
  1186. pthread_mutex_unlock (&engine->graph_lock);
  1187. }
  1188. }
  1189. return 0;
  1190. }
  1191. int
  1192. jack_client_set_order (jack_engine_t *engine, jack_client_internal_t *client)
  1193. {
  1194. jack_event_t event;
  1195. event.type = GraphReordered;
  1196. event.x.n = client->rank;
  1197. return jack_deliver_event (engine, client, &event);
  1198. }
  1199. int
  1200. jack_rechain_graph (jack_engine_t *engine, int take_lock)
  1201. {
  1202. GSList *node, *next;
  1203. unsigned long n;
  1204. int err = 0;
  1205. int need_to_reset_fifo;
  1206. jack_client_internal_t *client, *subgraph_client, *next_client;
  1207. if (take_lock) {
  1208. pthread_mutex_lock (&engine->graph_lock);
  1209. }
  1210. /* We're going to try to avoid reconnecting clients that
  1211. don't need to be reconnected. This is slightly tricky,
  1212. but worth it for performance reasons.
  1213. */
  1214. subgraph_client = 0;
  1215. n = 0;
  1216. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1217. next = g_slist_next (node);
  1218. if (((jack_client_internal_t *) node->data)->control->active) {
  1219. client = (jack_client_internal_t *) node->data;
  1220. /* find the next active client. its ok for this to be NULL */
  1221. while (next) {
  1222. if (((jack_client_internal_t *) next->data)->control->active) {
  1223. break;
  1224. }
  1225. next = g_slist_next (next);
  1226. };
  1227. if (next == NULL) {
  1228. next_client = NULL;
  1229. } else {
  1230. next_client = (jack_client_internal_t *) next->data;
  1231. }
  1232. if (client->rank != n || client->next_client != next_client) {
  1233. client->rank = n;
  1234. client->next_client = next_client;
  1235. need_to_reset_fifo = TRUE;
  1236. } else {
  1237. need_to_reset_fifo = FALSE;
  1238. }
  1239. if (jack_client_is_inprocess (client)) {
  1240. /* break the chain for the current subgraph. the server
  1241. will wait for chain on the nth FIFO, and will
  1242. then execute this in-process client.
  1243. */
  1244. if (subgraph_client) {
  1245. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1246. }
  1247. subgraph_client = 0;
  1248. } else {
  1249. if (subgraph_client == 0) {
  1250. /* start a new subgraph. the engine will start the chain
  1251. by writing to the nth FIFO.
  1252. */
  1253. subgraph_client = client;
  1254. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1255. }
  1256. if (need_to_reset_fifo) {
  1257. jack_client_set_order (engine, client);
  1258. }
  1259. n++;
  1260. }
  1261. }
  1262. };
  1263. if (subgraph_client) {
  1264. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1265. }
  1266. if (take_lock) {
  1267. pthread_mutex_unlock (&engine->graph_lock);
  1268. }
  1269. return err;
  1270. }
  1271. static void
  1272. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1273. {
  1274. jack_client_internal_t *c2;
  1275. /* make a copy of the existing list of routes that feed c1 */
  1276. GSList *existing;
  1277. GSList *node;
  1278. if (c1->fed_by == 0) {
  1279. return;
  1280. }
  1281. existing = g_slist_copy (c1->fed_by);
  1282. /* for each route that feeds c1, recurse, marking it as feeding
  1283. rbase as well.
  1284. */
  1285. for (node = existing; node; node = g_slist_next (node)) {
  1286. c2 = (jack_client_internal_t *) node->data;
  1287. /* c2 is a route that feeds c1 which somehow feeds base. mark
  1288. base as being fed by c2, but don't do it more than
  1289. once.
  1290. */
  1291. if (g_slist_find (rbase->fed_by, c2) != NULL) {
  1292. continue;
  1293. }
  1294. rbase->fed_by = g_slist_prepend (rbase->fed_by, c2);
  1295. if (c2 != rbase && c2 != c1) {
  1296. /* now recurse, so that we can mark base as being fed by
  1297. all routes that feed c2
  1298. */
  1299. jack_trace_terminal (c2, rbase);
  1300. }
  1301. }
  1302. }
  1303. static int
  1304. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  1305. {
  1306. if (g_slist_find (a->fed_by, b)) {
  1307. if (g_slist_find (b->fed_by, a)) {
  1308. /* feedback loop: if `a' is the driver
  1309. client, let that execute first.
  1310. */
  1311. if (a->control->type == ClientDriver) {
  1312. /* b comes after a */
  1313. return -1;
  1314. }
  1315. }
  1316. /* a comes after b */
  1317. return 1;
  1318. } else if (g_slist_find (b->fed_by, a)) {
  1319. if (g_slist_find (a->fed_by, b)) {
  1320. /* feedback loop: if `b' is the driver
  1321. client, let that execute first.
  1322. */
  1323. if (b->control->type == ClientDriver) {
  1324. /* b comes before a */
  1325. return 1;
  1326. }
  1327. }
  1328. /* b comes after a */
  1329. return -1;
  1330. } else {
  1331. /* we don't care */
  1332. return 0;
  1333. }
  1334. }
  1335. static int
  1336. jack_client_feeds (jack_client_internal_t *might, jack_client_internal_t *target)
  1337. {
  1338. GSList *pnode, *cnode;
  1339. /* Check every port of `might' for an outbound connection to `target'
  1340. */
  1341. for (pnode = might->ports; pnode; pnode = g_slist_next (pnode)) {
  1342. jack_port_internal_t *port;
  1343. port = (jack_port_internal_t *) pnode->data;
  1344. for (cnode = port->connections; cnode; cnode = g_slist_next (cnode)) {
  1345. jack_connection_internal_t *c;
  1346. c = (jack_connection_internal_t *) cnode->data;
  1347. if (c->source->shared->client_id == might->control->id &&
  1348. c->destination->shared->client_id == target->control->id) {
  1349. return 1;
  1350. }
  1351. }
  1352. }
  1353. return 0;
  1354. }
  1355. /**
  1356. * Sorts the network of clients using the following
  1357. * algorithm:
  1358. *
  1359. * 1) figure out who is connected to whom:
  1360. *
  1361. * foreach client1
  1362. * foreach input port
  1363. * foreach client2
  1364. * foreach output port
  1365. * if client1->input port connected to client2->output port
  1366. * mark client1 fed by client 2
  1367. *
  1368. * 2) trace the connections as terminal arcs in the graph so that
  1369. * if client A feeds client B who feeds client C, mark client C
  1370. * as fed by client A as well as client B, and so forth.
  1371. *
  1372. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  1373. * if the condition is true, client2 must execute before client1
  1374. *
  1375. */
  1376. static void
  1377. jack_sort_graph (jack_engine_t *engine, int take_lock)
  1378. {
  1379. GSList *node, *onode;
  1380. jack_client_internal_t *client;
  1381. jack_client_internal_t *oclient;
  1382. if (take_lock) {
  1383. pthread_mutex_lock (&engine->graph_lock);
  1384. }
  1385. for (node = engine->clients; node; node = g_slist_next (node)) {
  1386. client = (jack_client_internal_t *) node->data;
  1387. g_slist_free (client->fed_by);
  1388. client->fed_by = 0;
  1389. for (onode = engine->clients; onode; onode = g_slist_next (onode)) {
  1390. oclient = (jack_client_internal_t *) onode->data;
  1391. if (jack_client_feeds (oclient, client)) {
  1392. client->fed_by = g_slist_prepend (client->fed_by, oclient);
  1393. }
  1394. }
  1395. }
  1396. for (node = engine->clients; node; node = g_slist_next (node)) {
  1397. jack_trace_terminal ((jack_client_internal_t *) node->data,
  1398. (jack_client_internal_t *) node->data);
  1399. }
  1400. engine->clients = g_slist_sort (engine->clients, (GCompareFunc) jack_client_sort);
  1401. jack_rechain_graph (engine, FALSE);
  1402. if (take_lock) {
  1403. pthread_mutex_unlock (&engine->graph_lock);
  1404. }
  1405. }
  1406. /**
  1407. * Dumps current engine configuration to stderr.
  1408. */
  1409. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  1410. {
  1411. GSList *clientnode, *portnode, *connectionnode;
  1412. jack_client_internal_t *client;
  1413. jack_client_control_t *ctl;
  1414. jack_port_internal_t *port;
  1415. jack_connection_internal_t* connection;
  1416. int n, m, o;
  1417. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  1418. if (take_lock) {
  1419. pthread_mutex_lock (&engine->graph_lock);
  1420. }
  1421. for (n = 0, clientnode = engine->clients; clientnode; clientnode = g_slist_next (clientnode)) {
  1422. client = (jack_client_internal_t *) clientnode->data;
  1423. ctl = client->control;
  1424. fprintf (stderr, "client #%d: %s\n",
  1425. ++n,
  1426. ctl->name);
  1427. for(m = 0, portnode = client->ports; portnode; portnode = g_slist_next (portnode)) {
  1428. port = (jack_port_internal_t *) portnode->data;
  1429. fprintf(stderr, "\t port #%d: %s\n", ++m, port->shared->name);
  1430. for(o = 0, connectionnode = port->connections;
  1431. connectionnode;
  1432. connectionnode = g_slist_next (connectionnode)) {
  1433. connection = (jack_connection_internal_t *) connectionnode->data;
  1434. fprintf(stderr, "\t\t connection #%d: %s -> %s\n",
  1435. ++o,
  1436. connection->source->shared->name,
  1437. connection->destination->shared->name);
  1438. }
  1439. }
  1440. }
  1441. if (take_lock) {
  1442. pthread_mutex_unlock (&engine->graph_lock);
  1443. }
  1444. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  1445. }
  1446. static int
  1447. jack_port_do_connect (jack_engine_t *engine,
  1448. const char *source_port,
  1449. const char *destination_port)
  1450. {
  1451. jack_connection_internal_t *connection;
  1452. jack_port_internal_t *srcport, *dstport;
  1453. jack_port_id_t src_id, dst_id;
  1454. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1455. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1456. return -1;
  1457. }
  1458. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1459. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1460. return -1;
  1461. }
  1462. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  1463. jack_error ("destination port in attempted connection of %s and %s is not an input port",
  1464. source_port, destination_port);
  1465. return -1;
  1466. }
  1467. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  1468. jack_error ("source port in attempted connection of %s and %s is not an output port",
  1469. source_port, destination_port);
  1470. return -1;
  1471. }
  1472. if (srcport->shared->locked) {
  1473. jack_error ("source port %s is locked against connection changes", source_port);
  1474. return -1;
  1475. }
  1476. if (dstport->shared->locked) {
  1477. jack_error ("destination port %s is locked against connection changes", destination_port);
  1478. return -1;
  1479. }
  1480. if (strcmp (srcport->shared->type_info.type_name,
  1481. dstport->shared->type_info.type_name) != 0) {
  1482. jack_error ("ports used in attemped connection are not of the same data type");
  1483. return -1;
  1484. }
  1485. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  1486. connection->source = srcport;
  1487. connection->destination = dstport;
  1488. src_id = srcport->shared->id;
  1489. dst_id = dstport->shared->id;
  1490. pthread_mutex_lock (&engine->graph_lock);
  1491. if (dstport->connections && dstport->shared->type_info.mixdown == NULL) {
  1492. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  1493. free (connection);
  1494. return -1;
  1495. } else {
  1496. if (engine->verbose) {
  1497. fprintf (stderr, "connect %s and %s\n",
  1498. srcport->shared->name,
  1499. dstport->shared->name);
  1500. }
  1501. dstport->connections = g_slist_prepend (dstport->connections, connection);
  1502. srcport->connections = g_slist_prepend (srcport->connections, connection);
  1503. jack_sort_graph (engine, FALSE);
  1504. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  1505. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  1506. }
  1507. pthread_mutex_unlock (&engine->graph_lock);
  1508. return 0;
  1509. }
  1510. int
  1511. jack_port_disconnect_internal (jack_engine_t *engine,
  1512. jack_port_internal_t *srcport,
  1513. jack_port_internal_t *dstport,
  1514. int sort_graph)
  1515. {
  1516. GSList *node;
  1517. jack_connection_internal_t *connect;
  1518. int ret = -1;
  1519. jack_port_id_t src_id, dst_id;
  1520. /* call tree **** MUST HOLD **** engine->graph_lock. */
  1521. for (node = srcport->connections; node; node = g_slist_next (node)) {
  1522. connect = (jack_connection_internal_t *) node->data;
  1523. if (connect->source == srcport && connect->destination == dstport) {
  1524. if (engine->verbose) {
  1525. fprintf (stderr, "DIS-connect %s and %s\n",
  1526. srcport->shared->name,
  1527. dstport->shared->name);
  1528. }
  1529. srcport->connections = g_slist_remove (srcport->connections, connect);
  1530. dstport->connections = g_slist_remove (dstport->connections, connect);
  1531. src_id = srcport->shared->id;
  1532. dst_id = dstport->shared->id;
  1533. /* this is a bit harsh, but it basically says that if we actually
  1534. do a disconnect, and its the last one, then make sure that
  1535. any input monitoring is turned off on the srcport. this isn't
  1536. ideal for all situations, but it works better for most of them.
  1537. */
  1538. if (srcport->connections == NULL) {
  1539. srcport->shared->monitor_requests = 0;
  1540. }
  1541. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  1542. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  1543. free (connect);
  1544. ret = 0;
  1545. break;
  1546. }
  1547. }
  1548. if (sort_graph) {
  1549. jack_sort_graph (engine, FALSE);
  1550. }
  1551. return ret;
  1552. }
  1553. static int
  1554. jack_port_do_disconnect_all (jack_engine_t *engine,
  1555. jack_port_id_t port_id)
  1556. {
  1557. if (port_id >= engine->control->port_max) {
  1558. jack_error ("illegal port ID in attempted disconnection [%u]", port_id);
  1559. return -1;
  1560. }
  1561. if (engine->verbose) {
  1562. fprintf (stderr, "clear connections for %s\n", engine->internal_ports[port_id].shared->name);
  1563. }
  1564. pthread_mutex_lock (&engine->graph_lock);
  1565. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  1566. jack_sort_graph (engine, FALSE);
  1567. pthread_mutex_unlock (&engine->graph_lock);
  1568. return 0;
  1569. }
  1570. static int
  1571. jack_port_do_disconnect (jack_engine_t *engine,
  1572. const char *source_port,
  1573. const char *destination_port)
  1574. {
  1575. jack_port_internal_t *srcport, *dstport;
  1576. int ret = -1;
  1577. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1578. jack_error ("unknown source port in attempted disconnection [%s]", source_port);
  1579. return -1;
  1580. }
  1581. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1582. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1583. return -1;
  1584. }
  1585. pthread_mutex_lock (&engine->graph_lock);
  1586. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  1587. pthread_mutex_unlock (&engine->graph_lock);
  1588. return ret;
  1589. }
  1590. static int
  1591. jack_port_get_total_latency (jack_engine_t *engine, jack_port_internal_t *port, nframes_t *latency)
  1592. {
  1593. GSList *node;
  1594. /* call tree should hold engine->graph_lock. */
  1595. (*latency) = port->shared->latency;
  1596. for (node = port->connections; node; node = g_slist_next (node)) {
  1597. nframes_t this_latency;
  1598. jack_connection_internal_t *connection;
  1599. connection = (jack_connection_internal_t *) node->data;
  1600. /* if we're a destination in the connection, recurse on the source to
  1601. get its total latency
  1602. */
  1603. if (connection->destination == port) {
  1604. jack_port_get_total_latency (engine, connection->source, &this_latency);
  1605. if (this_latency > *latency) {
  1606. (*latency) = this_latency;
  1607. }
  1608. }
  1609. }
  1610. return 0;
  1611. }
  1612. static int
  1613. jack_get_total_latency (jack_engine_t *engine, const char *portname, nframes_t *latency)
  1614. {
  1615. jack_port_internal_t *port;
  1616. if ((port = jack_get_port_by_name (engine, portname)) == NULL) {
  1617. return -1;
  1618. }
  1619. return jack_port_get_total_latency (engine, port, latency);
  1620. }
  1621. static int
  1622. jack_get_fifo_fd (jack_engine_t *engine, int which_fifo)
  1623. {
  1624. char path[PATH_MAX+1];
  1625. struct stat statbuf;
  1626. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1627. if (stat (path, &statbuf)) {
  1628. if (errno == ENOENT) {
  1629. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  1630. jack_error ("cannot create inter-client FIFO [%s] (%s)\n", path, strerror (errno));
  1631. return -1;
  1632. }
  1633. } else {
  1634. jack_error ("cannot check on FIFO %d\n", which_fifo);
  1635. return -1;
  1636. }
  1637. } else {
  1638. if (!S_ISFIFO(statbuf.st_mode)) {
  1639. jack_error ("FIFO %d (%s) already exists, but is not a FIFO!\n", which_fifo, path);
  1640. return -1;
  1641. }
  1642. }
  1643. if (which_fifo >= engine->fifo_size) {
  1644. int i;
  1645. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  1646. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  1647. engine->fifo[i] = -1;
  1648. }
  1649. engine->fifo_size += 16;
  1650. }
  1651. if (engine->fifo[which_fifo] < 0) {
  1652. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT, 0666)) < 0) {
  1653. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  1654. return -1;
  1655. }
  1656. }
  1657. return engine->fifo[which_fifo];
  1658. }
  1659. int
  1660. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  1661. {
  1662. if (engine->driver) {
  1663. engine->driver->detach (engine->driver, engine);
  1664. engine->driver = 0;
  1665. }
  1666. if (driver) {
  1667. if (driver->attach (driver, engine)) {
  1668. return -1;
  1669. }
  1670. }
  1671. engine->driver = driver;
  1672. return 0;
  1673. }
  1674. /* PORT RELATED FUNCTIONS */
  1675. jack_port_id_t
  1676. jack_get_free_port (jack_engine_t *engine)
  1677. {
  1678. jack_port_id_t i;
  1679. pthread_mutex_lock (&engine->port_lock);
  1680. for (i = 0; i < engine->port_max; i++) {
  1681. if (engine->control->ports[i].in_use == 0) {
  1682. engine->control->ports[i].in_use = 1;
  1683. break;
  1684. }
  1685. }
  1686. pthread_mutex_unlock (&engine->port_lock);
  1687. if (i == engine->port_max) {
  1688. return NoPort;
  1689. }
  1690. return i;
  1691. }
  1692. static void
  1693. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  1694. {
  1695. pthread_mutex_lock (&engine->port_lock);
  1696. port->shared->in_use = 0;
  1697. if (port->buffer_info) {
  1698. pthread_mutex_lock (&engine->buffer_lock);
  1699. engine->port_buffer_freelist = g_slist_prepend (engine->port_buffer_freelist, port->buffer_info);
  1700. pthread_mutex_unlock (&engine->buffer_lock);
  1701. }
  1702. pthread_mutex_unlock (&engine->port_lock);
  1703. }
  1704. jack_port_internal_t *
  1705. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  1706. {
  1707. jack_port_id_t id;
  1708. pthread_mutex_lock (&engine->port_lock);
  1709. for (id = 0; id < engine->port_max; id++) {
  1710. if (strcmp (engine->control->ports[id].name, name) == 0) {
  1711. break;
  1712. }
  1713. }
  1714. pthread_mutex_unlock (&engine->port_lock);
  1715. if (id != engine->port_max) {
  1716. return &engine->internal_ports[id];
  1717. } else {
  1718. return NULL;
  1719. }
  1720. }
  1721. int
  1722. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  1723. {
  1724. jack_port_id_t port_id;
  1725. jack_port_shared_t *shared;
  1726. jack_port_internal_t *port;
  1727. jack_client_internal_t *client;
  1728. pthread_mutex_lock (&engine->graph_lock);
  1729. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == 0) {
  1730. jack_error ("unknown client id in port registration request");
  1731. return -1;
  1732. }
  1733. pthread_mutex_unlock (&engine->graph_lock);
  1734. if ((port_id = jack_get_free_port (engine)) == NoPort) {
  1735. jack_error ("no ports available!");
  1736. return -1;
  1737. }
  1738. shared = &engine->control->ports[port_id];
  1739. strcpy (shared->name, req->x.port_info.name);
  1740. shared->client_id = req->x.port_info.client_id;
  1741. shared->flags = req->x.port_info.flags;
  1742. shared->buffer_size = req->x.port_info.buffer_size;
  1743. shared->latency = 0;
  1744. shared->monitor_requests = 0;
  1745. shared->locked = 0;
  1746. port = &engine->internal_ports[port_id];
  1747. port->shared = shared;
  1748. port->connections = 0;
  1749. if (jack_port_assign_buffer (engine, port)) {
  1750. jack_error ("cannot assign buffer for port");
  1751. return -1;
  1752. }
  1753. pthread_mutex_lock (&engine->graph_lock);
  1754. client->ports = g_slist_prepend (client->ports, port);
  1755. jack_port_registration_notify (engine, port_id, TRUE);
  1756. pthread_mutex_unlock (&engine->graph_lock);
  1757. if (engine->verbose) {
  1758. fprintf (stderr, "registered port %s, offset = %u\n", shared->name, shared->offset);
  1759. }
  1760. req->x.port_info.port_id = port_id;
  1761. return 0;
  1762. }
  1763. int
  1764. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  1765. {
  1766. jack_client_internal_t *client;
  1767. jack_port_shared_t *shared;
  1768. jack_port_internal_t *port;
  1769. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  1770. jack_error ("invalid port ID %d in unregister request\n", req->x.port_info.port_id);
  1771. return -1;
  1772. }
  1773. shared = &engine->control->ports[req->x.port_info.port_id];
  1774. pthread_mutex_lock (&engine->graph_lock);
  1775. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  1776. jack_error ("unknown client id in port registration request");
  1777. pthread_mutex_unlock (&engine->graph_lock);
  1778. return -1;
  1779. }
  1780. port = &engine->internal_ports[req->x.port_info.port_id];
  1781. jack_port_clear_connections (engine, port);
  1782. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  1783. client->ports = g_slist_remove (client->ports, port);
  1784. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  1785. pthread_mutex_unlock (&engine->graph_lock);
  1786. return 0;
  1787. }
  1788. void
  1789. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  1790. {
  1791. jack_event_t event;
  1792. jack_client_internal_t *client;
  1793. GSList *node;
  1794. event.type = (yn ? PortRegistered : PortUnregistered);
  1795. event.x.port_id = port_id;
  1796. for (node = engine->clients; node; node = g_slist_next (node)) {
  1797. client = (jack_client_internal_t *) node->data;
  1798. if (!client->control->active) {
  1799. continue;
  1800. }
  1801. if (client->control->port_register) {
  1802. if (jack_deliver_event (engine, client, &event)) {
  1803. jack_error ("cannot send port registration notification to %s (%s)",
  1804. client->control->name, strerror (errno));
  1805. }
  1806. }
  1807. }
  1808. }
  1809. int
  1810. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  1811. {
  1812. GSList *node;
  1813. jack_port_segment_info_t *psi = 0;
  1814. jack_port_buffer_info_t *bi;
  1815. port->shared->shm_key = -1;
  1816. if (port->shared->flags & JackPortIsInput) {
  1817. port->shared->offset = 0;
  1818. return 0;
  1819. }
  1820. pthread_mutex_lock (&engine->buffer_lock);
  1821. if (engine->port_buffer_freelist == NULL) {
  1822. jack_error ("no more buffers available!");
  1823. goto out;
  1824. }
  1825. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  1826. for (node = engine->port_segments; node; node = g_slist_next (node)) {
  1827. psi = (jack_port_segment_info_t *) node->data;
  1828. if (bi->shm_key == psi->shm_key) {
  1829. port->shared->shm_key = psi->shm_key;
  1830. port->shared->offset = bi->offset;
  1831. port->buffer_info = bi;
  1832. break;
  1833. }
  1834. }
  1835. if (port->shared->shm_key >= 0) {
  1836. engine->port_buffer_freelist = g_slist_remove (engine->port_buffer_freelist, bi);
  1837. } else {
  1838. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  1839. }
  1840. out:
  1841. pthread_mutex_unlock (&engine->buffer_lock);
  1842. if (port->shared->shm_key < 0) {
  1843. return -1;
  1844. } else {
  1845. return 0;
  1846. }
  1847. }
  1848. static jack_port_internal_t *
  1849. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  1850. {
  1851. jack_port_id_t id;
  1852. /* Note the potential race on "in_use". Other design
  1853. elements prevent this from being a problem.
  1854. */
  1855. for (id = 0; id < engine->port_max; id++) {
  1856. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  1857. return &engine->internal_ports[id];
  1858. }
  1859. }
  1860. return NULL;
  1861. }
  1862. static int
  1863. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  1864. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  1865. {
  1866. jack_client_internal_t *client;
  1867. jack_event_t event;
  1868. if ((client = jack_client_internal_by_id (engine, client_id)) == 0) {
  1869. jack_error ("no such client %d during connection notification", client_id);
  1870. return -1;
  1871. }
  1872. event.type = (connected ? PortConnected : PortDisconnected);
  1873. event.x.self_id = self_id;
  1874. event.y.other_id = other_id;
  1875. if (jack_deliver_event (engine, client, &event)) {
  1876. jack_error ("cannot send port connection notification to client %s (%s)",
  1877. client->control->name, strerror (errno));
  1878. return -1;
  1879. }
  1880. return 0;
  1881. }