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"
36 #define JACKDBUS_IFACE_CONTROL "org.jackaudio.JackControl"
37 #define JACKDBUS_IFACE_CONFIGURE "org.jackaudio.Configure"
39 jack_proxy_callback_server_started g_on_server_started
;
40 jack_proxy_callback_server_stopped g_on_server_stopped
;
41 jack_proxy_callback_server_appeared g_on_server_appeared
;
42 jack_proxy_callback_server_disappeared g_on_server_disappeared
;
46 on_jack_control_signal(
47 DBusMessage
* message_ptr
,
48 const char * signal_name
)
50 if (strcmp(signal_name
, "ServerStarted") == 0)
52 lash_debug("JACK server start detected.");
53 if (g_on_server_started
!= NULL
)
55 g_on_server_started();
61 if (strcmp(signal_name
, "ServerStopped") == 0)
63 lash_debug("JACK server stop detected.");
64 if (g_on_server_stopped
!= NULL
)
66 g_on_server_stopped();
76 DBusMessage
* message_ptr
,
77 const char * signal_name
)
79 const char * object_name
;
80 const char * old_owner
;
81 const char * new_owner
;
83 //lash_info("bus signal '%s' received", signal_name);
85 dbus_error_init(&g_dbus_error
);
87 if (strcmp(signal_name
, "NameOwnerChanged") == 0)
89 //lash_info("NameOwnerChanged signal received");
91 if (!dbus_message_get_args(
94 DBUS_TYPE_STRING
, &object_name
,
95 DBUS_TYPE_STRING
, &old_owner
,
96 DBUS_TYPE_STRING
, &new_owner
,
98 lash_error("Cannot get message arguments: %s", g_dbus_error
.message
);
99 dbus_error_free(&g_dbus_error
);
100 return DBUS_HANDLER_RESULT_HANDLED
;
103 if (strcmp(object_name
, JACKDBUS_SERVICE
) != 0)
105 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
108 if (old_owner
[0] == '\0')
110 lash_debug("JACK serivce appeared");
111 if (g_on_server_appeared
!= NULL
)
113 g_on_server_appeared();
116 else if (new_owner
[0] == '\0')
118 lash_debug("JACK serivce disappeared");
119 if (g_on_server_disappeared
!= NULL
)
121 g_on_server_disappeared();
125 return DBUS_HANDLER_RESULT_HANDLED
;
128 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
134 DBusConnection
* connection_ptr
,
135 DBusMessage
* message_ptr
,
138 const char * object_path
;
139 const char * interface
;
140 const char * signal_name
;
142 /* Non-signal messages are ignored */
143 if (dbus_message_get_type(message_ptr
) != DBUS_MESSAGE_TYPE_SIGNAL
)
145 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
148 interface
= dbus_message_get_interface(message_ptr
);
149 if (interface
== NULL
)
151 /* Signals with no interface are ignored */
152 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
155 object_path
= dbus_message_get_path(message_ptr
);
157 signal_name
= dbus_message_get_member(message_ptr
);
158 if (signal_name
== NULL
)
160 lash_error("Received signal with NULL member");
161 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
164 lash_debug("'%s' sent signal '%s'::'%s'", object_path
, interface
, signal_name
);
166 /* Handle JACK patchbay and control interface signals */
167 if (object_path
!= NULL
&& strcmp(object_path
, JACKDBUS_OBJECT
) == 0)
169 if (strcmp(interface
, JACKDBUS_IFACE_CONTROL
) == 0)
171 on_jack_control_signal(message_ptr
, signal_name
);
172 return DBUS_HANDLER_RESULT_HANDLED
;
175 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
178 /* Handle session bus signals to track JACK service alive state */
179 if (strcmp(interface
, DBUS_INTERFACE_DBUS
) == 0)
181 return on_bus_signal(message_ptr
, signal_name
);
184 /* Let everything else pass through */
185 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
190 jack_proxy_callback_server_started server_started
,
191 jack_proxy_callback_server_stopped server_stopped
,
192 jack_proxy_callback_server_appeared server_appeared
,
193 jack_proxy_callback_server_disappeared server_disappeared
)
197 const char ** signal
;
199 const char * control_signals
[] = {
206 "type='signal',interface='"DBUS_INTERFACE_DBUS
"',member=NameOwnerChanged,arg0='"JACKDBUS_SERVICE
"'",
208 if (dbus_error_is_set(&err
))
210 lash_error("Failed to add D-Bus match rule: %s", err
.message
);
211 dbus_error_free(&err
);
215 for (signal
= control_signals
; *signal
!= NULL
; signal
++)
220 "type='signal',sender='"JACKDBUS_SERVICE
"',path='"JACKDBUS_OBJECT
"',interface='"JACKDBUS_IFACE_CONTROL
"',member='%s'",
223 dbus_bus_add_match(g_dbus_connection
, rule
, &err
);
224 if (dbus_error_is_set(&err
))
226 lash_error("Failed to add D-Bus match rule: %s", err
.message
);
227 dbus_error_free(&err
);
232 if (!dbus_connection_add_filter(g_dbus_connection
, dbus_signal_handler
, NULL
, NULL
))
234 lash_error("Failed to add D-Bus filter");
238 g_on_server_started
= server_started
;
239 g_on_server_stopped
= server_stopped
;
240 g_on_server_appeared
= server_appeared
;
241 g_on_server_disappeared
= server_disappeared
;
250 dbus_connection_remove_filter(g_dbus_connection
, dbus_signal_handler
, NULL
);
254 jack_proxy_is_started(
259 if (!dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &started
))
264 *started_ptr
= started
;
269 jack_proxy_get_client_pid(
277 jack_proxy_connect_ports(
287 DBusMessageIter
* iter_ptr
,
288 const char * address
)
290 DBusMessageIter array_iter
;
291 const char * component
;
293 if (!dbus_message_iter_open_container(iter_ptr
, DBUS_TYPE_ARRAY
, "s", &array_iter
))
295 lash_error("dbus_message_iter_open_container() failed.");
302 while (*component
!= 0)
304 if (!dbus_message_iter_append_basic(&array_iter
, DBUS_TYPE_STRING
, &component
))
306 lash_error("dbus_message_iter_append_basic() failed.");
310 component
+= strlen(component
) + 1;
314 dbus_message_iter_close_container(iter_ptr
, &array_iter
);
320 jack_proxy_read_conf_container(
321 const char * address
,
322 void * callback_context
,
323 bool (* callback
)(void * context
, bool leaf
, const char * address
, char * child
))
325 DBusMessage
* request_ptr
;
326 DBusMessage
* reply_ptr
;
327 DBusMessageIter top_iter
;
328 DBusMessageIter array_iter
;
329 const char * reply_signature
;
330 dbus_bool_t leaf
; /* Whether children are parameters (true) or containers (false) */
333 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONFIGURE
, "ReadContainer");
334 if (request_ptr
== NULL
)
336 lash_error("dbus_message_new_method_call() failed.");
340 dbus_message_iter_init_append(request_ptr
, &top_iter
);
342 if (!add_address(&top_iter
, address
))
344 dbus_message_unref(request_ptr
);
348 // send message and get a handle for a reply
349 reply_ptr
= dbus_connection_send_with_reply_and_block(
352 DBUS_CALL_DEFAULT_TIMEOUT
,
355 dbus_message_unref(request_ptr
);
357 if (reply_ptr
== NULL
)
359 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
360 dbus_error_free(&g_dbus_error
);
364 reply_signature
= dbus_message_get_signature(reply_ptr
);
366 if (strcmp(reply_signature
, "bas") != 0)
368 lash_error("ReadContainer() reply signature mismatch. '%s'", reply_signature
);
369 dbus_message_unref(reply_ptr
);
373 dbus_message_iter_init(reply_ptr
, &top_iter
);
375 dbus_message_iter_get_basic(&top_iter
, &leaf
);
376 dbus_message_iter_next(&top_iter
);
378 dbus_message_iter_recurse(&top_iter
, &array_iter
);
380 while (dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
)
382 dbus_message_iter_get_basic(&array_iter
, &child
);
384 if (!callback(callback_context
, leaf
, address
, child
))
389 dbus_message_iter_next(&array_iter
);
392 dbus_message_unref(reply_ptr
);
399 DBusMessageIter
* iter_ptr
,
400 struct jack_parameter_variant
* parameter_ptr
)
402 DBusMessageIter variant_iter
;
406 dbus_uint32_t uint32
;
409 dbus_message_iter_recurse(iter_ptr
, &variant_iter
);
410 lash_debug("variant signature: '%s'", dbus_message_iter_get_signature(&variant_iter
));
412 type
= dbus_message_iter_get_arg_type(&variant_iter
);
415 case DBUS_TYPE_INT32
:
416 dbus_message_iter_get_basic(&variant_iter
, &int32
);
417 parameter_ptr
->value
.int32
= int32
;
418 parameter_ptr
->type
= jack_int32
;
420 case DBUS_TYPE_UINT32
:
421 dbus_message_iter_get_basic(&variant_iter
, &uint32
);
422 parameter_ptr
->value
.uint32
= uint32
;
423 parameter_ptr
->type
= jack_uint32
;
426 dbus_message_iter_get_basic(&variant_iter
, ¶meter_ptr
->value
.byte
);
427 parameter_ptr
->type
= jack_byte
;
429 case DBUS_TYPE_STRING
:
430 dbus_message_iter_get_basic(&variant_iter
, &string
);
431 string
= strdup(string
);
434 lash_error("strdup failed.");
438 parameter_ptr
->value
.string
= string
;
439 parameter_ptr
->type
= jack_string
;
441 case DBUS_TYPE_BOOLEAN
:
442 dbus_message_iter_get_basic(&variant_iter
, &boolean
);
443 parameter_ptr
->value
.boolean
= boolean
;
444 parameter_ptr
->type
= jack_boolean
;
448 lash_error("Unknown D-Bus parameter type %i", (int)type
);
453 jack_proxy_get_parameter_value(
454 const char * address
,
456 struct jack_parameter_variant
* parameter_ptr
)
458 DBusMessage
* request_ptr
;
459 DBusMessage
* reply_ptr
;
460 DBusMessageIter top_iter
;
461 const char * reply_signature
;
463 struct jack_parameter_variant default_value
;
465 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONFIGURE
, "GetParameterValue");
466 if (request_ptr
== NULL
)
468 lash_error("dbus_message_new_method_call() failed.");
472 dbus_message_iter_init_append(request_ptr
, &top_iter
);
474 if (!add_address(&top_iter
, address
))
476 dbus_message_unref(request_ptr
);
480 // send message and get a handle for a reply
481 reply_ptr
= dbus_connection_send_with_reply_and_block(
484 DBUS_CALL_DEFAULT_TIMEOUT
,
487 dbus_message_unref(request_ptr
);
489 if (reply_ptr
== NULL
)
491 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
492 dbus_error_free(&g_dbus_error
);
496 reply_signature
= dbus_message_get_signature(reply_ptr
);
498 if (strcmp(reply_signature
, "bvv") != 0)
500 lash_error("GetParameterValue() reply signature mismatch. '%s'", reply_signature
);
501 dbus_message_unref(reply_ptr
);
505 dbus_message_iter_init(reply_ptr
, &top_iter
);
507 dbus_message_iter_get_basic(&top_iter
, &is_set
);
508 dbus_message_iter_next(&top_iter
);
510 if (!get_variant(&top_iter
, &default_value
))
512 dbus_message_unref(reply_ptr
);
516 if (default_value
.type
== jack_string
)
518 free(default_value
.value
.string
);
521 dbus_message_iter_next(&top_iter
);
523 if (!get_variant(&top_iter
, parameter_ptr
))
525 dbus_message_unref(reply_ptr
);
529 dbus_message_unref(reply_ptr
);
531 *is_set_ptr
= is_set
;
536 bool jack_proxy_start_server(void)
538 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "StartServer", "", "");
541 bool jack_proxy_stop_server(void)
543 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "StopServer", "", "");
546 bool jack_proxy_is_realtime(bool * realtime_ptr
)
548 dbus_bool_t realtime
;
550 if (!dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &realtime
))
555 *realtime_ptr
= realtime
;
559 bool jack_proxy_sample_rate(uint32_t * sample_rate_ptr
)
561 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetSampleRate", "", "u", sample_rate_ptr
);
564 bool jack_proxy_get_xruns(uint32_t * xruns_ptr
)
566 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetXruns", "", "u", xruns_ptr
);
569 bool jack_proxy_get_dsp_load(double * dsp_load_ptr
)
571 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetXruns", "", "d", dsp_load_ptr
);
574 bool jack_proxy_get_buffer_size(uint32_t * size_ptr
)
576 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "", "u", size_ptr
);
579 bool jack_proxy_set_buffer_size(uint32_t size
)
581 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "u", &size
, "");
584 bool jack_proxy_reset_xruns(void)
586 return dbus_call_simple(JACKDBUS_SERVICE
, JACKDBUS_OBJECT
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "", "");