| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: _process.i |
| 3 | // Purpose: SWIG interface stuff for wxProcess and wxExecute |
| 4 | // |
| 5 | // Author: Robin Dunn |
| 6 | // |
| 7 | // Created: 18-June-1999 |
| 8 | // RCS-ID: $Id$ |
| 9 | // Copyright: (c) 2003 by Total Control Software |
| 10 | // Licence: wxWindows license |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | // Not a %module |
| 14 | |
| 15 | |
| 16 | //--------------------------------------------------------------------------- |
| 17 | %newgroup |
| 18 | |
| 19 | %{ |
| 20 | %} |
| 21 | |
| 22 | //--------------------------------------------------------------------------- |
| 23 | |
| 24 | enum |
| 25 | { |
| 26 | // no redirection |
| 27 | wxPROCESS_DEFAULT = 0, |
| 28 | |
| 29 | // redirect the IO of the child process |
| 30 | wxPROCESS_REDIRECT = 1 |
| 31 | }; |
| 32 | |
| 33 | enum wxKillError |
| 34 | { |
| 35 | wxKILL_OK, // no error |
| 36 | wxKILL_BAD_SIGNAL, // no such signal |
| 37 | wxKILL_ACCESS_DENIED, // permission denied |
| 38 | wxKILL_NO_PROCESS, // no such process |
| 39 | wxKILL_ERROR // another, unspecified error |
| 40 | }; |
| 41 | |
| 42 | enum wxKillFlags |
| 43 | { |
| 44 | wxKILL_NOCHILDREN = 0, // don't kill children |
| 45 | wxKILL_CHILDREN = 1 // kill children |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | enum wxSignal |
| 50 | { |
| 51 | wxSIGNONE = 0, // verify if the process exists under Unix |
| 52 | wxSIGHUP, |
| 53 | wxSIGINT, |
| 54 | wxSIGQUIT, |
| 55 | wxSIGILL, |
| 56 | wxSIGTRAP, |
| 57 | wxSIGABRT, |
| 58 | wxSIGIOT = wxSIGABRT, // another name |
| 59 | wxSIGEMT, |
| 60 | wxSIGFPE, |
| 61 | wxSIGKILL, |
| 62 | wxSIGBUS, |
| 63 | wxSIGSEGV, |
| 64 | wxSIGSYS, |
| 65 | wxSIGPIPE, |
| 66 | wxSIGALRM, |
| 67 | wxSIGTERM |
| 68 | |
| 69 | // further signals are different in meaning between different Unix systems |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | //--------------------------------------------------------------------------- |
| 74 | |
| 75 | |
| 76 | %{ |
| 77 | IMP_PYCALLBACK_VOID_INTINT( wxPyProcess, wxProcess, OnTerminate); |
| 78 | %} |
| 79 | |
| 80 | |
| 81 | %rename(Process) wxPyProcess; |
| 82 | class wxPyProcess : public wxEvtHandler { |
| 83 | public: |
| 84 | // kill the process with the given PID |
| 85 | static wxKillError Kill(int pid, |
| 86 | wxSignal sig = wxSIGTERM, |
| 87 | int flags = wxKILL_NOCHILDREN); |
| 88 | |
| 89 | // test if the given process exists |
| 90 | static bool Exists(int pid); |
| 91 | |
| 92 | // this function replaces the standard popen() one: it launches a process |
| 93 | // asynchronously and allows the caller to get the streams connected to its |
| 94 | // std{in|out|err} |
| 95 | // |
| 96 | // on error NULL is returned, in any case the process object will be |
| 97 | // deleted automatically when the process terminates and should *not* be |
| 98 | // deleted by the caller |
| 99 | static wxPyProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); |
| 100 | |
| 101 | |
| 102 | %pythonAppend wxPyProcess "self._setCallbackInfo(self, Process)" |
| 103 | wxPyProcess(wxEvtHandler *parent = NULL, int id = -1); |
| 104 | ~wxPyProcess(); |
| 105 | |
| 106 | |
| 107 | DocDeclStr( |
| 108 | long , GetPid() const, |
| 109 | "get the process ID of the process executed by Open()", ""); |
| 110 | |
| 111 | |
| 112 | |
| 113 | void _setCallbackInfo(PyObject* self, PyObject* _class); |
| 114 | |
| 115 | void OnTerminate(int pid, int status); |
| 116 | %MAKE_BASE_FUNC(Process, OnTerminate); |
| 117 | |
| 118 | // call Redirect before passing the object to wxExecute() to redirect the |
| 119 | // launched process stdin/stdout, then use GetInputStream() and |
| 120 | // GetOutputStream() to get access to them |
| 121 | void Redirect(); |
| 122 | bool IsRedirected(); |
| 123 | |
| 124 | |
| 125 | // detach from the parent - should be called by the parent if it's deleted |
| 126 | // before the process it started terminates |
| 127 | void Detach(); |
| 128 | |
| 129 | wxInputStream *GetInputStream(); |
| 130 | wxInputStream *GetErrorStream(); |
| 131 | wxOutputStream *GetOutputStream(); |
| 132 | |
| 133 | void CloseOutput(); |
| 134 | |
| 135 | // return True if the child process stdout is not closed |
| 136 | bool IsInputOpened() const; |
| 137 | |
| 138 | // return True if any input is available on the child process stdout/err |
| 139 | bool IsInputAvailable() const; |
| 140 | bool IsErrorAvailable() const; |
| 141 | |
| 142 | %property(ErrorStream, GetErrorStream, doc="See `GetErrorStream`"); |
| 143 | %property(InputStream, GetInputStream, doc="See `GetInputStream`"); |
| 144 | %property(OutputStream, GetOutputStream, doc="See `GetOutputStream`"); |
| 145 | |
| 146 | %property(InputOpened, IsInputOpened); |
| 147 | %property(InputAvailable, IsInputAvailable); |
| 148 | %property(ErrorAvailable, IsErrorAvailable); |
| 149 | }; |
| 150 | |
| 151 | //--------------------------------------------------------------------------- |
| 152 | |
| 153 | |
| 154 | class wxProcessEvent : public wxEvent { |
| 155 | public: |
| 156 | wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0); |
| 157 | int GetPid(); |
| 158 | int GetExitCode(); |
| 159 | int m_pid, m_exitcode; |
| 160 | |
| 161 | %property(ExitCode, GetExitCode, doc="See `GetExitCode`"); |
| 162 | %property(Pid, GetPid, doc="See `GetPid`"); |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | %constant wxEventType wxEVT_END_PROCESS; |
| 167 | |
| 168 | %pythoncode { |
| 169 | EVT_END_PROCESS = wx.PyEventBinder( wxEVT_END_PROCESS, 1 ) |
| 170 | } |
| 171 | |
| 172 | //--------------------------------------------------------------------------- |
| 173 | |
| 174 | enum |
| 175 | { |
| 176 | // execute the process asynchronously |
| 177 | wxEXEC_ASYNC = 0, |
| 178 | |
| 179 | // execute it synchronously, i.e. wait until it finishes |
| 180 | wxEXEC_SYNC = 1, |
| 181 | |
| 182 | // under Windows, don't hide the child even if it's IO is redirected (this |
| 183 | // is done by default) |
| 184 | wxEXEC_NOHIDE = 2, |
| 185 | |
| 186 | // under Unix, if the process is the group leader then killing -pid kills |
| 187 | // all children as well as pid |
| 188 | wxEXEC_MAKE_GROUP_LEADER = 4, |
| 189 | |
| 190 | // by default synchronous execution disables all program windows to avoid |
| 191 | // that the user interacts with the program while the child process is |
| 192 | // running, you can use this flag to prevent this from happening |
| 193 | wxEXEC_NODISABLE = 8 |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | MustHaveApp(wxExecute); |
| 198 | |
| 199 | long wxExecute(const wxString& command, |
| 200 | int flags = wxEXEC_ASYNC, |
| 201 | wxPyProcess *process = NULL); |
| 202 | |
| 203 | |
| 204 | |
| 205 | %typemap(in,numinputs=0) wxKillError* rc ( wxKillError temp ) { $1 = &temp; } |
| 206 | %typemap(argout) wxKillError* rc |
| 207 | { |
| 208 | PyObject* o; |
| 209 | o = PyInt_FromLong((long) (*$1)); |
| 210 | #if SWIG_VERSION < 0x010328 |
| 211 | $result = t_output_helper($result, o); |
| 212 | #else |
| 213 | $result = SWIG_Python_AppendOutput($result, o); |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | int wxKill(long pid, wxSignal sig = wxSIGTERM, wxKillError* rc, int flags = wxKILL_NOCHILDREN); |
| 218 | |
| 219 | |
| 220 | //--------------------------------------------------------------------------- |
| 221 | %init %{ |
| 222 | wxPyPtrTypeMap_Add("wxProcess", "wxPyProcess"); |
| 223 | %} |
| 224 | //--------------------------------------------------------------------------- |