Properly handle stop of apps with jack clients without ports. Fix for #94
[ladish.git] / dbus / method.c
blob2fdb5881356fdd64d8d9f62043585117703ff03b
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains D-Bus methods helpers
10 **************************************************************************
12 * Licensed under the Academic Free License version 2.1
14 * LADI Session Handler is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * LADI Session Handler is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
26 * or write to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "../common.h"
31 #include "helpers.h"
32 #include "method.h"
35 * Construct a void method return.
37 * The operation can only fail due to lack of memory, in which case
38 * there's no sense in trying to construct an error return. Instead,
39 * call_ptr->reply will be set to NULL and handled in send_method_return().
41 void
42 method_return_new_void(struct dbus_method_call * call_ptr)
44 if (!(call_ptr->reply = dbus_message_new_method_return(call_ptr->message))) {
45 log_error("Ran out of memory trying to construct method return");
50 * Construct a method return which holds a single argument or, if
51 * the type parameter is DBUS_TYPE_INVALID, no arguments at all
52 * (a void message).
54 * The operation can only fail due to lack of memory, in which case
55 * there's no sense in trying to construct an error return. Instead,
56 * call_ptr->reply will be set to NULL and handled in send_method_return().
58 void
59 method_return_new_single(struct dbus_method_call * call_ptr,
60 int type,
61 const void *arg)
63 if (!call_ptr || !arg) {
64 log_error("Invalid arguments");
65 return;
68 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
70 if (!call_ptr->reply)
71 goto fail_no_mem;
73 /* Prevent crash on NULL input string. */
74 if (type == DBUS_TYPE_STRING && !(*((const char **) arg)))
75 *((const char **) arg) = "";
77 DBusMessageIter iter;
79 dbus_message_iter_init_append(call_ptr->reply, &iter);
81 if (dbus_message_iter_append_basic(&iter, type, arg))
82 return;
84 dbus_message_unref(call_ptr->reply);
85 call_ptr->reply = NULL;
87 fail_no_mem:
88 log_error("Ran out of memory trying to construct method return");
91 void
92 method_return_new_valist(struct dbus_method_call * call_ptr,
93 int type,
94 ...)
96 if (!call_ptr) {
97 log_error("Call pointer is NULL");
98 return;
101 if (type == DBUS_TYPE_INVALID) {
102 log_error("No argument(s) supplied");
103 return;
106 va_list argp;
108 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
109 if (!call_ptr->reply)
110 goto fail_no_mem;
112 va_start(argp, type);
114 if (dbus_message_append_args_valist(call_ptr->reply, type, argp)) {
115 va_end(argp);
116 return;
119 va_end(argp);
121 dbus_message_unref(call_ptr->reply);
122 call_ptr->reply = NULL;
124 fail_no_mem:
125 log_error("Ran out of memory trying to construct method return");
129 * Send a method return.
131 * If call_ptr->reply is NULL, i.e. a previous attempt to construct
132 * a return has failed, attempt to send a void return.
134 void
135 method_return_send(struct dbus_method_call * call_ptr)
137 if (call_ptr->reply) {
138 retry_send:
139 if (!dbus_connection_send(call_ptr->connection, call_ptr->reply, NULL))
140 log_error("Ran out of memory trying to queue "
141 "method return");
142 else
143 dbus_connection_flush(call_ptr->connection);
145 dbus_message_unref(call_ptr->reply);
146 call_ptr->reply = NULL;
147 } else {
148 log_debug("Message was NULL, trying to construct a void return");
150 if ((call_ptr->reply = dbus_message_new_method_return(call_ptr->message))) {
151 log_debug("Constructed a void return, trying to queue it");
152 goto retry_send;
153 } else {
154 log_error("Failed to construct method return!");
159 bool
160 method_return_verify(DBusMessage *msg,
161 const char **str)
163 if (!msg || dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR)
164 return true;
166 const char *ptr;
168 if (!dbus_message_get_args(msg, &g_dbus_error,
169 DBUS_TYPE_STRING, &ptr,
170 DBUS_TYPE_INVALID)) {
171 log_error("Cannot read description from D-Bus error message: %s ",
172 g_dbus_error.message);
173 dbus_error_free(&g_dbus_error);
174 ptr = NULL;
177 if (str)
178 *str = ptr;
180 return false;