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 class wxTopLevelWindow;
137 
138 
151 struct KIFACE
152 {
153  // The order of functions establishes the vtable sequence, do not change the
154  // order of functions in this listing unless you recompile all clients of
155  // this interface.
156 
157  virtual ~KIFACE() throw() {}
158 
159 #define KFCTL_STANDALONE (1<<0)
160 #define KFCTL_CPP_PROJECT_SUITE (1<<1)
161 #define KFCTL_PY_PROJECT_SUITE (1<<2)
162 
163 
182  VTBL_ENTRY bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) = 0;
183 
189  VTBL_ENTRY void OnKifaceEnd() = 0;
190 
211  VTBL_ENTRY wxWindow* CreateWindow( wxWindow* aParent, int aClassId,
212  KIWAY* aKIWAY, int aCtlBits = 0 ) = 0;
213 
226  VTBL_ENTRY void* IfaceOrAddress( int aDataId ) = 0;
227 };
228 
229 
258 class KIWAY : public wxEvtHandler
259 {
260  friend struct PGM_SINGLE_TOP; // can use set_kiface()
261 
262 public:
264  enum FACE_T
265  {
273 
274  KIWAY_FACE_COUNT
275  };
276 
277  ~KIWAY() throw () {}
278 
286  static FACE_T KifaceType( FRAME_T aFrameType );
287 
288  // If you change the vtable, recompile all of KiCad.
289 
295  VTBL_ENTRY KIFACE* KiFACE( FACE_T aFaceId, bool doLoad = true );
296 
316  VTBL_ENTRY KIWAY_PLAYER* Player( FRAME_T aFrameType, bool doCreate = true,
317  wxTopLevelWindow* aParent = NULL );
318 
327  VTBL_ENTRY bool PlayerClose( FRAME_T aFrameType, bool doForce );
328 
338  VTBL_ENTRY bool PlayersClose( bool doForce );
339 
346  VTBL_ENTRY void ExpressMail( FRAME_T aDestination, MAIL_T aCommand,
347  std::string aPayload, wxWindow* aSource = NULL );
348 
355  VTBL_ENTRY PROJECT& Prj() const;
356 
361  VTBL_ENTRY void SetLanguage( int aLanguage );
362 
368  VTBL_ENTRY void CommonSettingsChanged();
369 
370  KIWAY( PGM_BASE* aProgram, int aCtlBits, wxFrame* aTop = NULL );
371 
380  void SetTop( wxFrame* aTop );
381 
382  void OnKiwayEnd();
383 
384  bool ProcessEvent( wxEvent& aEvent ) override;
385 
386 private:
387 
389  const wxString dso_search_path( FACE_T aFaceId );
390 
391 #if 0
392  void player_destroy_handler( wxWindowDestroyEvent& event );
394 #endif
395 
396  bool set_kiface( FACE_T aFaceType, KIFACE* aKiface )
397  {
398  if( unsigned( aFaceType ) < unsigned( KIWAY_FACE_COUNT ) )
399  {
400  m_kiface[aFaceType] = aKiface;
401  return true;
402  }
403  return false;
404  }
405 
410  KIWAY_PLAYER* GetPlayerFrame( FRAME_T aFrameType );
411 
412  static KIFACE* m_kiface[KIWAY_FACE_COUNT];
413  static int m_kiface_version[KIWAY_FACE_COUNT];
414 
416  int m_ctl;
417 
418  wxFrame* m_top; // Usually m_top is the Project manager
419 
420  // a string array ( size KIWAY_PLAYER_COUNT ) to Store the frame name
421  // of PLAYER frames which were run.
422  // A non empty name means only a PLAYER was run at least one time.
423  // It can be closed. Call :
424  // wxWindow::FindWindowByName( m_playerFrameName[aFrameType] )
425  // to know if still exists (or GetPlayerFrame( FRAME_T aFrameType )
426  wxArrayString m_playerFrameName;
427 
428  PROJECT m_project; // do not assume this is here, use Prj().
429 };
430 
431 
432 #ifndef SWIG
433 // provided by single_top.cpp and kicad.cpp;
434 extern KIWAY Kiway;
435 // whereas python launchers: single_top.py and project manager instantiate as a python object
436 #endif
437 
438 
452 typedef KIFACE* KIFACE_GETTER_FUNC( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
453 
454 
455 #ifndef SWIG
456 
458 extern "C" {
459 #if defined(BUILD_KIWAY_DLL)
460  MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
461 #else
462  KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
463 #endif
464 }
465 
466 #endif // SWIG
467 
468 #endif // KIWAY_H_
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:120
Struct PGM_SINGLE_TOP implements PGM_BASE with its own OnPgmInit() and OnPgmExit().
Definition: single_top.cpp:62
Class PROJECT holds project specific data.
Definition: project.h:57
virtual ~KIFACE()
Definition: kiway.h:157
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:149
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
PROJECT & Prj()
Definition: kicad.cpp:292
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:415
~KIWAY()
Definition: kiway.h:277
VTBL_ENTRY void * IfaceOrAddress(int aDataId)=0
Function IfaceOrAddress returns a pointer to the requested object.
#define VTBL_ENTRY
Definition: kiway.h:109
bool set_kiface(FACE_T aFaceType, KIFACE *aKiface)
Definition: kiway.h:396
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.
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:452
PROJECT m_project
Definition: kiway.h:428
int m_ctl
Definition: kiway.h:416
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
pcbnew DSO
Definition: kiway.h:267
#define KIFACE_GETTER
Definition: kiway.h:112
VTBL_ENTRY bool SetLanguage(bool first_time=false)
Function SetLanguage sets the dictionary file name for internationalization.
Definition: pgm_base.cpp:657
FACE_T
Known KIFACE implementations.
Definition: kiway.h:264
eeschema DSO
Definition: kiway.h:266
int aKIWAYversion
Definition: cvpcb.cpp:112
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:151
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:426
wxFrame * m_top
Definition: kiway.h:418
int PGM_BASE * aProgram
Definition: pcbnew.cpp:217
KIWAY Kiway