[4dad63]: kwave / PluginManager.cpp Maximize Restore History

Download this file

PluginManager.cpp    634 lines (534 with data), 19.0 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/***************************************************************************
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 <pthread.h>
#include "qobject.h"
#include <kglobal.h>
#include <kconfig.h>
#include <kmainwindow.h>
#include <kstddirs.h>
#include <kmessagebox.h>
#include <klocale.h>
#include "mt/TSS_Object.h"
#include "mt/ThreadCondition.h"
#include "mt/ThreadsafeX11Guard.h"
#include "libkwave/Parser.h"
#include "libkwave/LineParser.h"
#include "libkwave/FileLoader.h"
#include "libkwave/MultiTrackReader.h"
#include "libkwave/MultiTrackWriter.h"
#include "libkwave/SampleReader.h"
#include "libkwave/SampleWriter.h"
#include "mt/SignalProxy.h"
#include "libgui/KwavePlugin.h"
#include "libgui/PluginContext.h"
#include "KwaveApp.h"
#include "TopWidget.h"
#include "SignalManager.h"
#include "UndoAction.h"
#include "UndoModifyAction.h"
#include "UndoTransactionGuard.h"
#include "PluginManager.h"
//****************************************************************************
//****************************************************************************
//class PluginGarbageCollector: public Thread
//{
//public:
// virtual void run();
//
//slots:
// void remove(KwavePlugin *plugin);
//
//private:
// QQueue<Plugin> m_queue;
// SignalProxy1<KwavePlugin *> m_spx_plugin;
//}
//
//void PluginGarbageCollector::run()
//{
// KwavePlugin *plugin;
// do {
// plugin = m_spx_plugin.dequeue();
// if (!plugin) {
// // dequeued a null plugin -> signal to shut down
// debug("PluginGarbageCollector: shutting down...");
// break;
// }
//
// // wait until the plugin's thread is down
//
// // delete the plugin
//
// // remove it's handle
//
// } while (plugin);
//}
// static initializer
QMap<QString, QString> PluginManager::m_plugin_files;
//****************************************************************************
PluginManager::PluginManager(TopWidget &parent)
:m_spx_name_changed(this, SLOT(emitNameChanged())),
m_spx_command(this, SLOT(forwardCommand())),
m_top_widget(parent)
{
}
//****************************************************************************
bool PluginManager::isOK()
{
return true;
}
//****************************************************************************
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(%s)",
p->name().data());
pluginClosed(p, true);
} else {
debug("PluginManager::~PluginManager(): removing null plugin");
m_loaded_plugins.removeRef(p);
}
}
}
//***************************************************************************
void PluginManager::loadAllPlugins()
{
// try to load all plugins
// NOTE: this also makes it resident if it is persistent
QMap<QString, QString>::Iterator it;
for (it=m_plugin_files.begin(); it != m_plugin_files.end(); ++it) {
QString name = it.key();
KwavePlugin *plugin = loadPlugin(name);
if (plugin) {
// remove it again if it is not persistent
if (!plugin->isPersistent()) pluginClosed(plugin, true);
} else {
// loading failed => remove it from the list
warning("PluginManager::loadAllPlugins(): removing '%s' "\
"from list", name.data());
m_plugin_files.remove(name);
}
}
}
//**********************************************************
KwavePlugin *PluginManager::loadPlugin(const QString &name)
{
// first find out if the plugin is already loaded AND persistent
// or unique
QListIterator<KwavePlugin> it(m_loaded_plugins);
for ( ; it.current(); ++it ) {
KwavePlugin *p = it.current();
if (p->name() == name) {
if (p->isPersistent()) {
// persistent -> return reference only
debug("PluginManager::loadPlugin(): returning reference "\
"to persistent plugin '%s'", name.data());
return p;
}
if (p->isUnique()) {
// prevent from re-loading of a unique plugin
warning("PluginManager::loadPlugin(): attempt to re-load "\
"unique plugin '%s'", name.data());
return 0;
}
}
}
// show a warning and abort if the plugin is unknown
if (!(m_plugin_files.contains(name))) {
QString message =i18n("oops, plugin '%1' is unknown or invalid!");
message = message.arg(name);
KMessageBox::error(&m_top_widget, message,
i18n("error on loading plugin"));
return 0;
}
QString &filename = m_plugin_files[name];
// try to get the file handle of the plugin's binary
void *handle = dlopen(filename, RTLD_NOW);
if (!handle) {
QString message = i18n("unable to load the file \n'%1'\n"\
" that contains the plugin '%2' !");
message = message.arg(filename).arg(name);
KMessageBox::error(&m_top_widget, message,
i18n("error on loading plugin"));
return 0;
}
// get the loader function
KwavePlugin *(*plugin_loader)(PluginContext *c) = 0;
#ifdef HAVE_CPLUS_MANGLE_OPNAME
// would be fine, but needs libiberty
const char *sym_version = cplus_mangle_opname("const char *", 0);
const char *sym_author = cplus_mangle_opname("author", 0);
const char *sym_loader = cplus_mangle_opname("load(PluginContext *)",0);
#else
// hardcoded, might fail on some systems :-(
const char *sym_version = "version";
const char *sym_author = "author";
const char *sym_loader = "load__FR13PluginContext";
#endif
// get the plugin's author
const char *author = "";
const char **pauthor = (const char **)dlsym(handle, sym_author);
ASSERT(pauthor);
if (pauthor) author=*pauthor;
if (!author) author = i18n("(unknown)");
// get the plugin's version string
const char *version = "";
const char **pver = (const char **)dlsym(handle, sym_version);
ASSERT(pver);
if (pver) version=*pver;
if (!version) version = i18n("(unknown)");
plugin_loader = (KwavePlugin *(*)(PluginContext *))dlsym(handle, sym_loader);
ASSERT(plugin_loader);
if (!plugin_loader) {
// plugin is null, out of memory or not found
warning("PluginManager::loadPlugin(): "\
"plugin does not contain a loader, "\
"maybe it is damaged or the wrong version?");
dlclose(handle);
return 0;
}
PluginContext *context = new PluginContext(
m_top_widget.getKwaveApp(),
*this,
0, // LabelManager *label_mgr,
0, // MenuManager *menu_mgr,
m_top_widget,
handle,
name,
version,
author
);
ASSERT(context);
if (!context) {
warning("PluginManager::loadPlugin(): out of memory");
dlclose(handle);
return 0;
}
// call the loader function to create an instance
KwavePlugin *plugin = (*plugin_loader)(context);
ASSERT(plugin);
if (!plugin) {
warning("PluginManager::loadPlugin(): out of memory");
dlclose(handle);
return 0;
}
// append the plugin into our list of plugins
m_loaded_plugins.append(plugin);
// connect all signals
connectPlugin(plugin);
// get the last settings and call the "load" function
// now the plugin is present and loaded
QStringList last_params = loadPluginDefaults(name, plugin->version());
plugin->load(last_params);
return plugin;
}
//***************************************************************************
void PluginManager::executePlugin(const QString &name, QStringList *params)
{
QString command;
// load the plugin
KwavePlugin* plugin = loadPlugin(name);
if (!plugin) return;
if (params) {
// parameters were specified -> call directly
// without setup dialog
int result = plugin->start(*params);
// 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;
}
if (plugin && (result >= 0)) {
plugin->execute(*params);
// now it's the the task of the plugin's thread
// to clean up after execution
plugin = 0;
}
} else {
// load previous parameters from config
QStringList last_params = loadPluginDefaults(name, plugin->version());
// 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.
// store parameters for the next time
savePluginDefaults(name, plugin->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()); // ###
}
}
// now the plugin is no longer needed here, so delete it
// if it has not already been detached
if (plugin && !plugin->isPersistent()) pluginClosed(plugin, true);
// emit a command, let the toplevel window (and macro recorder) get
// it and call us again later...
if (command.length()) emit sigCommand(command);
}
//***************************************************************************
void PluginManager::setupPlugin(const QString &name)
{
// load the plugin
KwavePlugin* plugin = loadPlugin(name);
if (!plugin) return;
// now the plugin is present and loaded
QStringList last_params = loadPluginDefaults(name, plugin->version());
// call the plugin's setup function
QStringList *params = plugin->setup(last_params);
if (params) {
// we have a non-zero parameter list, so
// the setup function has not been aborted.
savePluginDefaults(name, plugin->version(), *params);
delete params;
}
// now the plugin is no longer needed here, so delete it
// if it has not already been detached and is not persistent
if (!plugin->isPersistent()) pluginClosed(plugin, true);
}
//***************************************************************************
QStringList PluginManager::loadPluginDefaults(const QString &name,
const QString &version)
{
QString def_version;
QString section("plugin ");
QStringList list;
section += name;
KConfig *cfg = KGlobal::config();
ASSERT(cfg);
if (!cfg) return list;
cfg->sync();
cfg->setGroup(section);
def_version = cfg->readEntry("version");
if (!def_version.length()) {
return list;
}
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());
return list;
}
list = cfg->readListEntry("defaults", ',');
return list;
}
//***************************************************************************
void PluginManager::savePluginDefaults(const QString &name,
const QString &version,
QStringList &params)
{
QString def_version;
QString section("plugin ");
section += name;
KConfig *cfg = KGlobal::config();
ASSERT(cfg);
if (!cfg) return;
cfg->sync();
cfg->setGroup(section);
cfg->writeEntry("version", version);
cfg->writeEntry("defaults", params);
cfg->sync();
}
//***************************************************************************
unsigned int PluginManager::signalLength()
{
return m_top_widget.signalManager().length();
}
//***************************************************************************
unsigned int PluginManager::signalRate()
{
return m_top_widget.signalManager().rate();
}
//***************************************************************************
const QArray<unsigned int> PluginManager::selectedTracks()
{
return m_top_widget.signalManager().selectedTracks();
}
//***************************************************************************
unsigned int PluginManager::selectionStart()
{
return m_top_widget.signalManager().selection().first();
}
//***************************************************************************
unsigned int PluginManager::selectionEnd()
{
return m_top_widget.signalManager().selection().last();
}
//***************************************************************************
QBitmap *PluginManager::overview(unsigned int width, unsigned int height,
unsigned int offset, unsigned int length)
{
return m_top_widget.signalManager().overview(width,height,offset,length);
}
//***************************************************************************
SampleWriter *PluginManager::openSampleWriter(unsigned int track,
InsertMode mode, unsigned int left, unsigned int right)
{
SignalManager &manager = m_top_widget.signalManager();
return manager.openSampleWriter(track, mode, left, right, true);
}
//***************************************************************************
void PluginManager::openMultiTrackReader(MultiTrackReader &readers,
const QArray<unsigned int> &track_list,
unsigned int first, unsigned int last)
{
SignalManager &manager = m_top_widget.signalManager();
manager.openMultiTrackReader(readers, track_list, first, last);
}
//***************************************************************************
void PluginManager::openMultiTrackWriter(MultiTrackWriter &writers,
const QArray<unsigned int> &track_list, InsertMode mode,
unsigned int left, unsigned int right)
{
SignalManager &manager = m_top_widget.signalManager();
manager.openMultiTrackWriter(writers, track_list, mode, left, right);
}
//***************************************************************************
void PluginManager::openMultiTrackWriter(MultiTrackWriter &writers,
InsertMode mode)
{
SignalManager &manager = m_top_widget.signalManager();
QArray<unsigned int> tracks = manager.selectedTracks();
unsigned int left = selectionStart();
unsigned int right = selectionEnd();
if (left == right) {
left = 0;
right = signalLength()-1;
}
openMultiTrackWriter(writers, tracks, mode, left, right);
}
//***************************************************************************
PlaybackController &PluginManager::playbackController()
{
return m_top_widget.signalManager().playbackController();
}
//***************************************************************************
void PluginManager::enqueueCommand(const QString &command)
{
m_spx_command.enqueue(command);
}
//***************************************************************************
void PluginManager::forwardCommand()
{
const QString *command = m_spx_command.dequeue();
if (command) {
m_top_widget.executeCommand(*command);
delete command;
}
}
//***************************************************************************
void PluginManager::pluginClosed(KwavePlugin *p, bool remove)
{
ASSERT(p);
ASSERT(!m_loaded_plugins.isEmpty());
if (p) {
// void *h = p->handle();
// 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()
{
const QString *name = m_spx_name_changed.dequeue();
ASSERT(name);
if (name) {
emit sigSignalNameChanged(*name);
delete name;
}
}
//****************************************************************************
void PluginManager::setSignalName(const QString &name)
{
m_spx_name_changed.enqueue(name);
}
//***************************************************************************
void PluginManager::findPlugins()
{
QStringList files = KGlobal::dirs()->findAllResources("data",
"kwave/plugins/*", false, true);
QStringList::Iterator it;
for (it=files.begin(); it != files.end(); ++it) {
QString file = *it;
void *handle = dlopen(file, RTLD_NOW);
if (handle) {
const char **name = (const char **)dlsym(handle, "name");
const char **version = (const char **)dlsym(handle, "version");
const char **author = (const char **)dlsym(handle, "author");
// skip it if something is missing or null
if (!name || !version || !author) continue;
if (!*name || !*version || !*author) continue;
m_plugin_files.insert(*name, file);
printf("%14s %5s written by %s", *name, *version, *author);
dlclose (handle);
} else {
printf("error in '%s':\n\t %s\n", file.data(),
dlerror());
}
printf("\n");
}
printf("--- \n found %d plugins\n", m_plugin_files.count());
}
//****************************************************************************
//****************************************************************************
/* end of PluginManager.cpp */