1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
3 Copyright (C) 2007,2008,2010 Nedko Arnaudov
4 Copyright (C) 2007-2008 Juuso Alasuutari
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #if defined(HAVE_CONFIG_H)
29 #include <dbus/dbus.h>
32 #include "controller_internal.h"
35 #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackControl"
38 jack_controller_control_send_signal_server_started(void)
41 jack_dbus_send_signal(
42 JACK_CONTROLLER_OBJECT_PATH
,
49 jack_controller_control_send_signal_server_stopped(void)
52 jack_dbus_send_signal(
53 JACK_CONTROLLER_OBJECT_PATH
,
59 #define controller_ptr ((struct jack_controller *)call->context)
62 * Check if the supplied method name exists in org.jackaudio.JackControl,
63 * if it does execute it and return true. Otherwise return false.
67 jack_control_run_method(
68 struct jack_dbus_method_call
* call
,
69 const struct jack_dbus_interface_method_descriptor
* methods
)
75 /* use empty reply if not overridden in the code that follows */
76 type
= DBUS_TYPE_INVALID
;
78 if (strcmp (call
->method_name
, "Exit") == 0)
80 g_exit_command
= TRUE
;
82 else if (strcmp (call
->method_name
, "IsStarted") == 0)
84 type
= DBUS_TYPE_BOOLEAN
;
85 arg
.boolean
= (dbus_bool_t
) (controller_ptr
->started
? TRUE
: FALSE
);
87 else if (strcmp (call
->method_name
, "StartServer") == 0)
89 if (controller_ptr
->started
)
91 jack_info("Ignoring JACK server start request because server is already started.");
95 if (!jack_controller_start_server(controller_ptr
, call
))
97 /* the reply is set by the failed function */
98 assert(call
->reply
!= NULL
);
102 jack_controller_control_send_signal_server_started();
105 else if (strcmp (call
->method_name
, "StopServer") == 0)
107 if (!controller_ptr
->started
)
109 jack_info("Ignoring JACK server stop request because server is already stopped.");
113 if (!jack_controller_stop_server(controller_ptr
, call
))
115 /* the reply is set by the failed function */
116 assert(call
->reply
!= NULL
);
120 jack_controller_control_send_signal_server_stopped();
123 else if (strcmp (call
->method_name
, "SwitchMaster") == 0)
125 if (!controller_ptr
->started
)
131 if (!jack_controller_switch_master(controller_ptr
, call
))
133 /* the reply is set by the failed function */
134 assert(call
->reply
!= NULL
);
139 else if (strcmp (call
->method_name
, "GetLoad") == 0)
141 if (!controller_ptr
->started
)
146 type
= DBUS_TYPE_DOUBLE
;
147 arg
.doubl
= jack_cpu_load(controller_ptr
->client
);
149 else if (strcmp (call
->method_name
, "GetXruns") == 0)
151 type
= DBUS_TYPE_UINT32
;
152 arg
.uint32
= controller_ptr
->xruns
;
154 else if (strcmp (call
->method_name
, "GetSampleRate") == 0)
156 if (!controller_ptr
->started
)
161 type
= DBUS_TYPE_UINT32
;
162 arg
.uint32
= jack_get_sample_rate(controller_ptr
->client
);
164 else if (strcmp (call
->method_name
, "GetLatency") == 0)
166 if (!controller_ptr
->started
)
171 type
= DBUS_TYPE_DOUBLE
;
172 arg
.doubl
= ((float)jack_get_buffer_size(controller_ptr
->client
) / (float)jack_get_sample_rate(controller_ptr
->client
)) * 1000.0f
;
174 else if (strcmp (call
->method_name
, "GetBufferSize") == 0)
176 if (!controller_ptr
->started
)
181 type
= DBUS_TYPE_UINT32
;
182 arg
.uint32
= jack_get_buffer_size(controller_ptr
->client
);
184 else if (strcmp (call
->method_name
, "SetBufferSize") == 0)
186 dbus_uint32_t buffer_size
;
188 if (!controller_ptr
->started
)
193 if (!jack_dbus_get_method_args(call
, DBUS_TYPE_UINT32
, &buffer_size
, DBUS_TYPE_INVALID
))
195 /* jack_dbus_get_method_args() has set reply for us */
199 ret
= jack_set_buffer_size(controller_ptr
->client
, buffer_size
);
204 JACK_DBUS_ERROR_GENERIC
,
205 "jack_set_buffer_size(%u) failed with error %d", (unsigned int)buffer_size
, ret
);
210 else if (strcmp (call
->method_name
, "IsRealtime") == 0)
212 type
= DBUS_TYPE_BOOLEAN
;
213 arg
.boolean
= jack_is_realtime(controller_ptr
->client
) ? TRUE
: FALSE
;
215 else if (strcmp (call
->method_name
, "ResetXruns") == 0)
217 controller_ptr
->xruns
= 0;
219 else if (strcmp (call
->method_name
, "LoadInternal") == 0)
221 const char *internal_name
;
223 if (!jack_dbus_get_method_args(call
, DBUS_TYPE_STRING
, &internal_name
, DBUS_TYPE_INVALID
))
225 /* The method call had invalid arguments meaning that
226 * get_method_args() has constructed an error for us.
231 if (!jack_controller_load_internal(controller_ptr
, internal_name
)) {
234 JACK_DBUS_ERROR_GENERIC
,
235 "jack_controller_load_internal failed for internal (%s)", internal_name
);
238 else if (strcmp (call
->method_name
, "AddSlaveDriver") == 0)
240 const char *driver_name
;
242 if (controller_ptr
->started
)
247 if (!jack_dbus_get_method_args(call
, DBUS_TYPE_STRING
, &driver_name
, DBUS_TYPE_INVALID
))
249 /* The method call had invalid arguments meaning that
250 * get_method_args() has constructed an error for us.
255 if (!jack_controller_add_slave_driver(controller_ptr
, driver_name
))
259 JACK_DBUS_ERROR_GENERIC
,
260 "jack_controller_add_slave_driver failed for driver (%s)", driver_name
);
264 jack_controller_pending_save(controller_ptr
);
267 else if (strcmp (call
->method_name
, "RemoveSlaveDriver") == 0)
269 const char *driver_name
;
271 if (controller_ptr
->started
)
276 if (!jack_dbus_get_method_args(call
, DBUS_TYPE_STRING
, &driver_name
, DBUS_TYPE_INVALID
))
278 /* The method call had invalid arguments meaning that
279 * get_method_args() has constructed an error for us.
284 if (!jack_controller_remove_slave_driver(controller_ptr
, driver_name
))
288 JACK_DBUS_ERROR_GENERIC
,
289 "jack_controller_remove_slave_driver failed for driver (%s)", driver_name
);
293 jack_controller_pending_save(controller_ptr
);
296 else if (strcmp (call
->method_name
, "UnloadInternal") == 0)
298 const char *internal_name
;
300 if (!jack_dbus_get_method_args(call
, DBUS_TYPE_STRING
, &internal_name
, DBUS_TYPE_INVALID
))
302 /* The method call had invalid arguments meaning that
303 * get_method_args() has constructed an error for us.
308 if (!jack_controller_unload_internal(controller_ptr
, internal_name
)) {
311 JACK_DBUS_ERROR_GENERIC
,
312 "jack_controller_unload_internal failed for internal (%s)", internal_name
);
321 jack_dbus_construct_method_return_single(call
, type
, arg
);
325 jack_dbus_only_error(
327 JACK_DBUS_ERROR_SERVER_NOT_RUNNING
,
328 "Can't execute method '%s' with stopped JACK server",
333 jack_dbus_only_error(
335 JACK_DBUS_ERROR_SERVER_RUNNING
,
336 "Can't execute method '%s' with started JACK server",
344 #undef controller_ptr
346 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(IsStarted
)
347 JACK_DBUS_METHOD_ARGUMENT("started", "b", true)
348 JACK_DBUS_METHOD_ARGUMENTS_END
350 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(StartServer
)
351 JACK_DBUS_METHOD_ARGUMENTS_END
353 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(StopServer
)
354 JACK_DBUS_METHOD_ARGUMENTS_END
356 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(SwitchMaster
)
357 JACK_DBUS_METHOD_ARGUMENTS_END
359 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetLoad
)
360 JACK_DBUS_METHOD_ARGUMENT("load", "d", true)
361 JACK_DBUS_METHOD_ARGUMENTS_END
363 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetXruns
)
364 JACK_DBUS_METHOD_ARGUMENT("xruns_count", "u", true)
365 JACK_DBUS_METHOD_ARGUMENTS_END
367 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetSampleRate
)
368 JACK_DBUS_METHOD_ARGUMENT("sample_rate", "u", true)
369 JACK_DBUS_METHOD_ARGUMENTS_END
371 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetLatency
)
372 JACK_DBUS_METHOD_ARGUMENT("latency_ms", "d", true)
373 JACK_DBUS_METHOD_ARGUMENTS_END
375 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetBufferSize
)
376 JACK_DBUS_METHOD_ARGUMENT("buffer_size_frames", "u", true)
377 JACK_DBUS_METHOD_ARGUMENTS_END
379 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(SetBufferSize
)
380 JACK_DBUS_METHOD_ARGUMENT("buffer_size_frames", "u", false)
381 JACK_DBUS_METHOD_ARGUMENTS_END
383 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(IsRealtime
)
384 JACK_DBUS_METHOD_ARGUMENT("realtime", "b", true)
385 JACK_DBUS_METHOD_ARGUMENTS_END
387 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ResetXruns
)
388 JACK_DBUS_METHOD_ARGUMENTS_END
390 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(LoadInternal
)
391 JACK_DBUS_METHOD_ARGUMENT("internal", "s", false)
392 JACK_DBUS_METHOD_ARGUMENTS_END
394 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(UnloadInternal
)
395 JACK_DBUS_METHOD_ARGUMENT("internal", "s", false)
396 JACK_DBUS_METHOD_ARGUMENTS_END
398 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(AddSlaveDriver
)
399 JACK_DBUS_METHOD_ARGUMENT("driver_name", "s", false)
400 JACK_DBUS_METHOD_ARGUMENTS_END
402 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(RemoveSlaveDriver
)
403 JACK_DBUS_METHOD_ARGUMENT("driver_name", "s", false)
404 JACK_DBUS_METHOD_ARGUMENTS_END
406 JACK_DBUS_METHODS_BEGIN
407 JACK_DBUS_METHOD_DESCRIBE(IsStarted
, NULL
)
408 JACK_DBUS_METHOD_DESCRIBE(StartServer
, NULL
)
409 JACK_DBUS_METHOD_DESCRIBE(StopServer
, NULL
)
410 JACK_DBUS_METHOD_DESCRIBE(SwitchMaster
, NULL
)
411 JACK_DBUS_METHOD_DESCRIBE(GetLoad
, NULL
)
412 JACK_DBUS_METHOD_DESCRIBE(GetXruns
, NULL
)
413 JACK_DBUS_METHOD_DESCRIBE(GetSampleRate
, NULL
)
414 JACK_DBUS_METHOD_DESCRIBE(GetLatency
, NULL
)
415 JACK_DBUS_METHOD_DESCRIBE(GetBufferSize
, NULL
)
416 JACK_DBUS_METHOD_DESCRIBE(SetBufferSize
, NULL
)
417 JACK_DBUS_METHOD_DESCRIBE(IsRealtime
, NULL
)
418 JACK_DBUS_METHOD_DESCRIBE(ResetXruns
, NULL
)
419 JACK_DBUS_METHOD_DESCRIBE(LoadInternal
, NULL
)
420 JACK_DBUS_METHOD_DESCRIBE(UnloadInternal
, NULL
)
421 JACK_DBUS_METHOD_DESCRIBE(AddSlaveDriver
, NULL
)
422 JACK_DBUS_METHOD_DESCRIBE(RemoveSlaveDriver
, NULL
)
423 JACK_DBUS_METHODS_END
425 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ServerStarted
)
426 JACK_DBUS_SIGNAL_ARGUMENTS_END
428 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ServerStopped
)
429 JACK_DBUS_SIGNAL_ARGUMENTS_END
431 JACK_DBUS_SIGNALS_BEGIN
432 JACK_DBUS_SIGNAL_DESCRIBE(ServerStarted
)
433 JACK_DBUS_SIGNAL_DESCRIBE(ServerStopped
)
434 JACK_DBUS_SIGNALS_END
436 JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_control
, JACK_DBUS_IFACE_NAME
)
437 JACK_DBUS_IFACE_HANDLER(jack_control_run_method
)
438 JACK_DBUS_IFACE_EXPOSE_METHODS
439 JACK_DBUS_IFACE_EXPOSE_SIGNALS