Better looking toolbar styles (especially on Windows)
[qemu-gui.git] / pipedprocess.cpp
blob5220b671f18427e12b0172f359fc7443970919cc
1 #include <wx/timer.h>
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)
13 END_EVENT_TABLE()
15 PipedProcess::PipedProcess() : wxProcess(){
16 Redirect();
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"));
27 SendEvent(event);
29 pid = wxExecute(cmd, wxEXEC_ASYNC | wxEXEC_NODISABLE, this);
30 running = true;
31 if (pid)
33 /* close stdin of child process */
34 CloseOutput();
35 timer.SetOwner(this, ID_QEMU_TIMER_POLL_PROCESS);
36 timer.Start(interval);
38 return pid;
41 void PipedProcess::SendString(const wxString& text)
43 #if 0
44 if(!running)
45 return;
47 wxOutputStream *out = GetOutputStream();
48 if(out)
50 wxTextOutputStream sin(*out);
51 wxString msg = text + wxT("\n");
52 sin.WriteString(msg);
53 wxCommandEvent event(EVT_PROCESS_OUTPUT,wxID_ANY);
54 event.SetEventObject(this);
55 event.SetString(msg);
56 SendEvent(event);
58 #endif
61 bool PipedProcess::HasInput()
63 bool hasInput = false;
65 if (IsInputAvailable())
67 wxInputStream *in = GetInputStream();
68 if(in->CanRead()){
69 char buffer[4096];
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');
74 if(p){
75 wxString msg(++p,wxConvUTF8,in->LastRead() - (p - buffer));
76 wxCommandEvent event(EVT_PROCESS_INPUT,wxID_ANY);
77 event.SetEventObject(this);
78 event.SetString(msg);
79 SendEvent(event);
82 hasInput = true;
85 if(IsErrorAvailable())
87 wxTextInputStream serr(*GetErrorStream());
89 wxString msg;
90 msg << serr.ReadLine();
92 wxCommandEvent event(EVT_PROCESS_INPUT,wxID_ANY);
93 event.SetEventObject(this);
94 event.SetString(msg);
95 SendEvent(event);
97 hasInput = true;
100 return hasInput;
103 void PipedProcess::OnTerminate(int pid, int status)
105 running = false;
106 timer.Stop();
107 // show the rest of the output
108 while (HasInput())
111 wxCommandEvent event(EVT_PROCESS_TERMINATED,wxID_ANY);
112 event.SetEventObject(this);
113 event.SetInt(status);
114 SendEvent(event);
116 //delete this;
119 void PipedProcess::OnTimer(wxTimerEvent& event){
120 while (HasInput())
124 void PipedProcess::OnIdle(wxIdleEvent& event){
125 while (HasInput())
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);