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.

193 lines
4.7KB

  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. #ifndef WIN32
  25. #include <unistd.h>
  26. #endif
  27. #include <signal.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <jack/jack.h>
  31. #include <jack/jslist.h>
  32. #include <jack/transport.h>
  33. #include <jack/session.h>
  34. #ifdef _MSC_VER
  35. #define alloca _alloca
  36. #endif
  37. char *package; /* program name */
  38. jack_client_t *client;
  39. jack_session_event_type_t notify_type;
  40. char *save_path = NULL;
  41. void jack_shutdown(void *arg)
  42. {
  43. fprintf(stderr, "JACK shut down, exiting ...\n");
  44. exit(1);
  45. }
  46. void signal_handler(int sig)
  47. {
  48. jack_client_close(client);
  49. fprintf(stderr, "signal received, exiting ...\n");
  50. exit(0);
  51. }
  52. void parse_arguments(int argc, char *argv[])
  53. {
  54. /* basename $0 */
  55. package = strrchr(argv[0], '/');
  56. if (package == 0)
  57. package = argv[0];
  58. else
  59. package++;
  60. if (argc==2) {
  61. if( !strcmp( argv[1], "quit" ) ) {
  62. notify_type = JackSessionSaveAndQuit;
  63. return;
  64. }
  65. }
  66. if (argc==3) {
  67. if( !strcmp( argv[1], "save" ) ) {
  68. notify_type = JackSessionSave;
  69. save_path = argv[2];
  70. return;
  71. }
  72. }
  73. fprintf(stderr, "usage: %s quit|save [path]\n", package);
  74. exit(9);
  75. }
  76. typedef struct {
  77. char name[32];
  78. char uuid[16];
  79. } uuid_map_t;
  80. JSList *uuid_map = NULL;
  81. void add_uuid_mapping( const char *uuid ) {
  82. char *clientname = jack_get_client_name_by_uuid( client, uuid );
  83. if( !clientname ) {
  84. printf( "error... cant find client for uuid" );
  85. return;
  86. }
  87. uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
  88. snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
  89. snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
  90. uuid_map = jack_slist_append( uuid_map, mapping );
  91. }
  92. char *map_port_name_to_uuid_port( const char *port_name )
  93. {
  94. JSList *node;
  95. char retval[300];
  96. char *port_component = strchr( port_name,':' );
  97. char *client_component = strdup( port_name );
  98. strchr( client_component, ':' )[0] = '\0';
  99. sprintf( retval, "%s", port_name );
  100. for( node=uuid_map; node; node=jack_slist_next(node) ) {
  101. uuid_map_t *mapping = node->data;
  102. if( !strcmp( mapping->name, client_component ) ) {
  103. sprintf( retval, "%s%s", mapping->uuid, port_component );
  104. break;
  105. }
  106. }
  107. return strdup(retval);
  108. }
  109. int main(int argc, char *argv[])
  110. {
  111. parse_arguments(argc, argv);
  112. jack_session_command_t *retval;
  113. int k,i,j;
  114. /* become a JACK client */
  115. if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
  116. fprintf(stderr, "JACK server not running?\n");
  117. exit(1);
  118. }
  119. #ifndef WIN32
  120. signal(SIGQUIT, signal_handler);
  121. signal(SIGHUP, signal_handler);
  122. #endif
  123. signal(SIGTERM, signal_handler);
  124. signal(SIGINT, signal_handler);
  125. jack_on_shutdown(client, jack_shutdown, 0);
  126. jack_activate(client);
  127. retval = jack_session_notify( client, NULL, notify_type, save_path );
  128. for (i = 0; retval[i].uuid; i++) {
  129. printf( "export SESSION_DIR=\"%s%s/\"\n", save_path, retval[i].client_name );
  130. printf( "%s &\n", retval[i].command );
  131. add_uuid_mapping(retval[i].uuid);
  132. }
  133. printf( "sleep 10\n" );
  134. for (k = 0; retval[k].uuid; k++) {
  135. char* port_regexp = alloca( jack_client_name_size()+3 );
  136. char* client_name = jack_get_client_name_by_uuid( client, retval[k].uuid );
  137. snprintf( port_regexp, jack_client_name_size()+3, "%s:.*", client_name );
  138. jack_free(client_name);
  139. const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
  140. if( !ports ) {
  141. continue;
  142. }
  143. for (i = 0; ports[i]; ++i) {
  144. const char **connections;
  145. if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
  146. for (j = 0; connections[j]; j++) {
  147. char *src = map_port_name_to_uuid_port( ports[i] );
  148. char *dst = map_port_name_to_uuid_port( connections[j] );
  149. printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
  150. }
  151. jack_free (connections);
  152. }
  153. }
  154. jack_free(ports);
  155. }
  156. jack_session_commands_free(retval);
  157. jack_client_close(client);
  158. return 0;
  159. }