Commit | Line | Data |
---|---|---|
6945df9a 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) | |
27 | (const bt_connection **BTOUTCONN) | |
28 | (bt_connection *temp_conn = NULL) { | |
29 | $1 = &temp_conn; | |
30 | } | |
31 | ||
32 | %typemap(argout) | |
33 | (const bt_connection **BTOUTCONN) { | |
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) | |
48 | (const bt_component_source **OUT) | |
49 | (bt_component_source *temp_comp = NULL) { | |
50 | $1 = &temp_comp; | |
51 | } | |
52 | ||
53 | %typemap(in, numinputs=0) | |
54 | (const bt_component_filter **OUT) | |
55 | (bt_component_filter *temp_comp = NULL) { | |
56 | $1 = &temp_comp; | |
57 | } | |
58 | ||
59 | %typemap(in, numinputs=0) | |
60 | (const bt_component_sink **OUT) | |
61 | (bt_component_sink *temp_comp = NULL) { | |
62 | $1 = &temp_comp; | |
63 | } | |
64 | ||
65 | %typemap(argout) (const bt_component_source **OUT) { | |
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 | ||
78 | %typemap(argout) (const bt_component_filter **OUT) { | |
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 | ||
91 | %typemap(argout) (const bt_component_sink **OUT) { | |
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 | ||
104 | /* From graph-const.h */ | |
105 | ||
106 | typedef enum bt_graph_status { | |
107 | BT_GRAPH_STATUS_OK = 0, | |
108 | BT_GRAPH_STATUS_END = 1, | |
109 | BT_GRAPH_STATUS_AGAIN = 11, | |
110 | BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION = 111, | |
111 | BT_GRAPH_STATUS_CANCELED = 125, | |
112 | BT_GRAPH_STATUS_ERROR = -1, | |
113 | BT_GRAPH_STATUS_NOMEM = -12, | |
114 | } bt_graph_status; | |
115 | ||
116 | extern bt_bool bt_graph_is_canceled(const bt_graph *graph); | |
117 | ||
118 | extern void bt_graph_get_ref(const bt_graph *graph); | |
119 | ||
120 | extern void bt_graph_put_ref(const bt_graph *graph); | |
121 | ||
122 | /* From graph.h */ | |
123 | ||
8cc56726 SM |
124 | typedef enum bt_graph_listener_status { |
125 | BT_GRAPH_LISTENER_STATUS_OK = 0, | |
126 | BT_GRAPH_LISTENER_STATUS_ERROR = -1, | |
127 | BT_GRAPH_LISTENER_STATUS_NOMEM = -12, | |
128 | } bt_graph_listener_status; | |
129 | ||
130 | ||
131 | typedef bt_graph_listener_status | |
132 | (*bt_graph_filter_component_input_port_added_listener_func)( | |
6945df9a SM |
133 | const bt_component_filter *component, |
134 | const bt_port_input *port, void *data); | |
135 | ||
8cc56726 SM |
136 | typedef bt_graph_listener_status |
137 | (*bt_graph_sink_component_input_port_added_listener_func)( | |
6945df9a SM |
138 | const bt_component_sink *component, |
139 | const bt_port_input *port, void *data); | |
140 | ||
8cc56726 SM |
141 | typedef bt_graph_listener_status |
142 | (*bt_graph_source_component_output_port_added_listener_func)( | |
6945df9a SM |
143 | const bt_component_source *component, |
144 | const bt_port_output *port, void *data); | |
145 | ||
8cc56726 SM |
146 | typedef bt_graph_listener_status |
147 | (*bt_graph_filter_component_output_port_added_listener_func)( | |
6945df9a SM |
148 | const bt_component_filter *component, |
149 | const bt_port_output *port, void *data); | |
150 | ||
8cc56726 SM |
151 | typedef bt_graph_listener_status |
152 | (*bt_graph_source_filter_component_ports_connected_listener_func)( | |
6945df9a SM |
153 | const bt_component_source *source_component, |
154 | const bt_component_filter *filter_component, | |
155 | const bt_port_output *upstream_port, | |
156 | const bt_port_input *downstream_port, void *data); | |
157 | ||
8cc56726 SM |
158 | typedef bt_graph_listener_status |
159 | (*bt_graph_source_sink_component_ports_connected_listener_func)( | |
6945df9a SM |
160 | const bt_component_source *source_component, |
161 | const bt_component_sink *sink_component, | |
162 | const bt_port_output *upstream_port, | |
163 | const bt_port_input *downstream_port, void *data); | |
164 | ||
8cc56726 SM |
165 | typedef bt_graph_listener_status |
166 | (*bt_graph_filter_filter_component_ports_connected_listener_func)( | |
6945df9a SM |
167 | const bt_component_filter *filter_component_upstream, |
168 | const bt_component_filter *filter_component_downstream, | |
169 | const bt_port_output *upstream_port, | |
170 | const bt_port_input *downstream_port, | |
171 | void *data); | |
172 | ||
8cc56726 SM |
173 | typedef bt_graph_listener_status |
174 | (*bt_graph_filter_sink_component_ports_connected_listener_func)( | |
6945df9a SM |
175 | const bt_component_filter *filter_component, |
176 | const bt_component_sink *sink_component, | |
177 | const bt_port_output *upstream_port, | |
178 | const bt_port_input *downstream_port, void *data); | |
179 | ||
180 | typedef void (* bt_graph_listener_removed_func)(void *data); | |
181 | ||
182 | extern bt_graph *bt_graph_create(void); | |
183 | ||
184 | extern bt_graph_status bt_graph_add_source_component(bt_graph *graph, | |
185 | const bt_component_class_source *component_class, | |
186 | const char *name, const bt_value *params, | |
187 | const bt_component_source **OUT); | |
188 | ||
189 | extern bt_graph_status bt_graph_add_source_component_with_init_method_data( | |
190 | bt_graph *graph, | |
191 | const bt_component_class_source *component_class, | |
192 | const char *name, const bt_value *params, | |
193 | void *init_method_data, | |
194 | const bt_component_source **OUT); | |
195 | ||
196 | extern bt_graph_status bt_graph_add_filter_component(bt_graph *graph, | |
197 | const bt_component_class_filter *component_class, | |
198 | const char *name, const bt_value *params, | |
199 | const bt_component_filter **OUT); | |
200 | ||
201 | extern bt_graph_status bt_graph_add_filter_component_with_init_method_data( | |
202 | bt_graph *graph, | |
203 | const bt_component_class_filter *component_class, | |
204 | const char *name, const bt_value *params, | |
205 | void *init_method_data, | |
206 | const bt_component_filter **OUT); | |
207 | ||
208 | extern bt_graph_status bt_graph_add_sink_component( | |
209 | bt_graph *graph, const bt_component_class_sink *component_class, | |
210 | const char *name, const bt_value *params, | |
211 | const bt_component_sink **OUT); | |
212 | ||
213 | extern bt_graph_status bt_graph_add_sink_component_with_init_method_data( | |
214 | bt_graph *graph, const bt_component_class_sink *component_class, | |
215 | const char *name, const bt_value *params, | |
216 | void *init_method_data, | |
217 | const bt_component_sink **OUT); | |
218 | ||
219 | extern bt_graph_status bt_graph_connect_ports(bt_graph *graph, | |
220 | const bt_port_output *upstream, | |
221 | const bt_port_input *downstream, | |
222 | const bt_connection **BTOUTCONN); | |
223 | ||
224 | extern bt_graph_status bt_graph_run(bt_graph *graph); | |
225 | ||
226 | extern bt_graph_status bt_graph_consume(bt_graph *graph); | |
227 | ||
228 | extern bt_graph_status bt_graph_add_filter_component_input_port_added_listener( | |
229 | bt_graph *graph, | |
230 | bt_graph_filter_component_input_port_added_listener_func listener, | |
231 | bt_graph_listener_removed_func listener_removed, void *data, | |
232 | int *listener_id); | |
233 | ||
234 | extern bt_graph_status bt_graph_add_sink_component_input_port_added_listener( | |
235 | bt_graph *graph, | |
236 | bt_graph_sink_component_input_port_added_listener_func listener, | |
237 | bt_graph_listener_removed_func listener_removed, void *data, | |
238 | int *listener_id); | |
239 | ||
240 | extern bt_graph_status bt_graph_add_source_component_output_port_added_listener( | |
241 | bt_graph *graph, | |
242 | bt_graph_source_component_output_port_added_listener_func listener, | |
243 | bt_graph_listener_removed_func listener_removed, void *data, | |
244 | int *listener_id); | |
245 | ||
246 | extern bt_graph_status bt_graph_add_filter_component_output_port_added_listener( | |
247 | bt_graph *graph, | |
248 | bt_graph_filter_component_output_port_added_listener_func listener, | |
249 | bt_graph_listener_removed_func listener_removed, void *data, | |
250 | int *listener_id); | |
251 | ||
252 | extern bt_graph_status | |
253 | bt_graph_add_source_filter_component_ports_connected_listener( | |
254 | bt_graph *graph, | |
255 | bt_graph_source_filter_component_ports_connected_listener_func listener, | |
256 | bt_graph_listener_removed_func listener_removed, void *data, | |
257 | int *listener_id); | |
258 | ||
259 | extern bt_graph_status | |
260 | bt_graph_add_filter_filter_component_ports_connected_listener( | |
261 | bt_graph *graph, | |
262 | bt_graph_filter_filter_component_ports_connected_listener_func listener, | |
263 | bt_graph_listener_removed_func listener_removed, void *data, | |
264 | int *listener_id); | |
265 | ||
266 | extern bt_graph_status | |
267 | bt_graph_add_source_sink_component_ports_connected_listener( | |
268 | bt_graph *graph, | |
269 | bt_graph_source_sink_component_ports_connected_listener_func listener, | |
270 | bt_graph_listener_removed_func listener_removed, void *data, | |
271 | int *listener_id); | |
272 | ||
273 | extern bt_graph_status | |
274 | bt_graph_add_filter_sink_component_ports_connected_listener( | |
275 | bt_graph *graph, | |
276 | bt_graph_filter_sink_component_ports_connected_listener_func listener, | |
277 | bt_graph_listener_removed_func listener_removed, void *data, | |
278 | int *listener_id); | |
279 | ||
280 | extern bt_graph_status bt_graph_cancel(bt_graph *graph); | |
281 | ||
282 | /* Helper functions for Python */ | |
283 | ||
284 | %{ | |
285 | ||
286 | static void graph_listener_removed(void *py_callable) | |
287 | { | |
288 | BT_ASSERT(py_callable); | |
289 | Py_DECREF(py_callable); | |
290 | } | |
291 | ||
8cc56726 | 292 | static bt_graph_listener_status |
6945df9a SM |
293 | port_added_listener( |
294 | const void *component, | |
295 | swig_type_info *component_swig_type, | |
8cc56726 | 296 | bt_component_class_type component_class_type, |
6945df9a SM |
297 | const void *port, |
298 | swig_type_info *port_swig_type, | |
299 | bt_port_type port_type, | |
300 | void *py_callable) | |
301 | { | |
302 | PyObject *py_component_ptr = NULL; | |
303 | PyObject *py_port_ptr = NULL; | |
304 | PyObject *py_res = NULL; | |
8cc56726 | 305 | bt_graph_listener_status status = BT_GRAPH_LISTENER_STATUS_OK; |
6945df9a SM |
306 | |
307 | py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0); | |
308 | if (!py_component_ptr) { | |
309 | BT_LOGF_STR("Failed to create component SWIG pointer object."); | |
310 | goto end; | |
311 | } | |
312 | ||
313 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0); | |
314 | if (!py_port_ptr) { | |
315 | BT_LOGF_STR("Failed to create port SWIG pointer object."); | |
316 | goto end; | |
317 | } | |
318 | ||
319 | py_res = PyObject_CallFunction(py_callable, "(OiOi)", | |
320 | py_component_ptr, component_class_type, py_port_ptr, port_type); | |
321 | if (!py_res) { | |
322 | bt2_py_loge_exception(); | |
323 | PyErr_Clear(); | |
324 | } else { | |
325 | BT_ASSERT(py_res == Py_None); | |
326 | } | |
327 | ||
328 | end: | |
329 | Py_XDECREF(py_res); | |
330 | Py_XDECREF(py_port_ptr); | |
331 | Py_XDECREF(py_component_ptr); | |
8cc56726 SM |
332 | |
333 | return status; | |
6945df9a SM |
334 | } |
335 | ||
8cc56726 | 336 | static bt_graph_listener_status |
6945df9a SM |
337 | source_component_output_port_added_listener(const bt_component_source *component_source, |
338 | const bt_port_output *port_output, void *py_callable) | |
339 | { | |
8cc56726 | 340 | return port_added_listener( |
6945df9a SM |
341 | component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE, |
342 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
343 | } | |
344 | ||
8cc56726 | 345 | static bt_graph_listener_status |
6945df9a SM |
346 | filter_component_input_port_added_listener(const bt_component_filter *component_filter, |
347 | const bt_port_input *port_input, void *py_callable) | |
348 | { | |
8cc56726 | 349 | return port_added_listener( |
6945df9a SM |
350 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
351 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
352 | } | |
353 | ||
8cc56726 | 354 | static bt_graph_listener_status |
6945df9a SM |
355 | filter_component_output_port_added_listener(const bt_component_filter *component_filter, |
356 | const bt_port_output *port_output, void *py_callable) | |
357 | { | |
8cc56726 | 358 | return port_added_listener( |
6945df9a SM |
359 | component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER, |
360 | port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable); | |
361 | } | |
362 | ||
8cc56726 | 363 | static bt_graph_listener_status |
6945df9a SM |
364 | sink_component_input_port_added_listener(const bt_component_sink *component_sink, |
365 | const bt_port_input *port_input, void *py_callable) | |
366 | { | |
8cc56726 | 367 | return port_added_listener( |
6945df9a SM |
368 | component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK, |
369 | port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable); | |
370 | } | |
371 | ||
372 | static PyObject * | |
373 | bt_py3_graph_add_port_added_listener(struct bt_graph *graph, | |
374 | PyObject *py_callable) | |
375 | { | |
376 | PyObject *py_listener_ids = NULL; | |
377 | PyObject *py_listener_id = NULL; | |
378 | int listener_id; | |
379 | bt_graph_status status; | |
380 | ||
381 | BT_ASSERT(graph); | |
382 | BT_ASSERT(py_callable); | |
383 | ||
384 | /* | |
385 | * Behind the scene, we will be registering 4 different listeners and | |
386 | * return all of their ids. | |
387 | */ | |
388 | py_listener_ids = PyTuple_New(4); | |
389 | if (!py_listener_ids) { | |
390 | goto error; | |
391 | } | |
392 | ||
393 | /* source output port */ | |
394 | status = bt_graph_add_source_component_output_port_added_listener( | |
395 | graph, source_component_output_port_added_listener, | |
396 | graph_listener_removed, py_callable, &listener_id); | |
397 | if (status != BT_GRAPH_STATUS_OK) { | |
398 | goto error; | |
399 | } | |
400 | ||
401 | py_listener_id = PyLong_FromLong(listener_id); | |
402 | if (!py_listener_id) { | |
403 | goto error; | |
404 | } | |
405 | ||
406 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); | |
407 | py_listener_id = NULL; | |
408 | ||
409 | /* filter input port */ | |
410 | status = bt_graph_add_filter_component_input_port_added_listener( | |
411 | graph, filter_component_input_port_added_listener, | |
412 | graph_listener_removed, py_callable, &listener_id); | |
413 | if (status != BT_GRAPH_STATUS_OK) { | |
414 | goto error; | |
415 | } | |
416 | ||
417 | py_listener_id = PyLong_FromLong(listener_id); | |
418 | if (!py_listener_id) { | |
419 | goto error; | |
420 | } | |
421 | ||
422 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); | |
423 | py_listener_id = NULL; | |
424 | ||
425 | /* filter output port */ | |
426 | status = bt_graph_add_filter_component_output_port_added_listener( | |
427 | graph, filter_component_output_port_added_listener, | |
428 | graph_listener_removed, py_callable, &listener_id); | |
429 | if (status != BT_GRAPH_STATUS_OK) { | |
430 | goto error; | |
431 | } | |
432 | ||
433 | py_listener_id = PyLong_FromLong(listener_id); | |
434 | if (!py_listener_id) { | |
435 | goto error; | |
436 | } | |
437 | ||
438 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); | |
439 | py_listener_id = NULL; | |
440 | ||
441 | /* sink input port */ | |
442 | status = bt_graph_add_sink_component_input_port_added_listener( | |
443 | graph, sink_component_input_port_added_listener, | |
444 | graph_listener_removed, py_callable, &listener_id); | |
445 | if (status != BT_GRAPH_STATUS_OK) { | |
446 | goto error; | |
447 | } | |
448 | ||
449 | py_listener_id = PyLong_FromLong(listener_id); | |
450 | if (!py_listener_id) { | |
451 | goto error; | |
452 | } | |
453 | ||
454 | ||
455 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); | |
456 | py_listener_id = NULL; | |
457 | ||
458 | Py_INCREF(py_callable); | |
459 | Py_INCREF(py_callable); | |
460 | Py_INCREF(py_callable); | |
461 | Py_INCREF(py_callable); | |
462 | ||
463 | goto end; | |
464 | ||
465 | error: | |
466 | Py_XDECREF(py_listener_ids); | |
467 | py_listener_ids = Py_None; | |
468 | Py_INCREF(py_listener_ids); | |
469 | ||
470 | end: | |
471 | ||
472 | Py_XDECREF(py_listener_id); | |
473 | return py_listener_ids; | |
474 | } | |
475 | ||
8cc56726 | 476 | static bt_graph_listener_status |
6945df9a SM |
477 | ports_connected_listener(const bt_port_output *upstream_port, |
478 | const bt_port_input *downstream_port, | |
479 | void *py_callable) | |
480 | { | |
481 | PyObject *py_upstream_port_ptr = NULL; | |
482 | PyObject *py_downstream_port_ptr = NULL; | |
483 | PyObject *py_res = NULL; | |
8cc56726 | 484 | bt_graph_listener_status status = BT_GRAPH_LISTENER_STATUS_OK; |
6945df9a SM |
485 | |
486 | py_upstream_port_ptr = SWIG_NewPointerObj( | |
487 | SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port_output, 0); | |
488 | if (!py_upstream_port_ptr) { | |
489 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
490 | abort(); | |
491 | } | |
492 | ||
493 | py_downstream_port_ptr = SWIG_NewPointerObj( | |
494 | SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port_input, 0); | |
495 | if (!py_downstream_port_ptr) { | |
496 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
497 | abort(); | |
498 | } | |
499 | ||
500 | py_res = PyObject_CallFunction(py_callable, "(OO)", | |
501 | py_upstream_port_ptr, py_downstream_port_ptr); | |
502 | if (!py_res) { | |
503 | bt2_py_loge_exception(); | |
504 | PyErr_Clear(); | |
505 | } else { | |
506 | BT_ASSERT(py_res == Py_None); | |
507 | } | |
508 | ||
509 | Py_DECREF(py_upstream_port_ptr); | |
510 | Py_DECREF(py_downstream_port_ptr); | |
511 | Py_XDECREF(py_res); | |
8cc56726 SM |
512 | |
513 | return status; | |
6945df9a SM |
514 | } |
515 | ||
8cc56726 | 516 | static bt_graph_listener_status |
6945df9a SM |
517 | source_filter_component_ports_connected_listener( |
518 | const bt_component_source *source_component, | |
519 | const bt_component_filter *filter_component, | |
520 | const bt_port_output *upstream_port, | |
521 | const bt_port_input *downstream_port, void *py_callable) | |
522 | { | |
8cc56726 SM |
523 | return ports_connected_listener(upstream_port, downstream_port, |
524 | py_callable); | |
6945df9a SM |
525 | } |
526 | ||
8cc56726 | 527 | static bt_graph_listener_status |
6945df9a SM |
528 | source_sink_component_ports_connected_listener( |
529 | const bt_component_source *source_component, | |
530 | const bt_component_sink *sink_component, | |
531 | const bt_port_output *upstream_port, | |
532 | const bt_port_input *downstream_port, void *py_callable) | |
533 | { | |
8cc56726 SM |
534 | return ports_connected_listener(upstream_port, downstream_port, |
535 | py_callable); | |
6945df9a SM |
536 | } |
537 | ||
8cc56726 | 538 | static bt_graph_listener_status |
6945df9a SM |
539 | filter_filter_component_ports_connected_listener( |
540 | const bt_component_filter *filter_component_left, | |
541 | const bt_component_filter *filter_component_right, | |
542 | const bt_port_output *upstream_port, | |
543 | const bt_port_input *downstream_port, void *py_callable) | |
544 | { | |
8cc56726 SM |
545 | return ports_connected_listener(upstream_port, downstream_port, |
546 | py_callable); | |
6945df9a SM |
547 | } |
548 | ||
8cc56726 | 549 | static bt_graph_listener_status |
6945df9a SM |
550 | filter_sink_component_ports_connected_listener( |
551 | const bt_component_filter *filter_component, | |
552 | const bt_component_sink *sink_component, | |
553 | const bt_port_output *upstream_port, | |
554 | const bt_port_input *downstream_port, void *py_callable) | |
555 | { | |
8cc56726 SM |
556 | return ports_connected_listener(upstream_port, downstream_port, |
557 | py_callable); | |
6945df9a SM |
558 | } |
559 | ||
560 | ||
561 | static PyObject* | |
562 | bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph, | |
563 | PyObject *py_callable) | |
564 | { | |
565 | PyObject *py_listener_ids = NULL; | |
566 | PyObject *py_listener_id = NULL; | |
567 | int listener_id; | |
568 | bt_graph_status status; | |
569 | ||
570 | BT_ASSERT(graph); | |
571 | BT_ASSERT(py_callable); | |
572 | ||
573 | /* Behind the scene, we will be registering 4 different listeners and | |
574 | * return all of their ids. */ | |
575 | py_listener_ids = PyTuple_New(4); | |
576 | if (!py_listener_ids) { | |
577 | goto error; | |
578 | } | |
579 | ||
580 | /* source -> filter connection */ | |
581 | status = bt_graph_add_source_filter_component_ports_connected_listener( | |
582 | graph, source_filter_component_ports_connected_listener, | |
583 | graph_listener_removed, py_callable, &listener_id); | |
584 | if (status != BT_GRAPH_STATUS_OK) { | |
585 | goto error; | |
586 | } | |
587 | ||
588 | py_listener_id = PyLong_FromLong(listener_id); | |
589 | if (!py_listener_id) { | |
590 | goto error; | |
591 | } | |
592 | ||
593 | PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id); | |
594 | py_listener_id = NULL; | |
595 | ||
596 | /* source -> sink connection */ | |
597 | status = bt_graph_add_source_sink_component_ports_connected_listener( | |
598 | graph, source_sink_component_ports_connected_listener, | |
599 | graph_listener_removed, py_callable, &listener_id); | |
600 | if (status != BT_GRAPH_STATUS_OK) { | |
601 | goto error; | |
602 | } | |
603 | ||
604 | py_listener_id = PyLong_FromLong(listener_id); | |
605 | if (!py_listener_id) { | |
606 | goto error; | |
607 | } | |
608 | ||
609 | PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id); | |
610 | py_listener_id = NULL; | |
611 | ||
612 | /* filter -> filter connection */ | |
613 | status = bt_graph_add_filter_filter_component_ports_connected_listener( | |
614 | graph, filter_filter_component_ports_connected_listener, | |
615 | graph_listener_removed, py_callable, &listener_id); | |
616 | if (status != BT_GRAPH_STATUS_OK) { | |
617 | goto error; | |
618 | } | |
619 | ||
620 | py_listener_id = PyLong_FromLong(listener_id); | |
621 | if (!py_listener_id) { | |
622 | goto error; | |
623 | } | |
624 | ||
625 | PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id); | |
626 | py_listener_id = NULL; | |
627 | ||
628 | /* filter -> sink connection */ | |
629 | status = bt_graph_add_filter_sink_component_ports_connected_listener( | |
630 | graph, filter_sink_component_ports_connected_listener, | |
631 | graph_listener_removed, py_callable, &listener_id); | |
632 | if (status != BT_GRAPH_STATUS_OK) { | |
633 | goto error; | |
634 | } | |
635 | ||
636 | py_listener_id = PyLong_FromLong(listener_id); | |
637 | if (!py_listener_id) { | |
638 | goto error; | |
639 | } | |
640 | ||
641 | PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id); | |
642 | py_listener_id = NULL; | |
643 | ||
644 | Py_INCREF(py_callable); | |
645 | Py_INCREF(py_callable); | |
646 | Py_INCREF(py_callable); | |
647 | Py_INCREF(py_callable); | |
648 | ||
649 | goto end; | |
650 | ||
651 | error: | |
652 | Py_XDECREF(py_listener_ids); | |
653 | py_listener_ids = Py_None; | |
654 | Py_INCREF(py_listener_ids); | |
655 | ||
656 | end: | |
657 | ||
658 | Py_XDECREF(py_listener_id); | |
659 | return py_listener_ids; | |
660 | } | |
661 | ||
662 | %} | |
663 | ||
664 | PyObject *bt_py3_graph_add_port_added_listener(struct bt_graph *graph, | |
665 | PyObject *py_callable); | |
666 | PyObject *bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph, | |
667 | PyObject *py_callable); |