~ubuntu-branches/ubuntu/dapper/terminatorx/dapper

« back to all changes in this revision

Viewing changes to src/tX_ladspa_class.cc

  • Committer: Bazaar Package Importer
  • Author(s): Mike Furr
  • Date: 2004-04-26 21:20:09 UTC
  • Revision ID: james.westby@ubuntu.com-20040426212009-acjw8flkt05j945f
Tags: upstream-3.81
Import upstream version 3.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    terminatorX - realtime audio scratching software
 
3
    Copyright (C) 1999-2003  Alexander K�nig
 
4
 
 
5
    This program is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 2 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 
 
19
    File: tX_ladspa_class.cc
 
20
*/
 
21
 
 
22
#include "tX_ladspa_class.h"
 
23
#include "tX_global.h"
 
24
#include <dirent.h>
 
25
#include <dlfcn.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
 
 
30
#ifdef USE_LRDF
 
31
#include <lrdf.h>
 
32
#endif
 
33
 
 
34
LADSPA_Class * LADSPA_Class::root=NULL;
 
35
LADSPA_Class * LADSPA_Class::unclassified=NULL;
 
36
std::list <char *> LADSPA_Class::rdf_files;
 
37
vtt_class *LADSPA_Class::current_vtt;
 
38
bool LADSPA_Class::liblrdf_error=false;
 
39
 
 
40
/* Why do have to code this myself? */
 
41
static int compare(const char *a, const char *b) 
 
42
{
 
43
        int lena, lenb, i;
 
44
 
 
45
        if (!a && !b) return 0;
 
46
        if (!a) return 2;
 
47
        if (!b) return 1;
 
48
        
 
49
        lena=strlen(a);
 
50
        lenb=strlen(b);
 
51
        
 
52
        for (i=0; (i<lena) && (i<lenb); i++) {
 
53
                if (a[i]>b[i]) {
 
54
                        return 2;
 
55
                } else if (a[i]<b[i]) {
 
56
                        return 1;
 
57
                }
 
58
        }
 
59
        
 
60
        if (lena>lenb) return 1;
 
61
        else if (lenb>lena) return 2;
 
62
        return 0;
 
63
}
 
64
 
 
65
void LADSPA_Class::init() {
 
66
        char *start, *end, *buffer;
 
67
 
 
68
#ifdef USE_LRDF
 
69
        /* Scanning every dir in path */
 
70
        start = globals.lrdf_path;
 
71
        
 
72
        while (*start != '\0') {
 
73
                end = start;
 
74
                while (*end != ':' && *end != '\0') end++;
 
75
    
 
76
                buffer = (char *) malloc(1 + end - start);
 
77
                if (end > start) strncpy(buffer, start, end - start);
 
78
                        
 
79
                buffer[end - start] = '\0';
 
80
                LADSPA_Class::scandir(buffer);
 
81
                free (buffer); 
 
82
    
 
83
        start = end;
 
84
        if (*start == ':') start++;
 
85
        }
 
86
        
 
87
        if (rdf_files.size() > 0) {
 
88
                char *uris[rdf_files.size()+1];
 
89
                std::list <char *> :: iterator i;
 
90
                int t;
 
91
                
 
92
                for (i=rdf_files.begin(), t=0; i!=rdf_files.end(); i++, t++) {
 
93
                        uris[t]=(*i);
 
94
                }
 
95
                uris[t]=NULL;
 
96
                
 
97
                lrdf_init();
 
98
        
 
99
                if (lrdf_read_files((const char **) uris)) {
 
100
                        tX_error("liblrdf had problems reading the rdf files - cannot provide structured menu");
 
101
                        liblrdf_error=true;
 
102
        }
 
103
#endif
 
104
                root=new LADSPA_Class("http://ladspa.org/ontology#Plugin");
 
105
#ifdef USE_LRDF         
 
106
                lrdf_cleanup();
 
107
        } else {
 
108
                tX_error("No RDF files found");
 
109
        }
 
110
#endif
 
111
        
 
112
        unclassified=new LADSPA_Class();
 
113
        /* This is the last class to accpet all plugins not accepted by other classes. */
 
114
        root->subclasses.push_back(unclassified);
 
115
}
 
