KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kiway_player.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, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <kiway_player.h>
23#include <kiway_mail.h>
24#include <kiway.h>
25#include <id.h>
26#include <macros.h>
27#include <typeinfo>
28#include <wx/utils.h>
29#include <wx/evtloop.h>
30#include <wx/socket.h>
31#include <core/raii.h>
32#include <wx/log.h>
33
34
35static const wxString HOSTNAME( wxT( "localhost" ) );
36
37// buffer for read and write data in socket connections
38#define IPC_BUF_SIZE 4096
40
41BEGIN_EVENT_TABLE( KIWAY_PLAYER, EDA_BASE_FRAME )
43END_EVENT_TABLE()
44
45
46KIWAY_PLAYER::KIWAY_PLAYER( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrameType,
47 const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
48 long aStyle, const wxString& aFrameName,
49 const EDA_IU_SCALE& aIuScale ) :
50 EDA_BASE_FRAME( aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName, aKiway,
51 aIuScale ),
52 m_modal( false ),
53 m_modal_loop( nullptr ),
54 m_modal_resultant_parent( nullptr ),
55 m_modal_ret_val( false ),
56 m_socketServer( nullptr )
57{
58}
59
60
62{
63 // socket server must be destructed before we complete
64 // destructing the frame or else we could crash
65 // as the socket server holds a reference to this frame
66 if( m_socketServer )
67 {
68 // ensure any event handling stops
69 m_socketServer->Notify( false );
70
71 delete m_socketServer;
72 m_socketServer = nullptr;
73 }
74
75 // remove active sockets as well
76 for( wxSocketBase* socket : m_sockets )
77 {
78 if( !socket )
79 continue;
80
81 // ensure any event handling stops
82 socket->Notify( false );
83
84 delete socket;
85 }
86
87 m_sockets.clear();
88}
89
90
92{
93 // override this in derived classes.
94}
95
96
97bool KIWAY_PLAYER::ShowModal( wxString* aResult, wxWindow* aResultantFocusWindow )
98{
99 wxASSERT_MSG( IsModal(), wxT( "ShowModal() shouldn't be called on non-modal frame" ) );
100
101 /*
102 This function has a nice interface but a necessarily unsightly implementation.
103 Now the implementation is encapsulated, localizing future changes.
104
105 It works in tandem with DismissModal(). But only ShowModal() is in the
106 vtable and therefore cross-module capable.
107 */
108
109 NULLER raii_nuller( (void*&) m_modal_loop );
110
111 m_modal_resultant_parent = aResultantFocusWindow;
112
113 Show( true );
114 Raise(); // Needed on some Window managers to always display the frame
115
116 SetFocus();
117
118 {
119 // Using wxWindowDisabler() has two issues: it will disable top-level windows that are
120 // our *children* (such as sub-frames), and it will disable all context menus we try to
121 // put up. Fortunatly we already had to cross this Rubicon for QuasiModal dialogs, so
122 // we re-use that strategy.
123 wxWindow* parent = GetParent();
124
125 while( parent && !parent->IsTopLevel() )
126 parent = parent->GetParent();
127
128 WINDOW_DISABLER raii_parent_disabler( parent );
129
130 wxGUIEventLoop event_loop;
131 m_modal_loop = &event_loop;
132 event_loop.Run();
133 }
134
135 if( aResult )
136 *aResult = m_modal_string;
137
138 if( aResultantFocusWindow )
139 {
140 aResultantFocusWindow->Raise();
141
142 // have the final say, after wxWindowDisabler reenables my parent and
143 // the events settle down, set the focus
144 wxSafeYield();
145 aResultantFocusWindow->SetFocus();
146 }
147
148 return m_modal_ret_val;
149}
150
151
153{
155
156 return EDA_BASE_FRAME::Destroy();
157}
158
159
161{
162 return !m_modal_loop;
163}
164
165
166void KIWAY_PLAYER::DismissModal( bool aRetVal, const wxString& aResult )
167{
168 m_modal_ret_val = aRetVal;
169 m_modal_string = aResult;
170
171 if( m_modal_loop )
172 {
173 m_modal_loop->Exit();
174 m_modal_loop = nullptr; // this marks it as dismissed.
175 }
176
177 Show( false );
178}
179
180
182{
183 // logging support
184 KiwayMailIn( aEvent ); // call the virtual, override in derived.
185}
186
187
188void KIWAY_PLAYER::CreateServer( int service, bool local )
189{
190 wxIPV4address addr;
191
192 // Set the port number
193 addr.Service( service );
194
195 // Listen on localhost only if requested
196 if( local )
197 addr.Hostname( HOSTNAME );
198
199 if( m_socketServer )
200 {
201 // this helps prevent any events that could come in during deletion
202 m_socketServer->Notify( false );
203 delete m_socketServer;
204 }
205
206 m_socketServer = new wxSocketServer( addr );
207
208 m_socketServer->SetNotify( wxSOCKET_CONNECTION_FLAG );
209 m_socketServer->SetEventHandler( *this, ID_EDA_SOCKET_EVENT_SERV );
210 m_socketServer->Notify( true );
211}
212
213
214void KIWAY_PLAYER::OnSockRequest( wxSocketEvent& evt )
215{
216 size_t len;
217 wxSocketBase* sock = evt.GetSocket();
218
219 switch( evt.GetSocketEvent() )
220 {
221 case wxSOCKET_INPUT:
222 sock->Read( client_ipc_buffer, 1 );
223
224 if( sock->LastCount() == 0 )
225 break; // No data, occurs on opening connection
226
227 sock->Read( client_ipc_buffer + 1, IPC_BUF_SIZE - 2 );
228 len = 1 + sock->LastCount();
229 client_ipc_buffer[len] = 0;
231 break;
232
233 case wxSOCKET_LOST:
234 return;
235 break;
236
237 default:
238 wxLogError( wxT( "EDA_DRAW_FRAME::OnSockRequest() error: Invalid event !" ) );
239 break;
240 }
241}
242
243
244void KIWAY_PLAYER::OnSockRequestServer( wxSocketEvent& evt )
245{
246 wxSocketBase* socket;
247 wxSocketServer* server = (wxSocketServer*) evt.GetSocket();
248
249 socket = server->Accept();
250
251 if( socket == nullptr )
252 return;
253
254 m_sockets.push_back( socket );
255
256 socket->Notify( true );
257 socket->SetEventHandler( *this, ID_EDA_SOCKET_EVENT );
258 socket->SetNotify( wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG );
259}
260
The base frame for deriving all KiCad main window classes.
FRAME_T GetFrameType() const
EDA_BASE_FRAME(wxWindow *aParent, FRAME_T aFrameType, const wxString &aTitle, const wxPoint &aPos, const wxSize &aSize, long aStyle, const wxString &aFrameName, KIWAY *aKiway, const EDA_IU_SCALE &aIuScale)
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition kiway_mail.h:34
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
wxString m_modal_string
virtual void ExecuteRemoteCommand(const char *cmdline)
Execute a remote command sent via socket (to port KICAD_PCB_PORT_SERVICE_NUMBER, currently 4242).
void CreateServer(int service, bool local=true)
virtual void KiwayMailIn(KIWAY_MAIL_EVENT &aEvent)
Receive #KIWAY_ROUTED_EVENT messages from other players.
wxWindow * m_modal_resultant_parent
KIWAY_PLAYER(KIWAY *aKiway, wxWindow *aParent, FRAME_T aFrameType, const wxString &aTitle, const wxPoint &aPos, const wxSize &aSize, long aStyle, const wxString &aFrameName, const EDA_IU_SCALE &aIuScale)
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
wxGUIEventLoop * m_modal_loop
Points to nested event_loop. NULL means not modal and dismissed.
std::vector< wxSocketBase * > m_sockets
void DismissModal(bool aRetVal, const wxString &aResult=wxEmptyString)
void OnSockRequestServer(wxSocketEvent &evt)
void OnSockRequest(wxSocketEvent &evt)
bool IsModal() const override
Return true if the frame is shown in our modal mode and false if the frame is shown as an usual frame...
void kiway_express(KIWAY_MAIL_EVENT &aEvent)
Event handler, routes to derivative specific virtual KiwayMailIn().
wxSocketServer * m_socketServer
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:311
void PlayerDidClose(FRAME_T aFrameType)
Notifies a Kiway that a player has been closed.
Definition kiway.cpp:490
Definition raii.h:34
Temporarily disable a window, and then re-enable on destruction.
Definition raii.h:83
static const wxString HOSTNAME(wxT("localhost"))
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ ID_EDA_SOCKET_EVENT
Definition id.h:132
@ ID_EDA_SOCKET_EVENT_SERV
Definition id.h:131
#define EVT_KIWAY_EXPRESS(func)
Event table definition for the KIWAY_ROUTED_EVENT event class.
Definition kiway_mail.h:74
#define IPC_BUF_SIZE
static char client_ipc_buffer[IPC_BUF_SIZE]
static const wxString HOSTNAME(wxT("localhost"))
This file contains miscellaneous commonly used macros and functions.