You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 Kicad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <eda_dde.h>
  20. #include <kiface_base.h>
  21. #include <kiface_ids.h>
  22. #include <kipython_frame.h>
  23. #include <kipython_settings.h>
  24. #include <kiway.h>
  25. #include <pgm_base.h>
  26. #include <python_scripting.h>
  27. #include <settings/settings_manager.h>
  28. //-----<KIFACE>-----------------------------------------------------------------
  29. namespace KIPYTHON {
  30. static struct IFACE : public KIFACE_BASE
  31. {
  32. bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
  33. void OnKifaceEnd() override;
  34. wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
  35. {
  36. KIPYTHON_FRAME* frame = new KIPYTHON_FRAME( aKiway, aParent );
  37. if( Kiface().IsSingle() )
  38. {
  39. // only run this under single_top, not under a project manager.
  40. frame->CreateServer( KICAD_PY_PORT_SERVICE_NUMBER );
  41. }
  42. return frame;
  43. }
  44. /**
  45. * Function IfaceOrAddress
  46. * return a pointer to the requested object. The safest way to use this
  47. * is to retrieve a pointer to a static instance of an interface, similar to
  48. * how the KIFACE interface is exported. But if you know what you are doing
  49. * use it to retrieve anything you want.
  50. *
  51. * @param aDataId identifies which object you want the address of.
  52. *
  53. * @return void* - and must be cast into the know type.
  54. */
  55. void* IfaceOrAddress( int aDataId ) override
  56. {
  57. return NULL;
  58. }
  59. IFACE( const char* aDSOname, KIWAY::FACE_T aType ) :
  60. KIFACE_BASE( aDSOname, aType )
  61. {}
  62. } kiface( "KIPYTHON", KIWAY::FACE_PYTHON );
  63. } // namespace KIPYTHON
  64. using namespace KIPYTHON;
  65. static PGM_BASE* process;
  66. KIFACE_BASE& Kiface()
  67. {
  68. return kiface;
  69. }
  70. // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
  71. // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
  72. KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram )
  73. {
  74. process = (PGM_BASE*) aProgram;
  75. return &kiface;
  76. }
  77. PGM_BASE& Pgm()
  78. {
  79. wxASSERT( process ); // KIFACE_GETTER has already been called.
  80. return *process;
  81. }
  82. // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from
  83. // a python script or something else.
  84. PGM_BASE* PgmOrNull()
  85. {
  86. return process;
  87. }
  88. bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
  89. {
  90. InitSettings( new KIPYTHON_SETTINGS );
  91. Pgm().GetSettingsManager().RegisterSettings( KifaceSettings() );
  92. return start_common( aCtlBits );
  93. }
  94. void IFACE::OnKifaceEnd()
  95. {
  96. end_common();
  97. }