Two views, one for raw jack and one for studio
[ladish.git] / jack_proxy.c
blob4007c2e3674daeb689cde96d6d2416085aca47b0
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"
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;
42 static
43 void
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();
56 return;
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();
67 return;
71 static
72 DBusHandlerResult
73 on_bus_signal(
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(
90 message_ptr,
91 &g_dbus_error,
92 DBUS_TYPE_STRING, &object_name,
93 DBUS_TYPE_STRING, &old_owner,
94 DBUS_TYPE_STRING, &new_owner,
95 DBUS_TYPE_INVALID)) {
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;
129 static
130 DBusHandlerResult
131 dbus_signal_handler(
132 DBusConnection * connection_ptr,
133 DBusMessage * message_ptr,
134 void * data)
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;
186 bool
187 jack_proxy_init(
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)
193 DBusError err;
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 &err);
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);
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, &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);
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;
241 return true;
244 void
245 jack_proxy_uninit(
246 void)
248 dbus_connection_remove_filter(g_dbus_connection, dbus_signal_handler, NULL);
251 bool
252 jack_proxy_is_started(
253 bool * started_ptr)
255 dbus_bool_t started;
257 if (!dbus_call_simple(JACKDBUS_SERVICE_NAME, JACKDBUS_OBJECT_PATH, JACKDBUS_IFACE_CONTROL, "IsStarted", "", "b", &started))
259 return false;
262 *started_ptr = started;
263 return true;
266 bool
267 jack_proxy_get_client_pid(
268 uint64_t client_id,
269 pid_t * pid_ptr)
271 return false;
274 bool
275 jack_proxy_connect_ports(
276 uint64_t port1_id,
277 uint64_t port2_id)
279 return false;
282 static
283 bool
284 add_address(
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.");
294 return false;
297 if (address != NULL)
299 component = address;
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.");
305 return false;
308 component += strlen(component) + 1;
312 dbus_message_iter_close_container(iter_ptr, &array_iter);
314 return true;
317 bool
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) */
329 char * child;
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.");
335 return false;
338 dbus_message_iter_init_append(request_ptr, &top_iter);
340 if (!add_address(&top_iter, address))
342 dbus_message_unref(request_ptr);
343 return false;
346 // send message and get a handle for a reply
347 reply_ptr = dbus_connection_send_with_reply_and_block(
348 g_dbus_connection,
349 request_ptr,
350 DBUS_CALL_DEFAULT_TIMEOUT,
351 &g_dbus_error);
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);
359 return false;
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);
368 return false;
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))
384 break;
387 dbus_message_iter_next(&array_iter);
390 dbus_message_unref(reply_ptr);
392 return true;
395 bool
396 get_variant(
397 DBusMessageIter * iter_ptr,
398 struct jack_parameter_variant * parameter_ptr)
400 DBusMessageIter variant_iter;
401 int type;
402 dbus_bool_t boolean;
403 dbus_int32_t int32;
404 dbus_uint32_t uint32;
405 char * string;
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);
411 switch (type)
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;
417 return true;
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;
422 return true;
423 case DBUS_TYPE_BYTE:
424 dbus_message_iter_get_basic(&variant_iter, &parameter_ptr->value.byte);
425 parameter_ptr->type = jack_byte;
426 return true;
427 case DBUS_TYPE_STRING:
428 dbus_message_iter_get_basic(&variant_iter, &string);
429 string = strdup(string);
430 if (string == NULL)
432 lash_error("strdup failed.");
433 return false;
436 parameter_ptr->value.string = string;
437 parameter_ptr->type = jack_string;
438 return true;
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;
443 return true;
446 lash_error("Unknown D-Bus parameter type %i", (int)type);
447 return false;
450 bool
451 jack_proxy_get_parameter_value(
452 const char * address,
453 bool * is_set_ptr,
454 struct jack_parameter_variant * parameter_ptr)
456 DBusMessage * request_ptr;
457 DBusMessage * reply_ptr;
458 DBusMessageIter top_iter;
459 const char * reply_signature;
460 dbus_bool_t is_set;
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.");
467 return false;
470 dbus_message_iter_init_append(request_ptr, &top_iter);
472 if (!add_address(&top_iter, address))
474 dbus_message_unref(request_ptr);
475 return false;
478 // send message and get a handle for a reply
479 reply_ptr = dbus_connection_send_with_reply_and_block(
480 g_dbus_connection,
481 request_ptr,
482 DBUS_CALL_DEFAULT_TIMEOUT,
483 &g_dbus_error);
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);
491 return false;
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);
500 return false;
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);
511 return false;
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);
524 return false;
527 dbus_message_unref(reply_ptr);
529 *is_set_ptr = is_set;
531 return true;
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))
550 return false;
553 *realtime_ptr = realtime;
554 return true;
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", "", "");