Skip to content

Commit

Permalink
More resilient
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Oct 6, 2023
1 parent 4e5ea7a commit 8f2ca52
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 109 deletions.
125 changes: 74 additions & 51 deletions systray/mod-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class AppWindow : public QMainWindow
bool startingUI = false;
bool stoppingHost = false;
bool stoppingUI = false;
bool successfullyStarted = false;
int timerId = 0;

#ifdef Q_OS_WIN
Expand All @@ -89,6 +90,7 @@ class AppWindow : public QMainWindow
connect(ui.b_start, &QPushButton::clicked, this, &AppWindow::start);
connect(ui.b_stop, &QPushButton::clicked, this, &AppWindow::stop);
connect(ui.b_opengui, &QPushButton::clicked, this, &AppWindow::openGui);
connect(ui.b_openuserfiles, &QPushButton::clicked, this, &AppWindow::openUserFilesDir);
connect(ui.gb_logs, &QGroupBox::toggled, this, &AppWindow::showLogs);

const QIcon icon(":/mod-logo.svg");
Expand Down Expand Up @@ -274,13 +276,15 @@ class AppWindow : public QMainWindow
printf("----------- %s %d\n", __FUNCTION__, __LINE__);
QSettings settings;
settings.setValue("FirstRun", false);
settings.setValue("AutoStart", ui.cb_autostart->isChecked());
settings.setValue("Geometry", saveGeometry());
settings.setValue("AudioDevice", ui.cb_device->currentText());
settings.setValue("AudioBufferSize", ui.cb_buffersize->currentText());
settings.setValue("EnableMIDI", ui.gb_midi->isChecked());
settings.setValue("PedalboardsMidiChannel", ui.sp_midi_pb->value());
settings.setValue("SnapshotsMidiChannel", ui.sp_midi_ss->value());
settings.setValue("ShowLogs", ui.gb_logs->isChecked());
settings.setValue("SuccessfullyStarted", successfullyStarted);
}

void loadSettings()
Expand All @@ -302,17 +306,20 @@ class AppWindow : public QMainWindow
}

ui.cb_buffersize->setCurrentIndex(settings.value("AudioBufferSize", "128").toString() == "256" ? 1 : 0);
ui.cb_autostart->setChecked(settings.value("AutoStart", false).toBool());

ui.gb_midi->setChecked(settings.value("EnableMIDI", false).toBool());
ui.sp_midi_pb->setValue(settings.value("PedalboardsMidiChannel", 0).toInt());
ui.sp_midi_pb->setValue(settings.value("SnapshotsMidiChannel", 0).toInt());

successfullyStarted = settings.value("SuccessfullyStarted", false).toBool();

adjustSize();

if (settings.contains("Geometry"))
restoreGeometry(settings.value("Geometry").toByteArray());

