gui: remove unused and duplicate dbus helper code
[ladish.git] / dbus / object_path.c
blob36c5b618f0780e38d00f01a36184ef21b7e9b115
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains D-Bus object path 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 "../common/safety.h"
33 #include "error.h" /* lash_dbus_error() */
35 struct dbus_object_path_interface
37 const struct dbus_interface_descriptor * iface;
38 void * iface_context;
41 struct dbus_object_path
43 char * name;
44 DBusMessage * introspection;
45 struct dbus_object_path_interface * ifaces;
48 #define write_buf(args...) buf_ptr += sprintf(buf_ptr, ## args)
50 DBusMessage *
51 introspection_new(struct dbus_object_path * opath_ptr)
53 char *xml_data, *buf_ptr;
54 const struct dbus_object_path_interface * iface_ptr;
55 const struct dbus_method_descriptor * method_ptr;
56 const struct dbus_method_arg_descriptor * method_arg_ptr;
57 const struct dbus_signal_descriptor * signal_ptr;
58 const struct dbus_signal_arg_descriptor * signal_arg_ptr;
59 DBusMessage * msg;
60 DBusMessageIter iter;
62 log_debug("Creating introspection message");
65 * Create introspection XML data.
68 xml_data = lash_malloc(1, 16384);
69 buf_ptr = xml_data;
71 write_buf("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
72 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
73 "<node name=\"%s\">\n", opath_ptr->name);
75 /* Add the object path's interfaces. */
76 for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++)
78 write_buf(" <interface name=\"%s\">\n", iface_ptr->iface->name);
79 if (iface_ptr->iface->methods != NULL)
81 /* Add the interface's methods. */
82 for (method_ptr = iface_ptr->iface->methods; method_ptr->name != NULL; method_ptr++)
84 write_buf(" <method name=\"%s\">\n", method_ptr->name);
85 /* Add the method's arguments. */
86 for (method_arg_ptr = method_ptr->args; method_arg_ptr->name != NULL; method_arg_ptr++)
88 write_buf(
89 " <arg name=\"%s\" type=\"%s\" direction=\"%s\" />\n",
90 method_arg_ptr->name,
91 method_arg_ptr->type,
92 method_arg_ptr->direction_in ? "in" : "out");
94 write_buf(" </method>\n");
97 if (iface_ptr->iface->signals != NULL)
99 /* Add the interface's signals. */
100 for (signal_ptr = iface_ptr->iface->signals; signal_ptr->name != NULL; signal_ptr++)
102 write_buf(" <signal name=\"%s\">\n", signal_ptr->name);
103 /* Add the signal's arguments. */
104 for (signal_arg_ptr = signal_ptr->args; signal_arg_ptr->name != NULL; signal_arg_ptr++)
106 write_buf(" <arg name=\"%s\" type=\"%s\" />\n", signal_arg_ptr->name, signal_arg_ptr->type);
108 write_buf(" </signal>\n");
111 write_buf(" </interface>\n");
113 write_buf("</node>");
116 * Create a D-Bus message from the XML data.
119 if ((msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN)))
121 dbus_message_iter_init_append(msg, &iter);
122 if (dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, (const void *) &xml_data))
124 dbus_message_set_no_reply(msg, TRUE);
126 else
128 dbus_message_unref(msg);
129 msg = NULL;
130 log_error("Failed to append data to introspection message");
133 else
135 log_error("Failed to create introspection message");
138 free(xml_data);
139 return msg;
142 #undef write_buf
144 void
145 introspection_destroy(struct dbus_object_path *path)
147 log_debug("Destroying introspection message");
149 if (path && path->introspection) {
150 dbus_message_unref(path->introspection);
151 path->introspection = NULL;
153 #ifdef LADISH_DEBUG
154 else
155 log_debug("Nothing to destroy");
156 #endif
159 static bool introspection_handler(const struct dbus_interface_descriptor * interface, struct dbus_method_call * call_ptr)
161 if (strcmp(call_ptr->method_name, "Introspect") != 0)
163 /* The requested method wasn't "Introspect". */
164 return false;
167 /* Try to construct the instrospection message */
168 call_ptr->reply = dbus_message_copy(call_ptr->iface_context); /* context contains the reply message */
169 if (call_ptr->reply == NULL)
171 log_error("Ran out of memory trying to copy introspection message");
172 goto fail;
175 if (!dbus_message_set_destination(call_ptr->reply, dbus_message_get_sender(call_ptr->message)))
177 log_error("dbus_message_set_destination() failed.");
178 goto unref_reply;
181 if (!dbus_message_set_reply_serial(call_ptr->reply, dbus_message_get_serial(call_ptr->message)))
183 log_error("dbus_message_set_reply_serial() failed.");
184 goto unref_reply;
187 return true;
189 unref_reply:
190 dbus_message_unref(call_ptr->reply);
191 call_ptr->reply = NULL;
193 fail:
194 /* Even after an error we need to return true, because the
195 handler is only supposed to return false if a nonexistent
196 method is requested. */
197 return true;
200 METHOD_ARGS_BEGIN(Introspect, "Get introspection XML")
201 METHOD_ARG_DESCRIBE_OUT("xml_data", "s", "XML description of the object")
202 METHOD_ARGS_END
204 METHODS_BEGIN
205 METHOD_DESCRIBE(Introspect, NULL)
206 METHODS_END
208 INTERFACE_BEGIN(g_dbus_interface_dtor_introspectable, "org.freedesktop.DBus.Introspectable")
209 INTERFACE_HANDLER(introspection_handler)
210 INTERFACE_EXPOSE_METHODS
211 INTERFACE_END
213 dbus_object_path dbus_object_path_new(const char *name, const struct dbus_interface_descriptor * iface1_ptr, ...)
215 struct dbus_object_path * opath_ptr;
216 va_list ap;
217 const struct dbus_interface_descriptor * iface_src_ptr;
218 struct dbus_object_path_interface * iface_dst_ptr;
219 void * iface_context;
220 size_t len;
222 log_debug("Creating object path");
224 opath_ptr = malloc(sizeof(struct dbus_object_path));
225 if (opath_ptr == NULL)
227 log_error("malloc() failed to allocate struct dbus_object_path.");
228 goto fail;
231 opath_ptr->name = strdup(name);
232 if (opath_ptr->name == NULL)
234 log_error("malloc() failed to allocate struct dbus_object_path.");
235 goto free;
238 va_start(ap, iface1_ptr);
239 iface_src_ptr = iface1_ptr;
240 len = 0;
241 while (iface_src_ptr != NULL)
243 iface_context = va_arg(ap, void *);
244 iface_src_ptr = va_arg(ap, const struct dbus_interface_descriptor *);
245 len++;
247 va_end(ap);
249 opath_ptr->ifaces = malloc((len + 2) * sizeof(struct dbus_object_path_interface));
250 if (opath_ptr->ifaces == NULL)
252 log_error("malloc failed to allocate interfaces array");
253 goto free_name;
256 va_start(ap, iface1_ptr);
257 iface_src_ptr = iface1_ptr;
258 iface_dst_ptr = opath_ptr->ifaces;
259 while (iface_src_ptr != NULL)
261 iface_dst_ptr->iface = iface_src_ptr;
262 iface_dst_ptr->iface_context = va_arg(ap, void *);
263 iface_src_ptr = va_arg(ap, const struct dbus_interface_descriptor *);
264 iface_dst_ptr++;
265 len--;
267 va_end(ap);
269 ASSERT(len == 0);
271 iface_dst_ptr->iface = NULL;
272 opath_ptr->introspection = introspection_new(opath_ptr);
273 if (opath_ptr->introspection == NULL)
275 log_error("introspection_new() failed.");
276 goto free_ifaces;
279 iface_dst_ptr->iface = &g_dbus_interface_dtor_introspectable;
280 iface_dst_ptr->iface_context = opath_ptr->introspection;
281 iface_dst_ptr++;
282 iface_dst_ptr->iface = NULL;
284 return (dbus_object_path)opath_ptr;
286 free_ifaces:
287 free(opath_ptr->ifaces);
288 free_name:
289 free(opath_ptr->name);
290 free:
291 free(opath_ptr);
292 fail:
293 return NULL;
296 #define opath_ptr ((struct dbus_object_path *)data)
298 void dbus_object_path_destroy(DBusConnection * connection_ptr, dbus_object_path data)
300 log_debug("Destroying object path");
302 if (connection_ptr != NULL && !dbus_connection_unregister_object_path(connection_ptr, opath_ptr->name))
304 log_error("dbus_connection_unregister_object_path() failed.");
307 introspection_destroy(opath_ptr);
308 free(opath_ptr->ifaces);
309 free(opath_ptr->name);
310 free(opath_ptr);
313 static DBusHandlerResult dbus_object_path_handler(DBusConnection * connection, DBusMessage * message, void * data)
315 const char * iface_name;
316 const struct dbus_object_path_interface * iface_ptr;
317 struct dbus_method_call call;
319 /* Check if the message is a method call. If not, ignore it. */
320 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
322 goto handled;
325 /* Get the invoked method's name and make sure it's non-NULL. */
326 call.method_name = dbus_message_get_member(message);
327 if (call.method_name == NULL)
329 lash_dbus_error(&call, LASH_DBUS_ERROR_UNKNOWN_METHOD, "Received method call with empty method name");
330 goto send_return;
333 /* Initialize our data. */
334 call.connection = connection;
335 call.message = message;
336 call.iface = NULL; /* To be set by the default interface handler */
337 call.reply = NULL;
339 /* Check if there's an interface specified for this method call. */
340 iface_name = dbus_message_get_interface(message);
341 if (iface_name != NULL)
343 for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++)
345 if (strcmp(iface_name, iface_ptr->iface->name) == 0)
347 call.iface_context = iface_ptr->iface_context;
348 if (!iface_ptr->iface->handler(iface_ptr->iface, &call))
350 /* unknown method */
351 break;
354 goto send_return;
358 else
360 /* No interface was specified so we have to try them all. D-Bus spec states:
362 * Optionally, the message has an INTERFACE field giving the interface the method is a part of.
363 * In the absence of an INTERFACE field, if two interfaces on the same object have a method with
364 * the same name, it is undefined which of the two methods will be invoked.
365 * Implementations may also choose to return an error in this ambiguous case.
366 * However, if a method name is unique implementations must not require an interface field.
368 for (iface_ptr = opath_ptr->ifaces; iface_ptr->iface != NULL; iface_ptr++)
370 call.iface_context = iface_ptr->iface_context;
371 if (!iface_ptr->iface->handler(iface_ptr->iface, &call))
373 /* known method */
374 goto send_return;
379 lash_dbus_error(&call, LASH_DBUS_ERROR_UNKNOWN_METHOD, "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist", call.method_name, dbus_message_get_signature(message), iface_name);
381 send_return:
382 method_return_send(&call);
384 handled:
385 return DBUS_HANDLER_RESULT_HANDLED;
388 static void dbus_object_path_handler_unregister(DBusConnection * connection_ptr, void * data)
390 #ifdef LADISH_DEBUG
391 log_debug("Message handler of object path %s was unregistered", (opath_ptr && path->name) ? opath_ptr->name : "<unknown>");
392 #endif /* LADISH_DEBUG */
395 bool dbus_object_path_register(DBusConnection * connection_ptr, dbus_object_path data)
397 log_debug("Registering object path");
399 DBusObjectPathVTable vtable =
401 dbus_object_path_handler_unregister,
402 dbus_object_path_handler,
403 NULL, NULL, NULL, NULL
406 dbus_connection_register_object_path(connection_ptr, opath_ptr->name, &vtable, opath_ptr);
408 return true;
411 #undef opath_ptr