Quit when requested from the menu
[ladish.git] / jack_proxy.c
blobe8a710f7759f04c9c946ad1d80bb48cdee56b37b
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"
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;
44 static
45 void
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();
58 return;
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();
69 return;
73 static
74 DBusHandlerResult
75 on_bus_signal(
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(
92 message_ptr,
93 &g_dbus_error,
94 DBUS_TYPE_STRING, &object_name,
95 DBUS_TYPE_STRING, &old_owner,
96 DBUS_TYPE_STRING, &new_owner,
97 DBUS_TYPE_INVALID)) {
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;
131 static
132 DBusHandlerResult
133 dbus_signal_handler(
134 DBusConnection * connection_ptr,
135 DBusMessage * message_ptr,
136 void * data)
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;
188 bool
189 jack_proxy_init(
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)
195 DBusError err;
196 char rule[1024];
197 const char ** signal;
199 const char * control_signals[] = {
200 "ServerStarted",
201 "ServerStopped",
202 NULL};
204 dbus_bus_add_match(
205 g_dbus_connection,
206 "type='signal',interface='"DBUS_INTERFACE_DBUS"',member=NameOwnerChanged,arg0='"JACKDBUS_SERVICE"'",
207 &err);
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);
212 return false;
215 for (signal = control_signals; *signal != NULL; signal++)
217 snprintf(
218 rule,
219 sizeof(rule),
220 "type='signal',sender='"JACKDBUS_SERVICE"',path='"JACKDBUS_OBJECT"',interface='"JACKDBUS_IFACE_CONTROL"',member='%s'",
221 *signal);
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);
228 return false;
232 if (!dbus_connection_add_filter(g_dbus_connection, dbus_signal_handler, NULL, NULL))
234 lash_error("Failed to add D-Bus filter");
235 return false;
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;
243 return true;
246 void
247 jack_proxy_uninit(
248 void)
250 dbus_connection_remove_filter(g_dbus_connection, dbus_signal_handler, NULL);
253 bool
254 jack_proxy_is_started(
255 bool * started_ptr)
257 dbus_bool_t started;
259 if (!dbus_call_simple(JACKDBUS_SERVICE, JACKDBUS_OBJECT, JACKDBUS_IFACE_CONTROL, "IsStarted", "", "b", &started))
261 return false;
264 *started_ptr = started;
265 return true;
268 bool
269 jack_proxy_get_client_pid(
270 uint64_t client_id,
271 pid_t * pid_ptr)
273 return false;
276 bool
277 jack_proxy_connect_ports(
278 uint64_t port1_id,
279 uint64_t port2_id)
281 return false;
284 static
285 bool
286 add_address(
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.");
296 return false;
299 if (address != NULL)
301 component = address;
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.");
307 return false;
310 component += strlen(component) + 1;
314 dbus_message_iter_close_container(iter_ptr, &array_iter);
316 return true;
319 bool
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) */
331 char * child;
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.");
337 return false;
340 dbus_message_iter_init_append(request_ptr, &top_iter);
342 if (!add_address(&top_iter, address))
344 dbus_message_unref(request_ptr);
345 return false;
348 // send message and get a handle for a reply
349 reply_ptr = dbus_connection_send_with_reply_and_block(
350 g_dbus_connection,
351 request_ptr,
352 DBUS_CALL_DEFAULT_TIMEOUT,
353 &g_dbus_error);
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);
361 return false;
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);
370 return false;
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))
386 break;
389 dbus_message_iter_next(&array_iter);
392 dbus_message_unref(reply_ptr);
394 return true;
397 bool
398 get_variant(
399 DBusMessageIter * iter_ptr,
400 struct jack_parameter_variant * parameter_ptr)
402 DBusMessageIter variant_iter;
403 int type;
404 dbus_bool_t boolean;
405 dbus_int32_t int32;
406 dbus_uint32_t uint32;
407 char * string;
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);
413 switch (type)
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;
419 return true;
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;
424 return true;
425 case DBUS_TYPE_BYTE:
426 dbus_message_iter_get_basic(&variant_iter, &parameter_ptr->value.byte);
427 parameter_ptr->type = jack_byte;
428 return true;
429 case DBUS_TYPE_STRING:
430 dbus_message_iter_get_basic(&variant_iter, &string);
431 string = strdup(string);
432 if (string == NULL)
434 lash_error("strdup failed.");
435 return false;
438 parameter_ptr->value.string = string;
439 parameter_ptr->type = jack_string;
440 return true;
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;
445 return true;
448 lash_error("Unknown D-Bus parameter type %i", (int)type);
449 return false;
452 bool
453 jack_proxy_get_parameter_value(
454 const char * address,
455 bool * is_set_ptr,
456 struct jack_parameter_variant * parameter_ptr)
458 DBusMessage * request_ptr;
459 DBusMessage * reply_ptr;
460 DBusMessageIter top_iter;
461 const char * reply_signature;
462 dbus_bool_t is_set;
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.");
469 return false;
472 dbus_message_iter_init_append(request_ptr, &top_iter);
474 if (!add_address(&top_iter, address))
476 dbus_message_unref(request_ptr);
477 return false;
480 // send message and get a handle for a reply
481 reply_ptr = dbus_connection_send_with_reply_and_block(
482 g_dbus_connection,
483 request_ptr,
484 DBUS_CALL_DEFAULT_TIMEOUT,
485 &g_dbus_error);
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);
493 return false;
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);
502 return false;
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);
513 return false;
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);
526 return false;
529 dbus_message_unref(reply_ptr);
531 *is_set_ptr = is_set;
533 return true;
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))
552 return false;
555 *realtime_ptr = realtime;
556 return true;
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", "", "");