if (settings.value("FirstRun", true).toBool() || getenv("KDE_FULL_SESSION") != nullptr)
if (settings.value("FirstRun", true).toBool() || !ui.cb_autostart->isChecked() || !successfullyStarted)
{
setStopped();
QTimer::singleShot(0, this, &QMainWindow::show);
Expand All @@ -328,20 +335,26 @@ class AppWindow : public QMainWindow
void setRunning()
{
printf("----------- %s %d\n", __FUNCTION__, __LINE__);
successfullyStarted = true;
ui.cb_device->setEnabled(false);
ui.cb_buffersize->setEnabled(false);
ui.b_start->setEnabled(false);
ui.b_stop->setEnabled(true);
ui.b_opengui->setEnabled(true);
ui.gb_midi->setEnabled(true);
systray->setToolTip(tr("MOD App: Running"));
systray->showMessage(tr("MOD App"), tr("Running"), QSystemTrayIcon::Information);
}

void setStopped()
{
printf("----------- %s %d\n", __FUNCTION__, __LINE__);
ui.cb_device->setEnabled(true);
ui.cb_buffersize->setEnabled(true);
ui.b_start->setEnabled(true);
ui.b_stop->setEnabled(false);
ui.b_opengui->setEnabled(false);
ui.gb_midi->setEnabled(true);
systray->setToolTip(tr("MOD App: Stopped"));
}

Expand Down Expand Up @@ -418,6 +431,8 @@ private slots:
{
saveSettings();

successfullyStarted = false;

printf("----------- %s %d\n", __FUNCTION__, __LINE__);
if (processHost.state() != QProcess::NotRunning)
{
Expand All @@ -432,60 +447,72 @@ private slots:

const bool midiEnabled = ui.gb_midi->isChecked();

#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
const QString argPre1 = midiEnabled ? "-X" : "-S";
#ifdef Q_OS_WIN
const QString argPre2 = midiEnabled ? "winmme" : "-S";
#else
const QString argPre2 = midiEnabled ? "coremidi" : "-S";
#endif
const QString argPost1;
const QString argPost2;
#else
const QString argPre1;
const QString argPre2;
const QString argPost1 = midiEnabled ? "-X" : "-z";
const QString argPost2 = midiEnabled ? "seqmidi" : "n";
#endif

#ifdef Q_OS_WIN
const QString& inputDev(matchingWasapiInputDevices[ui.cb_device->currentIndex()]);
#endif

const QStringList arguments = {
QStringList arguments = {
"-R",
"-S",
argPre1, argPre2,
"-C",
#ifdef Q_OS_WIN
".\\jack\\jack-session.conf",
#else
"./jack/jack-session.conf",
#endif
"-d",
#if defined(Q_OS_WIN)
"portaudio",
#elif defined(Q_OS_MAC)
"coreaudio",
#else
"alsa",
#endif
"-r", "48000",
"-p",
ui.cb_buffersize->currentIndex() == 0 ? "128" : "256",
};

#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
if (midiEnabled)
{
arguments.append("-X");
#ifdef Q_OS_WIN
inputDev.isEmpty() ? "-d" : "-C",
inputDev.isEmpty() ? ui.cb_device->currentText() : inputDev,
inputDev.isEmpty() ? "-d" : "-P",
ui.cb_device->currentText(),
arguments.append("winmme");
#else
"-d",
ui.cb_device->currentText(),
arguments.append("coremidi");
#endif
argPost1, argPost2
};
}
#endif

arguments.append("-d");
#if defined(Q_OS_WIN)
arguments.append("portaudio");
#elif defined(Q_OS_MAC)
arguments.append("coreaudio");
#else
arguments.append("alsa");
#endif

arguments.append("-r");
arguments.append("48000");

arguments.append("-p");
arguments.append(ui.cb_buffersize->currentIndex() == 0 ? "128" : "256");

arguments.append("-d");
arguments.append(ui.cb_device->currentText());

#ifdef Q_OS_WIN
const QString& inputDev(matchingWasapiInputDevices[ui.cb_device->currentIndex()]);

if (! inputDev.isEmpty())
{
arguments.append("-P");
arguments.append(ui.cb_device->currentText());
arguments.append("-C");
arguments.append(inputDev);
}
#endif

#if !(defined(Q_OS_WIN) || defined(Q_OS_MAC))
if (! midiEnabled)
{
arguments.append("-X");
arguments.append("seqmidi");
}
#endif

processHost.setArguments(arguments);

ui.text_host->appendPlainText("Starting jackd using:");
ui.text_host->appendPlainText(arguments.join(" "));

ui.b_start->setEnabled(false);

startingHost = true;
Expand All @@ -495,17 +522,12 @@ private slots:
void stop()
{
printf("----------- %s %d\n", __FUNCTION__, __LINE__);
if (processHost.state() == QProcess::NotRunning)
{
qWarning() << "stop ignored";
stopUIIfNeeded();
return;
}

ui.b_stop->setEnabled(false);

stoppingUI = true;
processUI.terminate();
stopUIIfNeeded();
stopHostIfNeeded();

ui.b_stop->setEnabled(true);
}

void openGui() const
Expand Down Expand Up @@ -586,6 +608,7 @@ private slots:
printf("----------- %s %d\n", __FUNCTION__, __LINE__);
startingUI = stoppingUI = false;
stopHostIfNeeded();
setStopped();
}

void iconActivated(QSystemTrayIcon::ActivationReason reason)
Expand Down
Loading

0 comments on commit 8f2ca52

Please sign in to comment.