1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
3 Copyright (C) 2007,2008 Nedko Arnaudov
4 Copyright (C) 2007-2008 Juuso Alasuutari
5 Copyright (C) 2008 Marc-Olivier Barre
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #if defined(HAVE_CONFIG_H)
33 #include <dbus/dbus.h>
40 #include "controller.h"
41 #include "jack/jack.h"
42 #include "jack/jslist.h"
43 #include "jack/control.h"
45 #include "svnversion.h"
48 char *g_jackdbus_config_dir
;
49 size_t g_jackdbus_config_dir_len
; /* without terminating '\0' char */
50 char *g_jackdbus_log_dir
;
51 size_t g_jackdbus_log_dir_len
; /* without terminating '\0' char */
53 DBusConnection
*g_connection
;
56 jack_dbus_send_signal(
57 const char *sender_object_path
,
59 const char *signal_name
,
63 DBusMessage
*message_ptr
;
66 va_start(ap
, first_arg_type
);
68 message_ptr
= dbus_message_new_signal(sender_object_path
, iface
, signal_name
);
69 if (message_ptr
== NULL
)
71 jack_error("dbus_message_new_signal() failed.");
75 if (!dbus_message_append_args_valist(message_ptr
, first_arg_type
, ap
))
77 jack_error("dbus_message_append_args_valist() failed.");
81 /* Add message to outgoing message queue */
82 if (!dbus_connection_send(g_connection
, message_ptr
, NULL
))
84 jack_error("dbus_connection_send() failed.");
89 dbus_message_unref(message_ptr
);
96 * Send a method return.
98 * If call->reply is NULL (i.e. a message construct method failed
99 * due to lack of memory) attempt to send a void method return.
103 jack_dbus_send_method_return(
104 struct jack_dbus_method_call
* call
)
109 if (!dbus_connection_send (call
->connection
, call
->reply
, NULL
))
111 jack_error ("Ran out of memory trying to queue method return");
114 dbus_connection_flush (call
->connection
);
115 dbus_message_unref (call
->reply
);
120 jack_error ("send_method_return() called with a NULL message,"
121 " trying to construct a void return...");
123 if ((call
->reply
= dbus_message_new_method_return (call
->message
)))
129 jack_error ("Failed to construct method return!");
134 #define object_ptr ((struct jack_dbus_object_descriptor *)data)
137 * The D-Bus message handler for object path /org/jackaudio/Controller.
140 jack_dbus_message_handler(
141 DBusConnection
*connection
,
142 DBusMessage
*message
,
145 struct jack_dbus_method_call call
;
146 const char *interface_name
;
147 struct jack_dbus_interface_descriptor
** interface_ptr_ptr
;
149 /* Check if the message is a method call. If not, ignore it. */
150 if (dbus_message_get_type (message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
155 /* Get the invoked method's name and make sure it's non-NULL. */
156 if (!(call
.method_name
= dbus_message_get_member (message
)))
160 JACK_DBUS_ERROR_UNKNOWN_METHOD
,
161 "Received method call with empty method name");
165 /* Initialize our data. */
166 call
.context
= object_ptr
->context
;
167 call
.connection
= connection
;
168 call
.message
= message
;
171 /* Check if there's an interface specified for this method call. */
172 interface_name
= dbus_message_get_interface (message
);
173 if (interface_name
!= NULL
)
175 /* Check if we can match the interface and method.
176 * The inteface handler functions only return false if the
177 * method name was unknown, otherwise they run the specified
178 * method and return TRUE.
181 interface_ptr_ptr
= object_ptr
->interfaces
;
183 while (*interface_ptr_ptr
!= NULL
)
185 if (strcmp(interface_name
, (*interface_ptr_ptr
)->name
) == 0)
187 if (!(*interface_ptr_ptr
)->handler(&call
, (*interface_ptr_ptr
)->methods
))
200 /* No interface was specified so we have to try them all. This is
201 * dictated by the D-Bus specification which states that method calls
202 * omitting the interface must never be rejected.
205 interface_ptr_ptr
= object_ptr
->interfaces
;
207 while (*interface_ptr_ptr
!= NULL
)
209 if ((*interface_ptr_ptr
)->handler(&call
, (*interface_ptr_ptr
)->methods
))
220 JACK_DBUS_ERROR_UNKNOWN_METHOD
,
221 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist",
223 dbus_message_get_signature(message
),
227 jack_dbus_send_method_return(&call
);
230 return DBUS_HANDLER_RESULT_HANDLED
;
234 jack_dbus_message_handler_unregister(
235 DBusConnection
*connection
,
238 jack_info ("Message handler was unregistered");
244 * Check if the supplied method name exists in org.jackaudio.JackConfigure,
245 * if it does execute it and return TRUE. Otherwise return FALSE.
248 jack_dbus_run_method(
249 struct jack_dbus_method_call
*call
,
250 const struct jack_dbus_interface_method_descriptor
* methods
)
252 const struct jack_dbus_interface_method_descriptor
* method_ptr
;
254 method_ptr
= methods
;
256 while (method_ptr
->name
!= NULL
)
258 if (strcmp(call
->method_name
, method_ptr
->name
) == 0)
260 method_ptr
->handler(call
);
271 * Read arguments from a method call.
272 * If the operation fails construct an error and return false,
273 * otherwise return true.
276 jack_dbus_get_method_args(
277 struct jack_dbus_method_call
*call
,
285 va_start (args
, type
);
286 dbus_error_init (&error
);
288 if (!dbus_message_get_args_valist (call
->message
, &error
, type
, args
))
290 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
291 "Invalid arguments to method \"%s\"",
296 dbus_error_free (&error
);
303 * Read a string and a variant argument from a method call.
304 * If the operation fails construct an error and return false,
305 * otherwise return true.
308 jack_dbus_get_method_args_string_and_variant(
309 struct jack_dbus_method_call
*call
,
314 DBusMessageIter iter
, sub_iter
;
316 /* First we want a string... */
317 if (dbus_message_iter_init (call
->message
, &iter
)
318 && dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_STRING
)
320 dbus_message_iter_get_basic (&iter
, arg1
);
321 dbus_message_iter_next (&iter
);
323 /* ...and then a variant. */
324 if (dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_VARIANT
)
326 dbus_message_iter_recurse (&iter
, &sub_iter
);
327 dbus_message_iter_get_basic (&sub_iter
, arg2
);
328 *type_ptr
= dbus_message_iter_get_arg_type (&sub_iter
);
330 /* Got what we wanted. */
335 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
336 "Invalid arguments to method \"%s\"",
343 * Read two strings and a variant argument from a method call.
344 * If the operation fails construct an error and return false,
345 * otherwise return true.
348 jack_dbus_get_method_args_two_strings_and_variant(
349 struct jack_dbus_method_call
*call
,
355 DBusMessageIter iter
, sub_iter
;
357 /* First we want a string... */
358 if (dbus_message_iter_init (call
->message
, &iter
)
359 && dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_STRING
)
361 dbus_message_iter_get_basic (&iter
, arg1
);
362 dbus_message_iter_next (&iter
);
364 /* ...and then a second string. */
365 if (dbus_message_iter_get_arg_type (&iter
) != DBUS_TYPE_STRING
)
370 /* Got what we wanted. */
371 dbus_message_iter_get_basic (&iter
, arg2
);
372 dbus_message_iter_next (&iter
);
374 /* ...and then a variant. */
375 if (dbus_message_iter_get_arg_type (&iter
) == DBUS_TYPE_VARIANT
)
377 dbus_message_iter_recurse (&iter
, &sub_iter
);
378 dbus_message_iter_get_basic (&sub_iter
, arg3
);
379 *type_ptr
= dbus_message_iter_get_arg_type (&sub_iter
);
381 /* Got what we wanted. */
386 jack_dbus_error (call
, JACK_DBUS_ERROR_INVALID_ARGS
,
387 "Invalid arguments to method \"%s\"",
394 * Append a variant type to a D-Bus message.
395 * Return false if something fails, true otherwise.
398 jack_dbus_message_append_variant(
399 DBusMessageIter
*iter
,
401 const char *signature
,
404 DBusMessageIter sub_iter
;
406 /* Open a variant container. */
407 if (!dbus_message_iter_open_container (iter
, DBUS_TYPE_VARIANT
, signature
, &sub_iter
))
412 /* Append the supplied value. */
413 if (!dbus_message_iter_append_basic (&sub_iter
, type
, (const void *) arg
))
415 dbus_message_iter_close_container (iter
, &sub_iter
);
419 /* Close the container. */
420 if (!dbus_message_iter_close_container (iter
, &sub_iter
))
432 * Construct an empty method return message.
434 * The operation can only fail due to lack of memory, in which case
435 * there's no sense in trying to construct an error return. Instead,
436 * call->reply will be set to NULL and handled in send_method_return().
439 jack_dbus_construct_method_return_empty(
440 struct jack_dbus_method_call
* call
)
442 call
->reply
= dbus_message_new_method_return (call
->message
);
444 if (call
->reply
== NULL
)
446 jack_error ("Ran out of memory trying to construct method return");
451 * Construct a method return which holds a single argument or, if
452 * the type parameter is DBUS_TYPE_INVALID, no arguments at all
455 * The operation can only fail due to lack of memory, in which case
456 * there's no sense in trying to construct an error return. Instead,
457 * call->reply will be set to NULL and handled in send_method_return().
460 jack_dbus_construct_method_return_single(
461 struct jack_dbus_method_call
*call
,
465 DBusMessageIter iter
;
466 call
->reply
= dbus_message_new_method_return (call
->message
);
468 if (call
->reply
== NULL
)
473 /* Void method return requested by caller. */
474 if (type
== DBUS_TYPE_INVALID
)
479 /* Prevent crash on NULL input string. */
480 else if (type
== DBUS_TYPE_STRING
&& arg
.string
== NULL
)
485 dbus_message_iter_init_append (call
->reply
, &iter
);
487 if (!dbus_message_iter_append_basic (&iter
, type
, (const void *) &arg
))
489 dbus_message_unref (call
->reply
);
497 jack_error ("Ran out of memory trying to construct method return");
501 * Construct a method return which holds an array of strings.
503 * The operation can only fail due to lack of memory, in which case
504 * there's no sense in trying to construct an error return. Instead,
505 * call->reply will be set to NULL and handled in send_method_return().
508 jack_dbus_construct_method_return_array_of_strings(
509 struct jack_dbus_method_call
*call
,
510 unsigned int num_members
,
513 DBusMessageIter iter
, sub_iter
;
516 call
->reply
= dbus_message_new_method_return (call
->message
);
522 dbus_message_iter_init_append (call
->reply
, &iter
);
524 if (!dbus_message_iter_open_container (&iter
, DBUS_TYPE_ARRAY
, "s", &sub_iter
))
529 for (i
= 0; i
< num_members
; ++i
)
531 if (!dbus_message_iter_append_basic (&sub_iter
, DBUS_TYPE_STRING
, (const void *) &array
[i
]))
533 dbus_message_iter_close_container (&iter
, &sub_iter
);
538 if (!dbus_message_iter_close_container (&iter
, &sub_iter
))
546 dbus_message_unref (call
->reply
);
550 jack_error ("Ran out of memory trying to construct method return");
554 jack_dbus_info_callback(const char *msg
)
557 char timestamp_str
[26];
560 ctime_r(×tamp
, timestamp_str
);
561 timestamp_str
[24] = 0;
563 fprintf(g_logfile
, "%s: %s\n", timestamp_str
, msg
);
567 #define ANSI_BOLD_ON "\033[1m"
568 #define ANSI_BOLD_OFF "\033[22m"
569 #define ANSI_COLOR_RED "\033[31m"
570 #define ANSI_RESET "\033[0m"
573 jack_dbus_error_callback(const char *msg
)
576 char timestamp_str
[26];
579 ctime_r(×tamp
, timestamp_str
);
580 timestamp_str
[24] = 0;
582 fprintf(g_logfile
, "%s: " ANSI_BOLD_ON ANSI_COLOR_RED
"ERROR: %s" ANSI_RESET
"\n", timestamp_str
, msg
);
587 ensure_dir_exist(const char *dirname
, int mode
)
590 if (stat(dirname
, &st
) != 0)
594 printf("Directory \"%s\" does not exist. Creating...\n", dirname
);
595 if (mkdir(dirname
, mode
) != 0)
597 fprintf(stderr
, "Failed to create \"%s\" directory: %d (%s)\n", dirname
, errno
, strerror(errno
));
603 fprintf(stderr
, "Failed to stat \"%s\": %d (%s)\n", dirname
, errno
, strerror(errno
));
609 if (!S_ISDIR(st
.st_mode
))
611 fprintf(stderr
, "\"%s\" exists but is not directory.\n", dirname
);
619 pathname_cat(const char *pathname_a
, const char *pathname_b
)
622 int pathname_a_len
, pathname_b_len
, pathname_len
;
623 pathname_a_len
= strlen(pathname_a
);
624 pathname_b_len
= strlen(pathname_b
);
625 pathname
= malloc(pathname_a_len
+ pathname_b_len
+ 1);
626 if (pathname
== NULL
)
628 fprintf(stderr
, "Out of memory\n");
631 memcpy(pathname
, pathname_a
, pathname_a_len
);
632 memcpy(pathname
+ pathname_a_len
, pathname_b
, pathname_b_len
);
633 pathname_len
= pathname_a_len
+ pathname_b_len
;
634 pathname
[pathname_len
] = 0;
641 const char *home_dir
, *xdg_config_home
, *xdg_log_home
;
643 home_dir
= getenv("HOME");
644 if (home_dir
== NULL
)
646 fprintf(stderr
, "Environment variable HOME not set\n");
650 xdg_config_home
= getenv("XDG_CONFIG_HOME");
651 if (xdg_config_home
== NULL
)
653 if (!(xdg_config_home
= pathname_cat(home_dir
, DEFAULT_XDG_CONFIG
))) goto fail
;
656 if (!(xdg_log_home
= pathname_cat(home_dir
, DEFAULT_XDG_LOG
))) goto fail
;
658 if (!(g_jackdbus_config_dir
= pathname_cat(xdg_config_home
, JACKDBUS_DIR
))) goto fail
;
659 if (!(g_jackdbus_log_dir
= pathname_cat(xdg_log_home
, JACKDBUS_DIR
))) goto fail
;
661 if (!ensure_dir_exist(xdg_config_home
, 0700))
666 if (!ensure_dir_exist(xdg_log_home
, 0700))
671 if (!ensure_dir_exist(g_jackdbus_config_dir
, 0700))
673 free(g_jackdbus_config_dir
);
676 g_jackdbus_config_dir_len
= strlen(g_jackdbus_config_dir
);
678 if (!ensure_dir_exist(g_jackdbus_log_dir
, 0700))
680 free(g_jackdbus_log_dir
);
683 g_jackdbus_log_dir_len
= strlen(g_jackdbus_log_dir
);
694 free(g_jackdbus_config_dir
);
695 free(g_jackdbus_log_dir
);
704 log_len
= strlen(JACKDBUS_LOG
);
706 log_filename
= malloc(g_jackdbus_log_dir_len
+ log_len
+ 1);
707 if (log_filename
== NULL
)
709 fprintf(stderr
, "Out of memory\n");
713 memcpy(log_filename
, g_jackdbus_log_dir
, g_jackdbus_log_dir_len
);
714 memcpy(log_filename
+ g_jackdbus_log_dir_len
, JACKDBUS_LOG
, log_len
);
715 log_filename
[g_jackdbus_log_dir_len
+ log_len
] = 0;
717 g_logfile
= fopen(log_filename
, "a");
718 if (g_logfile
== NULL
)
720 fprintf(stderr
, "Cannot open jackdbus log file \"%s\": %d (%s)\n", log_filename
, errno
, strerror(errno
));
738 void *dbus_call_context_ptr
,
739 const char *error_name
,
746 va_start(ap
, format
);
748 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
750 jack_error_callback(buffer
);
751 if (dbus_call_context_ptr
!= NULL
)
753 if (((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
!= NULL
)
755 dbus_message_unref(((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
);
756 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= NULL
;
759 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->reply
= dbus_message_new_error(
760 ((struct jack_dbus_method_call
*)dbus_call_context_ptr
)->message
,
769 main (int argc
, char **argv
)
773 void *controller_ptr
;
775 char timestamp_str
[26];
779 ctime_r(&st
.st_mtime
, timestamp_str
);
780 timestamp_str
[24] = 0;
782 if (!jack_controller_settings_init())
788 if (argc
!= 2 || strcmp(argv
[1], "auto") != 0)
793 "jackdbus should be auto-executed by D-Bus message bus daemon.\n"
794 "If you want to run it manually anyway, specify \"auto\" as only parameter\n");
795 goto fail_uninit_xml
;
801 goto fail_uninit_xml
;
807 goto fail_uninit_paths
;
810 #if !defined(DISABLE_SIGNAL_MAGIC)
811 jackctl_setup_signals(0);
814 jack_set_error_function(jack_dbus_error_callback
);
815 jack_set_info_function(jack_dbus_info_callback
);
817 /* setup our SIGSEGV magic that prints nice stack in our logfile */
820 jack_info("------------------");
821 jack_info("Controller activated. Version %s (%s) built on %s", jack_get_version_string(), SVN_VERSION
, timestamp_str
);
823 if (!dbus_threads_init_default())
825 jack_error("dbus_threads_init_default() failed");
827 goto fail_uninit_log
;
830 dbus_error_init (&error
);
831 g_connection
= dbus_bus_get (DBUS_BUS_SESSION
, &error
);
832 if (dbus_error_is_set (&error
))
834 jack_error("Cannot connect to D-Bus session bus: %s", error
.message
);
836 goto fail_uninit_log
;
839 ret
= dbus_bus_request_name(
841 "org.jackaudio.service",
842 DBUS_NAME_FLAG_DO_NOT_QUEUE
,
846 jack_error("Cannot request service name: %s", error
.message
);
847 dbus_error_free(&error
);
849 goto fail_unref_connection
;
851 else if (ret
== DBUS_REQUEST_NAME_REPLY_EXISTS
)
853 jack_error("Requested D-Bus service name already exists");
855 goto fail_unref_connection
;
858 controller_ptr
= jack_controller_create(g_connection
);
860 if (controller_ptr
== NULL
)
863 goto fail_unref_connection
;
866 jack_info("Listening for D-Bus messages");
868 g_exit_command
= FALSE
;
869 while (!g_exit_command
&& dbus_connection_read_write_dispatch (g_connection
, 200));
871 jack_controller_destroy(controller_ptr
);
873 jack_info("Controller deactivated.");
877 fail_unref_connection
:
878 dbus_connection_unref(g_connection
);
887 jack_controller_settings_uninit();