2 #include <wx/txtstrm.h>
3 #include "pipedprocess.h"
5 DEFINE_EVENT_TYPE(EVT_PROCESS_INPUT
)
6 DEFINE_EVENT_TYPE(EVT_PROCESS_OUTPUT
)
7 DEFINE_EVENT_TYPE(EVT_PROCESS_CREATED
)
8 DEFINE_EVENT_TYPE(EVT_PROCESS_TERMINATED
)
10 BEGIN_EVENT_TABLE(PipedProcess
, wxProcess
)
11 EVT_TIMER(ID_QEMU_TIMER_POLL_PROCESS
, PipedProcess::OnTimer
)
12 EVT_IDLE(PipedProcess::OnIdle
)
15 PipedProcess::PipedProcess() : wxProcess(){
19 PipedProcess::~PipedProcess()
23 long PipedProcess::Launch(const wxString
& cmd
,unsigned int interval
){
24 wxCommandEvent
event(EVT_PROCESS_CREATED
,wxID_ANY
);
25 event
.SetEventObject(this);
26 event
.SetString(cmd
+ wxT("\n"));
29 pid
= wxExecute(cmd
, wxEXEC_ASYNC
| wxEXEC_NODISABLE
, this);
33 /* close stdin of child process */
35 timer
.SetOwner(this, ID_QEMU_TIMER_POLL_PROCESS
);
36 timer
.Start(interval
);
41 void PipedProcess::SendString(const wxString
& text
)
47 wxOutputStream
*out
= GetOutputStream();
50 wxTextOutputStream
sin(*out
);
51 wxString msg
= text
+ wxT("\n");
53 wxCommandEvent
event(EVT_PROCESS_OUTPUT
,wxID_ANY
);
54 event
.SetEventObject(this);
61 bool PipedProcess::HasInput()
63 bool hasInput
= false;
65 if (IsInputAvailable())
67 wxInputStream
*in
= GetInputStream();
70 in
->Read(buffer
,WXSIZEOF(buffer
) - 1);
71 buffer
[in
->LastRead()] = '\0';
72 // remove the first line, because it contains some non printable characters
73 char *p
= strchr(buffer
,'\n');
75 wxString
msg(++p
,wxConvUTF8
,in
->LastRead() - (p
- buffer
));
76 wxCommandEvent
event(EVT_PROCESS_INPUT
,wxID_ANY
);
77 event
.SetEventObject(this);
85 if(IsErrorAvailable())
87 wxTextInputStream
serr(*GetErrorStream());
90 msg
<< serr
.ReadLine();
92 wxCommandEvent
event(EVT_PROCESS_INPUT
,wxID_ANY
);
93 event
.SetEventObject(this);
103 void PipedProcess::OnTerminate(int pid
, int status
)
107 // show the rest of the output
111 wxCommandEvent
event(EVT_PROCESS_TERMINATED
,wxID_ANY
);
112 event
.SetEventObject(this);
113 event
.SetInt(status
);
119 void PipedProcess::OnTimer(wxTimerEvent
& event
){
124 void PipedProcess::OnIdle(wxIdleEvent
& event
){
129 void PipedProcess::AddEventHandler(wxEvtHandler
*handler
){
130 evtHandlers
.Add(handler
);
133 void PipedProcess::SendEvent(wxCommandEvent
& event
){
134 for(int i
= 0; i
< evtHandlers
.GetCount(); i
++){
135 evtHandlers
.Item(i
)->ProcessEvent(event
);