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