Correct MIDI ports memory management in JackNetDriver.
[jack2.git] / example-clients / session_notify.c
blob9cb3f894dc7fb22b04944833d3d96cbca270c10d
1 /*
2 * session_notify.c -- ultra minimal session manager
4 * Copyright (C) 2010 Torben Hohn.
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.
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.
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.
21 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <jack/jack.h>
28 #include <jack/jslist.h>
29 #include <jack/transport.h>
30 #include <jack/session.h>
32 char *package; /* program name */
33 jack_client_t *client;
35 jack_session_event_type_t notify_type;
36 char *save_path = NULL;
38 void jack_shutdown(void *arg)
40 fprintf(stderr, "JACK shut down, exiting ...\n");
41 exit(1);
44 void signal_handler(int sig)
46 jack_client_close(client);
47 fprintf(stderr, "signal received, exiting ...\n");
48 exit(0);
51 void parse_arguments(int argc, char *argv[])
54 /* basename $0 */
55 package = strrchr(argv[0], '/');
56 if (package == 0)
57 package = argv[0];
58 else
59 package++;
61 if (argc==2) {
62 if( !strcmp( argv[1], "quit" ) ) {
63 notify_type = JackSessionSaveAndQuit;
64 return;
67 if (argc==3) {
68 if( !strcmp( argv[1], "save" ) ) {
69 notify_type = JackSessionSave;
70 save_path = argv[2];
71 return;
75 fprintf(stderr, "usage: %s quit|save [path]\n", package);
76 exit(9);
79 typedef struct {
80 char name[32];
81 char uuid[16];
82 } uuid_map_t;
84 JSList *uuid_map = NULL;
86 void add_uuid_mapping( const char *uuid ) {
87 char *clientname = jack_get_client_name_by_uuid( client, uuid );
88 if( !clientname ) {
89 printf( "error... cant find client for uuid" );
90 return;
93 uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
94 snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
95 snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
96 uuid_map = jack_slist_append( uuid_map, mapping );
99 char *map_port_name_to_uuid_port( const char *port_name )
101 JSList *node;
102 char retval[300];
103 char *port_component = strchr( port_name,':' );
104 char *client_component = strdup( port_name );
105 strchr( client_component, ':' )[0] = '\0';
107 sprintf( retval, "%s", port_name );
109 for( node=uuid_map; node; node=jack_slist_next(node) ) {
110 uuid_map_t *mapping = node->data;
111 if( !strcmp( mapping->name, client_component ) ) {
112 sprintf( retval, "%s%s", mapping->uuid, port_component );
113 break;
117 return strdup(retval);
120 int main(int argc, char *argv[])
122 parse_arguments(argc, argv);
123 jack_session_command_t *retval;
124 int k,i,j;
127 /* become a JACK client */
128 if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
129 fprintf(stderr, "JACK server not running?\n");
130 exit(1);
133 signal(SIGQUIT, signal_handler);
134 signal(SIGTERM, signal_handler);
135 signal(SIGHUP, signal_handler);
136 signal(SIGINT, signal_handler);
138 jack_on_shutdown(client, jack_shutdown, 0);
140 jack_activate(client);
143 retval = jack_session_notify( client, NULL, notify_type, save_path );
144 for(i=0; retval[i].uuid; i++ ) {
145 printf( "export SESSION_DIR=\"%s%s/\"\n", save_path, retval[i].client_name );
146 printf( "%s &\n", retval[i].command );
147 add_uuid_mapping(retval[i].uuid);
150 printf( "sleep 10\n" );
152 for(k=0; retval[k].uuid; k++ ) {
154 char* port_regexp = alloca( jack_client_name_size()+3 );
155 char* client_name = jack_get_client_name_by_uuid( client, retval[k].uuid );
156 snprintf( port_regexp, jack_client_name_size()+3, "%s:.*", client_name );
157 jack_free(client_name);
158 const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
159 if( !ports ) {
160 continue;
162 for (i = 0; ports[i]; ++i) {
163 const char **connections;
164 if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
165 for (j = 0; connections[j]; j++) {
166 char *src = map_port_name_to_uuid_port( ports[i] );
167 char *dst = map_port_name_to_uuid_port( connections[j] );
168 printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
170 jack_free (connections);
173 jack_free(ports);
176 jack_session_commands_free(retval);
178 jack_client_close(client);
180 return 0;