KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_plugin_manager.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <deque>
22#include <memory>
23#include <set>
24
25#include <wx/event.h>
26
27#include <api/api_plugin.h>
28#include <frame_type.h>
30#include <kicommon.h>
31
32class REPORTER;
33class wxTimer;
34
36extern const KICOMMON_API wxEventTypeTag<wxCommandEvent> EDA_EVT_PLUGIN_MANAGER_JOB_FINISHED;
37
39extern const KICOMMON_API wxEventTypeTag<wxCommandEvent> EDA_EVT_PLUGIN_AVAILABILITY_CHANGED;
40
45class KICOMMON_API API_PLUGIN_MANAGER : public wxEvtHandler
46{
47public:
48 API_PLUGIN_MANAGER( wxEvtHandler* aParent );
49
56 void ReloadPlugins( std::optional<wxString> aDirectoryToScan = std::nullopt,
57 std::shared_ptr<REPORTER> aReporter = nullptr );
58
64 bool RecreatePluginEnvironment( const wxString& aIdentifier );
65
67
68 void InvokeAction( const wxString& aIdentifier,
69 std::shared_ptr<REPORTER> aReporter = nullptr );
70
82 int InvokeActionSync( const wxString& aIdentifier, std::vector<wxString> aExtraArgs,
83 wxString* aStdout = nullptr, wxString* aStderr = nullptr,
84 std::shared_ptr<REPORTER> aReporter = nullptr );
85
86 bool Busy() const;
87
88 std::optional<const PLUGIN_ACTION*> GetAction( const wxString& aIdentifier );
89
90 std::vector<const PLUGIN_ACTION*> GetActionsForScope( PLUGIN_ACTION_SCOPE aScope );
91
92 std::map<int, wxString>& ButtonBindings( FRAME_T aFrame );
93
94 std::map<int, wxString>& MenuBindings( FRAME_T aFrame );
95
96 std::shared_ptr<REPORTER> GetReporter() { return m_reloadReporter; }
97
98private:
99 wxString pluginName( const wxString& aIdentifier ) const;
100
101 void processPluginDependencies();
102
103 void processNextJob( wxCommandEvent& aEvent );
104
105 bool cancelPluginJobs( const wxString& aIdentifier );
106
107 void recreateCancelledPlugin( const wxString& aIdentifier );
108
109 int doInvokeAction( const wxString& aIdentifier, std::vector<wxString> aExtraArgs,
110 bool aSync = false, wxString* aStdout = nullptr,
111 wxString* aStderr = nullptr,
112 std::shared_ptr<REPORTER> aReporter = nullptr );
113
114 wxEvtHandler* m_parent;
115
116 std::set<std::unique_ptr<API_PLUGIN>, CompareApiPluginIdentifiers> m_plugins;
117
118 std::map<wxString, const API_PLUGIN*> m_pluginsCache;
119
120 std::map<wxString, const PLUGIN_ACTION*> m_actionsCache;
121
123 std::map<wxString, wxString> m_environmentCache;
124
126 std::map<FRAME_T, std::map<int, wxString>> m_buttonBindings;
127
129 std::map<FRAME_T, std::map<int, wxString>> m_menuBindings;
130
131 std::set<wxString> m_readyPlugins;
132
133 std::set<wxString> m_busyPlugins;
134
141
143 std::map<wxString, long> m_runningPids;
144
146 std::set<wxString> m_cancelledPlugins;
147
148 struct JOB
149 {
151 wxString identifier;
152 wxString plugin_path;
153 wxString env_path;
154 };
155
156 std::deque<JOB> m_jobs;
157
158 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_validator;
159
160 std::shared_ptr<REPORTER> m_reloadReporter;
161
162 [[maybe_unused]] long m_lastPid;
163 [[maybe_unused]] wxTimer* m_raiseTimer;
164};
const KICOMMON_API wxEventTypeTag< wxCommandEvent > EDA_EVT_PLUGIN_AVAILABILITY_CHANGED
Notifies other parts of KiCad when plugin availability changes.
const KICOMMON_API wxEventTypeTag< wxCommandEvent > EDA_EVT_PLUGIN_MANAGER_JOB_FINISHED
Event used for marking async plugin setup tasks as done.
std::set< std::unique_ptr< API_PLUGIN >, CompareApiPluginIdentifiers > m_plugins
int InvokeActionSync(const wxString &aIdentifier, std::vector< wxString > aExtraArgs, wxString *aStdout=nullptr, wxString *aStderr=nullptr, std::shared_ptr< REPORTER > aReporter=nullptr)
Invokes an action synchronously, capturing its output.
bool RecreatePluginEnvironment(const wxString &aIdentifier)
Removes the given plugin's virtual environment (if any) and schedules a new one to be created with th...
void ReloadPlugins(std::optional< wxString > aDirectoryToScan=std::nullopt, std::shared_ptr< REPORTER > aReporter=nullptr)
Clears the loaded plugins and actions and re-scans the filesystem to register new ones.
std::map< wxString, wxString > m_environmentCache
Map of plugin identifier to a path for the plugin's virtual environment, if it has one.
std::shared_ptr< REPORTER > m_reloadReporter
void InvokeAction(const wxString &aIdentifier, std::shared_ptr< REPORTER > aReporter=nullptr)
std::set< wxString > m_cancelledPlugins
Plugins whose setup jobs were cancelled and need their environment recreated.
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_validator
std::deque< JOB > m_jobs
std::vector< const PLUGIN_ACTION * > GetActionsForScope(PLUGIN_ACTION_SCOPE aScope)
std::map< FRAME_T, std::map< int, wxString > > m_menuBindings
Map of menu wx item id to action identifier, per frame.
std::map< int, wxString > & ButtonBindings(FRAME_T aFrame)
std::shared_ptr< REPORTER > GetReporter()
std::map< wxString, const API_PLUGIN * > m_pluginsCache
std::optional< const PLUGIN_ACTION * > GetAction(const wxString &aIdentifier)
std::map< FRAME_T, std::map< int, wxString > > m_buttonBindings
Map of button wx item id to action identifier, per frame.
std::set< wxString > m_readyPlugins
std::map< wxString, long > m_runningPids
Map of plugin identifier to the pid of its currently-running setup process, if any.
std::map< wxString, const PLUGIN_ACTION * > m_actionsCache
API_PLUGIN_MANAGER(wxEvtHandler *aParent)
std::set< wxString > m_busyPlugins
std::map< int, wxString > & MenuBindings(FRAME_T aFrame)
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
#define KICOMMON_API
Definition kicommon.h:27
PLUGIN_ACTION_SCOPE
Comparison functor for ensuring API_PLUGINs have unique identifiers.
Definition api_plugin.h:134