process.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/process.h
  3. // Purpose: wxProcess class
  4. // Author: Guilhem Lavaux
  5. // Modified by: Vadim Zeitlin to check error codes, added Detach() method
  6. // Created: 24/06/98
  7. // Copyright: (c) 1998 Guilhem Lavaux
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_PROCESSH__
  11. #define _WX_PROCESSH__
  12. #include "wx/event.h"
  13. #if wxUSE_STREAMS
  14. #include "wx/stream.h"
  15. #endif
  16. #include "wx/utils.h" // for wxSignal
  17. // the wxProcess creation flags
  18. enum
  19. {
  20. // no redirection
  21. wxPROCESS_DEFAULT = 0,
  22. // redirect the IO of the child process
  23. wxPROCESS_REDIRECT = 1
  24. };
  25. // ----------------------------------------------------------------------------
  26. // A wxProcess object should be passed to wxExecute - than its OnTerminate()
  27. // function will be called when the process terminates.
  28. // ----------------------------------------------------------------------------
  29. class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler
  30. {
  31. public:
  32. // kill the process with the given PID
  33. static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN);
  34. // test if the given process exists
  35. static bool Exists(int pid);
  36. // this function replaces the standard popen() one: it launches a process
  37. // asynchronously and allows the caller to get the streams connected to its
  38. // std{in|out|err}
  39. //
  40. // on error NULL is returned, in any case the process object will be
  41. // deleted automatically when the process terminates and should *not* be
  42. // deleted by the caller
  43. static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC);
  44. // ctors
  45. wxProcess(wxEvtHandler *parent = NULL, int nId = wxID_ANY)
  46. { Init(parent, nId, wxPROCESS_DEFAULT); }
  47. wxProcess(int flags) { Init(NULL, wxID_ANY, flags); }
  48. virtual ~wxProcess();
  49. // get the process ID of the process executed by Open()
  50. long GetPid() const { return m_pid; }
  51. // may be overridden to be notified about process termination
  52. virtual void OnTerminate(int pid, int status);
  53. // call this before passing the object to wxExecute() to redirect the
  54. // launched process stdin/stdout, then use GetInputStream() and
  55. // GetOutputStream() to get access to them
  56. void Redirect() { m_redirect = true; }
  57. bool IsRedirected() const { return m_redirect; }
  58. // detach from the parent - should be called by the parent if it's deleted
  59. // before the process it started terminates
  60. void Detach();
  61. #if wxUSE_STREAMS
  62. // Pipe handling
  63. wxInputStream *GetInputStream() const { return m_inputStream; }
  64. wxInputStream *GetErrorStream() const { return m_errorStream; }
  65. wxOutputStream *GetOutputStream() const { return m_outputStream; }
  66. // close the output stream indicating that nothing more will be written
  67. void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
  68. // return true if the child process stdout is not closed
  69. bool IsInputOpened() const;
  70. // return true if any input is available on the child process stdout/err
  71. bool IsInputAvailable() const;
  72. bool IsErrorAvailable() const;
  73. // implementation only (for wxExecute)
  74. //
  75. // NB: the streams passed here should correspond to the child process
  76. // stdout, stdin and stderr and here the normal naming convention is
  77. // used unlike elsewhere in this class
  78. void SetPipeStreams(wxInputStream *outStream,
  79. wxOutputStream *inStream,
  80. wxInputStream *errStream);
  81. #endif // wxUSE_STREAMS
  82. // priority
  83. // Sets the priority to the given value: see wxPRIORITY_XXX constants.
  84. //
  85. // NB: the priority can only be set before the process is created
  86. void SetPriority(unsigned priority);
  87. // Get the current priority.
  88. unsigned GetPriority() const { return m_priority; }
  89. // implementation only - don't use!
  90. // --------------------------------
  91. // needs to be public since it needs to be used from wxExecute() global func
  92. void SetPid(long pid) { m_pid = pid; }
  93. protected:
  94. void Init(wxEvtHandler *parent, int id, int flags);
  95. int m_id;
  96. long m_pid;
  97. unsigned m_priority;
  98. #if wxUSE_STREAMS
  99. // these streams are connected to stdout, stderr and stdin of the child
  100. // process respectively (yes, m_inputStream corresponds to stdout -- very
  101. // confusing but too late to change now)
  102. wxInputStream *m_inputStream,
  103. *m_errorStream;
  104. wxOutputStream *m_outputStream;
  105. #endif // wxUSE_STREAMS
  106. bool m_redirect;
  107. DECLARE_DYNAMIC_CLASS(wxProcess)
  108. wxDECLARE_NO_COPY_CLASS(wxProcess);
  109. };
  110. // ----------------------------------------------------------------------------
  111. // wxProcess events
  112. // ----------------------------------------------------------------------------
  113. class WXDLLIMPEXP_FWD_BASE wxProcessEvent;
  114. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, wxProcessEvent );
  115. class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent
  116. {
  117. public:
  118. wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId)
  119. {
  120. m_eventType = wxEVT_END_PROCESS;
  121. m_pid = pid;
  122. m_exitcode = exitcode;
  123. }
  124. // accessors
  125. // PID of process which terminated
  126. int GetPid() { return m_pid; }
  127. // the exit code
  128. int GetExitCode() { return m_exitcode; }
  129. // implement the base class pure virtual
  130. virtual wxEvent *Clone() const { return new wxProcessEvent(*this); }
  131. public:
  132. int m_pid,
  133. m_exitcode;
  134. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent)
  135. };
  136. typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&);
  137. #define wxProcessEventHandler(func) \
  138. wxEVENT_HANDLER_CAST(wxProcessEventFunction, func)
  139. #define EVT_END_PROCESS(id, func) \
  140. wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
  141. #endif // _WX_PROCESSH__