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.

182 lines
4.5KB

  1. /*
  2. * smaplerate.c -- get current samplerate
  3. *
  4. * Copyright (C) 2003 Jack O'Quin.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <jack/jack.h>
  27. #include <jack/jslist.h>
  28. #include <jack/transport.h>
  29. char *package; /* program name */
  30. jack_client_t *client;
  31. jack_session_event_t notify_type;
  32. char *save_path = NULL;
  33. void jack_shutdown(void *arg)
  34. {
  35. fprintf(stderr, "JACK shut down, exiting ...\n");
  36. exit(1);
  37. }
  38. void signal_handler(int sig)
  39. {
  40. jack_client_close(client);
  41. fprintf(stderr, "signal received, exiting ...\n");
  42. exit(0);
  43. }
  44. void parse_arguments(int argc, char *argv[])
  45. {
  46. /* basename $0 */
  47. package = strrchr(argv[0], '/');
  48. if (package == 0)
  49. package = argv[0];
  50. else
  51. package++;
  52. if (argc==2) {
  53. if( !strcmp( argv[1], "quit" ) ) {
  54. notify_type = JackSessionQuit;
  55. return;
  56. }
  57. }
  58. if (argc==3) {
  59. if( !strcmp( argv[1], "save" ) ) {
  60. notify_type = JackSessionSave;
  61. save_path = argv[2];
  62. return;
  63. }
  64. }
  65. fprintf(stderr, "usage: %s quit|save [path]\n", package);
  66. exit(9);
  67. }
  68. typedef struct {
  69. char name[32];
  70. char uuid[16];
  71. } uuid_map_t;
  72. JSList *uuid_map = NULL;
  73. void add_uuid_mapping( const char *uuid ) {
  74. char *clientname = jack_get_client_name_by_uuid( client, uuid );
  75. if( !clientname ) {
  76. printf( "error... cant find client for uuid" );
  77. return;
  78. }
  79. uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
  80. snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
  81. snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
  82. uuid_map = jack_slist_append( uuid_map, mapping );
  83. }
  84. char *map_port_name_to_uuid_port( const char *port_name )
  85. {
  86. JSList *node;
  87. char retval[300];
  88. char *port_component = strchr( port_name,':' );
  89. char *client_component = strdup( port_name );
  90. strchr( client_component, ':' )[0] = '\0';
  91. sprintf( retval, "%s", port_name );
  92. for( node=uuid_map; node; node=jack_slist_next(node) ) {
  93. uuid_map_t *mapping = node->data;
  94. if( !strcmp( mapping->name, client_component ) ) {
  95. sprintf( retval, "%s%s", mapping->uuid, port_component );
  96. break;
  97. }
  98. }
  99. return strdup(retval);
  100. }
  101. int main(int argc, char *argv[])
  102. {
  103. parse_arguments(argc, argv);
  104. struct session_command *retval;
  105. int k,i,j;
  106. /* become a JACK client */
  107. if ((client = jack_client_new(package)) == 0) {
  108. fprintf(stderr, "JACK server not running?\n");
  109. exit(1);
  110. }
  111. signal(SIGQUIT, signal_handler);
  112. signal(SIGTERM, signal_handler);
  113. signal(SIGHUP, signal_handler);
  114. signal(SIGINT, signal_handler);
  115. jack_on_shutdown(client, jack_shutdown, 0);
  116. retval = jack_session_notify( client, notify_type, save_path );
  117. for(i=0; retval[i].uid != 0; i++ ) {
  118. char uidstring[16];
  119. snprintf( uidstring, sizeof(uidstring), "%d", retval[i].uid );
  120. printf( "%s &\n", retval[i].command );
  121. add_uuid_mapping(uidstring);
  122. }
  123. printf( "sleep 10\n" );
  124. for(k=0; retval[k].uid != 0; k++ ) {
  125. char uidstring[16];
  126. snprintf( uidstring, sizeof(uidstring), "%d", retval[k].uid );
  127. char* port_regexp = alloca( jack_client_name_size()+3 );
  128. char* client_name = jack_get_client_name_by_uuid( client, uidstring );
  129. snprintf( port_regexp, jack_client_name_size()+3, "%s.*", client_name );
  130. jack_free(client_name);
  131. const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
  132. if( !ports ) {
  133. continue;
  134. }
  135. for (i = 0; ports[i]; ++i) {
  136. const char **connections;
  137. if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
  138. for (j = 0; connections[j]; j++) {
  139. char *src = map_port_name_to_uuid_port( ports[i] );
  140. char *dst = map_port_name_to_uuid_port( connections[j] );
  141. printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
  142. }
  143. jack_free (connections);
  144. }
  145. }
  146. jack_free(ports);
  147. }
  148. free(retval);
  149. jack_client_close(client);
  150. return 0;
  151. }