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.

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