File: aj-jack.c

package info (click to toggle)
aj-snapshot 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 528 kB
  • ctags: 82
  • sloc: sh: 1,042; ansic: 920; makefile: 8
file content (254 lines) | stat: -rw-r--r-- 9,361 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/************************************************************************/
/* Copyright (C) 2009-2012 Lieven Moors and Jari Suominen               */
/*                                                                      */
/* This file is part of aj-snapshot.                                    */
/*                                                                      */
/* aj-snapshot is free software: you can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or    */
/* (at your option) any later version.                                  */
/*                                                                      */
/* aj-snapshot is distributed in the hope that it will be useful,       */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
/* GNU General Public License for more details.                         */
/*                                                                      */
/* You should have received a copy of the GNU General Public License    */
/* along with aj-snapshot.  If not, see <http://www.gnu.org/licenses/>. */
/************************************************************************/

#include "aj-snapshot.h"

extern int verbose;
extern int daemon_running;
extern int jack_dirty;
extern pthread_mutex_t registration_callback_lock;
extern pthread_mutex_t shutdown_callback_lock;
extern int jack_success;


void jack_port_registration (jack_port_id_t port, int reg, void *arg) {
    /* This callback is called when new ports appear. */
    if(reg){
        pthread_mutex_lock( &registration_callback_lock );
        jack_dirty++;
        pthread_mutex_unlock( &registration_callback_lock );
    }
}

void jack_shutdown (void * jackc) {
    fprintf(stdout, "aj-snapshot: Jack server has been shut down.\n");

    jack_client_t** tmp;
    tmp = (jack_client_t**)jackc;

    pthread_mutex_lock( &shutdown_callback_lock );
    *tmp = NULL;
    pthread_mutex_unlock( &shutdown_callback_lock );    

}


void jack_initialize( jack_client_t** jackc, int callbacks_on )
{
    int result;
    jack_options_t options = JackNoStartServer;
    *jackc = jack_client_open("aj-snapshot", options, NULL);

    if (*jackc == NULL) {
        if (verbose && !daemon_running) fprintf(stderr, "aj-snapshot: Jack server is not running.\n");
        return;
    }

    jack_on_shutdown(*jackc, jack_shutdown, jackc);

    if (callbacks_on) {
        result = jack_set_port_registration_callback(*jackc, jack_port_registration, 0);
        if(result < 0) fprintf(stderr, "aj-snapshot: Failed to set the port registration callback.\n");
        if (jack_activate (*jackc)) {
            fprintf (stderr, "aj-snapshot: Jack server seems to be running but is not responding.\n");
            jack_client_close(*jackc); 
            *jackc = NULL;
            return;
        }
    }
    
    pthread_mutex_lock( &registration_callback_lock );
    jack_dirty = 1;
    pthread_mutex_unlock( &registration_callback_lock );

    if (verbose && daemon_running) fprintf(stdout, "aj-snapshot: Jack server was started.\n");

    return;
}

void jack_restore_connections( jack_client_t** jackc, const char* client_name, const char* port_name, mxml_node_t* port_node )
{
    mxml_node_t* connection_node;
    const char* dest_port;
    unsigned int s = strlen(client_name) + strlen(port_name) + 2;
    char src_port[s];
    char tmp_str[jack_port_name_size()];
    char* dest_client_name;

    snprintf(src_port, s, "%s:%s", client_name, port_name);

    connection_node = mxmlFindElement(port_node, port_node, "connection", NULL, NULL, MXML_DESCEND_FIRST);

    while (connection_node){
        dest_port = mxmlElementGetAttr(connection_node, "port");

        strcpy(tmp_str, dest_port); // dest_port is 'const char*'
        dest_client_name = strtok(tmp_str, ":");

        if(!is_ignored_client(dest_client_name)){
            int err;

            pthread_mutex_lock( &shutdown_callback_lock );
            if (*jackc == NULL) {
                err = ENOENT;
            } else {
                err = jack_connect(*jackc, src_port, dest_port);
            }
            pthread_mutex_unlock( &shutdown_callback_lock );

            if(verbose){
                if (err == 0) {
                    fprintf(stdout, "Connecting port '%s' with '%s'\n", src_port, dest_port);
                } 
                else if (!daemon_running) {
                    if (err == EEXIST) {
                        fprintf(stdout, "Port '%s' is already connected to '%s'\n", src_port, dest_port);
                    } 
                    else {
                        fprintf(stdout, "Failed to connect port '%s' to '%s'!\n", src_port, dest_port);
                        jack_success = 0;
                    }
                }
            }
        }
        else if(verbose && !daemon_running){
            fprintf(stdout, "Ignoring connection to JACK client: %s\n", dest_client_name);
        }
        connection_node = mxmlFindElement(connection_node, port_node, "connection", NULL, NULL, MXML_NO_DESCEND);
    }
}

