2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 void graph_listener_removed(void *py_callable
)
28 BT_ASSERT(py_callable
);
29 Py_DECREF(py_callable
);
32 static bt_graph_listener_func_status
port_added_listener(
33 const void *component
,
34 swig_type_info
*component_swig_type
,
35 bt_component_class_type component_class_type
,
37 swig_type_info
*port_swig_type
,
38 bt_port_type port_type
,
41 PyObject
*py_component_ptr
= NULL
;
42 PyObject
*py_port_ptr
= NULL
;
43 PyObject
*py_res
= NULL
;
44 bt_graph_listener_func_status status
;
46 py_component_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(component
), component_swig_type
, 0);
47 if (!py_component_ptr
) {
48 BT_LOGF_STR("Failed to create component SWIG pointer object.");
49 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
53 py_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(port
), port_swig_type
, 0);
55 BT_LOGF_STR("Failed to create port SWIG pointer object.");
56 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
60 py_res
= PyObject_CallFunction(py_callable
, "(OiOi)",
61 py_component_ptr
, component_class_type
, py_port_ptr
, port_type
);
63 loge_exception_append_cause_clear(
64 "Graph's port added listener (Python)",
66 status
= __BT_FUNC_STATUS_ERROR
;
70 BT_ASSERT(py_res
== Py_None
);
71 status
= __BT_FUNC_STATUS_OK
;
75 Py_XDECREF(py_port_ptr
);
76 Py_XDECREF(py_component_ptr
);
81 bt_graph_listener_func_status
82 source_component_output_port_added_listener(const bt_component_source
*component_source
,
83 const bt_port_output
*port_output
, void *py_callable
)
85 return port_added_listener(
86 component_source
, SWIGTYPE_p_bt_component_source
, BT_COMPONENT_CLASS_TYPE_SOURCE
,
87 port_output
, SWIGTYPE_p_bt_port_output
, BT_PORT_TYPE_OUTPUT
, py_callable
);
91 bt_graph_listener_func_status
92 filter_component_input_port_added_listener(const bt_component_filter
*component_filter
,
93 const bt_port_input
*port_input
, void *py_callable
)
95 return port_added_listener(
96 component_filter
, SWIGTYPE_p_bt_component_filter
, BT_COMPONENT_CLASS_TYPE_FILTER
,
97 port_input
, SWIGTYPE_p_bt_port_input
, BT_PORT_TYPE_INPUT
, py_callable
);
101 bt_graph_listener_func_status
102 filter_component_output_port_added_listener(const bt_component_filter
*component_filter
,
103 const bt_port_output
*port_output
, void *py_callable
)
105 return port_added_listener(
106 component_filter
, SWIGTYPE_p_bt_component_filter
, BT_COMPONENT_CLASS_TYPE_FILTER
,
107 port_output
, SWIGTYPE_p_bt_port_output
, BT_PORT_TYPE_OUTPUT
, py_callable
);
111 bt_graph_listener_func_status
112 sink_component_input_port_added_listener(const bt_component_sink
*component_sink
,
113 const bt_port_input
*port_input
, void *py_callable
)
115 return port_added_listener(
116 component_sink
, SWIGTYPE_p_bt_component_sink
, BT_COMPONENT_CLASS_TYPE_SINK
,
117 port_input
, SWIGTYPE_p_bt_port_input
, BT_PORT_TYPE_INPUT
, py_callable
);
121 PyObject
*bt_bt2_graph_add_port_added_listener(struct bt_graph
*graph
,
122 PyObject
*py_callable
)
124 PyObject
*py_listener_ids
= NULL
;
125 PyObject
*py_listener_id
= NULL
;
126 bt_listener_id listener_id
;
127 bt_graph_add_listener_status status
;
128 const char * const module_name
=
129 "graph_add_port_added_listener() (Python)";
132 BT_ASSERT(py_callable
);
135 * Behind the scene, we will be registering 4 different listeners and
136 * return all of their ids.
138 py_listener_ids
= PyTuple_New(4);
139 if (!py_listener_ids
) {
140 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
141 "Failed to allocate one PyTuple.");
145 /* source output port */
146 status
= bt_graph_add_source_component_output_port_added_listener(
147 graph
, source_component_output_port_added_listener
,
148 graph_listener_removed
, py_callable
, &listener_id
);
149 if (status
!= __BT_FUNC_STATUS_OK
) {
151 * bt_graph_add_source_component_output_port_added_listener has
152 * already logged/appended an error cause.
157 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
158 if (!py_listener_id
) {
159 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
160 "Failed to allocate one PyLong.");
164 PyTuple_SET_ITEM(py_listener_ids
, 0, py_listener_id
);
165 py_listener_id
= NULL
;
167 /* filter input port */
168 status
= bt_graph_add_filter_component_input_port_added_listener(
169 graph
, filter_component_input_port_added_listener
,
170 graph_listener_removed
, py_callable
, &listener_id
);
171 if (status
!= __BT_FUNC_STATUS_OK
) {
173 * bt_graph_add_filter_component_input_port_added_listener has
174 * already logged/appended an error cause.
179 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
180 if (!py_listener_id
) {
181 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
182 "Failed to allocate one PyLong.");
186 PyTuple_SET_ITEM(py_listener_ids
, 1, py_listener_id
);
187 py_listener_id
= NULL
;
189 /* filter output port */
190 status
= bt_graph_add_filter_component_output_port_added_listener(
191 graph
, filter_component_output_port_added_listener
,
192 graph_listener_removed
, py_callable
, &listener_id
);
193 if (status
!= __BT_FUNC_STATUS_OK
) {
195 * bt_graph_add_filter_component_output_port_added_listener has
196 * already logged/appended an error cause.
201 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
202 if (!py_listener_id
) {
203 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
204 "Failed to allocate one PyLong.");
208 PyTuple_SET_ITEM(py_listener_ids
, 2, py_listener_id
);
209 py_listener_id
= NULL
;
211 /* sink input port */
212 status
= bt_graph_add_sink_component_input_port_added_listener(
213 graph
, sink_component_input_port_added_listener
,
214 graph_listener_removed
, py_callable
, &listener_id
);
215 if (status
!= __BT_FUNC_STATUS_OK
) {
217 * bt_graph_add_sink_component_input_port_added_listener has
218 * already logged/appended an error cause.
223 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
224 if (!py_listener_id
) {
225 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
226 "Failed to allocate one PyLong.");
230 PyTuple_SET_ITEM(py_listener_ids
, 3, py_listener_id
);
231 py_listener_id
= NULL
;
233 Py_INCREF(py_callable
);
234 Py_INCREF(py_callable
);
235 Py_INCREF(py_callable
);
236 Py_INCREF(py_callable
);
241 Py_XDECREF(py_listener_ids
);
242 py_listener_ids
= Py_None
;
243 Py_INCREF(py_listener_ids
);
246 Py_XDECREF(py_listener_id
);
247 return py_listener_ids
;
251 bt_graph_add_component_status
252 bt_bt2_graph_add_source_component(
254 const bt_component_class_source
*component_class
,
255 const char *name
, const bt_value
*params
,
256 PyObject
*obj
, bt_logging_level log_level
,
257 const bt_component_source
**component
)
259 return bt_graph_add_source_component_with_initialize_method_data(graph
,
260 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
261 log_level
, component
);
265 bt_graph_add_component_status
266 bt_bt2_graph_add_filter_component(
268 const bt_component_class_filter
*component_class
,
269 const char *name
, const bt_value
*params
,
270 PyObject
*obj
, bt_logging_level log_level
,
271 const bt_component_filter
**component
)
273 return bt_graph_add_filter_component_with_initialize_method_data(graph
,
274 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
275 log_level
, component
);
279 bt_graph_add_component_status
280 bt_bt2_graph_add_sink_component(
282 const bt_component_class_sink
*component_class
,
283 const char *name
, const bt_value
*params
,
284 PyObject
*obj
, bt_logging_level log_level
,
285 const bt_component_sink
**component
)
287 return bt_graph_add_sink_component_with_initialize_method_data(graph
,
288 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
289 log_level
, component
);