1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari
8 **************************************************************************
9 * This file contains implementation of the D-Bus patchbay interface helpers
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "../dbus/error.h"
32 #define impl_ptr ((struct patchbay_implementator *)call_ptr->context)
34 static void get_all_ports(method_call_t
* call_ptr
)
36 DBusMessageIter iter
, sub_iter
;
38 call_ptr
->reply
= dbus_message_new_method_return(call_ptr
->message
);
39 if (call_ptr
->reply
== NULL
)
44 dbus_message_iter_init_append(call_ptr
->reply
, &iter
);
46 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub_iter
))
51 if (!dbus_message_iter_close_container(&iter
, &sub_iter
))
59 dbus_message_unref(call_ptr
->reply
);
60 call_ptr
->reply
= NULL
;
63 lash_error("Ran out of memory trying to construct method return");
66 static void get_graph(method_call_t
* call_ptr
)
68 dbus_uint64_t known_version
;
69 dbus_uint64_t current_version
;
71 DBusMessageIter clients_array_iter
;
72 DBusMessageIter connections_array_iter
;
74 DBusMessageIter ports_array_iter
;
75 DBusMessageIter client_struct_iter
;
76 DBusMessageIter port_struct_iter
;
77 DBusMessageIter connection_struct_iter
;
80 //lash_info("get_graph() called");
82 if (!dbus_message_get_args(call_ptr
->message
, &g_dbus_error
, DBUS_TYPE_UINT64
, &known_version
, DBUS_TYPE_INVALID
))
84 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\": %s", call_ptr
->method_name
, g_dbus_error
.message
);
85 dbus_error_free(&g_dbus_error
);
89 //lash_info("Getting graph, known version is %" PRIu64, known_version);
91 call_ptr
->reply
= dbus_message_new_method_return(call_ptr
->message
);
92 if (call_ptr
->reply
== NULL
)
94 lash_error("Ran out of memory trying to construct method return");
98 dbus_message_iter_init_append(call_ptr
->reply
, &iter
);
100 current_version
= impl_ptr
->get_graph_version(impl_ptr
->this);
101 if (known_version
> current_version
)
105 LASH_DBUS_ERROR_INVALID_ARGS
,
106 "known graph version %" PRIu64
" is newer than actual version %" PRIu64
,
112 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT64
, ¤t_version
))
117 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(tsa(tsuu))", &clients_array_iter
))
122 if (known_version
< current_version
)
125 list_for_each(client_node_ptr
, &patchbay_ptr
->graph
.clients
)
127 client_ptr
= list_entry(client_node_ptr
, struct jack_graph_client
, siblings
);
129 if (!dbus_message_iter_open_container (&clients_array_iter
, DBUS_TYPE_STRUCT
, NULL
, &client_struct_iter
))
131 goto nomem_close_clients_array
;
134 if (!dbus_message_iter_append_basic(&client_struct_iter
, DBUS_TYPE_UINT64
, &client_ptr
->id
))
136 goto nomem_close_client_struct
;
139 if (!dbus_message_iter_append_basic(&client_struct_iter
, DBUS_TYPE_STRING
, &client_ptr
->name
))
141 goto nomem_close_client_struct
;
144 if (!dbus_message_iter_open_container(&client_struct_iter
, DBUS_TYPE_ARRAY
, "(tsuu)", &ports_array_iter
))
146 goto nomem_close_client_struct
;
149 list_for_each(port_node_ptr
, &client_ptr
->ports
)
151 port_ptr
= list_entry(port_node_ptr
, struct jack_graph_port
, siblings_client
);
153 if (!dbus_message_iter_open_container(&ports_array_iter
, DBUS_TYPE_STRUCT
, NULL
, &port_struct_iter
))
155 goto nomem_close_ports_array
;
158 if (!dbus_message_iter_append_basic(&port_struct_iter
, DBUS_TYPE_UINT64
, &port_ptr
->id
))
160 goto nomem_close_port_struct
;
163 if (!dbus_message_iter_append_basic(&port_struct_iter
, DBUS_TYPE_STRING
, &port_ptr
->name
))
165 goto nomem_close_port_struct
;
168 if (!dbus_message_iter_append_basic(&port_struct_iter
, DBUS_TYPE_UINT32
, &port_ptr
->flags
))
170 goto nomem_close_port_struct
;
173 if (!dbus_message_iter_append_basic(&port_struct_iter
, DBUS_TYPE_UINT32
, &port_ptr
->type
))
175 goto nomem_close_port_struct
;
178 if (!dbus_message_iter_close_container(&ports_array_iter
, &port_struct_iter
))
180 goto nomem_close_ports_array
;
184 if (!dbus_message_iter_close_container(&client_struct_iter
, &ports_array_iter
))
186 goto nomem_close_client_struct
;
189 if (!dbus_message_iter_close_container(&clients_array_iter
, &client_struct_iter
))
191 goto nomem_close_clients_array
;
197 if (!dbus_message_iter_close_container(&iter
, &clients_array_iter
))
202 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(tstststst)", &connections_array_iter
))
207 if (known_version
< current_version
)
210 list_for_each(connection_node_ptr
, &patchbay_ptr
->graph
.connections
)
212 connection_ptr
= list_entry(connection_node_ptr
, struct jack_graph_connection
, siblings
);
214 if (!dbus_message_iter_open_container(&connections_array_iter
, DBUS_TYPE_STRUCT
, NULL
, &connection_struct_iter
))
216 goto nomem_close_connections_array
;
219 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_UINT64
, &connection_ptr
->port1
->client
->id
))
221 goto nomem_close_connection_struct
;
224 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_STRING
, &connection_ptr
->port1
->client
->name
))
226 goto nomem_close_connection_struct
;
229 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_UINT64
, &connection_ptr
->port1
->id
))
231 goto nomem_close_connection_struct
;
234 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_STRING
, &connection_ptr
->port1
->name
))
236 goto nomem_close_connection_struct
;
239 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_UINT64
, &connection_ptr
->port2
->client
->id
))
241 goto nomem_close_connection_struct
;
244 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_STRING
, &connection_ptr
->port2
->client
->name
))
246 goto nomem_close_connection_struct
;
249 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_UINT64
, &connection_ptr
->port2
->id
))
251 goto nomem_close_connection_struct
;
254 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_STRING
, &connection_ptr
->port2
->name
))
256 goto nomem_close_connection_struct
;
259 if (!dbus_message_iter_append_basic(&connection_struct_iter
, DBUS_TYPE_UINT64
, &connection_ptr
->id
))
261 goto nomem_close_connection_struct
;
264 if (!dbus_message_iter_close_container(&connections_array_iter
, &connection_struct_iter
))
266 goto nomem_close_connections_array
;
272 if (!dbus_message_iter_close_container(&iter
, &connections_array_iter
))
280 nomem_close_connection_struct
:
281 dbus_message_iter_close_container(&connections_array_iter
, &connection_struct_iter
);
283 nomem_close_connections_array
:
284 dbus_message_iter_close_container(&iter
, &connections_array_iter
);
287 nomem_close_port_struct
:
288 dbus_message_iter_close_container(&ports_array_iter
, &port_struct_iter
);
290 nomem_close_ports_array
:
291 dbus_message_iter_close_container(&client_struct_iter
, &ports_array_iter
);
293 nomem_close_client_struct
:
294 dbus_message_iter_close_container(&clients_array_iter
, &client_struct_iter
);
296 nomem_close_clients_array
:
297 dbus_message_iter_close_container(&iter
, &clients_array_iter
);
301 dbus_message_unref(call_ptr
->reply
);
302 call_ptr
->reply
= NULL
;
303 lash_error("Ran out of memory trying to construct method return");
309 static void connect_ports_by_name(method_call_t
* call_ptr
)
311 method_return_new_void(call_ptr
);
314 static void connect_ports_by_id(method_call_t
* call_ptr
)
316 method_return_new_void(call_ptr
);
319 static void disconnect_ports_by_name(method_call_t
* call_ptr
)
321 method_return_new_void(call_ptr
);
324 static void disconnect_ports_by_id(method_call_t
* call_ptr
)
326 method_return_new_void(call_ptr
);
329 static void disconnect_ports_by_connection_id(method_call_t
* call_ptr
)
331 method_return_new_void(call_ptr
);
334 static void get_client_pid(method_call_t
* call_ptr
)
337 method_return_new_single(call_ptr
, DBUS_TYPE_INT64
, &pid
);
340 METHOD_ARGS_BEGIN(GetAllPorts
, "Get all ports")
341 METHOD_ARG_DESCRIBE_IN("ports_list", "as", "List of all ports")
344 METHOD_ARGS_BEGIN(GetGraph
, "Get whole graph")
345 METHOD_ARG_DESCRIBE_IN("known_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "Known graph version")
346 METHOD_ARG_DESCRIBE_OUT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "Current graph version")
347 METHOD_ARG_DESCRIBE_OUT("clients_and_ports", "a(tsa(tsuu))", "Clients and their ports")
348 METHOD_ARG_DESCRIBE_OUT("connections", "a(tstststst)", "Connections array")
351 METHOD_ARGS_BEGIN(ConnectPortsByName
, "Connect ports")
352 METHOD_ARG_DESCRIBE_IN("client1_name", DBUS_TYPE_STRING_AS_STRING
, "name first port client")
353 METHOD_ARG_DESCRIBE_IN("port1_name", DBUS_TYPE_STRING_AS_STRING
, "name of first port")
354 METHOD_ARG_DESCRIBE_IN("client2_name", DBUS_TYPE_STRING_AS_STRING
, "name second port client")
355 METHOD_ARG_DESCRIBE_IN("port2_name", DBUS_TYPE_STRING_AS_STRING
, "name of second port")
358 METHOD_ARGS_BEGIN(ConnectPortsByID
, "Connect ports")
359 METHOD_ARG_DESCRIBE_IN("port1_id", DBUS_TYPE_UINT64_AS_STRING
, "id of first port")
360 METHOD_ARG_DESCRIBE_IN("port2_id", DBUS_TYPE_UINT64_AS_STRING
, "if of second port")
363 METHOD_ARGS_BEGIN(DisconnectPortsByName
, "Disconnect ports")
364 METHOD_ARG_DESCRIBE_IN("client1_name", DBUS_TYPE_STRING_AS_STRING
, "name first port client")
365 METHOD_ARG_DESCRIBE_IN("port1_name", DBUS_TYPE_STRING_AS_STRING
, "name of first port")
366 METHOD_ARG_DESCRIBE_IN("client2_name", DBUS_TYPE_STRING_AS_STRING
, "name second port client")
367 METHOD_ARG_DESCRIBE_IN("port2_name", DBUS_TYPE_STRING_AS_STRING
, "name of second port")
370 METHOD_ARGS_BEGIN(DisconnectPortsByID
, "Disconnect ports")
371 METHOD_ARG_DESCRIBE_IN("port1_id", DBUS_TYPE_UINT64_AS_STRING
, "id of first port")
372 METHOD_ARG_DESCRIBE_IN("port2_id", DBUS_TYPE_UINT64_AS_STRING
, "if of second port")
375 METHOD_ARGS_BEGIN(DisconnectPortsByConnectionID
, "Disconnect ports")
376 METHOD_ARG_DESCRIBE_IN("connection_id", DBUS_TYPE_UINT64_AS_STRING
, "id of connection to disconnect")
379 METHOD_ARGS_BEGIN(GetClientPID
, "get process id of client")
380 METHOD_ARG_DESCRIBE_IN("client_id", DBUS_TYPE_UINT64_AS_STRING
, "id of client")
381 METHOD_ARG_DESCRIBE_OUT("process_id", DBUS_TYPE_INT64_AS_STRING
, "pid of client")
385 METHOD_DESCRIBE(GetAllPorts
, get_all_ports
)
386 METHOD_DESCRIBE(GetGraph
, get_graph
)
387 METHOD_DESCRIBE(ConnectPortsByName
, connect_ports_by_name
)
388 METHOD_DESCRIBE(ConnectPortsByID
, connect_ports_by_id
)
389 METHOD_DESCRIBE(DisconnectPortsByName
, disconnect_ports_by_name
)
390 METHOD_DESCRIBE(DisconnectPortsByID
, disconnect_ports_by_id
)
391 METHOD_DESCRIBE(DisconnectPortsByConnectionID
, disconnect_ports_by_connection_id
)
392 METHOD_DESCRIBE(GetClientPID
, get_client_pid
)
395 SIGNAL_ARGS_BEGIN(GraphChanged
, "")
396 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
399 SIGNAL_ARGS_BEGIN(ClientAppeared
, "")
400 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
401 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING
, "")
402 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING
, "")
405 SIGNAL_ARGS_BEGIN(ClientDisappeared
, "")
406 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
407 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING
, "")
408 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING
, "")
411 SIGNAL_ARGS_BEGIN(PortAppeared
, "")
412 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
413 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING
, "")
414 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING
, "")
415 SIGNAL_ARG_DESCRIBE("port_id", DBUS_TYPE_UINT64_AS_STRING
, "")
416 SIGNAL_ARG_DESCRIBE("port_name", DBUS_TYPE_STRING_AS_STRING
, "")
417 SIGNAL_ARG_DESCRIBE("port_flags", DBUS_TYPE_UINT32_AS_STRING
, "")
418 SIGNAL_ARG_DESCRIBE("port_type", DBUS_TYPE_UINT32_AS_STRING
, "")
421 SIGNAL_ARGS_BEGIN(PortDisappeared
, "")
422 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
423 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING
, "")
424 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING
, "")
425 SIGNAL_ARG_DESCRIBE("port_id", DBUS_TYPE_UINT64_AS_STRING
, "")
426 SIGNAL_ARG_DESCRIBE("port_name", DBUS_TYPE_STRING_AS_STRING
, "")
429 SIGNAL_ARGS_BEGIN(PortsConnected
, "")
430 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
431 SIGNAL_ARG_DESCRIBE("client1_id", DBUS_TYPE_UINT64_AS_STRING
, "")
432 SIGNAL_ARG_DESCRIBE("client1_name", DBUS_TYPE_STRING_AS_STRING
, "")
433 SIGNAL_ARG_DESCRIBE("port1_id", DBUS_TYPE_UINT64_AS_STRING
, "")
434 SIGNAL_ARG_DESCRIBE("port1_name", DBUS_TYPE_STRING_AS_STRING
, "")
435 SIGNAL_ARG_DESCRIBE("client2_id", DBUS_TYPE_UINT64_AS_STRING
, "")
436 SIGNAL_ARG_DESCRIBE("client2_name", DBUS_TYPE_STRING_AS_STRING
, "")
437 SIGNAL_ARG_DESCRIBE("port2_id", DBUS_TYPE_UINT64_AS_STRING
, "")
438 SIGNAL_ARG_DESCRIBE("port2_name", DBUS_TYPE_STRING_AS_STRING
, "")
439 SIGNAL_ARG_DESCRIBE("connection_id", DBUS_TYPE_UINT64_AS_STRING
, "")
442 SIGNAL_ARGS_BEGIN(PortsDisconnected
, "")
443 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING
, "")
444 SIGNAL_ARG_DESCRIBE("client1_id", DBUS_TYPE_UINT64_AS_STRING
, "")
445 SIGNAL_ARG_DESCRIBE("client1_name", DBUS_TYPE_STRING_AS_STRING
, "")
446 SIGNAL_ARG_DESCRIBE("port1_id", DBUS_TYPE_UINT64_AS_STRING
, "")
447 SIGNAL_ARG_DESCRIBE("port1_name", DBUS_TYPE_STRING_AS_STRING
, "")
448 SIGNAL_ARG_DESCRIBE("client2_id", DBUS_TYPE_UINT64_AS_STRING
, "")
449 SIGNAL_ARG_DESCRIBE("client2_name", DBUS_TYPE_STRING_AS_STRING
, "")
450 SIGNAL_ARG_DESCRIBE("port2_id", DBUS_TYPE_UINT64_AS_STRING
, "")
451 SIGNAL_ARG_DESCRIBE("port2_name", DBUS_TYPE_STRING_AS_STRING
, "")
452 SIGNAL_ARG_DESCRIBE("connection_id", DBUS_TYPE_UINT64_AS_STRING
, "")
456 SIGNAL_DESCRIBE(GraphChanged
)
457 SIGNAL_DESCRIBE(ClientAppeared
)
458 SIGNAL_DESCRIBE(ClientDisappeared
)
459 SIGNAL_DESCRIBE(PortAppeared
)
460 SIGNAL_DESCRIBE(PortDisappeared
)
461 SIGNAL_DESCRIBE(PortsConnected
)
462 SIGNAL_DESCRIBE(PortsDisconnected
)
465 INTERFACE_BEGIN(g_interface_patchbay
, "org.jackaudio.JackPatchbay")
466 INTERFACE_DEFAULT_HANDLER
467 INTERFACE_EXPOSE_METHODS
468 INTERFACE_EXPOSE_SIGNALS