From 9c113b25a44acffe924f9645aa6b05cb0c21fff3 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Wed, 3 Apr 2024 22:03:58 -0400 Subject: [PATCH] Allow turning the API server on/off at runtime --- common/api/api_server.cpp | 30 ++++++++++++++++++++++++++---- common/eda_base_frame.cpp | 13 +++++++++++++ include/api/api_server.h | 4 ++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/common/api/api_server.cpp b/common/api/api_server.cpp index 9c7318f82e..9469dea269 100644 --- a/common/api/api_server.cpp +++ b/common/api/api_server.cpp @@ -50,12 +50,29 @@ KICAD_API_SERVER::KICAD_API_SERVER() : m_token( KIID().AsStdString() ), m_readyToReply( false ) { + m_commonHandler = std::make_unique(); + RegisterHandler( m_commonHandler.get() ); + if( !Pgm().GetCommonSettings()->m_Api.enable_server ) { wxLogTrace( traceApi, "Server: disabled by user preferences." ); return; } + Start(); +} + + +KICAD_API_SERVER::~KICAD_API_SERVER() +{ +} + + +void KICAD_API_SERVER::Start() +{ + if( Running() ) + return; + wxFileName socket; #ifdef __WXMAC__ socket.AssignDir( wxS( "/tmp" ) ); @@ -88,9 +105,6 @@ KICAD_API_SERVER::KICAD_API_SERVER() : fmt::format( "ipc://{}", socket.GetFullPath().ToStdString() ) ); m_server->SetCallback( [&]( std::string* aRequest ) { onApiRequest( aRequest ); } ); - m_commonHandler = std::make_unique(); - RegisterHandler( m_commonHandler.get() ); - m_logFilePath.AssignDir( PATHS::GetLogsPath() ); m_logFilePath.SetName( s_logFileName ); @@ -106,8 +120,16 @@ KICAD_API_SERVER::KICAD_API_SERVER() : } -KICAD_API_SERVER::~KICAD_API_SERVER() +void KICAD_API_SERVER::Stop() { + if( !Running() ) + return; + + wxLogTrace( traceApi, "Stopping server" ); + Unbind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this ); + + m_server->Stop(); + m_server.reset( nullptr ); } diff --git a/common/eda_base_frame.cpp b/common/eda_base_frame.cpp index 8f5629716e..9d52c36552 100644 --- a/common/eda_base_frame.cpp +++ b/common/eda_base_frame.cpp @@ -73,6 +73,10 @@ #include #include +#ifdef KICAD_IPC_API +#include +#endif + // Minimum window size static const wxSize minSizeLookup( FRAME_T aFrameType, wxWindow* aWindow ) @@ -549,6 +553,15 @@ void EDA_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVars COMMON_SETTINGS* settings = Pgm().GetCommonSettings(); +#ifdef KICAD_IPC_API + bool running = Pgm().GetApiServer().Running(); + + if( running && !settings->m_Api.enable_server ) + Pgm().GetApiServer().Stop(); + else if( !running && settings->m_Api.enable_server ) + Pgm().GetApiServer().Start(); +#endif + if( m_fileHistory ) { int historySize = settings->m_System.file_history_size; diff --git a/include/api/api_server.h b/include/api/api_server.h index 11bec3a14a..6c6a9c9289 100644 --- a/include/api/api_server.h +++ b/include/api/api_server.h @@ -46,6 +46,10 @@ public: ~KICAD_API_SERVER(); + void Start(); + + void Stop(); + bool Running() const; /**