Skip to content

Commit 2b4183d

Browse files
committed
API - separate Init and Boot methods
Previously the API Init method initialised all the paths, then booted the daemon and started the OSC server. However, the GUI now uses the API's path resolution to determine the config directory so it can read the audio config files and send the appropriate flag to the daemon to enable/disable audio inputs. Given the Init method both set paths and booted the daemon, this wasn't possible. Therefore, we Init just initialises the paths and other state, and Boot boots the servers. We can now Init, get the paths, figure out whether scsynth needs inputs, then pass that to the new Boot method.
1 parent cf46071 commit 2b4183d

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

app/api/include/api/sonicpi_api.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@ class OscServer;
4040

4141
enum class APIInitResult
4242
{
43-
Successful,
44-
TerminalError,
45-
ScsynthBootError,
46-
HomePathNotWritableError
43+
Successful,
44+
TerminalError,
45+
HomePathNotWritableError
46+
};
47+
48+
enum class APIBootResult
49+
{
50+
Successful,
51+
TerminalError,
52+
ScsynthBootError,
4753
};
4854

4955
enum class BootDaemonInitResult
5056
{
51-
Successful,
52-
TerminalError,
53-
ScsynthBootError
57+
Successful,
58+
TerminalError,
59+
ScsynthBootError
5460
};
5561

5662
enum class SonicPiPath
@@ -282,7 +288,8 @@ class SonicPiAPI
282288
virtual ~SonicPiAPI();
283289

284290
// Start the ruby server, connect the ports, find the paths.
285-
virtual APIInitResult Init(const fs::path& rootPath, bool noScsynthInputs = false);
291+
virtual APIInitResult Init(const fs::path& rootPath);
292+
virtual APIBootResult Boot(bool noScsynthInputs = false);
286293

287294
virtual void StartClearLogsScript();
288295

app/api/src/sonicpi_api.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,10 @@ bool SonicPiAPI::PingUntilServerCreated()
592592
}
593593

594594
// Initialize the API with the sonic pi root path (the folder containing the app folder)
595-
APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
595+
APIInitResult SonicPiAPI::Init(const fs::path& root)
596596
{
597-
598-
m_token = -1;
599-
m_osc_mtx.lock();
597+
m_token = -1;
598+
m_osc_mtx.lock();
600599

601600
if (m_state == State::Created)
602601
{
@@ -652,14 +651,20 @@ APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
652651
}
653652

654653
if (m_homeDirWriteable) {
655-
LOG(INFO, "Home dir writable: ");
654+
656655
} else {
656+
LOG(INFO, "Home dir not writable ");
657657
return APIInitResult::HomePathNotWritableError;
658658
}
659659

660-
661660
EnsurePathsAreCanonical();
662-
StartClearLogsScript();
661+
m_osc_mtx.unlock();
662+
return APIInitResult::Successful;
663+
}
664+
665+
APIBootResult SonicPiAPI::Boot(bool noScsynthInputs)
666+
{
667+
m_osc_mtx.lock();
663668

664669
// Setup redirection of log from this app to our log file
665670
// stdout into ~/.sonic-pi/log/gui.log
@@ -670,6 +675,8 @@ APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
670675
std::cout.rdbuf(m_stdlog.rdbuf());
671676
}
672677

678+
StartClearLogsScript();
679+
673680
LOG(INFO, "Starting...");
674681

675682
// Start again, shutdown if we fail init
@@ -694,9 +701,9 @@ APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
694701
LOG(INFO, "Attempting to start Boot Daemon failed....";)
695702
m_osc_mtx.unlock();
696703
if (boot_daemon_res == BootDaemonInitResult::ScsynthBootError) {
697-
return APIInitResult::ScsynthBootError;
704+
return APIBootResult::ScsynthBootError;
698705
} else {
699-
return APIInitResult::TerminalError;
706+
return APIBootResult::TerminalError;
700707
}
701708
}
702709

