Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[28d661]: src / PluginManager.cpp Maximize Restore History

Download this file

PluginManager.cpp    464 lines (384 with data), 13.8 kB

  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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/***************************************************************************
PluginManager.cpp - manager class for kwave's plugins
-------------------
begin : Sun Aug 27 2000
copyright : (C) 2000 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <dlfcn.h>
#include <kmsgbox.h>
#include <kapp.h>
#include <kconfig.h>
#include <libkwave/DynamicLoader.h>
#include <libkwave/DialogOperation.h>
#include <libkwave/Parser.h>
#include <libkwave/LineParser.h>
#include <libkwave/Global.h>
#include <libkwave/FileLoader.h>
#include <libkwave/Plugin.h>
#include "mt/SignalProxy.h"
#include "libgui/KwavePlugin.h"
#include "libgui/PluginContext.h"
#include "KwaveApp.h"
#include "TopWidget.h"
#include "SignalManager.h"
#include "PluginManager.h"
extern Global globals;
//****************************************************************************
//****************************************************************************
PluginManager::PluginManager(TopWidget &parent)
:m_top_widget(parent)
{
m_spx_name_changed = new SignalProxy1<const QString>(
this, SLOT(emitNameChanged()));
}
//****************************************************************************
bool PluginManager::isOK()
{
return true;//(spx_name_changed);
}
//****************************************************************************
PluginManager::~PluginManager()
{
// inform all plugins and client windows that we close now
emit sigClosed();
// remove all remaining plugins
while (!m_loaded_plugins.isEmpty()) {
KwavePlugin *p = m_loaded_plugins.last();
if (p) {
// debug("PluginManager::~PluginManager(): closing plugin(%p:%p)",p->getHandle(),p);
pluginClosed(p, true);
} else {
debug("PluginManager::~PluginManager(): removing null plugin");
m_loaded_plugins.removeRef(p);
}
}
}
//**********************************************************
void *PluginManager::loadPlugin(const char *name)
{
/* find the plugin in the global plugin list */
unsigned int index = 0;
bool found = false;
for (index=0; globals.dialogplugins[index]; index++) {
Plugin *p = globals.dialogplugins[index];
if (strcmp(name, p->getName()) == 0) {
found=true;
break;
}
}
/* show a warning and abort if the plugin was not found */
if ((!found) || (globals.dialogplugins[index] == 0)) {
char message[256];
snprintf(message, 256, i18n("oops, plugin '%s' is unknown !"), name);
KMsgBox::message(&m_top_widget,
i18n("error on loading plugin"),
(const char *)&message,
KMsgBox::EXCLAMATION
);
return 0;
}
/* try to get the file handle of the plugin's binary */
void *handle = dlopen(globals.dialogplugins[index]->getFileName(),
RTLD_NOW);
if (!handle) {
char message[256];
snprintf(message, 256, i18n(
"unable to load the file \n'%s'\n that contains the plugin '%s' !"),
globals.dialogplugins[index]->getFileName(), name
);
KMsgBox::message(&m_top_widget,
i18n("error on loading plugin"),
(const char *)&message,
KMsgBox::EXCLAMATION
);
return 0;
}
return handle;
}
//**********************************************************
void PluginManager::executePlugin(const char *name, QStrList *params)
{
QString command;
// load the plugin
void* handle = loadPlugin(name);
if (!handle) return;
KwavePlugin *(*plugin_loader)(PluginContext *c) = 0;
#ifdef HAVE_CPLUS_MANGLE_OPNAME
// would be fine, but needs libiberty
const char *sym_loader = cplus_mangle_opname("load(PluginContext *)",0);
const char *sym_version = cplus_mangle_opname("const char *)", 0);
#else
// hardcoded, fails on some systems :-(
const char *sym_loader = "load__FR13PluginContext";
const char *sym_version = "version";
#endif
const char *version = "";
const char **pver = (const char **)dlsym(handle, sym_version);
ASSERT(pver);
if (pver) version=*pver;
if (!version) version = "?";
plugin_loader = (KwavePlugin *(*)(PluginContext *))dlsym(handle, sym_loader);
ASSERT(plugin_loader);
if (plugin_loader) {
PluginContext *context = new PluginContext(
m_top_widget.getKwaveApp(),
*this,
0, // LabelManager *label_mgr,
0, // MenuManager *menu_mgr,
m_top_widget,
handle
);
ASSERT(context);
if (context) {
// call the loader function
debug("PluginManager: loading plugin instance"); // ###
KwavePlugin *plugin = (*plugin_loader)(context);
debug("PluginManager: loading plugin instance done."); // ###
ASSERT(plugin);
if (plugin) {
// append the plugin into our list of plugins
m_loaded_plugins.append(plugin);
// connect all signals
connectPlugin(plugin);
// now the plugin is present and loaded
QStrList *last_params = loadPluginDefaults(name, version);
if (params) {
// parameters were specified -> call directly
// without setup dialog
debug("PluginManager: starting plugin"); // ###
int result = plugin->start(*params);
debug("PluginManager: result: %d", result);// ###
// maybe the start() function has called close() ?
if (m_loaded_plugins.findRef(plugin) == -1) {
debug("PluginManager: plugin closed itself in start()"); // ###
result = -1;
plugin = 0;
handle = 0;
}
if (plugin && (result >= 0)) {
debug("PluginManager: executing plugin"); // ###
plugin->execute(*params);
debug("PluginManager: execute plugin done"); // ###
// now it's the the task of the plugin's thread
// to clean up after execution
plugin = 0;
handle = 0;
context = 0;
};
} else {
// call the plugin's setup function
params = plugin->setup(last_params);
if (params) {
// we have a non-zero parameter list, so
// the setup function has not been aborted.
// Now we can create a command string and
// emit a new command.
savePluginDefaults(name, version, *params);
// We DO NOT call the plugin's "execute"
// function directly, as it should be possible
// to record all function calls in the
// macro recorder
command = "plugin:execute(";
command += name;
for (unsigned int i=0; i < params->count(); i++) {
command += ", ";
command += params->at(i);
}
delete params;
command += ")";
debug("PluginManager: command='%s'",command.data()); // ###
}
}
// previous parameters are no longer needed
if (last_params) delete last_params;
last_params=0;
// now the plugin is no longer needed here, so delete it
// if it has not already been detached
if (plugin) {
pluginClosed(plugin, true);
handle = 0;
}
} else {
// plugin is null, out of memory or not found
warning("plugin = null");
delete context;
}
}
} else warning("%s", dlerror());
if (handle) dlclose(handle);
if (!command.isNull()) emit sigCommand(command);
}
//***************************************************************************
QStrList *PluginManager::loadPluginDefaults(const QString &name,
const QString &version)
{
QString def_version;
QString section("plugin ");
section += name;
KApplication *app = KApplication::getKApplication();
ASSERT(app);
if (!app) return 0;
KConfig *config = app->getConfig();
ASSERT(config);
if (!config) return 0;
config->sync();
config->setGroup(section);
def_version = config->readEntry("version");
if (!def_version.length()) {
debug("PluginManager::loadPluginDefaults: "\
"plugin '%s': no defaults found", name.data());
return 0;
}
if (!(def_version == version)) {
debug("PluginManager::loadPluginDefaults: "\
"plugin '%s': defaults for version '%s' not loaded, found "\
"old ones of version '%s'.", name.data(), version.data(),
def_version.data());
}
QStrList list;
int count = config->readListEntry("defaults", list, ',');
debug("PluginManager::loadPluginDefaults: list with %d entries loaded",
count);
return new QStrList(list);
}
//***************************************************************************
void PluginManager::savePluginDefaults(const QString &name,
const QString &version,
QStrList &params)
{
QString def_version;
QString section("plugin ");
section += name;
KApplication *app = KApplication::getKApplication();
ASSERT(app);
if (!app) return;
KConfig *config = app->getConfig();
ASSERT(config);
if (!config) return;
config->sync();
config->setGroup(section);
config->writeEntry("version", version);
config->writeEntry("defaults", params);
config->sync();
}
//***************************************************************************
unsigned int PluginManager::signalLength()
{
SignalManager *sig = m_top_widget.getSignalManager();
return ((sig) ? sig->getLength() : 0);
}
//***************************************************************************
unsigned int PluginManager::signalRate()
{
SignalManager *sig = m_top_widget.getSignalManager();
return ((sig) ? sig->getRate() : 0);
}
//***************************************************************************
const QArray<unsigned int> PluginManager::selectedChannels()
{
const QArray<unsigned int> empty;
SignalManager *sig = m_top_widget.getSignalManager();
return ((sig) ? (sig->selectedChannels()) : (empty));
}
//***************************************************************************
unsigned int PluginManager::selectionStart()
{
SignalManager *sig = m_top_widget.getSignalManager();
return (sig) ? sig->getLMarker() : 0;
}
//***************************************************************************
unsigned int PluginManager::selectionEnd()
{
SignalManager *sig = m_top_widget.getSignalManager();
return (sig) ? sig->getRMarker() : 0;
}
//***************************************************************************
int PluginManager::singleSample(unsigned int channel, unsigned int offset)
{
SignalManager *sig = m_top_widget.getSignalManager();
return (sig) ? sig->singleSample(channel, offset) : 0;
}
//***************************************************************************
int PluginManager::averageSample(unsigned int offset,
const QArray<unsigned int> *channels)
{
SignalManager *sig = m_top_widget.getSignalManager();
return (sig) ? sig->averageSample(offset, channels) : 0;
}
//***************************************************************************
QBitmap *PluginManager::overview(unsigned int width, unsigned int height,
unsigned int offset, unsigned int length)
{
SignalManager *sig = m_top_widget.getSignalManager();
return (sig) ? sig->overview(width, height, offset, length) : 0;
}
//***************************************************************************
void PluginManager::pluginClosed(KwavePlugin *p, bool remove)
{
ASSERT(p);
ASSERT(!m_loaded_plugins.isEmpty());
if (p) {
// void *h = p->getHandle();
// disconnect the signals to avoid recursion
disconnectPlugin(p);
m_loaded_plugins.setAutoDelete(false);
m_loaded_plugins.removeRef(p);
if (remove) {
debug("PluginManager::pluginClosed(%p): deleting",p);
delete p;
// if (h) dlclose(h);
}
}
debug("PluginManager::pluginClosed(): done");
}
//****************************************************************************
void PluginManager::connectPlugin(KwavePlugin *plugin)
{
ASSERT(plugin);
if (!plugin) return;
connect(this, SIGNAL(sigClosed()),
plugin, SLOT(close()));
connect(plugin, SIGNAL(sigClosed(KwavePlugin *,bool)),
this, SLOT(pluginClosed(KwavePlugin *,bool)));
}
//****************************************************************************
void PluginManager::disconnectPlugin(KwavePlugin *plugin)
{
ASSERT(plugin);
if (!plugin) return;
disconnect(this, SIGNAL(sigClosed()),
plugin, SLOT(close()));
disconnect(plugin, SIGNAL(sigClosed(KwavePlugin *,bool)),
this, SLOT(pluginClosed(KwavePlugin *,bool)));
}
//****************************************************************************
void PluginManager::emitNameChanged()
{
ASSERT(m_spx_name_changed);
if (!m_spx_name_changed) return;
const QString *name = m_spx_name_changed->dequeue();
ASSERT(name);
if (name) {
emit sigSignalNameChanged(*name);
delete name;
}
}
//****************************************************************************
void PluginManager::setSignalName(const QString &name)
{
ASSERT(m_spx_name_changed);
if (!m_spx_name_changed) return;
m_spx_name_changed->enqueue(name);
}
//****************************************************************************
//****************************************************************************
/* end of PluginManager.cpp */