2 #include "e_dbus_private.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;
12 typedef struct E_DBus_Signal E_DBus_Signal;
14 Ecore_Strbuf * e_dbus_object_introspect(E_DBus_Object *obj);
16 static void e_dbus_object_unregister(DBusConnection *conn, void *user_data);
17 static DBusHandlerResult e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data);
19 static void e_dbus_interface_free(E_DBus_Interface *iface);
21 static E_DBus_Method *e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func);
22 static void e_dbus_object_method_free(E_DBus_Method *m);
24 static E_DBus_Signal *e_dbus_signal_new(const char *name, const char *signature);
25 static void e_dbus_object_signal_free(E_DBus_Signal *s);
27 static void _introspect_indent_append(Ecore_Strbuf *buf, int level);
28 static void _introspect_interface_append(Ecore_Strbuf *buf, E_DBus_Interface *iface, int level);
29 static void _introspect_method_append(Ecore_Strbuf *buf, E_DBus_Method *method, int level);
30 static void _introspect_signal_append(Ecore_Strbuf *buf, E_DBus_Signal *signal, int level);
31 static void _introspect_arg_append(Ecore_Strbuf *buf, const char *type, const char *direction, int level);
34 //static Eina_List *standard_methods = NULL;
37 static DBusObjectPathVTable vtable = {
38 e_dbus_object_unregister,
39 e_dbus_object_handler,
48 E_DBus_Connection *conn;
50 Eina_List *interfaces;
51 char *introspection_data;
52 int introspection_dirty;
54 E_DBus_Object_Property_Get_Cb cb_property_get;
55 E_DBus_Object_Property_Set_Cb cb_property_set;
60 struct E_DBus_Interface
72 char *reply_signature;
73 E_DBus_Method_Cb func;
83 cb_introspect(E_DBus_Object *obj, DBusMessage *msg)
88 if (obj->introspection_dirty || !obj->introspection_data)
90 buf = e_dbus_object_introspect(obj);
93 ret = dbus_message_new_error(msg, "org.enlightenment.NotIntrospectable", "This object does not provide introspection data");
97 if (obj->introspection_data) free(obj->introspection_data);
98 obj->introspection_data = strdup(ecore_strbuf_string_get(buf));
99 ecore_strbuf_free(buf);
101 //printf("XML: \n\n%s\n\n", obj->introspection_data);
102 ret = dbus_message_new_method_return(msg);
103 dbus_message_append_args(ret, DBUS_TYPE_STRING, &(obj->introspection_data), DBUS_TYPE_INVALID);
109 cb_properties_get(E_DBus_Object *obj, DBusMessage *msg)
112 DBusMessageIter iter, sub;
118 dbus_error_init(&err);
119 dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
121 if (dbus_error_is_set(&err))
123 return dbus_message_new_error(msg, err.name, err.message);
126 obj->cb_property_get(obj, property, &type, &value);
127 if (type == DBUS_TYPE_INVALID)
129 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
132 if (dbus_type_is_basic(type))
134 reply = dbus_message_new_method_return(msg);
135 dbus_message_iter_init_append(msg, &iter);
136 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, e_dbus_basic_type_as_string(type), &sub);
137 dbus_message_iter_append_basic(&sub, type, &value);
138 dbus_message_iter_close_container(&iter, &sub);
143 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
148 cb_properties_set(E_DBus_Object *obj, DBusMessage *msg)
150 DBusMessageIter iter, sub;
155 dbus_message_iter_init(msg, &iter);
156 dbus_message_iter_get_basic(&iter, &property);
157 dbus_message_iter_recurse(&iter, &sub);
158 type = dbus_message_iter_get_arg_type(&sub);
159 if (dbus_type_is_basic(type))
161 dbus_message_iter_get_basic(&sub, &value);
162 if (obj->cb_property_set(obj, property, type, value))
164 return dbus_message_new_method_return(msg);
168 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
173 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
179 e_dbus_object_init(void)
181 introspectable_interface = e_dbus_interface_new("org.freedesktop.DBus.Introspectable");
182 properties_interface = e_dbus_interface_new("org.freedesktop.DBus.Properties");
183 if (!introspectable_interface || !properties_interface)
185 if (introspectable_interface) e_dbus_interface_unref(introspectable_interface);
186 introspectable_interface = NULL;
187 if (properties_interface) e_dbus_interface_unref(properties_interface);
188 properties_interface = NULL;
192 e_dbus_interface_method_add(introspectable_interface, "Introspect", "", "s", cb_introspect);
193 e_dbus_interface_method_add(properties_interface, "Get", "s", "v", cb_properties_get);
194 e_dbus_interface_method_add(properties_interface, "Set", "sv", "", cb_properties_set);
199 e_dbus_object_shutdown(void)
201 e_dbus_interface_unref(introspectable_interface);
202 introspectable_interface = NULL;
204 e_dbus_interface_unref(properties_interface);
205 properties_interface = NULL;
211 * @param conn the connection on with the object should listen
212 * @param object_path a unique string identifying an object (e.g. org/enlightenment/WindowManager
213 * @param data custom data to set on the object (retrievable via
214 * e_dbus_object_data_get())
217 e_dbus_object_add(E_DBus_Connection *conn, const char *object_path, void *data)
221 obj = calloc(1, sizeof(E_DBus_Object));
222 if (!obj) return NULL;
224 if (!dbus_connection_register_object_path(conn->conn, object_path, &vtable, obj))
231 e_dbus_connection_ref(conn);
232 obj->path = strdup(object_path);
234 obj->interfaces = NULL;
236 e_dbus_object_interface_attach(obj, introspectable_interface);
244 * @param obj the object to free
247 e_dbus_object_free(E_DBus_Object *obj)
249 E_DBus_Interface *iface;
253 E_DBUS_LOG_DBG("e_dbus_object_free (%s)", obj->path);
254 dbus_connection_unregister_object_path(obj->conn->conn, obj->path);
255 e_dbus_connection_close(obj->conn);
257 if (obj->path) free(obj->path);
258 EINA_LIST_FREE(obj->interfaces, iface)
259 e_dbus_interface_unref(iface);
261 if (obj->introspection_data) free(obj->introspection_data);
267 * @brief Fetch the data pointer for a dbus object
268 * @param obj the dbus object
271 e_dbus_object_data_get(E_DBus_Object *obj)
277 * @brief Get the dbus connection of a dbus object
278 * @param obj the dbus object
280 EAPI E_DBus_Connection *
281 e_dbus_object_conn_get(E_DBus_Object *obj)
287 * @brief Get the path of a dbus object
288 * @param obj the dbus object
291 e_dbus_object_path_get(E_DBus_Object *obj)
297 * @brief Get the interfaces of a dbus object
298 * @param obj the dbus object
300 EAPI const Eina_List *
301 e_dbus_object_interfaces_get(E_DBus_Object *obj)
303 return obj->interfaces;
307 * @brief Sets the callback to fetch properties from an object
308 * @param obj the object
309 * @param func the callback
312 e_dbus_object_property_get_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Get_Cb func)
314 obj->cb_property_get = func;
318 * @brief Sets the callback to set properties on an object
319 * @param obj the object
320 * @param func the callback
323 e_dbus_object_property_set_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Set_Cb func)
325 obj->cb_property_set = func;
329 e_dbus_object_interface_attach(E_DBus_Object *obj, E_DBus_Interface *iface)
331 e_dbus_interface_ref(iface);
332 obj->interfaces = eina_list_append(obj->interfaces, iface);
333 obj->introspection_dirty = 1;
334 E_DBUS_LOG_DBG("e_dbus_object_interface_attach (%s, %s) ", obj->path, iface->name);
338 e_dbus_object_interface_detach(E_DBus_Object *obj, E_DBus_Interface *iface)
340 E_DBus_Interface *found;
342 E_DBUS_LOG_DBG("e_dbus_object_interface_detach (%s, %s) ", obj->path, iface->name);
343 found = eina_list_data_find(obj->interfaces, iface);
344 if (found == NULL) return;
346 obj->interfaces = eina_list_remove(obj->interfaces, iface);
347 obj->introspection_dirty = 1;
348 e_dbus_interface_unref(iface);
352 e_dbus_interface_ref(E_DBus_Interface *iface)
355 E_DBUS_LOG_DBG("e_dbus_interface_ref (%s) = %d", iface->name, iface->refcount);
359 e_dbus_interface_unref(E_DBus_Interface *iface)
361 E_DBUS_LOG_DBG("e_dbus_interface_unref (%s) = %d", iface->name, iface->refcount - 1);
362 if (--(iface->refcount) == 0)
363 e_dbus_interface_free(iface);
367 e_dbus_interface_free(E_DBus_Interface *iface)
372 if (iface->name) free(iface->name);
373 EINA_LIST_FREE(iface->methods, m)
374 e_dbus_object_method_free(m);
375 EINA_LIST_FREE(iface->signals, s)
376 e_dbus_object_signal_free(s);
382 * Add a method to an object
384 * @param iface the E_DBus_Interface to which this method belongs
385 * @param member the name of the method
386 * @param signature an optional message signature. if provided, then messages
387 * with invalid signatures will be automatically rejected
388 * (an Error response will be sent) and introspection data
391 * @return 1 if successful, 0 if failed (e.g. no memory)
394 e_dbus_interface_method_add(E_DBus_Interface *iface, const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
398 m = e_dbus_method_new(member, signature, reply_signature, func);
399 E_DBUS_LOG_DBG("E-dbus: Add method %s: %p", member, m);
402 iface->methods = eina_list_append(iface->methods, m);
407 * Add a signal to an object
409 * @param iface the E_DBus_Interface to which this signal belongs
410 * @param name the name of the signal
411 * @param signature an optional message signature.
413 * @return 1 if successful, 0 if failed (e.g. no memory)
416 e_dbus_interface_signal_add(E_DBus_Interface *iface, const char *name, const char *signature)
420 s = e_dbus_signal_new(name, signature);
421 E_DBUS_LOG_DBG("E-dbus: Add signal %s: %p", name, s);
424 iface->signals = eina_list_append(iface->signals, s);
428 EAPI E_DBus_Interface *
429 e_dbus_interface_new(const char *interface)
431 E_DBus_Interface *iface;
433 if (!interface) return NULL;
435 iface = calloc(1, sizeof(E_DBus_Interface));
436 if (!iface) return NULL;
439 iface->name = strdup(interface);
440 iface->methods = NULL;
441 iface->signals = NULL;
446 static E_DBus_Method *
447 e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
451 if (!member || !func) return NULL;
453 if (signature && !dbus_signature_validate(signature, NULL)) return NULL;
454 if (reply_signature && !dbus_signature_validate(reply_signature, NULL)) return NULL;
455 m = calloc(1, sizeof(E_DBus_Method));
458 m->member = strdup(member);
460 m->signature = strdup(signature);
462 m->reply_signature = strdup(reply_signature);
469 e_dbus_object_method_free(E_DBus_Method *m)
472 if (m->member) free(m->member);
473 if (m->signature) free(m->signature);
474 if (m->reply_signature) free(m->reply_signature);
479 static E_DBus_Signal *
480 e_dbus_signal_new(const char *name, const char *signature)
484 if (!name) return NULL;
486 if (signature && !dbus_signature_validate(signature, NULL)) return NULL;
487 s = calloc(1, sizeof(E_DBus_Signal));
490 s->name = strdup(name);
492 s->signature = strdup(signature);
498 e_dbus_object_signal_free(E_DBus_Signal *s)
501 if (s->name) free(s->name);
502 if (s->signature) free(s->signature);
506 static E_DBus_Method *
507 e_dbus_object_method_find(E_DBus_Object *obj, const char *interface, const char *member)
510 E_DBus_Interface *iface;
513 if (!obj || !member) return NULL;
515 EINA_LIST_FOREACH(obj->interfaces, l, iface)
517 if (strcmp(interface, iface->name)) continue;
518 EINA_LIST_FOREACH(iface->methods, ll, m)
520 if (!strcmp(member, m->member))
527 static DBusHandlerResult
528 e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data)
533 dbus_uint32_t serial;
537 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
539 m = e_dbus_object_method_find(obj, dbus_message_get_interface(message), dbus_message_get_member(message));
541 /* XXX should this send an 'invalid method' error instead? */
543 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545 if (m->signature && !dbus_message_has_signature(message, m->signature))
546 reply = dbus_message_new_error_printf(message, "org.enlightenment.InvalidSignature", "Expected signature: %s", m->signature);
548 reply = m->func(obj, message);
550 dbus_connection_send(conn, reply, &serial);
551 dbus_message_unref(reply);
553 return DBUS_HANDLER_RESULT_HANDLED;
557 e_dbus_object_unregister(DBusConnection *conn, void *user_data)
559 /* free up the object struct? */
563 e_dbus_object_introspect(E_DBus_Object *obj)
567 E_DBus_Interface *iface;
570 buf = ecore_strbuf_new();
573 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");
575 ecore_strbuf_append(buf, "<node name=\"");
576 ecore_strbuf_append(buf, obj->path);
577 ecore_strbuf_append(buf, "\">\n");
580 EINA_LIST_FOREACH(obj->interfaces, l, iface)
581 _introspect_interface_append(buf, iface, level);
583 ecore_strbuf_append(buf, "</node>\n");
588 _introspect_indent_append(Ecore_Strbuf *buf, int level)
590 /* XXX optimize this? */
593 ecore_strbuf_append_char(buf, ' ');
596 _introspect_interface_append(Ecore_Strbuf *buf, E_DBus_Interface *iface, int level)
598 E_DBus_Method *method;
599 E_DBus_Signal *signal;
602 _introspect_indent_append(buf, level);
603 ecore_strbuf_append(buf, "<interface name=\"");
604 ecore_strbuf_append(buf, iface->name);
605 ecore_strbuf_append(buf, "\">\n");
608 E_DBUS_LOG_DBG("introspect iface: %s", iface->name);
609 EINA_LIST_FOREACH(iface->methods, l, method)
610 _introspect_method_append(buf, method, level);
611 EINA_LIST_FOREACH(iface->signals, l, signal)
612 _introspect_signal_append(buf, signal, level);
615 _introspect_indent_append(buf, level);
616 ecore_strbuf_append(buf, "</interface>\n");
619 _introspect_method_append(Ecore_Strbuf *buf, E_DBus_Method *method, int level)
621 DBusSignatureIter iter;
624 _introspect_indent_append(buf, level);
625 E_DBUS_LOG_DBG("introspect method: %s\n", method->member);
626 ecore_strbuf_append(buf, "<method name=\"");
627 ecore_strbuf_append(buf, method->member);
628 ecore_strbuf_append(buf, "\">\n");
632 if (method->signature &&
633 method->signature[0] &&
634 dbus_signature_validate(method->signature, NULL))
636 dbus_signature_iter_init(&iter, method->signature);
637 while ((type = dbus_signature_iter_get_signature(&iter)))
639 _introspect_arg_append(buf, type, "in", level);
642 if (!dbus_signature_iter_next(&iter)) break;
646 /* append reply args */
647 if (method->reply_signature &&
648 method->reply_signature[0] &&
649 dbus_signature_validate(method->reply_signature, NULL))
651 dbus_signature_iter_init(&iter, method->reply_signature);
652 while ((type = dbus_signature_iter_get_signature(&iter)))
654 _introspect_arg_append(buf, type, "out", level);
657 if (!dbus_signature_iter_next(&iter)) break;
662 _introspect_indent_append(buf, level);
663 ecore_strbuf_append(buf, "</method>\n");
667 _introspect_signal_append(Ecore_Strbuf *buf, E_DBus_Signal *signal, int level)
669 DBusSignatureIter iter;
672 _introspect_indent_append(buf, level);
673 E_DBUS_LOG_DBG("introspect signal: %s", signal->name);
674 ecore_strbuf_append(buf, "<signal name=\"");
675 ecore_strbuf_append(buf, signal->name);
676 ecore_strbuf_append(buf, "\">\n");
680 if (signal->signature &&
681 signal->signature[0] &&
682 dbus_signature_validate(signal->signature, NULL))
684 dbus_signature_iter_init(&iter, signal->signature);
685 while ((type = dbus_signature_iter_get_signature(&iter)))
687 _introspect_arg_append(buf, type, NULL, level);
690 if (!dbus_signature_iter_next(&iter)) break;
695 _introspect_indent_append(buf, level);
696 ecore_strbuf_append(buf, "</signal>\n");
700 _introspect_arg_append(Ecore_Strbuf *buf, const char *type, const char *direction, int level)
702 _introspect_indent_append(buf, level);
703 ecore_strbuf_append(buf, "<arg type=\"");
704 ecore_strbuf_append(buf, type);
707 ecore_strbuf_append(buf, "\" direction=\"");
708 ecore_strbuf_append(buf, direction);
710 ecore_strbuf_append(buf, "\"/>\n");