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.

1740 lines
42KB

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