Commit | Line | Data |
---|---|---|
6fa6bfa1 SM |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | /* Output argument typemap for connection output (always appends) */ | |
26 | %typemap(in, numinputs=0) | |
e4b56bb9 | 27 | (const bt_connection **) |
6fa6bfa1 SM |
28 | (bt_connection *temp_conn = NULL) { |
29 | $1 = &temp_conn; | |
30 | } | |
31 | ||
32 | %typemap(argout) | |
e4b56bb9 | 33 | (const bt_connection **) { |
6fa6bfa1 SM |
34 | if (*$1) { |
35 | /* SWIG_Python_AppendOutput() steals the created object */ | |
36 | $result = SWIG_Python_AppendOutput($result, | |
37 | SWIG_NewPointerObj(SWIG_as_voidptr(*$1), | |
38 | SWIGTYPE_p_bt_connection, 0)); | |
39 | } else { | |
40 | /* SWIG_Python_AppendOutput() steals Py_None */ | |
41 | Py_INCREF(Py_None); | |
42 | $result = SWIG_Python_AppendOutput($result, Py_None); | |
43 | } | |
44 | } | |
45 | ||
46 | /* Output argument typemap for component output (always appends) */ | |
47 | %typemap(in, numinputs=0) | |
e4b56bb9 | 48 | (const bt_component_source **) |
6fa6bfa1 SM |
49 | (bt_component_source *temp_comp = NULL) { |
50 | $1 = &temp_comp; | |
51 | } | |
52 | ||
53 | %typemap(in, numinputs=0) | |
e4b56bb9 | 54 | (const bt_component_filter **) |
6fa6bfa1 SM |
55 | (bt_component_filter *temp_comp = NULL) { |
56 | $1 = &temp_comp; | |
57 | } | |
58 | ||
59 | %typemap(in, numinputs=0) | |
e4b56bb9 | 60 | (const bt_component_sink **) |
6fa6bfa1 SM |
61 | (bt_component_sink *temp_comp = NULL) { |
62 | $1 = &temp_comp; | |
63 | } | |
64 | ||
e4b56bb9 | 65 | %typemap(argout) (const bt_component_source **) { |
6fa6bfa1 SM |
66 | if (*$1) { |
67 | /* SWIG_Python_AppendOutput() steals the created object */ | |
68 | $result = SWIG_Python_AppendOutput($result, | |
69 | SWIG_NewPointerObj(SWIG_as_voidptr(*$1), | |
70 | SWIGTYPE_p_bt_component_source, 0)); | |
71 | } else { | |
72 | /* SWIG_Python_AppendOutput() steals Py_None */ | |
73 | Py_INCREF(Py_None); | |
74 | $result = SWIG_Python_AppendOutput($result, Py_None); | |
75 | } | |
76 | } | |
77 | ||
e4b56bb9 | 78 | %typemap(argout) (const bt_component_filter **) { |
6fa6bfa1 SM |
79 | if (*$1) { |
80 | /* SWIG_Python_AppendOutput() steals the created object */ | |
81 | $result = SWIG_Python_AppendOutput($result, | |
82 | SWIG_NewPointerObj(SWIG_as_voidptr(*$1), | |
83 | SWIGTYPE_p_bt_component_filter, 0)); | |
84 | } else { | |
85 | /* SWIG_Python_AppendOutput() steals Py_None */ | |
86 | Py_INCREF(Py_None); | |
87 | $result = SWIG_Python_AppendOutput($result, Py_None); | |
88 | } | |
89 | } | |
90 | ||
e4b56bb9 | 91 | %typemap(argout) (const bt_component_sink **) { |
6fa6bfa1 SM |
92 | if (*$1) { |
93 | /* SWIG_Python_AppendOutput() steals the created object */ | |
94 | $result = SWIG_Python_AppendOutput($result, | |
95 | SWIG_NewPointerObj(SWIG_as_voidptr(*$1), | |
96 | SWIGTYPE_p_bt_component_sink, 0)); | |
97 | } else { | |
98 | /* SWIG_Python_AppendOutput() steals Py_None */ | |
99 | Py_INCREF(Py_None); | |
100 | $result = SWIG_Python_AppendOutput($result, Py_None); | |
101 | } | |
102 | } | |
103 | ||
e4b56bb9 SM |
104 | %include <babeltrace2/graph/graph-const.h> |
105 | %include <babeltrace2/graph/graph.h> | |
6fa6bfa1 SM |
106 | |
107 | /* Helper functions for Python */ | |
108 | ||
109 | %{ | |
fb25b9e3 PP |
110 | static |
111 | void graph_listener_removed(void *py_callable) | |
6fa6bfa1 SM |
112 | { |
113 | BT_ASSERT(py_callable); | |
114 | Py_DECREF(py_callable); | |
115 | } | |
116 | ||
fb25b9e3 | 117 | static bt_graph_listener_func_status port_added_listener( |
6fa6bfa1 SM |
118 | const void *component, |
119 | swig_type_info *component_swig_type, | |
e18f3e2b | 120 | bt_component_class_type component_class_type, |
6fa6bfa1 SM |
121 | const void *port, |
122 | swig_type_info *port_swig_type, | |
123 | bt_port_type port_type, | |
124 | void *py_callable) | |
125 | { | |
126 | PyObject *py_component_ptr = NULL; | |
127 | PyObject *py_port_ptr = NULL; | |
128 | PyObject *py_res = NULL; | |
fb25b9e3 | 129 | bt_graph_listener_func_status status; |
6fa6bfa1 SM |
130 | |
131 | py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); | |
132 | if (!py_component_ptr) { | |
133 | BT_LOGF_STR("Failed to create component SWIG pointer object."); | |
fb25b9e3 | 134 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
135 | goto end; |
136 | } | |
137 | ||
138 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); | |
139 | if (!py_port_ptr) { | |
140 | BT_LOGF_STR("Failed to create port SWIG pointer object."); | |
fb25b9e3 | 141 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
142 | goto end; |
143 | } | |
144 | ||
145 | py_res = PyObject_CallFunction(py_callable, "(OiOi)", | |
146 | py_component_ptr, component_class_type, py_port_ptr, port_type); | |
147 | if (!py_res) { | |
3b2be708 | 148 | loge_exception("Graph's port added listener (Python)"); |
6fa6bfa1 | 149 | PyErr_Clear(); |
fb25b9e3 | 150 | status = __BT_FUNC_STATUS_ERROR; |
871a292a | 151 | goto end; |
6fa6bfa1 | 152 | } |
cc81b5ab | 153 | |
871a292a | 154 | BT_ASSERT(py_res == Py_None); |
fb25b9e3 | 155 | status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
156 | |
157 | end: | |
158 | Py_XDECREF(py_res); | |
159 | Py_XDECREF(py_port_ptr); | |
160 | Py_XDECREF(py_component_ptr); | |
e18f3e2b | 161 | return status; |
6fa6bfa1 SM |
162 | } |
163 | ||
fb25b9e3 PP |
164 | static |
165 | bt_graph_listener_func_status | |
6fa6bfa1 SM |
166 | source_component_output_port_added_listener(const bt_component_source *component_source, |
167 | const bt_port_output *port_output, void *py_callable) | |
168 | { | |
e18f3e2b | 169 | return port_added_listener( |
6fa6bfa1 SM |
170 | component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, |
171 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
172 | } | |
173 | ||
fb25b9e3 PP |
174 | static |
175 | bt_graph_listener_func_status | |
6fa6bfa1 SM |
176 | filter_component_input_port_added_listener(const bt_component_filter *component_filter, |
177 | const bt_port_input *port_input, void *py_callable) | |
178 | { | |
e18f3e2b | 179 | return port_added_listener( |
6fa6bfa1 SM |
180 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
181 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
182 | } | |
183 | ||
fb25b9e3 PP |
184 | static |
185 | bt_graph_listener_func_status | |
6fa6bfa1 SM |
186 | filter_component_output_port_added_listener(const bt_component_filter *component_filter, |
187 | const bt_port_output *port_output, void *py_callable) | |
188 | { | |
e18f3e2b | 189 | return port_added_listener( |
6fa6bfa1 SM |
190 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
191 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
192 | } | |
193 | ||
fb25b9e3 PP |
194 | static |
195 | bt_graph_listener_func_status | |
6fa6bfa1 SM |
196 | sink_component_input_port_added_listener(const bt_component_sink *component_sink, |
197 | const bt_port_input *port_input, void *py_callable) | |
198 | { | |
e18f3e2b | 199 | return port_added_listener( |
6fa6bfa1 SM |
200 | component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, |
201 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
202 | } | |
203 | ||
fb25b9e3 PP |
204 | static |
205 | PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, | |
206 | PyObject *py_callable) | |
6fa6bfa1 SM |
207 | { |
208 | PyObject *py_listener_ids = NULL; | |
209 | PyObject *py_listener_id = NULL; | |
eba7ea21 | 210 | bt_listener_id listener_id; |
fb25b9e3 | 211 | bt_graph_add_listener_status status; |
d09c69f7 SM |
212 | const char * const module_name = |
213 | "graph_add_port_added_listener() (Python)"; | |
6fa6bfa1 SM |
214 | |
215 | BT_ASSERT(graph); | |
216 | BT_ASSERT(py_callable); | |
217 | ||
218 | /* | |
219 | * Behind the scene, we will be registering 4 different listeners and | |
220 | * return all of their ids. | |
221 | */ | |
222 | py_listener_ids = PyTuple_New(4); | |
223 | if (!py_listener_ids) { | |
d09c69f7 SM |
224 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
225 | "Failed to allocate one PyTuple."); | |
6fa6bfa1 SM |
226 | goto error; |
227 | } | |
228 | ||
229 | /* source output port */ | |
230 | status = bt_graph_add_source_component_output_port_added_listener( | |
231 | graph, source_component_output_port_added_listener, | |
232 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 233 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
234 | /* |
235 | * bt_graph_add_source_component_output_port_added_listener has | |
236 | * already logged/appended an error cause. | |
237 | */ | |
6fa6bfa1 SM |
238 | goto error; |
239 | } | |
240 | ||
eba7ea21 | 241 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 242 | if (!py_listener_id) { |
d09c69f7 SM |
243 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
244 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
245 | goto error; |
246 | } | |
247 | ||
248 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); | |
249 | py_listener_id = NULL; | |
250 | ||
251 | /* filter input port */ | |
252 | status = bt_graph_add_filter_component_input_port_added_listener( | |
253 | graph, filter_component_input_port_added_listener, | |
254 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 255 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
256 | /* |
257 | * bt_graph_add_filter_component_input_port_added_listener has | |
258 | * already logged/appended an error cause. | |
259 | */ | |
6fa6bfa1 SM |
260 | goto error; |
261 | } | |
262 | ||
eba7ea21 | 263 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 264 | if (!py_listener_id) { |
d09c69f7 SM |
265 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
266 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
267 | goto error; |
268 | } | |
269 | ||
270 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); | |
271 | py_listener_id = NULL; | |
272 | ||
273 | /* filter output port */ | |
274 | status = bt_graph_add_filter_component_output_port_added_listener( | |
275 | graph, filter_component_output_port_added_listener, | |
276 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 277 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
278 | /* |
279 | * bt_graph_add_filter_component_output_port_added_listener has | |
280 | * already logged/appended an error cause. | |
281 | */ | |
6fa6bfa1 SM |
282 | goto error; |
283 | } | |
284 | ||
eba7ea21 | 285 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 286 | if (!py_listener_id) { |
d09c69f7 SM |
287 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
288 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
289 | goto error; |
290 | } | |
291 | ||
292 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); | |
293 | py_listener_id = NULL; | |
294 | ||
295 | /* sink input port */ | |
296 | status = bt_graph_add_sink_component_input_port_added_listener( | |
297 | graph, sink_component_input_port_added_listener, | |
298 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 299 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
300 | /* |
301 | * bt_graph_add_sink_component_input_port_added_listener has | |
302 | * already logged/appended an error cause. | |
303 | */ | |
6fa6bfa1 SM |
304 | goto error; |
305 | } | |
306 | ||
eba7ea21 | 307 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 308 | if (!py_listener_id) { |
d09c69f7 SM |
309 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
310 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
311 | goto error; |
312 | } | |
313 | ||
314 | ||
315 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); | |
316 | py_listener_id = NULL; | |
317 | ||
318 | Py_INCREF(py_callable); | |
319 | Py_INCREF(py_callable); | |
320 | Py_INCREF(py_callable); | |
321 | Py_INCREF(py_callable); | |
322 | ||
323 | goto end; | |
324 | ||
325 | error: | |
326 | Py_XDECREF(py_listener_ids); | |
327 | py_listener_ids = Py_None; | |
328 | Py_INCREF(py_listener_ids); | |
329 | ||
330 | end: | |
331 | ||
332 | Py_XDECREF(py_listener_id); | |
333 | return py_listener_ids; | |
334 | } | |
335 | ||
fb25b9e3 PP |
336 | static |
337 | bt_graph_listener_func_status ports_connected_listener( | |
871a292a SM |
338 | const void *upstream_component, |
339 | swig_type_info *upstream_component_swig_type, | |
340 | bt_component_class_type upstream_component_class_type, | |
341 | const bt_port_output *upstream_port, | |
342 | const void *downstream_component, | |
343 | swig_type_info *downstream_component_swig_type, | |
344 | bt_component_class_type downstream_component_class_type, | |
345 | const bt_port_input *downstream_port, | |
346 | void *py_callable) | |
6fa6bfa1 | 347 | { |
871a292a | 348 | PyObject *py_upstream_component_ptr = NULL; |
6fa6bfa1 | 349 | PyObject *py_upstream_port_ptr = NULL; |
871a292a | 350 | PyObject *py_downstream_component_ptr = NULL; |
6fa6bfa1 SM |
351 | PyObject *py_downstream_port_ptr = NULL; |
352 | PyObject *py_res = NULL; | |
fb25b9e3 | 353 | bt_graph_listener_func_status status; |
871a292a SM |
354 | |
355 | py_upstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(upstream_component), | |
356 | upstream_component_swig_type, 0); | |
357 | if (!py_upstream_component_ptr) { | |
358 | BT_LOGF_STR("Failed to create upstream component SWIG pointer object."); | |
fb25b9e3 | 359 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
871a292a SM |
360 | goto end; |
361 | } | |
6fa6bfa1 SM |
362 | |
363 | py_upstream_port_ptr = SWIG_NewPointerObj( | |
364 | SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port_output, 0); | |
365 | if (!py_upstream_port_ptr) { | |
871a292a | 366 | BT_LOGF_STR("Failed to create upstream port SWIG pointer object."); |
fb25b9e3 | 367 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
871a292a SM |
368 | goto end; |
369 | } | |
cc81b5ab | 370 | |
871a292a SM |
371 | py_downstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(downstream_component), |
372 | downstream_component_swig_type, 0); | |
373 | if (!py_downstream_component_ptr) { | |
374 | BT_LOGF_STR("Failed to create downstream component SWIG pointer object."); | |
fb25b9e3 | 375 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
871a292a | 376 | goto end; |
6fa6bfa1 SM |
377 | } |
378 | ||
379 | py_downstream_port_ptr = SWIG_NewPointerObj( | |
380 | SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port_input, 0); | |
381 | if (!py_downstream_port_ptr) { | |
871a292a | 382 | BT_LOGF_STR("Failed to create downstream port SWIG pointer object."); |
fb25b9e3 | 383 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
871a292a | 384 | goto end; |
6fa6bfa1 SM |
385 | } |
386 | ||
871a292a SM |
387 | py_res = PyObject_CallFunction(py_callable, "(OiOOiO)", |
388 | py_upstream_component_ptr, upstream_component_class_type, | |
389 | py_upstream_port_ptr, | |
390 | py_downstream_component_ptr, downstream_component_class_type, | |
391 | py_downstream_port_ptr); | |
6fa6bfa1 | 392 | if (!py_res) { |
3b2be708 | 393 | loge_exception("Graph's port connected listener (Python)"); |
6fa6bfa1 | 394 | PyErr_Clear(); |
fb25b9e3 | 395 | status = __BT_FUNC_STATUS_ERROR; |
871a292a | 396 | goto end; |
6fa6bfa1 | 397 | } |
cc81b5ab | 398 | |
871a292a | 399 | BT_ASSERT(py_res == Py_None); |
fb25b9e3 | 400 | status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 | 401 | |
871a292a SM |
402 | end: |
403 | Py_XDECREF(py_upstream_component_ptr); | |
404 | Py_XDECREF(py_upstream_port_ptr); | |
405 | Py_XDECREF(py_downstream_component_ptr); | |
406 | Py_XDECREF(py_downstream_port_ptr); | |
6fa6bfa1 | 407 | Py_XDECREF(py_res); |
e18f3e2b | 408 | return status; |
6fa6bfa1 SM |
409 | } |
410 | ||
fb25b9e3 PP |
411 | static |
412 | bt_graph_listener_func_status source_filter_component_ports_connected_listener( | |
6fa6bfa1 SM |
413 | const bt_component_source *source_component, |
414 | const bt_component_filter *filter_component, | |
415 | const bt_port_output *upstream_port, | |
416 | const bt_port_input *downstream_port, void *py_callable) | |
417 | { | |
871a292a SM |
418 | return ports_connected_listener( |
419 | source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, | |
420 | upstream_port, | |
421 | filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
422 | downstream_port, | |
423 | py_callable); | |
6fa6bfa1 SM |
424 | } |
425 | ||
fb25b9e3 PP |
426 | static |
427 | bt_graph_listener_func_status source_sink_component_ports_connected_listener( | |
6fa6bfa1 SM |
428 | const bt_component_source *source_component, |
429 | const bt_component_sink *sink_component, | |
430 | const bt_port_output *upstream_port, | |
431 | const bt_port_input *downstream_port, void *py_callable) | |
432 | { | |
871a292a SM |
433 | return ports_connected_listener( |
434 | source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, | |
435 | upstream_port, | |
436 | sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, | |
437 | downstream_port, | |
438 | py_callable); | |
6fa6bfa1 SM |
439 | } |
440 | ||
fb25b9e3 PP |
441 | static |
442 | bt_graph_listener_func_status filter_filter_component_ports_connected_listener( | |
6fa6bfa1 SM |
443 | const bt_component_filter *filter_component_left, |
444 | const bt_component_filter *filter_component_right, | |
445 | const bt_port_output *upstream_port, | |
446 | const bt_port_input *downstream_port, void *py_callable) | |
447 | { | |
871a292a SM |
448 | return ports_connected_listener( |
449 | filter_component_left, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
450 | upstream_port, | |
451 | filter_component_right, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
452 | downstream_port, | |
453 | py_callable); | |
6fa6bfa1 SM |
454 | } |
455 | ||
fb25b9e3 PP |
456 | static |
457 | bt_graph_listener_func_status filter_sink_component_ports_connected_listener( | |
6fa6bfa1 SM |
458 | const bt_component_filter *filter_component, |
459 | const bt_component_sink *sink_component, | |
460 | const bt_port_output *upstream_port, | |
461 | const bt_port_input *downstream_port, void *py_callable) | |
462 | { | |
871a292a SM |
463 | return ports_connected_listener( |
464 | filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, | |
465 | upstream_port, | |
466 | sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, | |
467 | downstream_port, | |
468 | py_callable); | |
6fa6bfa1 SM |
469 | } |
470 | ||
fb25b9e3 PP |
471 | static |
472 | PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, | |
473 | PyObject *py_callable) | |
6fa6bfa1 SM |
474 | { |
475 | PyObject *py_listener_ids = NULL; | |
476 | PyObject *py_listener_id = NULL; | |
eba7ea21 | 477 | bt_listener_id listener_id; |
fb25b9e3 | 478 | bt_graph_add_listener_status status; |
d09c69f7 SM |
479 | const char * const module_name = |
480 | "graph_add_ports_connected_listener() (Python)"; | |
6fa6bfa1 SM |
481 | |
482 | BT_ASSERT(graph); | |
483 | BT_ASSERT(py_callable); | |
484 | ||
485 | /* Behind the scene, we will be registering 4 different listeners and | |
486 | * return all of their ids. */ | |
487 | py_listener_ids = PyTuple_New(4); | |
488 | if (!py_listener_ids) { | |
d09c69f7 SM |
489 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
490 | "Failed to allocate one PyTuple."); | |
6fa6bfa1 SM |
491 | goto error; |
492 | } | |
493 | ||
494 | /* source -> filter connection */ | |
495 | status = bt_graph_add_source_filter_component_ports_connected_listener( | |
496 | graph, source_filter_component_ports_connected_listener, | |
497 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 498 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
499 | /* |
500 | * bt_graph_add_source_filter_component_ports_connected_listener | |
501 | * has already logged/appended an error cause. | |
502 | */ | |
6fa6bfa1 SM |
503 | goto error; |
504 | } | |
505 | ||
eba7ea21 | 506 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 507 | if (!py_listener_id) { |
d09c69f7 SM |
508 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
509 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
510 | goto error; |
511 | } | |
512 | ||
513 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); | |
514 | py_listener_id = NULL; | |
515 | ||
516 | /* source -> sink connection */ | |
517 | status = bt_graph_add_source_sink_component_ports_connected_listener( | |
518 | graph, source_sink_component_ports_connected_listener, | |
519 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 520 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
521 | /* |
522 | * bt_graph_add_source_sink_component_ports_connected_listener | |
523 | * has already logged/appended an error cause. | |
524 | */ | |
6fa6bfa1 SM |
525 | goto error; |
526 | } | |
527 | ||
eba7ea21 | 528 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 529 | if (!py_listener_id) { |
d09c69f7 SM |
530 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
531 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
532 | goto error; |
533 | } | |
534 | ||
535 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); | |
536 | py_listener_id = NULL; | |
537 | ||
538 | /* filter -> filter connection */ | |
539 | status = bt_graph_add_filter_filter_component_ports_connected_listener( | |
540 | graph, filter_filter_component_ports_connected_listener, | |
541 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 542 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
543 | /* |
544 | * bt_graph_add_filter_filter_component_ports_connected_listener | |
545 | * has already logged/appended an error cause. | |
546 | */ | |
6fa6bfa1 SM |
547 | goto error; |
548 | } | |
549 | ||
eba7ea21 | 550 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 551 | if (!py_listener_id) { |
d09c69f7 SM |
552 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
553 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
554 | goto error; |
555 | } | |
556 | ||
557 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); | |
558 | py_listener_id = NULL; | |
559 | ||
560 | /* filter -> sink connection */ | |
561 | status = bt_graph_add_filter_sink_component_ports_connected_listener( | |
562 | graph, filter_sink_component_ports_connected_listener, | |
563 | graph_listener_removed, py_callable, &listener_id); | |
fb25b9e3 | 564 | if (status != __BT_FUNC_STATUS_OK) { |
d09c69f7 SM |
565 | /* |
566 | * bt_graph_add_filter_sink_component_ports_connected_listener | |
567 | * has already logged/appended an error cause. | |
568 | */ | |
6fa6bfa1 SM |
569 | goto error; |
570 | } | |
571 | ||
eba7ea21 | 572 | py_listener_id = PyLong_FromUnsignedLongLong(listener_id); |
6fa6bfa1 | 573 | if (!py_listener_id) { |
d09c69f7 SM |
574 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
575 | "Failed to allocate one PyLong."); | |
6fa6bfa1 SM |
576 | goto error; |
577 | } | |
578 | ||
579 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); | |
580 | py_listener_id = NULL; | |
581 | ||
582 | Py_INCREF(py_callable); | |
583 | Py_INCREF(py_callable); | |
584 | Py_INCREF(py_callable); | |
585 | Py_INCREF(py_callable); | |
586 | ||
587 | goto end; | |
588 | ||
589 | error: | |
590 | Py_XDECREF(py_listener_ids); | |
591 | py_listener_ids = Py_None; | |
592 | Py_INCREF(py_listener_ids); | |
593 | ||
594 | end: | |
595 | ||
596 | Py_XDECREF(py_listener_id); | |
597 | return py_listener_ids; | |
598 | } | |
6fa6bfa1 SM |
599 | %} |
600 | ||
fb25b9e3 | 601 | PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph, |
6fa6bfa1 | 602 | PyObject *py_callable); |
fb25b9e3 | 603 | PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph, |
6fa6bfa1 | 604 | PyObject *py_callable); |