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.

1829 lines
43KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #if defined(__APPLE__) && defined(__POWERPC__)
  18. #include "pThreadUtilities.h"
  19. #include "ipc.h"
  20. #include "fakepoll.h"
  21. #else
  22. #include <sys/poll.h>
  23. #endif
  24. #include <sys/socket.h>
  25. #include <sys/un.h>
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/ipc.h>
  31. #include <sys/mman.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <regex.h>
  36. #include <config.h>
  37. #include <jack/jack.h>
  38. #include <jack/internal.h>
  39. #include <jack/engine.h>
  40. #include <jack/pool.h>
  41. #include <jack/time.h>
  42. #include <jack/jslist.h>
  43. #include <jack/version.h>
  44. #include <jack/shm.h>
  45. #include "local.h"
  46. #ifdef WITH_TIMESTAMPS
  47. #include <jack/timestamps.h>
  48. #endif /* WITH_TIMESTAMPS */
  49. #ifdef DEFAULT_TMP_DIR
  50. char *jack_server_dir = DEFAULT_TMP_DIR;
  51. #else
  52. char *jack_server_dir = "/tmp";
  53. #endif
  54. void
  55. jack_set_server_dir (const char *path)
  56. {
  57. jack_error ("jack_set_server_dir() is deprecated.\n"
  58. "Please contact the program's author");
  59. jack_server_dir = strdup (path);
  60. }
  61. static pthread_mutex_t client_lock;
  62. static pthread_cond_t client_ready;
  63. void *jack_zero_filled_buffer = NULL;
  64. #define event_fd pollfd[0].fd
  65. #define graph_wait_fd pollfd[1].fd
  66. typedef struct {
  67. int status;
  68. struct _jack_client *client;
  69. const char *client_name;
  70. } client_info;
  71. void
  72. jack_error (const char *fmt, ...)
  73. {
  74. va_list ap;
  75. char buffer[300];
  76. va_start (ap, fmt);
  77. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  78. jack_error_callback (buffer);
  79. va_end (ap);
  80. }
  81. void
  82. default_jack_error_callback (const char *desc)
  83. {
  84. fprintf(stderr, "%s\n", desc);
  85. }
  86. void
  87. silent_jack_error_callback (const char *desc)
  88. {
  89. }
  90. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  91. static int
  92. oop_client_deliver_request (void *ptr, jack_request_t *req)
  93. {
  94. int wok, rok;
  95. jack_client_t *client = (jack_client_t*) ptr;
  96. wok = (write (client->request_fd, req, sizeof (*req))
  97. == sizeof (*req));
  98. rok = (read (client->request_fd, req, sizeof (*req))
  99. == sizeof (*req));
  100. if (wok && rok) /* everything OK? */
  101. return req->status;
  102. req->status = -1; /* request failed */
  103. /* check for server shutdown */
  104. if (client->engine->engine_ok == 0)
  105. return req->status;
  106. /* otherwise report errors */
  107. if (!wok)
  108. jack_error ("cannot send request type %d to server",
  109. req->type);
  110. if (!rok)
  111. jack_error ("cannot read result for request type %d from"
  112. " server (%s)", req->type, strerror (errno));
  113. return req->status;
  114. }
  115. int
  116. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  117. {
  118. /* indirect through the function pointer that was set
  119. either by jack_client_new() (external) or handle_new_client()
  120. in the server.
  121. */
  122. return client->control->deliver_request (client->control->deliver_arg, req);
  123. }
  124. jack_client_t *
  125. jack_client_alloc ()
  126. {
  127. jack_client_t *client;
  128. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  129. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  130. client->pollmax = 2;
  131. client->request_fd = -1;
  132. client->event_fd = -1;
  133. client->graph_wait_fd = -1;
  134. client->graph_next_fd = -1;
  135. client->ports = NULL;
  136. client->engine = NULL;
  137. client->control = NULL;
  138. client->thread_ok = FALSE;
  139. client->first_active = TRUE;
  140. client->on_shutdown = NULL;
  141. client->n_port_types = 0;
  142. client->port_segment = NULL;
  143. return client;
  144. }
  145. jack_client_t *
  146. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  147. {
  148. jack_client_t* client;
  149. client = jack_client_alloc ();
  150. client->control = cc;
  151. client->engine = engine->control;
  152. client->n_port_types = client->engine->n_port_types;
  153. client->port_segment = &engine->port_segment[0];
  154. return client;
  155. }
  156. static void
  157. jack_client_free (jack_client_t *client)
  158. {
  159. if (client->pollfd) {
  160. free (client->pollfd);
  161. }
  162. free (client);
  163. }
  164. void
  165. jack_client_invalidate_port_buffers (jack_client_t *client)
  166. {
  167. JSList *node;
  168. jack_port_t *port;
  169. /* This releases all local memory owned by input ports
  170. and sets the buffer pointer to NULL. This will cause
  171. jack_port_get_buffer() to reallocate space for the
  172. buffer on the next call (if there is one).
  173. */
  174. for (node = client->ports; node; node = jack_slist_next (node)) {
  175. port = (jack_port_t *) node->data;
  176. if (port->shared->flags & JackPortIsInput) {
  177. if (port->mix_buffer) {
  178. jack_pool_release (port->mix_buffer);
  179. port->mix_buffer = NULL;
  180. }
  181. }
  182. }
  183. }
  184. int
  185. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  186. {
  187. jack_port_t *control_port;
  188. jack_port_t *other;
  189. JSList *node;
  190. switch (event->type) {
  191. case PortConnected:
  192. other = jack_port_new (client, event->y.other_id, client->engine);
  193. control_port = jack_port_by_id (client, event->x.self_id);
  194. pthread_mutex_lock (&control_port->connection_lock);
  195. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  196. pthread_mutex_unlock (&control_port->connection_lock);
  197. break;
  198. case PortDisconnected:
  199. control_port = jack_port_by_id (client, event->x.self_id);
  200. pthread_mutex_lock (&control_port->connection_lock);
  201. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  202. other = (jack_port_t *) node->data;
  203. if (other->shared->id == event->y.other_id) {
  204. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  205. jack_slist_free_1 (node);
  206. free (other);
  207. break;
  208. }
  209. }
  210. pthread_mutex_unlock (&control_port->connection_lock);
  211. break;
  212. default:
  213. /* impossible */
  214. break;
  215. }
  216. return 0;
  217. }
  218. static int
  219. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  220. {
  221. char path[PATH_MAX+1];
  222. if (client->graph_wait_fd >= 0) {
  223. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  224. close (client->graph_wait_fd);
  225. client->graph_wait_fd = -1;
  226. }
  227. if (client->graph_next_fd >= 0) {
  228. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  229. close (client->graph_next_fd);
  230. client->graph_next_fd = -1;
  231. }
  232. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  233. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  234. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  235. return -1;
  236. }
  237. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  238. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  239. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  240. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  241. return -1;
  242. }
  243. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  244. /* If the client registered its own callback for graph order events,
  245. execute it now.
  246. */
  247. if (client->control->graph_order) {
  248. client->control->graph_order (client->control->graph_order_arg);
  249. }
  250. return 0;
  251. }
  252. static int
  253. server_connect (int which)
  254. {
  255. int fd;
  256. struct sockaddr_un addr;
  257. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  258. jack_error ("cannot create client socket (%s)",
  259. strerror (errno));
  260. return -1;
  261. }
  262. addr.sun_family = AF_UNIX;
  263. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  264. jack_server_dir, which);
  265. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  266. close (fd);
  267. return -1;
  268. }
  269. return fd;
  270. }
  271. static int
  272. server_event_connect (jack_client_t *client)
  273. {
  274. int fd;
  275. struct sockaddr_un addr;
  276. jack_client_connect_ack_request_t req;
  277. jack_client_connect_ack_result_t res;
  278. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  279. jack_error ("cannot create client event socket (%s)",
  280. strerror (errno));
  281. return -1;
  282. }
  283. addr.sun_family = AF_UNIX;
  284. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  285. jack_server_dir);
  286. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  287. jack_error ("cannot connect to jack server for events",
  288. strerror (errno));
  289. close (fd);
  290. return -1;
  291. }
  292. req.client_id = client->control->id;
  293. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  294. jack_error ("cannot write event connect request to server (%s)",
  295. strerror (errno));
  296. close (fd);
  297. return -1;
  298. }
  299. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  300. jack_error ("cannot read event connect result from server (%s)",
  301. strerror (errno));
  302. close (fd);
  303. return -1;
  304. }
  305. if (res.status != 0) {
  306. jack_error ("cannot connect to server for event stream (%s)",
  307. strerror (errno));
  308. close (fd);
  309. return -1;
  310. }
  311. return fd;
  312. }
  313. static int
  314. jack_request_client (ClientType type, const char* client_name, const char* so_name,
  315. const char* so_data, jack_client_connect_result_t *res, int *req_fd)
  316. {
  317. jack_client_connect_request_t req;
  318. *req_fd = -1;
  319. memset (&req, 0, sizeof (req));
  320. if (strlen (client_name) > sizeof (req.name) - 1) {
  321. jack_error ("\"%s\" is too long to be used as a JACK client"
  322. " name.\n"
  323. "Please use %lu characters or less.",
  324. client_name, sizeof (req.name) - 1);
  325. return -1;
  326. }
  327. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  328. jack_error ("\"%s\" is too long to be used as a JACK shared"
  329. " object name.\n"
  330. "Please use %lu characters or less.",
  331. so_name, sizeof (req.object_path) - 1);
  332. return -1;
  333. }
  334. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  335. jack_error ("\"%s\" is too long to be used as a JACK shared"
  336. " object data string.\n"
  337. "Please use %lu characters or less.",
  338. so_data, sizeof (req.object_data) - 1);
  339. return -1;
  340. }
  341. if ((*req_fd = server_connect (0)) < 0) {
  342. goto fail;
  343. }
  344. req.load = TRUE;
  345. req.type = type;
  346. snprintf (req.name, sizeof (req.name), "%s", client_name);
  347. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  348. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  349. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  350. jack_error ("cannot send request to jack server (%s)",
  351. strerror (errno));
  352. goto fail;
  353. }
  354. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  355. if (errno == 0) {
  356. /* server shut the socket */
  357. jack_error ("could not attach as client "
  358. "(duplicate client name?)");
  359. goto fail;
  360. }
  361. jack_error ("cannot read response from jack server (%s)",
  362. strerror (errno));
  363. goto fail;
  364. }
  365. if (res->status) {
  366. jack_error ("could not attach as client "
  367. "(duplicate client name?)");
  368. goto fail;
  369. }
  370. if (res->protocol_v != jack_protocol_version){
  371. jack_error ("application linked against incompatible libjack"
  372. " version.");
  373. goto fail;
  374. }
  375. switch (type) {
  376. case ClientDriver:
  377. case ClientInternal:
  378. close (*req_fd);
  379. *req_fd = -1;
  380. break;
  381. default:
  382. break;
  383. }
  384. return 0;
  385. fail:
  386. if (*req_fd >= 0) {
  387. close (*req_fd);
  388. *req_fd = -1;
  389. }
  390. return -1;
  391. }
  392. int
  393. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  394. {
  395. /* Lookup, attach and register the port/buffer segments in use
  396. * right now.
  397. */
  398. if (client->control->type != ClientExternal) {
  399. jack_error("Only external clients need attach port segments");
  400. abort();
  401. }
  402. /* make sure we have space to store the port
  403. segment information.
  404. */
  405. if (ptid >= client->n_port_types) {
  406. client->port_segment = (jack_shm_info_t*)
  407. realloc (client->port_segment,
  408. sizeof (jack_shm_info_t) * (ptid+1));
  409. memset (&client->port_segment[client->n_port_types],
  410. 0,
  411. sizeof (jack_shm_info_t) *
  412. (ptid - client->n_port_types));
  413. client->n_port_types = ptid + 1;
  414. } else {
  415. /* release any previous segment */
  416. jack_release_shm (&client->port_segment[ptid]);
  417. }
  418. /* get the index into the shm registry */
  419. client->port_segment[ptid].index =
  420. client->engine->port_types[ptid].shm_registry_index;
  421. /* attach the relevant segment */
  422. if (jack_attach_shm (&client->port_segment[ptid])) {
  423. jack_error ("cannot attach port segment shared memory"
  424. " (%s)", strerror (errno));
  425. return -1;
  426. }
  427. /* The first chunk of the audio port segment will be set by
  428. * the engine to be a zero-filled buffer. This hasn't been
  429. * done yet, but it will happen before the process cycle
  430. * (re)starts.
  431. */
  432. if (ptid == JACK_AUDIO_PORT_TYPE) {
  433. jack_zero_filled_buffer =
  434. jack_shm_addr (&client->port_segment[ptid]);
  435. }
  436. return 0;
  437. }
  438. jack_client_t *
  439. jack_client_new (const char *client_name)
  440. {
  441. int req_fd = -1;
  442. int ev_fd = -1;
  443. jack_client_connect_result_t res;
  444. jack_client_t *client;
  445. jack_port_type_id_t ptid;
  446. /* external clients need this initialized; internal clients
  447. will use the setup in the server's address space.
  448. */
  449. jack_init_time ();
  450. jack_initialize_shm ();
  451. if (jack_request_client (ClientExternal, client_name, "", "",
  452. &res, &req_fd)) {
  453. return NULL;
  454. }
  455. client = jack_client_alloc ();
  456. strcpy (client->fifo_prefix, res.fifo_prefix);
  457. client->request_fd = req_fd;
  458. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  459. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  460. /* attach the engine control/info block */
  461. client->engine_shm = res.engine_shm;
  462. if (jack_attach_shm (&client->engine_shm)) {
  463. jack_error ("cannot attached engine control shared memory"
  464. " segment");
  465. goto fail;
  466. }
  467. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  468. /* now attach the client control block */
  469. client->control_shm = res.client_shm;
  470. if (jack_attach_shm (&client->control_shm)) {
  471. jack_error ("cannot attached client control shared memory"
  472. " segment");
  473. goto fail;
  474. }
  475. client->control = (jack_client_control_t *)
  476. jack_shm_addr (&client->control_shm);
  477. /* nobody else needs to access this shared memory any more, so
  478. destroy it. because we have our own attachment to it, it won't
  479. vanish till we exit (and release it).
  480. */
  481. jack_destroy_shm (&client->control_shm);
  482. client->n_port_types = client->engine->n_port_types;
  483. client->port_segment = (jack_shm_info_t *)
  484. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  485. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  486. client->port_segment[ptid].index =
  487. client->engine->port_types[ptid].shm_registry_index;
  488. jack_attach_port_segment (client, ptid);
  489. }
  490. /* set up the client so that it does the right thing for an
  491. * external client
  492. */
  493. client->control->deliver_request = oop_client_deliver_request;
  494. client->control->deliver_arg = client;
  495. if ((ev_fd = server_event_connect (client)) < 0) {
  496. goto fail;
  497. }
  498. client->event_fd = ev_fd;
  499. #if defined(__APPLE__) && defined(__POWERPC__)
  500. /* specific resources for server/client real-time thread
  501. * communication */
  502. client->clienttask = mach_task_self();
  503. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  504. jack_error ("Can't find bootstrap port");
  505. goto fail;
  506. }
  507. if (allocate_mach_clientport(client, res.portnum) < 0) {
  508. jack_error("Can't allocate mach port");
  509. goto fail;
  510. };
  511. #endif
  512. return client;
  513. fail:
  514. if (client->engine) {
  515. jack_release_shm (&client->engine_shm);
  516. client->engine = 0;
  517. }
  518. if (client->control) {
  519. jack_release_shm (&client->control_shm);
  520. client->control = 0;
  521. }
  522. if (req_fd >= 0) {
  523. close (req_fd);
  524. }
  525. if (ev_fd >= 0) {
  526. close (ev_fd);
  527. }
  528. return 0;
  529. }
  530. int
  531. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  532. {
  533. jack_client_connect_result_t res;
  534. int req_fd;
  535. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  536. }
  537. void
  538. jack_internal_client_close (const char *client_name)
  539. {
  540. jack_client_connect_request_t req;
  541. int fd;
  542. req.load = FALSE;
  543. snprintf (req.name, sizeof (req.name), "%s", client_name);
  544. if ((fd = server_connect (0)) < 0) {
  545. return;
  546. }
  547. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  548. jack_error ("cannot deliver ClientUnload request to JACK server.");
  549. }
  550. /* no response to this request */
  551. close (fd);
  552. return;
  553. }
  554. int
  555. jack_drop_real_time_scheduling (pthread_t thread)
  556. {
  557. struct sched_param rtparam;
  558. int x;
  559. memset (&rtparam, 0, sizeof (rtparam));
  560. rtparam.sched_priority = 0;
  561. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  562. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  563. return -1;
  564. }
  565. return 0;
  566. }
  567. int
  568. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  569. {
  570. struct sched_param rtparam;
  571. int x;
  572. memset (&rtparam, 0, sizeof (rtparam));
  573. rtparam.sched_priority = priority;
  574. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  575. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  576. "(%d: %s)", rtparam.sched_priority, x,
  577. strerror (x));
  578. return -1;
  579. }
  580. return 0;
  581. }
  582. int
  583. jack_set_freewheel (jack_client_t* client, int onoff)
  584. {
  585. jack_request_t request;
  586. request.type = onoff ? FreeWheel : StopFreeWheel;
  587. return jack_client_deliver_request (client, &request);
  588. }
  589. void
  590. jack_start_freewheel (jack_client_t* client)
  591. {
  592. jack_client_control_t *control = client->control;
  593. if (client->engine->real_time) {
  594. jack_drop_real_time_scheduling (client->thread);
  595. }
  596. if (control->freewheel_cb) {
  597. control->freewheel_cb (1, control->freewheel_arg);
  598. }
  599. }
  600. void
  601. jack_stop_freewheel (jack_client_t* client)
  602. {
  603. jack_client_control_t *control = client->control;
  604. if (control->freewheel_cb) {
  605. control->freewheel_cb (0, control->freewheel_arg);
  606. }
  607. if (client->engine->real_time) {
  608. jack_acquire_real_time_scheduling (client->thread,
  609. client->engine->client_priority);
  610. }
  611. }
  612. static void *
  613. jack_client_thread (void *arg)
  614. {
  615. jack_client_t *client = (jack_client_t *) arg;
  616. jack_client_control_t *control = client->control;
  617. jack_event_t event;
  618. char status = 0;
  619. char c;
  620. int err = 0;
  621. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  622. pthread_mutex_lock (&client_lock);
  623. client->thread_ok = TRUE;
  624. client->thread_id = pthread_self();
  625. pthread_cond_signal (&client_ready);
  626. pthread_mutex_unlock (&client_lock);
  627. client->control->pid = getpid();
  628. client->control->pgrp = getpgrp();
  629. DEBUG ("client thread is now running");
  630. while (err == 0) {
  631. if (client->engine->engine_ok == 0) {
  632. if (client->on_shutdown)
  633. client->on_shutdown (client->on_shutdown_arg);
  634. else
  635. jack_error ("engine unexpectedly shutdown; "
  636. "thread exiting\n");
  637. pthread_exit (0);
  638. }
  639. DEBUG ("client polling on event_fd and graph_wait_fd...");
  640. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  641. if (errno == EINTR) {
  642. continue;
  643. }
  644. jack_error ("poll failed in client (%s)",
  645. strerror (errno));
  646. status = -1;
  647. break;
  648. }
  649. /* get an accurate timestamp on waking from poll for a
  650. * process() cycle.
  651. */
  652. if (client->pollfd[1].revents & POLLIN) {
  653. control->awake_at = jack_get_microseconds();
  654. }
  655. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  656. client->pollfd[0].revents,
  657. client->pollfd[1].revents);
  658. pthread_testcancel();
  659. if ((client->pollfd[0].revents & ~POLLIN) ||
  660. client->control->dead) {
  661. goto zombie;
  662. }
  663. if (client->pollfd[0].revents & POLLIN) {
  664. DEBUG ("client receives an event, "
  665. "now reading on event fd");
  666. /* server has sent us an event. process the
  667. * event and reply */
  668. if (read (client->event_fd, &event, sizeof (event))
  669. != sizeof (event)) {
  670. jack_error ("cannot read server event (%s)",
  671. strerror (errno));
  672. err++;
  673. break;
  674. }
  675. status = 0;
  676. switch (event.type) {
  677. case PortRegistered:
  678. if (control->port_register) {
  679. control->port_register
  680. (event.x.port_id, TRUE,
  681. control->port_register_arg);
  682. }
  683. break;
  684. case PortUnregistered:
  685. if (control->port_register) {
  686. control->port_register
  687. (event.x.port_id, FALSE,
  688. control->port_register_arg);
  689. }
  690. break;
  691. case GraphReordered:
  692. status = jack_handle_reorder (client, &event);
  693. break;
  694. case PortConnected:
  695. case PortDisconnected:
  696. status = jack_client_handle_port_connection
  697. (client, &event);
  698. break;
  699. case BufferSizeChange:
  700. jack_client_invalidate_port_buffers (client);
  701. if (control->bufsize) {
  702. status = control->bufsize
  703. (control->nframes,
  704. control->bufsize_arg);
  705. }
  706. break;
  707. case SampleRateChange:
  708. if (control->srate) {
  709. status = control->srate
  710. (control->nframes,
  711. control->srate_arg);
  712. }
  713. break;
  714. case XRun:
  715. if (control->xrun) {
  716. status = control->xrun
  717. (control->xrun_arg);
  718. }
  719. break;
  720. case AttachPortSegment:
  721. jack_attach_port_segment (client, event.y.ptid);
  722. break;
  723. case StartFreewheel:
  724. jack_start_freewheel (client);
  725. break;
  726. case StopFreewheel:
  727. jack_stop_freewheel (client);
  728. break;
  729. }
  730. DEBUG ("client has dealt with the event, writing "
  731. "response on event fd");
  732. if (write (client->event_fd, &status, sizeof (status))
  733. != sizeof (status)) {
  734. jack_error ("cannot send event response to "
  735. "engine (%s)", strerror (errno));
  736. err++;
  737. break;
  738. }
  739. }
  740. if (client->pollfd[1].revents & ~POLLIN) {
  741. goto zombie;
  742. }
  743. if (client->pollfd[1].revents & POLLIN) {
  744. #ifdef WITH_TIMESTAMPS
  745. jack_reset_timestamps ();
  746. #endif
  747. DEBUG ("client %d signalled at %" PRIu64
  748. ", awake for process at %" PRIu64
  749. " (delay = %" PRIu64
  750. " usecs) (wakeup on graph_wait_fd==%d)",
  751. getpid(),
  752. control->signalled_at,
  753. control->awake_at,
  754. control->awake_at - control->signalled_at,
  755. client->pollfd[1].fd);
  756. control->state = Running;
  757. if (control->sync_cb)
  758. jack_call_sync_client (client);
  759. if (control->process) {
  760. if (control->process (control->nframes,
  761. control->process_arg)
  762. == 0) {
  763. control->state = Finished;
  764. }
  765. } else {
  766. control->state = Finished;
  767. }
  768. if (control->timebase_cb)
  769. jack_call_timebase_master (client);
  770. control->finished_at = jack_get_microseconds();
  771. #ifdef WITH_TIMESTAMPS
  772. jack_timestamp ("finished");
  773. #endif
  774. /* pass the execution token along */
  775. DEBUG ("client finished processing at %" PRIu64
  776. " (elapsed = %" PRIu64
  777. " usecs), writing on graph_next_fd==%d",
  778. control->finished_at,
  779. control->finished_at - control->awake_at,
  780. client->graph_next_fd);
  781. if (write (client->graph_next_fd, &c, sizeof (c))
  782. != sizeof (c)) {
  783. jack_error ("cannot continue execution of the "
  784. "processing graph (%s)",
  785. strerror(errno));
  786. err++;
  787. break;
  788. }
  789. DEBUG ("client sent message to next stage by %" PRIu64
  790. ", client reading on graph_wait_fd==%d",
  791. jack_get_microseconds(), client->graph_wait_fd);
  792. #ifdef WITH_TIMESTAMPS
  793. jack_timestamp ("read pending byte from wait");
  794. #endif
  795. DEBUG("reading cleanup byte from pipe\n");
  796. if ((read (client->graph_wait_fd, &c, sizeof (c))
  797. != sizeof (c))) {
  798. DEBUG ("WARNING: READ FAILED!");
  799. #if 0
  800. jack_error ("cannot complete execution of the "
  801. "processing graph (%s)",
  802. strerror(errno));
  803. err++;
  804. break;
  805. #endif
  806. }
  807. /* check if we were killed during the process
  808. * cycle (or whatever) */
  809. if (client->control->dead) {
  810. goto zombie;
  811. }
  812. DEBUG("process cycle fully complete\n");
  813. #ifdef WITH_TIMESTAMPS
  814. jack_timestamp ("read done");
  815. jack_dump_timestamps (stdout);
  816. #endif
  817. }
  818. }
  819. return (void *) ((intptr_t)err);
  820. zombie:
  821. if (client->on_shutdown) {
  822. jack_error ("zombified - calling shutdown handler");
  823. client->on_shutdown (client->on_shutdown_arg);
  824. } else {
  825. jack_error ("zombified - exiting from JACK");
  826. jack_client_close (client);
  827. /* Need a fix : possibly make client crash if
  828. * zombified without shutdown handler
  829. */
  830. }
  831. pthread_exit (0);
  832. /*NOTREACHED*/
  833. return 0;
  834. }
  835. #if defined(__APPLE__) && defined(__POWERPC__)
  836. /* real-time thread : separated from the normal client thread, it will
  837. * communicate with the server using fast mach RPC mechanism */
  838. static void *
  839. jack_client_process_thread (void *arg)
  840. {
  841. jack_client_t *client = (jack_client_t *) arg;
  842. jack_client_control_t *control = client->control;
  843. int err = 0;
  844. client->control->pid = getpid();
  845. DEBUG ("client process thread is now running");
  846. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  847. while (err == 0) {
  848. if (jack_client_suspend(client) < 0) {
  849. pthread_exit (0);
  850. }
  851. control->awake_at = jack_get_microseconds();
  852. DEBUG ("client resumed");
  853. control->state = Running;
  854. if (control->process) {
  855. if (control->process (control->nframes,
  856. control->process_arg) == 0) {
  857. control->state = Finished;
  858. }
  859. } else {
  860. control->state = Finished;
  861. }
  862. control->finished_at = jack_get_microseconds();
  863. #ifdef WITH_TIMESTAMPS
  864. jack_timestamp ("finished");
  865. #endif
  866. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  867. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  868. /* check if we were killed during the process cycle (or whatever) */
  869. if (client->control->dead) {
  870. jack_error ("jack_client_process_thread : client->control->dead");
  871. goto zombie;
  872. }
  873. DEBUG("process cycle fully complete\n");
  874. }
  875. return (void *) ((intptr_t)err);
  876. zombie:
  877. jack_error ("jack_client_process_thread : zombified");
  878. if (client->on_shutdown) {
  879. jack_error ("zombified - calling shutdown handler");
  880. client->on_shutdown (client->on_shutdown_arg);
  881. } else {
  882. jack_error ("zombified - exiting from JACK");
  883. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  884. }
  885. pthread_exit (0);
  886. /*NOTREACHED*/
  887. return 0;
  888. }
  889. #endif
  890. static int
  891. jack_start_thread (jack_client_t *client)
  892. {
  893. pthread_attr_t *attributes = 0;
  894. #ifdef USE_CAPABILITIES
  895. int policy = SCHED_OTHER;
  896. struct sched_param client_param, temp_param;
  897. #endif
  898. if (client->engine->real_time) {
  899. /* Get the client thread to run as an RT-FIFO
  900. scheduled thread of appropriate priority.
  901. */
  902. struct sched_param rt_param;
  903. attributes = (pthread_attr_t *)
  904. malloc (sizeof (pthread_attr_t));
  905. pthread_attr_init (attributes);
  906. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  907. jack_error ("cannot set FIFO scheduling class for RT "
  908. "thread");
  909. return -1;
  910. }
  911. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  912. jack_error ("Cannot set scheduling scope for RT "
  913. "thread");
  914. return -1;
  915. }
  916. memset (&rt_param, 0, sizeof (rt_param));
  917. rt_param.sched_priority = client->engine->client_priority;
  918. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  919. jack_error ("Cannot set scheduling priority for RT "
  920. "thread (%s)", strerror (errno));
  921. return -1;
  922. }
  923. #if defined(__APPLE__) && defined(__POWERPC__)
  924. // To be implemented
  925. #else
  926. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  927. jack_error ("cannot lock down memory for RT thread (%s)",
  928. strerror (errno));
  929. #ifdef ENSURE_MLOCK
  930. return -1;
  931. #endif /* ENSURE_MLOCK */
  932. }
  933. #endif
  934. }
  935. if (pthread_create (&client->thread, attributes,
  936. jack_client_thread, client)) {
  937. #ifdef USE_CAPABILITIES
  938. if (client->engine->real_time) {
  939. /* we are probably dealing with a broken glibc so try
  940. to work around the bug, see below for more details
  941. */
  942. goto capabilities_workaround;
  943. }
  944. #endif
  945. return -1;
  946. }
  947. #if defined(__APPLE__) && defined(__POWERPC__)
  948. /* a spcial real-time thread to call the "process"
  949. * callback. It will communicate with the server using fast
  950. * mach RPC mechanism */
  951. if (pthread_create (&client->process_thread, attributes,
  952. jack_client_process_thread, client)) {
  953. jack_error("pthread_create failed for process_thread \n");
  954. return -1;
  955. }
  956. if (client->engine->real_time){
  957. /* time constraint thread */
  958. setThreadToPriority(client->process_thread, 96, true, 10000000);
  959. }else{
  960. /* fixed priority thread */
  961. setThreadToPriority(client->process_thread, 63, true, 10000000);
  962. }
  963. #endif
  964. return 0;
  965. #ifdef USE_CAPABILITIES
  966. /* we get here only with engine running realtime and capabilities */
  967. capabilities_workaround:
  968. /* the version of glibc I've played with has a bug that makes
  969. that code fail when running under a non-root user but with the
  970. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  971. does not check for capabilities, only for the uid being
  972. zero). Newer versions apparently have this fixed. This
  973. workaround temporarily switches the client thread to the
  974. proper scheduler and priority, then starts the realtime
  975. thread so that it can inherit them and finally switches the
  976. client thread back to what it was before. Sigh. For ardour
  977. I have to check again and switch the thread explicitly to
  978. realtime, don't know why or how to debug - nando
  979. */
  980. /* get current scheduler and parameters of the client process */
  981. if ((policy = sched_getscheduler (0)) < 0) {
  982. jack_error ("Cannot get current client scheduler: %s",
  983. strerror(errno));
  984. return -1;
  985. }
  986. memset (&client_param, 0, sizeof (client_param));
  987. if (sched_getparam (0, &client_param)) {
  988. jack_error ("Cannot get current client scheduler "
  989. "parameters: %s", strerror(errno));
  990. return -1;
  991. }
  992. /* temporarily change the client process to SCHED_FIFO so that
  993. the realtime thread can inherit the scheduler and priority
  994. */
  995. memset (&temp_param, 0, sizeof (temp_param));
  996. temp_param.sched_priority = client->engine->client_priority;
  997. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  998. jack_error ("Cannot temporarily set client to RT scheduler:"
  999. " %s", strerror(errno));
  1000. return -1;
  1001. }
  1002. /* prepare the attributes for the realtime thread */
  1003. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1004. pthread_attr_init (attributes);
  1005. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1006. sched_setscheduler (0, policy, &client_param);
  1007. jack_error ("Cannot set scheduling scope for RT thread");
  1008. return -1;
  1009. }
  1010. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1011. sched_setscheduler (0, policy, &client_param);
  1012. jack_error ("Cannot set scheduler inherit policy for RT "
  1013. "thread");
  1014. return -1;
  1015. }
  1016. /* create the RT thread */
  1017. if (pthread_create (&client->thread, attributes,
  1018. jack_client_thread, client)) {
  1019. sched_setscheduler (0, policy, &client_param);
  1020. return -1;
  1021. }
  1022. /* return the client process to the scheduler it was in before */
  1023. if (sched_setscheduler (0, policy, &client_param)) {
  1024. jack_error ("Cannot reset original client scheduler: %s",
  1025. strerror(errno));
  1026. return -1;
  1027. }
  1028. /* check again... inheritance of policy and priority works in
  1029. jack_simple_client but not in ardour! So I check again and
  1030. force the policy if it is not set correctly. This does not
  1031. really really work either, the manager thread of the
  1032. linuxthreads implementation is left running with
  1033. SCHED_OTHER, that is presumably very bad.
  1034. */
  1035. memset (&client_param, 0, sizeof (client_param));
  1036. if (pthread_getschedparam(client->thread, &policy,
  1037. &client_param) == 0) {
  1038. if (policy != SCHED_FIFO) {
  1039. memset (&client_param, 0, sizeof (client_param));
  1040. client_param.sched_priority =
  1041. client->engine->client_priority;
  1042. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1043. &client_param)) {
  1044. jack_error ("Cannot set (again) FIFO scheduling"
  1045. " class for RT thread\n");
  1046. return -1;
  1047. }
  1048. }
  1049. }
  1050. return 0;
  1051. #endif
  1052. }
  1053. int
  1054. jack_activate (jack_client_t *client)
  1055. {
  1056. jack_request_t req;
  1057. /* we need to scribble on our stack to ensure that its memory
  1058. * pages are actually mapped (more important for mlockall(2)
  1059. * usage in jack_start_thread())
  1060. */
  1061. #if defined(__APPLE__) && defined(__POWERPC__)
  1062. /* a bigger stack makes the application crash... */
  1063. #define BIG_ENOUGH_STACK 10000
  1064. #else
  1065. #define BIG_ENOUGH_STACK 1048576
  1066. #endif
  1067. char buf[BIG_ENOUGH_STACK];
  1068. int i;
  1069. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  1070. buf[i] = (char) (i & 0xff);
  1071. }
  1072. #undef BIG_ENOUGH_STACK
  1073. if (client->control->type == ClientInternal ||
  1074. client->control->type == ClientDriver) {
  1075. goto startit;
  1076. }
  1077. /* get the pid of the client process to pass it to engine */
  1078. client->control->pid = getpid ();
  1079. #ifdef USE_CAPABILITIES
  1080. if (client->engine->has_capabilities != 0 &&
  1081. client->control->pid != 0 && client->engine->real_time != 0) {
  1082. /* we need to ask the engine for realtime capabilities
  1083. before trying to start the realtime thread
  1084. */
  1085. req.type = SetClientCapabilities;
  1086. req.x.client_id = client->control->id;
  1087. jack_client_deliver_request (client, &req);
  1088. if (req.status) {
  1089. /* what to do? engine is running realtime, it
  1090. is using capabilities and has them
  1091. (otherwise we would not get an error
  1092. return) but for some reason it could not
  1093. give the client the required capabilities,
  1094. so for now downgrade the client so that it
  1095. still runs, albeit non-realtime - nando
  1096. */
  1097. jack_error ("could not receive realtime capabilities, "
  1098. "client will run non-realtime");
  1099. /* XXX wrong, this is a property of the engine
  1100. client->engine->real_time = 0;
  1101. */
  1102. }
  1103. }
  1104. #endif
  1105. if (client->first_active) {
  1106. pthread_mutex_init (&client_lock, NULL);
  1107. pthread_cond_init (&client_ready, NULL);
  1108. pthread_mutex_lock (&client_lock);
  1109. if (jack_start_thread (client)) {
  1110. pthread_mutex_unlock (&client_lock);
  1111. return -1;
  1112. }
  1113. pthread_cond_wait (&client_ready, &client_lock);
  1114. pthread_mutex_unlock (&client_lock);
  1115. if (!client->thread_ok) {
  1116. jack_error ("could not start client thread");
  1117. return -1;
  1118. }
  1119. client->first_active = FALSE;
  1120. }
  1121. startit:
  1122. req.type = ActivateClient;
  1123. req.x.client_id = client->control->id;
  1124. return jack_client_deliver_request (client, &req);
  1125. }
  1126. int
  1127. jack_deactivate (jack_client_t *client)
  1128. {
  1129. jack_request_t req;
  1130. req.type = DeactivateClient;
  1131. req.x.client_id = client->control->id;
  1132. return jack_client_deliver_request (client, &req);
  1133. }
  1134. int
  1135. jack_client_close (jack_client_t *client)
  1136. {
  1137. JSList *node;
  1138. void *status;
  1139. if (client->control->active) {
  1140. jack_deactivate (client);
  1141. }
  1142. if (client->control->type == ClientExternal) {
  1143. /* stop the thread that communicates with the jack
  1144. * server, only if it was actually running
  1145. */
  1146. if (client->thread_ok){
  1147. pthread_cancel (client->thread);
  1148. pthread_join (client->thread, &status);
  1149. }
  1150. if (client->control) {
  1151. jack_release_shm (&client->control_shm);
  1152. client->control = NULL;
  1153. }
  1154. if (client->engine) {
  1155. jack_release_shm (&client->engine_shm);
  1156. client->engine = NULL;
  1157. }
  1158. if (client->port_segment) {
  1159. jack_port_type_id_t ptid;
  1160. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1161. jack_release_shm (&client->port_segment[ptid]);
  1162. }
  1163. free (client->port_segment);
  1164. client->port_segment = NULL;
  1165. }
  1166. if (client->graph_wait_fd) {
  1167. close (client->graph_wait_fd);
  1168. }
  1169. if (client->graph_next_fd) {
  1170. close (client->graph_next_fd);
  1171. }
  1172. close (client->event_fd);
  1173. close (client->request_fd);
  1174. }
  1175. for (node = client->ports; node; node = jack_slist_next (node)) {
  1176. free (node->data);
  1177. }
  1178. jack_slist_free (client->ports);
  1179. jack_client_free (client);
  1180. return 0;
  1181. }
  1182. int
  1183. jack_is_realtime (jack_client_t *client)
  1184. {
  1185. return client->engine->real_time;
  1186. }
  1187. jack_nframes_t
  1188. jack_get_buffer_size (jack_client_t *client)
  1189. {
  1190. return client->engine->buffer_size;
  1191. }
  1192. int
  1193. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1194. {
  1195. #ifdef DO_BUFFER_RESIZE
  1196. jack_request_t req;
  1197. req.type = SetBufferSize;
  1198. req.x.nframes = nframes;
  1199. return jack_client_deliver_request (client, &req);
  1200. #else
  1201. return ENOSYS;
  1202. #endif /* DO_BUFFER_RESIZE */
  1203. }
  1204. int
  1205. jack_connect (jack_client_t *client, const char *source_port,
  1206. const char *destination_port)
  1207. {
  1208. jack_request_t req;
  1209. req.type = ConnectPorts;
  1210. snprintf (req.x.connect.source_port,
  1211. sizeof (req.x.connect.source_port), "%s", source_port);
  1212. snprintf (req.x.connect.destination_port,
  1213. sizeof (req.x.connect.destination_port),
  1214. "%s", destination_port);
  1215. return jack_client_deliver_request (client, &req);
  1216. }
  1217. int
  1218. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1219. {
  1220. jack_request_t req;
  1221. pthread_mutex_lock (&port->connection_lock);
  1222. if (port->connections == NULL) {
  1223. pthread_mutex_unlock (&port->connection_lock);
  1224. return 0;
  1225. }
  1226. pthread_mutex_unlock (&port->connection_lock);
  1227. req.type = DisconnectPort;
  1228. req.x.port_info.port_id = port->shared->id;
  1229. return jack_client_deliver_request (client, &req);
  1230. }
  1231. int
  1232. jack_disconnect (jack_client_t *client, const char *source_port,
  1233. const char *destination_port)
  1234. {
  1235. jack_request_t req;
  1236. req.type = DisconnectPorts;
  1237. snprintf (req.x.connect.source_port,
  1238. sizeof (req.x.connect.source_port), "%s", source_port);
  1239. snprintf (req.x.connect.destination_port,
  1240. sizeof (req.x.connect.destination_port),
  1241. "%s", destination_port);
  1242. return jack_client_deliver_request (client, &req);
  1243. }
  1244. void
  1245. jack_set_error_function (void (*func) (const char *))
  1246. {
  1247. jack_error_callback = func;
  1248. }
  1249. int
  1250. jack_set_graph_order_callback (jack_client_t *client,
  1251. JackGraphOrderCallback callback, void *arg)
  1252. {
  1253. if (client->control->active) {
  1254. jack_error ("You cannot set callbacks on an active client.");
  1255. return -1;
  1256. }
  1257. client->control->graph_order = callback;
  1258. client->control->graph_order_arg = arg;
  1259. return 0;
  1260. }
  1261. int jack_set_xrun_callback (jack_client_t *client,
  1262. JackXRunCallback callback, void *arg)
  1263. {
  1264. if (client->control->active) {
  1265. jack_error ("You cannot set callbacks on an active client.");
  1266. return -1;
  1267. }
  1268. client->control->xrun = callback;
  1269. client->control->xrun_arg = arg;
  1270. return 0;
  1271. }
  1272. int
  1273. jack_set_process_callback (jack_client_t *client,
  1274. JackProcessCallback callback, void *arg)
  1275. {
  1276. if (client->control->active) {
  1277. jack_error ("You cannot set callbacks on an active client.");
  1278. return -1;
  1279. }
  1280. client->control->process_arg = arg;
  1281. client->control->process = callback;
  1282. return 0;
  1283. }
  1284. int
  1285. jack_set_freewheel_callback (jack_client_t *client,
  1286. JackFreewheelCallback callback, void *arg)
  1287. {
  1288. if (client->control->active) {
  1289. jack_error ("You cannot set callbacks on an active client.");
  1290. return -1;
  1291. }
  1292. client->control->freewheel_arg = arg;
  1293. client->control->freewheel_cb = callback;
  1294. return 0;
  1295. }
  1296. int
  1297. jack_set_buffer_size_callback (jack_client_t *client,
  1298. JackBufferSizeCallback callback, void *arg)
  1299. {
  1300. client->control->bufsize_arg = arg;
  1301. client->control->bufsize = callback;
  1302. return 0;
  1303. }
  1304. int
  1305. jack_set_port_registration_callback(jack_client_t *client,
  1306. JackPortRegistrationCallback callback,
  1307. void *arg)
  1308. {
  1309. if (client->control->active) {
  1310. jack_error ("You cannot set callbacks on an active client.");
  1311. return -1;
  1312. }
  1313. client->control->port_register_arg = arg;
  1314. client->control->port_register = callback;
  1315. return 0;
  1316. }
  1317. int
  1318. jack_get_process_start_fd (jack_client_t *client)
  1319. {
  1320. /* once this has been called, the client thread
  1321. does not sleep on the graph wait fd.
  1322. */
  1323. client->pollmax = 1;
  1324. return client->graph_wait_fd;
  1325. }
  1326. int
  1327. jack_get_process_done_fd (jack_client_t *client)
  1328. {
  1329. return client->graph_next_fd;
  1330. }
  1331. void
  1332. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1333. {
  1334. client->on_shutdown = function;
  1335. client->on_shutdown_arg = arg;
  1336. }
  1337. const char **
  1338. jack_get_ports (jack_client_t *client,
  1339. const char *port_name_pattern,
  1340. const char *type_name_pattern,
  1341. unsigned long flags)
  1342. {
  1343. jack_control_t *engine;
  1344. const char **matching_ports;
  1345. unsigned long match_cnt;
  1346. jack_port_shared_t *psp;
  1347. unsigned long i;
  1348. regex_t port_regex;
  1349. regex_t type_regex;
  1350. int matching;
  1351. engine = client->engine;
  1352. if (port_name_pattern && port_name_pattern[0]) {
  1353. regcomp (&port_regex, port_name_pattern,
  1354. REG_EXTENDED|REG_NOSUB);
  1355. }
  1356. if (type_name_pattern && type_name_pattern[0]) {
  1357. regcomp (&type_regex, type_name_pattern,
  1358. REG_EXTENDED|REG_NOSUB);
  1359. }
  1360. psp = engine->ports;
  1361. match_cnt = 0;
  1362. matching_ports = (const char **)
  1363. malloc (sizeof (char *) * engine->port_max);
  1364. for (i = 0; i < engine->port_max; i++) {
  1365. matching = 1;
  1366. if (!psp[i].in_use) {
  1367. continue;
  1368. }
  1369. if (flags) {
  1370. if ((psp[i].flags & flags) != flags) {
  1371. matching = 0;
  1372. }
  1373. }
  1374. if (matching && port_name_pattern && port_name_pattern[0]) {
  1375. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1376. matching = 0;
  1377. }
  1378. }
  1379. if (matching && type_name_pattern && type_name_pattern[0]) {
  1380. jack_port_type_id_t ptid = psp[i].ptype_id;
  1381. if (regexec (&type_regex,
  1382. engine->port_types[ptid].type_name,
  1383. 0, NULL, 0)) {
  1384. matching = 0;
  1385. }
  1386. }
  1387. if (matching) {
  1388. matching_ports[match_cnt++] = psp[i].name;
  1389. }
  1390. }
  1391. matching_ports[match_cnt] = 0;
  1392. if (match_cnt == 0) {
  1393. free (matching_ports);
  1394. matching_ports = 0;
  1395. }
  1396. return matching_ports;
  1397. }
  1398. float
  1399. jack_cpu_load (jack_client_t *client)
  1400. {
  1401. return client->engine->cpu_load;
  1402. }
  1403. pthread_t
  1404. jack_client_thread_id (jack_client_t *client)
  1405. {
  1406. return client->thread_id;
  1407. }
  1408. #if defined(__APPLE__) && defined(__POWERPC__)
  1409. double __jack_time_ratio;
  1410. void jack_init_time ()
  1411. {
  1412. mach_timebase_info_data_t info;
  1413. mach_timebase_info(&info);
  1414. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1415. }
  1416. #else
  1417. jack_time_t
  1418. jack_get_mhz (void)
  1419. {
  1420. FILE *f = fopen("/proc/cpuinfo", "r");
  1421. if (f == 0)
  1422. {
  1423. perror("can't open /proc/cpuinfo\n");
  1424. exit(1);
  1425. }
  1426. for ( ; ; )
  1427. {
  1428. jack_time_t mhz;
  1429. int ret;
  1430. char buf[1000];
  1431. if (fgets(buf, sizeof(buf), f) == NULL) {
  1432. jack_error ("FATAL: cannot locate cpu MHz in "
  1433. "/proc/cpuinfo\n");
  1434. exit(1);
  1435. }
  1436. #if defined(__powerpc__)
  1437. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  1438. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  1439. defined(__x86_64__)
  1440. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  1441. #elif defined( __sparc__ )
  1442. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  1443. #elif defined( __mc68000__ )
  1444. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  1445. #elif defined( __s390__ )
  1446. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  1447. #else /* MIPS, ARM, alpha */
  1448. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  1449. #endif
  1450. if (ret == 1)
  1451. {
  1452. fclose(f);
  1453. return (jack_time_t)mhz;
  1454. }
  1455. }
  1456. }
  1457. jack_time_t __jack_cpu_mhz;
  1458. void jack_init_time ()
  1459. {
  1460. __jack_cpu_mhz = jack_get_mhz ();
  1461. }
  1462. #endif