void jack_restore_ports( jack_client_t** jackc, const char* client_name, mxml_node_t* client_node)
{
    mxml_node_t* port_node;
    const char* port_name;

    port_node = mxmlFindElement(client_node, client_node, "port", NULL, NULL, MXML_DESCEND_FIRST);

    while (port_node){
        port_name = mxmlElementGetAttr(port_node, "name");
        jack_restore_connections(jackc, client_name, port_name, port_node);
        port_node = mxmlFindElement(port_node, client_node, "port", NULL, NULL, MXML_NO_DESCEND);
    }
}

void jack_restore_clients( jack_client_t** jackc, mxml_node_t* jack_node )
{
    mxml_node_t* client_node;
    const char* client_name;

    client_node = mxmlFindElement(jack_node, jack_node, "client", NULL, NULL, MXML_DESCEND_FIRST);

    while (client_node){
        client_name = mxmlElementGetAttr(client_node, "name");

        if( !is_ignored_client(client_name) ){
            jack_restore_ports(jackc, client_name, client_node);
        }
        else if(verbose  && !daemon_running){
                fprintf(stdout, "Ignoring JACK client %s\n", client_name);
        }
        client_node = mxmlFindElement(client_node, jack_node, "client", NULL, NULL, MXML_NO_DESCEND);
    }
}

void jack_restore( jack_client_t** jackc, mxml_node_t* xml_node )
{
   
    mxml_node_t* jack_node;
    jack_node = mxmlFindElement(xml_node, xml_node, "jack", NULL, NULL, MXML_DESCEND_FIRST);
    jack_restore_clients(jackc, jack_node);
}

void jack_store_connections( jack_client_t* jackc, const char* port_name, mxml_node_t* port_node )
{

    mxml_node_t* connection_node;

    jack_port_t* port = jack_port_by_name(jackc, port_name);
    const char** connected_ports = jack_port_get_all_connections(jackc, port);

    if(connected_ports != NULL)
    {
        unsigned int i = 0;
        const char* connected_port = connected_ports[i];
        char tmp_str[jack_port_name_size()];
        char* dest_client_name;

        while (connected_port) {
            strcpy(tmp_str, connected_port); // otherwise we change the names of ports in jack !!
            dest_client_name = strtok(tmp_str, ":");

            if(!is_ignored_client(dest_client_name)){
                connection_node = mxmlNewElement(port_node, "connection");
                mxmlElementSetAttr(connection_node, "port", connected_port);
            } 
            else if(verbose){
                    fprintf(stdout, "Ignoring connection to JACK client: %s\n", dest_client_name);
            }
            connected_port = connected_ports[++i];
        }
    }
}

void jack_store( jack_client_t* jackc, mxml_node_t* xml_node )
{
    mxml_node_t* jack_node = mxmlNewElement(xml_node, "jack");
    mxml_node_t* client_node = NULL;
    mxml_node_t* port_node = NULL;

    char tmp_str[jack_port_name_size()];
    char client_name_prev[jack_port_name_size()];
    const char* client_name;
    char* port_name;

    if (jackc == NULL) return;

    const char **jack_output_ports = jack_get_ports(jackc, NULL, NULL, JackPortIsOutput);
    
    unsigned int i = 0;
    const char* full_name = jack_output_ports[i];

    while (full_name) {
        strcpy(tmp_str, full_name); // otherwise we change the names of ports in jack !!

        client_name = strtok(tmp_str, ":");
        port_name = strtok(NULL, "\0");

        if( !is_ignored_client(client_name) ){
            if ( strcmp(client_name_prev, client_name) ){
                client_node = mxmlNewElement(jack_node, "client");
                mxmlElementSetAttr(client_node, "name", client_name);
                strcpy(client_name_prev, client_name);
            }
            port_node = mxmlNewElement(client_node, "port");
            mxmlElementSetAttr(port_node, "name", port_name);
            jack_store_connections(jackc, full_name, port_node);
        }
        else {
            if( strcmp(client_name_prev, client_name) ){
                if(verbose) 
                    fprintf(stdout, "Ignoring JACK client: %s\n", client_name);
                strcpy(client_name_prev, client_name);
            }
        }

        full_name = jack_output_ports[++i];
    }
}