KiCad PCB EDA Suite
kiway.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) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2016 KiCad Developers, see CHANGELOG.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 #ifndef KIWAY_H_
26 #define KIWAY_H_
27 
28 /*
29 
30 The KIWAY and KIFACE classes are used to communicate between various process
31 modules, all residing within a single process. The program modules are either
32 top level like an *.exe or subsidiary like a *.dll. In much of the documentation
33 the term DSO is used to refer to the *.dll portions, that is the term used on
34 linux. But it should be taken to mean DLL on Windows.
35 
36 <p>These are a couple of reasons why this design was chosen:
37 
38 <ol>
39 
40 <li>By using DSOs within a single process, it is not necessary to use IPC.
41 The DSOs can send wxEvents between themselves using wxEvtHandler interfaces in
42 a platform independent way. There can also be function calls from one DSO to
43 another.</li>
44 
45 <li>The use of a number of separately linked DSOs closely resembles the original
46 KiCad program design, consisting of Eeschema and Pcbnew. But it also allows
47 separate compilation and linking of those two DSOs without a ton of inter-DSO
48 dependencies and common data structures. Linking smaller, purpose specific DSOs
49 is thought to be better for maintenance simplicity than a large single link
50 image. </li>
51 
52 <li>By keeping the core functionality in DSOs rather than EXE tops, it becomes
53 possible to re-use the DSOs under different program tops. For example, a DSO
54 named _pcbnew.so can be used under a C++ top or under a python top. Only one
55 CMake target must be defined to build either. Whether that is a separate build
56 or not is not the important thing. Simply having a single CMake target has
57 advantages. (Each builder person will have his/her own intentions relative to
58 use of python or not.) Once a DSO is python capable, it can be driven by any
59 number of python program tops, including demo-ing (automation) and testing
60 separately.</li>
61 
62 
63 </ol>
64 
65 All KiCad source code is UTF8 encoded by law, so make sure your editor is set
66 as such! As such, it is OK to use UTF8 characters:
67 
68 ┏ ┗ ┓ ┛ ━ ┃
69 
70 <pre>
71 
72  ┏━━━process top━━━━━┓
73  ┃ ┃ wxEvent channels
74  ┏━━━━━━━━━━━━━━━━━━━-━[KIWAY project 1]━-━━━━━━━━━━━━━━━━━━━━━━┓
75  ┃ ┃ ┃ ┃
76  ┃ ┏━━━━━━━━━━━━━-━[KIWAY project 2]━-━━━━━━━━━━┓ ┃
77  ┃ ┃ ┃ ┃ ┃ ┃
78  ┃ ┃ ┏━-━[KIWAY project 3]━-━┓ ┃ ┃
79  ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━┛ ┃ ┃ ┃
80  ┃ ┃ ┃ ┃ ┃ ┃
81  ┃ ┃ ┃ ┃ ┃ ┃
82 ┏━━━━━━━━|━━━━━|━━━━━━━━━━━|━━━━━━━━━┓ ┏━━━━━━━━|━━━━━━━━|━━━━━━━━━━━|━━━━━┓
83 ┃ KIFACE ┃ ┃ ┃ ┃ ┃ KIFACE ┃ ┃ ┃ ┃
84 ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃
85 ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃
86 ┃┏━━━━━━━+━┓ ┏━+━━━━━━━┓ ┏━+━━━━━━━┓ ┃ ┃┏━━━━━━━+━┓ ┏━━━━+━━━━┓ ┏━━━━+━━━━┓┃
87 ┃┃wxFrame ┃ ┃wxFrame ┃ ┃wxFrame ┃ ┃ ┃┃wxFrame ┃ ┃wxFrame ┃ ┃wxFrame ┃┃
88 ┃┃project 1┃ ┃project 2┃ ┃project 3┃ ┃ ┃┃project 3┃ ┃project 2┃ ┃project 1┃┃
89 ┃┗━━━━━━━━━┛ ┗━━━━━━━━━┛ ┗━━━━━━━━━┛ ┃ ┃┗━━━━━━━━━┛ ┗━━━━━━━━━┛ ┗━━━━━━━━━┛┃
90 ┃ ┃ ┃ ┃
91 ┃ ┃ ┃ ┃
92 ┗━━━━━━ eeschema DSO ━━━━━━━━━━━━━━━━┛ ┗━━━━━━ pcbnew DSO ━━━━━━━━━━━━━━━━━┛
93 
94 </pre>
95 
96 */
97 
98 
99 #include <wx/event.h>
100 #include <wx/dynlib.h>
101 #include <import_export.h>
102 #include <search_stack.h>
103 #include <project.h>
104 #include <frame_type.h>
105 #include <mail_type.h>
106 #include <ki_exception.h>
107 
108 
109 #define VTBL_ENTRY virtual
110 
111 #define KIFACE_VERSION 1
112 #define KIFACE_GETTER KIFACE_1
113 
114 // The KIFACE acquistion function is declared extern "C" so its name should not
115 // be mangled.
116 #define KIFACE_INSTANCE_NAME_AND_VERSION "KIFACE_1"
117 
118 #ifndef SWIG
119 #if defined(__linux__) || defined(__FreeBSD__)
120  #define LIB_ENV_VAR wxT( "LD_LIBRARY_PATH" )
121 #elif defined(__WXMAC__)
122  #define LIB_ENV_VAR wxT( "DYLD_LIBRARY_PATH" )
123 #elif defined(_WIN32)
124  #define LIB_ENV_VAR wxT( "PATH" )
125 #else
126  #error Platform support missing
127 #endif
128 #endif // SWIG
129 
130 class wxConfigBase;
131 class wxWindow;
132 class wxConfigBase;
133 class PGM_BASE;
134 class KIWAY;
135 class KIWAY_PLAYER;
136 
137 
150 struct KIFACE
151 {
152  // The order of functions establishes the vtable sequence, do not change the
153  // order of functions in this listing unless you recompile all clients of
154  // this interface.
155 
156  virtual ~KIFACE() throw() {}
157 
158 #define KFCTL_STANDALONE (1<<0)
159 #define KFCTL_CPP_PROJECT_SUITE (1<<1)
160 #define KFCTL_PY_PROJECT_SUITE (1<<2)
161 
162 
181  VTBL_ENTRY bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) = 0;
182 
188  VTBL_ENTRY void OnKifaceEnd() = 0;
189 
210  VTBL_ENTRY wxWindow* CreateWindow( wxWindow* aParent, int aClassId,
211  KIWAY* aKIWAY, int aCtlBits = 0 ) = 0;
212 
225  VTBL_ENTRY void* IfaceOrAddress( int aDataId ) = 0;
226 };
227 
228 
257 class KIWAY : public wxEvtHandler
258 {
259  friend struct PGM_SINGLE_TOP; // can use set_kiface()
260 
261 public:
263  enum FACE_T
264  {
272 
274  };
275 
276  ~KIWAY() throw () {}
277 
285  static FACE_T KifaceType( FRAME_T aFrameType );
286 
287  // If you change the vtable, recompile all of KiCad.
288 
294  VTBL_ENTRY KIFACE* KiFACE( FACE_T aFaceId, bool doLoad = true );
295 
314  VTBL_ENTRY KIWAY_PLAYER* Player( FRAME_T aFrameType, bool doCreate = true, KIWAY_PLAYER* aParent = NULL );
315 
324  VTBL_ENTRY bool PlayerClose( FRAME_T aFrameType, bool doForce );
325 
335  VTBL_ENTRY bool PlayersClose( bool doForce );
336 
343  VTBL_ENTRY void ExpressMail( FRAME_T aDestination, MAIL_T aCommand,
344  const std::string& aPayload, wxWindow* aSource = NULL );
345 
352  VTBL_ENTRY PROJECT& Prj() const;
353 
358  VTBL_ENTRY void SetLanguage( int aLanguage );
359 
367 
368  KIWAY( PGM_BASE* aProgram, int aCtlBits, wxFrame* aTop = NULL );
369 
378  void SetTop( wxFrame* aTop );
379 
380  void OnKiwayEnd();
381 
382  bool ProcessEvent( wxEvent& aEvent ) override;
383 
384 private:
385 
387  const wxString dso_search_path( FACE_T aFaceId );
388 
389 #if 0
390  void player_destroy_handler( wxWindowDestroyEvent& event );
392 #endif
393 
394  bool set_kiface( FACE_T aFaceType, KIFACE* aKiface )
395  {
396  if( unsigned( aFaceType ) < unsigned( KIWAY_FACE_COUNT ) )
397  {
398  m_kiface[aFaceType] = aKiface;
399  return true;
400  }
401  return false;
402  }
403 
408  KIWAY_PLAYER* GetPlayerFrame( FRAME_T aFrameType );
409 
412 
414  int m_ctl;
415 
416  wxFrame* m_top; // Usually m_top is the Project manager
417 
418  // a string array ( size KIWAY_PLAYER_COUNT ) to Store the frame name
419  // of PLAYER frames which were run.
420  // A non empty name means only a PLAYER was run at least one time.
421  // It can be closed. Call :
422  // wxWindow::FindWindowByName( m_playerFrameName[aFrameType] )
423  // to know if still exists (or GetPlayerFrame( FRAME_T aFrameType )
424  wxArrayString m_playerFrameName;
425 
426  PROJECT m_project; // do not assume this is here, use Prj().
427 };
428 
429 
430 #ifndef SWIG
431 // provided by single_top.cpp and kicad.cpp;
432 extern KIWAY Kiway;
433 // whereas python launchers: single_top.py and project manager instantiate as a python object
434 #endif
435 
436 
450 typedef KIFACE* KIFACE_GETTER_FUNC( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
451 
452 
453 #ifndef SWIG
454 
456 extern "C" {
457 #if defined(BUILD_KIWAY_DLL)
458  MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
459 #else
460  KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
461 #endif
462 }
463 
464 #endif // SWIG
465 
466 #endif // KIWAY_H_
VTBL_ENTRY bool PlayersClose(bool doForce)
Function PlayersClose calls the KIWAY_PLAYER::Close( bool force ) function on all the windows and if ...
Definition: kiway.cpp:374
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:111
Struct PGM_SINGLE_TOP implements PGM_BASE with its own OnPgmInit() and OnPgmExit().
Definition: single_top.cpp:61
Class PROJECT holds project specific data.
Definition: project.h:56
static FACE_T KifaceType(FRAME_T aFrameType)
Function KifaceType is a simple mapping function which returns the FACE_T which is known to implement...
Definition: kiway.cpp:252
KIWAY_PLAYER * GetPlayerFrame(FRAME_T aFrameType)
Definition: kiway.cpp:293
virtual ~KIFACE()
Definition: kiway.h:156
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:107
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
KIWAY(PGM_BASE *aProgram, int aCtlBits, wxFrame *aTop=NULL)
Definition: kiway.cpp:44
VTBL_ENTRY void OnKifaceEnd()=0
Function OnKifaceEnd is called just once just before the DSO is to be unloaded.
PGM_BASE * m_program
Definition: kiway.h:413
VTBL_ENTRY void SetLanguage(int aLanguage)
Function SetLanguage changes the language and then calls ShowChangedLanguage() on all KIWAY_PLAYERs...
Definition: kiway.cpp:396
~KIWAY()
Definition: kiway.h:276
void OnKiwayEnd()
Definition: kiway.cpp:485
VTBL_ENTRY void * IfaceOrAddress(int aDataId)=0
Function IfaceOrAddress returns a pointer to the requested object.
VTBL_ENTRY void ShowChangedIcons()
Function ShowChangedIcons Calls ShowChangedIcons() on all KIWAY_PLAYERs.
Definition: kiway.cpp:432
#define VTBL_ENTRY
Definition: kiway.h:109
bool set_kiface(FACE_T aFaceType, KIFACE *aKiface)
Definition: kiway.h:394
MAIL_T
Enum MAIL_T is the set of mail types sendable via KIWAY::ExpressMail() and supplied as the aCommand p...
Definition: mail_type.h:37
#define MY_API(rettype)
Definition: import_export.h:55
VTBL_ENTRY wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKIWAY, int aCtlBits=0)=0
Function CreateWindow creates a wxWindow for the current project.
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, KIWAY_PLAYER *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:302
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:144
VTBL_ENTRY bool PlayerClose(FRAME_T aFrameType, bool doForce)
Function PlayerClose calls the KIWAY_PLAYER::Close( bool force ) function on the window and if not ve...
Definition: kiway.cpp:349
bool ProcessEvent(wxEvent &aEvent) override
Definition: kiway.cpp:459
KIFACE * KIFACE_GETTER_FUNC(int *aKIFACEversion, int aKIWAYversion, PGM_BASE *aProgram)
Function Pointer KIFACE_GETTER_FUNC points to the one and only KIFACE export.
Definition: kiway.h:450
PROJECT m_project
Definition: kiway.h:426
int m_ctl
Definition: kiway.h:414
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:257
pcbnew DSO
Definition: kiway.h:266
#define KIFACE_GETTER
Definition: kiway.h:112
const wxString dso_search_path(FACE_T aFaceId)
Get the [path &] name of the DSO holding the requested FACE_T.
Definition: kiway.cpp:96
VTBL_ENTRY KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Function KiFACE returns the KIFACE* given a FACE_T.
Definition: kiway.cpp:150
void SetTop(wxFrame *aTop)
Function SetTop tells this KIWAY about the top most frame in the program and optionally allows it to ...
Definition: kiway.cpp:78
FACE_T
Known KIFACE implementations.
Definition: kiway.h:263
eeschema DSO
Definition: kiway.h:265
int aKIWAYversion
Definition: cvpcb.cpp:116
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, const std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:387
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
static int m_kiface_version[KIWAY_FACE_COUNT]
Definition: kiway.h:411
VTBL_ENTRY bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits)=0
Function OnKifaceStart is called just once shortly after the DSO is loaded.
wxArrayString m_playerFrameName
Definition: kiway.h:424
wxFrame * m_top
Definition: kiway.h:416
int PGM_BASE * aProgram
Definition: pcbnew.cpp:195
static KIFACE * m_kiface[KIWAY_FACE_COUNT]
Definition: kiway.h:410
KIWAY Kiway