1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains helper functionality for accessing JACK through D-Bus
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <dbus/dbus.h>
32 #include "jack_proxy.h"
33 #include "dbus/helpers.h"
34 #include "common/debug.h"
35 #include "dbus_constants.h"
37 jack_proxy_callback_server_started g_on_server_started
;
38 jack_proxy_callback_server_stopped g_on_server_stopped
;
39 jack_proxy_callback_server_appeared g_on_server_appeared
;
40 jack_proxy_callback_server_disappeared g_on_server_disappeared
;
44 on_jack_control_signal(
45 DBusMessage
* message_ptr
,
46 const char * signal_name
)
48 if (strcmp(signal_name
, "ServerStarted") == 0)
50 lash_debug("JACK server start detected.");
51 if (g_on_server_started
!= NULL
)
53 g_on_server_started();
59 if (strcmp(signal_name
, "ServerStopped") == 0)
61 lash_debug("JACK server stop detected.");
62 if (g_on_server_stopped
!= NULL
)
64 g_on_server_stopped();
74 DBusMessage
* message_ptr
,
75 const char * signal_name
)
77 const char * object_name
;
78 const char * old_owner
;
79 const char * new_owner
;
81 //lash_info("bus signal '%s' received", signal_name);
83 dbus_error_init(&g_dbus_error
);
85 if (strcmp(signal_name
, "NameOwnerChanged") == 0)
87 //lash_info("NameOwnerChanged signal received");
89 if (!dbus_message_get_args(
92 DBUS_TYPE_STRING
, &object_name
,
93 DBUS_TYPE_STRING
, &old_owner
,
94 DBUS_TYPE_STRING
, &new_owner
,
96 lash_error("Cannot get message arguments: %s", g_dbus_error
.message
);
97 dbus_error_free(&g_dbus_error
);
98 return DBUS_HANDLER_RESULT_HANDLED
;
101 if (strcmp(object_name
, JACKDBUS_SERVICE_NAME
) != 0)
103 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
106 if (old_owner
[0] == '\0')
108 lash_debug("JACK serivce appeared");
109 if (g_on_server_appeared
!= NULL
)
111 g_on_server_appeared();
114 else if (new_owner
[0] == '\0')
116 lash_debug("JACK serivce disappeared");
117 if (g_on_server_disappeared
!= NULL
)
119 g_on_server_disappeared();
123 return DBUS_HANDLER_RESULT_HANDLED
;
126 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
132 DBusConnection
* connection_ptr
,
133 DBusMessage
* message_ptr
,
136 const char * object_path
;
137 const char * interface
;
138 const char * signal_name
;
140 /* Non-signal messages are ignored */
141 if (dbus_message_get_type(message_ptr
) != DBUS_MESSAGE_TYPE_SIGNAL
)
143 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
146 interface
= dbus_message_get_interface(message_ptr
);
147 if (interface
== NULL
)
149 /* Signals with no interface are ignored */
150 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
153 object_path
= dbus_message_get_path(message_ptr
);
155 signal_name
= dbus_message_get_member(message_ptr
);
156 if (signal_name
== NULL
)
158 lash_error("Received signal with NULL member");
159 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
162 lash_debug("'%s' sent signal '%s'::'%s'", object_path
, interface
, signal_name
);
164 /* Handle JACK patchbay and control interface signals */
165 if (object_path
!= NULL
&& strcmp(object_path
, JACKDBUS_OBJECT_PATH
) == 0)
167 if (strcmp(interface
, JACKDBUS_IFACE_CONTROL
) == 0)
169 on_jack_control_signal(message_ptr
, signal_name
);
170 return DBUS_HANDLER_RESULT_HANDLED
;
173 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
176 /* Handle session bus signals to track JACK service alive state */
177 if (strcmp(interface
, DBUS_INTERFACE_DBUS
) == 0)
179 return on_bus_signal(message_ptr
, signal_name
);
182 /* Let everything else pass through */
183 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
188 jack_proxy_callback_server_started server_started
,
189 jack_proxy_callback_server_stopped server_stopped
,
190 jack_proxy_callback_server_appeared server_appeared
,
191 jack_proxy_callback_server_disappeared server_disappeared
)
195 const char ** signal
;
197 const char * control_signals
[] = {
204 "type='signal',interface='"DBUS_INTERFACE_DBUS
"',member=NameOwnerChanged,arg0='"JACKDBUS_SERVICE_NAME
"'",
206 if (dbus_error_is_set(&err
))
208 lash_error("Failed to add D-Bus match rule: %s", err
.message
);
209 dbus_error_free(&err
);
213 for (signal
= control_signals
; *signal
!= NULL
; signal
++)
218 "type='signal',sender='"JACKDBUS_SERVICE_NAME
"',path='"JACKDBUS_OBJECT_PATH
"',interface='"JACKDBUS_IFACE_CONTROL
"',member='%s'",
221 dbus_bus_add_match(g_dbus_connection
, rule
, &err
);
222 if (dbus_error_is_set(&err
))
224 lash_error("Failed to add D-Bus match rule: %s", err
.message
);
225 dbus_error_free(&err
);
230 if (!dbus_connection_add_filter(g_dbus_connection
, dbus_signal_handler
, NULL
, NULL
))
232 lash_error("Failed to add D-Bus filter");
236 g_on_server_started
= server_started
;
237 g_on_server_stopped
= server_stopped
;
238 g_on_server_appeared
= server_appeared
;
239 g_on_server_disappeared
= server_disappeared
;
248 dbus_connection_remove_filter(g_dbus_connection
, dbus_signal_handler
, NULL
);
252 jack_proxy_is_started(
257 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &started
))
262 *started_ptr
= started
;
267 jack_proxy_get_client_pid(
275 jack_proxy_connect_ports(
285 DBusMessageIter
* iter_ptr
,
286 const char * address
)
288 DBusMessageIter array_iter
;
289 const char * component
;
291 if (!dbus_message_iter_open_container(iter_ptr
, DBUS_TYPE_ARRAY
, "s", &array_iter
))
293 lash_error("dbus_message_iter_open_container() failed.");
300 while (*component
!= 0)
302 if (!dbus_message_iter_append_basic(&array_iter
, DBUS_TYPE_STRING
, &component
))
304 lash_error("dbus_message_iter_append_basic() failed.");
308 component
+= strlen(component
) + 1;
312 dbus_message_iter_close_container(iter_ptr
, &array_iter
);
318 jack_proxy_read_conf_container(
319 const char * address
,
320 void * callback_context
,
321 bool (* callback
)(void * context
, bool leaf
, const char * address
, char * child
))
323 DBusMessage
* request_ptr
;
324 DBusMessage
* reply_ptr
;
325 DBusMessageIter top_iter
;
326 DBusMessageIter array_iter
;
327 const char * reply_signature
;
328 dbus_bool_t leaf
; /* Whether children are parameters (true) or containers (false) */
331 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "ReadContainer");
332 if (request_ptr
== NULL
)
334 lash_error("dbus_message_new_method_call() failed.");
338 dbus_message_iter_init_append(request_ptr
, &top_iter
);
340 if (!add_address(&top_iter
, address
))
342 dbus_message_unref(request_ptr
);
346 // send message and get a handle for a reply
347 reply_ptr
= dbus_connection_send_with_reply_and_block(
350 DBUS_CALL_DEFAULT_TIMEOUT
,
353 dbus_message_unref(request_ptr
);
355 if (reply_ptr
== NULL
)
357 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
358 dbus_error_free(&g_dbus_error
);
362 reply_signature
= dbus_message_get_signature(reply_ptr
);
364 if (strcmp(reply_signature
, "bas") != 0)
366 lash_error("ReadContainer() reply signature mismatch. '%s'", reply_signature
);
367 dbus_message_unref(reply_ptr
);
371 dbus_message_iter_init(reply_ptr
, &top_iter
);
373 dbus_message_iter_get_basic(&top_iter
, &leaf
);
374 dbus_message_iter_next(&top_iter
);
376 dbus_message_iter_recurse(&top_iter
, &array_iter
);
378 while (dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
)
380 dbus_message_iter_get_basic(&array_iter
, &child
);
382 if (!callback(callback_context
, leaf
, address
, child
))
387 dbus_message_iter_next(&array_iter
);
390 dbus_message_unref(reply_ptr
);
397 DBusMessageIter
* iter_ptr
,
398 struct jack_parameter_variant
* parameter_ptr
)
400 DBusMessageIter variant_iter
;
404 dbus_uint32_t uint32
;
407 dbus_message_iter_recurse(iter_ptr
, &variant_iter
);
408 lash_debug("variant signature: '%s'", dbus_message_iter_get_signature(&variant_iter
));
410 type
= dbus_message_iter_get_arg_type(&variant_iter
);
413 case DBUS_TYPE_INT32
:
414 dbus_message_iter_get_basic(&variant_iter
, &int32
);
415 parameter_ptr
->value
.int32
= int32
;
416 parameter_ptr
->type
= jack_int32
;
418 case DBUS_TYPE_UINT32
:
419 dbus_message_iter_get_basic(&variant_iter
, &uint32
);
420 parameter_ptr
->value
.uint32
= uint32
;
421 parameter_ptr
->type
= jack_uint32
;
424 dbus_message_iter_get_basic(&variant_iter
, ¶meter_ptr
->value
.byte
);
425 parameter_ptr
->type
= jack_byte
;
427 case DBUS_TYPE_STRING
:
428 dbus_message_iter_get_basic(&variant_iter
, &string
);
429 string
= strdup(string
);
432 lash_error("strdup failed.");
436 parameter_ptr
->value
.string
= string
;
437 parameter_ptr
->type
= jack_string
;
439 case DBUS_TYPE_BOOLEAN
:
440 dbus_message_iter_get_basic(&variant_iter
, &boolean
);
441 parameter_ptr
->value
.boolean
= boolean
;
442 parameter_ptr
->type
= jack_boolean
;
446 lash_error("Unknown D-Bus parameter type %i", (int)type
);
451 jack_proxy_get_parameter_value(
452 const char * address
,
454 struct jack_parameter_variant
* parameter_ptr
)
456 DBusMessage
* request_ptr
;
457 DBusMessage
* reply_ptr
;
458 DBusMessageIter top_iter
;
459 const char * reply_signature
;
461 struct jack_parameter_variant default_value
;
463 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "GetParameterValue");
464 if (request_ptr
== NULL
)
466 lash_error("dbus_message_new_method_call() failed.");
470 dbus_message_iter_init_append(request_ptr
, &top_iter
);
472 if (!add_address(&top_iter
, address
))
474 dbus_message_unref(request_ptr
);
478 // send message and get a handle for a reply
479 reply_ptr
= dbus_connection_send_with_reply_and_block(
482 DBUS_CALL_DEFAULT_TIMEOUT
,
485 dbus_message_unref(request_ptr
);
487 if (reply_ptr
== NULL
)
489 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
490 dbus_error_free(&g_dbus_error
);
494 reply_signature
= dbus_message_get_signature(reply_ptr
);
496 if (strcmp(reply_signature
, "bvv") != 0)
498 lash_error("GetParameterValue() reply signature mismatch. '%s'", reply_signature
);
499 dbus_message_unref(reply_ptr
);
503 dbus_message_iter_init(reply_ptr
, &top_iter
);
505 dbus_message_iter_get_basic(&top_iter
, &is_set
);
506 dbus_message_iter_next(&top_iter
);
508 if (!get_variant(&top_iter
, &default_value
))
510 dbus_message_unref(reply_ptr
);
514 if (default_value
.type
== jack_string
)
516 free(default_value
.value
.string
);
519 dbus_message_iter_next(&top_iter
);
521 if (!get_variant(&top_iter
, parameter_ptr
))
523 dbus_message_unref(reply_ptr
);
527 dbus_message_unref(reply_ptr
);
529 *is_set_ptr
= is_set
;
534 bool jack_proxy_start_server(void)
536 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "StartServer", "", "");
539 bool jack_proxy_stop_server(void)
541 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "StopServer", "", "");
544 bool jack_proxy_is_realtime(bool * realtime_ptr
)
546 dbus_bool_t realtime
;
548 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &realtime
))
553 *realtime_ptr
= realtime
;
557 bool jack_proxy_sample_rate(uint32_t * sample_rate_ptr
)
559 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetSampleRate", "", "u", sample_rate_ptr
);
562 bool jack_proxy_get_xruns(uint32_t * xruns_ptr
)
564 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetXruns", "", "u", xruns_ptr
);
567 bool jack_proxy_get_dsp_load(double * dsp_load_ptr
)
569 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetXruns", "", "d", dsp_load_ptr
);
572 bool jack_proxy_get_buffer_size(uint32_t * size_ptr
)
574 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "", "u", size_ptr
);
577 bool jack_proxy_set_buffer_size(uint32_t size
)
579 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "u", &size
, "");
582 bool jack_proxy_reset_xruns(void)
584 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "", "");