| 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 | /* Types */ |
| 26 | struct bt_graph; |
| 27 | |
| 28 | /* Status */ |
| 29 | enum bt_graph_status { |
| 30 | BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION = 111, |
| 31 | BT_GRAPH_STATUS_CANCELED = 125, |
| 32 | BT_GRAPH_STATUS_AGAIN = 11, |
| 33 | BT_GRAPH_STATUS_END = 1, |
| 34 | BT_GRAPH_STATUS_OK = 0, |
| 35 | BT_GRAPH_STATUS_INVALID = -22, |
| 36 | BT_GRAPH_STATUS_NO_SINK = -6, |
| 37 | BT_GRAPH_STATUS_CANNOT_CONSUME = -2, |
| 38 | BT_GRAPH_STATUS_ERROR = -1, |
| 39 | BT_GRAPH_STATUS_NOMEM = -12, |
| 40 | }; |
| 41 | |
| 42 | /* Functions */ |
| 43 | struct bt_graph *bt_graph_create(void); |
| 44 | enum bt_graph_status bt_graph_add_component( |
| 45 | struct bt_graph *graph, |
| 46 | struct bt_component_class *component_class, |
| 47 | const char *name, struct bt_value *params, |
| 48 | struct bt_component **BTOUTCOMP); |
| 49 | enum bt_graph_status bt_graph_add_component_with_init_method_data( |
| 50 | struct bt_graph *graph, |
| 51 | struct bt_component_class *component_class, |
| 52 | const char *name, struct bt_value *params, |
| 53 | void *init_method_data, |
| 54 | struct bt_component **BTOUTCOMP); |
| 55 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
| 56 | struct bt_port *upstream, struct bt_port *downstream, |
| 57 | struct bt_connection **BTOUTCONN); |
| 58 | enum bt_graph_status bt_graph_run(struct bt_graph *graph); |
| 59 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph); |
| 60 | enum bt_graph_status bt_graph_cancel(struct bt_graph *graph); |
| 61 | int bt_graph_is_canceled(struct bt_graph *graph); |
| 62 | |
| 63 | /* Helper functions for Python */ |
| 64 | %{ |
| 65 | static void port_added_listener(struct bt_port *port, void *py_callable) |
| 66 | { |
| 67 | PyObject *py_port_ptr = NULL; |
| 68 | PyObject *py_res = NULL; |
| 69 | |
| 70 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), |
| 71 | SWIGTYPE_p_bt_port, 0); |
| 72 | if (!py_port_ptr) { |
| 73 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 74 | abort(); |
| 75 | } |
| 76 | |
| 77 | py_res = PyObject_CallFunction(py_callable, "(O)", py_port_ptr); |
| 78 | BT_ASSERT(py_res == Py_None); |
| 79 | Py_DECREF(py_port_ptr); |
| 80 | Py_DECREF(py_res); |
| 81 | } |
| 82 | |
| 83 | static void port_removed_listener(struct bt_component *component, |
| 84 | struct bt_port *port, void *py_callable) |
| 85 | { |
| 86 | PyObject *py_port_ptr = NULL; |
| 87 | PyObject *py_res = NULL; |
| 88 | |
| 89 | py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), |
| 90 | SWIGTYPE_p_bt_port, 0); |
| 91 | if (!py_port_ptr) { |
| 92 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 93 | abort(); |
| 94 | } |
| 95 | |
| 96 | py_res = PyObject_CallFunction(py_callable, "(O)", py_port_ptr); |
| 97 | BT_ASSERT(py_res == Py_None); |
| 98 | Py_DECREF(py_port_ptr); |
| 99 | Py_DECREF(py_res); |
| 100 | } |
| 101 | |
| 102 | static void ports_connected_listener(struct bt_port *upstream_port, |
| 103 | struct bt_port *downstream_port, void *py_callable) |
| 104 | { |
| 105 | PyObject *py_upstream_port_ptr = NULL; |
| 106 | PyObject *py_downstream_port_ptr = NULL; |
| 107 | PyObject *py_res = NULL; |
| 108 | |
| 109 | py_upstream_port_ptr = SWIG_NewPointerObj( |
| 110 | SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port, 0); |
| 111 | if (!py_upstream_port_ptr) { |
| 112 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 113 | abort(); |
| 114 | } |
| 115 | |
| 116 | py_downstream_port_ptr = SWIG_NewPointerObj( |
| 117 | SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port, 0); |
| 118 | if (!py_downstream_port_ptr) { |
| 119 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 120 | abort(); |
| 121 | } |
| 122 | |
| 123 | py_res = PyObject_CallFunction(py_callable, "(OO)", |
| 124 | py_upstream_port_ptr, py_downstream_port_ptr); |
| 125 | BT_ASSERT(py_res == Py_None); |
| 126 | Py_DECREF(py_upstream_port_ptr); |
| 127 | Py_DECREF(py_downstream_port_ptr); |
| 128 | Py_DECREF(py_res); |
| 129 | } |
| 130 | |
| 131 | static void ports_disconnected_listener( |
| 132 | struct bt_component *upstream_component, |
| 133 | struct bt_component *downstream_component, |
| 134 | struct bt_port *upstream_port, struct bt_port *downstream_port, |
| 135 | void *py_callable) |
| 136 | { |
| 137 | PyObject *py_upstream_comp_ptr = NULL; |
| 138 | PyObject *py_downstream_comp_ptr = NULL; |
| 139 | PyObject *py_upstream_port_ptr = NULL; |
| 140 | PyObject *py_downstream_port_ptr = NULL; |
| 141 | PyObject *py_res = NULL; |
| 142 | |
| 143 | py_upstream_comp_ptr = SWIG_NewPointerObj( |
| 144 | SWIG_as_voidptr(upstream_component), |
| 145 | SWIGTYPE_p_bt_component, 0); |
| 146 | if (!py_upstream_comp_ptr) { |
| 147 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 148 | abort(); |
| 149 | } |
| 150 | |
| 151 | py_downstream_comp_ptr = SWIG_NewPointerObj( |
| 152 | SWIG_as_voidptr(downstream_component), |
| 153 | SWIGTYPE_p_bt_component, 0); |
| 154 | if (!py_downstream_comp_ptr) { |
| 155 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 156 | abort(); |
| 157 | } |
| 158 | |
| 159 | py_upstream_port_ptr = SWIG_NewPointerObj( |
| 160 | SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port, 0); |
| 161 | if (!py_upstream_port_ptr) { |
| 162 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 163 | abort(); |
| 164 | } |
| 165 | |
| 166 | py_downstream_port_ptr = SWIG_NewPointerObj( |
| 167 | SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port, 0); |
| 168 | if (!py_downstream_port_ptr) { |
| 169 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 170 | abort(); |
| 171 | } |
| 172 | |
| 173 | py_res = PyObject_CallFunction(py_callable, "(OOOO)", |
| 174 | py_upstream_comp_ptr, py_downstream_comp_ptr, |
| 175 | py_upstream_port_ptr, py_downstream_port_ptr); |
| 176 | BT_ASSERT(py_res == Py_None); |
| 177 | Py_DECREF(py_upstream_comp_ptr); |
| 178 | Py_DECREF(py_downstream_comp_ptr); |
| 179 | Py_DECREF(py_upstream_port_ptr); |
| 180 | Py_DECREF(py_downstream_port_ptr); |
| 181 | Py_DECREF(py_res); |
| 182 | } |
| 183 | |
| 184 | static void graph_listener_removed(void *py_callable) |
| 185 | { |
| 186 | BT_ASSERT(py_callable); |
| 187 | Py_DECREF(py_callable); |
| 188 | } |
| 189 | |
| 190 | static int bt_py3_graph_add_port_added_listener(struct bt_graph *graph, |
| 191 | PyObject *py_callable) |
| 192 | { |
| 193 | int ret = 0; |
| 194 | |
| 195 | BT_ASSERT(graph); |
| 196 | BT_ASSERT(py_callable); |
| 197 | ret = bt_graph_add_port_added_listener(graph, port_added_listener, |
| 198 | graph_listener_removed, py_callable); |
| 199 | if (ret >= 0) { |
| 200 | Py_INCREF(py_callable); |
| 201 | } |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static int bt_py3_graph_add_port_removed_listener(struct bt_graph *graph, |
| 207 | PyObject *py_callable) |
| 208 | { |
| 209 | int ret = 0; |
| 210 | |
| 211 | BT_ASSERT(graph); |
| 212 | BT_ASSERT(py_callable); |
| 213 | ret = bt_graph_add_port_removed_listener(graph, port_removed_listener, |
| 214 | graph_listener_removed, py_callable); |
| 215 | if (ret >= 0) { |
| 216 | Py_INCREF(py_callable); |
| 217 | } |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph, |
| 223 | PyObject *py_callable) |
| 224 | { |
| 225 | int ret = 0; |
| 226 | |
| 227 | BT_ASSERT(graph); |
| 228 | BT_ASSERT(py_callable); |
| 229 | ret = bt_graph_add_ports_connected_listener(graph, |
| 230 | ports_connected_listener, graph_listener_removed, py_callable); |
| 231 | if (ret >= 0) { |
| 232 | Py_INCREF(py_callable); |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static int bt_py3_graph_add_ports_disconnected_listener(struct bt_graph *graph, |
| 239 | PyObject *py_callable) |
| 240 | { |
| 241 | int ret = 0; |
| 242 | |
| 243 | BT_ASSERT(graph); |
| 244 | BT_ASSERT(py_callable); |
| 245 | ret = bt_graph_add_ports_disconnected_listener(graph, |
| 246 | ports_disconnected_listener, graph_listener_removed, |
| 247 | py_callable); |
| 248 | if (ret >= 0) { |
| 249 | Py_INCREF(py_callable); |
| 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | %} |
| 255 | |
| 256 | int bt_py3_graph_add_port_added_listener(struct bt_graph *graph, |
| 257 | PyObject *py_callable); |
| 258 | int bt_py3_graph_add_port_removed_listener(struct bt_graph *graph, |
| 259 | PyObject *py_callable); |
| 260 | int bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph, |
| 261 | PyObject *py_callable); |
| 262 | int bt_py3_graph_add_ports_disconnected_listener(struct bt_graph *graph, |
| 263 | PyObject *py_callable); |