]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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 | ||
c1718b2c RD |
42 | enum wxKillFlags |
43 | { | |
44 | wxKILL_NOCHILDREN = 0, // don't kill children | |
45 | wxKILL_CHILDREN = 1 // kill children | |
46 | }; | |
47 | ||
48 | ||
d14a1e28 RD |
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 | %name(Process)class wxPyProcess : public wxEvtHandler { | |
82 | public: | |
83 | // kill the process with the given PID | |
c1718b2c RD |
84 | static wxKillError Kill(int pid, |
85 | wxSignal sig = wxSIGTERM, | |
86 | int flags = wxKILL_NOCHILDREN); | |
d14a1e28 RD |
87 | |
88 | // test if the given process exists | |
89 | static bool Exists(int pid); | |
90 | ||
91 | // this function replaces the standard popen() one: it launches a process | |
92 | // asynchronously and allows the caller to get the streams connected to its | |
93 | // std{in|out|err} | |
94 | // | |
95 | // on error NULL is returned, in any case the process object will be | |
96 | // deleted automatically when the process terminates and should *not* be | |
97 | // deleted by the caller | |
98 | static wxPyProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); | |
99 | ||
100 | ||
2b9048c5 | 101 | %pythonAppend wxPyProcess "self._setCallbackInfo(self, Process)" |
d14a1e28 RD |
102 | wxPyProcess(wxEvtHandler *parent = NULL, int id = -1); |
103 | ||
104 | void _setCallbackInfo(PyObject* self, PyObject* _class); | |
105 | ||
106 | void base_OnTerminate(int pid, int status); | |
107 | ||
108 | // call Redirect before passing the object to wxExecute() to redirect the | |
109 | // launched process stdin/stdout, then use GetInputStream() and | |
110 | // GetOutputStream() to get access to them | |
111 | void Redirect(); | |
112 | bool IsRedirected(); | |
113 | ||
114 | ||
115 | // detach from the parent - should be called by the parent if it's deleted | |
116 | // before the process it started terminates | |
117 | void Detach(); | |
118 | ||
119 | wxInputStream *GetInputStream(); | |
120 | wxInputStream *GetErrorStream(); | |
121 | wxOutputStream *GetOutputStream(); | |
122 | ||
123 | void CloseOutput(); | |
124 | ||
dd9f7fea | 125 | // return True if the child process stdout is not closed |
d14a1e28 RD |
126 | bool IsInputOpened() const; |
127 | ||
dd9f7fea | 128 | // return True if any input is available on the child process stdout/err |
d14a1e28 RD |
129 | bool IsInputAvailable() const; |
130 | bool IsErrorAvailable() const; | |
131 | }; | |
132 | ||
133 | //--------------------------------------------------------------------------- | |
134 | ||
135 | ||
136 | class wxProcessEvent : public wxEvent { | |
137 | public: | |
138 | wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0); | |
139 | int GetPid(); | |
140 | int GetExitCode(); | |
141 | int m_pid, m_exitcode; | |
142 | }; | |
143 | ||
144 | ||
145 | %constant wxEventType wxEVT_END_PROCESS; | |
146 | ||
147 | %pythoncode { | |
148 | EVT_END_PROCESS = wx.PyEventBinder( wxEVT_END_PROCESS, 1 ) | |
149 | } | |
150 | ||
151 | //--------------------------------------------------------------------------- | |
152 | ||
153 | enum | |
154 | { | |
155 | // execute the process asynchronously | |
156 | wxEXEC_ASYNC = 0, | |
157 | ||
158 | // execute it synchronously, i.e. wait until it finishes | |
159 | wxEXEC_SYNC = 1, | |
160 | ||
161 | // under Windows, don't hide the child even if it's IO is redirected (this | |
162 | // is done by default) | |
163 | wxEXEC_NOHIDE = 2, | |
164 | ||
165 | // under Unix, if the process is the group leader then killing -pid kills | |
166 | // all children as well as pid | |
167 | wxEXEC_MAKE_GROUP_LEADER = 4 | |
168 | }; | |
169 | ||
170 | ||
ab1f7d2a RD |
171 | MustHaveApp(wxExecute); |
172 | ||
d14a1e28 RD |
173 | long wxExecute(const wxString& command, |
174 | int flags = wxEXEC_ASYNC, | |
175 | wxPyProcess *process = NULL); | |
176 | ||
177 | ||
c1718b2c RD |
178 | |
179 | %typemap(in,numinputs=0) wxKillError* rc ( wxKillError temp ) { $1 = &temp; } | |
180 | %typemap(argout) wxKillError* rc | |
181 | { | |
182 | PyObject* o; | |
183 | o = PyInt_FromLong((long) (*$1)); | |
184 | $result = t_output_helper($result, o); | |
185 | } | |
186 | ||
187 | int wxKill(long pid, wxSignal sig = wxSIGTERM, wxKillError* rc, int flags = wxKILL_NOCHILDREN); | |
188 | ||
189 | ||
d14a1e28 RD |
190 | //--------------------------------------------------------------------------- |
191 | %init %{ | |
192 | wxPyPtrTypeMap_Add("wxProcess", "wxPyProcess"); | |
193 | %} | |
194 | //--------------------------------------------------------------------------- |