set and reset parameter value proxies
[ladish.git] / jack_proxy.c
blob8d8d8eb6ac939f5d4f4f9037274fd85cff5f5a5e
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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.
27 //#define LASH_DEBUG
29 #include <dbus/dbus.h>
31 #include "common.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;
43 static
44 void
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();
57 return;
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();
68 return;
72 static
73 DBusHandlerResult
74 on_bus_signal(
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(
91 message_ptr,
92 &g_dbus_error,
93 DBUS_TYPE_STRING, &object_name,
94 DBUS_TYPE_STRING, &old_owner,
95 DBUS_TYPE_STRING, &new_owner,
96 DBUS_TYPE_INVALID)) {
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;
130 static
131 DBusHandlerResult
132 dbus_signal_handler(
133 DBusConnection * connection_ptr,
134 DBusMessage * message_ptr,
135 void * data)
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;
187 bool
188 jack_proxy_init(
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)
194 char rule[1024];
195 const char ** signal;
197 const char * control_signals[] = {
198 "ServerStarted",
199 "ServerStopped",
200 NULL};
202 dbus_bus_add_match(
203 g_dbus_connection,
204 "type='signal',interface='"DBUS_INTERFACE_DBUS"',member=NameOwnerChanged,arg0='"JACKDBUS_SERVICE_NAME"'",
205 &g_dbus_error);
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);
210 return false;
213 for (signal = control_signals; *signal != NULL; signal++)
215 snprintf(
216 rule,
217 sizeof(rule),
218 "type='signal',sender='"JACKDBUS_SERVICE_NAME"',path='"JACKDBUS_OBJECT_PATH"',interface='"JACKDBUS_IFACE_CONTROL"',member='%s'",
219 *signal);
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);
226 return false;
230 if (!dbus_connection_add_filter(g_dbus_connection, dbus_signal_handler, NULL, NULL))
232 lash_error("Failed to add D-Bus filter");
233 return false;
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;
242 bool started;
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();
258 return true;
261 void
262 jack_proxy_uninit(
263 void)
265 dbus_connection_remove_filter(g_dbus_connection, dbus_signal_handler, NULL);
268 bool
269 jack_proxy_is_started(
270 bool * started_ptr)
272 dbus_bool_t started;
274 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME, JACKDBUS_OBJECT_PATH, JACKDBUS_IFACE_CONTROL, "IsStarted", "", "b", &started))
276 return false;
279 *started_ptr = started;
280 return true;
283 bool
284 jack_proxy_get_client_pid(
285 uint64_t client_id,
286 pid_t * pid_ptr)
288 return false;
291 bool
292 jack_proxy_connect_ports(
293 uint64_t port1_id,
294 uint64_t port2_id)
296 return false;
299 static
300 bool
301 add_address(
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.");
311 return false;
314 if (address != NULL)
316 component = address;
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.");
322 return false;
325 component += strlen(component) + 1;
329 dbus_message_iter_close_container(iter_ptr, &array_iter);
331 return true;
334 bool
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) */
346 char * child;
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.");
352 return false;
355 dbus_message_iter_init_append(request_ptr, &top_iter);
357 if (!add_address(&top_iter, address))
359 dbus_message_unref(request_ptr);
360 return false;
363 // send message and get a handle for a reply
364 reply_ptr = dbus_connection_send_with_reply_and_block(
365 g_dbus_connection,
366 request_ptr,
367 DBUS_CALL_DEFAULT_TIMEOUT,
368 &g_dbus_error);
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);
376 return false;
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);
385 return false;
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))
401 break;
404 dbus_message_iter_next(&array_iter);
407 dbus_message_unref(reply_ptr);
409 return true;
412 bool
413 get_variant(
414 DBusMessageIter * iter_ptr,
415 struct jack_parameter_variant * parameter_ptr)
417 DBusMessageIter variant_iter;
418 int type;
419 dbus_bool_t boolean;
420 dbus_int32_t int32;
421 dbus_uint32_t uint32;
422 char * string;
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);
428 switch (type)
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;
434 return true;
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;
439 return true;
440 case DBUS_TYPE_BYTE:
441 dbus_message_iter_get_basic(&variant_iter, &parameter_ptr->value.byte);
442 parameter_ptr->type = jack_byte;
443 return true;
444 case DBUS_TYPE_STRING:
445 dbus_message_iter_get_basic(&variant_iter, &string);
446 string = strdup(string);
447 if (string == NULL)
449 lash_error("strdup failed.");
450 return false;
453 parameter_ptr->value.string = string;
454 parameter_ptr->type = jack_string;
455 return true;
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;
460 return true;
463 lash_error("Unknown D-Bus parameter type %i", (int)type);
464 return false;
467 bool
468 jack_proxy_get_parameter_value(
469 const char * address,
470 bool * is_set_ptr,
471 struct jack_parameter_variant * parameter_ptr)
473 DBusMessage * request_ptr;
474 DBusMessage * reply_ptr;
475 DBusMessageIter top_iter;
476 const char * reply_signature;
477 dbus_bool_t is_set;
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.");
484 return false;
487 dbus_message_iter_init_append(request_ptr, &top_iter);
489 if (!add_address(&top_iter, address))
491 dbus_message_unref(request_ptr);
492 return false;
495 // send message and get a handle for a reply
496 reply_ptr = dbus_connection_send_with_reply_and_block(
497 g_dbus_connection,
498 request_ptr,
499 DBUS_CALL_DEFAULT_TIMEOUT,
500 &g_dbus_error);
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);
508 return false;
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);
517 return false;
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);
528 return false;
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);
541 return false;
544 dbus_message_unref(reply_ptr);
546 *is_set_ptr = is_set;
548 return true;
551 bool
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;
560 int type;
561 const void * value_ptr;
562 dbus_bool_t boolean;
564 switch (parameter_ptr->type)
566 case jack_int32:
567 type = DBUS_TYPE_INT32;
568 value_ptr = &parameter_ptr->value.int32;
569 break;
570 case jack_uint32:
571 type = DBUS_TYPE_UINT32;
572 value_ptr = &parameter_ptr->value.uint32;
573 break;
574 case jack_byte:
575 type = DBUS_TYPE_BYTE;
576 value_ptr = &parameter_ptr->value.byte;
577 break;
578 case jack_string:
579 type = DBUS_TYPE_STRING;
580 value_ptr = &parameter_ptr->value.string;
581 break;
582 case jack_boolean:
583 type = DBUS_TYPE_BOOLEAN;
584 boolean = parameter_ptr->value.boolean;
585 value_ptr = &boolean;
586 break;
587 default:
588 lash_error("Unknown jack parameter type %i", (int)type);
589 return false;
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.");
596 return false;
599 dbus_message_iter_init_append(request_ptr, &top_iter);
601 if (!add_address(&top_iter, address))
603 dbus_message_unref(request_ptr);
604 return false;
607 if (!method_iter_append_variant(&top_iter, type, value_ptr))
609 dbus_message_unref(request_ptr);
610 return false;
613 // send message and get a handle for a reply
614 reply_ptr = dbus_connection_send_with_reply_and_block(
615 g_dbus_connection,
616 request_ptr,
617 DBUS_CALL_DEFAULT_TIMEOUT,
618 &g_dbus_error);
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);
626 return false;
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);
636 return false;
639 return false;
642 bool
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.");
655 return false;
658 dbus_message_iter_init_append(request_ptr, &top_iter);
660 if (!add_address(&top_iter, address))
662 dbus_message_unref(request_ptr);
663 return false;
666 // send message and get a handle for a reply
667 reply_ptr = dbus_connection_send_with_reply_and_block(
668 g_dbus_connection,
669 request_ptr,
670 DBUS_CALL_DEFAULT_TIMEOUT,
671 &g_dbus_error);
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);
679 return false;
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);
689 return false;
692 return true;
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))
711 return false;
714 *realtime_ptr = realtime;
715 return true;
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", "", "");