3
Copyright (C) 1999 Stefan Westerfeld
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.
22
#include "structure.h"
23
#include "soundserver.h"
27
#include <arts/debug.h>
31
ExecutableStructure::ExecutableStructure()
36
ExecutableStructure::~ExecutableStructure()
38
// to make destructor virtual
39
// stop execution here?
42
void ExecutableStructure::stopExecute()
45
arts_debug("TODO: PORT: freeStructure");
46
//Synthesizer->freeStructure(execID);
50
bool ExecutableStructure::isExecuting()
52
if(!execID) return(false);
53
arts_debug("TODO:PORT:isExecuting()");
55
//return(Synthesizer->isExecuting(execID));
58
bool ExecutableStructure::saveSession(const char *filename)
60
arts_debug("TODO:PORT:saveSession()");
64
ArtsCorba::StringSeq_var data;
65
arts_debug("saveSession");
66
if(Synthesizer->saveSession(execID,true,data))
69
FILE *file = fopen(filename,"w");
70
if(!file) return false;
73
for(i=0;i<data->length();i++) fprintf(file,"%s\n",(char *)(*data)[i]);
83
Structure::Structure() :ExecutableStructure()
88
void Structure::setCanvas(StructureCanvas *canvas)
90
this->canvas = canvas;
93
Structure::~Structure()
96
arts_debug("~Structure (releasing structuredesc from structure)");
99
bool Structure::startExecute()
104
arts_debug("PORT: TODO startExecute()");
105
// just in case synthesis has been halted before,
106
// restart it and hope we'll have enough computing power now
107
//Synthesizer->Reset();
108
//execID = Synthesizer->createStructure(StructureDesc,preferredservers);
112
/* connect to the sound server */
113
Arts::SimpleSoundServer server;
115
server = Arts::Reference("global:Arts_SimpleSoundServer");
119
/* move a copy of the structure to the server, so that there will be
120
no latencies in querying what to connect to what */
121
vector<string> *savePtr = StructureDesc.saveToList();
122
Arts::StructureDesc remoteSD;
123
remoteSD = Arts::DynamicCast(server.createObject("Arts::StructureDesc"));
124
assert(!remoteSD.isNull());
125
remoteSD.loadFromList(*savePtr);
128
/* create a structure builder on the server */
129
Arts::StructureBuilder builder;
130
builder = Arts::DynamicCast(server.createObject("Arts::StructureBuilder"));
132
/* create a local factory - this will enable the builder to create gui qt
133
widgets (which need to reside within an qt application to work) */
135
Arts::LocalFactory factory;
136
builder.addFactory(factory);
138
/* create the structure on the server */
139
structure = Arts::DynamicCast(builder.createObject(remoteSD));
145
void Structure::stopExecute()
146
{ // TODO:PORT: verify this code
147
structure = Arts::SynthModule::null();
150
void Structure::publish()
152
arts_debug("PORT: TODO publish()");
153
//Synthesizer->publishStructureDesc(StructureDesc);
156
bool Structure::valid()
158
return StructureDesc.valid();
161
string Structure::name()
163
return StructureDesc.name();
166
void Structure::rename(const char *newname)
168
StructureDesc.name(newname);
171
bool Structure::save(const char *filename)
173
vector<string> *savePtr = StructureDesc.saveToList();
174
vector<string>& list = *savePtr;
176
FILE *file = fopen(filename,"w");
180
vector<string>::iterator i;
182
for(i=list.begin();i != list.end(); i++)
183
fprintf(file,"%s\n",(*i).c_str());
188
/* tell the server to rescan for structures */
189
Arts::SoundServerV2 server = Arts::Reference("global:Arts_SoundServerV2");
190
if(!server.isNull()) server.checkNewObjects();
195
void Structure::clear()
197
list<StructureComponent *>::iterator ci;
201
for(ci = ComponentList.begin(); ci != ComponentList.end(); ci++)
204
ComponentList.erase(ComponentList.begin(), ComponentList.end());
205
ModuleList.erase(ModuleList.begin(), ModuleList.end());
207
for(ci = ComponentList.begin(); ci != ComponentList.end(); ci++)
208
(*ci)->setSelected(true);
212
// shouldn't do much, now that we deleted every single component of
213
// the structure, but we to it anyway, just to be sure.
214
StructureDesc.clear();
217
void Structure::retrieve(const char *pubname)
219
arts_debug("PORT: TODO: retrieve");
221
arts_debug("retrieve... %s",pubname);
222
ArtsCorba::StructureDesc_var psd = Synthesizer->lookupStructureDesc(pubname);
224
arts_debug("psdlookup ok");
227
arts_debug("starting savetolist");
228
ArtsCorba::StringSeq_var strseq=psd->saveToList();
229
arts_debug("savetolist ok");
230
loadFromList(strseq);
231
arts_debug("loadfromlist ok");
233
arts_debug("retrieve... ok");
237
void Structure::load(const char *filename)
239
ifstream infile(filename);
241
vector<string> strseq;
243
while(getline(infile,line))
244
strseq.push_back(line);
246
loadFromList(strseq);
248
FILE *infile = fopen(filename,"r");
249
ArtsCorba::StringSeq_var strseq = new ArtsCorba::StringSeq;
254
while(fgets(line,1024,infile))
256
// cut eventual CR and/or LFs at the end of the line
257
while(strlen(line) && line[strlen(line)-1] < 14)
258
line[strlen(line)-1] = 0;
261
(*strseq)[i++] = CORBA::string_dup(line);
265
arts_debug(">>loadfromlist...");
266
loadFromList(strseq);
267
arts_debug("<<loadfromlist...");
271
void Structure::loadFromList(const vector<string>& strseq)
275
arts_debug(">>clear");
277
arts_debug("<<clear");
279
StructureDesc.loadFromList(strseq);
281
vector<Arts::ModuleDesc> *modules = StructureDesc.modules();
282
vector<Arts::ModuleDesc>::iterator mi;
284
for(mi=modules->begin(); mi != modules->end(); mi++)
286
Module *m = new Module(*mi,StructureDesc,canvas);
289
ModuleList.push_back(m);
290
ComponentList.push_back(m);
294
vector<Arts::StructurePortDesc> *ports = StructureDesc.ports();
295
vector<Arts::StructurePortDesc>::iterator pi;
297
for(pi=ports->begin(); pi != ports->end(); pi++)
299
StructurePort *p = new StructurePort(*pi,StructureDesc,canvas);
302
ComponentList.push_back(p);
307
Module *Structure::createModule(const Arts::ModuleInfo& minfo)
310
Module *m = new Module(minfo,StructureDesc,canvas);
312
ComponentList.push_back(m);
313
ModuleList.push_back(m);
317
StructurePort *Structure::createStructurePort(const Arts::PortType& type)
318
{ // TODO:PORT: verify this code
320
arts_debug("TODO:PORT:createStructurePort");
322
// portname generation
323
unsigned long portindex = 1,l,baselen;;
327
if(type.direction == Arts::input) {
328
// this is a port where our structure puts its results
329
// so it is an input port, that is named out
330
namebase = "out"; baselen = 3;
332
// this is a port where our structure gets data from
333
// so it is an output port, that is named in
334
namebase = "in"; baselen = 2;
337
vector<Arts::StructurePortDesc> *sps = StructureDesc.ports();
339
for(l=0;l<sps->size();l++) {
340
string thisname = (*sps)[l].name();
341
if(strncmp(thisname.c_str(), namebase.c_str(), baselen) == 0 &&
342
strlen(thisname.c_str()) > baselen)
344
unsigned long index2 = atol(&thisname.c_str()[baselen]);
345
if(index2 >= portindex) portindex = index2+1;
350
sprintf(name,"%s%ld",namebase.c_str(),portindex);
351
arts_debug("new Portname: %s",name);
352
Arts::StructurePortDesc spd =
353
StructureDesc.createStructurePortDesc(type,name);
356
StructurePort *s = new StructurePort(spd,StructureDesc,canvas);
357
ComponentList.push_back(s);
361
list<Module *> *Structure::getModuleList()
366
list<StructureComponent *> *Structure::getComponentList()
368
return(&ComponentList);
371
long Structure::countSelected()
373
list<StructureComponent *>::iterator ci;
376
for(ci=ComponentList.begin();ci!=ComponentList.end();ci++)
377
if((*ci)->selected()) targets++;
382
void Structure::deleteSelected()
384
arts_debug("deleteSelected...");
386
// remove module from the ModuleList
387
list<Module *>::iterator mi;
388
for(mi=ModuleList.begin();mi!=ModuleList.end();)
390
if((*mi)->selected())
392
ModuleList.erase(mi);
393
mi = ModuleList.begin();
398
// and from the ComponentList (the future ;)
400
list<StructureComponent *>::iterator ci = ComponentList.begin();
402
while(ci!=ComponentList.end())
404
if((*ci)->selected())
407
ComponentList.erase(ci);
408
ci = ComponentList.begin();
413
arts_debug("deleteSelected ok.");
416
StructureComponent *Structure::componentAt(long x, long y, bool ignore_selected)
418
list<StructureComponent *>::iterator ci;
420
for(ci=ComponentList.begin();ci!=ComponentList.end();ci++)
422
StructureComponent *c = *ci;
424
if(x >= c->x() && x < c->x()+c->width() &&
425
y >= c->y() && y < c->y()+c->height())
427
if((c->selected() && !ignore_selected) || !c->selected()) return c;