KiCad PCB EDA Suite
Loading...
Searching...
No Matches
single_top.cpp
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) 2014 SoftPLC Corporation, Dick Hollenbeck <[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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26/*
27
28 This is a program launcher for a single KIFACE DSO. It only mimics a KIWAY,
29 not actually implements one, since only a single DSO is supported by it.
30
31 It is compiled multiple times, once for each standalone program and as such
32 gets different compiler command line supplied #defines from CMake.
33
34*/
35
36
37#include <typeinfo>
38#include <wx/cmdline.h>
39#include <wx/dialog.h>
40#include <wx/filename.h>
41#include <wx/stdpaths.h>
42#include <wx/snglinst.h>
43#include <wx/html/htmlwin.h>
44
45#include <kiway.h>
46#include <build_version.h>
47#include <pgm_base.h>
48#include <app_monitor.h>
49#include <kiway_player.h>
50#include <macros.h>
51#include <confirm.h>
53
56#include <paths.h>
57
58#include <kiplatform/app.h>
60
61#include <git2.h>
62#include <thread_pool.h>
63
66
67#ifdef KICAD_USE_SENTRY
68#include <sentry.h>
69#endif
70
71#ifdef KICAD_IPC_API
72#include <api/api_server.h>
73#endif
74
75// Only a single KIWAY is supported in this single_top top level component,
76// which is dedicated to loading only a single DSO.
78
79
80// implement a PGM_BASE and a wxApp side by side:
81
85static struct PGM_SINGLE_TOP : public PGM_BASE
86{
87 bool OnPgmInit();
88
89 void OnPgmExit()
90 {
91 // Abort and wait on any background jobs
92 GetKiCadThreadPool().purge();
93 GetKiCadThreadPool().wait();
94
95 Kiway.OnKiwayEnd();
96
97#ifdef KICAD_IPC_API
98 m_api_server.reset();
99#endif
100
102 {
104 m_settings_manager->Save();
105 }
106
107 // Destroy everything in PGM_BASE, especially wxSingleInstanceCheckerImpl
108 // earlier than wxApp and earlier than static destruction would.
110 git_libgit2_shutdown();
111 }
112
113 void MacOpenFile( const wxString& aFileName ) override
114 {
115 wxFileName filename( aFileName );
116
117 if( filename.FileExists() )
118 {
119 #if 0
120 // this pulls in EDA_DRAW_FRAME type info, which we don't want in
121 // the single_top link image.
122 KIWAY_PLAYER* frame = dynamic_cast<KIWAY_PLAYER*>( App().GetTopWindow() );
123 #else
124 KIWAY_PLAYER* frame = (KIWAY_PLAYER*) App().GetTopWindow();
125 #endif
126 if( frame )
127 {
128 if( wxWindow* blocking_win = frame->Kiway().GetBlockingDialog() )
129 blocking_win->Close( true );
130
131 frame->OpenProjectFiles( std::vector<wxString>( 1, aFileName ) );
132 }
133 }
134 }
135
137
138
139// A module to allow Html module initialization/cleanup
140// When a wxHtmlWindow is used *only* in a dll/so module, the Html text is displayed
141// as plain text.
142// This helper class is just used to force wxHtmlWinParser initialization
143// see https://groups.google.com/forum/#!topic/wx-users/FF0zv5qGAT0
144class HtmlModule: public wxModule
145{
146public:
148 virtual bool OnInit() override { AddDependency( CLASSINFO( wxHtmlWinParser ) ); return true; };
149 virtual void OnExit() override {};
150
151private:
153};
154
156
157
158#ifdef NDEBUG
159// Define a custom assertion handler
160void CustomAssertHandler( const wxString& file,
161 int line,
162 const wxString& func,
163 const wxString& cond,
164 const wxString& msg )
165{
166 Pgm().HandleAssert( file, line, func, cond, msg );
167}
168#endif
169
170
175struct APP_SINGLE_TOP : public wxApp
176{
177 APP_SINGLE_TOP() : wxApp()
178 {
179 SetPgm( &program );
180
181 // Init the environment each platform wants
183 }
184
185
186 bool OnInit() override
187 {
188#ifdef NDEBUG
189 // These checks generate extra assert noise
190 wxSizerFlags::DisableConsistencyChecks();
191 wxDISABLE_DEBUG_SUPPORT();
192 wxSetAssertHandler( CustomAssertHandler );
193#endif
194
195 // Perform platform-specific init tasks
196 if( !KIPLATFORM::APP::Init() )
197 return false;
198
199#ifndef DEBUG
200 // Enable logging traces to the console in release build.
201 // This is usually disabled, but it can be useful for users to run to help
202 // debug issues and other problems.
203 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
204 {
205 wxLog::EnableLogging( true );
206 wxLog::SetLogLevel( wxLOG_Trace );
207 }
208#endif
209
210 // Force wxHtmlWinParser initialization when a wxHtmlWindow is used only
211 // in a shared library (.so or .dll file)
212 // Otherwise the Html text is displayed as plain text.
213 HtmlModule html_init;
214
215 try
216 {
217 return program.OnPgmInit();
218 }
219 catch( ... )
220 {
221 Pgm().HandleException( std::current_exception() );
222 }
223
224 program.OnPgmExit();
225
226 return false;
227 }
228
229 int OnExit() override
230 {
231 program.OnPgmExit();
232 return wxApp::OnExit();
233 }
234
235 int OnRun() override
236 {
237 int ret = -1;
238
239 try
240 {
241 ret = wxApp::OnRun();
242 }
243 catch(...)
244 {
245 Pgm().HandleException( std::current_exception() );
246 }
247
248 return ret;
249 }
250
251 int FilterEvent( wxEvent& aEvent ) override
252 {
253 if( aEvent.GetEventType() == wxEVT_SHOW )
254 {
255 wxShowEvent& event = static_cast<wxShowEvent&>( aEvent );
256 wxDialog* dialog = dynamic_cast<wxDialog*>( event.GetEventObject() );
257
258 std::vector<void*>& dlgs = Pgm().m_ModalDialogs;
259
260 if( dialog )
261 {
262 if( event.IsShown() && dialog->IsModal() )
263 {
264 dlgs.push_back( dialog );
265 }
266 // Under GTK, sometimes the modal flag is cleared before hiding
267 else if( !event.IsShown() && !dlgs.empty() )
268 {
269 // If we close the expected dialog, remove it from our stack
270 if( dlgs.back() == dialog )
271 dlgs.pop_back();
272 // If an out-of-order, remove all dialogs added after the closed one
273 else if( auto it = std::find( dlgs.begin(), dlgs.end(), dialog ); it != dlgs.end() )
274 dlgs.erase( it, dlgs.end() );
275 }
276 }
277 }
278
279 return Event_Skip;
280 }
281
282 void OnUnhandledException() override
283 {
284 Pgm().HandleException( std::current_exception(), true );
285 }
286
287#if defined( DEBUG )
295 virtual bool OnExceptionInMainLoop() override
296 {
297 try
298 {
299 throw;
300 }
301 catch( ... )
302 {
303 Pgm().HandleException( std::current_exception() );
304 }
305
306 return false; // continue on. Return false to abort program
307 }
308#endif
309
310#ifdef __WXMAC__
311
319 void MacOpenFile( const wxString& aFileName ) override
320 {
321 Pgm().MacOpenFile( aFileName );
322 }
323
324#endif
325};
326
327IMPLEMENT_APP( APP_SINGLE_TOP )
328
329
331{
332#if defined(DEBUG)
333 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
334
335 if( !wxIsAbsolutePath( absoluteArgv0 ) )
336 {
337 wxLogError( wxT( "No meaningful argv[0]" ) );
338 return false;
339 }
340#endif
341
342 // Initialize the git library before trying to initialize individual programs
343 int gitInit = git_libgit2_init();
344
345 if( gitInit < 0 )
346 {
347 const git_error* err = git_error_last();
348 wxString msg = wxS( "Failed to initialize git library" );
349
350 if( err && err->message )
351 msg += wxS( ": " ) + wxString::FromUTF8( err->message );
352
353 wxLogError( msg );
354 return false;
355 }
356
357 // Not all KiCad applications use the python stuff. skip python init
358 // for these apps.
359 bool skip_python_initialization = false;
360
361#if defined( BITMAP_2_CMP ) || defined( PL_EDITOR ) || defined( GERBVIEW ) || \
362 defined( PCB_CALCULATOR_BUILD )
363 skip_python_initialization = true;
364#endif
365
366 if( !InitPgm( false, skip_python_initialization ) )
367 {
368 // Clean up
369 OnPgmExit();
370 return false;
371 }
372
373#if !defined(BUILD_KIWAY_DLL)
374
375 // Only bitmap2component and pcb_calculator use this code currently, as they
376 // are not split to use single_top as a link image separate from a *.kiface.
377 // i.e. they are single part link images so don't need to load a *.kiface.
378
379 // Get the getter, it is statically linked into this binary image.
380 KIFACE_GETTER_FUNC* ki_getter = &KIFACE_GETTER;
381
382 int kiface_version;
383
384 // Get the KIFACE.
385 KIFACE* kiface = ki_getter( &kiface_version, KIFACE_VERSION, this );
386
387 // Trick the KIWAY into thinking it loaded a KIFACE, by recording the KIFACE
388 // in the KIWAY. It needs to be there for KIWAY::OnKiwayEnd() anyways.
389 Kiway.set_kiface( KIWAY::KifaceType( TOP_FRAME ), kiface );
390#endif
391
392 // Tell the settings manager about the current Kiway
394
396
397#ifdef KICAD_IPC_API
398 // Create the API server thread once the app event loop exists
399 m_api_server = std::make_unique<KICAD_API_SERVER>();
400#endif
401
402 // Use KIWAY to create a top window, which registers its existence also.
403 // "TOP_FRAME" is a macro that is passed on compiler command line from CMake,
404 // and is one of the types in FRAME_T.
405 KIWAY_PLAYER* frame = Kiway.Player( TOP_FRAME, true );
406
407 if( frame == nullptr )
408 {
409 // Clean up
410 OnPgmExit();
411 return false;
412 }
413
414 Kiway.SetTop( frame );
415
416 STARTWIZARD startWizard;
417 startWizard.CheckAndRun( frame );
418
419 // Load library tables after startup wizard
420 GetLibraryManager().LoadGlobalTables();
421
422 // Preload libraries, since for single-top this won't have been done earlier
423 if( KIFACE* topFrame = Kiway.KiFACE( KIWAY::KifaceType( TOP_FRAME ) ) )
424 topFrame->PreloadLibraries( &Kiway );
425
427
428 App().SetTopWindow( frame ); // wxApp gets a face.
429 App().SetAppDisplayName( frame->GetAboutTitle() );
430
431 wxString relaunchDisplayName = frame->GetAboutTitle() + " " + GetMajorMinorVersion();
433
434 // Allocate a slice of time to show the frame and update wxWidgets widgets
435 // (especially setting valid sizes) after creating frame and before calling
436 // OpenProjectFiles() that can update/use some widgets.
437 // The 2 calls to wxSafeYield are needed on wxGTK for best results.
438 wxSafeYield();
439 HideSplash();
440 frame->Show();
441 wxSafeYield();
442
443 // Now after the frame processing, the rest of the positional args are files
444 std::vector<wxString> fileArgs;
445
446
447 static const wxCmdLineEntryDesc desc[] = {
448 { wxCMD_LINE_PARAM, nullptr, nullptr, "File to load", wxCMD_LINE_VAL_STRING,
449 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
450 { wxCMD_LINE_NONE, nullptr, nullptr, nullptr, wxCMD_LINE_VAL_NONE, 0 }
451 };
452
453 wxCmdLineParser parser( App().argc, App().argv );
454 parser.SetDesc( desc );
455 parser.Parse( false );
456
457 if( parser.GetParamCount() )
458 {
459 /*
460 gerbview handles multiple project data files, i.e. gerber files on
461 cmd line. Others currently do not, they handle only one. For common
462 code simplicity we simply pass all the arguments in however, each
463 program module can do with them what they want, ignore, complain
464 whatever. We don't establish policy here, as this is a multi-purpose
465 launcher.
466 */
467
468 for( size_t i = 0; i < parser.GetParamCount(); i++ )
469 fileArgs.push_back( parser.GetParam( i ) );
470
471 // special attention to a single argument: argv[1] (==argSet[0])
472 if( fileArgs.size() == 1 )
473 {
474 wxFileName argv1( fileArgs[0] );
475
476#if defined(PGM_DATA_FILE_EXT)
477 // PGM_DATA_FILE_EXT, if present, may be different for each compile,
478 // it may come from CMake on the compiler command line, but often does not.
479 // This facility is mostly useful for those program footprints
480 // supporting a single argv[1].
481 if( !argv1.GetExt() )
482 argv1.SetExt( wxT( PGM_DATA_FILE_EXT ) );
483#endif
484 argv1.MakeAbsolute();
485
486 fileArgs[0] = argv1.GetFullPath();
487 }
488
489 frame->OpenProjectFiles( fileArgs );
490 }
491
492#ifdef KICAD_IPC_API
493 m_api_server->SetReadyToReply();
494#endif
495
496 return true;
497}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
const wxString & GetAboutTitle() const
virtual bool OnInit() override
virtual void OnExit() override
wxDECLARE_DYNAMIC_CLASS(HtmlModule)
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:294
static FACE_T KifaceType(FRAME_T aFrameType)
A simple mapping function which returns the FACE_T which is known to implement aFrameType.
Definition kiway.cpp:341
wxWindow * GetBlockingDialog()
Gets the window pointer to the blocking dialog (to send it signals)
Definition kiway.cpp:685
static const wxString & GetExecutablePath()
Definition paths.cpp:649
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition pgm_base.cpp:200
void PreloadDesignBlockLibraries(KIWAY *aKiway)
Starts a background job to preload the global and project design block libraries.
Definition pgm_base.cpp:893
virtual void MacOpenFile(const wxString &aFileName)=0
Specific to MacOSX (not used under Linux or Windows).
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition pgm_base.h:412
void Destroy()
Definition pgm_base.cpp:179
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition pgm_base.cpp:316
void HandleException(std::exception_ptr aPtr, bool aUnhandled=false)
A exception handler to be used at the top level if exceptions bubble up that for.
Definition pgm_base.cpp:808
std::vector< void * > m_ModalDialogs
Definition pgm_base.h:393
void HandleAssert(const wxString &aFile, int aLine, const wxString &aFunc, const wxString &aCond, const wxString &aMsg)
A common assert handler to be used between single_top and kicad.
Definition pgm_base.cpp:845
void HideSplash()
Definition pgm_base.cpp:305
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:134
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition pgm_base.cpp:538
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
void CheckAndRun(wxWindow *parent)
This file is part of the common library.
KIFACE * KIFACE_GETTER_FUNC(int *aKIFACEversion, int aKIWAYversion, PGM_BASE *aProgram)
Point to the one and only KIFACE export.
Definition kiway.h:529
#define KIFACE_GETTER
Definition kiway.h:110
#define KIFACE_VERSION
The KIWAY and KIFACE classes are used to communicate between various process modules,...
Definition kiway.h:109
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition kiway.h:162
This file contains miscellaneous commonly used macros and functions.
bool Init()
Perform application-specific initialization tasks.
Definition unix/app.cpp:40
void Init()
Perform environment initialization tasks.
void SetAppDetailsForWindow(wxWindow *aWindow, const wxString &aRelaunchCommand, const wxString &aRelaunchDisplayName)
Sets the relaunch command for taskbar pins, this is intended for Windows.
SETTINGS_MANAGER * GetSettingsManager()
void SetPgm(PGM_BASE *pgm)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
PGM_SINGLE_TOP program
wxIMPLEMENT_DYNAMIC_CLASS(HtmlModule, wxModule)
KIWAY Kiway(KFCTL_STANDALONE)
Implement a bare naked wxApp (so that we don't become dependent on functionality in a wxApp derivativ...
int OnRun() override
int OnExit() override
void OnUnhandledException() override
bool OnInit() override
int FilterEvent(wxEvent &aEvent) override
Implement a participant in the KIWAY alchemy.
Definition kiway.h:155
Implement PGM_BASE with its own OnPgmInit() and OnPgmExit().
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.