1998-07-01 17:26:46 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2006-10-30 11:56:12 +00:00
|
|
|
// Name: wx/process.h
|
1998-07-01 17:26:46 +00:00
|
|
|
// Purpose: wxProcess class
|
|
|
|
// Author: Guilhem Lavaux
|
1999-02-17 17:56:59 +00:00
|
|
|
// Modified by: Vadim Zeitlin to check error codes, added Detach() method
|
1998-07-01 17:26:46 +00:00
|
|
|
// Created: 24/06/98
|
|
|
|
// Copyright: (c) 1998 Guilhem Lavaux
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-07-01 17:26:46 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-15 00:23:28 +00:00
|
|
|
#ifndef _WX_PROCESSH__
|
|
|
|
#define _WX_PROCESSH__
|
1998-07-01 17:26:46 +00:00
|
|
|
|
|
|
|
#include "wx/event.h"
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
#if wxUSE_STREAMS
|
2001-01-31 17:21:59 +00:00
|
|
|
#include "wx/stream.h"
|
2000-07-15 19:51:35 +00:00
|
|
|
#endif
|
1998-07-01 17:26:46 +00:00
|
|
|
|
2001-06-16 00:59:07 +00:00
|
|
|
#include "wx/utils.h" // for wxSignal
|
|
|
|
|
2002-07-12 18:15:49 +00:00
|
|
|
// the wxProcess creation flags
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// no redirection
|
|
|
|
wxPROCESS_DEFAULT = 0,
|
|
|
|
|
|
|
|
// redirect the IO of the child process
|
|
|
|
wxPROCESS_REDIRECT = 1
|
|
|
|
};
|
|
|
|
|
2001-01-31 17:16:40 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-02-07 21:26:54 +00:00
|
|
|
// A wxProcess object should be passed to wxExecute - than its OnTerminate()
|
|
|
|
// function will be called when the process terminates.
|
2001-01-31 17:16:40 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-07-02 01:59:24 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler
|
1998-07-01 17:26:46 +00:00
|
|
|
{
|
1999-02-07 21:26:54 +00:00
|
|
|
public:
|
2002-07-12 18:15:49 +00:00
|
|
|
// kill the process with the given PID
|
2004-12-05 12:53:25 +00:00
|
|
|
static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN);
|
2002-07-12 18:15:49 +00:00
|
|
|
|
|
|
|
// test if the given process exists
|
|
|
|
static bool Exists(int pid);
|
|
|
|
|
|
|
|
// this function replaces the standard popen() one: it launches a process
|
|
|
|
// asynchronously and allows the caller to get the streams connected to its
|
|
|
|
// std{in|out|err}
|
|
|
|
//
|
|
|
|
// on error NULL is returned, in any case the process object will be
|
|
|
|
// deleted automatically when the process terminates and should *not* be
|
|
|
|
// deleted by the caller
|
2002-07-12 23:25:14 +00:00
|
|
|
static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC);
|
2002-07-12 18:15:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ctors
|
2009-01-19 13:55:27 +00:00
|
|
|
wxProcess(wxEvtHandler *parent = NULL, int nId = wxID_ANY)
|
2004-10-07 08:53:48 +00:00
|
|
|
{ Init(parent, nId, wxPROCESS_DEFAULT); }
|
2002-07-12 18:15:49 +00:00
|
|
|
|
2004-09-20 11:31:49 +00:00
|
|
|
wxProcess(int flags) { Init(NULL, wxID_ANY, flags); }
|
1998-07-01 17:26:46 +00:00
|
|
|
|
2000-03-02 19:06:13 +00:00
|
|
|
virtual ~wxProcess();
|
|
|
|
|
2006-10-30 09:03:18 +00:00
|
|
|
// get the process ID of the process executed by Open()
|
|
|
|
long GetPid() const { return m_pid; }
|
|
|
|
|
2000-03-02 19:06:13 +00:00
|
|
|
// may be overridden to be notified about process termination
|
1999-02-07 21:26:54 +00:00
|
|
|
virtual void OnTerminate(int pid, int status);
|
1998-07-01 17:26:46 +00:00
|
|
|
|
2000-03-02 19:06:13 +00:00
|
|
|
// call this before passing the object to wxExecute() to redirect the
|
|
|
|
// launched process stdin/stdout, then use GetInputStream() and
|
|
|
|
// GetOutputStream() to get access to them
|
2004-09-20 11:31:49 +00:00
|
|
|
void Redirect() { m_redirect = true; }
|
2000-03-02 19:06:13 +00:00
|
|
|
bool IsRedirected() const { return m_redirect; }
|
|
|
|
|
1999-02-17 17:56:59 +00:00
|
|
|
// detach from the parent - should be called by the parent if it's deleted
|
|
|
|
// before the process it started terminates
|
|
|
|
void Detach();
|
|
|
|
|
2015-03-10 19:28:22 +00:00
|
|
|
// Activates a GUI process by bringing its (main) window to the front.
|
|
|
|
//
|
|
|
|
// Currently only implemented in wxMSW, simply returns false under the
|
|
|
|
// other platforms.
|
|
|
|
bool Activate() const;
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
#if wxUSE_STREAMS
|
2000-02-27 10:44:49 +00:00
|
|
|
// Pipe handling
|
2000-03-02 19:06:13 +00:00
|
|
|
wxInputStream *GetInputStream() const { return m_inputStream; }
|
2000-07-15 19:51:35 +00:00
|
|
|
wxInputStream *GetErrorStream() const { return m_errorStream; }
|
2000-03-02 19:06:13 +00:00
|
|
|
wxOutputStream *GetOutputStream() const { return m_outputStream; }
|
2000-02-27 10:44:49 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
// close the output stream indicating that nothing more will be written
|
|
|
|
void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
|
|
|
|
|
2004-09-20 11:31:49 +00:00
|
|
|
// return true if the child process stdout is not closed
|
2002-08-20 22:33:20 +00:00
|
|
|
bool IsInputOpened() const;
|
|
|
|
|
2004-09-20 11:31:49 +00:00
|
|
|
// return true if any input is available on the child process stdout/err
|
2002-08-20 22:33:20 +00:00
|
|
|
bool IsInputAvailable() const;
|
|
|
|
bool IsErrorAvailable() const;
|
|
|
|
|
2000-03-02 19:06:13 +00:00
|
|
|
// implementation only (for wxExecute)
|
2002-07-12 18:15:49 +00:00
|
|
|
//
|
|
|
|
// NB: the streams passed here should correspond to the child process
|
|
|
|
// stdout, stdin and stderr and here the normal naming convention is
|
|
|
|
// used unlike elsewhere in this class
|
|
|
|
void SetPipeStreams(wxInputStream *outStream,
|
|
|
|
wxOutputStream *inStream,
|
2000-07-15 19:51:35 +00:00
|
|
|
wxInputStream *errStream);
|
|
|
|
#endif // wxUSE_STREAMS
|
2000-02-27 10:44:49 +00:00
|
|
|
|
2013-01-20 02:10:12 +00:00
|
|
|
// priority
|
|
|
|
// Sets the priority to the given value: see wxPRIORITY_XXX constants.
|
|
|
|
//
|
|
|
|
// NB: the priority can only be set before the process is created
|
|
|
|
void SetPriority(unsigned priority);
|
|
|
|
|
|
|
|
// Get the current priority.
|
|
|
|
unsigned GetPriority() const { return m_priority; }
|
|
|
|
|
2009-03-23 16:23:44 +00:00
|
|
|
// implementation only - don't use!
|
|
|
|
// --------------------------------
|
2009-08-21 10:41:26 +00:00
|
|
|
|
2009-03-23 16:23:44 +00:00
|
|
|
// needs to be public since it needs to be used from wxExecute() global func
|
|
|
|
void SetPid(long pid) { m_pid = pid; }
|
|
|
|
|
1999-02-07 21:26:54 +00:00
|
|
|
protected:
|
2002-07-12 18:15:49 +00:00
|
|
|
void Init(wxEvtHandler *parent, int id, int flags);
|
2000-03-02 19:06:13 +00:00
|
|
|
|
1999-02-07 21:26:54 +00:00
|
|
|
int m_id;
|
2006-10-30 09:03:18 +00:00
|
|
|
long m_pid;
|
2000-03-02 19:06:13 +00:00
|
|
|
|
2013-01-20 02:10:12 +00:00
|
|
|
unsigned m_priority;
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
#if wxUSE_STREAMS
|
2002-07-12 18:15:49 +00:00
|
|
|
// these streams are connected to stdout, stderr and stdin of the child
|
|
|
|
// process respectively (yes, m_inputStream corresponds to stdout -- very
|
|
|
|
// confusing but too late to change now)
|
2000-07-15 19:51:35 +00:00
|
|
|
wxInputStream *m_inputStream,
|
|
|
|
*m_errorStream;
|
2000-03-02 19:06:13 +00:00
|
|
|
wxOutputStream *m_outputStream;
|
2000-07-15 19:51:35 +00:00
|
|
|
#endif // wxUSE_STREAMS
|
2000-03-02 19:06:13 +00:00
|
|
|
|
|
|
|
bool m_redirect;
|
2001-06-16 00:59:07 +00:00
|
|
|
|
2015-04-23 11:49:01 +00:00
|
|
|
wxDECLARE_DYNAMIC_CLASS(wxProcess);
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxProcess);
|
1998-07-01 17:26:46 +00:00
|
|
|
};
|
|
|
|
|
2001-01-31 17:16:40 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxProcess events
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-01-12 14:26:13 +00:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxProcessEvent;
|
|
|
|
|
2009-02-07 18:59:25 +00:00
|
|
|
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, wxProcessEvent );
|
2001-01-31 17:25:56 +00:00
|
|
|
|
2003-07-02 01:59:24 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent
|
2001-01-31 17:21:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2004-10-07 08:53:48 +00:00
|
|
|
wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId)
|
2001-01-31 17:21:59 +00:00
|
|
|
{
|
|
|
|
m_eventType = wxEVT_END_PROCESS;
|
|
|
|
m_pid = pid;
|
|
|
|
m_exitcode = exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// accessors
|
|
|
|
// PID of process which terminated
|
|
|
|
int GetPid() { return m_pid; }
|
|
|
|
|
|
|
|
// the exit code
|
|
|
|
int GetExitCode() { return m_exitcode; }
|
|
|
|
|
2001-11-21 21:44:52 +00:00
|
|
|
// implement the base class pure virtual
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual wxEvent *Clone() const wxOVERRIDE { return new wxProcessEvent(*this); }
|
2001-11-21 21:44:52 +00:00
|
|
|
|
2001-01-31 17:21:59 +00:00
|
|
|
public:
|
2001-11-21 21:44:52 +00:00
|
|
|
int m_pid,
|
|
|
|
m_exitcode;
|
2001-01-31 17:21:59 +00:00
|
|
|
|
2015-04-23 11:49:01 +00:00
|
|
|
wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent);
|
2001-01-31 17:21:59 +00:00
|
|
|
};
|
|
|
|
|
2001-10-10 22:34:55 +00:00
|
|
|
typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&);
|
1998-07-01 17:26:46 +00:00
|
|
|
|
2005-02-14 23:53:48 +00:00
|
|
|
#define wxProcessEventHandler(func) \
|
2009-01-12 14:26:13 +00:00
|
|
|
wxEVENT_HANDLER_CAST(wxProcessEventFunction, func)
|
2005-02-14 23:53:48 +00:00
|
|
|
|
2001-01-26 17:35:36 +00:00
|
|
|
#define EVT_END_PROCESS(id, func) \
|
2005-02-14 23:53:48 +00:00
|
|
|
wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
|
1998-07-01 17:26:46 +00:00
|
|
|
|
2005-02-14 23:53:48 +00:00
|
|
|
#endif // _WX_PROCESSH__
|