From 65cd6a3f2a415a48f42bc59c942c1bf5cacf2bb7 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 15 Aug 2023 22:46:50 -0400 Subject: [PATCH] we cannot cleanly restart with a new plugin through loading. must relaunch. --- tools/lammps-gui/lammpswrapper.cpp | 5 +++- tools/lammps-gui/preferences.cpp | 37 +++++++++++++++++++++--------- tools/lammps-gui/preferences.h | 2 ++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/tools/lammps-gui/lammpswrapper.cpp b/tools/lammps-gui/lammpswrapper.cpp index 8bc6173752..ff5a4f2213 100644 --- a/tools/lammps-gui/lammpswrapper.cpp +++ b/tools/lammps-gui/lammpswrapper.cpp @@ -228,7 +228,10 @@ bool LammpsWrapper::has_plugin() const bool LammpsWrapper::load_lib(const char *libfile) { - if (plugin_handle) liblammpsplugin_release((liblammpsplugin_t *)plugin_handle); + if (plugin_handle) { + close(); + liblammpsplugin_release((liblammpsplugin_t *)plugin_handle); + } plugin_handle = liblammpsplugin_load(libfile); if (!plugin_handle) return false; if (((liblammpsplugin_t *)plugin_handle)->abiversion != LAMMPSPLUGIN_ABI_VERSION) { diff --git a/tools/lammps-gui/preferences.cpp b/tools/lammps-gui/preferences.cpp index abee4eb6bb..2c20a23514 100644 --- a/tools/lammps-gui/preferences.cpp +++ b/tools/lammps-gui/preferences.cpp @@ -44,10 +44,20 @@ #include #endif +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#define execv(exe, args) _execv(exe, args) +#else +#include +#endif + Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) : QDialog(parent), tabWidget(new QTabWidget), buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel)), - settings(new QSettings), lammps(_lammps) + settings(new QSettings), lammps(_lammps), need_relaunch(false) { tabWidget->addTab(new GeneralTab(settings, lammps), "&General Settings"); tabWidget->addTab(new AcceleratorTab(settings, lammps), "&Accelerators"); @@ -141,6 +151,16 @@ void Preferences::accept() if (box) settings->setValue("viewlog", box->isChecked()); box = tabWidget->findChild("viewchart"); if (box) settings->setValue("viewchart", box->isChecked()); + + if (need_relaunch) { + QMessageBox msg(QMessageBox::Information, QString("Relaunching LAMMPS-GUI"), + QString("LAMMPS library plugin path was changed.\n" + "LAMMPS-GUI must be relaunched."), + QMessageBox::Ok); + msg.exec(); + execv(QCoreApplication::applicationFilePath().toStdString().c_str(), nullptr); + fprintf(stderr, "after relaunch\n"); + } QDialog::accept(); } @@ -258,16 +278,11 @@ void GeneralTab::pluginpath() QFileDialog::getOpenFileName(this, "Select Shared LAMMPS Library to Load", field->text(), "Shared Objects (*.so *.dll *.dylib)"); if (!pluginfile.isEmpty() && pluginfile.contains("liblammps", Qt::CaseSensitive)) { - if (lammps->load_lib(pluginfile.toStdString().c_str())) { - auto canonical = QFileInfo(pluginfile).canonicalFilePath(); - field->setText(pluginfile); - settings->setValue("plugin_path", canonical); - } else { - // plugin did not load cannot continue - settings->remove("plugin_path"); - QMessageBox::critical(this, "Error", "Cannot open LAMMPS shared library file"); - QCoreApplication::quit(); - } + auto canonical = QFileInfo(pluginfile).canonicalFilePath(); + field->setText(pluginfile); + settings->setValue("plugin_path", canonical); + // ugly hack + qobject_cast(parent()->parent()->parent())->need_relaunch = true; } } diff --git a/tools/lammps-gui/preferences.h b/tools/lammps-gui/preferences.h index b5529b0c46..1aa447e7b1 100644 --- a/tools/lammps-gui/preferences.h +++ b/tools/lammps-gui/preferences.h @@ -32,6 +32,8 @@ public: private slots: void accept() override; +public: + bool need_relaunch; private: QTabWidget *tabWidget; QDialogButtonBox *buttonBox;