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"
36 #include "dbus/method.h"
38 jack_proxy_callback_server_started g_on_server_started
;
39 jack_proxy_callback_server_stopped g_on_server_stopped
;
40 jack_proxy_callback_server_appeared g_on_server_appeared
;
41 jack_proxy_callback_server_disappeared g_on_server_disappeared
;
45 on_jack_control_signal(
46 DBusMessage
* message_ptr
,
47 const char * signal_name
)
49 if (strcmp(signal_name
, "ServerStarted") == 0)
51 lash_debug("JACK server start detected.");
52 if (g_on_server_started
!= NULL
)
54 g_on_server_started();
60 if (strcmp(signal_name
, "ServerStopped") == 0)
62 lash_debug("JACK server stop detected.");
63 if (g_on_server_stopped
!= NULL
)
65 g_on_server_stopped();
75 DBusMessage
* message_ptr
,
76 const char * signal_name
)
78 const char * object_name
;
79 const char * old_owner
;
80 const char * new_owner
;
82 //lash_info("bus signal '%s' received", signal_name);
84 dbus_error_init(&g_dbus_error
);
86 if (strcmp(signal_name
, "NameOwnerChanged") == 0)
88 //lash_info("NameOwnerChanged signal received");
90 if (!dbus_message_get_args(
93 DBUS_TYPE_STRING
, &object_name
,
94 DBUS_TYPE_STRING
, &old_owner
,
95 DBUS_TYPE_STRING
, &new_owner
,
97 lash_error("Cannot get message arguments: %s", g_dbus_error
.message
);
98 dbus_error_free(&g_dbus_error
);
99 return DBUS_HANDLER_RESULT_HANDLED
;
102 if (strcmp(object_name
, JACKDBUS_SERVICE_NAME
) != 0)
104 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
107 if (old_owner
[0] == '\0')
109 lash_debug("JACK serivce appeared");
110 if (g_on_server_appeared
!= NULL
)
112 g_on_server_appeared();
115 else if (new_owner
[0] == '\0')
117 lash_debug("JACK serivce disappeared");
118 if (g_on_server_disappeared
!= NULL
)
120 g_on_server_disappeared();
124 return DBUS_HANDLER_RESULT_HANDLED
;
127 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
133 DBusConnection
* connection_ptr
,
134 DBusMessage
* message_ptr
,
137 const char * object_path
;
138 const char * interface
;
139 const char * signal_name
;
141 /* Non-signal messages are ignored */
142 if (dbus_message_get_type(message_ptr
) != DBUS_MESSAGE_TYPE_SIGNAL
)
144 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
147 interface
= dbus_message_get_interface(message_ptr
);
148 if (interface
== NULL
)
150 /* Signals with no interface are ignored */
151 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
154 object_path
= dbus_message_get_path(message_ptr
);
156 signal_name
= dbus_message_get_member(message_ptr
);
157 if (signal_name
== NULL
)
159 lash_error("Received signal with NULL member");
160 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
163 lash_debug("'%s' sent signal '%s'::'%s'", object_path
, interface
, signal_name
);
165 /* Handle JACK patchbay and control interface signals */
166 if (object_path
!= NULL
&& strcmp(object_path
, JACKDBUS_OBJECT_PATH
) == 0)
168 if (strcmp(interface
, JACKDBUS_IFACE_CONTROL
) == 0)
170 on_jack_control_signal(message_ptr
, signal_name
);
171 return DBUS_HANDLER_RESULT_HANDLED
;
174 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
177 /* Handle session bus signals to track JACK service alive state */
178 if (strcmp(interface
, DBUS_INTERFACE_DBUS
) == 0)
180 return on_bus_signal(message_ptr
, signal_name
);
183 /* Let everything else pass through */
184 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
189 jack_proxy_callback_server_started server_started
,
190 jack_proxy_callback_server_stopped server_stopped
,
191 jack_proxy_callback_server_appeared server_appeared
,
192 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(&g_dbus_error
))
208 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
209 dbus_error_free(&g_dbus_error
);
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
, &g_dbus_error
);
222 if (dbus_error_is_set(&g_dbus_error
))
224 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
225 dbus_error_free(&g_dbus_error
);
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
;
244 if (jack_proxy_is_started(&started
))
246 if (g_on_server_appeared
!= NULL
)
248 g_on_server_appeared();
251 if (g_on_server_started
!= NULL
&& started
)
253 g_on_server_started();
265 dbus_connection_remove_filter(g_dbus_connection
, dbus_signal_handler
, NULL
);
269 jack_proxy_is_started(
274 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &started
))
279 *started_ptr
= started
;
284 jack_proxy_get_client_pid(
292 jack_proxy_connect_ports(
302 DBusMessageIter
* iter_ptr
,
303 const char * address
)
305 DBusMessageIter array_iter
;
306 const char * component
;
308 if (!dbus_message_iter_open_container(iter_ptr
, DBUS_TYPE_ARRAY
, "s", &array_iter
))
310 lash_error("dbus_message_iter_open_container() failed.");
317 while (*component
!= 0)
319 if (!dbus_message_iter_append_basic(&array_iter
, DBUS_TYPE_STRING
, &component
))
321 lash_error("dbus_message_iter_append_basic() failed.");
325 component
+= strlen(component
) + 1;
329 dbus_message_iter_close_container(iter_ptr
, &array_iter
);
335 jack_proxy_read_conf_container(
336 const char * address
,
337 void * callback_context
,
338 bool (* callback
)(void * context
, bool leaf
, const char * address
, char * child
))
340 DBusMessage
* request_ptr
;
341 DBusMessage
* reply_ptr
;
342 DBusMessageIter top_iter
;
343 DBusMessageIter array_iter
;
344 const char * reply_signature
;
345 dbus_bool_t leaf
; /* Whether children are parameters (true) or containers (false) */
348 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "ReadContainer");
349 if (request_ptr
== NULL
)
351 lash_error("dbus_message_new_method_call() failed.");
355 dbus_message_iter_init_append(request_ptr
, &top_iter
);
357 if (!add_address(&top_iter
, address
))
359 dbus_message_unref(request_ptr
);
363 // send message and get a handle for a reply
364 reply_ptr
= dbus_connection_send_with_reply_and_block(
367 DBUS_CALL_DEFAULT_TIMEOUT
,
370 dbus_message_unref(request_ptr
);
372 if (reply_ptr
== NULL
)
374 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
375 dbus_error_free(&g_dbus_error
);
379 reply_signature
= dbus_message_get_signature(reply_ptr
);
381 if (strcmp(reply_signature
, "bas") != 0)
383 lash_error("ReadContainer() reply signature mismatch. '%s'", reply_signature
);
384 dbus_message_unref(reply_ptr
);
388 dbus_message_iter_init(reply_ptr
, &top_iter
);
390 dbus_message_iter_get_basic(&top_iter
, &leaf
);
391 dbus_message_iter_next(&top_iter
);
393 dbus_message_iter_recurse(&top_iter
, &array_iter
);
395 while (dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
)
397 dbus_message_iter_get_basic(&array_iter
, &child
);
399 if (!callback(callback_context
, leaf
, address
, child
))
404 dbus_message_iter_next(&array_iter
);
407 dbus_message_unref(reply_ptr
);
414 DBusMessageIter
* iter_ptr
,
415 struct jack_parameter_variant
* parameter_ptr
)
417 DBusMessageIter variant_iter
;
421 dbus_uint32_t uint32
;
424 dbus_message_iter_recurse(iter_ptr
, &variant_iter
);
425 lash_debug("variant signature: '%s'", dbus_message_iter_get_signature(&variant_iter
));
427 type
= dbus_message_iter_get_arg_type(&variant_iter
);
430 case DBUS_TYPE_INT32
:
431 dbus_message_iter_get_basic(&variant_iter
, &int32
);
432 parameter_ptr
->value
.int32
= int32
;
433 parameter_ptr
->type
= jack_int32
;
435 case DBUS_TYPE_UINT32
:
436 dbus_message_iter_get_basic(&variant_iter
, &uint32
);
437 parameter_ptr
->value
.uint32
= uint32
;
438 parameter_ptr
->type
= jack_uint32
;
441 dbus_message_iter_get_basic(&variant_iter
, ¶meter_ptr
->value
.byte
);
442 parameter_ptr
->type
= jack_byte
;
444 case DBUS_TYPE_STRING
:
445 dbus_message_iter_get_basic(&variant_iter
, &string
);
446 string
= strdup(string
);
449 lash_error("strdup failed.");
453 parameter_ptr
->value
.string
= string
;
454 parameter_ptr
->type
= jack_string
;
456 case DBUS_TYPE_BOOLEAN
:
457 dbus_message_iter_get_basic(&variant_iter
, &boolean
);
458 parameter_ptr
->value
.boolean
= boolean
;
459 parameter_ptr
->type
= jack_boolean
;
463 lash_error("Unknown D-Bus parameter type %i", (int)type
);
468 jack_proxy_get_parameter_value(
469 const char * address
,
471 struct jack_parameter_variant
* parameter_ptr
)
473 DBusMessage
* request_ptr
;
474 DBusMessage
* reply_ptr
;
475 DBusMessageIter top_iter
;
476 const char * reply_signature
;
478 struct jack_parameter_variant default_value
;
480 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "GetParameterValue");
481 if (request_ptr
== NULL
)
483 lash_error("dbus_message_new_method_call() failed.");
487 dbus_message_iter_init_append(request_ptr
, &top_iter
);
489 if (!add_address(&top_iter
, address
))
491 dbus_message_unref(request_ptr
);
495 // send message and get a handle for a reply
496 reply_ptr
= dbus_connection_send_with_reply_and_block(
499 DBUS_CALL_DEFAULT_TIMEOUT
,
502 dbus_message_unref(request_ptr
);
504 if (reply_ptr
== NULL
)
506 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
507 dbus_error_free(&g_dbus_error
);
511 reply_signature
= dbus_message_get_signature(reply_ptr
);
513 if (strcmp(reply_signature
, "bvv") != 0)
515 lash_error("GetParameterValue() reply signature mismatch. '%s'", reply_signature
);
516 dbus_message_unref(reply_ptr
);
520 dbus_message_iter_init(reply_ptr
, &top_iter
);
522 dbus_message_iter_get_basic(&top_iter
, &is_set
);
523 dbus_message_iter_next(&top_iter
);
525 if (!get_variant(&top_iter
, &default_value
))
527 dbus_message_unref(reply_ptr
);
531 if (default_value
.type
== jack_string
)
533 free(default_value
.value
.string
);
536 dbus_message_iter_next(&top_iter
);
538 if (!get_variant(&top_iter
, parameter_ptr
))
540 dbus_message_unref(reply_ptr
);
544 dbus_message_unref(reply_ptr
);
546 *is_set_ptr
= is_set
;
552 jack_proxy_set_parameter_value(
553 const char * address
,
554 const struct jack_parameter_variant
* parameter_ptr
)
556 DBusMessage
* request_ptr
;
557 DBusMessage
* reply_ptr
;
558 DBusMessageIter top_iter
;
559 const char * reply_signature
;
561 const void * value_ptr
;
564 switch (parameter_ptr
->type
)
567 type
= DBUS_TYPE_INT32
;
568 value_ptr
= ¶meter_ptr
->value
.int32
;
571 type
= DBUS_TYPE_UINT32
;
572 value_ptr
= ¶meter_ptr
->value
.uint32
;
575 type
= DBUS_TYPE_BYTE
;
576 value_ptr
= ¶meter_ptr
->value
.byte
;
579 type
= DBUS_TYPE_STRING
;
580 value_ptr
= ¶meter_ptr
->value
.string
;
583 type
= DBUS_TYPE_BOOLEAN
;
584 boolean
= parameter_ptr
->value
.boolean
;
585 value_ptr
= &boolean
;
588 lash_error("Unknown jack parameter type %i", (int)type
);
592 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "SetParameterValue");
593 if (request_ptr
== NULL
)
595 lash_error("dbus_message_new_method_call() failed.");
599 dbus_message_iter_init_append(request_ptr
, &top_iter
);
601 if (!add_address(&top_iter
, address
))
603 dbus_message_unref(request_ptr
);
607 if (!method_iter_append_variant(&top_iter
, type
, value_ptr
))
609 dbus_message_unref(request_ptr
);
613 // send message and get a handle for a reply
614 reply_ptr
= dbus_connection_send_with_reply_and_block(
617 DBUS_CALL_DEFAULT_TIMEOUT
,
620 dbus_message_unref(request_ptr
);
622 if (reply_ptr
== NULL
)
624 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
625 dbus_error_free(&g_dbus_error
);
629 reply_signature
= dbus_message_get_signature(reply_ptr
);
631 dbus_message_unref(reply_ptr
);
633 if (strcmp(reply_signature
, "") != 0)
635 lash_error("SetParameterValue() reply signature mismatch. '%s'", reply_signature
);
643 jack_proxy_reset_parameter_value(
644 const char * address
)
646 DBusMessage
* request_ptr
;
647 DBusMessage
* reply_ptr
;
648 DBusMessageIter top_iter
;
649 const char * reply_signature
;
651 request_ptr
= dbus_message_new_method_call(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONFIGURE
, "ResetParameterValue");
652 if (request_ptr
== NULL
)
654 lash_error("dbus_message_new_method_call() failed.");
658 dbus_message_iter_init_append(request_ptr
, &top_iter
);
660 if (!add_address(&top_iter
, address
))
662 dbus_message_unref(request_ptr
);
666 // send message and get a handle for a reply
667 reply_ptr
= dbus_connection_send_with_reply_and_block(
670 DBUS_CALL_DEFAULT_TIMEOUT
,
673 dbus_message_unref(request_ptr
);
675 if (reply_ptr
== NULL
)
677 lash_error("no reply from JACK server, error is '%s'", g_dbus_error
.message
);
678 dbus_error_free(&g_dbus_error
);
682 reply_signature
= dbus_message_get_signature(reply_ptr
);
684 dbus_message_unref(reply_ptr
);
686 if (strcmp(reply_signature
, "") != 0)
688 lash_error("ResetParameterValue() reply signature mismatch. '%s'", reply_signature
);
695 bool jack_proxy_start_server(void)
697 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "StartServer", "", "");
700 bool jack_proxy_stop_server(void)
702 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "StopServer", "", "");
705 bool jack_proxy_is_realtime(bool * realtime_ptr
)
707 dbus_bool_t realtime
;
709 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "IsStarted", "", "b", &realtime
))
714 *realtime_ptr
= realtime
;
718 bool jack_proxy_sample_rate(uint32_t * sample_rate_ptr
)
720 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetSampleRate", "", "u", sample_rate_ptr
);
723 bool jack_proxy_get_xruns(uint32_t * xruns_ptr
)
725 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetXruns", "", "u", xruns_ptr
);
728 bool jack_proxy_get_dsp_load(double * dsp_load_ptr
)
730 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetLoad", "", "d", dsp_load_ptr
);
733 bool jack_proxy_get_buffer_size(uint32_t * size_ptr
)
735 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "GetBufferSize", "", "u", size_ptr
);
738 bool jack_proxy_set_buffer_size(uint32_t size
)
740 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "SetBufferSize", "u", &size
, "");
743 bool jack_proxy_reset_xruns(void)
745 return dbus_call_simple(JACKDBUS_SERVICE_NAME
, JACKDBUS_OBJECT_PATH
, JACKDBUS_IFACE_CONTROL
, "ResetXruns", "", "");
753 const char * address
,
756 const char * component
;
760 if (*address
== 0 && !*(bool *)context
&& strcmp(child
, "drivers") == 0)
762 /* don't reset drivers branch unless requested */
767 while (*component
!= 0)
769 component
+= strlen(component
) + 1;
772 /* address always is same buffer as the one in the jack_reset_all_params() stack */
773 dst
= (char *)component
;
775 len
= strlen(child
) + 1;
776 memcpy(dst
, child
, len
);
781 if (!jack_proxy_reset_parameter_value(address
))
783 lash_error("cannot reset value of parameter");
789 if (!jack_proxy_read_conf_container(address
, context
, reset_callback
))
791 lash_error("cannot read container");
801 bool jack_reset_all_params(bool reset_drivers
)
803 char address
[1024] = "";
805 return jack_proxy_read_conf_container(address
, &reset_drivers
, reset_callback
);