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. 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_drop_real_time_scheduling (client->thread);
  567. }
  568. void
  569. jack_stop_freewheel (jack_client_t* client)
  570. {
  571. jack_acquire_real_time_scheduling (client->thread,
  572. client->engine->client_priority);
  573. }
  574. static void *
  575. jack_client_thread (void *arg)
  576. {
  577. jack_client_t *client = (jack_client_t *) arg;
  578. jack_client_control_t *control = client->control;
  579. jack_event_t event;
  580. char status = 0;
  581. char c;
  582. int err = 0;
  583. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  584. pthread_mutex_lock (&client_lock);
  585. client->thread_ok = TRUE;
  586. client->thread_id = pthread_self();
  587. pthread_cond_signal (&client_ready);
  588. pthread_mutex_unlock (&client_lock);
  589. client->control->pid = getpid();
  590. client->control->pgrp = getpgrp();
  591. DEBUG ("client thread is now running");
  592. while (err == 0) {
  593. if (client->engine->engine_ok == 0) {
  594. jack_error ("engine unexpectedly shutdown; "
  595. "thread exiting\n");
  596. if (client->on_shutdown) {
  597. client->on_shutdown (client->on_shutdown_arg);
  598. }
  599. pthread_exit (0);
  600. }
  601. DEBUG ("client polling on event_fd and graph_wait_fd...");
  602. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  603. if (errno == EINTR) {
  604. fprintf (stderr, "poll interrupted\n");
  605. continue;
  606. }
  607. jack_error ("poll failed in client (%s)",
  608. strerror (errno));
  609. status = -1;
  610. break;
  611. }
  612. /* get an accurate timestamp on waking from poll for a
  613. * process() cycle.
  614. */
  615. if (client->pollfd[1].revents & POLLIN) {
  616. control->awake_at = jack_get_microseconds();
  617. }
  618. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  619. client->pollfd[0].revents,
  620. client->pollfd[1].revents);
  621. pthread_testcancel();
  622. if ((client->pollfd[0].revents & ~POLLIN) ||
  623. client->control->dead) {
  624. goto zombie;
  625. }
  626. if (client->pollfd[0].revents & POLLIN) {
  627. DEBUG ("client receives an event, "
  628. "now reading on event fd");
  629. /* server has sent us an event. process the
  630. * event and reply */
  631. if (read (client->event_fd, &event, sizeof (event))
  632. != sizeof (event)) {
  633. jack_error ("cannot read server event (%s)",
  634. strerror (errno));
  635. err++;
  636. break;
  637. }
  638. status = 0;
  639. switch (event.type) {
  640. case PortRegistered:
  641. if (control->port_register) {
  642. control->port_register
  643. (event.x.port_id, TRUE,
  644. control->port_register_arg);
  645. }
  646. break;
  647. case PortUnregistered:
  648. if (control->port_register) {
  649. control->port_register
  650. (event.x.port_id, FALSE,
  651. control->port_register_arg);
  652. }
  653. break;
  654. case GraphReordered:
  655. status = jack_handle_reorder (client, &event);
  656. break;
  657. case PortConnected:
  658. case PortDisconnected:
  659. status = jack_client_handle_port_connection
  660. (client, &event);
  661. break;
  662. case BufferSizeChange:
  663. jack_client_invalidate_port_buffers (client);
  664. if (control->bufsize) {
  665. status = control->bufsize
  666. (control->nframes,
  667. control->bufsize_arg);
  668. }
  669. break;
  670. case SampleRateChange:
  671. if (control->srate) {
  672. status = control->srate
  673. (control->nframes,
  674. control->srate_arg);
  675. }
  676. break;
  677. case XRun:
  678. if (control->xrun) {
  679. status = control->xrun
  680. (control->xrun_arg);
  681. }
  682. break;
  683. case AttachPortSegment:
  684. jack_attach_port_segment (client, event.y.ptid);
  685. break;
  686. case StartFreewheel:
  687. jack_start_freewheel (client);
  688. break;
  689. case StopFreewheel:
  690. jack_stop_freewheel (client);
  691. break;
  692. }
  693. DEBUG ("client has dealt with the event, writing "
  694. "response on event fd");
  695. if (write (client->event_fd, &status, sizeof (status))
  696. != sizeof (status)) {
  697. jack_error ("cannot send event response to "
  698. "engine (%s)", strerror (errno));
  699. err++;
  700. break;
  701. }
  702. }
  703. if (client->pollfd[1].revents & ~POLLIN) {
  704. goto zombie;
  705. }
  706. if (client->pollfd[1].revents & POLLIN) {
  707. #ifdef WITH_TIMESTAMPS
  708. jack_reset_timestamps ();
  709. #endif
  710. DEBUG ("client %d signalled at %" PRIu64
  711. ", awake for process at %" PRIu64
  712. " (delay = %" PRIu64
  713. " usecs) (wakeup on graph_wait_fd==%d)",
  714. getpid(),
  715. control->signalled_at,
  716. control->awake_at,
  717. control->awake_at - control->signalled_at,
  718. client->pollfd[1].fd);
  719. control->state = Running;
  720. if (control->sync_cb)
  721. jack_call_sync_client (client);
  722. if (control->process) {
  723. if (control->process (control->nframes,
  724. control->process_arg)
  725. == 0) {
  726. control->state = Finished;
  727. }
  728. } else {
  729. control->state = Finished;
  730. }
  731. if (control->timebase_cb)
  732. jack_call_timebase_master (client);
  733. control->finished_at = jack_get_microseconds();
  734. #ifdef WITH_TIMESTAMPS
  735. jack_timestamp ("finished");
  736. #endif
  737. /* pass the execution token along */
  738. DEBUG ("client finished processing at %" PRIu64
  739. " (elapsed = %" PRIu64
  740. " usecs), writing on graph_next_fd==%d",
  741. control->finished_at,
  742. control->finished_at - control->awake_at,
  743. client->graph_next_fd);
  744. if (write (client->graph_next_fd, &c, sizeof (c))
  745. != sizeof (c)) {
  746. jack_error ("cannot continue execution of the "
  747. "processing graph (%s)",
  748. strerror(errno));
  749. err++;
  750. break;
  751. }
  752. DEBUG ("client sent message to next stage by %" PRIu64
  753. ", client reading on graph_wait_fd==%d",
  754. jack_get_microseconds(), client->graph_wait_fd);
  755. #ifdef WITH_TIMESTAMPS
  756. jack_timestamp ("read pending byte from wait");
  757. #endif
  758. DEBUG("reading cleanup byte from pipe\n");
  759. if ((read (client->graph_wait_fd, &c, sizeof (c))
  760. != sizeof (c))) {
  761. DEBUG ("WARNING: READ FAILED!");
  762. #if 0
  763. jack_error ("cannot complete execution of the "
  764. "processing graph (%s)",
  765. strerror(errno));
  766. err++;
  767. break;
  768. #endif
  769. }
  770. /* check if we were killed during the process
  771. * cycle (or whatever) */
  772. if (client->control->dead) {
  773. goto zombie;
  774. }
  775. DEBUG("process cycle fully complete\n");
  776. #ifdef WITH_TIMESTAMPS
  777. jack_timestamp ("read done");
  778. jack_dump_timestamps (stdout);
  779. #endif
  780. }
  781. }
  782. return (void *) ((intptr_t)err);
  783. zombie:
  784. if (client->on_shutdown) {
  785. jack_error ("zombified - calling shutdown handler");
  786. client->on_shutdown (client->on_shutdown_arg);
  787. } else {
  788. jack_error ("zombified - exiting from JACK");
  789. jack_client_close (client);
  790. /* Need a fix : possibly make client crash if
  791. * zombified without shutdown handler
  792. */
  793. }
  794. pthread_exit (0);
  795. /*NOTREACHED*/
  796. return 0;
  797. }
  798. #if defined(__APPLE__) && defined(__POWERPC__)
  799. /* real-time thread : separated from the normal client thread, it will communicate with the server using fast mach RPC mechanism */
  800. static void *
  801. jack_client_process_thread (void *arg)
  802. {
  803. jack_client_t *client = (jack_client_t *) arg;
  804. jack_client_control_t *control = client->control;
  805. int err = 0;
  806. client->control->pid = getpid();
  807. DEBUG ("client process thread is now running");
  808. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  809. while (err == 0) {
  810. if (jack_client_suspend(client) < 0) {
  811. pthread_exit (0);
  812. }
  813. control->awake_at = jack_get_microseconds();
  814. DEBUG ("client resumed");
  815. control->state = Running;
  816. if (control->process) {
  817. if (control->process (control->nframes, control->process_arg) == 0) {
  818. control->state = Finished;
  819. }
  820. } else {
  821. control->state = Finished;
  822. }
  823. control->finished_at = jack_get_microseconds();
  824. #ifdef WITH_TIMESTAMPS
  825. jack_timestamp ("finished");
  826. #endif
  827. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  828. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  829. /* check if we were killed during the process cycle (or whatever) */
  830. if (client->control->dead) {
  831. jack_error ("jack_client_process_thread : client->control->dead");
  832. goto zombie;
  833. }
  834. DEBUG("process cycle fully complete\n");
  835. }
  836. return (void *) ((intptr_t)err);
  837. zombie:
  838. jack_error ("jack_client_process_thread : zombified");
  839. if (client->on_shutdown) {
  840. jack_error ("zombified - calling shutdown handler");
  841. client->on_shutdown (client->on_shutdown_arg);
  842. } else {
  843. jack_error ("zombified - exiting from JACK");
  844. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  845. }
  846. pthread_exit (0);
  847. /*NOTREACHED*/
  848. return 0;
  849. }
  850. #endif
  851. static int
  852. jack_start_thread (jack_client_t *client)
  853. {
  854. pthread_attr_t *attributes = 0;
  855. #ifdef USE_CAPABILITIES
  856. int policy = SCHED_OTHER;
  857. struct sched_param client_param, temp_param;
  858. #endif
  859. if (client->engine->real_time) {
  860. /* Get the client thread to run as an RT-FIFO
  861. scheduled thread of appropriate priority.
  862. */
  863. struct sched_param rt_param;
  864. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  865. pthread_attr_init (attributes);
  866. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  867. jack_error ("cannot set FIFO scheduling class for RT thread");
  868. return -1;
  869. }
  870. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  871. jack_error ("Cannot set scheduling scope for RT thread");
  872. return -1;
  873. }
  874. memset (&rt_param, 0, sizeof (rt_param));
  875. rt_param.sched_priority = client->engine->client_priority;
  876. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  877. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  878. return -1;
  879. }
  880. #if defined(__APPLE__) && defined(__POWERPC__)
  881. // To be implemented
  882. #else
  883. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  884. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  885. return -1;
  886. }
  887. #endif
  888. }
  889. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  890. #ifdef USE_CAPABILITIES
  891. if (client->engine->real_time && client->engine->has_capabilities) {
  892. /* we are probably dealing with a broken glibc so try
  893. to work around the bug, see below for more details
  894. */
  895. goto capabilities_workaround;
  896. }
  897. #endif
  898. return -1;
  899. }
  900. #if defined(__APPLE__) && defined(__POWERPC__)
  901. /* a spcial real-time thread to call the "process" callback. It will communicate with the server using fast mach RPC mechanism */
  902. if (pthread_create (&client->process_thread, attributes, jack_client_process_thread, client)) {
  903. jack_error("pthread_create failed for process_thread \n");
  904. return -1;
  905. }
  906. if (client->engine->real_time){
  907. /* time constraint thread */
  908. setThreadToPriority(client->process_thread, 96, true, 10000000);
  909. }else{
  910. /* fixed priority thread */
  911. setThreadToPriority(client->process_thread, 63, true, 10000000);
  912. }
  913. #endif
  914. return 0;
  915. #ifdef USE_CAPABILITIES
  916. /* we get here only with engine running realtime and capabilities */
  917. capabilities_workaround:
  918. /* the version of glibc I've played with has a bug that makes
  919. that code fail when running under a non-root user but with the
  920. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  921. does not check for capabilities, only for the uid being
  922. zero). Newer versions apparently have this fixed. This
  923. workaround temporarily switches the client thread to the
  924. proper scheduler and priority, then starts the realtime
  925. thread so that it can inherit them and finally switches the
  926. client thread back to what it was before. Sigh. For ardour
  927. I have to check again and switch the thread explicitly to
  928. realtime, don't know why or how to debug - nando
  929. */
  930. /* get current scheduler and parameters of the client process */
  931. if ((policy = sched_getscheduler (0)) < 0) {
  932. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  933. return -1;
  934. }
  935. memset (&client_param, 0, sizeof (client_param));
  936. if (sched_getparam (0, &client_param)) {
  937. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  938. return -1;
  939. }
  940. /* temporarily change the client process to SCHED_FIFO so that
  941. the realtime thread can inherit the scheduler and priority
  942. */
  943. memset (&temp_param, 0, sizeof (temp_param));
  944. temp_param.sched_priority = client->engine->client_priority;
  945. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  946. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  947. return -1;
  948. }
  949. /* prepare the attributes for the realtime thread */
  950. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  951. pthread_attr_init (attributes);
  952. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  953. sched_setscheduler (0, policy, &client_param);
  954. jack_error ("Cannot set scheduling scope for RT thread");
  955. return -1;
  956. }
  957. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  958. sched_setscheduler (0, policy, &client_param);
  959. jack_error ("Cannot set scheduler inherit policy for RT thread");
  960. return -1;
  961. }
  962. /* create the RT thread */
  963. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  964. sched_setscheduler (0, policy, &client_param);
  965. return -1;
  966. }
  967. /* return the client process to the scheduler it was in before */
  968. if (sched_setscheduler (0, policy, &client_param)) {
  969. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  970. return -1;
  971. }
  972. /* check again... inheritance of policy and priority works in jack_simple_client
  973. but not in ardour! So I check again and force the policy if it is not set
  974. correctly. This does not really really work either, the manager thread
  975. of the linuxthreads implementation is left running with SCHED_OTHER,
  976. that is presumably very bad.
  977. */
  978. memset (&client_param, 0, sizeof (client_param));
  979. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  980. if (policy != SCHED_FIFO) {
  981. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  982. memset (&client_param, 0, sizeof (client_param));
  983. client_param.sched_priority = client->engine->client_priority;
  984. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  985. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  986. return -1;
  987. }
  988. }
  989. }
  990. return 0;
  991. #endif
  992. }
  993. int
  994. jack_activate (jack_client_t *client)
  995. {
  996. jack_request_t req;
  997. /* we need to scribble on our stack to ensure that its memory pages are
  998. * actually mapped (more important for mlockall(2) usage in
  999. * jack_start_thread())
  1000. */
  1001. #if defined(__APPLE__) && defined(__POWERPC__)
  1002. #define BIG_ENOUGH_STACK 10000 // a bigger stack make the application crash...
  1003. #else
  1004. #define BIG_ENOUGH_STACK 1048576
  1005. #endif
  1006. char buf[BIG_ENOUGH_STACK];
  1007. int i;
  1008. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  1009. buf[i] = (char) (i & 0xff);
  1010. }
  1011. #undef BIG_ENOUGH_STACK
  1012. if (client->control->type == ClientInternal || client->control->type == ClientDriver) {
  1013. goto startit;
  1014. }
  1015. /* get the pid of the client process to pass it to engine */
  1016. client->control->pid = getpid ();
  1017. #ifdef USE_CAPABILITIES
  1018. if (client->engine->has_capabilities != 0 &&
  1019. client->control->pid != 0 && client->engine->real_time != 0) {
  1020. /* we need to ask the engine for realtime capabilities
  1021. before trying to start the realtime thread
  1022. */
  1023. req.type = SetClientCapabilities;
  1024. req.x.client_id = client->control->id;
  1025. jack_client_deliver_request (client, &req);
  1026. if (req.status) {
  1027. /* what to do? engine is running realtime, it is using capabilities and has
  1028. them (otherwise we would not get an error return) but for some reason it
  1029. could not give the client the required capabilities, so for now downgrade
  1030. the client so that it still runs, albeit non-realtime - nando
  1031. */
  1032. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  1033. /* XXX wrong, this is a property of the engine
  1034. client->engine->real_time = 0;
  1035. */
  1036. }
  1037. }
  1038. #endif
  1039. if (client->first_active) {
  1040. pthread_mutex_init (&client_lock, NULL);
  1041. pthread_cond_init (&client_ready, NULL);
  1042. pthread_mutex_lock (&client_lock);
  1043. if (jack_start_thread (client)) {
  1044. pthread_mutex_unlock (&client_lock);
  1045. return -1;
  1046. }
  1047. pthread_cond_wait (&client_ready, &client_lock);
  1048. pthread_mutex_unlock (&client_lock);
  1049. if (!client->thread_ok) {
  1050. jack_error ("could not start client thread");
  1051. return -1;
  1052. }
  1053. client->first_active = FALSE;
  1054. }
  1055. startit:
  1056. req.type = ActivateClient;
  1057. req.x.client_id = client->control->id;
  1058. return jack_client_deliver_request (client, &req);
  1059. }
  1060. int
  1061. jack_deactivate (jack_client_t *client)
  1062. {
  1063. jack_request_t req;
  1064. req.type = DeactivateClient;
  1065. req.x.client_id = client->control->id;
  1066. return jack_client_deliver_request (client, &req);
  1067. }
  1068. int
  1069. jack_client_close (jack_client_t *client)
  1070. {
  1071. JSList *node;
  1072. void *status;
  1073. if (client->control->active) {
  1074. jack_deactivate (client);
  1075. }
  1076. if (client->control->type == ClientExternal) {
  1077. /* stop the thread that communicates with the jack
  1078. * server, only if it was actually running
  1079. */
  1080. if (client->thread_ok){
  1081. pthread_cancel (client->thread);
  1082. pthread_join (client->thread, &status);
  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. if (client->port_segment) {
  1093. jack_port_type_id_t ptid;
  1094. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1095. jack_release_shm (&client->port_segment[ptid]);
  1096. }
  1097. free (client->port_segment);
  1098. client->port_segment = NULL;
  1099. }
  1100. if (client->graph_wait_fd) {
  1101. close (client->graph_wait_fd);
  1102. }
  1103. if (client->graph_next_fd) {
  1104. close (client->graph_next_fd);
  1105. }
  1106. close (client->event_fd);
  1107. close (client->request_fd);
  1108. }
  1109. for (node = client->ports; node; node = jack_slist_next (node)) {
  1110. free (node->data);
  1111. }
  1112. jack_slist_free (client->ports);
  1113. jack_client_free (client);
  1114. return 0;
  1115. }
  1116. int
  1117. jack_is_realtime (jack_client_t *client)
  1118. {
  1119. return client->engine->real_time;
  1120. }
  1121. jack_nframes_t
  1122. jack_get_buffer_size (jack_client_t *client)
  1123. {
  1124. return client->engine->buffer_size;
  1125. }
  1126. int
  1127. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1128. {
  1129. jack_request_t req;
  1130. req.type = SetBufferSize;
  1131. req.x.nframes = nframes;
  1132. return jack_client_deliver_request (client, &req);
  1133. }
  1134. int
  1135. jack_connect (jack_client_t *client, const char *source_port,
  1136. const char *destination_port)
  1137. {
  1138. jack_request_t req;
  1139. req.type = ConnectPorts;
  1140. snprintf (req.x.connect.source_port,
  1141. sizeof (req.x.connect.source_port), "%s", source_port);
  1142. snprintf (req.x.connect.destination_port,
  1143. sizeof (req.x.connect.destination_port),
  1144. "%s", destination_port);
  1145. return jack_client_deliver_request (client, &req);
  1146. }
  1147. int
  1148. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1149. {
  1150. jack_request_t req;
  1151. pthread_mutex_lock (&port->connection_lock);
  1152. if (port->connections == NULL) {
  1153. pthread_mutex_unlock (&port->connection_lock);
  1154. return 0;
  1155. }
  1156. pthread_mutex_unlock (&port->connection_lock);
  1157. req.type = DisconnectPort;
  1158. req.x.port_info.port_id = port->shared->id;
  1159. return jack_client_deliver_request (client, &req);
  1160. }
  1161. int
  1162. jack_disconnect (jack_client_t *client, const char *source_port,
  1163. const char *destination_port)
  1164. {
  1165. jack_request_t req;
  1166. req.type = DisconnectPorts;
  1167. snprintf (req.x.connect.source_port,
  1168. sizeof (req.x.connect.source_port), "%s", source_port);
  1169. snprintf (req.x.connect.destination_port,
  1170. sizeof (req.x.connect.destination_port),
  1171. "%s", destination_port);
  1172. return jack_client_deliver_request (client, &req);
  1173. }
  1174. void
  1175. jack_set_error_function (void (*func) (const char *))
  1176. {
  1177. jack_error_callback = func;
  1178. }
  1179. int
  1180. jack_set_graph_order_callback (jack_client_t *client,
  1181. JackGraphOrderCallback callback, void *arg)
  1182. {
  1183. if (client->control->active) {
  1184. jack_error ("You cannot set callbacks on an active client.");
  1185. return -1;
  1186. }
  1187. client->control->graph_order = callback;
  1188. client->control->graph_order_arg = arg;
  1189. return 0;
  1190. }
  1191. int jack_set_xrun_callback (jack_client_t *client,
  1192. JackXRunCallback callback, void *arg)
  1193. {
  1194. if (client->control->active) {
  1195. jack_error ("You cannot set callbacks on an active client.");
  1196. return -1;
  1197. }
  1198. client->control->xrun = callback;
  1199. client->control->xrun_arg = arg;
  1200. return 0;
  1201. }
  1202. int
  1203. jack_set_process_callback (jack_client_t *client,
  1204. JackProcessCallback callback, void *arg)
  1205. {
  1206. if (client->control->active) {
  1207. jack_error ("You cannot set callbacks on an active client.");
  1208. return -1;
  1209. }
  1210. client->control->process_arg = arg;
  1211. client->control->process = callback;
  1212. return 0;
  1213. }
  1214. int
  1215. jack_set_buffer_size_callback (jack_client_t *client,
  1216. JackBufferSizeCallback callback, void *arg)
  1217. {
  1218. client->control->bufsize_arg = arg;
  1219. client->control->bufsize = callback;
  1220. return 0;
  1221. }
  1222. int
  1223. jack_set_port_registration_callback(jack_client_t *client,
  1224. JackPortRegistrationCallback callback,
  1225. void *arg)
  1226. {
  1227. if (client->control->active) {
  1228. jack_error ("You cannot set callbacks on an active client.");
  1229. return -1;
  1230. }
  1231. client->control->port_register_arg = arg;
  1232. client->control->port_register = callback;
  1233. return 0;
  1234. }
  1235. int
  1236. jack_get_process_start_fd (jack_client_t *client)
  1237. {
  1238. /* once this has been called, the client thread
  1239. does not sleep on the graph wait fd.
  1240. */
  1241. client->pollmax = 1;
  1242. return client->graph_wait_fd;
  1243. }
  1244. int
  1245. jack_get_process_done_fd (jack_client_t *client)
  1246. {
  1247. return client->graph_next_fd;
  1248. }
  1249. void
  1250. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1251. {
  1252. client->on_shutdown = function;
  1253. client->on_shutdown_arg = arg;
  1254. }
  1255. const char **
  1256. jack_get_ports (jack_client_t *client,
  1257. const char *port_name_pattern,
  1258. const char *type_name_pattern,
  1259. unsigned long flags)
  1260. {
  1261. jack_control_t *engine;
  1262. const char **matching_ports;
  1263. unsigned long match_cnt;
  1264. jack_port_shared_t *psp;
  1265. unsigned long i;
  1266. regex_t port_regex;
  1267. regex_t type_regex;
  1268. int matching;
  1269. engine = client->engine;
  1270. if (port_name_pattern && port_name_pattern[0]) {
  1271. regcomp (&port_regex, port_name_pattern,
  1272. REG_EXTENDED|REG_NOSUB);
  1273. }
  1274. if (type_name_pattern && type_name_pattern[0]) {
  1275. regcomp (&type_regex, type_name_pattern,
  1276. REG_EXTENDED|REG_NOSUB);
  1277. }
  1278. psp = engine->ports;
  1279. match_cnt = 0;
  1280. matching_ports = (const char **)
  1281. malloc (sizeof (char *) * engine->port_max);
  1282. for (i = 0; i < engine->port_max; i++) {
  1283. matching = 1;
  1284. if (!psp[i].in_use) {
  1285. continue;
  1286. }
  1287. if (flags) {
  1288. if ((psp[i].flags & flags) != flags) {
  1289. matching = 0;
  1290. }
  1291. }
  1292. if (matching && port_name_pattern && port_name_pattern[0]) {
  1293. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1294. matching = 0;
  1295. }
  1296. }
  1297. if (matching && type_name_pattern && type_name_pattern[0]) {
  1298. jack_port_type_id_t ptid = psp[i].ptype_id;
  1299. if (regexec (&type_regex,
  1300. engine->port_types[ptid].type_name,
  1301. 0, NULL, 0)) {
  1302. matching = 0;
  1303. }
  1304. }
  1305. if (matching) {
  1306. matching_ports[match_cnt++] = psp[i].name;
  1307. }
  1308. }
  1309. matching_ports[match_cnt] = 0;
  1310. if (match_cnt == 0) {
  1311. free (matching_ports);
  1312. matching_ports = 0;
  1313. }
  1314. return matching_ports;
  1315. }
  1316. float
  1317. jack_cpu_load (jack_client_t *client)
  1318. {
  1319. return client->engine->cpu_load;
  1320. }
  1321. pthread_t
  1322. jack_client_thread_id (jack_client_t *client)
  1323. {
  1324. return client->thread_id;
  1325. }
  1326. #if defined(__APPLE__) && defined(__POWERPC__)
  1327. double __jack_time_ratio;
  1328. void jack_init_time ()
  1329. {
  1330. mach_timebase_info_data_t info;
  1331. mach_timebase_info(&info);
  1332. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1333. }
  1334. #else
  1335. jack_time_t
  1336. jack_get_mhz (void)
  1337. {
  1338. FILE *f = fopen("/proc/cpuinfo", "r");
  1339. if (f == 0)
  1340. {
  1341. perror("can't open /proc/cpuinfo\n");
  1342. exit(1);
  1343. }
  1344. for ( ; ; )
  1345. {
  1346. jack_time_t mhz;
  1347. int ret;
  1348. char buf[1000];
  1349. if (fgets(buf, sizeof(buf), f) == NULL)
  1350. {
  1351. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1352. exit(1);
  1353. }
  1354. #if defined(__powerpc__)
  1355. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  1356. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  1357. defined(__x86_64__)
  1358. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  1359. #elif defined( __sparc__ )
  1360. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  1361. #elif defined( __mc68000__ )
  1362. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  1363. #elif defined( __s390__ )
  1364. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  1365. #else /* MIPS, ARM, alpha */
  1366. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  1367. #endif
  1368. if (ret == 1)
  1369. {
  1370. fclose(f);
  1371. return (jack_time_t)mhz;
  1372. }
  1373. }
  1374. }
  1375. jack_time_t __jack_cpu_mhz;
  1376. void jack_init_time ()
  1377. {
  1378. __jack_cpu_mhz = jack_get_mhz ();
  1379. }
  1380. #endif