116
 
 
117
void LADSPA_Class::scandir(char *dirname) {
 
118
        int dirlen=strlen(dirname);
 
119
        int needslash=0;
 
120
        DIR * dir;
 
121
        struct dirent * entry;
 
122
        char *filename;
 
123
        
 
124
        if (!dirlen) { tX_error("LADSPA_Class::scandir() Empty directory name"); return; };
 
125
 
 
126
        if (dirname[dirlen - 1] != '/') needslash=1;
 
127
        
 
128
        dir = opendir(dirname);
 
129
        
 
130
        if (!dir) { tX_error("LADSPA_Class::scandir() couldn't access directory \"%s\"", dirname); return; };
 
131
        
 
132
        while (1) {
 
133
                entry=readdir(dir);             
 
134
                if (!entry) { closedir(dir); return; }
 
135
                
 
136
                if ((strcmp(entry->d_name, ".")==0) ||
 
137
                        (strcmp(entry->d_name, "..")==0)) continue;
 
138
                
 
139
                filename = (char *) malloc (dirlen + strlen(entry->d_name) + 10 + needslash);
 
140
                
 
141
                strcpy(filename, "file:");
 
142
                strcat(filename, dirname);
 
143
                if (needslash) strcat(filename, "/");
 
144
                strcat(filename, entry->d_name);
 
145
                
 
146
                tX_debug("Found RDF file: %s", filename);
 
147
                rdf_files.push_back(filename);          
 
148
        }       
 
149
}
 
150
 
 
151
void LADSPA_Class::insert_class(LADSPA_Class *cls) {
 
152
        std::list <LADSPA_Class *> :: iterator i;
 
153
        
 
154
        for (i=subclasses.begin(); i!=subclasses.end(); i++) {
 
155
                LADSPA_Class *a_class=(*i);
 
156
                int res=compare(cls->label, a_class->label);
 
157
                
 
158
                if (res < 2) {
 
159
                        subclasses.insert(i, cls);
 
160
                        return;
 
161
                }
 
162
        }
 
163
        
 
164
        subclasses.push_back(cls);
 
165
}
 
166
 
 
167
LADSPA_Class :: LADSPA_Class (char *uri) : label(NULL), accept_all(false) {
 
168
#ifdef USE_LRDF 
 
169
        lrdf_uris *ulist;
 
170
        char *urilabel;
 
171
        int i;
 
172
 
 
173
        if (!liblrdf_error) {
 
174
                urilabel=lrdf_get_label(uri);
 
175
                
 
176
                if (urilabel) {
 
177
                        label=strdup(urilabel);
 
178
                }
 
179
                
 
180
                /* Finding subclasses... */
 
181
                ulist = lrdf_get_subclasses(uri);
 
182
                
 
183
                for (i = 0; ulist && i < ulist->count; i++) {
 
184
                        insert_class(new LADSPA_Class(ulist->items[i]));
 
185
                }
 
186
        
 
187
                lrdf_free_uris(ulist);
 
188
        
 
189
                /* Finding instances... */
 
190
                ulist=lrdf_get_instances(uri);
 
191
                
 
192
                for (i = 0; ulist && i < ulist->count; i++) {
 
193
                        registered_ids.push_back(lrdf_get_uid(ulist->items[i]));
 
194
                }
 
195
        
 
196
                lrdf_free_uris(ulist);
 
197
        }
 
198
#endif  
 
199
}
 
200
 
 
201
LADSPA_Class :: LADSPA_Class() : label("Unclassified"), accept_all(true) {
 
202
}
 
203
 
 
204
bool LADSPA_Class :: add_plugin(LADSPA_Plugin *plugin) {
 
205
        return root->add_plugin_instance(plugin);
 
206
}
 
207
 
 
208
bool LADSPA_Class :: add_plugin_instance(LADSPA_Plugin *plugin) {
 
209
        if (accept_all) {
 
210
                insert_plugin(plugin);
 
211
                return true;
 
212
        }
 
213
        
 
214
        long id=plugin->getUniqueID();
 
215
        std::list <long> :: iterator i;
 
216
        
 
217
        /* Is this plugin an instance of this class? */
 
218
        
 
219
        for (i=registered_ids.begin(); i!=registered_ids.end(); i++) {
 
220
                if ((*i)==id) {
 
221
                        /* The plugin belongs to this class... */
 
222
                        insert_plugin(plugin);
 
223
                        return true;
 
224
                }
 
225
        }
 
226
        
 
227
        /* Try to insert the plugin in subclasses */
 
228
        std::list <LADSPA_Class *> :: iterator cls;
 
229
        
 
230
        for (cls=subclasses.begin(); cls!=subclasses.end(); cls++) {
 
231
                LADSPA_Class *lrdf_class=(*cls);
 
232
                
 
233
                if (lrdf_class->add_plugin_instance(plugin)) return true;
 
234
        }
 
235
        
 
236
        /* Giving up... */
 
237
        
 
238
        return false;
 
239
}
 
