LADI JACK2: Switch to 2.minor versioning scheme
[jackdbus.git] / tools / session_notify.c
blob9f96c2617002005aa248072698989a4327f93d13
1 /*
2 * session_notify.c -- ultra minimal session manager
4 * Copyright (C) 2018 Karl Linden <karl.j.linden@gmail.com>
5 * Copyright (C) 2010 Torben Hohn.
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.
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.
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.
22 #include <alloca.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <jack/jack.h>
30 #include <jack/jslist.h>
31 #include <jack/transport.h>
32 #include <jack/session.h>
34 char *package; /* program name */
35 jack_client_t *client;
37 jack_session_event_type_t notify_type;
38 char *save_path = NULL;
40 void jack_shutdown(void *arg)
42 fprintf(stderr, "JACK shut down, exiting ...\n");
43 exit(1);
46 void signal_handler(int sig)
48 jack_client_close(client);
49 fprintf(stderr, "signal received, exiting ...\n");
50 exit(0);
53 void parse_arguments(int argc, char *argv[])
56 /* basename $0 */
57 package = strrchr(argv[0], '/');
58 if (package == 0)
59 package = argv[0];
60 else
61 package++;
63 if (argc==2) {
64 if( !strcmp( argv[1], "quit" ) ) {
65 notify_type = JackSessionSaveAndQuit;
66 return;
69 if (argc==3) {
70 if( !strcmp( argv[1], "save" ) ) {
71 notify_type = JackSessionSave;
72 save_path = argv[2];
73 return;
77 fprintf(stderr, "usage: %s quit|save [path]\n", package);
78 exit(9);
81 typedef struct {
82 char name[32];
83 char uuid[16];
84 } uuid_map_t;
86 JSList *uuid_map = NULL;
88 void add_uuid_mapping( const char *uuid ) {
89 char *clientname = jack_get_client_name_by_uuid( client, uuid );
90 if( !clientname ) {
91 printf( "error... can not find client for uuid" );
92 return;
95 uuid_map_t *mapping = malloc( sizeof(uuid_map_t) );
96 snprintf( mapping->uuid, sizeof(mapping->uuid), "%s", uuid );
97 snprintf( mapping->name, sizeof(mapping->name), "%s", clientname );
98 uuid_map = jack_slist_append( uuid_map, mapping );
101 char *map_port_name_to_uuid_port( const char *port_name )
103 JSList *node;
104 char retval[300];
105 char *port_component = strchr( port_name,':' );
106 char *client_component = strdup( port_name );
107 strchr( client_component, ':' )[0] = '\0';
109 sprintf( retval, "%s", port_name );
111 for( node=uuid_map; node; node=jack_slist_next(node) ) {
112 uuid_map_t *mapping = node->data;
113 if( !strcmp( mapping->name, client_component ) ) {
114 sprintf( retval, "%s%s", mapping->uuid, port_component );
115 break;
119 return strdup(retval);
122 int main(int argc, char *argv[])
124 parse_arguments(argc, argv);
125 jack_session_command_t *retval;
126 int k,i,j;
129 /* become a JACK client */
130 if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
131 fprintf(stderr, "JACK server not running?\n");
132 exit(1);
135 #ifndef WIN32
136 signal(SIGQUIT, signal_handler);
137 signal(SIGHUP, signal_handler);
138 #endif
140 signal(SIGTERM, signal_handler);
141 signal(SIGINT, signal_handler);
143 jack_on_shutdown(client, jack_shutdown, 0);
145 jack_activate(client);
148 retval = jack_session_notify( client, NULL, notify_type, save_path );
149 for (i = 0; retval[i].uuid; i++) {
150 printf( "export SESSION_DIR=\"%s%s/\"\n", save_path, retval[i].client_name );
151 printf( "%s &\n", retval[i].command );
152 add_uuid_mapping(retval[i].uuid);
155 printf( "sleep 10\n" );
157 for (k = 0; retval[k].uuid; k++) {
159 char* port_regexp = alloca( jack_client_name_size()+3 );
160 char* client_name = jack_get_client_name_by_uuid( client, retval[k].uuid );
161 snprintf( port_regexp, jack_client_name_size()+3, "%s:.*", client_name );
162 jack_free(client_name);
163 const char **ports = jack_get_ports( client, port_regexp, NULL, 0 );
164 if( !ports ) {
165 continue;
167 for (i = 0; ports[i]; ++i) {
168 const char **connections;
169 if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
170 for (j = 0; connections[j]; j++) {
171 char *src = map_port_name_to_uuid_port( ports[i] );
172 char *dst = map_port_name_to_uuid_port( connections[j] );
173 printf( "jack_connect -u \"%s\" \"%s\"\n", src, dst );
175 jack_free (connections);
178 jack_free(ports);
181 jack_session_commands_free(retval);
183 jack_client_close(client);
185 return 0;