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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
|
/***************************************************************************
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 "config.h"
#include <errno.h>
#include <unistd.h>
#include <new>
#include <QApplication>
#include <QDir>
#include <QLatin1Char>
#include <QLibrary>
#include <QLibraryInfo>
#include <QMutableListIterator>
#include <QtGlobal>
#include <QVariantList>
#include <KAboutData>
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KMainWindow>
#include <KPluginFactory>
#include <KPluginMetaData>
#include <KSharedConfig>
#include "libkwave/MessageBox.h"
#include "libkwave/MultiPlaybackSink.h"
#include "libkwave/PlayBackDevice.h"
#include "libkwave/PlaybackDeviceFactory.h"
#include "libkwave/Plugin.h"
#include "libkwave/PluginManager.h"
#include "libkwave/SignalManager.h"
#include "libkwave/Utils.h"
#include "libkwave/Writer.h"
#include "libkwave/undo/UndoAction.h"
#include "libkwave/undo/UndoModifyAction.h"
#include "libkwave/undo/UndoTransactionGuard.h"
//***************************************************************************
// static initializers
QMap<QString, Kwave::PluginManager::PluginModule>
Kwave::PluginManager::m_plugin_modules;
Kwave::PluginManager *Kwave::PluginManager::m_active_instance = nullptr;
//***************************************************************************
Kwave::PluginManager::PluginManager(QWidget *parent,
Kwave::SignalManager &signal_manager)
:m_plugin_instances(),
m_running_plugins(),
m_parent_widget(parent),
m_signal_manager(signal_manager),
m_view_manager(nullptr)
{
}
//***************************************************************************
Kwave::PluginManager::~PluginManager()
{
// inform all plugins and client windows that we close now
emit sigClosed();
// wait until all plugins are really closed
this->sync();
// give all plugins that still are loaded the chance to do some cleanups
// or unregistration tasks. Ideally this should also trigger a "release"
// of these remaining plugins, so that afterwards we have no more
// plugin instances left.
while (!m_plugin_instances.isEmpty()) {
KwavePluginPointer p = m_plugin_instances.takeLast();
Q_ASSERT(p);
if (p) p->unload();
}
// this should make the cleanup handlers run (deferred delete)
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
// release all loaded modules
for (QMap<QString, PluginModule>::iterator it(m_plugin_modules.begin());
it != m_plugin_modules.end(); )
{
// const QString &name = it.key();
PluginModule &p = it.value();
p.m_use_count--;
// qDebug("PluginManager: releasing module '%s' [refcnt=%d]",
// DBG(name), p.m_use_count);
if (p.m_use_count == 0) {
// take out the pointer to the loadable module
KPluginFactory *factory = p.m_factory;
p.m_factory = nullptr;
// remove the module from the list
it = m_plugin_modules.erase(it);
// now the handle of the shared object can be released too
delete factory;
} else {
// still in use
++it;
}
}
// we are no longer the active instance
if (m_active_instance == this)
m_active_instance = nullptr;
}
//***************************************************************************
bool Kwave::PluginManager::loadAllPlugins()
{
// Try to load all plugins. This has to be called only once per
// instance of the main window!
// NOTE: this also gives each plugin the chance to stay in memory
// if necessary (e.g. for codecs)
for (QMap<QString, PluginModule>::iterator it(m_plugin_modules.begin());
it != m_plugin_modules.end(); ++it)
{
const QString &name = it.key();
KwavePluginPointer plugin = createPluginInstance(name);
if (plugin) {
// qDebug("PluginManager::loadAllPlugins(): plugin '%s'",
// DBG(plugin->name()));
// get the last settings and call the "load" function
// now the plugin is present and loaded
QStringList last_params = defaultParams(name);
plugin->load(last_params);
// reduce use count again, we loaded the plugin only to give
// it a chance to register some service if necessary (e.g. a
// codec)
// Most plugins fall back to use count zero and will be
// deleted again.
plugin->release();
} else {
// loading failed => remove it from the list
qWarning("PluginManager::loadAllPlugins(): removing '%s' "
"from list", DBG(name));
m_plugin_modules.remove(name);
}
}
return !m_plugin_modules.isEmpty();
}
//***************************************************************************
void Kwave::PluginManager::stopAllPlugins()
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
if (!m_plugin_instances.isEmpty()) {
foreach (const KwavePluginPointer &plugin, m_plugin_instances)
if (plugin && plugin->isRunning())
plugin->stop() ;
}
sync();
}
//***************************************************************************
Kwave::Plugin *Kwave::PluginManager::createPluginInstance(const QString &name)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
// show an error message and abort if the plugin is unknown
if (!(m_plugin_modules.contains(name))) {
Kwave::MessageBox::error(m_parent_widget,
i18n("The plugin '%1' is unknown or invalid.", name),
i18n("Error On Loading Plugin"));
return nullptr;
}
PluginModule &info = m_plugin_modules[name];
// qDebug("loadPlugin(%s) [module use count=%d]",
// DBG(name), info.m_use_count);
KPluginFactory *factory = info.m_factory;
Q_ASSERT(factory);
// call the loader function to create an instance
QVariantList args;
args << info.m_name;
args << info.m_description;
Kwave::Plugin *plugin = factory->create<Kwave::Plugin>(this, args);
Q_ASSERT(plugin);
if (!plugin) {
qWarning("PluginManager::loadPlugin('%s'): out of memory", DBG(name));
return nullptr;
}
// now we have a newly created plugin, the use count is 1
// append to our list of loaded plugins
m_plugin_instances.append(plugin);
// connect all necessary signals/slots
connectPlugin(plugin);
return plugin;
}
//***************************************************************************
int Kwave::PluginManager::executePlugin(const QString &name,
QStringList *params)
{
QString command;
int result = 0;
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
// synchronize: wait until any currently running plugins are done
this->sync();
// load the new plugin
KwavePluginPointer plugin = createPluginInstance(name);
if (!plugin) return -ENOMEM;
if (params) {
// parameters were specified -> call directly
// without setup dialog
result = plugin->start(*params);
// maybe the start() function has called close() ?
if (!m_plugin_instances.contains(plugin)) {
qDebug("PluginManager: plugin closed itself in start()");
result = -1;
plugin = nullptr;
}
if (plugin && (result >= 0)) {
plugin->execute(*params);
}
} else {
// load previous parameters from config
QStringList last_params = defaultParams(name);
// 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, *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;
foreach (const QString &p, *params)
command += _(", ") + p;
delete params;
command += _(")");
// qDebug("PluginManager: command='%s'",command.data());
}
}
// now the plugin is no longer needed here, release it
if (plugin) plugin->release();
// emit a command, let the toplevel window (and macro recorder) get
// it and call us again later...
if (command.length()) emit sigCommand(command);
return result;
}
//***************************************************************************
bool Kwave::PluginManager::canClose()
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
if (!m_plugin_instances.isEmpty()) {
foreach (const KwavePluginPointer &plugin, m_plugin_instances)
if (plugin && !plugin->canClose()) return false;
}
return true;
}
//***************************************************************************
bool Kwave::PluginManager::onePluginRunning()
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
if (!m_plugin_instances.isEmpty()) {
foreach (const KwavePluginPointer &plugin, m_plugin_instances)
if (plugin && plugin->isRunning()) return true;
}
return false;
}
//***************************************************************************
void Kwave::PluginManager::sync()
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
// this triggers all kinds of garbage collector (objects queued for
// deletion through obj->deleteLater()
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
// wait until all plugins have finished their work...
while (onePluginRunning()) {
Kwave::yield();
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
usleep(100000);
}
}
//***************************************************************************
int Kwave::PluginManager::setupPlugin(const QString &name,
const QStringList ¶ms)
{
// load the plugin
Kwave::Plugin *plugin = createPluginInstance(name);
if (!plugin) return -ENOMEM;
// now the plugin is present and loaded
QStringList prev_params = (params.isEmpty()) ?
defaultParams(name) : params;
// call the plugins' setup function
QStringList *new_params = plugin->setup(prev_params);
if (new_params) {
// we have a non-zero parameter list, so
// the setup function has not been aborted.
savePluginDefaults(name, *new_params);
delete new_params;
} else {
plugin->release();
return 1;
}
plugin->release();
return 0;
}
//***************************************************************************
QStringList Kwave::PluginManager::defaultParams(const QString &name)
{
QString def_version;
QString section = _("plugin ");
QStringList list;
section += name;
// get the plugin version
if (!m_plugin_modules.contains(name)) return list;
const PluginModule &info = m_plugin_modules[name];
QString version = info.m_version;
Q_ASSERT(KSharedConfig::openConfig());
if (!KSharedConfig::openConfig()) return list;
KConfigGroup cfg = KSharedConfig::openConfig()->group(section);
cfg.sync();
def_version = cfg.readEntry("version");
if (!def_version.length()) {
return list;
}
if (!(def_version == version)) {
qDebug("PluginManager::defaultParams: "
"plugin '%s': defaults for version '%s' not loaded, found "
"old ones of version '%s'.",
DBG(name), DBG(version), DBG(def_version));
// delete the old settings
cfg.deleteEntry("version");
cfg.deleteEntry("defaults");
return list;
}
list = cfg.readEntry("defaults").split(QLatin1Char(','));
return list;
}
//***************************************************************************
void Kwave::PluginManager::savePluginDefaults(const QString &name,
QStringList ¶ms)
{
// get the plugin version
if (!m_plugin_modules.contains(name)) return;
const PluginModule &info = m_plugin_modules[name];
QString version = info.m_version;
QString section = _("plugin ");
section += name;
Q_ASSERT(KSharedConfig::openConfig());
if (!KSharedConfig::openConfig()) return;
KConfigGroup cfg = KSharedConfig::openConfig()->group(section);
cfg.sync();
cfg.writeEntry("version", version);
cfg.writeEntry("defaults", params.join(QLatin1Char(',')));
cfg.sync();
}
//***************************************************************************
sample_index_t Kwave::PluginManager::signalLength()
{
return m_signal_manager.length();
}
//***************************************************************************
double Kwave::PluginManager::signalRate()
{
return m_signal_manager.rate();
}
//***************************************************************************
sample_index_t Kwave::PluginManager::selectionStart()
{
return m_signal_manager.selection().first();
}
//***************************************************************************
sample_index_t Kwave::PluginManager::selectionEnd()
{
return m_signal_manager.selection().last();
}
//***************************************************************************
void Kwave::PluginManager::selectRange(sample_index_t offset,
sample_index_t length)
{
m_signal_manager.selectRange(offset, length);
}
//***************************************************************************
Kwave::SampleSink *Kwave::PluginManager::openMultiTrackPlayback(
unsigned int tracks,
const Kwave::PlayBackParam *playback_params
)
{
Kwave::PlayBackDevice *device =
m_signal_manager.playbackController().openDevice(
tracks, playback_params);
if (!device) return nullptr;
// create the multi track playback sink
Kwave::SampleSink *sink = new(std::nothrow)
Kwave::MultiPlaybackSink(tracks, device);
Q_ASSERT(sink);
return sink;
}
//***************************************************************************
Kwave::PlaybackController &Kwave::PluginManager::playbackController()
{
return m_signal_manager.playbackController();
}
//***************************************************************************
void Kwave::PluginManager::insertView(Kwave::SignalView *view,
QWidget *controls)
{
if (m_view_manager)
m_view_manager->insertView(view, controls);
}
//***************************************************************************
void Kwave::PluginManager::registerViewManager(Kwave::ViewManager *view_manager)
{
Q_ASSERT(!view_manager || !m_view_manager);
m_view_manager = view_manager;
}
//***************************************************************************
void Kwave::PluginManager::enqueueCommand(const QString &command)
{
emit sigCommand(command);
}
//***************************************************************************
void Kwave::PluginManager::signalClosed()
{
emit sigClosed();
}
//***************************************************************************
void Kwave::PluginManager::pluginClosed(Kwave::Plugin *p)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
Q_ASSERT(p);
if (!p) return;
// disconnect the signals to avoid recursion
disconnectPlugin(p);
if (m_plugin_instances.contains(p))
m_plugin_instances.removeAll(p);
// schedule the deferred delete/unload of the plugin
p->deleteLater();
}
//***************************************************************************
void Kwave::PluginManager::pluginStarted(Kwave::Plugin *p)
{
Q_ASSERT(p);
if (!p) return;
// the plugin is running -> increase the usage count in order to
// prevent our lists from containing invalid entries
p->use();
// add the plugin to the list of running plugins
m_running_plugins.append(p);
}
//***************************************************************************
void Kwave::PluginManager::pluginDone(Kwave::Plugin *p)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
Q_ASSERT(p);
if (!p) return;
// remove the plugin from the list of running plugins
m_running_plugins.removeAll(p);
// release the plugin, at least we do no longer need it
p->release();
}
//***************************************************************************
void Kwave::PluginManager::connectPlugin(Kwave::Plugin *plugin)
{
Q_ASSERT(plugin);
if (!plugin) return;
connect(this, SIGNAL(sigClosed()),
plugin, SLOT(close()));
connect(plugin, SIGNAL(sigClosed(Kwave::Plugin*)),
this, SLOT(pluginClosed(Kwave::Plugin*)),
Qt::QueuedConnection);
connect(plugin, SIGNAL(sigRunning(Kwave::Plugin*)),
this, SLOT(pluginStarted(Kwave::Plugin*)),
Qt::DirectConnection);
connect(plugin, SIGNAL(sigDone(Kwave::Plugin*)),
this, SLOT(pluginDone(Kwave::Plugin*)),
Qt::QueuedConnection);
}
//***************************************************************************
void Kwave::PluginManager::disconnectPlugin(Kwave::Plugin *plugin)
{
Q_ASSERT(plugin);
if (!plugin) return;
disconnect(plugin, SIGNAL(sigDone(Kwave::Plugin*)),
this, SLOT(pluginDone(Kwave::Plugin*)));
disconnect(plugin, SIGNAL(sigRunning(Kwave::Plugin*)),
this, SLOT(pluginStarted(Kwave::Plugin*)));
disconnect(this, SIGNAL(sigClosed()),
plugin, SLOT(close()));
disconnect(plugin, SIGNAL(sigClosed(Kwave::Plugin*)),
this, SLOT(pluginClosed(Kwave::Plugin*)));
}
//***************************************************************************
void Kwave::PluginManager::setSignalName(const QString &name)
{
emit sigSignalNameChanged(name);
}
//***************************************************************************
void Kwave::PluginManager::searchPluginModules()
{
if (!m_plugin_modules.isEmpty()) {
// this is not the first call -> increment module use count only
for (QMap<QString, PluginModule>::iterator it(m_plugin_modules.begin());
it != m_plugin_modules.end(); ++it)
{
it.value().m_use_count++;
}
return;
}
QVector<KPluginMetaData> plugins_meta_data =
KPluginMetaData::findPlugins(_("kwave"));
foreach (const KPluginMetaData &i, plugins_meta_data) {
QString library = i.fileName();
QString description = i.name();
QString name = i.pluginId();
QString version_raw = i.version();
QString version;
QString settings;
QString author = i.authors().first().name();
if (version_raw.contains(_(":"))) {
version = version_raw.split(_(":")).at(0);
settings = version_raw.split(_(":")).at(1);
}
// qDebug("file='%s', name='%s', description='%s', binary_version='%s', "
// "settings_version='%s', author='%s'",
// DBG(library), DBG(name), DBG(description), DBG(version),
// DBG(settings), DBG(author)
// );
if ( library.isEmpty() || description.isEmpty() ||
name.isEmpty() || version.isEmpty() ) {
qWarning("plugin '%s' has no library, name or version", DBG(name));
continue;
}
if (version != _(KWAVE_VERSION)) {
qWarning("plugin '%s' has wrong ABI version: '%s' (should be %s)",
DBG(name), DBG(version), KWAVE_VERSION);
continue;
}
KPluginFactory::Result<KPluginFactory> result =
KPluginFactory::loadFactory(i);
if (!result) {
qWarning("plugin '%s': loading failed: '%s'", DBG(name),
DBG(result.errorString));
continue;
}
emit sigProgress(i18n("Loading plugin %1...", name));
QApplication::processEvents();
PluginModule info;
info.m_name = name;
info.m_author = author;
info.m_description = description;
info.m_version = settings;
info.m_factory = result.plugin;
info.m_use_count = 1;
m_plugin_modules.insert(info.m_name, info);
qDebug("%16s %5s written by %s", DBG(name), DBG(settings), DBG(author));
}
qDebug("--- \n found %lld plugins\n", m_plugin_modules.count());
}
//***************************************************************************
const QList<Kwave::PluginManager::PluginModule>
Kwave::PluginManager::pluginInfoList() const
{
return m_plugin_modules.values();
}
//***************************************************************************
void Kwave::PluginManager::migratePluginToActiveContext(Kwave::Plugin *plugin)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
Q_ASSERT(plugin);
if (!plugin) return;
if (m_active_instance == this) return; // nothing to do
Q_ASSERT(m_active_instance);
if (!m_active_instance) return; // should never happen
Kwave::PluginManager *old_mgr = this;
old_mgr->m_plugin_instances.removeAll(plugin);
old_mgr->m_running_plugins.removeAll(plugin);
old_mgr->disconnectPlugin(plugin);
Kwave::PluginManager *new_mgr = m_active_instance;
new_mgr->m_plugin_instances.append(plugin);
new_mgr->m_running_plugins.append(plugin);
new_mgr->connectPlugin(plugin);
plugin->setPluginManager(new_mgr);
}
//***************************************************************************
//***************************************************************************
#include "moc_PluginManager.cpp"
|