jack2 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.

187 lines
4.6KB

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