@@ -705,7 +712,7 @@ APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
705712
{
706713
LOG(INFO, "Attempting to start OSC Server failed....");
707714
m_osc_mtx.unlock();
708-
return APIInitResult::TerminalError;
715+
return APIBootResult::TerminalError;
709716
}
710717

711718
LOG(INFO, "API Init Started...");
@@ -720,7 +727,7 @@ APIInitResult SonicPiAPI::Init(const fs::path& root, bool noScsynthInputs)
720727
PingUntilServerCreated();
721728
});
722729

723-
return APIInitResult::Successful;
730+
return APIBootResult::Successful;
724731
}
725732

726733
bool SonicPiAPI::InitializePaths(const fs::path& root)

app/gui/imgui/app.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void start_sonic_pi()
107107
sonic.spClient = std::make_shared<SPClient>();
108108
sonic.spApi = std::make_shared<SonicPiAPI>(sonic.spClient.get(), APIProtocol::UDP, LogOption::File);
109109
sonic.spApi->Init(fs::path(APP_INSTALL_ROOT) / "..");
110+
sonic.spApi->Boot();
110111
}
111112

112113
bool sync_sonic_pi()

app/gui/qt/mainwindow.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,32 @@ MainWindow::MainWindow(QApplication& app, QSplashScreen* splash)
136136
latest_version_num = 0;
137137

138138
bool startupOK = false;
139-
bool noScsynthInputs = !piSettings->enable_scsynth_inputs;
140-
APIInitResult init_success = m_spAPI->Init(rootPath().toStdString(), noScsynthInputs);
139+
140+
APIInitResult init_success = m_spAPI->Init(rootPath().toStdString());
141+
141142

142143
if(init_success == APIInitResult::Successful) {
143-
std::cout << "[GUI] - API Init successful" << std::endl;
144-
} else if (init_success == APIInitResult::ScsynthBootError) {
145-
std::cout << "[GUI] - API Scsynth Boot Failed" << std::endl;
146-
scsynthBootError();
147144
} else if (init_success == APIInitResult::HomePathNotWritableError) {
148145
std::cout << "[GUI] - API HomePath Not Writable" << std::endl;
149146
homeDirWriteError();
150147
} else {
151148
std::cout << "[GUI] - API Init failed" << std::endl;
152149
}
153150

154-
QString settings_path = sonicPiConfigPath() + QDir::separator() + "gui-settings.ini";
155-
156-
gui_settings = new QSettings(settings_path, QSettings::IniFormat);
157-
readSettings();
158151
initPaths();
152+
readSettings();
153+
bool noScsynthInputs = !piSettings->enable_scsynth_inputs;
154+
APIBootResult boot_success = m_spAPI->Boot(noScsynthInputs);
155+
156+
if(boot_success == APIBootResult::Successful) {
157+
std::cout << "[GUI] - API Boot successful" << std::endl;
158+
} else if (boot_success == APIBootResult::ScsynthBootError) {
159+
std::cout << "[GUI] - API Scsynth Boot Failed" << std::endl;
160+
scsynthBootError();
161+
} else {
162+
std::cout << "[GUI] - API Boot failed" << std::endl;
163+
}
164+
159165

160166
const QRect rect = this->geometry();
161167
m_appWindowSizeRect = std::make_shared<QRect>(rect);
@@ -271,6 +277,10 @@ MainWindow::MainWindow(QApplication& app, QSplashScreen* splash)
271277

272278
void MainWindow::initPaths()
273279
{
280+
281+
QString settings_path = sonicPiConfigPath() + QDir::separator() + "gui-settings.ini";
282+
gui_settings = new QSettings(settings_path, QSettings::IniFormat);
283+
274284
QString root_path = rootPath();
275285

276286
qt_app_theme_path = QDir::toNativeSeparators(root_path + "/app/gui/qt/theme/app.qss");

0 commit comments

Comments
 (0)