240
 
 
241
void LADSPA_Class::insert_plugin(LADSPA_Plugin *plugin) {
 
242
        std::list <LADSPA_Plugin *> :: iterator i;
 
243
        
 
244
        for (i=plugins.begin(); i!=plugins.end(); i++) {
 
245
                LADSPA_Plugin *a_plug=(*i);
 
246
                int res=compare(plugin->getName(), a_plug->getName());
 
247
                
 
248
                if (res < 2) {
 
249
                        plugins.insert(i, plugin);
 
250
                        return;
 
251
                }
 
252
        }
 
253
        
 
254
        plugins.push_back(plugin);
 
255
}
 
256
 
 
257
void LADSPA_Class::list(char *buffer) {
 
258
        strcat(buffer, "\t");
 
259
        
 
260
        printf("%s class %s {\n", buffer, label);
 
261
 
 
262
        std::list <LADSPA_Plugin *> :: iterator i;      
 
263
        
 
264
        for (i=plugins.begin(); i!=plugins.end(); i++) {
 
265
                printf("%s - plugin: %s\n", buffer, (*i)->getName());
 
266
        }
 
267
        
 
268
        std::list <LADSPA_Class *> :: iterator c;
 
269
        
 
270
        for (c=subclasses.begin(); c!=subclasses.end(); c++) (*c)->list(buffer);
 
271
        
 
272
        printf("%s}\n", buffer);
 
273
        
 
274
        buffer[strlen(buffer)-1]=0;
 
275
}
 
276
 
 
277
void LADSPA_Class::dump() {
 
278
        char buffer[256]="";
 
279
        root->list(buffer);
 
280
}
 
281
 
 
282
static void menu_callback(GtkWidget *wid, LADSPA_Plugin *plugin) {
 
283
        vtt_class *vtt=LADSPA_Class::get_current_vtt();
 
284
        
 
285
        if (vtt) {
 
286
                vtt->add_effect(plugin);
 
287
        } else {
 
288
                tX_error("LADSPA_Class::menu_callback() no vtt");
 
289
        }
 
290
}
 
291
 
 
292
 
 
293
GtkWidget * LADSPA_Class :: get_menu() {
 
294
        std::list <LADSPA_Class *> :: iterator cls;
 
295
        GtkWidget *menu=gtk_menu_new();
 
296
        GtkWidget *item;
 
297
        
 
298
        for (cls=subclasses.begin(); cls!=subclasses.end(); cls++) {
 
299
                LADSPA_Class *c=(*cls);
 
300
                
 
301
                if (c->plugins.size() || c->subclasses.size()) {
 
302
                        item=gtk_menu_item_new_with_label(c->label);
 
303
                        GtkWidget *submenu=c->get_menu();
 
304
                        gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
 
305
                        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
 
306
                        gtk_widget_show(item);
 
307
                }
 
308
        }
 
309
        
 
310
        if (subclasses.size() && plugins.size()) {
 
311
                item = gtk_menu_item_new();
 
312
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
 
313
                gtk_widget_set_sensitive (item, FALSE);
 
314
                gtk_widget_show (item);
 
315
        }
 
316
        
 
317
        std::list <LADSPA_Plugin *> :: iterator plugin;
 
318
        
 
319
        for (plugin=plugins.begin(); plugin != plugins.end(); plugin++) {
 
320
                char buffer[512];
 
321
                LADSPA_Plugin *p=(*plugin);
 
322
                
 
323
                sprintf(buffer, "%s - (%s, %li)", p->getName(), p->getLabel(), p->getUniqueID());
 
324
                item=gtk_menu_item_new_with_label(buffer);
 
325
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
 
326
                g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(menu_callback), p);             
 
327
                gtk_widget_show(item);
 
328
        }
 
329
        
 
330
        return menu;
 
331
}
 
332
 
 
333
GtkWidget * LADSPA_Class :: get_ladspa_menu() {
 
334
        return root->get_menu();
 
335
}