process.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: process.h
  3. // Purpose: interface of wxProcess
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxProcess
  9. The objects of this class are used in conjunction with the ::wxExecute() function.
  10. When a wxProcess object is passed to ::wxExecute(), its OnTerminate() virtual method
  11. is called when the process terminates. This allows the program to be (asynchronously)
  12. notified about the process termination and also retrieve its exit status which is
  13. unavailable from ::wxExecute() in the case of asynchronous execution.
  14. @note
  15. If the @c wxEVT_END_PROCESS event sent after termination is processed by the
  16. parent, then it is responsible for deleting the wxProcess object which sent it.
  17. However, if it is not processed, the object will <b>delete itself</b> and so the
  18. library users should only delete those objects whose notifications have been
  19. processed (and call wxProcess::Detach for others).
  20. This also means that unless you're going to process the @c wxEVT_END_PROCESS event,
  21. you <b>must</b> allocate the wxProcess class on the heap.
  22. wxProcess also supports IO redirection of the child process. For this, you have
  23. to call its Redirect() method before passing it to ::wxExecute().
  24. If the child process was launched successfully, GetInputStream(), GetOutputStream()
  25. and GetErrorStream() can then be used to retrieve the streams corresponding to the
  26. child process standard output, input and error output respectively.
  27. @beginEventEmissionTable{wxProcessEvent}
  28. @event{EVT_END_PROCESS(id, func)}
  29. Process a @c wxEVT_END_PROCESS event, sent by wxProcess::OnTerminate upon
  30. the external process termination.
  31. @endEventTable
  32. @library{wxbase}
  33. @category{appmanagement}
  34. @beginWxPerlOnly
  35. In wxPerl this class has an additional @c Destroy method,
  36. for explicit destruction.
  37. @endWxPerlOnly
  38. @see wxExecute(), @ref page_samples_exec
  39. */
  40. class wxProcess : public wxEvtHandler
  41. {
  42. public:
  43. /**
  44. Constructs a process object. @a id is only used in the case you want to
  45. use wxWidgets events. It identifies this object, or another window that will
  46. receive the event.
  47. If the @a parent parameter is different from @NULL, it will receive
  48. a @c wxEVT_END_PROCESS notification event (you should insert @c EVT_END_PROCESS
  49. macro in the event table of the parent to handle it) with the given @a id.
  50. @param parent
  51. The event handler parent.
  52. @param id
  53. id of an event.
  54. */
  55. wxProcess(wxEvtHandler* parent = NULL, int id = -1);
  56. /**
  57. Creates an object without any associated parent (and hence no id neither)
  58. but allows to specify the @a flags which can have the value of
  59. @c wxPROCESS_DEFAULT or @c wxPROCESS_REDIRECT.
  60. Specifying the former value has no particular effect while using the latter
  61. one is equivalent to calling Redirect().
  62. */
  63. wxProcess(int flags);
  64. /**
  65. Destroys the wxProcess object.
  66. */
  67. virtual ~wxProcess();
  68. /**
  69. Closes the output stream (the one connected to the stdin of the child
  70. process).
  71. This function can be used to indicate to the child process that
  72. there is no more data to be read - usually, a filter program will only
  73. terminate when the input stream is closed.
  74. Notice that GetOutputStream() will return @NULL after the output stream
  75. is closed.
  76. */
  77. void CloseOutput();
  78. /**
  79. Detaches this event handler from the parent specified in the constructor
  80. (see wxEvtHandler::Unlink() for a similar but not identical function).
  81. Normally, a wxProcess object is deleted by its parent when it receives the
  82. notification about the process termination.
  83. However, it might happen that the parent object is destroyed before the external
  84. process is terminated (e.g. a window from which this external process was launched
  85. is closed by the user) and in this case it @b should not delete the wxProcess
  86. object, but @b should call Detach() instead.
  87. After the wxProcess object is detached from its parent, no notification events
  88. will be sent to the parent and the object will delete itself upon reception of
  89. the process termination notification.
  90. */
  91. void Detach();
  92. /**
  93. Returns @true if the given process exists in the system.
  94. @see Kill(), @ref page_samples_exec "Exec sample"
  95. */
  96. static bool Exists(int pid);
  97. /**
  98. Returns an input stream which corresponds to the standard error output (stderr)
  99. of the child process.
  100. */
  101. wxInputStream* GetErrorStream() const;
  102. /**
  103. It returns an input stream corresponding to the standard output stream of the
  104. subprocess. If it is @NULL, you have not turned on the redirection.
  105. @see Redirect().
  106. */
  107. wxInputStream* GetInputStream() const;
  108. /**
  109. It returns an output stream corresponding to the input stream of the subprocess.
  110. If it is @NULL, you have not turned on the redirection or already
  111. called CloseOutput().
  112. @see Redirect().
  113. */
  114. wxOutputStream* GetOutputStream() const;
  115. /**
  116. Returns the process ID of the process launched by Open() or set by
  117. wxExecute() (if you passed this wxProcess as argument).
  118. */
  119. long GetPid() const;
  120. /**
  121. Returns @true if there is data to be read on the child process standard
  122. error stream.
  123. @see IsInputAvailable()
  124. */
  125. bool IsErrorAvailable() const;
  126. /**
  127. Returns @true if there is data to be read on the child process standard
  128. output stream.
  129. This allows to write simple (and extremely inefficient) polling-based code
  130. waiting for a better mechanism in future wxWidgets versions.
  131. See the @ref page_samples_exec "exec sample" for an example of using this
  132. function.
  133. @see IsInputOpened()
  134. */
  135. bool IsInputAvailable() const;
  136. /**
  137. Returns @true if the child process standard output stream is opened.
  138. */
  139. bool IsInputOpened() const;
  140. /**
  141. Send the specified signal to the given process. Possible signal values
  142. can be one of the ::wxSignal enumeration values.
  143. @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning
  144. under both Unix and Windows but all the other signals are equivalent to
  145. @c wxSIGTERM under Windows.
  146. The @a flags parameter can be @c wxKILL_NOCHILDREN (the default),
  147. or @c wxKILL_CHILDREN, in which case the child processes of this
  148. process will be killed too. Note that under Unix, for @c wxKILL_CHILDREN
  149. to work you should have created the process passing @c wxEXEC_MAKE_GROUP_LEADER.
  150. Returns the element of ::wxKillError enum.
  151. @see Exists(), wxKill(), @ref page_samples_exec "Exec sample"
  152. */
  153. static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM,
  154. int flags = wxKILL_NOCHILDREN);
  155. /**
  156. It is called when the process with the pid @a pid finishes.
  157. It raises a wxWidgets event when it isn't overridden.
  158. Note that this function won't be called if you Kill() the process.
  159. @param pid
  160. The pid of the process which has just terminated.
  161. @param status
  162. The exit code of the process.
  163. */
  164. virtual void OnTerminate(int pid, int status);
  165. /**
  166. This static method replaces the standard @c popen() function: it launches
  167. the process specified by the @a cmd parameter and returns the wxProcess
  168. object which can be used to retrieve the streams connected to the standard
  169. input, output and error output of the child process.
  170. If the process couldn't be launched, @NULL is returned.
  171. @remarks
  172. In any case the returned pointer should @b not be deleted, rather the process
  173. object will be destroyed automatically when the child process terminates. This
  174. does mean that the child process should be told to quit before the main program
  175. exits to avoid memory leaks.
  176. @param cmd
  177. The command to execute, including optional arguments.
  178. @param flags
  179. The flags to pass to ::wxExecute().
  180. Note: @c wxEXEC_SYNC should not be used.
  181. @return A pointer to new wxProcess object or @NULL on error.
  182. @see ::wxExecute()
  183. */
  184. static wxProcess* Open(const wxString& cmd,
  185. int flags = wxEXEC_ASYNC);
  186. /**
  187. Turns on redirection.
  188. ::wxExecute() will try to open a couple of pipes to catch the subprocess stdio.
  189. The caught input stream is returned by GetOutputStream() as a non-seekable stream.
  190. The caught output stream is returned by GetInputStream() as a non-seekable stream.
  191. */
  192. void Redirect();
  193. /**
  194. Sets the priority of the process, between 0 (lowest) and 100 (highest).
  195. It can only be set before the process is created.
  196. The following symbolic constants can be used in addition to raw
  197. values in 0..100 range:
  198. - @b wxPRIORITY_MIN: 0
  199. - @b wxPRIORITY_DEFAULT: 50
  200. - @b wxPRIORITY_MAX: 100
  201. @since 2.9.5
  202. */
  203. void SetPriority(unsigned priority);
  204. };
  205. /**
  206. @class wxProcessEvent
  207. A process event is sent to the wxEvtHandler specified to wxProcess
  208. when a process is terminated.
  209. @beginEventTable{wxProcessEvent}
  210. @event{EVT_END_PROCESS(id, func)}
  211. Process a @c wxEVT_END_PROCESS event. @a id is the identifier of the process
  212. object (the id passed to the wxProcess constructor) or a window to receive
  213. the event.
  214. @endEventTable
  215. @library{wxbase}
  216. @category{events}
  217. @see wxProcess, @ref overview_events
  218. */
  219. class wxProcessEvent : public wxEvent
  220. {
  221. public:
  222. /**
  223. Constructor.
  224. Takes a wxProcessObject or window id, a process id and an exit status.
  225. */
  226. wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0);
  227. /**
  228. Returns the exist status.
  229. */
  230. int GetExitCode();
  231. /**
  232. Returns the process id.
  233. */
  234. int GetPid();
  235. };
  236. wxEventType wxEVT_END_PROCESS;