2 #include "e_dbus_private.h"
3 #include <Ecore_Data.h>
8 static E_DBus_Interface *introspectable_interface = NULL;
9 static E_DBus_Interface *properties_interface = NULL;
11 typedef struct E_DBus_Method E_DBus_Method;
13 Ecore_Strbuf * e_dbus_object_introspect(E_DBus_Object *obj);
15 static void e_dbus_object_unregister(DBusConnection *conn, void *user_data);
16 static DBusHandlerResult e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data);
18 static void e_dbus_interface_free(E_DBus_Interface *iface);
20 static E_DBus_Method *e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func);
21 static void e_dbus_object_method_free(E_DBus_Method *m);
23 static void _introspect_indent_append(Ecore_Strbuf *buf, int level);
24 static void _introspect_interface_append(Ecore_Strbuf *buf, E_DBus_Interface *iface, int level);
25 static void _introspect_method_append(Ecore_Strbuf *buf, E_DBus_Method *method, int level);
26 static void _introspect_arg_append(Ecore_Strbuf *buf, const char *type, const char *direction, int level);
29 //static Eina_List *standard_methods = NULL;
32 static DBusObjectPathVTable vtable = {
33 e_dbus_object_unregister,
34 e_dbus_object_handler,
43 E_DBus_Connection *conn;
45 Eina_List *interfaces;
46 char *introspection_data;
47 int introspection_dirty;
49 E_DBus_Object_Property_Get_Cb cb_property_get;
50 E_DBus_Object_Property_Set_Cb cb_property_set;
55 struct E_DBus_Interface
66 char *reply_signature;
67 E_DBus_Method_Cb func;
71 cb_introspect(E_DBus_Object *obj, DBusMessage *msg)
76 if (obj->introspection_dirty || !obj->introspection_data)
78 buf = e_dbus_object_introspect(obj);
81 ret = dbus_message_new_error(msg, "org.enlightenment.NotIntrospectable", "This object does not provide introspection data");
85 if (obj->introspection_data) free(obj->introspection_data);
86 obj->introspection_data = strdup(ecore_strbuf_string_get(buf));
87 ecore_strbuf_free(buf);
89 //printf("XML: \n\n%s\n\n", obj->introspection_data);
90 ret = dbus_message_new_method_return(msg);
91 dbus_message_append_args(ret, DBUS_TYPE_STRING, &(obj->introspection_data), DBUS_TYPE_INVALID);
97 cb_properties_get(E_DBus_Object *obj, DBusMessage *msg)
100 DBusMessageIter iter, sub;
106 dbus_error_init(&err);
107 dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
109 if (dbus_error_is_set(&err))
111 return dbus_message_new_error(msg, err.name, err.message);
114 obj->cb_property_get(obj, property, &type, &value);
115 if (type == DBUS_TYPE_INVALID)
117 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
120 if (dbus_type_is_basic(type))
122 reply = dbus_message_new_method_return(msg);
123 dbus_message_iter_init_append(msg, &iter);
124 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, e_dbus_basic_type_as_string(type), &sub);
125 dbus_message_iter_append_basic(&sub, type, &value);
126 dbus_message_iter_close_container(&iter, &sub);
131 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
136 cb_properties_set(E_DBus_Object *obj, DBusMessage *msg)
138 DBusMessageIter iter, sub;
143 dbus_message_iter_init(msg, &iter);
144 dbus_message_iter_get_basic(&iter, &property);
145 dbus_message_iter_recurse(&iter, &sub);
146 type = dbus_message_iter_get_arg_type(&sub);
147 if (dbus_type_is_basic(type))
149 dbus_message_iter_get_basic(&sub, &value);
150 if (obj->cb_property_set(obj, property, type, value))
152 return dbus_message_new_method_return(msg);
156 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
161 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
167 e_dbus_object_init(void)
169 introspectable_interface = e_dbus_interface_new("org.freedesktop.DBus.Introspectable");
170 properties_interface = e_dbus_interface_new("org.freedesktop.DBus.Properties");
171 if (!introspectable_interface || !properties_interface)
173 if (introspectable_interface) e_dbus_interface_unref(introspectable_interface);
174 introspectable_interface = NULL;
175 if (properties_interface) e_dbus_interface_unref(properties_interface);
176 properties_interface = NULL;
180 e_dbus_interface_method_add(introspectable_interface, "Introspect", "", "s", cb_introspect);
181 e_dbus_interface_method_add(properties_interface, "Get", "s", "v", cb_properties_get);
182 e_dbus_interface_method_add(properties_interface, "Set", "sv", "", cb_properties_set);
187 e_dbus_object_shutdown(void)
189 e_dbus_interface_unref(introspectable_interface);
190 introspectable_interface = NULL;
192 e_dbus_interface_unref(properties_interface);
193 properties_interface = NULL;
199 * @param conn the connection on with the object should listen
200 * @param object_path a unique string identifying an object (e.g. org/enlightenment/WindowManager
201 * @param data custom data to set on the object (retrievable via
202 * e_dbus_object_data_get())
205 e_dbus_object_add(E_DBus_Connection *conn, const char *object_path, void *data)
209 obj = calloc(1, sizeof(E_DBus_Object));
210 if (!obj) return NULL;
212 if (!dbus_connection_register_object_path(conn->conn, object_path, &vtable, obj))
219 e_dbus_connection_ref(conn);
220 obj->path = strdup(object_path);
222 obj->interfaces = NULL;
224 e_dbus_object_interface_attach(obj, introspectable_interface);
232 * @param obj the object to free
235 e_dbus_object_free(E_DBus_Object *obj)
237 E_DBus_Interface *iface;
241 DEBUG(5, "e_dbus_object_free (%s)\n", obj->path);
242 dbus_connection_unregister_object_path(obj->conn->conn, obj->path);
243 e_dbus_connection_close(obj->conn);
245 if (obj->path) free(obj->path);
246 while (obj->interfaces)
248 iface = eina_list_data_get(obj->interfaces);
249 e_dbus_interface_unref(iface);
250 obj->interfaces = eina_list_remove_list(obj->interfaces, obj->interfaces);
252 if (obj->introspection_data) free(obj->introspection_data);
258 * @brief Fetch the data pointer for a dbus object
259 * @param obj the dbus object
262 e_dbus_object_data_get(E_DBus_Object *obj)
268 * @brief Sets the callback to fetch properties from an object
269 * @param obj the object
270 * @param func the callback
273 e_dbus_object_property_get_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Get_Cb func)
275 obj->cb_property_get = func;
279 * @brief Sets the callback to set properties on an object
280 * @param obj the object
281 * @param func the callback
284 e_dbus_object_property_set_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Set_Cb func)
286 obj->cb_property_set = func;
290 e_dbus_object_interface_attach(E_DBus_Object *obj, E_DBus_Interface *iface)
292 e_dbus_interface_ref(iface);
293 obj->interfaces = eina_list_append(obj->interfaces, iface);
294 obj->introspection_dirty = 1;
295 DEBUG(4, "e_dbus_object_interface_attach (%s, %s) ", obj->path, iface->name);
299 e_dbus_object_interface_detach(E_DBus_Object *obj, E_DBus_Interface *iface)
301 E_DBus_Interface *found;
303 DEBUG(4, "e_dbus_object_interface_detach (%s, %s) ", obj->path, iface->name);
304 found = eina_list_data_find(obj->interfaces, iface);
305 if (found == NULL) return;
307 obj->interfaces = eina_list_remove(obj->interfaces, iface);
308 obj->introspection_dirty = 1;
309 e_dbus_interface_unref(iface);
313 e_dbus_interface_ref(E_DBus_Interface *iface)
316 DEBUG(4, "e_dbus_interface_ref (%s) = %d\n", iface->name, iface->refcount);
320 e_dbus_interface_unref(E_DBus_Interface *iface)
322 DEBUG(4, "e_dbus_interface_unref (%s) = %d\n", iface->name, iface->refcount - 1);
323 if (--(iface->refcount) == 0)
324 e_dbus_interface_free(iface);
328 e_dbus_interface_free(E_DBus_Interface *iface)
332 if (iface->name) free(iface->name);
333 while (iface->methods)
335 m = eina_list_data_get(iface->methods);
336 e_dbus_object_method_free(m);
337 iface->methods = eina_list_remove_list(iface->methods, iface->methods);
344 * Add a method to an object
346 * @param iface the E_DBus_Interface to which this method belongs
347 * @param member the name of the method
348 * @param signature an optional message signature. if provided, then messages
349 * with invalid signatures will be automatically rejected
350 * (an Error response will be sent) and introspection data
353 * @return 1 if successful, 0 if failed (e.g. no memory)
356 e_dbus_interface_method_add(E_DBus_Interface *iface, const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
360 m = e_dbus_method_new(member, signature, reply_signature, func);
361 DEBUG(4, "Add method %s: %p\n", member, m);
364 iface->methods = eina_list_append(iface->methods, m);
368 EAPI E_DBus_Interface *
369 e_dbus_interface_new(const char *interface)
371 E_DBus_Interface *iface;
373 if (!interface) return NULL;
375 iface = calloc(1, sizeof(E_DBus_Interface));
376 if (!iface) return NULL;
379 iface->name = strdup(interface);
380 iface->methods = NULL;
385 static E_DBus_Method *
386 e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
390 if (!member || !func) return NULL;
392 if (signature && !dbus_signature_validate(signature, NULL)) return NULL;
393 if (reply_signature && !dbus_signature_validate(reply_signature, NULL)) return NULL;
394 m = calloc(1, sizeof(E_DBus_Method));
397 m->member = strdup(member);
399 m->signature = strdup(signature);
401 m->reply_signature = strdup(reply_signature);
408 e_dbus_object_method_free(E_DBus_Method *m)
411 if (m->member) free(m->member);
412 if (m->signature) free(m->signature);
413 if (m->reply_signature) free(m->reply_signature);
418 static E_DBus_Method *
419 e_dbus_object_method_find(E_DBus_Object *obj, const char *interface, const char *member)
422 E_DBus_Interface *iface;
425 if (!obj || !member) return NULL;
427 EINA_LIST_FOREACH(obj->interfaces, l, iface)
429 if (strcmp(interface, iface->name)) continue;
430 EINA_LIST_FOREACH(iface->methods, ll, m)
432 if (!strcmp(member, m->member))
439 static DBusHandlerResult
440 e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data)
445 dbus_uint32_t serial;
449 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
451 m = e_dbus_object_method_find(obj, dbus_message_get_interface(message), dbus_message_get_member(message));
453 /* XXX should this send an 'invalid method' error instead? */
455 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
457 if (m->signature && !dbus_message_has_signature(message, m->signature))
458 reply = dbus_message_new_error_printf(message, "org.enlightenment.InvalidSignature", "Expected signature: %s", m->signature);
460 reply = m->func(obj, message);
462 dbus_connection_send(conn, reply, &serial);
463 dbus_message_unref(reply);
465 return DBUS_HANDLER_RESULT_HANDLED;
469 e_dbus_object_unregister(DBusConnection *conn, void *user_data)
471 /* free up the object struct? */
475 e_dbus_object_introspect(E_DBus_Object *obj)
479 E_DBus_Interface *iface;
482 buf = ecore_strbuf_new();
485 ecore_strbuf_append(buf, "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n");
487 ecore_strbuf_append(buf, "<node name=\"");
488 ecore_strbuf_append(buf, obj->path);
489 ecore_strbuf_append(buf, "\">\n");
492 EINA_LIST_FOREACH(obj->interfaces, l, iface)
493 _introspect_interface_append(buf, iface, level);
495 ecore_strbuf_append(buf, "</node>\n");
500 _introspect_indent_append(Ecore_Strbuf *buf, int level)
502 /* XXX optimize this? */
505 ecore_strbuf_append_char(buf, ' ');
508 _introspect_interface_append(Ecore_Strbuf *buf, E_DBus_Interface *iface, int level)
510 E_DBus_Method *method;
513 _introspect_indent_append(buf, level);
514 ecore_strbuf_append(buf, "<interface name=\"");
515 ecore_strbuf_append(buf, iface->name);
516 ecore_strbuf_append(buf, "\">\n");
519 DEBUG(4, "introspect iface: %s\n", iface->name);
520 EINA_LIST_FOREACH(iface->methods, l, method)
521 _introspect_method_append(buf, method, level);
524 _introspect_indent_append(buf, level);
525 ecore_strbuf_append(buf, "</interface>\n");
528 _introspect_method_append(Ecore_Strbuf *buf, E_DBus_Method *method, int level)
530 DBusSignatureIter iter;
533 _introspect_indent_append(buf, level);
534 DEBUG(4, "introspect method: %s\n", method->member);
535 ecore_strbuf_append(buf, "<method name=\"");
536 ecore_strbuf_append(buf, method->member);
537 ecore_strbuf_append(buf, "\">\n");
541 if (method->signature &&
542 method->signature[0] &&
543 dbus_signature_validate(method->signature, NULL))
545 dbus_signature_iter_init(&iter, method->signature);
546 while ((type = dbus_signature_iter_get_signature(&iter)))
548 _introspect_arg_append(buf, type, "in", level);
551 if (!dbus_signature_iter_next(&iter)) break;
555 /* append reply args */
556 if (method->reply_signature &&
557 method->reply_signature[0] &&
558 dbus_signature_validate(method->reply_signature, NULL))
560 dbus_signature_iter_init(&iter, method->reply_signature);
561 while ((type = dbus_signature_iter_get_signature(&iter)))
563 _introspect_arg_append(buf, type, "out", level);
566 if (!dbus_signature_iter_next(&iter)) break;
571 _introspect_indent_append(buf, level);
572 ecore_strbuf_append(buf, "</method>\n");
576 _introspect_arg_append(Ecore_Strbuf *buf, const char *type, const char *direction, int level)
578 _introspect_indent_append(buf, level);
579 ecore_strbuf_append(buf, "<arg type=\"");
580 ecore_strbuf_append(buf, type);
581 ecore_strbuf_append(buf, "\" direction=\"");
582 ecore_strbuf_append(buf, direction);
583 ecore_strbuf_append(buf, "\"/>\n");